Search in sources :

Example 21 with PemReader

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);
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader)

Example 22 with PemReader

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;
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) PrivateKey(java.security.PrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) PEMParser(org.bouncycastle.openssl.PEMParser) StringReader(java.io.StringReader) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KeyStoreException(java.security.KeyStoreException) CertPathValidatorException(java.security.cert.CertPathValidatorException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Example 23 with PemReader

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);
    }
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SecurityProviderException(com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 24 with PemReader

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);
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 25 with PemReader

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);
    }
}
Also used : ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

PemReader (org.bouncycastle.util.io.pem.PemReader)31 StringReader (java.io.StringReader)20 PemObject (org.bouncycastle.util.io.pem.PemObject)20 IOException (java.io.IOException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 X509Certificate (java.security.cert.X509Certificate)10 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)9 KeyFactory (java.security.KeyFactory)6 CertificateException (java.security.cert.CertificateException)6 CertificateFactory (java.security.cert.CertificateFactory)6 FileReader (java.io.FileReader)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 Certificate (java.security.cert.Certificate)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 ArrayList (java.util.ArrayList)5 InputStreamReader (java.io.InputStreamReader)4 PrivateKey (java.security.PrivateKey)4 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 PublicKey (java.security.PublicKey)3 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)3