1From 1dd43e0709fece299b15208f36cc7c76209ba0bb Mon Sep 17 00:00:00 2001 2From: Matt Caswell <matt@openssl.org> 3Date: Tue, 7 Mar 2023 16:52:55 +0000 4Subject: [PATCH] Ensure that EXFLAG_INVALID_POLICY is checked even in leaf 5 certs 6 7Even though we check the leaf cert to confirm it is valid, we 8later ignored the invalid flag and did not notice that the leaf 9cert was bad. 10 11Fixes: CVE-2023-0465 12 13Reviewed-by: Hugo Landau <hlandau@openssl.org> 14Reviewed-by: Tomas Mraz <tomas@openssl.org> 15(Merged from https://github.com/openssl/openssl/pull/20587) 16 17Upstream-Status: Backport from [https://git.openssl.org/gitweb/?p=openssl.git;a=commitdiff;h=1dd43e0709fece299b15208f36cc7c76209ba0bb] 18CVE: CVE-2023-0465 19Signed-off-by: Siddharth Doshi <sdoshi@mvista.com> 20--- 21 crypto/x509/x509_vfy.c | 12 ++++++++++-- 22 1 file changed, 10 insertions(+), 2 deletions(-) 23 24diff --git a/crypto/x509/x509_vfy.c b/crypto/x509/x509_vfy.c 25index 9384f1d..a0282c3 100644 26--- a/crypto/x509/x509_vfy.c 27+++ b/crypto/x509/x509_vfy.c 28@@ -1654,15 +1654,23 @@ static int check_policy(X509_STORE_CTX *ctx) 29 goto memerr; 30 /* Invalid or inconsistent extensions */ 31 if (ret == X509_PCY_TREE_INVALID) { 32- int i; 33+ int i, cbcalled = 0; 34 35 /* Locate certificates with bad extensions and notify callback. */ 36- for (i = 1; i < sk_X509_num(ctx->chain); i++) { 37+ for (i = 0; i < sk_X509_num(ctx->chain); i++) { 38 X509 *x = sk_X509_value(ctx->chain, i); 39 40+ if ((x->ex_flags & EXFLAG_INVALID_POLICY) != 0) 41+ cbcalled = 1; 42 CB_FAIL_IF((x->ex_flags & EXFLAG_INVALID_POLICY) != 0, 43 ctx, x, i, X509_V_ERR_INVALID_POLICY_EXTENSION); 44 } 45+ if (!cbcalled) { 46+ /* Should not be able to get here */ 47+ ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); 48+ return 0; 49+ } 50+ /* The callback ignored the error so we return success */ 51 return 1; 52 } 53 if (ret == X509_PCY_TREE_FAILURE) { 54-- 552.35.7 56 57