Blame SOURCES/0005-curl-7.76.1-CVE-2021-22924.patch

d889a8
From 30c7b4dd01734b6ba20bfc7790b9fe8bc0500214 Mon Sep 17 00:00:00 2001
d889a8
From: Daniel Stenberg <daniel@haxx.se>
d889a8
Date: Sat, 19 Jun 2021 00:42:28 +0200
d889a8
Subject: [PATCH] vtls: fix connection reuse checks for issuer cert and case
d889a8
 sensitivity
d889a8
d889a8
CVE-2021-22924
d889a8
d889a8
Reported-by: Harry Sintonen
d889a8
Bug: https://curl.se/docs/CVE-2021-22924.html
d889a8
d889a8
Upstream-commit: 5ea3145850ebff1dc2b13d17440300a01ca38161
d889a8
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
d889a8
---
d889a8
 lib/url.c          | 10 ++++++----
d889a8
 lib/urldata.h      |  4 ++--
d889a8
 lib/vtls/gtls.c    | 10 +++++-----
d889a8
 lib/vtls/nss.c     |  4 ++--
d889a8
 lib/vtls/openssl.c | 18 +++++++++---------
d889a8
 lib/vtls/vtls.c    | 26 +++++++++++++++++++++-----
d889a8
 6 files changed, 45 insertions(+), 27 deletions(-)
d889a8
d889a8
diff --git a/lib/url.c b/lib/url.c
d889a8
index 9f2c9f2..bdcb095 100644
d889a8
--- a/lib/url.c
d889a8
+++ b/lib/url.c
d889a8
@@ -3723,6 +3723,8 @@ static CURLcode create_conn(struct Curl_easy *data,
d889a8
   */
d889a8
   data->set.ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH];
d889a8
   data->set.ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE];
d889a8
+  data->set.ssl.primary.issuercert = data->set.str[STRING_SSL_ISSUERCERT];
d889a8
+  data->set.ssl.primary.issuercert_blob = data->set.blobs[BLOB_SSL_ISSUERCERT];
d889a8
   data->set.ssl.primary.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
d889a8
   data->set.ssl.primary.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
d889a8
   data->set.ssl.primary.cipher_list =
d889a8
@@ -3747,8 +3749,11 @@ static CURLcode create_conn(struct Curl_easy *data,
d889a8
   data->set.proxy_ssl.primary.pinned_key =
d889a8
     data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY];
d889a8
   data->set.proxy_ssl.primary.cert_blob = data->set.blobs[BLOB_CERT_PROXY];
d889a8
+  data->set.proxy_ssl.primary.issuercert =
d889a8
+    data->set.str[STRING_SSL_ISSUERCERT_PROXY];
d889a8
+  data->set.proxy_ssl.primary.issuercert_blob =
d889a8
+    data->set.blobs[BLOB_SSL_ISSUERCERT_PROXY];
d889a8
   data->set.proxy_ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE_PROXY];
d889a8
-  data->set.proxy_ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT_PROXY];
d889a8
   data->set.proxy_ssl.cert_type = data->set.str[STRING_CERT_TYPE_PROXY];
d889a8
   data->set.proxy_ssl.key = data->set.str[STRING_KEY_PROXY];
d889a8
   data->set.proxy_ssl.key_type = data->set.str[STRING_KEY_TYPE_PROXY];
d889a8
@@ -3757,7 +3762,6 @@ static CURLcode create_conn(struct Curl_easy *data,
d889a8
   data->set.proxy_ssl.key_blob = data->set.blobs[BLOB_KEY_PROXY];
d889a8
 #endif
d889a8
   data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE];
d889a8
-  data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT];
d889a8
   data->set.ssl.cert_type = data->set.str[STRING_CERT_TYPE];
d889a8
   data->set.ssl.key = data->set.str[STRING_KEY];
d889a8
   data->set.ssl.key_type = data->set.str[STRING_KEY_TYPE];
d889a8
@@ -3771,9 +3775,7 @@ static CURLcode create_conn(struct Curl_easy *data,
d889a8
   data->set.proxy_ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD_PROXY];
d889a8
 #endif
d889a8
 #endif
d889a8
-
d889a8
   data->set.ssl.key_blob = data->set.blobs[BLOB_KEY];
d889a8
-  data->set.ssl.issuercert_blob = data->set.blobs[BLOB_SSL_ISSUERCERT];
d889a8
 
d889a8
   if(!Curl_clone_primary_ssl_config(&data->set.ssl.primary,
d889a8
                                     &conn->ssl_config)) {
d889a8
diff --git a/lib/urldata.h b/lib/urldata.h
d889a8
index 2bb7d81..7cf63d0 100644
d889a8
--- a/lib/urldata.h
d889a8
+++ b/lib/urldata.h
d889a8
@@ -246,6 +246,7 @@ struct ssl_primary_config {
d889a8
   long version_max;      /* max supported version the client wants to use*/
d889a8
   char *CApath;          /* certificate dir (doesn't work on windows) */
d889a8
   char *CAfile;          /* certificate to verify peer against */
d889a8
+  char *issuercert;      /* optional issuer certificate filename */
d889a8
   char *clientcert;
d889a8
   char *random_file;     /* path to file containing "random" data */
d889a8
   char *egdsocket;       /* path to file containing the EGD daemon socket */
d889a8
@@ -253,6 +254,7 @@ struct ssl_primary_config {
d889a8
   char *cipher_list13;   /* list of TLS 1.3 cipher suites to use */
d889a8
   char *pinned_key;
d889a8
   struct curl_blob *cert_blob;
d889a8
+  struct curl_blob *issuercert_blob;
d889a8
   char *curves;          /* list of curves to use */
d889a8
   BIT(verifypeer);       /* set TRUE if this is desired */
d889a8
   BIT(verifyhost);       /* set TRUE if CN/SAN must match hostname */
d889a8
@@ -264,8 +266,6 @@ struct ssl_config_data {
d889a8
   struct ssl_primary_config primary;
d889a8
   long certverifyresult; /* result from the certificate verification */
d889a8
   char *CRLfile;   /* CRL to check certificate revocation */
d889a8
-  char *issuercert;/* optional issuer certificate filename */
d889a8
-  struct curl_blob *issuercert_blob;
d889a8
   curl_ssl_ctx_callback fsslctx; /* function to initialize ssl ctx */
d889a8
   void *fsslctxp;        /* parameter for call back */
d889a8
   char *cert_type; /* format for certificate (default: PEM)*/
d889a8
diff --git a/lib/vtls/gtls.c b/lib/vtls/gtls.c
d889a8
index ea54fe3..ccc5ce8 100644
d889a8
--- a/lib/vtls/gtls.c
d889a8
+++ b/lib/vtls/gtls.c
d889a8
@@ -849,7 +849,7 @@ gtls_connect_step3(struct Curl_easy *data,
d889a8
   if(!chainp) {
d889a8
     if(SSL_CONN_CONFIG(verifypeer) ||
d889a8
        SSL_CONN_CONFIG(verifyhost) ||
d889a8
-       SSL_SET_OPTION(issuercert)) {
d889a8
+       SSL_CONN_CONFIG(issuercert)) {
d889a8
 #ifdef HAVE_GNUTLS_SRP
d889a8
       if(SSL_SET_OPTION(authtype) == CURL_TLSAUTH_SRP
d889a8
          && SSL_SET_OPTION(username) != NULL
d889a8
@@ -1033,21 +1033,21 @@ gtls_connect_step3(struct Curl_easy *data,
d889a8
        gnutls_x509_crt_t format */
d889a8
     gnutls_x509_crt_import(x509_cert, chainp, GNUTLS_X509_FMT_DER);
d889a8
 
d889a8
-  if(SSL_SET_OPTION(issuercert)) {
d889a8
+  if(SSL_CONN_CONFIG(issuercert)) {
d889a8
     gnutls_x509_crt_init(&x509_issuer);
d889a8
-    issuerp = load_file(SSL_SET_OPTION(issuercert));
d889a8
+    issuerp = load_file(SSL_CONN_CONFIG(issuercert));
d889a8
     gnutls_x509_crt_import(x509_issuer, &issuerp, GNUTLS_X509_FMT_PEM);
d889a8
     rc = gnutls_x509_crt_check_issuer(x509_cert, x509_issuer);
d889a8
     gnutls_x509_crt_deinit(x509_issuer);
d889a8
     unload_file(issuerp);
d889a8
     if(rc <= 0) {
d889a8
       failf(data, "server certificate issuer check failed (IssuerCert: %s)",
d889a8
-            SSL_SET_OPTION(issuercert)?SSL_SET_OPTION(issuercert):"none");
d889a8
+            SSL_CONN_CONFIG(issuercert)?SSL_CONN_CONFIG(issuercert):"none");
d889a8
       gnutls_x509_crt_deinit(x509_cert);
d889a8
       return CURLE_SSL_ISSUER_ERROR;
d889a8
     }
d889a8
     infof(data, "\t server certificate issuer check OK (Issuer Cert: %s)\n",
d889a8
-          SSL_SET_OPTION(issuercert)?SSL_SET_OPTION(issuercert):"none");
d889a8
+          SSL_CONN_CONFIG(issuercert)?SSL_CONN_CONFIG(issuercert):"none");
d889a8
   }
d889a8
 
d889a8
   size = sizeof(certname);
d889a8
diff --git a/lib/vtls/nss.c b/lib/vtls/nss.c
d889a8
index ae3945c..b0b1e8c 100644
d889a8
--- a/lib/vtls/nss.c
d889a8
+++ b/lib/vtls/nss.c
d889a8
@@ -2156,9 +2156,9 @@ static CURLcode nss_do_connect(struct Curl_easy *data,
d889a8
   if(result)
d889a8
     goto error;
d889a8
 
d889a8
-  if(SSL_SET_OPTION(issuercert)) {
d889a8
+  if(SSL_CONN_CONFIG(issuercert)) {
d889a8
     SECStatus ret = SECFailure;
d889a8
-    char *nickname = dup_nickname(data, SSL_SET_OPTION(issuercert));
d889a8
+    char *nickname = dup_nickname(data, SSL_CONN_CONFIG(issuercert));
d889a8
     if(nickname) {
d889a8
       /* we support only nicknames in case of issuercert for now */
d889a8
       ret = check_issuer_cert(backend->handle, nickname);
d889a8
diff --git a/lib/vtls/openssl.c b/lib/vtls/openssl.c
d889a8
index 2404393..be7b811 100644
d889a8
--- a/lib/vtls/openssl.c
d889a8
+++ b/lib/vtls/openssl.c
d889a8
@@ -3872,10 +3872,10 @@ static CURLcode servercert(struct Curl_easy *data,
d889a8
        deallocating the certificate. */
d889a8
 
d889a8
     /* e.g. match issuer name with provided issuer certificate */
d889a8
-    if(SSL_SET_OPTION(issuercert) || SSL_SET_OPTION(issuercert_blob)) {
d889a8
-      if(SSL_SET_OPTION(issuercert_blob))
d889a8
-        fp = BIO_new_mem_buf(SSL_SET_OPTION(issuercert_blob)->data,
d889a8
-                             (int)SSL_SET_OPTION(issuercert_blob)->len);
d889a8
+    if(SSL_CONN_CONFIG(issuercert) || SSL_CONN_CONFIG(issuercert_blob)) {
d889a8
+      if(SSL_CONN_CONFIG(issuercert_blob))
d889a8
+        fp = BIO_new_mem_buf(SSL_CONN_CONFIG(issuercert_blob)->data,
d889a8
+                             (int)SSL_CONN_CONFIG(issuercert_blob)->len);
d889a8
       else {
d889a8
         fp = BIO_new(BIO_s_file());
d889a8
         if(fp == NULL) {
d889a8
@@ -3889,10 +3889,10 @@ static CURLcode servercert(struct Curl_easy *data,
d889a8
           return CURLE_OUT_OF_MEMORY;
d889a8
         }
d889a8
 
d889a8
-        if(BIO_read_filename(fp, SSL_SET_OPTION(issuercert)) <= 0) {
d889a8
+        if(BIO_read_filename(fp, SSL_CONN_CONFIG(issuercert)) <= 0) {
d889a8
           if(strict)
d889a8
             failf(data, "SSL: Unable to open issuer cert (%s)",
d889a8
-                  SSL_SET_OPTION(issuercert));
d889a8
+                  SSL_CONN_CONFIG(issuercert));
d889a8
           BIO_free(fp);
d889a8
           X509_free(backend->server_cert);
d889a8
           backend->server_cert = NULL;
d889a8
@@ -3904,7 +3904,7 @@ static CURLcode servercert(struct Curl_easy *data,
d889a8
       if(!issuer) {
d889a8
         if(strict)
d889a8
           failf(data, "SSL: Unable to read issuer cert (%s)",
d889a8
-                SSL_SET_OPTION(issuercert));
d889a8
+                SSL_CONN_CONFIG(issuercert));
d889a8
         BIO_free(fp);
d889a8
         X509_free(issuer);
d889a8
         X509_free(backend->server_cert);
d889a8
@@ -3915,7 +3915,7 @@ static CURLcode servercert(struct Curl_easy *data,
d889a8
       if(X509_check_issued(issuer, backend->server_cert) != X509_V_OK) {
d889a8
         if(strict)
d889a8
           failf(data, "SSL: Certificate issuer check failed (%s)",
d889a8
-                SSL_SET_OPTION(issuercert));
d889a8
+                SSL_CONN_CONFIG(issuercert));
d889a8
         BIO_free(fp);
d889a8
         X509_free(issuer);
d889a8
         X509_free(backend->server_cert);
d889a8
@@ -3924,7 +3924,7 @@ static CURLcode servercert(struct Curl_easy *data,
d889a8
       }
d889a8
 
d889a8
       infof(data, " SSL certificate issuer check ok (%s)\n",
d889a8
-            SSL_SET_OPTION(issuercert));
d889a8
+            SSL_CONN_CONFIG(issuercert));
d889a8
       BIO_free(fp);
d889a8
       X509_free(issuer);
d889a8
     }
d889a8
diff --git a/lib/vtls/vtls.c b/lib/vtls/vtls.c
d889a8
index fa8a6fa..1aa6fc8 100644
d889a8
--- a/lib/vtls/vtls.c
d889a8
+++ b/lib/vtls/vtls.c
d889a8
@@ -125,6 +125,16 @@ static bool blobcmp(struct curl_blob *first, struct curl_blob *second)
d889a8
   return !memcmp(first->data, second->data, first->len); /* same data */
d889a8
 }
d889a8
 
d889a8
+static bool safecmp(char *a, char *b)
d889a8
+{
d889a8
+  if(a && b)
d889a8
+    return !strcmp(a, b);
d889a8
+  else if(!a && !b)
d889a8
+    return TRUE; /* match */
d889a8
+  return FALSE; /* no match */
d889a8
+}
d889a8
+
d889a8
+
d889a8
 bool
d889a8
 Curl_ssl_config_matches(struct ssl_primary_config *data,
d889a8
                         struct ssl_primary_config *needle)
d889a8
@@ -135,11 +145,13 @@ Curl_ssl_config_matches(struct ssl_primary_config *data,
d889a8
      (data->verifyhost == needle->verifyhost) &&
d889a8
      (data->verifystatus == needle->verifystatus) &&
d889a8
      blobcmp(data->cert_blob, needle->cert_blob) &&
d889a8
-     Curl_safe_strcasecompare(data->CApath, needle->CApath) &&
d889a8
-     Curl_safe_strcasecompare(data->CAfile, needle->CAfile) &&
d889a8
-     Curl_safe_strcasecompare(data->clientcert, needle->clientcert) &&
d889a8
-     Curl_safe_strcasecompare(data->random_file, needle->random_file) &&
d889a8
-     Curl_safe_strcasecompare(data->egdsocket, needle->egdsocket) &&
d889a8
+     blobcmp(data->issuercert_blob, needle->issuercert_blob) &&
d889a8
+     safecmp(data->CApath, needle->CApath) &&
d889a8
+     safecmp(data->CAfile, needle->CAfile) &&
d889a8
+     safecmp(data->issuercert, needle->issuercert) &&
d889a8
+     safecmp(data->clientcert, needle->clientcert) &&
d889a8
+     safecmp(data->random_file, needle->random_file) &&
d889a8
+     safecmp(data->egdsocket, needle->egdsocket) &&
d889a8
      Curl_safe_strcasecompare(data->cipher_list, needle->cipher_list) &&
d889a8
      Curl_safe_strcasecompare(data->cipher_list13, needle->cipher_list13) &&
d889a8
      Curl_safe_strcasecompare(data->curves, needle->curves) &&
d889a8
@@ -161,8 +173,10 @@ Curl_clone_primary_ssl_config(struct ssl_primary_config *source,
d889a8
   dest->sessionid = source->sessionid;
d889a8
 
d889a8
   CLONE_BLOB(cert_blob);
d889a8
+  CLONE_BLOB(issuercert_blob);
d889a8
   CLONE_STRING(CApath);
d889a8
   CLONE_STRING(CAfile);
d889a8
+  CLONE_STRING(issuercert);
d889a8
   CLONE_STRING(clientcert);
d889a8
   CLONE_STRING(random_file);
d889a8
   CLONE_STRING(egdsocket);
d889a8
@@ -178,6 +192,7 @@ void Curl_free_primary_ssl_config(struct ssl_primary_config *sslc)
d889a8
 {
d889a8
   Curl_safefree(sslc->CApath);
d889a8
   Curl_safefree(sslc->CAfile);
d889a8
+  Curl_safefree(sslc->issuercert);
d889a8
   Curl_safefree(sslc->clientcert);
d889a8
   Curl_safefree(sslc->random_file);
d889a8
   Curl_safefree(sslc->egdsocket);
d889a8
@@ -185,6 +200,7 @@ void Curl_free_primary_ssl_config(struct ssl_primary_config *sslc)
d889a8
   Curl_safefree(sslc->cipher_list13);
d889a8
   Curl_safefree(sslc->pinned_key);
d889a8
   Curl_safefree(sslc->cert_blob);
d889a8
+  Curl_safefree(sslc->issuercert_blob);
d889a8
   Curl_safefree(sslc->curves);
d889a8
 }
d889a8
 
d889a8
-- 
d889a8
2.31.1
d889a8