CVE-2021-38597: Insufficient Verification of Data Authenticity (CWE-345)
value: wolfSSL before 4.8.1 incorrectly skips OCSP verification in certain situations of irrelevant response data that contains the NoCheck extension.
CWE description: The product does not sufficiently verify the origin or authenticity of data, in a way that causes it to accept invalid data.
CVSS v3 base score: 5.9
Published: 2021-08-12T15:15Z
Repository: https://github.com/wolfSSL/wolfssl
Language: C
Files changed: wolfcrypt/src/asn.c, wolfssl/wolfcrypt/asn.h
Commit date: 2021-07-16 12:19:39 +1000
Commit message: OCSP: improve handling of OCSP no check extension

Diff: diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c
index 300a34da99..61c3d10f88 100644
--- a/wolfcrypt/src/asn.c
+++ b/wolfcrypt/src/asn.c
@@ -10210,13 +10210,17 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm)
                 else
                     return ret;
             }
 
         #ifdef HAVE_OCSP
-            /* trust for the lifetime of the responder's cert*/
-            if (cert->ocspNoCheckSet && verify == VERIFY_OCSP)
-                verify = NO_VERIFY;
+            if (verify == VERIFY_OCSP_CERT) {
+                /* trust for the lifetime of the responder's cert*/
+                if (cert->ocspNoCheckSet)
+                    verify = VERIFY;
+                else
+                    verify = VERIFY_OCSP;
+            }
         #endif
             /* advance past extensions */
             cert->srcIdx = cert->sigIndex;
         }
 
@@ -17955,11 +17959,11 @@ static int DecodeBasicOcspResponse(byte* source, word32* ioIndex,
 
         InitDecodedCert(&cert, resp->cert, resp->certSz, heap);
 
         /* Don't verify if we don't have access to Cert Manager. */
         ret = ParseCertRelative(&cert, CERT_TYPE,
-                                noVerify ? NO_VERIFY : VERIFY_OCSP, cm);
+                                noVerify ? NO_VERIFY : VERIFY_OCSP_CERT, cm);
         if (ret < 0) {
             WOLFSSL_MSG("\tOCSP Responder certificate parsing failed");
             FreeDecodedCert(&cert);
             return ret;
         }
diff --git a/wolfssl/wolfcrypt/asn.h b/wolfssl/wolfcrypt/asn.h
index 2246e98262..0b3b1bd115 100644
--- a/wolfssl/wolfcrypt/asn.h
+++ b/wolfssl/wolfcrypt/asn.h
@@ -597,10 +597,11 @@ enum VerifyType {
     VERIFY      = 1,
     VERIFY_CRL  = 2,
     VERIFY_OCSP = 3,
     VERIFY_NAME = 4,
     VERIFY_SKIP_DATE = 5,
+    VERIFY_OCSP_CERT = 6,
 };
 
 #ifdef WOLFSSL_CERT_EXT
 enum KeyIdType {
     SKID_TYPE = 0,

Vulnerable code:             /* trust for the lifetime of the responder's cert*/
            if (cert->ocspNoCheckSet && verify == VERIFY_OCSP)
                verify = NO_VERIFY;
                                noVerify ? NO_VERIFY : VERIFY_OCSP, cm);
Fixed code:             if (verify == VERIFY_OCSP_CERT) {
                /* trust for the lifetime of the responder's cert*/
                if (cert->ocspNoCheckSet)
                    verify = VERIFY;
                else
                    verify = VERIFY_OCSP;
            }
                                noVerify ? NO_VERIFY : VERIFY_OCSP_CERT, cm);
    VERIFY_OCSP_CERT = 6,