From 8de7b839668986770d137957dc125417d63f46ce Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Thu, 4 Mar 2021 13:09:24 +0100 Subject: [PATCH] Fixes #6034 - SslContextFactory may select a wildcard certificate during SNI selection when a more specific SSL certificate is present. Now matching certificates are sorted, non-wildcard first, so that a more specific alias is returned. Signed-off-by: Simone Bordet --- .../ssl/SniSslConnectionFactoryTest.java | 27 +++++++++++++++++- .../src/test/resources/keystore_sni.p12 | Bin 8255 -> 10672 bytes .../jetty/util/ssl/SslContextFactory.java | 24 ++++++++++++---- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SniSslConnectionFactoryTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SniSslConnectionFactoryTest.java index 080437c0e3c..9b78f593977 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SniSslConnectionFactoryTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SniSslConnectionFactoryTest.java @@ -28,6 +28,7 @@ import java.util.Queue; import java.util.concurrent.LinkedBlockingQueue; import java.util.function.BiConsumer; import java.util.function.Consumer; +import java.util.stream.Collectors; import javax.net.ssl.SNIHostName; import javax.net.ssl.SNIServerName; import javax.net.ssl.SSLEngine; @@ -58,6 +59,7 @@ import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.ssl.SniX509ExtendedKeyManager; import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.eclipse.jetty.util.ssl.X509; import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -163,7 +165,27 @@ public class SniSslConnectionFactoryTest @Test public void testSNIConnect() throws Exception { - start("src/test/resources/keystore_sni.p12"); + start(ssl -> + { + ssl.setKeyStorePath("src/test/resources/keystore_sni.p12"); + ssl.setSNISelector((keyType, issuers, session, sniHost, certificates) -> + { + // Make sure the *.domain.com comes before sub.domain.com + // to test that we prefer more specific domains. + List sortedCertificates = certificates.stream() + // As sorted() sorts ascending, make *.domain.com the smallest. + .sorted((x509a, x509b) -> + { + if (x509a.matches("domain.com")) + return -1; + if (x509b.matches("domain.com")) + return 1; + return 0; + }) + .collect(Collectors.toList()); + return ssl.sniSelect(keyType, issuers, session, sniHost, sortedCertificates); + }); + }); String response = getResponse("jetty.eclipse.org", "jetty.eclipse.org"); assertThat(response, Matchers.containsString("X-HOST: jetty.eclipse.org")); @@ -174,6 +196,9 @@ public class SniSslConnectionFactoryTest response = getResponse("foo.domain.com", "*.domain.com"); assertThat(response, Matchers.containsString("X-HOST: foo.domain.com")); + response = getResponse("sub.domain.com", "sub.domain.com"); + assertThat(response, Matchers.containsString("X-HOST: sub.domain.com")); + response = getResponse("m.san.com", "san example"); assertThat(response, Matchers.containsString("X-HOST: m.san.com")); diff --git a/jetty-server/src/test/resources/keystore_sni.p12 b/jetty-server/src/test/resources/keystore_sni.p12 index 39bee9040537f5e5759834762a89c160fb1046b8..e1028961f4ba7210f70e8d856288451758dc492f 100644 GIT binary patch delta 7027 zcmY+GRZyJ^ux4T7?hxF9ySqCC2@pKE+s2*z8+Uhi3GTr?IKkZ|KyY`NQ#H5l)I5FF zt84W`KXi4Ec%6m_dmuv<2pZL){|~FM2jU|Ce=VFB z*g%{I_CTDQdJ%JU04@d|<$pv}WN1)+Ae=aRAe=C}<^M`>XzaS|{Qs{+WoLrLwW4D@ zhJ__;aoth5AZx&!=2CC71A*u;LC_TJsPKsYcOnQ04vL)=9?3XV9@+v17Mc+jGfweK zBdxxY_W-o2#VBNUAAk*no%Zf0VN<9@&%5KwBN2EUX1{1+KU6$p!ZCc?$}uVS`+{4Zd-VCaa*v3ePmU~#PG99 zouOiy+%tM21uiN};?}Th?McF?PNXaw+&1}F zhumnI5fz@#9Mv?KdZRCY8*tTdTDMh+H|drC9q;bro3K7g=sZDtm2k1KN7T}e+`BfO z9I)*i4COVU|5$BA%TKe-fVifNn=>qMiXBSg6s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OeROJR!e!;D!ggk~TPx%zf8D+}edea0euBgy*h9$f} zIXn(fE%VpXihxDV3lHCFx7^B-w44wu(IXCm`UTheSO1iPXTSj7pD##lygYxS=!Y}Z z?Yzh`#LxTD41%_Fe{F1gwfYC0ANFf!05)o?_fNQ_I5z5}@=5DwA!{A(_wQ810+SXx zm2faz3Nn7Y^PGL8rVI$zH-k8c$5!0zSG4RvZ%*<){bM;F&~M7Vn`0d#iR08bG>6@D z3G4~wN=`pf@HE|htAn~za1VdZHr96j4){}8nZC*JpQj*uT1m=p0V!)e1ld(77Xf1( zhQQ*H-NiF!IU^YQhx(rCT$;ss0!v{xqQOxOn|MibgkAb@MVeG`oNO~{N~KwI!z5bM z(w=JUm%%jl7QpsNTyZH6FKY!&UnCEn`3Xd}p;h7|5U}qi`XM^*$Q*wY33K1nt`BAb zASijmW)wuq%kewgizqa>L64b-!D*3V%2E`o>-Fx)3{iR?AKD)8Qk)fn1-p`w5L-Q~`Fd;Ar1_dh)0|FWa00b14yi>Ol_KGBs*;vp@uid2qDqti86x2X@ a9>!QlC certificates) throws SSLHandshakeException + public String sniSelect(String keyType, Principal[] issuers, SSLSession session, String sniHost, Collection certificates) { if (sniHost == null) { @@ -2239,12 +2239,24 @@ public abstract class SslContextFactory extends AbstractLifeCycle implements Dum } else { - // Match the SNI host, or let the JDK decide unless unmatched SNIs are rejected. - return certificates.stream() + // Match the SNI host. + List matching = certificates.stream() .filter(x509 -> x509.matches(sniHost)) - .findFirst() + .collect(Collectors.toList()); + + // No match, let the JDK decide unless unmatched SNIs are rejected. + if (matching.isEmpty()) + return isSniRequired() ? null : SniX509ExtendedKeyManager.SniSelector.DELEGATE; + + String alias = matching.get(0).getAlias(); + if (matching.size() == 1) + return alias; + + // Prefer strict matches over wildcard matches. + return matching.stream() + .min(Comparator.comparingInt(cert -> cert.getWilds().size())) .map(X509::getAlias) - .orElse(_sniRequired ? null : SniX509ExtendedKeyManager.SniSelector.DELEGATE); + .orElse(alias); } }