Search in sources :

Example 11 with PEMParser

use of org.bouncycastle.openssl.PEMParser in project oxAuth by GluuFederation.

the class CertificateParser method parsePem.

public static X509Certificate parsePem(String pemEncodedCert) throws CertificateException {
    StringReader sr = new StringReader(pemEncodedCert);
    PEMParser pemReader = new PEMParser(sr);
    try {
        X509CertificateHolder certificateHolder = ((X509CertificateHolder) pemReader.readObject());
        if (certificateHolder == null) {
            return null;
        }
        X509Certificate cert = new JcaX509CertificateConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME).getCertificate(certificateHolder);
        return cert;
    } catch (IOException ex) {
        throw new CertificateException(ex);
    } finally {
        IOUtils.closeQuietly(pemReader);
    }
}
Also used : PEMParser(org.bouncycastle.openssl.PEMParser) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) StringReader(java.io.StringReader) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate)

Example 12 with PEMParser

use of org.bouncycastle.openssl.PEMParser in project oxTrust by GluuFederation.

the class ManageCertificateAction method getKeyPair.

private KeyPair getKeyPair(String fileName) {
    KeyPair pair = null;
    JCERSAPrivateCrtKey privateKey = null;
    PEMParser r = null;
    FileReader fileReader = null;
    File keyFile = new File(getTempCertDir() + fileName.replace("crt", "key"));
    if (keyFile.isFile()) {
        try {
            fileReader = new FileReader(keyFile);
            r = new PEMParser(fileReader);
            Object keys = r.readObject();
            if (keys == null) {
                log.error(" Unable to read keys from: " + keyFile.getAbsolutePath());
                return null;
            }
            if (keys instanceof KeyPair) {
                pair = (KeyPair) keys;
                log.debug(keyFile.getAbsolutePath() + "contains KeyPair");
            } else if (keys instanceof JCERSAPrivateCrtKey) {
                privateKey = (JCERSAPrivateCrtKey) keys;
                log.debug(keyFile.getAbsolutePath() + "contains JCERSAPrivateCrtKey");
                BigInteger exponent = privateKey.getPublicExponent();
                BigInteger modulus = privateKey.getModulus();
                RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(modulus, exponent);
                PublicKey publicKey = null;
                try {
                    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                    publicKey = keyFactory.generatePublic(publicKeySpec);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                pair = new KeyPair(publicKey, privateKey);
            } else {
                log.error(keyFile.getAbsolutePath() + " Contains unsupported key type: " + keys.getClass().getName());
                return null;
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return null;
        } finally {
            try {
                r.close();
                fileReader.close();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return null;
            }
        }
    } else {
        log.error("Key file does not exist : " + keyFile.getAbsolutePath());
    }
    log.debug("KeyPair successfully extracted from: " + keyFile.getAbsolutePath());
    return pair;
}
Also used : KeyPair(java.security.KeyPair) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) LdapMappingException(org.gluu.site.ldap.persistence.exception.LdapMappingException) IOException(java.io.IOException) PEMParser(org.bouncycastle.openssl.PEMParser) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) BigInteger(java.math.BigInteger) FileReader(java.io.FileReader) JCERSAPrivateCrtKey(org.bouncycastle.jce.provider.JCERSAPrivateCrtKey) UploadedFile(org.richfaces.model.UploadedFile) File(java.io.File) KeyFactory(java.security.KeyFactory)

Aggregations

PEMParser (org.bouncycastle.openssl.PEMParser)12 PEMKeyPair (org.bouncycastle.openssl.PEMKeyPair)8 KeyPair (java.security.KeyPair)7 InputStreamReader (java.io.InputStreamReader)6 JcaPEMKeyConverter (org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter)6 BufferedReader (java.io.BufferedReader)4 IOException (java.io.IOException)4 Reader (java.io.Reader)4 X509Certificate (java.security.cert.X509Certificate)4 PEMDecryptorProvider (org.bouncycastle.openssl.PEMDecryptorProvider)4 PEMEncryptedKeyPair (org.bouncycastle.openssl.PEMEncryptedKeyPair)4 JcePEMDecryptorProviderBuilder (org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder)4 InputStream (java.io.InputStream)3 KeyFactory (java.security.KeyFactory)3 FileReader (java.io.FileReader)2 StringReader (java.io.StringReader)2 BigInteger (java.math.BigInteger)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Certificate (java.security.cert.Certificate)2 CertificateException (java.security.cert.CertificateException)2