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);
}
}
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;
}
Aggregations