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