Skip to content

Commit

Permalink
Rework a bit to simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Jan 10, 2018
1 parent 4087c7c commit 674074c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 46 deletions.
9 changes: 3 additions & 6 deletions src/main/java/io/vertx/core/net/TrustOptions.java
Expand Up @@ -15,6 +15,7 @@
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.net.impl.KeyStoreHelper;

import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.util.function.Function;

Expand Down Expand Up @@ -54,12 +55,8 @@ default TrustManagerFactory getTrustManagerFactory(Vertx vertx) throws Exception
* @param vertx the vertx instance
* @return the trustManager
*/
default Function<String, TrustManagerFactory> trustManagerMapper(Vertx vertx) throws Exception {
default Function<String, TrustManager[]> trustManagerMapper(Vertx vertx) throws Exception {
KeyStoreHelper helper = KeyStoreHelper.create((VertxInternal) vertx, this);
if (helper == null){
// if there is no KeyStoreHelper for the concrete TrustOptions type return a function which always returns null.
return (hostName) -> null;
}
return helper::getTrustMgr;
return helper != null ? helper::getTrustMgr : null;
}
}
5 changes: 3 additions & 2 deletions src/main/java/io/vertx/core/net/impl/KeyStoreHelper.java
Expand Up @@ -239,8 +239,9 @@ public KeyManager[] getKeyMgr() throws Exception {
return getKeyMgrFactory().getKeyManagers();
}

public TrustManagerFactory getTrustMgr(String serverName) {
return trustMgrMap.get(serverName);
public TrustManager[] getTrustMgr(String serverName) {
TrustManagerFactory fact = trustMgrMap.get(serverName);
return fact != null ? fact.getTrustManagers() : null;
}

public TrustManagerFactory getTrustMgrFactory(VertxInternal vertx) throws Exception {
Expand Down
33 changes: 21 additions & 12 deletions src/main/java/io/vertx/core/net/impl/SSLHelper.java
Expand Up @@ -41,6 +41,7 @@
import java.security.cert.X509Certificate;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -305,20 +306,29 @@ private KeyManagerFactory getKeyMgrFactory(VertxInternal vertx) throws Exception
}

private TrustManagerFactory getTrustMgrFactory(VertxInternal vertx, String serverName) throws Exception {
TrustManagerFactory fact;
TrustManager[] mgrs = null;
if (trustAll) {
TrustManager[] mgrs = new TrustManager[]{createTrustAllTrustManager()};
fact = new VertxTrustManagerFactory(mgrs);
mgrs = new TrustManager[]{createTrustAllTrustManager()};
} else if (trustOptions != null) {
if (serverName != null){
fact = trustOptions.trustManagerMapper(vertx).apply(serverName);
if (fact == null){
fact = trustOptions.getTrustManagerFactory(vertx);
if (serverName != null) {
Function<String, TrustManager[]> mapper = trustOptions.trustManagerMapper(vertx);
if (mapper != null) {
mgrs = mapper.apply(serverName);
}
if (mgrs == null) {
TrustManagerFactory fact = trustOptions.getTrustManagerFactory(vertx);
if (fact != null) {
mgrs = fact.getTrustManagers();
}
}
} else {
fact = trustOptions.getTrustManagerFactory(vertx);
TrustManagerFactory fact = trustOptions.getTrustManagerFactory(vertx);
if (fact != null) {
mgrs = fact.getTrustManagers();
}
}
} else {
}
if (mgrs == null) {
return null;
}
if (crlPaths != null && crlValues != null && (crlPaths.size() > 0 || crlValues.size() > 0)) {
Expand All @@ -332,10 +342,9 @@ private TrustManagerFactory getTrustMgrFactory(VertxInternal vertx, String serve
for (Buffer crlValue : tmp.collect(Collectors.toList())) {
crls.addAll(certificatefactory.generateCRLs(new ByteArrayInputStream(crlValue.getBytes())));
}
TrustManager[] mgrs = createUntrustRevokedCertTrustManager(fact.getTrustManagers(), crls);
fact = new VertxTrustManagerFactory(mgrs);
mgrs = createUntrustRevokedCertTrustManager(mgrs, crls);
}
return fact;
return new VertxTrustManagerFactory(mgrs);
}

/*
Expand Down
28 changes: 2 additions & 26 deletions src/test/java/io/vertx/test/core/HttpTLSTest.java
Expand Up @@ -770,32 +770,8 @@ public TrustOptions clone() {
public void testSNICustomTrustManagerFactoryMapper2() throws Exception {
testTLS(Cert.CLIENT_PEM, Trust.SNI_JKS_HOST2, Cert.SNI_JKS, () -> new TrustOptions() {
@Override
public Function<String, TrustManagerFactory> trustManagerMapper(Vertx v) throws Exception {
return (serverName) -> {
try {
return new TrustManagerFactory(new TrustManagerFactorySpi() {
@Override
protected void engineInit(KeyStore keyStore) throws KeyStoreException {
}

@Override
protected void engineInit(ManagerFactoryParameters managerFactoryParameters) throws
InvalidAlgorithmParameterException {

}

@Override
protected TrustManager[] engineGetTrustManagers() {
return new TrustManager[]{TrustAllTrustManager.INSTANCE};
}
}, KeyPairGenerator.getInstance("RSA")
.getProvider(), KeyPairGenerator.getInstance("RSA")
.getAlgorithm()) {
};
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
};
public Function<String, TrustManager[]> trustManagerMapper(Vertx v) throws Exception {
return (serverName) -> new TrustManager[]{TrustAllTrustManager.INSTANCE};
}

@Override
Expand Down

0 comments on commit 674074c

Please sign in to comment.