Search in sources :

Example 1 with JCERSAPrivateCrtKey

use of org.bouncycastle.jce.provider.JCERSAPrivateCrtKey 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

File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 GeneralSecurityException (java.security.GeneralSecurityException)1 KeyFactory (java.security.KeyFactory)1 KeyPair (java.security.KeyPair)1 PublicKey (java.security.PublicKey)1 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)1 JCERSAPrivateCrtKey (org.bouncycastle.jce.provider.JCERSAPrivateCrtKey)1 PEMParser (org.bouncycastle.openssl.PEMParser)1 LdapMappingException (org.gluu.site.ldap.persistence.exception.LdapMappingException)1 UploadedFile (org.richfaces.model.UploadedFile)1