Search in sources :

Example 1 with PEMKeyPair

use of org.jruby.ext.openssl.impl.pem.PEMKeyPair in project jruby-openssl by jruby.

the class PEMUtils method generatePKCS12.

public static byte[] generatePKCS12(final Reader keyReader, final byte[] cert, final String aliasName, final char[] password) throws IOException, GeneralSecurityException {
    final Collection<? extends Certificate> certChain = SecurityHelper.getCertificateFactory("X.509").generateCertificates(new ByteArrayInputStream(cert));
    final PEMKeyPair pemKeyPair = readInternal(keyReader, null);
    final KeyFactory keyFactory = getKeyFactory(pemKeyPair.getPrivateKeyInfo().getPrivateKeyAlgorithm());
    Key privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(pemKeyPair.getPrivateKeyInfo().getEncoded()));
    final KeyStore keyStore = SecurityHelper.getKeyStore("PKCS12");
    keyStore.load(null, null);
    keyStore.setKeyEntry(aliasName, privateKey, null, certChain.toArray(new Certificate[certChain.size()]));
    final ByteArrayOutputStream pkcs12Out = new ByteArrayOutputStream();
    keyStore.store(pkcs12Out, password == null ? new char[0] : password);
    return pkcs12Out.toByteArray();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) PEMKeyPair(org.jruby.ext.openssl.impl.pem.PEMKeyPair) ByteArrayOutputStream(org.jruby.ext.openssl.util.ByteArrayOutputStream) KeyStore(java.security.KeyStore) KeyFactory(java.security.KeyFactory) PEMInputOutput.getKeyFactory(org.jruby.ext.openssl.x509store.PEMInputOutput.getKeyFactory) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) Certificate(java.security.cert.Certificate)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 Key (java.security.Key)1 KeyFactory (java.security.KeyFactory)1 KeyStore (java.security.KeyStore)1 Certificate (java.security.cert.Certificate)1 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)1 SecretKey (javax.crypto.SecretKey)1 PEMKeyPair (org.jruby.ext.openssl.impl.pem.PEMKeyPair)1 ByteArrayOutputStream (org.jruby.ext.openssl.util.ByteArrayOutputStream)1 PEMInputOutput.getKeyFactory (org.jruby.ext.openssl.x509store.PEMInputOutput.getKeyFactory)1