Java源码示例:org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator

示例1
@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
            }
        }
    }
}
 
示例2
public static String pemEncodeJcaObject(Object object) throws IOException {
    StringWriter writer = new StringWriter();
    try (PemWriter pemWriter = new PemWriter(writer)) {
        pemWriter.writeObject(new JcaMiscPEMGenerator(object));
    }
    return writer.toString();
}
 
示例3
/**
 *
 * @return A PEM encoded string for the public key of the certificate.
 * @throws CertificateEncodingException if certificate can't be encoded.
 */
public String getPublicKeyCertificate() throws CertificateEncodingException {
	StringWriter sw = new StringWriter();
	try (PemWriter pw = new PemWriter(sw)) {
		PemObjectGenerator gen = new JcaMiscPEMGenerator(this.certificate);
		pw.writeObject(gen);
	}
	catch (IOException e) {
		throw new RuntimeException(e);
	}
	return sw.toString();
}
 
示例4
/**
 * Generates certificates with the tls-toolkit and then starts up the docker compose file
 */
@BeforeClass
public static void initCertificates() throws Exception {
    certificatesDirectory = Paths.get(NiFiRestConfigurationProviderSecureTest.class.getClassLoader()
            .getResource("docker-compose-NiFiRestConfigurationProviderSecureTest.yml").getFile()).getParent().toAbsolutePath().resolve("certificates-NiFiRestConfigurationProviderSecureTest");
    trustSslContext = initCertificates(certificatesDirectory, Arrays.asList("c2", "mocknifi"));
    healthCheckSocketFactory = trustSslContext.getSocketFactory();

    KeyStore mockNiFiKeyStore = KeyStore.getInstance("JKS");
    try (InputStream inputStream = Files.newInputStream(certificatesDirectory.resolve("mocknifi").resolve("keystore.jks"))) {
        mockNiFiKeyStore.load(inputStream, "badKeystorePass".toCharArray());
    }
    try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(certificatesDirectory.resolve("mocknifi").resolve("cert.pem"))))) {
        pemWriter.writeObject(new JcaMiscPEMGenerator(mockNiFiKeyStore.getKey(TlsToolkitStandalone.NIFI_KEY, "badKeyPass".toCharArray())));
        for (Certificate certificate : mockNiFiKeyStore.getCertificateChain(TlsToolkitStandalone.NIFI_KEY)) {
            pemWriter.writeObject(new JcaMiscPEMGenerator(certificate));
        }
    }

    KeyStore mockNiFiTrustStore = KeyStore.getInstance("JKS");
    try (InputStream inputStream = Files.newInputStream(certificatesDirectory.resolve("mocknifi").resolve("truststore.jks"))) {
        mockNiFiTrustStore.load(inputStream, "badTrustPass".toCharArray());
    }
    try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(certificatesDirectory.resolve("mocknifi").resolve("ca.pem"))))) {
        pemWriter.writeObject(new JcaMiscPEMGenerator(mockNiFiTrustStore.getCertificate(TlsToolkitStandalone.NIFI_CERT)));
    }

    docker.before();
}
 
示例5
public static byte[] toPEM(PKCS10CertificationRequest csr) throws IOException {
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8))) {
    pemWriter.writeObject(new JcaMiscPEMGenerator(csr));
    pemWriter.flush();
  }
  return os.toByteArray();
}
 
示例6
public static String toPEM(X509Certificate certificate) throws IOException {
  StringWriter stringWriter = new StringWriter();
  PemWriter pemWriter = new PemWriter(stringWriter);
  try {
    pemWriter.writeObject(new JcaMiscPEMGenerator(certificate));
    pemWriter.flush();
  } finally {
    pemWriter.close();
  }
  return stringWriter.toString();
}
 
示例7
public static String toPEM(PrivateKey privateKey) throws IOException {
  StringWriter stringWriter = new StringWriter();
  PemWriter pemWriter = new PemWriter(stringWriter);
  try {
    pemWriter.writeObject(new JcaMiscPEMGenerator(privateKey));
    pemWriter.flush();
  } finally {
    pemWriter.close();
  }
  return stringWriter.toString();
}
 
示例8
private static String convertToPEM(Object obj) {
	try (StringWriter out = new StringWriter(); PemWriter pemWriter = new PemWriter(out)) {
		pemWriter.writeObject(new JcaMiscPEMGenerator(obj));
		pemWriter.flush();
		return out.toString();
	} catch (Exception e) {
		throw new DSSException("Unable to convert DER to PEM", e);
	}
}
 
示例9
private void writePubCertificateToFile(File f) throws IOException, KeyStoreException {
    OptionsParam options = Model.getSingleton().getOptionsParam();
    DynSSLParam param = options.getParamSet(DynSSLParam.class);
    KeyStore ks = param.getRootca();
    if (ks != null) {
        final Certificate cert = ks.getCertificate(SslCertificateService.ZAPROXY_JKS_ALIAS);
        try (final Writer w = Files.newBufferedWriter(f.toPath(), StandardCharsets.US_ASCII);
                final PemWriter pw = new PemWriter(w)) {
            pw.writeObject(new JcaMiscPEMGenerator(cert));
            pw.flush();
        }
    }
}
 
示例10
/**
 * 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();
}
 
示例11
@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,  TlsHelper.escapeFilename(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
            }
        }
    }
}
 
示例12
private static void outputAsPem(Object pemObj, String filename, File directory, String extension) throws IOException {
    OutputStream outputStream = new FileOutputStream(new File(directory,  TlsHelper.escapeFilename(filename) + extension));
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
    JcaPEMWriter pemWriter = new JcaPEMWriter(outputStreamWriter);
    JcaMiscPEMGenerator pemGen = new JcaMiscPEMGenerator(pemObj);
    pemWriter.writeObject(pemGen);
    pemWriter.close();
}
 
示例13
public static String pemEncodeJcaObject(Object object) throws IOException {
    StringWriter writer = new StringWriter();
    try (PemWriter pemWriter = new PemWriter(writer)) {
        pemWriter.writeObject(new JcaMiscPEMGenerator(object));
    }
    return writer.toString();
}