find_issuer(): When returning an expired issuer, take the most recently expired one
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Thu, 7 Jan 2021 19:02:39 +0000 (20:02 +0100)
committerDr. David von Oheimb <dev@ddvo.net>
Thu, 14 Jan 2021 13:34:00 +0000 (14:34 +0100)
Also point out in the documenting comment that a non-expired issuer is preferred.

Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/13805)

crypto/x509/x509_vfy.c
doc/man1/openssl-verification-options.pod

index f5849a56037cca0d969dadef0125aef815e4fa60..1bef0a36654b6d6ab7fc2e71fc47a563fc840c38 100644 (file)
@@ -316,10 +316,10 @@ static int sk_X509_contains(STACK_OF(X509) *sk, X509 *cert)
 }
 
 /*
- * Find in given STACK_OF(X509) sk an issuer cert of given cert x.
- * The issuer must not yet be in ctx->chain, where the exceptional case
- * that x is self-issued and ctx->chain has just one element is allowed.
- * Prefer the first one that is not expired, else take the last expired one.
+ * Find in given STACK_OF(X509) |sk| an issuer cert (if any) of given cert |x|.
+ * The issuer must not yet be in |ctx->chain|, yet allowing the exception that
+ *     |x| is self-issued and |ctx->chain| has just one element.
+ * Prefer the first non-expired one, else take the most recently expired one.
  */
 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
 {
@@ -333,7 +333,9 @@ static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
                 || !sk_X509_contains(ctx->chain, issuer))) {
             if (x509_check_cert_time(ctx, issuer, -1))
                 return issuer;
-            rv = issuer;
+            if (rv == NULL || ASN1_TIME_compare(X509_get0_notAfter(issuer),
+                                                X509_get0_notAfter(rv)) > 0)
+                rv = issuer;
         }
     }
     return rv;
index af1c7e3a43f8269141879db4bac83dfc9b5abc96..620eacf5ccf2e34fad150dca6f88d8d28c72f7ea 100644 (file)
@@ -36,6 +36,8 @@ name of the current certificate are subject to further tests.
 The relevant authority key identifier components of the current certificate
 (if present) must match the subject key identifier (if present)
 and issuer and serial number of the candidate issuer certificate.
+If there is such a certificate, the first one found that is currently valid
+is taken, otherwise the one that expired most recently of all such certificates.
 
 The lookup first searches for issuer certificates in the trust store.
 If it does not find a match there it consults