Search in sources :

Example 6 with JcaMiscPEMGenerator

use of org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator in project zaproxy by zaproxy.

the class DynamicSSLPanel method setRootca.

private void setRootca(KeyStore rootca) {
    this.rootca = rootca;
    final StringWriter sw = new StringWriter();
    if (rootca != null) {
        try {
            final Certificate cert = rootca.getCertificate(org.parosproxy.paros.security.SslCertificateService.ZAPROXY_JKS_ALIAS);
            try (final PemWriter pw = new PemWriter(sw)) {
                pw.writeObject(new JcaMiscPEMGenerator(cert));
                pw.flush();
            }
        } catch (final Exception e) {
            logger.error("Error while extracting public part from generated Root CA certificate.", e);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Certificate defined.\n" + sw.toString());
    }
    txt_PubCert.setText(sw.toString());
}
Also used : JcaMiscPEMGenerator(org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator) StringWriter(java.io.StringWriter) PemWriter(org.bouncycastle.util.io.pem.PemWriter) IOException(java.io.IOException) Certificate(java.security.cert.Certificate)

Example 7 with JcaMiscPEMGenerator

use of org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator in project zaproxy by zaproxy.

the class ExtensionDynSSL method writeRootFullCaCertificateToFile.

/**
 * Writes the Root CA full certificate to the specified file in pem format, suitable for
 * importing into ZAP
 *
 * @param path the path the Root CA certificate will be written to
 * @throws IOException
 * @throws KeyStoreException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 * @throws UnrecoverableKeyException
 * @since 2.8.0
 */
public void writeRootFullCaCertificateToFile(Path path) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
    KeyStore ks = this.getParams().getRootca();
    if (ks != null) {
        final Certificate cert = ks.getCertificate(org.parosproxy.paros.security.SslCertificateService.ZAPROXY_JKS_ALIAS);
        try (final Writer w = Files.newBufferedWriter(path, StandardCharsets.US_ASCII);
            final PemWriter pw = new PemWriter(w)) {
            pw.writeObject(new JcaMiscPEMGenerator(cert));
            pw.flush();
            w.write(SslCertificateUtils.BEGIN_PRIVATE_KEY_TOKEN + "\n");
            Key key = ks.getKey(org.parosproxy.paros.security.SslCertificateService.ZAPROXY_JKS_ALIAS, org.parosproxy.paros.security.SslCertificateService.PASSPHRASE);
            PrivateKey pk = (PrivateKey) key;
            w.write(Base64.getMimeEncoder().encodeToString(pk.getEncoded()));
            w.write("\n" + SslCertificateUtils.END_PRIVATE_KEY_TOKEN + "\n");
        }
    }
}
Also used : JcaMiscPEMGenerator(org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator) PrivateKey(java.security.PrivateKey) PemWriter(org.bouncycastle.util.io.pem.PemWriter) KeyStore(java.security.KeyStore) PemWriter(org.bouncycastle.util.io.pem.PemWriter) Writer(java.io.Writer) Key(java.security.Key) PrivateKey(java.security.PrivateKey) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 8 with JcaMiscPEMGenerator

use of org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator in project Openfire by igniterealtime.

the class CertificateManager method toPemRepresentation.

/**
 * Generates a PEM representation of the input argument.
 *
 * @param object the input argument (cannot be null).
 * @return PEM representation of the input argument.
 * @throws IOException When a PEM representation of the input could not be created.
 */
public static String toPemRepresentation(Object object) throws IOException {
    final StringWriter result = new StringWriter();
    try (final PemWriter pemWriter = new PemWriter(result)) {
        final PemObjectGenerator objGen = new JcaMiscPEMGenerator(object);
        pemWriter.writeObject(objGen);
    }
    return result.toString();
}
Also used : JcaMiscPEMGenerator(org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator) PemObjectGenerator(org.bouncycastle.util.io.pem.PemObjectGenerator) PemWriter(org.bouncycastle.util.io.pem.PemWriter)

Example 9 with JcaMiscPEMGenerator

use of org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator in project java by kubernetes-client.

the class SSLUtils method dumpKey.

public static byte[] dumpKey(PrivateKey privateKey) throws IOException {
    StringWriter writer = new StringWriter();
    PemWriter pemWriter = new PemWriter(writer);
    pemWriter.writeObject(new JcaMiscPEMGenerator(privateKey));
    pemWriter.flush();
    return writer.toString().getBytes();
}
Also used : JcaMiscPEMGenerator(org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator) StringWriter(java.io.StringWriter) PemWriter(org.bouncycastle.util.io.pem.PemWriter)

Example 10 with JcaMiscPEMGenerator

use of org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator in project nifi by apache.

the class TlsClientManager method write.

@Override
public void write(OutputStreamFactory outputStreamFactory) throws IOException, GeneralSecurityException {
    super.write(outputStreamFactory);
    String trustStorePassword = tlsClientConfig.getTrustStorePassword();
    boolean trustStorePasswordGenerated = false;
    if (StringUtils.isEmpty(trustStorePassword)) {
        trustStorePassword = getPasswordUtil().generatePassword();
        trustStorePasswordGenerated = true;
    }
    trustStorePassword = TlsHelper.writeKeyStore(trustStore, outputStreamFactory, new File(tlsClientConfig.getTrustStore()), trustStorePassword, trustStorePasswordGenerated);
    tlsClientConfig.setTrustStorePassword(trustStorePassword);
    for (ConfigurationWriter<TlsClientConfig> configurationWriter : configurationWriters) {
        configurationWriter.write(tlsClientConfig, outputStreamFactory);
    }
    if (certificateAuthorityDirectory != null) {
        // Write out all trusted certificates from truststore
        for (String alias : Collections.list(trustStore.aliases())) {
            try {
                KeyStore.Entry trustStoreEntry = trustStore.getEntry(alias, null);
                if (trustStoreEntry instanceof KeyStore.TrustedCertificateEntry) {
                    Certificate trustedCertificate = ((KeyStore.TrustedCertificateEntry) trustStoreEntry).getTrustedCertificate();
                    try (OutputStream outputStream = outputStreamFactory.create(new File(certificateAuthorityDirectory, alias + ".pem"));
                        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
                        PemWriter pemWriter = new PemWriter(outputStreamWriter)) {
                        pemWriter.writeObject(new JcaMiscPEMGenerator(trustedCertificate));
                    }
                }
            } catch (UnrecoverableEntryException e) {
            // Ignore, not a trusted cert
            }
        }
    }
}
Also used : PemWriter(org.bouncycastle.util.io.pem.PemWriter) OutputStream(java.io.OutputStream) KeyStore(java.security.KeyStore) JcaMiscPEMGenerator(org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator) TlsClientConfig(org.apache.nifi.toolkit.tls.configuration.TlsClientConfig) UnrecoverableEntryException(java.security.UnrecoverableEntryException) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Certificate(java.security.cert.Certificate)

Aggregations

JcaMiscPEMGenerator (org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator)15 PemWriter (org.bouncycastle.util.io.pem.PemWriter)15 OutputStreamWriter (java.io.OutputStreamWriter)7 StringWriter (java.io.StringWriter)5 Writer (java.io.Writer)5 KeyStore (java.security.KeyStore)5 Certificate (java.security.cert.Certificate)5 IOException (java.io.IOException)3 X509Certificate (java.security.cert.X509Certificate)3 File (java.io.File)2 TlsClientConfig (org.apache.nifi.toolkit.tls.configuration.TlsClientConfig)2 PemObjectGenerator (org.bouncycastle.util.io.pem.PemObjectGenerator)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Key (java.security.Key)1 KeyPair (java.security.KeyPair)1 PrivateKey (java.security.PrivateKey)1