9b977c
From 2470bc91f62cc9b0ab1deac60a67f87b7cc95f6e Mon Sep 17 00:00:00 2001
9b977c
From: Daniel Stenberg <daniel@haxx.se>
9b977c
Date: Wed, 2 Dec 2020 23:01:11 +0100
9b977c
Subject: [PATCH] openssl: make the OCSP verification verify the certificate id
9b977c
9b977c
CVE-2020-8286
9b977c
9b977c
Reported by anonymous
9b977c
9b977c
Bug: https://curl.se/docs/CVE-2020-8286.html
9b977c
9b977c
Upstream-commit: d9d01672785b8ac04aab1abb6de95fe3072ae199
9b977c
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
9b977c
---
9b977c
 lib/vtls/openssl.c | 83 ++++++++++++++++++++++++++++++----------------
9b977c
 1 file changed, 54 insertions(+), 29 deletions(-)
9b977c
9b977c
diff --git a/lib/vtls/openssl.c b/lib/vtls/openssl.c
9b977c
index 9476773..35cd652 100644
9b977c
--- a/lib/vtls/openssl.c
9b977c
+++ b/lib/vtls/openssl.c
9b977c
@@ -1659,6 +1659,11 @@ static CURLcode verifystatus(struct connectdata *conn,
9b977c
   OCSP_BASICRESP *br = NULL;
9b977c
   X509_STORE     *st = NULL;
9b977c
   STACK_OF(X509) *ch = NULL;
9b977c
+  X509 *cert;
9b977c
+  OCSP_CERTID *id = NULL;
9b977c
+  int cert_status, crl_reason;
9b977c
+  ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
9b977c
+  int ret;
9b977c
 
9b977c
   long len = SSL_get_tlsext_status_ocsp_resp(BACKEND->handle, &p);
9b977c
 
9b977c
@@ -1727,43 +1732,63 @@ static CURLcode verifystatus(struct connectdata *conn,
9b977c
     goto end;
9b977c
   }
9b977c
 
9b977c
-  for(i = 0; i < OCSP_resp_count(br); i++) {
9b977c
-    int cert_status, crl_reason;
9b977c
-    OCSP_SINGLERESP *single = NULL;
9b977c
-
9b977c
-    ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
9b977c
+  /* Compute the certificate's ID */
9b977c
+  cert = SSL_get_peer_certificate(BACKEND->handle);
9b977c
+  if(!cert) {
9b977c
+    failf(data, "Error getting peer certficate");
9b977c
+    result = CURLE_SSL_INVALIDCERTSTATUS;
9b977c
+    goto end;
9b977c
+  }
9b977c
 
9b977c
-    single = OCSP_resp_get0(br, i);
9b977c
-    if(!single)
9b977c
-      continue;
9b977c
+  for(i = 0; i < sk_X509_num(ch); i++) {
9b977c
+    X509 *issuer = sk_X509_value(ch, i);
9b977c
+    if(X509_check_issued(issuer, cert) == X509_V_OK) {
9b977c
+      id = OCSP_cert_to_id(EVP_sha1(), cert, issuer);
9b977c
+      break;
9b977c
+    }
9b977c
+  }
9b977c
+  X509_free(cert);
9b977c
 
9b977c
-    cert_status = OCSP_single_get0_status(single, &crl_reason, &rev,
9b977c
-                                          &thisupd, &nextupd);
9b977c
+  if(!id) {
9b977c
+    failf(data, "Error computing OCSP ID");
9b977c
+    result = CURLE_SSL_INVALIDCERTSTATUS;
9b977c
+    goto end;
9b977c
+  }
9b977c
 
9b977c
-    if(!OCSP_check_validity(thisupd, nextupd, 300L, -1L)) {
9b977c
-      failf(data, "OCSP response has expired");
9b977c
-      result = CURLE_SSL_INVALIDCERTSTATUS;
9b977c
-      goto end;
9b977c
-    }
9b977c
+  /* Find the single OCSP response corresponding to the certificate ID */
9b977c
+  ret = OCSP_resp_find_status(br, id, &cert_status, &crl_reason, &rev,
9b977c
+                              &thisupd, &nextupd);
9b977c
+  OCSP_CERTID_free(id);
9b977c
+  if(ret != 1) {
9b977c
+    failf(data, "Could not find certificate ID in OCSP response");
9b977c
+    result = CURLE_SSL_INVALIDCERTSTATUS;
9b977c
+    goto end;
9b977c
+  }
9b977c
 
9b977c
-    infof(data, "SSL certificate status: %s (%d)\n",
9b977c
-          OCSP_cert_status_str(cert_status), cert_status);
9b977c
+  /* Validate the corresponding single OCSP response */
9b977c
+  if(!OCSP_check_validity(thisupd, nextupd, 300L, -1L)) {
9b977c
+    failf(data, "OCSP response has expired");
9b977c
+    result = CURLE_SSL_INVALIDCERTSTATUS;
9b977c
+    goto end;
9b977c
+  }
9b977c
 
9b977c
-    switch(cert_status) {
9b977c
-      case V_OCSP_CERTSTATUS_GOOD:
9b977c
-        break;
9b977c
+  infof(data, "SSL certificate status: %s (%d)\n",
9b977c
+        OCSP_cert_status_str(cert_status), cert_status);
9b977c
 
9b977c
-      case V_OCSP_CERTSTATUS_REVOKED:
9b977c
-        result = CURLE_SSL_INVALIDCERTSTATUS;
9b977c
+  switch(cert_status) {
9b977c
+  case V_OCSP_CERTSTATUS_GOOD:
9b977c
+    break;
9b977c
 
9b977c
-        failf(data, "SSL certificate revocation reason: %s (%d)",
9b977c
-              OCSP_crl_reason_str(crl_reason), crl_reason);
9b977c
-        goto end;
9b977c
+  case V_OCSP_CERTSTATUS_REVOKED:
9b977c
+    result = CURLE_SSL_INVALIDCERTSTATUS;
9b977c
+    failf(data, "SSL certificate revocation reason: %s (%d)",
9b977c
+          OCSP_crl_reason_str(crl_reason), crl_reason);
9b977c
+    goto end;
9b977c
 
9b977c
-      case V_OCSP_CERTSTATUS_UNKNOWN:
9b977c
-        result = CURLE_SSL_INVALIDCERTSTATUS;
9b977c
-        goto end;
9b977c
-    }
9b977c
+  case V_OCSP_CERTSTATUS_UNKNOWN:
9b977c
+  default:
9b977c
+    result = CURLE_SSL_INVALIDCERTSTATUS;
9b977c
+    goto end;
9b977c
   }
9b977c
 
9b977c
 end:
9b977c
-- 
9b977c
2.26.2
9b977c