83c154
# HG changeset patch
83c154
# User Daiki Ueno <dueno@redhat.com>
83c154
# Date 1594360877 -7200
83c154
#      Fri Jul 10 08:01:17 2020 +0200
83c154
# Node ID df1d2695e115ed9e6f7e8df6ad4d7be2c9bc77d8
83c154
# Parent  de661583d46713c9b4873a904dda3a8ba4a61976
83c154
Bug 1646324, advertise rsa_pkcs1_* schemes in CH and CR for certs, r=mt
83c154
83c154
Summary:
83c154
In TLS 1.3, unless "signature_algorithms_cert" is advertised, the
83c154
"signature_algorithms" extension is used as an indication of supported
83c154
algorithms for signatures on certificates.  While rsa_pkcs1_*
83c154
signatures schemes cannot be used for signing handshake messages, they
83c154
should be advertised if the peer wants to to support certificates
83c154
signed with RSA PKCS#1.
83c154
83c154
This adds a flag to ssl3_EncodeSigAlgs() and ssl3_FilterSigAlgs() to
83c154
preserve rsa_pkcs1_* schemes in the output.
83c154
83c154
Reviewers: mt
83c154
83c154
Reviewed By: mt
83c154
83c154
Bug #: 1646324
83c154
83c154
Differential Revision: https://phabricator.services.mozilla.com/D80881
83c154
83c154
diff -r de661583d467 -r df1d2695e115 gtests/ssl_gtest/ssl_auth_unittest.cc
83c154
--- a/gtests/ssl_gtest/ssl_auth_unittest.cc	Thu Jul 09 22:45:27 2020 +0000
83c154
+++ b/gtests/ssl_gtest/ssl_auth_unittest.cc	Fri Jul 10 08:01:17 2020 +0200
83c154
@@ -1591,6 +1591,47 @@
83c154
             capture->extension());
83c154
 }
83c154
 
83c154
+TEST_P(TlsConnectTls13, Tls13RsaPkcs1IsAdvertisedClient) {
83c154
+  EnsureTlsSetup();
83c154
+  static const SSLSignatureScheme kSchemes[] = {ssl_sig_rsa_pkcs1_sha256,
83c154
+                                                ssl_sig_rsa_pss_rsae_sha256};
83c154
+  client_->SetSignatureSchemes(kSchemes, PR_ARRAY_SIZE(kSchemes));
83c154
+  auto capture =
83c154
+      MakeTlsFilter<TlsExtensionCapture>(client_, ssl_signature_algorithms_xtn);
83c154
+  Connect();
83c154
+  // We should only have the one signature algorithm advertised.
83c154
+  static const uint8_t kExpectedExt[] = {0,
83c154
+                                         4,
83c154
+                                         ssl_sig_rsa_pss_rsae_sha256 >> 8,
83c154
+                                         ssl_sig_rsa_pss_rsae_sha256 & 0xff,
83c154
+                                         ssl_sig_rsa_pkcs1_sha256 >> 8,
83c154
+                                         ssl_sig_rsa_pkcs1_sha256 & 0xff};
83c154
+  ASSERT_EQ(DataBuffer(kExpectedExt, sizeof(kExpectedExt)),
83c154
+            capture->extension());
83c154
+}
83c154
+
83c154
+TEST_P(TlsConnectTls13, Tls13RsaPkcs1IsAdvertisedServer) {
83c154
+  EnsureTlsSetup();
83c154
+  static const SSLSignatureScheme kSchemes[] = {ssl_sig_rsa_pkcs1_sha256,
83c154
+                                                ssl_sig_rsa_pss_rsae_sha256};
83c154
+  server_->SetSignatureSchemes(kSchemes, PR_ARRAY_SIZE(kSchemes));
83c154
+  auto capture = MakeTlsFilter<TlsExtensionCapture>(
83c154
+      server_, ssl_signature_algorithms_xtn, true);
83c154
+  capture->SetHandshakeTypes({kTlsHandshakeCertificateRequest});
83c154
+  capture->EnableDecryption();
83c154
+  server_->RequestClientAuth(false);  // So we get a CertificateRequest.
83c154
+  Connect();
83c154
+  // We should only have the one signature algorithm advertised.
83c154
+  static const uint8_t kExpectedExt[] = {0,
83c154
+                                         4,
83c154
+                                         ssl_sig_rsa_pss_rsae_sha256 >> 8,
83c154
+                                         ssl_sig_rsa_pss_rsae_sha256 & 0xff,
83c154
+                                         ssl_sig_rsa_pkcs1_sha256 >> 8,
83c154
+                                         ssl_sig_rsa_pkcs1_sha256 & 0xff};
83c154
+  ASSERT_EQ(DataBuffer(kExpectedExt, sizeof(kExpectedExt)),
83c154
+            capture->extension());
83c154
+}
83c154
+
83c154
 // variant, version, certificate, auth type, signature scheme
83c154
 typedef std::tuple
83c154
                    SSLSignatureScheme>
83c154
diff -r de661583d467 -r df1d2695e115 lib/ssl/ssl3con.c
83c154
--- a/lib/ssl/ssl3con.c	Thu Jul 09 22:45:27 2020 +0000
83c154
+++ b/lib/ssl/ssl3con.c	Fri Jul 10 08:01:17 2020 +0200
83c154
@@ -784,15 +784,19 @@
83c154
  * Both by policy and by having a token that supports it. */
83c154
 static PRBool
83c154
 ssl_SignatureSchemeAccepted(PRUint16 minVersion,
83c154
-                            SSLSignatureScheme scheme)
83c154
+                            SSLSignatureScheme scheme,
83c154
+                            PRBool forCert)
83c154
 {
83c154
     /* Disable RSA-PSS schemes if there are no tokens to verify them. */
83c154
     if (ssl_IsRsaPssSignatureScheme(scheme)) {
83c154
         if (!PK11_TokenExists(auth_alg_defs[ssl_auth_rsa_pss])) {
83c154
             return PR_FALSE;
83c154
         }
83c154
-    } else if (ssl_IsRsaPkcs1SignatureScheme(scheme)) {
83c154
-        /* Disable PKCS#1 signatures if we are limited to TLS 1.3. */
83c154
+    } else if (!forCert && ssl_IsRsaPkcs1SignatureScheme(scheme)) {
83c154
+        /* Disable PKCS#1 signatures if we are limited to TLS 1.3.
83c154
+         * We still need to advertise PKCS#1 signatures in CH and CR
83c154
+         * for certificate signatures.
83c154
+         */
83c154
         if (minVersion >= SSL_LIBRARY_VERSION_TLS_1_3) {
83c154
             return PR_FALSE;
83c154
         }
83c154
@@ -851,7 +855,8 @@
83c154
     /* Ensure that there is a signature scheme that can be accepted.*/
83c154
     for (unsigned int i = 0; i < ss->ssl3.signatureSchemeCount; ++i) {
83c154
         if (ssl_SignatureSchemeAccepted(ss->vrange.min,
83c154
-                                        ss->ssl3.signatureSchemes[i])) {
83c154
+                                        ss->ssl3.signatureSchemes[i],
83c154
+                                        PR_FALSE /* forCert */)) {
83c154
             return SECSuccess;
83c154
         }
83c154
     }
83c154
@@ -880,7 +885,7 @@
83c154
         PRBool acceptable = authType == schemeAuthType ||
83c154
                             (schemeAuthType == ssl_auth_rsa_pss &&
83c154
                              authType == ssl_auth_rsa_sign);
83c154
-        if (acceptable && ssl_SignatureSchemeAccepted(ss->version, scheme)) {
83c154
+        if (acceptable && ssl_SignatureSchemeAccepted(ss->version, scheme, PR_FALSE /* forCert */)) {
83c154
             return PR_TRUE;
83c154
         }
83c154
     }
83c154
@@ -9803,12 +9808,13 @@
83c154
 }
83c154
 
83c154
 SECStatus
83c154
-ssl3_EncodeSigAlgs(const sslSocket *ss, PRUint16 minVersion, sslBuffer *buf)
83c154
+ssl3_EncodeSigAlgs(const sslSocket *ss, PRUint16 minVersion, PRBool forCert,
83c154
+                   sslBuffer *buf)
83c154
 {
83c154
     SSLSignatureScheme filtered[MAX_SIGNATURE_SCHEMES] = { 0 };
83c154
     unsigned int filteredCount = 0;
83c154
 
83c154
-    SECStatus rv = ssl3_FilterSigAlgs(ss, minVersion, PR_FALSE,
83c154
+    SECStatus rv = ssl3_FilterSigAlgs(ss, minVersion, PR_FALSE, forCert,
83c154
                                       PR_ARRAY_SIZE(filtered),
83c154
                                       filtered, &filteredCount);
83c154
     if (rv != SECSuccess) {
83c154
@@ -9843,8 +9849,21 @@
83c154
     return sslBuffer_InsertLength(buf, lengthOffset, 2);
83c154
 }
83c154
 
83c154
+/*
83c154
+ * In TLS 1.3 we are permitted to advertise support for PKCS#1
83c154
+ * schemes. This doesn't affect the signatures in TLS itself, just
83c154
+ * those on certificates. Not advertising PKCS#1 signatures creates a
83c154
+ * serious compatibility risk as it excludes many certificate chains
83c154
+ * that include PKCS#1. Hence, forCert is used to enable advertising
83c154
+ * PKCS#1 support. Note that we include these in signature_algorithms
83c154
+ * because we don't yet support signature_algorithms_cert. TLS 1.3
83c154
+ * requires that PKCS#1 schemes are placed last in the list if they
83c154
+ * are present. This sorting can be removed once we support
83c154
+ * signature_algorithms_cert.
83c154
+ */
83c154
 SECStatus
83c154
 ssl3_FilterSigAlgs(const sslSocket *ss, PRUint16 minVersion, PRBool disableRsae,
83c154
+                   PRBool forCert,
83c154
                    unsigned int maxSchemes, SSLSignatureScheme *filteredSchemes,
83c154
                    unsigned int *numFilteredSchemes)
83c154
 {
83c154
@@ -9856,15 +9875,32 @@
83c154
     }
83c154
 
83c154
     *numFilteredSchemes = 0;
83c154
+    PRBool allowUnsortedPkcs1 = forCert && minVersion < SSL_LIBRARY_VERSION_TLS_1_3;
83c154
     for (unsigned int i = 0; i < ss->ssl3.signatureSchemeCount; ++i) {
83c154
         if (disableRsae && ssl_IsRsaeSignatureScheme(ss->ssl3.signatureSchemes[i])) {
83c154
             continue;
83c154
         }
83c154
         if (ssl_SignatureSchemeAccepted(minVersion,
83c154
-                                        ss->ssl3.signatureSchemes[i])) {
83c154
+                                        ss->ssl3.signatureSchemes[i],
83c154
+                                        allowUnsortedPkcs1)) {
83c154
             filteredSchemes[(*numFilteredSchemes)++] = ss->ssl3.signatureSchemes[i];
83c154
         }
83c154
     }
83c154
+    if (forCert && !allowUnsortedPkcs1) {
83c154
+        for (unsigned int i = 0; i < ss->ssl3.signatureSchemeCount; ++i) {
83c154
+            if (disableRsae && ssl_IsRsaeSignatureScheme(ss->ssl3.signatureSchemes[i])) {
83c154
+                continue;
83c154
+            }
83c154
+            if (!ssl_SignatureSchemeAccepted(minVersion,
83c154
+                                             ss->ssl3.signatureSchemes[i],
83c154
+                                             PR_FALSE) &&
83c154
+                ssl_SignatureSchemeAccepted(minVersion,
83c154
+                                            ss->ssl3.signatureSchemes[i],
83c154
+                                            PR_TRUE)) {
83c154
+                filteredSchemes[(*numFilteredSchemes)++] = ss->ssl3.signatureSchemes[i];
83c154
+            }
83c154
+        }
83c154
+    }
83c154
     return SECSuccess;
83c154
 }
83c154
 
83c154
@@ -9901,7 +9937,7 @@
83c154
 
83c154
     length = 1 + certTypesLength + 2 + calen;
83c154
     if (isTLS12) {
83c154
-        rv = ssl3_EncodeSigAlgs(ss, ss->version, &sigAlgsBuf);
83c154
+        rv = ssl3_EncodeSigAlgs(ss, ss->version, PR_TRUE /* forCert */, &sigAlgsBuf);
83c154
         if (rv != SECSuccess) {
83c154
             return rv;
83c154
         }
83c154
diff -r de661583d467 -r df1d2695e115 lib/ssl/ssl3exthandle.c
83c154
--- a/lib/ssl/ssl3exthandle.c	Thu Jul 09 22:45:27 2020 +0000
83c154
+++ b/lib/ssl/ssl3exthandle.c	Fri Jul 10 08:01:17 2020 +0200
83c154
@@ -1652,7 +1652,7 @@
83c154
         minVersion = ss->vrange.min; /* ClientHello */
83c154
     }
83c154
 
83c154
-    SECStatus rv = ssl3_EncodeSigAlgs(ss, minVersion, buf);
83c154
+    SECStatus rv = ssl3_EncodeSigAlgs(ss, minVersion, PR_TRUE /* forCert */, buf);
83c154
     if (rv != SECSuccess) {
83c154
         return SECFailure;
83c154
     }
83c154
diff -r de661583d467 -r df1d2695e115 lib/ssl/sslimpl.h
83c154
--- a/lib/ssl/sslimpl.h	Thu Jul 09 22:45:27 2020 +0000
83c154
+++ b/lib/ssl/sslimpl.h	Fri Jul 10 08:01:17 2020 +0200
83c154
@@ -1688,12 +1688,12 @@
83c154
 SECStatus ssl3_AuthCertificate(sslSocket *ss);
83c154
 SECStatus ssl_ReadCertificateStatus(sslSocket *ss, PRUint8 *b,
83c154
                                     PRUint32 length);
83c154
-SECStatus ssl3_EncodeSigAlgs(const sslSocket *ss, PRUint16 minVersion,
83c154
+SECStatus ssl3_EncodeSigAlgs(const sslSocket *ss, PRUint16 minVersion, PRBool forCert,
83c154
                              sslBuffer *buf);
83c154
 SECStatus ssl3_EncodeFilteredSigAlgs(const sslSocket *ss,
83c154
                                      const SSLSignatureScheme *schemes,
83c154
                                      PRUint32 numSchemes, sslBuffer *buf);
83c154
-SECStatus ssl3_FilterSigAlgs(const sslSocket *ss, PRUint16 minVersion, PRBool disableRsae,
83c154
+SECStatus ssl3_FilterSigAlgs(const sslSocket *ss, PRUint16 minVersion, PRBool disableRsae, PRBool forCert,
83c154
                              unsigned int maxSchemes, SSLSignatureScheme *filteredSchemes,
83c154
                              unsigned int *numFilteredSchemes);
83c154
 SECStatus ssl_GetCertificateRequestCAs(const sslSocket *ss,
83c154
diff -r de661583d467 -r df1d2695e115 lib/ssl/tls13exthandle.c
83c154
--- a/lib/ssl/tls13exthandle.c	Thu Jul 09 22:45:27 2020 +0000
83c154
+++ b/lib/ssl/tls13exthandle.c	Fri Jul 10 08:01:17 2020 +0200
83c154
@@ -1519,7 +1519,8 @@
83c154
     SSLSignatureScheme filtered[MAX_SIGNATURE_SCHEMES] = { 0 };
83c154
     unsigned int filteredCount = 0;
83c154
     SECStatus rv = ssl3_FilterSigAlgs(ss, ss->vrange.max,
83c154
-                                      PR_TRUE,
83c154
+                                      PR_TRUE /* disableRsae */,
83c154
+                                      PR_FALSE /* forCert */,
83c154
                                       MAX_SIGNATURE_SCHEMES,
83c154
                                       filtered,
83c154
                                       &filteredCount);