@Test public void testCopyPKCS12Options() throws Exception { PfxOptions options = new PfxOptions(); String password = TestUtils.randomAlphaString(100); String path = TestUtils.randomAlphaString(100); Buffer value = Buffer.buffer(TestUtils.randomAlphaString(100)); options.setPassword(password); options.setPath(path); options.setValue(value); options = new PfxOptions(options); assertEquals(password, options.getPassword()); assertEquals(path, options.getPath()); assertEquals(value, options.getValue()); options = new PfxOptions(options.toJson()); assertEquals(password, options.getPassword()); assertEquals(path, options.getPath()); assertEquals(value, options.getValue()); }
/** * Copy constructor * * @param other the options to copy */ public PfxOptions(PfxOptions other) { super(); this.password = other.getPassword(); this.path = other.getPath(); this.value = other.getValue(); }
static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, PfxOptions obj) { for (java.util.Map.Entry<String, Object> member : json) { switch (member.getKey()) { case "password": if (member.getValue() instanceof String) { obj.setPassword((String)member.getValue()); } break; case "path": if (member.getValue() instanceof String) { obj.setPath((String)member.getValue()); } break; case "value": if (member.getValue() instanceof String) { obj.setValue(io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)member.getValue()))); } break; } } }
@Test public void testDefaultPKCS12OptionsJson() { PfxOptions def = new PfxOptions(); PfxOptions json = new PfxOptions(new JsonObject()); assertEquals(def.getPassword(), json.getPassword()); assertEquals(def.getPath(), json.getPath()); assertEquals(def.getValue(), json.getValue()); }
if (isFileExists(fullKeyStore)) { if (STORE_PKCS12.equalsIgnoreCase(sslOption.getKeyStoreType())) { PfxOptions keyPfxOptions = new PfxOptions(); keyPfxOptions.setPath(sslCustom.getFullPath(sslOption.getKeyStore())); keyPfxOptions.setPassword(new String(sslCustom.decode(sslOption.getKeyStoreValue().toCharArray()))); tcpClientOptions.setPfxKeyCertOptions(keyPfxOptions); } else if (STORE_JKS.equalsIgnoreCase(sslOption.getKeyStoreType())) { if (isFileExists(fullTrustStore)) { if (STORE_PKCS12.equalsIgnoreCase(sslOption.getTrustStoreType())) { PfxOptions trustPfxOptions = new PfxOptions(); trustPfxOptions.setPath(sslCustom.getFullPath(sslOption.getTrustStore())); trustPfxOptions .setPassword(new String(sslCustom.decode(sslOption.getTrustStoreValue().toCharArray()))); tcpClientOptions.setPfxTrustOptions(trustPfxOptions); } else if (STORE_JKS.equalsIgnoreCase(sslOption.getTrustStoreType())) {
case PKCS12: PKCS12TrustStore pkcs12TrustStore = (PKCS12TrustStore) sslOptions.getTrustStore(); PfxOptions pfxOptions = new PfxOptions(); pfxOptions.setPassword(pkcs12TrustStore.getPassword()); if (pkcs12TrustStore.getPath() != null && !pkcs12TrustStore.getPath().isEmpty()) { pfxOptions.setPath(pkcs12TrustStore.getPath()); } else { pfxOptions.setValue(io.vertx.core.buffer.Buffer.buffer(pkcs12TrustStore.getContent())); case PKCS12: PKCS12KeyStore pkcs12KeyStore = (PKCS12KeyStore) sslOptions.getKeyStore(); PfxOptions pfxOptions = new PfxOptions(); pfxOptions.setPassword(pkcs12KeyStore.getPassword()); if (pkcs12KeyStore.getPath() != null && !pkcs12KeyStore.getPath().isEmpty()) { pfxOptions.setPath(pkcs12KeyStore.getPath()); } else if (pkcs12KeyStore.getContent() != null && !pkcs12KeyStore.getContent().isEmpty()) { pfxOptions.setValue(io.vertx.core.buffer.Buffer.buffer(pkcs12KeyStore.getContent()));
@Test public void testPKCS12Options() throws Exception { PfxOptions options = new PfxOptions(); assertNull(options.getPath()); String randString = TestUtils.randomAlphaString(100); assertEquals(options, options.setPath(randString)); assertEquals(randString, options.getPath()); assertNull(options.getPassword()); randString = TestUtils.randomAlphaString(100); assertEquals(options, options.setPassword(randString)); assertEquals(randString, options.getPassword()); }
public static TrustOptions randomTrustOptions() { TrustOptions trustOptions; switch (TestUtils.randomPositiveInt() % 3) { case 0: trustOptions = new JksOptions(); String tsPassword = TestUtils.randomAlphaString(100); ((JksOptions) trustOptions).setPassword(tsPassword); break; case 1: trustOptions = new PemTrustOptions(); Buffer keyValue = TestUtils.randomBuffer(100); ((PemTrustOptions) trustOptions).addCertValue(keyValue); break; default: trustOptions = new PfxOptions(); String pfxPassword = TestUtils.randomAlphaString(100); ((PfxOptions) trustOptions).setPassword(pfxPassword); } return trustOptions; }
@Override public PfxOptions clone() { return new PfxOptions(this); } }
@Test public void testPKCS12InvalidPassword() { testInvalidKeyStore(Cert.SERVER_PKCS12.get().setPassword("wrongpassword"), Arrays.asList( "failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded", "keystore password was incorrect"), null); }
@Test public void testKeyOptionsEquality() { JksOptions jksOptions = Cert.SERVER_JKS.get(); JksOptions jksOptionsCopy = new JksOptions(jksOptions); PfxOptions pfxOptions = Cert.SERVER_PKCS12.get(); PfxOptions pfxOptionsCopy = new PfxOptions(pfxOptions); PemKeyCertOptions pemKeyCertOptions = Cert.SERVER_PEM.get(); PemKeyCertOptions pemKeyCertOptionsCopy = new PemKeyCertOptions(pemKeyCertOptions); assertEquals(jksOptions, jksOptionsCopy); assertEquals(jksOptions.hashCode(), jksOptionsCopy.hashCode()); assertEquals(pfxOptions, pfxOptionsCopy); assertEquals(pfxOptions.hashCode(), pfxOptionsCopy.hashCode()); assertEquals(pemKeyCertOptions, pemKeyCertOptionsCopy); assertEquals(pemKeyCertOptions.hashCode(), pemKeyCertOptionsCopy.hashCode()); }
@Test public void testPKCS12InvalidPath() { testInvalidKeyStore(Cert.SERVER_PKCS12.get().setPath("/invalid.p12"), "java.nio.file.NoSuchFileException: ", "invalid.p12"); }
kpPassword = ((JksOptions) ksOptions).getPassword(); } else if (ksOptions instanceof PfxOptions) { kpPassword = ((PfxOptions) ksOptions).getPassword();
private void initializeTLSParameters(NetClientOptions options) { String keyStoreType = System.getProperty(JAVAX_NET_SSL_KEYSTORE_TYPE, KeyStore.getDefaultType()); if ("JKS".equalsIgnoreCase(keyStoreType)) { options.setKeyStoreOptions(new JksOptions() .setPath(System.getProperty(JAVAX_NET_SSL_KEYSTORE)) .setPassword(System.getProperty(JAVAX_NET_SSL_KEYSTORE_PASSWORD))); } else { options.setPfxKeyCertOptions(new PfxOptions() .setPath(System.getProperty(JAVAX_NET_SSL_KEYSTORE)) .setPassword(System.getProperty(JAVAX_NET_SSL_KEYSTORE_PASSWORD))); } String trustStoreType = System.getProperty(JAVAX_NET_SSL_TRUSTSTORE_TYPE, KeyStore.getDefaultType()); if ("JKS".equalsIgnoreCase(trustStoreType)) { options.setTrustStoreOptions(new JksOptions() .setPath(System.getProperty(JAVAX_NET_SSL_TRUSTSTORE)) .setPassword(System.getProperty(JAVAX_NET_SSL_TRUSTSTORE_PASSWORD))); } else { options.setPfxTrustOptions(new PfxOptions() .setPath(System.getProperty(JAVAX_NET_SSL_TRUSTSTORE)) .setPassword(System.getProperty(JAVAX_NET_SSL_TRUSTSTORE_PASSWORD))); } String allowedProtocols = System.getProperty(JDK_TLS_CLIENT_PROTOCOLS, "TLSv1.2"); Stream.of(allowedProtocols.split("\\s*,\\s*")) .forEach(options::addEnabledSecureTransportProtocol); String allowedCiphers = System.getProperty(HTTPS_CIPHERSUITES); if (allowedCiphers != null) { Stream.of(allowedCiphers.split("\\s*,\\s*")) .forEach(options::addEnabledCipherSuite); } }
case PKCS12: PKCS12TrustStore pkcs12TrustStore = (PKCS12TrustStore) sslOptions.getTrustStore(); PfxOptions pfxOptions = new PfxOptions(); pfxOptions.setPassword(pkcs12TrustStore.getPassword()); if (pkcs12TrustStore.getPath() != null && !pkcs12TrustStore.getPath().isEmpty()) { pfxOptions.setPath(pkcs12TrustStore.getPath()); } else { pfxOptions.setValue(io.vertx.core.buffer.Buffer.buffer(pkcs12TrustStore.getContent())); case PKCS12: PKCS12KeyStore pkcs12KeyStore = (PKCS12KeyStore) sslOptions.getKeyStore(); PfxOptions pfxOptions = new PfxOptions(); pfxOptions.setPassword(pkcs12KeyStore.getPassword()); if (pkcs12KeyStore.getPath() != null && !pkcs12KeyStore.getPath().isEmpty()) { pfxOptions.setPath(pkcs12KeyStore.getPath()); } else if (pkcs12KeyStore.getContent() != null && !pkcs12KeyStore.getContent().isEmpty()) { pfxOptions.setValue(io.vertx.core.buffer.Buffer.buffer(pkcs12KeyStore.getContent()));
@Test public void testPKCS12Options() throws Exception { PfxOptions options = new PfxOptions(); assertNull(options.getPath()); String randString = TestUtils.randomAlphaString(100); assertEquals(options, options.setPath(randString)); assertEquals(randString, options.getPath()); assertNull(options.getPassword()); randString = TestUtils.randomAlphaString(100); assertEquals(options, options.setPassword(randString)); assertEquals(randString, options.getPassword()); }
public static KeyCertOptions randomKeyCertOptions() { KeyCertOptions keyCertOptions; switch (TestUtils.randomPositiveInt() % 3) { case 0: keyCertOptions = new JksOptions(); String jksPassword = TestUtils.randomAlphaString(100); ((JksOptions) keyCertOptions).setPassword(jksPassword); break; case 1: keyCertOptions = new PemKeyCertOptions(); Buffer keyValue = TestUtils.randomBuffer(100); ((PemKeyCertOptions) keyCertOptions).setKeyValue(keyValue); break; default: keyCertOptions = new PfxOptions(); String pfxPassword = TestUtils.randomAlphaString(100); ((PfxOptions) keyCertOptions).setPassword(pfxPassword); } return keyCertOptions; }
@Test public void testPKCS12OptionsJson() throws Exception { PfxOptions options = new PfxOptions(new JsonObject()); assertEquals(null, options.getPassword()); assertEquals(null, options.getPath()); assertEquals(null, options.getValue()); String password = TestUtils.randomAlphaString(100); String path = TestUtils.randomAlphaString(100); String value = TestUtils.randomAlphaString(100); options = new PfxOptions(new JsonObject(). put("password", password). put("path", path). put("value", value.getBytes())); assertEquals(password, options.getPassword()); assertEquals(path, options.getPath()); assertEquals(Buffer.buffer(value), options.getValue()); }