use of java.security.KeyStore in project platformlayer by platformlayer.
the class KeyStoreEncryptionStore method main.
public static void main(String[] args) throws Exception {
if (!args[0].equals("explode")) {
throw new IllegalStateException();
}
char[] password = "notasecret".toCharArray();
ProtectionParameter protParam = new KeyStore.PasswordProtection(password);
KeyStore keyStore = KeyStoreUtils.load(new File(args[1]));
File dest = new File(args[2]);
dest.mkdirs();
Enumeration<String> aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (keyStore.isKeyEntry(alias)) {
Entry entry = keyStore.getEntry(alias, protParam);
PrivateKeyEntry privateKeyEntry = (PrivateKeyEntry) entry;
{
X509Certificate[] certificateChain = toX509(privateKeyEntry.getCertificateChain());
String encoded = CertificateUtils.toPem(certificateChain);
File out = new File(dest, alias + ".crt");
Files.write(encoded, out, Charsets.UTF_8);
}
{
PrivateKey key = privateKeyEntry.getPrivateKey();
String encoded = PrivateKeys.toPem(key);
File out = new File(dest, alias + ".key");
Files.write(encoded, out, Charsets.UTF_8);
}
}
if (keyStore.isCertificateEntry(alias)) {
Entry entry = keyStore.getEntry(alias, null);
TrustedCertificateEntry trustedCertificateEntry = (TrustedCertificateEntry) entry;
X509Certificate cert = (X509Certificate) trustedCertificateEntry.getTrustedCertificate();
String encoded = CertificateUtils.toPem(cert);
File out = new File(dest, alias + ".crt");
Files.write(encoded, out, Charsets.UTF_8);
}
}
}
use of java.security.KeyStore in project camel by apache.
the class HttpsServerTestSupport method getSSLContext.
@Override
protected SSLContext getSSLContext() throws Exception {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(KEYSTORE), PASSWORD.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, PASSWORD.toCharArray());
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(new FileInputStream(KEYSTORE), PASSWORD.toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
SSLContext sslcontext = SSLContext.getInstance(SECURE_SOCKET_PROTOCOL);
sslcontext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
return sslcontext;
}
use of java.security.KeyStore in project camel by apache.
the class AbstractJsseParametersTest method createPropertiesPlaceholderAwareContext.
protected CamelContext createPropertiesPlaceholderAwareContext() throws Exception {
Properties supplementalProperties = new Properties();
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
SecureRandom sr = null;
try {
sr = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
// Ignore
}
SSLContext sslc = SSLContext.getInstance("TLS");
sslc.init(null, null, null);
SSLSocket socket = (SSLSocket) sslc.getSocketFactory().createSocket();
supplementalProperties.setProperty("keyStoreParameters.type", KeyStore.getDefaultType());
supplementalProperties.setProperty("keyStoreParameters.provider", ks.getProvider().getName());
supplementalProperties.setProperty("keyManagersParameters.algorithm", KeyManagerFactory.getDefaultAlgorithm());
supplementalProperties.setProperty("keyManagersParameters.provider", kmf.getProvider().getName());
supplementalProperties.setProperty("trustManagersParameters.algorithm", TrustManagerFactory.getDefaultAlgorithm());
supplementalProperties.setProperty("trustManagersParameters.provider", tmf.getProvider().getName());
if (sr != null) {
supplementalProperties.setProperty("secureRandomParameters.algorithm", "SHA1PRNG");
supplementalProperties.setProperty("secureRandomParameters.provider", sr.getProvider().getName());
}
supplementalProperties.setProperty("sslContextParameters.provider", sslc.getProvider().getName());
supplementalProperties.setProperty("cipherSuite.0", socket.getSupportedCipherSuites()[0]);
// Have to skip this guy because he doesn't work with TLS as the SSLContext protocol
String ssp = "";
for (String protocol : socket.getSupportedProtocols()) {
if (!"SSLv2Hello".equals(protocol)) {
ssp = protocol;
break;
}
}
supplementalProperties.setProperty("secureSocketProtocol.0", ssp);
return this.createPropertiesPlaceholderAwareContext(supplementalProperties);
}
use of java.security.KeyStore in project camel by apache.
the class KeyStoreParametersTest method testExplicitType.
public void testExplicitType() throws Exception {
KeyStoreParameters ksp = this.createMinimalKeyStoreParameters();
ksp.setType("jks");
KeyStore ks = ksp.createKeyStore();
assertNotNull(ks.getCertificate("server"));
}
use of java.security.KeyStore in project camel by apache.
the class KeyStoreParametersTest method testValidParameters.
public void testValidParameters() throws GeneralSecurityException, IOException, URISyntaxException {
KeyStoreParameters ksp = this.createMinimalKeyStoreParameters();
KeyStore ks = ksp.createKeyStore();
assertNotNull(ks.getCertificate("server"));
URL resourceUrl = this.getClass().getResource("/org/apache/camel/util/jsse/localhost.ks");
ksp.setResource(resourceUrl.toExternalForm());
ks = ksp.createKeyStore();
assertNotNull(ks.getCertificate("server"));
resourceUrl = this.getClass().getResource("/org/apache/camel/util/jsse/localhost.ks");
File file = new File(resourceUrl.toURI());
ksp.setResource(file.getAbsolutePath());
ks = ksp.createKeyStore();
assertNotNull(ks.getCertificate("server"));
}
Aggregations