use of org.gudy.bouncycastle.asn1.pkcs.PrivateKeyInfo in project android_packages_apps_Settings by DirtyUnicorns.
the class CredentialStorage method isHardwareBackedKey.
private boolean isHardwareBackedKey(byte[] keyData) {
try {
ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(keyData));
PrivateKeyInfo pki = PrivateKeyInfo.getInstance(bIn.readObject());
String algOid = pki.getAlgorithmId().getAlgorithm().getId();
String algName = new AlgorithmId(new ObjectIdentifier(algOid)).getName();
return KeyChain.isBoundKeyAlgorithm(algName);
} catch (IOException e) {
Log.e(TAG, "Failed to parse key data");
return false;
}
}
use of org.gudy.bouncycastle.asn1.pkcs.PrivateKeyInfo in project runwar by cfmlprojects.
the class SSLUtil method loadPemPrivateKey.
private static PrivateKey loadPemPrivateKey(final File file, final char[] passphrase) throws Exception {
Security.addProvider(new BouncyCastleProvider());
final PEMParser pemParser = new PEMParser(new BufferedReader(new FileReader(file)));
final PEMDecryptorProvider build = new JcePEMDecryptorProviderBuilder().build(passphrase);
final JcaPEMKeyConverter jcaPEMKeyConverter = new JcaPEMKeyConverter();
PrivateKey privateKey;
try {
final Object object = pemParser.readObject();
if (object instanceof PEMEncryptedKeyPair) {
privateKey = jcaPEMKeyConverter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(build)).getPrivate();
} else {
PrivateKeyInfo privateKeyInfo;
if (object instanceof PEMKeyPair) {
privateKeyInfo = ((PEMKeyPair) object).getPrivateKeyInfo();
} else {
privateKeyInfo = (PrivateKeyInfo) object;
}
if (privateKeyInfo != null) {
privateKey = new JcaPEMKeyConverter().getPrivateKey(privateKeyInfo);
} else {
final KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
final FileInputStream fileInputStream = new FileInputStream(file);
keyStore.load(fileInputStream, passphrase);
fileInputStream.close();
final Enumeration<String> aliases = keyStore.aliases();
String alias = "";
while (aliases.hasMoreElements()) {
alias = aliases.nextElement();
}
privateKey = (PrivateKey) keyStore.getKey(alias, passphrase);
}
}
} finally {
pemParser.close();
}
pemParser.close();
return privateKey;
}
use of org.gudy.bouncycastle.asn1.pkcs.PrivateKeyInfo in project incubator-pulsar by apache.
the class MessageCrypto method loadPrivateKey.
private PrivateKey loadPrivateKey(byte[] keyBytes) throws Exception {
Reader keyReader = new StringReader(new String(keyBytes));
PrivateKey privateKey = null;
try (PEMParser pemReader = new PEMParser(keyReader)) {
X9ECParameters ecParam = null;
Object pemObj = pemReader.readObject();
if (pemObj instanceof ASN1ObjectIdentifier) {
// make sure this is EC Parameter we're handling. In which case
// we'll store it and read the next object which should be our
// EC Private Key
ASN1ObjectIdentifier ecOID = (ASN1ObjectIdentifier) pemObj;
ecParam = ECNamedCurveTable.getByOID(ecOID);
if (ecParam == null) {
throw new PEMException("Unable to find EC Parameter for the given curve oid: " + ecOID.getId());
}
pemObj = pemReader.readObject();
} else if (pemObj instanceof X9ECParameters) {
ecParam = (X9ECParameters) pemObj;
pemObj = pemReader.readObject();
}
if (pemObj instanceof PEMKeyPair) {
PrivateKeyInfo pKeyInfo = ((PEMKeyPair) pemObj).getPrivateKeyInfo();
JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter();
privateKey = pemConverter.getPrivateKey(pKeyInfo);
}
if (ecParam != null && ECDSA.equals(privateKey.getAlgorithm())) {
ECParameterSpec ecSpec = new ECParameterSpec(ecParam.getCurve(), ecParam.getG(), ecParam.getN(), ecParam.getH(), ecParam.getSeed());
KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BouncyCastleProvider.PROVIDER_NAME);
ECPrivateKeySpec keySpec = new ECPrivateKeySpec(((BCECPrivateKey) privateKey).getS(), ecSpec);
privateKey = (PrivateKey) keyFactory.generatePrivate(keySpec);
}
} catch (IOException e) {
throw new Exception(e);
}
return privateKey;
}
use of org.gudy.bouncycastle.asn1.pkcs.PrivateKeyInfo in project candlepin by candlepin.
the class PrivateKeyReaderTest method testReadUnencryptedPKCS8.
@Test
public void testReadUnencryptedPKCS8() throws Exception {
String keyFile = "keys/pkcs8-unencrypted.pem";
try (InputStream keyStream = cl.getResourceAsStream(keyFile);
Reader expectedReader = new InputStreamReader(cl.getResourceAsStream(keyFile))) {
PrivateKey actualKey = new PrivateKeyReader().read(keyStream, null);
PrivateKeyInfo expected = (PrivateKeyInfo) new PEMParser(expectedReader).readObject();
PrivateKey expectedKey = new JcaPEMKeyConverter().setProvider(BC_PROVIDER).getPrivateKey(expected);
assertEquals(actualKey, expectedKey);
}
}
use of org.gudy.bouncycastle.asn1.pkcs.PrivateKeyInfo in project candlepin by candlepin.
the class PrivateKeyReaderTest method testReadEncryptedPKCS8.
/**
* Currently fails due to a bug in OpenJDK: https://bugs.openjdk.java.net/browse/JDK-8076999
*/
@Test
@Ignore
public void testReadEncryptedPKCS8() throws Exception {
String keyFile = "keys/pkcs8-aes256-encrypted.pem";
try (InputStream keyStream = cl.getResourceAsStream(keyFile);
Reader expectedReader = new InputStreamReader(cl.getResourceAsStream(keyFile))) {
PrivateKey actualKey = new PrivateKeyReader().read(keyStream, "password");
PKCS8EncryptedPrivateKeyInfo expected = (PKCS8EncryptedPrivateKeyInfo) new PEMParser(expectedReader).readObject();
// the PBE in JcePKCSPBEInputDecryptorProviderBuilder stands for "password based encryption"
InputDecryptorProvider provider = new JcePKCSPBEInputDecryptorProviderBuilder().setProvider(BC_PROVIDER).build(PASSWORD);
PrivateKeyInfo decryptedInfo = expected.decryptPrivateKeyInfo(provider);
PrivateKey expectedKey = new JcaPEMKeyConverter().setProvider(BC_PROVIDER).getPrivateKey(decryptedInfo);
assertEquals(actualKey, expectedKey);
}
}
Aggregations