From 99af5acc8ff2fadf255518b6e086e3908ee4dd1a Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Thu, 6 May 2021 22:08:11 +0200 Subject: [PATCH] Fixes #6099 - Cipher preference may break SNI if certificates have different key types. Updated the logic in SslContextFactory.Server.sniSelect(...) to check if there is any certificate that matches, and if so return a null alias in the hope to be called again and pick the right alias for the SNI. Signed-off-by: Simone Bordet (cherry picked from commit 68296911b7ee58a9853b1baadbba549fb89f3904) --- .../ssl/SniSslConnectionFactoryTest.java | 16 ++++++++ .../test/resources/keystore_sni_key_types.p12 | Bin 0 -> 3348 bytes .../util/ssl/SniX509ExtendedKeyManager.java | 2 +- .../jetty/util/ssl/SslContextFactory.java | 37 +++++++++++------- 4 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 jetty-server/src/test/resources/keystore_sni_key_types.p12 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 9b78f593977f..91954a40065a 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 @@ -40,6 +40,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpTester; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.io.Connection; @@ -489,6 +490,21 @@ protected void customize(Socket socket, Class connection, assertEquals(0, history.size()); } + @Test + public void testSNIWithDifferentKeyTypes() throws Exception + { + // This KeyStore contains 2 certificates, one with keyAlg=EC, one with keyAlg=RSA. + start(ssl -> ssl.setKeyStorePath("src/test/resources/keystore_sni_key_types.p12")); + + // Make a request with SNI = rsa.domain.com, the RSA certificate should be chosen. + HttpTester.Response response1 = HttpTester.parseResponse(getResponse("rsa.domain.com", "rsa.domain.com")); + assertEquals(HttpStatus.OK_200, response1.getStatus()); + + // Make a request with SNI = ec.domain.com, the EC certificate should be chosen. + HttpTester.Response response2 = HttpTester.parseResponse(getResponse("ec.domain.com", "ec.domain.com")); + assertEquals(HttpStatus.OK_200, response2.getStatus()); + } + private String getResponse(String host, String cn) throws Exception { String response = getResponse(host, host, cn); diff --git a/jetty-server/src/test/resources/keystore_sni_key_types.p12 b/jetty-server/src/test/resources/keystore_sni_key_types.p12 new file mode 100644 index 0000000000000000000000000000000000000000..b000fbeff57371976aa04a8c0961db5420ece8cb GIT binary patch literal 3348 zcmaKtcQhLc`^SZd+FKDT#4Zvgh*301mG;_75v^6D)-`L!Y7w+*6|r}z8Ld5Pw>3L# ztx|i%j8@*hzj5!m=RN2B>pAE1J>Ta%-+#Uj49mzu37~>u8HPZ#QnBdR6Gi|HARo)n z0>m;9e&c!=79{o$33M5V1+o9e%&^EV5dHs|80Y|$`2jFLm?Vhq|IzQgU?9f|f$i38 zIT)jp2exu)sp}mj6-DnKK=gNa+w|hH#z=>PRb(d_J?8gD_k2@((*D|sLCl-@upO!nG+NqTh3hR{Dltdoe%~$|Ljocn+Fj-SeG?4!MEOa4nSI?;^ zN2OxHrH#}knrd2U`1VKIxx5IhZY6_qniXiHrv!altkD$9H+^ttN7DGSI9nzNpl&494{P0E z(`C83RNM}%?jUmA{ZOTGdaV~ZXsN1@+Iec<{tWWy%t4Nt8q{VM(P?JrbX;$?QtGsE zvB-8DNZT9Wt=%wxYWj)xZW#Ri+Q^B;0e`jIrOyn8AFV?urPQw%2t>!GFgj*qCri`& zz2FMZ6+Ss4^ShOJrHO5F=_xOx>8jK&)iu&>=oU`7PSW%xtVjl^CU$JSuyAOVBv_(qd( z5yhSNT_YxDAP5T(v=S2}qSE+614@a#S++~^{y5~umoA6;B)+jh<6_5(^D04lC31iE zQfgQfU$wyUsG=S6%Qa!AR$(1Mey5>@W*zL{xO2D{Uib>DbZc*00^#kocn3e?{vp|l zJN^xxa=z}1Qr1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Wc(gZq$f(C-R|&fSyWtwY-l^J1c@!apxmC&2vf%4Q zl@%n0cJ^8mxvxCCYSa#070#{w(KkqZWeV?$lSGgoJ}XtItO1o4s3Axz*%dX2gO?DL zvQNpr1LN^$)r~}I52=rY$1j7esRIVN8%ne=_2PLYt;wHmWPGDuJY9R;v0Oc4o6AL_ zrdRfr)rI+;m{qsgXuHLDoMeHupOyqpqG7Q~cg`n!xU*yYv}oz3gPxS-xBXeNUUi7E zCSNCUmcN85^kTNfhM^pNcsyT&V^zr`yUZ7ru2wN0s4OgXm5gFZ%1Xs7qVKnWSdz;^ zLYdM)WRTR%2M?QL~NTZK6iaHgIU88{88a$dS1@p_CtZq z#~GUYMJ&;~LlOqQXSow8`iM2YMmlMrmBX};P@GU`&$$zkHFlwSrEB;0wyb^F((bLk zj0o8-T+Fq{VRLl7;k)}8&V85Ces&?1>{tYq(}-u+@2cM|9TZ(h&~IhR)Ti{hM^^uY z3O$x|<(p)FzT8@$amPrWc|4^Qg=>~V?@+wCe)|ZfHn%KK>1Selects a certificate based on SNI information.

*

This method may be invoked multiple times during the TLS handshake, with different parameters. * For example, the {@code keyType} could be different, and subsequently the collection of certificates - * (because they need to match the {@code keyType}.

+ * (because they need to match the {@code keyType}).

* * @param keyType the key algorithm type name * @param issuers the list of acceptable CA issuer subject names or null if it does not matter which issuers are used diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java b/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java index 1104dbd19df3..a445cad47903 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java @@ -2155,12 +2155,17 @@ public void setWantClientAuth(boolean wantClientAuth) } /** - * Does the default {@link #sniSelect(String, Principal[], SSLSession, String, Collection)} implementation - * require an SNI match? Note that if a non SNI handshake is accepted, requests may still be rejected - * at the HTTP level for incorrect SNI (see SecureRequestCustomizer). + *

Returns whether an SNI match is required when choosing the alias that + * identifies the certificate to send to the client.

+ *

The exact logic to choose an alias given the SNI is configurable via + * {@link #setSNISelector(SniX509ExtendedKeyManager.SniSelector)}.

+ *

The default implementation is {@link #sniSelect(String, Principal[], SSLSession, String, Collection)} + * and if SNI is not required it will delegate the TLS implementation to + * choose an alias (typically the first alias in the KeyStore).

+ *

Note that if a non SNI handshake is accepted, requests may still be rejected + * at the HTTP level for incorrect SNI (see SecureRequestCustomizer).

* - * @return true if no SNI match is handled as no certificate match, false if no SNI match is handled by - * delegation to the non SNI matching methods. + * @return whether an SNI match is required when choosing the alias that identifies the certificate */ @ManagedAttribute("Whether the TLS handshake is rejected if there is no SNI host match") public boolean isSniRequired() @@ -2169,14 +2174,12 @@ public boolean isSniRequired() } /** - * Set if the default {@link #sniSelect(String, Principal[], SSLSession, String, Collection)} implementation - * require an SNI match? Note that if a non SNI handshake is accepted, requests may still be rejected - * at the HTTP level for incorrect SNI (see SecureRequestCustomizer). - * This setting may have no effect if {@link #sniSelect(String, Principal[], SSLSession, String, Collection)} is - * overridden or a non null function is passed to {@link #setSNISelector(SniX509ExtendedKeyManager.SniSelector)}. + *

Sets whether an SNI match is required when choosing the alias that + * identifies the certificate to send to the client.

+ *

This setting may have no effect if {@link #sniSelect(String, Principal[], SSLSession, String, Collection)} is + * overridden or a custom function is passed to {@link #setSNISelector(SniX509ExtendedKeyManager.SniSelector)}.

* - * @param sniRequired true if no SNI match is handled as no certificate match, false if no SNI match is handled by - * delegation to the non SNI matching methods. + * @param sniRequired whether an SNI match is required when choosing the alias that identifies the certificate */ public void setSniRequired(boolean sniRequired) { @@ -2244,9 +2247,15 @@ public String sniSelect(String keyType, Principal[] issuers, SSLSession session, .filter(x509 -> x509.matches(sniHost)) .collect(Collectors.toList()); - // No match, let the JDK decide unless unmatched SNIs are rejected. if (matching.isEmpty()) - return isSniRequired() ? null : SniX509ExtendedKeyManager.SniSelector.DELEGATE; + { + // There is no match for this SNI among the certificates valid for + // this keyType; check if there is any certificate that matches this + // SNI, as we will likely be called again with a different keyType. + boolean anyMatching = aliasCerts().values().stream() + .anyMatch(x509 -> x509.matches(sniHost)); + return isSniRequired() || anyMatching ? null : SniX509ExtendedKeyManager.SniSelector.DELEGATE; + } String alias = matching.get(0).getAlias(); if (matching.size() == 1)