use of org.bouncycastle.util.io.pem.PemReader in project gocd by gocd.
the class EncryptionHelper method getRSAPrivateKeyFrom.
private static PrivateKey getRSAPrivateKeyFrom(String content) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
PemReader reader = new PemReader(new StringReader(content));
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent());
return KeyFactory.getInstance("RSA").generatePrivate(spec);
}
use of org.bouncycastle.util.io.pem.PemReader in project fabric-sdk-java by hyperledger.
the class CryptoPrimitives method bytesToPrivateKey.
/**
* Return PrivateKey from pem bytes.
*
* @param pemKey pem-encoded private key
* @return
*/
public PrivateKey bytesToPrivateKey(byte[] pemKey) throws CryptoException {
PrivateKey pk = null;
CryptoException ce = null;
try {
PemReader pr = new PemReader(new StringReader(new String(pemKey)));
PemObject po = pr.readPemObject();
PEMParser pem = new PEMParser(new StringReader(new String(pemKey)));
logger.debug("found private key with type " + po.getType());
if (po.getType().equals("PRIVATE KEY")) {
pk = new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) pem.readObject());
} else {
PEMKeyPair kp = (PEMKeyPair) pem.readObject();
pk = new JcaPEMKeyConverter().getPrivateKey(kp.getPrivateKeyInfo());
}
} catch (Exception e) {
throw new CryptoException("Failed to convert private key bytes", e);
}
return pk;
}
use of org.bouncycastle.util.io.pem.PemReader in project azure-iot-sdk-java by Azure.
the class SecurityProviderX509Cert method parsePublicKeyCertificate.
private static X509Certificate parsePublicKeyCertificate(String publicKeyCertificateString) throws CertificateException {
try {
// Codes_SRS_SecurityClientDiceEmulator_34_003: [This function shall return an X509Certificate instance created by the provided PEM formatted publicKeyCertificateString.]
Security.addProvider(new BouncyCastleProvider());
PemReader publicKeyCertificateReader = new PemReader(new StringReader(publicKeyCertificateString));
PemObject possiblePublicKeyCertificate = publicKeyCertificateReader.readPemObject();
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
return (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(possiblePublicKeyCertificate.getContent()));
} catch (Exception e) {
// Codes_SRS_SecurityClientDiceEmulator_34_004: [If any exception is encountered while attempting to create the public key certificate instance, this function shall throw a CertificateException.]
throw new CertificateException(e);
}
}
use of org.bouncycastle.util.io.pem.PemReader in project azure-iot-sdk-java by Azure.
the class SecurityProviderX509CertTest method parsePublicKeyCertificateSuccess.
// Tests_SRS_SecurityClientDiceEmulator_34_003: [This function shall return an X509Certificate instance created by the provided PEM formatted publicKeyCertificateString.]
@Test
public void parsePublicKeyCertificateSuccess() throws CertificateException, IOException {
// arrange
new NonStrictExpectations() {
{
new PemReader(new StringReader(expectedPublicKeyCertificateString));
result = mockedPemReader;
mockedPemReader.readPemObject();
result = mockedPemObject;
CertificateFactory.getInstance("X.509");
result = mockedCertificateFactory;
mockedCertificateFactory.generateCertificate(new ByteArrayInputStream(mockedPemObject.getContent()));
result = mockedX509Certificate;
}
};
// act
X509Certificate actualPublicKeyCertificate = Deencapsulation.invoke(SecurityProviderX509Cert.class, "parsePublicKeyCertificate", new Class[] { String.class }, expectedPublicKeyCertificateString);
// assert
assertEquals(mockedX509Certificate, actualPublicKeyCertificate);
}
use of org.bouncycastle.util.io.pem.PemReader in project azure-iot-sdk-java by Azure.
the class IotHubSSLContext method parsePublicKeyCertificate.
private static Collection<X509Certificate> parsePublicKeyCertificate(String publicKeyCertificateString) throws CertificateException {
try {
Collection<X509Certificate> certChain = new ArrayList<>();
// Codes_SRS_IOTHUBSSLCONTEXT_34_033: [This function shall return the X509Certificate cert chain specified by the PEM formatted publicKeyCertificateString.]
Security.addProvider(new BouncyCastleProvider());
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
try (PemReader publicKeyCertificateReader = new PemReader(new StringReader(publicKeyCertificateString))) {
PemObject possiblePublicKeyCertificate;
while (((possiblePublicKeyCertificate = publicKeyCertificateReader.readPemObject()) != null)) {
byte[] content = possiblePublicKeyCertificate.getContent();
if (content.length > 0) {
final ByteArrayInputStream bais = new ByteArrayInputStream(content);
while (bais.available() > 0) {
final Certificate cert = certFactory.generateCertificate(bais);
if (cert instanceof X509Certificate) {
certChain.add((X509Certificate) cert);
}
}
} else {
break;
}
}
}
return certChain;
} catch (Exception e) {
// Codes_SRS_IOTHUBSSLCONTEXT_34_034: [If any exception is encountered while attempting to create the public key certificate instance, this function shall throw a CertificateException.]
throw new CertificateException(e);
}
}
Aggregations