use of org.bouncycastle.openssl.PEMKeyPair in project certmgr by hdecarne.
the class KeyHelper method rebuildKeyPair.
/**
* Rebuild the {@link KeyPair} associated with the submitted {@link PrivateKey}.
*
* @param privateKey The {@link PrivateKey} to rebuild the {@link KeyPair} for.
* @return The rebuild {@link KeyPair}.
* @throws IOException if an I/O error occurs during the rebuild.
*/
public static KeyPair rebuildKeyPair(PrivateKey privateKey) throws IOException {
StringWriter stringWriter = new StringWriter();
try (JcaPEMWriter pemWriter = new JcaPEMWriter(stringWriter)) {
pemWriter.writeObject(privateKey);
pemWriter.flush();
}
StringReader stringReader = new StringReader(stringWriter.toString());
Object pemObject;
try (PEMParser pemParser = new PEMParser(stringReader)) {
pemObject = pemParser.readObject();
}
if (!(pemObject instanceof PEMKeyPair)) {
throw new IOException("Unexpected pem object: " + pemObject);
}
PEMKeyPair pemKeyPair = (PEMKeyPair) pemObject;
return PEM_KEY_CONVERTER.getKeyPair(pemKeyPair);
}
use of org.bouncycastle.openssl.PEMKeyPair in project vespa by vespa-engine.
the class KeyUtils method fromPemEncodedPrivateKey.
public static PrivateKey fromPemEncodedPrivateKey(String pem) {
try (PEMParser parser = new PEMParser(new StringReader(pem))) {
Object pemObject = parser.readObject();
if (pemObject instanceof PrivateKeyInfo) {
PrivateKeyInfo keyInfo = (PrivateKeyInfo) pemObject;
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyInfo.getEncoded());
return KeyFactory.getInstance(KeyAlgorithm.RSA.getAlgorithmName()).generatePrivate(keySpec);
} else if (pemObject instanceof PEMKeyPair) {
PEMKeyPair pemKeypair = (PEMKeyPair) pemObject;
PrivateKeyInfo keyInfo = pemKeypair.getPrivateKeyInfo();
JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter();
return pemConverter.getPrivateKey(keyInfo);
}
throw new IllegalArgumentException("Unexpected type of PEM type: " + pemObject);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
use of org.bouncycastle.openssl.PEMKeyPair in project groovity by disney.
the class KeyPairValueHandler method doLoad.
@Override
protected Object doLoad(InputStream stream, String contentType, @SuppressWarnings("rawtypes") Class valueClass, @SuppressWarnings("rawtypes") Map config) throws Exception {
// look here http://stackoverflow.com/questions/15656644/get-keypair-from-pem-key-with-bouncycastle
PEMKeyPair keyPair;
Reader reader = new InputStreamReader(stream, getCharset(contentType));
PEMParser pemReader = new PEMParser(reader);
try {
Object o = pemReader.readObject();
if (o == null) {
return null;
}
if (o instanceof PEMEncryptedKeyPair) {
if (config != null && config.containsKey(PASSWORD)) {
String password = config.get(PASSWORD).toString();
PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(password.toCharArray());
keyPair = ((PEMEncryptedKeyPair) o).decryptKeyPair(decryptionProv);
} else {
throw new RuntimeException("Keypair requires password but none provided");
}
} else {
keyPair = ((PEMKeyPair) o);
}
} finally {
pemReader.close();
}
return new JcaPEMKeyConverter().getKeyPair(keyPair);
}
use of org.bouncycastle.openssl.PEMKeyPair in project candlepin by candlepin.
the class DefaultEntitlementCertServiceAdapterTest method keyPair.
@BeforeClass
public static void keyPair() throws Exception {
ClassLoader cl = DefaultEntitlementCertServiceAdapterTest.class.getClassLoader();
InputStream keyStream = cl.getResourceAsStream("test.key");
PEMParser reader = null;
keyPair = null;
try {
reader = new PEMParser(new InputStreamReader(keyStream));
keyPair = new JcaPEMKeyConverter().setProvider(BC_PROVIDER).getKeyPair((PEMKeyPair) reader.readObject());
} finally {
if (reader != null) {
reader.close();
}
}
}
use of org.bouncycastle.openssl.PEMKeyPair in project candlepin by candlepin.
the class X509CRLStreamWriterTest method testAddEntryToActualCRL.
@Test
public void testAddEntryToActualCRL() throws Exception {
ClassLoader classLoader = this.getClass().getClassLoader();
InputStream crl = classLoader.getResourceAsStream("real-crl.der");
InputStream keyStream = classLoader.getResourceAsStream("real.key");
InputStreamReader keyReader = new InputStreamReader(keyStream);
PEMParser reader = null;
try {
reader = new PEMParser(keyReader);
Object pemObj = reader.readObject();
if (pemObj == null) {
crl.close();
throw new RuntimeException("Reading CA private key failed");
}
if (pemObj instanceof PEMKeyPair) {
keyPair = new JcaPEMKeyConverter().getKeyPair((PEMKeyPair) pemObj);
} else {
crl.close();
throw new RuntimeException("Unexpected CA key object: " + pemObj.getClass().getName());
}
} finally {
if (reader != null) {
reader.close();
}
}
File outfile = new File(folder.getRoot(), "new.crl");
X509CRLStreamWriter stream = new X509CRLStreamWriter(crl, (RSAPrivateKey) keyPair.getPrivate(), (RSAPublicKey) keyPair.getPublic());
// Add enough items to cause the number of length bytes to change
Set<BigInteger> newSerials = new HashSet<>(Arrays.asList(new BigInteger("2358215310"), new BigInteger("7231352433"), new BigInteger("8233181205"), new BigInteger("1455615868"), new BigInteger("4323487764"), new BigInteger("6673256679")));
for (BigInteger i : newSerials) {
stream.add(i, new Date(), CRLReason.privilegeWithdrawn);
}
// Since we have to walk the stream twice, we need two streams!
stream.preScan(classLoader.getResourceAsStream("real-crl.der")).lock();
OutputStream o = new BufferedOutputStream(new FileOutputStream(outfile));
stream.write(o);
o.close();
X509CRL changedCrl = readCRL();
Set<BigInteger> discoveredSerials = new HashSet<>();
for (X509CRLEntry entry : changedCrl.getRevokedCertificates()) {
discoveredSerials.add(entry.getSerialNumber());
}
assertTrue(discoveredSerials.containsAll(newSerials));
}
Aggregations