Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Side by Side Diff: net/cert/internal/verify_certificate_chain.cc

Issue 2920013003: Add policies support to VerifyCertificateChain(). (Closed)
Patch Set: Fix assertion on linux debug builds Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/cert/internal/verify_certificate_chain.h" 5 #include "net/cert/internal/verify_certificate_chain.h"
6 6
7 #include <algorithm>
7 #include <memory> 8 #include <memory>
8 9
9 #include "base/logging.h" 10 #include "base/logging.h"
10 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
11 #include "net/cert/internal/cert_error_params.h" 12 #include "net/cert/internal/cert_error_params.h"
12 #include "net/cert/internal/cert_errors.h" 13 #include "net/cert/internal/cert_errors.h"
13 #include "net/cert/internal/extended_key_usage.h" 14 #include "net/cert/internal/extended_key_usage.h"
14 #include "net/cert/internal/name_constraints.h" 15 #include "net/cert/internal/name_constraints.h"
15 #include "net/cert/internal/parse_certificate.h" 16 #include "net/cert/internal/parse_certificate.h"
16 #include "net/cert/internal/signature_algorithm.h" 17 #include "net/cert/internal/signature_algorithm.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 DEFINE_CERT_ERROR_ID(kVerifySignedDataFailed, "VerifySignedData failed"); 57 DEFINE_CERT_ERROR_ID(kVerifySignedDataFailed, "VerifySignedData failed");
57 DEFINE_CERT_ERROR_ID(kSignatureAlgorithmsDifferentEncoding, 58 DEFINE_CERT_ERROR_ID(kSignatureAlgorithmsDifferentEncoding,
58 "Certificate.signatureAlgorithm is encoded differently " 59 "Certificate.signatureAlgorithm is encoded differently "
59 "than TBSCertificate.signature"); 60 "than TBSCertificate.signature");
60 DEFINE_CERT_ERROR_ID(kEkuLacksServerAuth, 61 DEFINE_CERT_ERROR_ID(kEkuLacksServerAuth,
61 "The extended key usage does not include server auth"); 62 "The extended key usage does not include server auth");
62 DEFINE_CERT_ERROR_ID(kEkuLacksClientAuth, 63 DEFINE_CERT_ERROR_ID(kEkuLacksClientAuth,
63 "The extended key usage does not include client auth"); 64 "The extended key usage does not include client auth");
64 DEFINE_CERT_ERROR_ID(kCertIsNotTrustAnchor, 65 DEFINE_CERT_ERROR_ID(kCertIsNotTrustAnchor,
65 "Certificate is not a trust anchor"); 66 "Certificate is not a trust anchor");
67 DEFINE_CERT_ERROR_ID(kNoValidPolicy, "No valid policy");
68 DEFINE_CERT_ERROR_ID(kPolicyMappingAnyPolicy,
69 "PolicyMappings must not map anyPolicy");
66 70
67 bool IsHandledCriticalExtensionOid(const der::Input& oid) { 71 bool IsHandledCriticalExtensionOid(const der::Input& oid) {
68 if (oid == BasicConstraintsOid()) 72 if (oid == BasicConstraintsOid())
69 return true; 73 return true;
70 // Key Usage is NOT processed for end-entity certificates (this is the 74 // Key Usage is NOT processed for end-entity certificates (this is the
71 // responsibility of callers), however it is considered "handled" here in 75 // responsibility of callers), however it is considered "handled" here in
72 // order to allow being marked as critical. 76 // order to allow being marked as critical.
73 if (oid == KeyUsageOid()) 77 if (oid == KeyUsageOid())
74 return true; 78 return true;
75 if (oid == ExtKeyUsageOid()) 79 if (oid == ExtKeyUsageOid())
76 return true; 80 return true;
77 if (oid == NameConstraintsOid()) 81 if (oid == NameConstraintsOid())
78 return true; 82 return true;
79 if (oid == SubjectAltNameOid()) 83 if (oid == SubjectAltNameOid())
80 return true; 84 return true;
85 // TODO(eroman): The policy qualifiers are not processed (or in some cases
86 // even parsed). This is fine when the policies extension is non-critical,
87 // however if it is critical the code should also ensure that the policy
88 // qualifiers are only recognized ones (CPS and User Notice).
89 if (oid == CertificatePoliciesOid())
90 return true;
91 if (oid == PolicyMappingsOid())
92 return true;
93 if (oid == PolicyConstraintsOid())
94 return true;
95 if (oid == InhibitAnyPolicyOid())
96 return true;
81 97
82 // TODO(eroman): Make this more complete.
83 return false; 98 return false;
84 } 99 }
85 100
86 // Adds errors to |errors| if the certificate contains unconsumed _critical_ 101 // Adds errors to |errors| if the certificate contains unconsumed _critical_
87 // extensions. 102 // extensions.
88 void VerifyNoUnconsumedCriticalExtensions(const ParsedCertificate& cert, 103 void VerifyNoUnconsumedCriticalExtensions(const ParsedCertificate& cert,
89 CertErrors* errors) { 104 CertErrors* errors) {
90 for (const auto& it : cert.extensions()) { 105 for (const auto& it : cert.extensions()) {
91 const ParsedExtension& extension = it.second; 106 const ParsedExtension& extension = it.second;
92 if (extension.critical && !IsHandledCriticalExtensionOid(extension.oid)) { 107 if (extension.critical && !IsHandledCriticalExtensionOid(extension.oid)) {
(...skipping 20 matching lines...) Expand all
113 } 128 }
114 129
115 // Adds errors to |errors| if |cert| is not valid at time |time|. 130 // Adds errors to |errors| if |cert| is not valid at time |time|.
116 // 131 //
117 // The certificate's validity requirements are described by RFC 5280 section 132 // The certificate's validity requirements are described by RFC 5280 section
118 // 4.1.2.5: 133 // 4.1.2.5:
119 // 134 //
120 // The validity period for a certificate is the period of time from 135 // The validity period for a certificate is the period of time from
121 // notBefore through notAfter, inclusive. 136 // notBefore through notAfter, inclusive.
122 void VerifyTimeValidity(const ParsedCertificate& cert, 137 void VerifyTimeValidity(const ParsedCertificate& cert,
123 const der::GeneralizedTime time, 138 const der::GeneralizedTime& time,
124 CertErrors* errors) { 139 CertErrors* errors) {
125 if (time < cert.tbs().validity_not_before) 140 if (time < cert.tbs().validity_not_before)
126 errors->AddError(kValidityFailedNotBefore); 141 errors->AddError(kValidityFailedNotBefore);
127 142
128 if (cert.tbs().validity_not_after < time) 143 if (cert.tbs().validity_not_after < time)
129 errors->AddError(kValidityFailedNotAfter); 144 errors->AddError(kValidityFailedNotAfter);
130 } 145 }
131 146
132 // Adds errors to |errors| if |cert| has internally inconsistent signature 147 // Adds errors to |errors| if |cert| has internally inconsistent signature
133 // algorithms. 148 // algorithms.
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 if (key_purpose_oid == ClientAuth()) 224 if (key_purpose_oid == ClientAuth())
210 return; 225 return;
211 } 226 }
212 227
213 errors->AddError(kEkuLacksClientAuth); 228 errors->AddError(kEkuLacksClientAuth);
214 break; 229 break;
215 } 230 }
216 } 231 }
217 } 232 }
218 233
219 // This function corresponds to RFC 5280 section 6.1.3's "Basic Certificate 234 // Returns |true| if |policies| contains the OID |search_oid|.
220 // Processing" procedure. 235 bool SetContains(const std::set<der::Input>& policies,
221 void BasicCertificateProcessing( 236 const der::Input& search_oid) {
237 return policies.count(search_oid) > 0;
238 }
239
240 // Representation of RFC 5280's "valid_policy_tree", used to keep track of the
241 // valid policies and policy re-mappings.
242 //
243 // ValidPolicyTree differs slightly from RFC 5280's description in that:
244 //
245 // (1) It does not track "qualifier_set". This is not needed as it is not
246 // output by this implementation.
247 //
248 // (2) It only stores the most recent level of the policy tree rather than
249 // the full tree of nodes.
250 class ValidPolicyTree {
251 public:
252 ValidPolicyTree() {}
253
254 struct Node {
255 // |root_policy| is equivalent to |valid_policy|, but in the domain of the
256 // caller.
257 //
258 // The reason for this distinction is the Policy Mappings extension.
259 //
260 // So whereas |valid_policy| is in the remapped domain defined by the
261 // issuing certificate, |root_policy| is in the fixed domain of the caller.
262 //
263 // OIDs in "user_initial_policy_set" and "user_constrained_policy_set" are
264 // directly comparable to |root_policy| values, but not necessarily to
265 // |valid_policy|.
266 //
267 // In terms of the valid policy tree, |root_policy| can be found by
268 // starting at the node's root ancestor, and finding the first node with a
269 // valid_policy other than anyPolicy. This is effectively the same process
270 // as used during policy tree intersection in RFC 5280 6.1.5.g.iii.1
271 der::Input root_policy;
272
273 // The same as RFC 5280's "valid_policy" variable.
274 der::Input valid_policy;
275
276 // The same as RFC 5280s "expected_policy_set" variable.
277 std::set<der::Input> expected_policy_set;
278
279 // Note that RFC 5280's "qualifier_set" is omitted.
280 };
281
282 // Level represents all the nodes at depth "i" in the valid_policy_tree.
283 using Level = std::vector<Node>;
284
285 // Initializes the ValidPolicyTree for the given "user_initial_policy_set".
286 //
287 // In RFC 5280, the valid_policy_tree is initialized to a root node at depth
288 // 0 of "anyPolicy"; the intersection with the "user_initial_policy_set" is
289 // done at the end (Wrap Up) as described in section 6.1.5 step g.
290 //
291 // Whereas in this implementation, the restriction on policies is added here,
292 // and intersecting the valid policy tree during Wrap Up is no longer needed.
293 //
294 // The final "user_constrained_policy_set" obtained will be the same. The
295 // advantages of this approach is simpler code.
296 void Init(const std::set<der::Input>& user_initial_policy_set) {
297 Clear();
298 for (const der::Input& policy_oid : user_initial_policy_set)
299 AddRootNode(policy_oid);
300 }
301
302 // Returns the current level (i.e. all nodes at depth i in the valid
303 // policy tree).
304 const Level& current_level() const { return current_level_; }
305 Level& current_level() { return current_level_; }
306
307 // In RFC 5280 valid_policy_tree may be set to null. That is represented here
308 // by emptiness.
309 bool IsNull() const { return current_level_.empty(); }
310 void SetNull() { Clear(); }
311
312 // This implementation keeps only the last level of the valid policy
313 // tree. Calling StartLevel() returns the nodes for the previous
314 // level, and starts a new level.
315 Level StartLevel() {
316 Level prev_level;
317 std::swap(prev_level, current_level_);
318 return prev_level;
319 }
320
321 // Gets the set of policies (in terms of root authority's policy domain) that
322 // are valid at the curent level of the policy tree.
323 //
324 // For example:
325 //
326 // * If the valid policy tree was initialized with anyPolicy, then this
327 // function returns what X.509 calls "authorities-constrained-policy-set".
328 //
329 // * If the valid policy tree was instead initialized with the
330 // "user-initial-policy_set", then this function returns what X.509
331 // calls "user-constrained-policy-set"
332 // ("authorities-constrained-policy-set" intersected with the
333 // "user-initial-policy-set").
334 void GetValidRootPolicySet(std::set<der::Input>* policy_set) {
335 policy_set->clear();
336 for (const Node& node : current_level_)
337 policy_set->insert(node.root_policy);
338
339 // If the result includes anyPolicy, simplify it to a set of size 1.
340 if (policy_set->size() > 1 && SetContains(*policy_set, AnyPolicy()))
341 *policy_set = {AnyPolicy()};
342 }
343
344 // Adds a node |n| to the current level which is a child of |parent|
345 // such that:
346 // * n.valid_policy = policy_oid
347 // * n.expected_policy_set = {policy_oid}
348 void AddNode(const Node& parent, const der::Input& policy_oid) {
349 AddNodeWithExpectedPolicySet(parent, policy_oid, {policy_oid});
350 }
351
352 // Adds a node |n| to the current level which is a child of |parent|
353 // such that:
354 // * n.valid_policy = policy_oid
355 // * n.expected_policy_set = expected_policy_set
356 void AddNodeWithExpectedPolicySet(
357 const Node& parent,
358 const der::Input& policy_oid,
359 const std::set<der::Input>& expected_policy_set) {
360 Node new_node;
361 new_node.valid_policy = policy_oid;
362 new_node.expected_policy_set = expected_policy_set;
363
364 // Consider the root policy as the first policy other than anyPolicy (or
365 // anyPolicy if it hasn't been restricted yet).
366 new_node.root_policy =
367 (parent.root_policy == AnyPolicy()) ? policy_oid : parent.root_policy;
368
369 current_level_.push_back(std::move(new_node));
370 }
371
372 // Returns the first node having valid_policy == anyPolicy in |level|, or
373 // nullptr if there is none.
374 static const Node* FindAnyPolicyNode(const Level& level) {
375 for (const Node& node : level) {
376 if (node.valid_policy == AnyPolicy())
377 return &node;
378 }
379 return nullptr;
380 }
381
382 // Deletes all nodes |n| in |level| where |n.valid_policy| matches the given
383 // |valid_policy|. This may re-order the nodes in |level|.
384 static void DeleteNodesMatchingValidPolicy(const der::Input& valid_policy,
385 Level* level) {
386 // This works by swapping nodes to the end of the vector, and then doing a
387 // single resize to delete them all.
388 auto cur = level->begin();
389 auto end = level->end();
390 while (cur != end) {
391 bool should_delete_node = cur->valid_policy == valid_policy;
392 if (should_delete_node) {
393 end = std::prev(end);
394 if (cur != end)
395 std::iter_swap(cur, end);
396 } else {
397 ++cur;
398 }
399 }
400 level->erase(end, level->end());
401 }
402
403 private:
404 // Deletes all nodes in the valid policy tree.
405 void Clear() { current_level_.clear(); }
406
407 // Adds a node to the current level for OID |policy_oid|. The current level
408 // is assumed to be the root level.
409 void AddRootNode(const der::Input& policy_oid) {
410 Node new_node;
411 new_node.root_policy = policy_oid;
412 new_node.valid_policy = policy_oid;
413 new_node.expected_policy_set = {policy_oid};
414 current_level_.push_back(std::move(new_node));
415 }
416
417 Level current_level_;
418
419 DISALLOW_COPY_AND_ASSIGN(ValidPolicyTree);
420 };
421
422 // Class that encapsulates the state variables used by certificate path
423 // validation.
424 class PathVerifier {
425 public:
426 // Same parameters and meaning as VerifyCertificateChain().
427 void Run(const ParsedCertificateList& certs,
428 const CertificateTrust& last_cert_trust,
429 const SignaturePolicy* signature_policy,
430 const der::GeneralizedTime& time,
431 KeyPurpose required_key_purpose,
432 InitialExplicitPolicy initial_explicit_policy,
433 const std::set<der::Input>& user_initial_policy_set,
434 InitialPolicyMappingInhibit initial_policy_mapping_inhibit,
435 InitialAnyPolicyInhibit initial_any_policy_inhibit,
436 std::set<der::Input>* user_constrained_policy_set,
437 CertPathErrors* errors);
438
439 private:
440 // Verifies and updates the valid policies. This corresponds with RFC 5280
441 // section 6.1.3 steps d-f.
442 void VerifyPolicies(const ParsedCertificate& cert,
443 bool is_target_cert,
444 CertErrors* errors);
445
446 // Applies the policy mappings. This corresponds with RFC 5280 section 6.1.4
447 // steps a-b.
448 void VerifyPolicyMappings(const ParsedCertificate& cert, CertErrors* errors);
449
450 // This function corresponds to RFC 5280 section 6.1.3's "Basic Certificate
451 // Processing" procedure.
452 void BasicCertificateProcessing(const ParsedCertificate& cert,
453 bool is_target_cert,
454 const SignaturePolicy* signature_policy,
455 const der::GeneralizedTime& time,
456 KeyPurpose required_key_purpose,
457 CertErrors* errors);
458
459 // This function corresponds to RFC 5280 section 6.1.4's "Preparation for
460 // Certificate i+1" procedure. |cert| is expected to be an intermediate.
461 void PrepareForNextCertificate(const ParsedCertificate& cert,
462 CertErrors* errors);
463
464 // This function corresponds with RFC 5280 section 6.1.5's "Wrap-Up
465 // Procedure". It does processing for the final certificate (the target cert).
466 void WrapUp(const ParsedCertificate& cert, CertErrors* errors);
467
468 // Enforces trust anchor constraints compatibile with RFC 5937.
469 //
470 // Note that the anchor constraints are encoded via the attached certificate
471 // itself.
472 void ApplyTrustAnchorConstraints(const ParsedCertificate& cert,
473 KeyPurpose required_key_purpose,
474 CertErrors* errors);
475
476 // Initializes the path validation algorithm given anchor constraints. This
477 // follows the description in RFC 5937
478 void ProcessRootCertificate(const ParsedCertificate& cert,
479 const CertificateTrust& trust,
480 KeyPurpose required_key_purpose,
481 CertErrors* errors);
482
483 ValidPolicyTree valid_policy_tree_;
484
485 // Will contain a NameConstraints for each previous cert in the chain which
486 // had nameConstraints. This corresponds to the permitted_subtrees and
487 // excluded_subtrees state variables from RFC 5280.
488 std::vector<const NameConstraints*> name_constraints_list_;
489
490 // |explicit_policy_| corresponds with the same named variable from RFC 5280
491 // section 6.1.2:
492 //
493 // explicit_policy: an integer that indicates if a non-NULL
494 // valid_policy_tree is required. The integer indicates the
495 // number of non-self-issued certificates to be processed before
496 // this requirement is imposed. Once set, this variable may be
497 // decreased, but may not be increased. That is, if a certificate in the
498 // path requires a non-NULL valid_policy_tree, a later certificate cannot
499 // remove this requirement. If initial-explicit-policy is set, then the
500 // initial value is 0, otherwise the initial value is n+1.
501 size_t explicit_policy_;
502
503 // |inhibit_any_policy_| corresponds with the same named variable from RFC
504 // 5280 section 6.1.2:
505 //
506 // inhibit_anyPolicy: an integer that indicates whether the
507 // anyPolicy policy identifier is considered a match. The
508 // integer indicates the number of non-self-issued certificates
509 // to be processed before the anyPolicy OID, if asserted in a
510 // certificate other than an intermediate self-issued
511 // certificate, is ignored. Once set, this variable may be
512 // decreased, but may not be increased. That is, if a
513 // certificate in the path inhibits processing of anyPolicy, a
514 // later certificate cannot permit it. If initial-any-policy-
515 // inhibit is set, then the initial value is 0, otherwise the
516 // initial value is n+1.
517 size_t inhibit_any_policy_;
518
519 // |policy_mapping_| corresponds with the same named variable from RFC 5280
520 // section 6.1.2:
521 //
522 // policy_mapping: an integer that indicates if policy mapping
523 // is permitted. The integer indicates the number of non-self-
524 // issued certificates to be processed before policy mapping is
525 // inhibited. Once set, this variable may be decreased, but may
526 // not be increased. That is, if a certificate in the path
527 // specifies that policy mapping is not permitted, it cannot be
528 // overridden by a later certificate. If initial-policy-
529 // mapping-inhibit is set, then the initial value is 0,
530 // otherwise the initial value is n+1.
531 size_t policy_mapping_;
532
533 // |working_spki_| is an amalgamation of 3 separate variables from RFC 5280:
534 // * working_public_key
535 // * working_public_key_algorithm
536 // * working_public_key_parameters
537 //
538 // They are combined for simplicity since the signature verification takes an
539 // SPKI, and the parameter inheritence is not applicable for the supported
540 // key types.
541 //
542 // An approximate explanation of |working_spki| is this description from RFC
543 // 5280 section 6.1.2:
544 //
545 // working_public_key: the public key used to verify the
546 // signature of a certificate.
547 der::Input working_spki_;
548
549 // |working_normalized_issuer_name_| is the normalized value of the
550 // working_issuer_name variable in RFC 5280 section 6.1.2:
551 //
552 // working_issuer_name: the issuer distinguished name expected
553 // in the next certificate in the chain.
554 der::Input working_normalized_issuer_name_;
555
556 // |max_path_length_| corresponds with the same named variable in RFC 5280
557 // section 6.1.2.
558 //
559 // max_path_length: this integer is initialized to n, is
560 // decremented for each non-self-issued certificate in the path,
561 // and may be reduced to the value in the path length constraint
562 // field within the basic constraints extension of a CA
563 // certificate.
564 size_t max_path_length_;
565 };
566
567 void PathVerifier::VerifyPolicies(const ParsedCertificate& cert,
568 bool is_target_cert,
569 CertErrors* errors) {
570 // From RFC 5280 section 6.1.3:
571 //
572 // (d) If the certificate policies extension is present in the
573 // certificate and the valid_policy_tree is not NULL, process
574 // the policy information by performing the following steps in
575 // order:
576 if (cert.has_policy_oids() && !valid_policy_tree_.IsNull()) {
577 ValidPolicyTree::Level previous_level = valid_policy_tree_.StartLevel();
578
579 // Identify if there was a node with valid_policy == anyPolicy at depth i-1.
580 const ValidPolicyTree::Node* any_policy_node_prev_level =
581 ValidPolicyTree::FindAnyPolicyNode(previous_level);
582
583 // (1) For each policy P not equal to anyPolicy in the
584 // certificate policies extension, let P-OID denote the OID
585 // for policy P and P-Q denote the qualifier set for policy
586 // P. Perform the following steps in order:
587 bool cert_has_any_policy = false;
588 for (const der::Input& p_oid : cert.policy_oids()) {
589 if (p_oid == AnyPolicy()) {
590 cert_has_any_policy = true;
591 continue;
592 }
593
594 // (i) For each node of depth i-1 in the valid_policy_tree
595 // where P-OID is in the expected_policy_set, create a
596 // child node as follows: set the valid_policy to P-OID,
597 // set the qualifier_set to P-Q, and set the
598 // expected_policy_set to {P-OID}.
599 bool found_match = false;
600 for (const ValidPolicyTree::Node& prev_node : previous_level) {
601 if (SetContains(prev_node.expected_policy_set, p_oid)) {
602 valid_policy_tree_.AddNode(prev_node, p_oid);
603 found_match = true;
604 }
605 }
606
607 // (ii) If there was no match in step (i) and the
608 // valid_policy_tree includes a node of depth i-1 with
609 // the valid_policy anyPolicy, generate a child node with
610 // the following values: set the valid_policy to P-OID,
611 // set the qualifier_set to P-Q, and set the
612 // expected_policy_set to {P-OID}.
613 if (!found_match && any_policy_node_prev_level)
614 valid_policy_tree_.AddNode(*any_policy_node_prev_level, p_oid);
615 }
616
617 // (2) If the certificate policies extension includes the policy
618 // anyPolicy with the qualifier set AP-Q and either (a)
619 // inhibit_anyPolicy is greater than 0 or (b) i<n and the
620 // certificate is self-issued, then:
621 //
622 // For each node in the valid_policy_tree of depth i-1, for
623 // each value in the expected_policy_set (including
624 // anyPolicy) that does not appear in a child node, create a
625 // child node with the following values: set the valid_policy
626 // to the value from the expected_policy_set in the parent
627 // node, set the qualifier_set to AP-Q, and set the
628 // expected_policy_set to the value in the valid_policy from
629 // this node.
630 if (cert_has_any_policy && ((inhibit_any_policy_ > 0) ||
631 (!is_target_cert && IsSelfIssued(cert)))) {
632 // Keep track of the existing policies at depth i.
633 std::set<der::Input> child_node_policies;
634 for (const ValidPolicyTree::Node& node :
635 valid_policy_tree_.current_level())
636 child_node_policies.insert(node.valid_policy);
637
638 for (const ValidPolicyTree::Node& prev_node : previous_level) {
639 for (const der::Input& expected_policy :
640 prev_node.expected_policy_set) {
641 if (!SetContains(child_node_policies, expected_policy)) {
642 child_node_policies.insert(expected_policy);
643 valid_policy_tree_.AddNode(prev_node, expected_policy);
644 }
645 }
646 }
647 }
648
649 // (3) If there is a node in the valid_policy_tree of depth i-1
650 // or less without any child nodes, delete that node. Repeat
651 // this step until there are no nodes of depth i-1 or less
652 // without children.
653 //
654 // Nothing needs to be done for this step, since this implementation only
655 // stores the nodes at depth i, and the entire level has already been
656 // calculated.
657 }
658
659 // (e) If the certificate policies extension is not present, set the
660 // valid_policy_tree to NULL.
661 if (!cert.has_policy_oids())
662 valid_policy_tree_.SetNull();
663
664 // (f) Verify that either explicit_policy is greater than 0 or the
665 // valid_policy_tree is not equal to NULL;
666 if (!((explicit_policy_ > 0) || !valid_policy_tree_.IsNull()))
667 errors->AddError(kNoValidPolicy);
668 }
669
670 void PathVerifier::VerifyPolicyMappings(const ParsedCertificate& cert,
671 CertErrors* errors) {
672 if (!cert.has_policy_mappings())
673 return;
674
675 // From RFC 5280 section 6.1.4:
676 //
677 // (a) If a policy mappings extension is present, verify that the
678 // special value anyPolicy does not appear as an
679 // issuerDomainPolicy or a subjectDomainPolicy.
680 for (const ParsedPolicyMapping& mapping : cert.policy_mappings()) {
681 if (mapping.issuer_domain_policy == AnyPolicy() ||
682 mapping.subject_domain_policy == AnyPolicy()) {
683 // Because this implementation continues processing certificates after
684 // this error, clear the valid policy tree to ensure the
685 // "user_constrained_policy_set" output upon failure is empty.
686 valid_policy_tree_.SetNull();
687 errors->AddError(kPolicyMappingAnyPolicy);
688 }
689 }
690
691 // (b) If a policy mappings extension is present, then for each
692 // issuerDomainPolicy ID-P in the policy mappings extension:
693 //
694 // (1) If the policy_mapping variable is greater than 0, for each
695 // node in the valid_policy_tree of depth i where ID-P is the
696 // valid_policy, set expected_policy_set to the set of
697 // subjectDomainPolicy values that are specified as
698 // equivalent to ID-P by the policy mappings extension.
699 //
700 // If no node of depth i in the valid_policy_tree has a
701 // valid_policy of ID-P but there is a node of depth i with a
702 // valid_policy of anyPolicy, then generate a child node of
703 // the node of depth i-1 that has a valid_policy of anyPolicy
704 // as follows:
705 //
706 // (i) set the valid_policy to ID-P;
707 //
708 // (ii) set the qualifier_set to the qualifier set of the
709 // policy anyPolicy in the certificate policies
710 // extension of certificate i; and
711 //
712 // (iii) set the expected_policy_set to the set of
713 // subjectDomainPolicy values that are specified as
714 // equivalent to ID-P by the policy mappings extension.
715 //
716 if (policy_mapping_ > 0) {
717 const ValidPolicyTree::Node* any_policy_node =
718 ValidPolicyTree::FindAnyPolicyNode(valid_policy_tree_.current_level());
719
720 // Group mappings by issuer domain policy.
721 std::map<der::Input, std::set<der::Input>> mappings;
722 for (const ParsedPolicyMapping& mapping : cert.policy_mappings()) {
723 mappings[mapping.issuer_domain_policy].insert(
724 mapping.subject_domain_policy);
725 }
726
727 for (const auto& it : mappings) {
728 const der::Input& issuer_domain_policy = it.first;
729 const std::set<der::Input>& subject_domain_policies = it.second;
730 bool found_node = false;
731
732 for (ValidPolicyTree::Node& node : valid_policy_tree_.current_level()) {
733 if (node.valid_policy == issuer_domain_policy) {
734 node.expected_policy_set = subject_domain_policies;
735 found_node = true;
736 }
737 }
738
739 if (!found_node && any_policy_node) {
740 valid_policy_tree_.AddNodeWithExpectedPolicySet(
741 *any_policy_node, issuer_domain_policy, subject_domain_policies);
742 }
743 }
744 }
745
746 // (b) If a policy mappings extension is present, then for each
747 // issuerDomainPolicy ID-P in the policy mappings extension:
748 //
749 // ...
750 //
751 // (2) If the policy_mapping variable is equal to 0:
752 //
753 // (i) delete each node of depth i in the valid_policy_tree
754 // where ID-P is the valid_policy.
755 //
756 // (ii) If there is a node in the valid_policy_tree of depth
757 // i-1 or less without any child nodes, delete that
758 // node. Repeat this step until there are no nodes of
759 // depth i-1 or less without children.
760 if (policy_mapping_ == 0) {
761 for (const ParsedPolicyMapping& mapping : cert.policy_mappings()) {
762 ValidPolicyTree::DeleteNodesMatchingValidPolicy(
763 mapping.issuer_domain_policy, &valid_policy_tree_.current_level());
764 }
765 }
766 }
767
768 void PathVerifier::BasicCertificateProcessing(
222 const ParsedCertificate& cert, 769 const ParsedCertificate& cert,
223 bool is_target_cert, 770 bool is_target_cert,
224 const SignaturePolicy* signature_policy, 771 const SignaturePolicy* signature_policy,
225 const der::GeneralizedTime& time, 772 const der::GeneralizedTime& time,
226 const der::Input& working_spki, 773 KeyPurpose required_key_purpose,
227 const der::Input& working_normalized_issuer_name,
228 const std::vector<const NameConstraints*>& name_constraints_list,
229 CertErrors* errors) { 774 CertErrors* errors) {
230 // Check that the signature algorithms in Certificate vs TBSCertificate 775 // Check that the signature algorithms in Certificate vs TBSCertificate
231 // match. This isn't part of RFC 5280 section 6.1.3, but is mandated by 776 // match. This isn't part of RFC 5280 section 6.1.3, but is mandated by
232 // sections 4.1.1.2 and 4.1.2.3. 777 // sections 4.1.1.2 and 4.1.2.3.
233 VerifySignatureAlgorithmsMatch(cert, errors); 778 VerifySignatureAlgorithmsMatch(cert, errors);
234 779
235 // Verify the digital signature using the previous certificate's key (RFC 780 // Verify the digital signature using the previous certificate's key (RFC
236 // 5280 section 6.1.3 step a.1). 781 // 5280 section 6.1.3 step a.1).
237 if (!VerifySignedData(cert.signature_algorithm(), cert.tbs_certificate_tlv(), 782 if (!VerifySignedData(cert.signature_algorithm(), cert.tbs_certificate_tlv(),
238 cert.signature_value(), working_spki, signature_policy, 783 cert.signature_value(), working_spki_, signature_policy,
239 errors)) { 784 errors)) {
240 errors->AddError(kVerifySignedDataFailed); 785 errors->AddError(kVerifySignedDataFailed);
241 } 786 }
242 787
243 // Check the time range for the certificate's validity, ensuring it is valid 788 // Check the time range for the certificate's validity, ensuring it is valid
244 // at |time|. 789 // at |time|.
245 // (RFC 5280 section 6.1.3 step a.2) 790 // (RFC 5280 section 6.1.3 step a.2)
246 VerifyTimeValidity(cert, time, errors); 791 VerifyTimeValidity(cert, time, errors);
247 792
248 // TODO(eroman): Check revocation (RFC 5280 section 6.1.3 step a.3) 793 // TODO(eroman): Check revocation (RFC 5280 section 6.1.3 step a.3)
249 794
250 // Verify the certificate's issuer name matches the issuing certificate's 795 // Verify the certificate's issuer name matches the issuing certificate's
251 // subject name. (RFC 5280 section 6.1.3 step a.4) 796 // subject name. (RFC 5280 section 6.1.3 step a.4)
252 if (cert.normalized_issuer() != working_normalized_issuer_name) 797 if (cert.normalized_issuer() != working_normalized_issuer_name_)
253 errors->AddError(kSubjectDoesNotMatchIssuer); 798 errors->AddError(kSubjectDoesNotMatchIssuer);
254 799
255 // Name constraints (RFC 5280 section 6.1.3 step b & c) 800 // Name constraints (RFC 5280 section 6.1.3 step b & c)
256 // If certificate i is self-issued and it is not the final certificate in the 801 // If certificate i is self-issued and it is not the final certificate in the
257 // path, skip this step for certificate i. 802 // path, skip this step for certificate i.
258 if (!name_constraints_list.empty() && 803 if (!name_constraints_list_.empty() &&
259 (!IsSelfIssued(cert) || is_target_cert)) { 804 (!IsSelfIssued(cert) || is_target_cert)) {
260 for (const NameConstraints* nc : name_constraints_list) { 805 for (const NameConstraints* nc : name_constraints_list_) {
261 if (!nc->IsPermittedCert(cert.normalized_subject(), 806 if (!nc->IsPermittedCert(cert.normalized_subject(),
262 cert.subject_alt_names())) { 807 cert.subject_alt_names())) {
263 errors->AddError(kNotPermittedByNameConstraints); 808 errors->AddError(kNotPermittedByNameConstraints);
264 } 809 }
265 } 810 }
266 } 811 }
267 812
268 // TODO(eroman): Steps d-f are omitted, as policy constraints are not yet 813 // RFC 5280 section 6.1.3 step d - f.
269 // implemented. 814 VerifyPolicies(cert, is_target_cert, errors);
815
816 // The key purpose is checked not just for the end-entity certificate, but
817 // also interpreted as a constraint when it appears in intermediates. This
818 // goes beyond what RFC 5280 describes, but is the de-facto standard. See
819 // https://wiki.mozilla.org/CA:CertificatePolicyV2.1#Frequently_Asked_Question s
820 VerifyExtendedKeyUsage(cert, required_key_purpose, errors);
270 } 821 }
271 822
272 // This function corresponds to RFC 5280 section 6.1.4's "Preparation for 823 void PathVerifier::PrepareForNextCertificate(const ParsedCertificate& cert,
273 // Certificate i+1" procedure. |cert| is expected to be an intermediate. 824 CertErrors* errors) {
274 void PrepareForNextCertificate( 825 // RFC 5280 section 6.1.4 step a-b
275 const ParsedCertificate& cert, 826 VerifyPolicyMappings(cert, errors);
276 size_t* max_path_length_ptr,
277 der::Input* working_spki,
278 der::Input* working_normalized_issuer_name,
279 std::vector<const NameConstraints*>* name_constraints_list,
280 CertErrors* errors) {
281 // TODO(crbug.com/634456): Steps a-b are omitted, as policy mappings are not
282 // yet implemented.
283 827
284 // From RFC 5280 section 6.1.4 step c: 828 // From RFC 5280 section 6.1.4 step c:
285 // 829 //
286 // Assign the certificate subject name to working_normalized_issuer_name. 830 // Assign the certificate subject name to working_normalized_issuer_name.
287 *working_normalized_issuer_name = cert.normalized_subject(); 831 working_normalized_issuer_name_ = cert.normalized_subject();
288 832
289 // From RFC 5280 section 6.1.4 step d: 833 // From RFC 5280 section 6.1.4 step d:
290 // 834 //
291 // Assign the certificate subjectPublicKey to working_public_key. 835 // Assign the certificate subjectPublicKey to working_public_key.
292 *working_spki = cert.tbs().spki_tlv; 836 working_spki_ = cert.tbs().spki_tlv;
293 837
294 // Note that steps e and f are omitted as they are handled by 838 // Note that steps e and f are omitted as they are handled by
295 // the assignment to |working_spki| above. See the definition 839 // the assignment to |working_spki| above. See the definition
296 // of |working_spki|. 840 // of |working_spki|.
297 841
298 // From RFC 5280 section 6.1.4 step g: 842 // From RFC 5280 section 6.1.4 step g:
299 if (cert.has_name_constraints()) 843 if (cert.has_name_constraints())
300 name_constraints_list->push_back(&cert.name_constraints()); 844 name_constraints_list_.push_back(&cert.name_constraints());
301 845
302 // TODO(eroman): Steps h-j are omitted as policy 846 // (h) If certificate i is not self-issued:
303 // constraints/mappings/inhibitAnyPolicy are not yet implemented. 847 if (!IsSelfIssued(cert)) {
848 // (1) If explicit_policy is not 0, decrement explicit_policy by
849 // 1.
850 if (explicit_policy_ > 0)
851 explicit_policy_ -= 1;
852
853 // (2) If policy_mapping is not 0, decrement policy_mapping by 1.
854 if (policy_mapping_ > 0)
855 policy_mapping_ -= 1;
856
857 // (3) If inhibit_anyPolicy is not 0, decrement inhibit_anyPolicy
858 // by 1.
859 if (inhibit_any_policy_ > 0)
860 inhibit_any_policy_ -= 1;
861 }
862
863 // (i) If a policy constraints extension is included in the
864 // certificate, modify the explicit_policy and policy_mapping
865 // state variables as follows:
866 if (cert.has_policy_constraints()) {
867 // (1) If requireExplicitPolicy is present and is less than
868 // explicit_policy, set explicit_policy to the value of
869 // requireExplicitPolicy.
870 if (cert.policy_constraints().has_require_explicit_policy &&
871 cert.policy_constraints().require_explicit_policy < explicit_policy_) {
872 explicit_policy_ = cert.policy_constraints().require_explicit_policy;
873 }
874
875 // (2) If inhibitPolicyMapping is present and is less than
876 // policy_mapping, set policy_mapping to the value of
877 // inhibitPolicyMapping.
878 if (cert.policy_constraints().has_inhibit_policy_mapping &&
879 cert.policy_constraints().inhibit_policy_mapping < policy_mapping_) {
880 policy_mapping_ = cert.policy_constraints().inhibit_policy_mapping;
881 }
882 }
883
884 // (j) If the inhibitAnyPolicy extension is included in the
885 // certificate and is less than inhibit_anyPolicy, set
886 // inhibit_anyPolicy to the value of inhibitAnyPolicy.
887 if (cert.has_inhibit_any_policy() &&
888 cert.inhibit_any_policy() < inhibit_any_policy_) {
889 inhibit_any_policy_ = cert.inhibit_any_policy();
890 }
304 891
305 // From RFC 5280 section 6.1.4 step k: 892 // From RFC 5280 section 6.1.4 step k:
306 // 893 //
307 // If certificate i is a version 3 certificate, verify that the 894 // If certificate i is a version 3 certificate, verify that the
308 // basicConstraints extension is present and that cA is set to 895 // basicConstraints extension is present and that cA is set to
309 // TRUE. (If certificate i is a version 1 or version 2 896 // TRUE. (If certificate i is a version 1 or version 2
310 // certificate, then the application MUST either verify that 897 // certificate, then the application MUST either verify that
311 // certificate i is a CA certificate through out-of-band means 898 // certificate i is a CA certificate through out-of-band means
312 // or reject the certificate. Conforming implementations may 899 // or reject the certificate. Conforming implementations may
313 // choose to reject all version 1 and version 2 intermediate 900 // choose to reject all version 1 and version 2 intermediate
314 // certificates.) 901 // certificates.)
315 // 902 //
316 // This code implicitly rejects non version 3 intermediates, since they 903 // This code implicitly rejects non version 3 intermediates, since they
317 // can't contain a BasicConstraints extension. 904 // can't contain a BasicConstraints extension.
318 if (!cert.has_basic_constraints()) { 905 if (!cert.has_basic_constraints()) {
319 errors->AddError(kMissingBasicConstraints); 906 errors->AddError(kMissingBasicConstraints);
320 } else if (!cert.basic_constraints().is_ca) { 907 } else if (!cert.basic_constraints().is_ca) {
321 errors->AddError(kBasicConstraintsIndicatesNotCa); 908 errors->AddError(kBasicConstraintsIndicatesNotCa);
322 } 909 }
323 910
324 // From RFC 5280 section 6.1.4 step l: 911 // From RFC 5280 section 6.1.4 step l:
325 // 912 //
326 // If the certificate was not self-issued, verify that 913 // If the certificate was not self-issued, verify that
327 // max_path_length is greater than zero and decrement 914 // max_path_length is greater than zero and decrement
328 // max_path_length by 1. 915 // max_path_length by 1.
329 if (!IsSelfIssued(cert)) { 916 if (!IsSelfIssued(cert)) {
330 if (*max_path_length_ptr == 0) { 917 if (max_path_length_ == 0) {
331 errors->AddError(kMaxPathLengthViolated); 918 errors->AddError(kMaxPathLengthViolated);
332 } else { 919 } else {
333 --(*max_path_length_ptr); 920 --max_path_length_;
334 } 921 }
335 } 922 }
336 923
337 // From RFC 5280 section 6.1.4 step m: 924 // From RFC 5280 section 6.1.4 step m:
338 // 925 //
339 // If pathLenConstraint is present in the certificate and is 926 // If pathLenConstraint is present in the certificate and is
340 // less than max_path_length, set max_path_length to the value 927 // less than max_path_length, set max_path_length to the value
341 // of pathLenConstraint. 928 // of pathLenConstraint.
342 if (cert.has_basic_constraints() && cert.basic_constraints().has_path_len && 929 if (cert.has_basic_constraints() && cert.basic_constraints().has_path_len &&
343 cert.basic_constraints().path_len < *max_path_length_ptr) { 930 cert.basic_constraints().path_len < max_path_length_) {
344 *max_path_length_ptr = cert.basic_constraints().path_len; 931 max_path_length_ = cert.basic_constraints().path_len;
345 } 932 }
346 933
347 // From RFC 5280 section 6.1.4 step n: 934 // From RFC 5280 section 6.1.4 step n:
348 // 935 //
349 // If a key usage extension is present, verify that the 936 // If a key usage extension is present, verify that the
350 // keyCertSign bit is set. 937 // keyCertSign bit is set.
351 if (cert.has_key_usage() && 938 if (cert.has_key_usage() &&
352 !cert.key_usage().AssertsBit(KEY_USAGE_BIT_KEY_CERT_SIGN)) { 939 !cert.key_usage().AssertsBit(KEY_USAGE_BIT_KEY_CERT_SIGN)) {
353 errors->AddError(kKeyCertSignBitNotSet); 940 errors->AddError(kKeyCertSignBitNotSet);
354 } 941 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 cert.basic_constraints().is_ca && 988 cert.basic_constraints().is_ca &&
402 (!cert.has_key_usage() || 989 (!cert.has_key_usage() ||
403 cert.key_usage().AssertsBit(KEY_USAGE_BIT_KEY_CERT_SIGN)); 990 cert.key_usage().AssertsBit(KEY_USAGE_BIT_KEY_CERT_SIGN));
404 if (!success) { 991 if (!success) {
405 // TODO(eroman): Add DER for basic constraints and key usage. 992 // TODO(eroman): Add DER for basic constraints and key usage.
406 errors->AddError(kTargetCertInconsistentCaBits); 993 errors->AddError(kTargetCertInconsistentCaBits);
407 } 994 }
408 } 995 }
409 } 996 }
410 997
411 // This function corresponds with RFC 5280 section 6.1.5's "Wrap-Up Procedure". 998 void PathVerifier::WrapUp(const ParsedCertificate& cert, CertErrors* errors) {
412 // It does processing for the final certificate (the target cert). 999 // From RFC 5280 section 6.1.5:
413 void WrapUp(const ParsedCertificate& cert, CertErrors* errors) { 1000 // (a) If explicit_policy is not 0, decrement explicit_policy by 1.
414 // TODO(crbug.com/634452): Steps a-b are omitted as policy constraints are not 1001 if (explicit_policy_ > 0)
415 // yet implemented. 1002 explicit_policy_ -= 1;
416 1003
417 // Note step c-e are omitted the verification function does 1004 // (b) If a policy constraints extension is included in the
1005 // certificate and requireExplicitPolicy is present and has a
1006 // value of 0, set the explicit_policy state variable to 0.
1007 if (cert.has_policy_constraints() &&
1008 cert.policy_constraints().has_require_explicit_policy &&
1009 cert.policy_constraints().require_explicit_policy == 0) {
1010 explicit_policy_ = 0;
1011 }
1012
1013 // Note step c-e are omitted as the verification function does
418 // not output the working public key. 1014 // not output the working public key.
419 1015
420 // From RFC 5280 section 6.1.5 step f: 1016 // From RFC 5280 section 6.1.5 step f:
421 // 1017 //
422 // Recognize and process any other critical extension present in 1018 // Recognize and process any other critical extension present in
423 // the certificate n. Process any other recognized non-critical 1019 // the certificate n. Process any other recognized non-critical
424 // extension present in certificate n that is relevant to path 1020 // extension present in certificate n that is relevant to path
425 // processing. 1021 // processing.
426 // 1022 //
427 // Note that this is duplicated by PrepareForNextCertificate() so as to 1023 // Note that this is duplicated by PrepareForNextCertificate() so as to
428 // directly match the procedures in RFC 5280's section 6.1. 1024 // directly match the procedures in RFC 5280's section 6.1.
429 VerifyNoUnconsumedCriticalExtensions(cert, errors); 1025 VerifyNoUnconsumedCriticalExtensions(cert, errors);
430 1026
431 // TODO(eroman): Step g is omitted, as policy constraints are not yet 1027 // RFC 5280 section 6.1.5 step g is skipped, as the intersection of valid
432 // implemented. 1028 // policies was computed during previous steps.
1029 //
1030 // If either (1) the value of explicit_policy variable is greater than
1031 // zero or (2) the valid_policy_tree is not NULL, then path processing
1032 // has succeeded.
1033 if (!(explicit_policy_ > 0 || !valid_policy_tree_.IsNull())) {
1034 errors->AddError(kNoValidPolicy);
1035 }
433 1036
434 // The following check is NOT part of RFC 5280 6.1.5's "Wrap-Up Procedure", 1037 // The following check is NOT part of RFC 5280 6.1.5's "Wrap-Up Procedure",
435 // however is implied by RFC 5280 section 4.2.1.9. 1038 // however is implied by RFC 5280 section 4.2.1.9.
436 VerifyTargetCertHasConsistentCaBits(cert, errors); 1039 VerifyTargetCertHasConsistentCaBits(cert, errors);
437 } 1040 }
438 1041
439 // Enforces trust anchor constraints compatibile with RFC 5937. 1042 void PathVerifier::ApplyTrustAnchorConstraints(const ParsedCertificate& cert,
440 // 1043 KeyPurpose required_key_purpose,
441 // Note that the anchor constraints are encoded via the attached certificate 1044 CertErrors* errors) {
442 // itself.
443 void ApplyTrustAnchorConstraints(
444 const ParsedCertificate& cert,
445 KeyPurpose required_key_purpose,
446 size_t* max_path_length_ptr,
447 std::vector<const NameConstraints*>* name_constraints_list,
448 CertErrors* errors) {
449 // This is not part of RFC 5937 nor RFC 5280, but matches the EKU handling 1045 // This is not part of RFC 5937 nor RFC 5280, but matches the EKU handling
450 // done for intermediates (described in Web PKI's Baseline Requirements). 1046 // done for intermediates (described in Web PKI's Baseline Requirements).
451 VerifyExtendedKeyUsage(cert, required_key_purpose, errors); 1047 VerifyExtendedKeyUsage(cert, required_key_purpose, errors);
452 1048
453 // The following enforcements follow from RFC 5937 (primarily section 3.2): 1049 // The following enforcements follow from RFC 5937 (primarily section 3.2):
454 1050
455 // Initialize name constraints initial-permitted/excluded-subtrees. 1051 // Initialize name constraints initial-permitted/excluded-subtrees.
456 if (cert.has_name_constraints()) 1052 if (cert.has_name_constraints())
457 name_constraints_list->push_back(&cert.name_constraints()); 1053 name_constraints_list_.push_back(&cert.name_constraints());
458 1054
459 // TODO(eroman): Initialize user-initial-policy-set based on anchor 1055 // TODO(eroman): Initialize user-initial-policy-set based on anchor
460 // constraints. 1056 // constraints.
461 1057
462 // TODO(eroman): Initialize inhibit any policy based on anchor constraints. 1058 // TODO(eroman): Initialize inhibit any policy based on anchor constraints.
463 1059
464 // TODO(eroman): Initialize require explicit policy based on anchor 1060 // TODO(eroman): Initialize require explicit policy based on anchor
465 // constraints. 1061 // constraints.
466 1062
467 // TODO(eroman): Initialize inhibit policy mapping based on anchor 1063 // TODO(eroman): Initialize inhibit policy mapping based on anchor
468 // constraints. 1064 // constraints.
469 1065
470 // From RFC 5937 section 3.2: 1066 // From RFC 5937 section 3.2:
471 // 1067 //
472 // If a basic constraints extension is associated with the trust 1068 // If a basic constraints extension is associated with the trust
473 // anchor and contains a pathLenConstraint value, set the 1069 // anchor and contains a pathLenConstraint value, set the
474 // max_path_length state variable equal to the pathLenConstraint 1070 // max_path_length state variable equal to the pathLenConstraint
475 // value from the basic constraints extension. 1071 // value from the basic constraints extension.
476 // 1072 //
477 // NOTE: RFC 5937 does not say to enforce the CA=true part of basic 1073 // NOTE: RFC 5937 does not say to enforce the CA=true part of basic
478 // constraints. 1074 // constraints.
479 if (cert.has_basic_constraints() && cert.basic_constraints().has_path_len) 1075 if (cert.has_basic_constraints() && cert.basic_constraints().has_path_len)
480 *max_path_length_ptr = cert.basic_constraints().path_len; 1076 max_path_length_ = cert.basic_constraints().path_len;
481 1077
482 // From RFC 5937 section 2: 1078 // From RFC 5937 section 2:
483 // 1079 //
484 // Extensions may be marked critical or not critical. When trust anchor 1080 // Extensions may be marked critical or not critical. When trust anchor
485 // constraints are enforced, clients MUST reject certification paths 1081 // constraints are enforced, clients MUST reject certification paths
486 // containing a trust anchor with unrecognized critical extensions. 1082 // containing a trust anchor with unrecognized critical extensions.
487 VerifyNoUnconsumedCriticalExtensions(cert, errors); 1083 VerifyNoUnconsumedCriticalExtensions(cert, errors);
488 } 1084 }
489 1085
490 // Initializes the path validation algorithm given anchor constraints. This 1086 void PathVerifier::ProcessRootCertificate(const ParsedCertificate& cert,
491 // follows the description in RFC 5937 1087 const CertificateTrust& trust,
492 void ProcessRootCertificate( 1088 KeyPurpose required_key_purpose,
493 const ParsedCertificate& cert, 1089 CertErrors* errors) {
494 const CertificateTrust& trust,
495 KeyPurpose required_key_purpose,
496 size_t* max_path_length_ptr,
497 std::vector<const NameConstraints*>* name_constraints_list,
498 der::Input* working_spki,
499 der::Input* working_normalized_issuer_name,
500 CertErrors* errors) {
501 // Use the certificate's SPKI and subject when verifying the next certificate. 1090 // Use the certificate's SPKI and subject when verifying the next certificate.
502 // Note this is initialized even in the case of untrusted roots (they already 1091 // Note this is initialized even in the case of untrusted roots (they already
503 // emit an error for the distrust). 1092 // emit an error for the distrust).
504 *working_spki = cert.tbs().spki_tlv; 1093 working_spki_ = cert.tbs().spki_tlv;
505 *working_normalized_issuer_name = cert.normalized_subject(); 1094 working_normalized_issuer_name_ = cert.normalized_subject();
506 1095
507 switch (trust.type) { 1096 switch (trust.type) {
508 case CertificateTrustType::UNSPECIFIED: 1097 case CertificateTrustType::UNSPECIFIED:
509 // Doesn't chain to a trust anchor - implicitly distrusted 1098 // Doesn't chain to a trust anchor - implicitly distrusted
510 errors->AddError(kCertIsNotTrustAnchor); 1099 errors->AddError(kCertIsNotTrustAnchor);
511 break; 1100 break;
512 case CertificateTrustType::DISTRUSTED: 1101 case CertificateTrustType::DISTRUSTED:
513 // Chains to an actively distrusted certificate. 1102 // Chains to an actively distrusted certificate.
514 errors->AddError(kCertIsDistrusted); 1103 errors->AddError(kCertIsDistrusted);
515 break; 1104 break;
516 case CertificateTrustType::TRUSTED_ANCHOR: 1105 case CertificateTrustType::TRUSTED_ANCHOR:
517 case CertificateTrustType::TRUSTED_ANCHOR_WITH_CONSTRAINTS: 1106 case CertificateTrustType::TRUSTED_ANCHOR_WITH_CONSTRAINTS:
518 // If the trust anchor has constraints, enforce them. 1107 // If the trust anchor has constraints, enforce them.
519 if (trust.type == CertificateTrustType::TRUSTED_ANCHOR_WITH_CONSTRAINTS) { 1108 if (trust.type == CertificateTrustType::TRUSTED_ANCHOR_WITH_CONSTRAINTS) {
520 ApplyTrustAnchorConstraints(cert, required_key_purpose, 1109 ApplyTrustAnchorConstraints(cert, required_key_purpose, errors);
521 max_path_length_ptr, name_constraints_list,
522 errors);
523 } 1110 }
524 break; 1111 break;
525 } 1112 }
526 } 1113 }
527 1114
528 } // namespace 1115 void PathVerifier::Run(
529 1116 const ParsedCertificateList& certs,
530 // This implementation is structured to mimic the description of certificate 1117 const CertificateTrust& last_cert_trust,
531 // path verification given by RFC 5280 section 6.1. 1118 const SignaturePolicy* signature_policy,
532 void VerifyCertificateChain(const ParsedCertificateList& certs, 1119 const der::GeneralizedTime& time,
533 const CertificateTrust& last_cert_trust, 1120 KeyPurpose required_key_purpose,
534 const SignaturePolicy* signature_policy, 1121 InitialExplicitPolicy initial_explicit_policy,
535 const der::GeneralizedTime& time, 1122 const std::set<der::Input>& user_initial_policy_set,
536 KeyPurpose required_key_purpose, 1123 InitialPolicyMappingInhibit initial_policy_mapping_inhibit,
537 CertPathErrors* errors) { 1124 InitialAnyPolicyInhibit initial_any_policy_inhibit,
1125 std::set<der::Input>* user_constrained_policy_set,
1126 CertPathErrors* errors) {
1127 // This implementation is structured to mimic the description of certificate
1128 // path verification given by RFC 5280 section 6.1.
538 DCHECK(signature_policy); 1129 DCHECK(signature_policy);
539 DCHECK(errors); 1130 DCHECK(errors);
540 1131
541 // An empty chain is necessarily invalid. 1132 // An empty chain is necessarily invalid.
542 if (certs.empty()) { 1133 if (certs.empty()) {
543 errors->GetOtherErrors()->AddError(kChainIsEmpty); 1134 errors->GetOtherErrors()->AddError(kChainIsEmpty);
544 return; 1135 return;
545 } 1136 }
546 1137
547 // TODO(eroman): Verifying a trusted leaf certificate is not currently 1138 // TODO(eroman): Verifying a trusted leaf certificate is not currently
548 // permitted. 1139 // permitted.
549 if (certs.size() == 1) { 1140 if (certs.size() == 1) {
550 errors->GetOtherErrors()->AddError(kChainIsLength1); 1141 errors->GetOtherErrors()->AddError(kChainIsLength1);
551 return; 1142 return;
552 } 1143 }
553 1144
554 // Will contain a NameConstraints for each previous cert in the chain which 1145 // RFC 5280's "n" variable is the length of the path, which does not count
555 // had nameConstraints. This corresponds to the permitted_subtrees and 1146 // the trust anchor. (Although in practice it doesn't really change behaviors
556 // excluded_subtrees state variables from RFC 5280. 1147 // if n is used in place of n+1).
557 std::vector<const NameConstraints*> name_constraints_list; 1148 const size_t n = certs.size() - 1;
558 1149
559 // |working_spki| is an amalgamation of 3 separate variables from RFC 5280: 1150 valid_policy_tree_.Init(user_initial_policy_set);
560 // * working_public_key 1151
561 // * working_public_key_algorithm 1152 // RFC 5280 section section 6.1.2:
562 // * working_public_key_parameters
563 // 1153 //
564 // They are combined for simplicity since the signature verification takes an 1154 // If initial-explicit-policy is set, then the initial value
565 // SPKI, and the parameter inheritence is not applicable for the supported 1155 // [of explicit_policy] is 0, otherwise the initial value is n+1.
566 // key types. 1156 explicit_policy_ =
1157 initial_explicit_policy == InitialExplicitPolicy::kTrue ? 0 : n + 1;
1158
1159 // RFC 5280 section section 6.1.2:
567 // 1160 //
568 // An approximate explanation of |working_spki| is this description from RFC 1161 // If initial-any-policy-inhibit is set, then the initial value
569 // 5280 section 6.1.2: 1162 // [of inhibit_anyPolicy] is 0, otherwise the initial value is n+1.
1163 inhibit_any_policy_ =
1164 initial_any_policy_inhibit == InitialAnyPolicyInhibit::kTrue ? 0 : n + 1;
1165
1166 // RFC 5280 section section 6.1.2:
570 // 1167 //
571 // working_public_key: the public key used to verify the 1168 // If initial-policy-mapping-inhibit is set, then the initial value
572 // signature of a certificate. 1169 // [of policy_mapping] is 0, otherwise the initial value is n+1.
573 der::Input working_spki; 1170 policy_mapping_ =
1171 initial_policy_mapping_inhibit == InitialPolicyMappingInhibit::kTrue
1172 ? 0
1173 : n + 1;
574 1174
575 // |working_normalized_issuer_name| is the normalized value of the 1175 // RFC 5280 section section 6.1.2:
576 // working_issuer_name variable in RFC 5280 section 6.1.2:
577 // 1176 //
578 // working_issuer_name: the issuer distinguished name expected 1177 // max_path_length: this integer is initialized to n, ...
579 // in the next certificate in the chain. 1178 max_path_length_ = n;
580 der::Input working_normalized_issuer_name;
581
582 // |max_path_length| corresponds with the same named variable in RFC 5280
583 // section 6.1.2:
584 //
585 // max_path_length: this integer is initialized to n, is
586 // decremented for each non-self-issued certificate in the path,
587 // and may be reduced to the value in the path length constraint
588 // field within the basic constraints extension of a CA
589 // certificate.
590 size_t max_path_length = certs.size();
591 1179
592 // Iterate over all the certificates in the reverse direction: starting from 1180 // Iterate over all the certificates in the reverse direction: starting from
593 // the root certificate and progressing towards the target certificate. 1181 // the root certificate and progressing towards the target certificate.
594 // 1182 //
595 // * i=0 : Root certificate (i.e. trust anchor) 1183 // * i=0 : Root certificate (i.e. trust anchor)
596 // * i=1 : Certificated signed by the root certificate 1184 // * i=1 : Certificate issued by root
597 // * i=certs.size()-1 : Target certificate. 1185 // * i=x : Certificate i=x is issued by certificate i=x-1
1186 // * i=n : Target certificate.
598 for (size_t i = 0; i < certs.size(); ++i) { 1187 for (size_t i = 0; i < certs.size(); ++i) {
599 const size_t index_into_certs = certs.size() - i - 1; 1188 const size_t index_into_certs = certs.size() - i - 1;
600 1189
601 // |is_target_cert| is true if the current certificate is the target 1190 // |is_target_cert| is true if the current certificate is the target
602 // certificate being verified. The target certificate isn't necessarily an 1191 // certificate being verified. The target certificate isn't necessarily an
603 // end-entity certificate. 1192 // end-entity certificate.
604 const bool is_target_cert = index_into_certs == 0; 1193 const bool is_target_cert = index_into_certs == 0;
605 const bool is_root_cert = i == 0; 1194 const bool is_root_cert = i == 0;
606 1195
607 const ParsedCertificate& cert = *certs[index_into_certs]; 1196 const ParsedCertificate& cert = *certs[index_into_certs];
608 1197
609 // Output errors for the current certificate into an error bucket that is 1198 // Output errors for the current certificate into an error bucket that is
610 // associated with that certificate. 1199 // associated with that certificate.
611 CertErrors* cert_errors = errors->GetErrorsForCert(index_into_certs); 1200 CertErrors* cert_errors = errors->GetErrorsForCert(index_into_certs);
612 1201
613 if (is_root_cert) { 1202 if (is_root_cert) {
614 ProcessRootCertificate(cert, last_cert_trust, required_key_purpose, 1203 ProcessRootCertificate(cert, last_cert_trust, required_key_purpose,
615 &max_path_length, &name_constraints_list,
616 &working_spki, &working_normalized_issuer_name,
617 cert_errors); 1204 cert_errors);
618 1205
619 // Don't do any other checks for root certificates. 1206 // Don't do any other checks for root certificates.
620 continue; 1207 continue;
621 } 1208 }
622 1209
623 // Per RFC 5280 section 6.1: 1210 // Per RFC 5280 section 6.1:
624 // * Do basic processing for each certificate 1211 // * Do basic processing for each certificate
625 // * If it is the last certificate in the path (target certificate) 1212 // * If it is the last certificate in the path (target certificate)
626 // - Then run "Wrap up" 1213 // - Then run "Wrap up"
627 // - Otherwise run "Prepare for Next cert" 1214 // - Otherwise run "Prepare for Next cert"
628 BasicCertificateProcessing(cert, is_target_cert, signature_policy, time, 1215 BasicCertificateProcessing(cert, is_target_cert, signature_policy, time,
629 working_spki, working_normalized_issuer_name, 1216 required_key_purpose, cert_errors);
630 name_constraints_list, cert_errors);
631
632 // The key purpose is checked not just for the end-entity certificate, but
633 // also interpreted as a constraint when it appears in intermediates. This
634 // goes beyond what RFC 5280 describes, but is the de-facto standard. See
635 // https://wiki.mozilla.org/CA:CertificatePolicyV2.1#Frequently_Asked_Questi ons
636 VerifyExtendedKeyUsage(cert, required_key_purpose, cert_errors);
637
638 if (!is_target_cert) { 1217 if (!is_target_cert) {
639 PrepareForNextCertificate(cert, &max_path_length, &working_spki, 1218 PrepareForNextCertificate(cert, cert_errors);
640 &working_normalized_issuer_name,
641 &name_constraints_list, cert_errors);
642 } else { 1219 } else {
643 WrapUp(cert, cert_errors); 1220 WrapUp(cert, cert_errors);
644 } 1221 }
645 } 1222 }
646 1223
1224 if (user_constrained_policy_set) {
1225 // valid_policy_tree_ already contains the intersection of valid policies
1226 // with user_initial_policy_set.
1227 valid_policy_tree_.GetValidRootPolicySet(user_constrained_policy_set);
1228 }
1229
647 // TODO(eroman): RFC 5280 forbids duplicate certificates per section 6.1: 1230 // TODO(eroman): RFC 5280 forbids duplicate certificates per section 6.1:
648 // 1231 //
649 // A certificate MUST NOT appear more than once in a prospective 1232 // A certificate MUST NOT appear more than once in a prospective
650 // certification path. 1233 // certification path.
651 } 1234 }
652 1235
1236 } // namespace
1237
1238 void VerifyCertificateChain(
1239 const ParsedCertificateList& certs,
1240 const CertificateTrust& last_cert_trust,
1241 const SignaturePolicy* signature_policy,
1242 const der::GeneralizedTime& time,
1243 KeyPurpose required_key_purpose,
1244 InitialExplicitPolicy initial_explicit_policy,
1245 const std::set<der::Input>& user_initial_policy_set,
1246 InitialPolicyMappingInhibit initial_policy_mapping_inhibit,
1247 InitialAnyPolicyInhibit initial_any_policy_inhibit,
1248 std::set<der::Input>* user_constrained_policy_set,
1249 CertPathErrors* errors) {
1250 PathVerifier verifier;
1251 verifier.Run(certs, last_cert_trust, signature_policy, time,
1252 required_key_purpose, initial_explicit_policy,
1253 user_initial_policy_set, initial_policy_mapping_inhibit,
1254 initial_any_policy_inhibit, user_constrained_policy_set, errors);
1255 }
1256
653 } // namespace net 1257 } // namespace net
OLDNEW
« no previous file with comments | « net/cert/internal/verify_certificate_chain.h ('k') | net/cert/internal/verify_certificate_chain_pkits_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698