use of org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8EncryptorBuilder in project kafka by apache.
the class TestSslUtils method pem.
static String pem(PrivateKey privateKey, Password password) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8.name()))) {
if (password == null) {
pemWriter.writeObject(new JcaPKCS8Generator(privateKey, null));
} else {
JceOpenSSLPKCS8EncryptorBuilder encryptorBuilder = new JceOpenSSLPKCS8EncryptorBuilder(PKCS8Generator.PBE_SHA1_3DES);
encryptorBuilder.setPassword(password.value().toCharArray());
try {
pemWriter.writeObject(new JcaPKCS8Generator(privateKey, encryptorBuilder.build()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return new String(out.toByteArray(), StandardCharsets.UTF_8);
}
use of org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8EncryptorBuilder in project zookeeper by apache.
the class X509TestHelpers method pemEncodePrivateKey.
/**
* PEM-encodes the given private key (compatible with OpenSSL), optionally protecting it with a password, and
* returns the result as a String.
* @param key the private key.
* @param password an optional key password. If empty or null, the private key will not be encrypted.
* @return a String containing the PEM encoding of the private key.
* @throws IOException if converting the key to PEM format fails.
* @throws OperatorCreationException if constructing the encryptor from the given password fails.
*/
public static String pemEncodePrivateKey(PrivateKey key, String password) throws IOException, OperatorCreationException {
StringWriter stringWriter = new StringWriter();
JcaPEMWriter pemWriter = new JcaPEMWriter(stringWriter);
OutputEncryptor encryptor = null;
if (password != null && password.length() > 0) {
encryptor = new JceOpenSSLPKCS8EncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC).setProvider(BouncyCastleProvider.PROVIDER_NAME).setRandom(PRNG).setPasssword(password.toCharArray()).build();
}
pemWriter.writeObject(new JcaPKCS8Generator(key, encryptor));
pemWriter.close();
return stringWriter.toString();
}
use of org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8EncryptorBuilder in project graylog2-server by Graylog2.
the class KeyUtil method generatePKCS8FromPrivateKey.
/**
* Build a password-encrypted PKCS8 private key and write it to a PEM file in the temp directory.
* Caller is responsible for ensuring that the temp directory is writable. The file will be deleted
* when the VM exits.
* @param tmpDir path to directory in which to create the
* @param password to protect the key
* @param key encrypt this key
* @return PEM file
* @throws GeneralSecurityException
*/
public static File generatePKCS8FromPrivateKey(Path tmpDir, char[] password, PrivateKey key) throws GeneralSecurityException {
try {
JceOpenSSLPKCS8EncryptorBuilder encryptorBuilder = new JceOpenSSLPKCS8EncryptorBuilder(PKCS8Generator.AES_256_CBC).setRandom(new SecureRandom()).setPasssword(password);
OutputEncryptor encryptor = encryptorBuilder.build();
// construct object to create the PKCS8 object from the private key and encryptor
PemObject pemObj = new JcaPKCS8Generator(key, encryptor).generate();
StringWriter stringWriter = new StringWriter();
try (JcaPEMWriter pemWriter = new JcaPEMWriter(stringWriter)) {
pemWriter.writeObject(pemObj);
}
// write PKCS8 to file
String pkcs8Key = stringWriter.toString();
File tmpFile = Files.createTempFile(tmpDir, "pkcs8", ".key").toFile();
try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
fos.write(pkcs8Key.getBytes(StandardCharsets.UTF_8));
tmpFile.deleteOnExit();
}
return tmpFile;
} catch (IOException | OperatorCreationException e) {
throw new GeneralSecurityException(e);
}
}
Aggregations