pki: Track the issuing certificate in PathVerifier

PathVerifier tracked the issuer's subject (RFC 5280's
working_issuer_name) separately from the issuer itself. RFC 5280 splits
the issuer into separate variables because its trust anchor need not be
a certificate, but here the trust anchor is always a certificate.

So track the issuing certificate, so that later changes can read other
properties of the issuer, such as the MTC CA extension, when verifying
the next certificate.

No behavior change.

Bug: 575507260
Change-Id: I79eaa48ecbe98d41fff6f6af11368a106a6a6964
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/105389
Reviewed-by: Matt Mueller <mattm@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: Lily Chen <chlily@google.com>
diff --git a/pki/verify_certificate_chain.cc b/pki/verify_certificate_chain.cc
index 9a5943b..da4929d 100644
--- a/pki/verify_certificate_chain.cc
+++ b/pki/verify_certificate_chain.cc
@@ -882,6 +882,18 @@
   //   otherwise the initial value is n+1.
   size_t policy_mapping_;
 
+  // `working_issuer_cert_` is the certificate that the chain-to-verify claims
+  // issued the next certificate in the chain: the trust anchor's certificate
+  // at first, and then each intermediate in turn. It points into the
+  // certificate chain being verified.
+  //
+  // RFC 5280 section 6.1.2 describes the issuer's fields as separate variables,
+  // since it is agnostic as to whether the trust anchor is a certificate. Here
+  // the trust anchor is always a certificate, so we track that certificate and
+  // read the issuer's subject (the working_issuer_name variable in RFC 5280)
+  // from it directly.
+  const ParsedCertificate* working_issuer_cert_ = nullptr;
+
   // `working_public_key_` is an amalgamation of 3 separate variables from RFC
   // 5280:
   //    * working_public_key
@@ -899,18 +911,11 @@
   //    signature of a certificate.
   bssl::UniquePtr<EVP_PKEY> working_public_key_;
 
-  // `working_normalized_issuer_name_` is the normalized value of the
-  // working_issuer_name variable in RFC 5280 section 6.1.2:
-  //
-  //    working_issuer_name:  the issuer distinguished name expected
-  //    in the next certificate in the chain.
-  der::Input working_normalized_issuer_name_;
-
   // `working_mtc_anchor_` is the trusted MTC Anchor that the chain-to-verify
   // claims issued the next certificate in the chain, and should be used to
-  // verify that certificate. It is analogous to `working_public_key_`, except
-  // that MTCs don't have an EVP_PKEY that can be used to verify their
-  // "signature" and instead have an MTCAnchor used for verification.
+  // verify that certificate.
+  // TODO(crbug.com/575507260): Remove this field and use `working_issuer_cert_`
+  // instead.
   const MTCAnchor *working_mtc_anchor_ = nullptr;
 
   // `max_path_length_` corresponds with the same named variable in RFC 5280
@@ -1632,7 +1637,7 @@
 
   // Verify the certificate's issuer name matches the issuing certificate's
   // subject name. (RFC 5280 section 6.1.3 step a.4)
-  if (cert.normalized_issuer() != working_normalized_issuer_name_) {
+  if (cert.normalized_issuer() != working_issuer_cert_->normalized_subject()) {
     errors->AddError(cert_errors::kSubjectDoesNotMatchIssuer);
   }
 
@@ -1666,8 +1671,10 @@
 
   // From RFC 5280 section 6.1.4 step c:
   //
-  //    Assign the certificate subject name to working_normalized_issuer_name.
-  working_normalized_issuer_name_ = cert.normalized_subject();
+  //    Assign the certificate subject name to working_issuer_name.
+  //
+  // This is done by tracking the certificate itself.
+  working_issuer_cert_ = &cert;
 
   // From RFC 5280 section 6.1.4 step d:
   //
@@ -1990,7 +1997,7 @@
   } else {
     working_public_key_ = ParseAndCheckPublicKey(cert.tbs().spki_tlv, errors);
   }
-  working_normalized_issuer_name_ = cert.normalized_subject();
+  working_issuer_cert_ = &cert;
 }
 
 void PathVerifier::ProcessSingleCertChain(const ParsedCertificate &cert,