OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/base/x509_cert_types.h" | |
6 | |
7 #include "net/base/x509_certificate.h" | |
8 #include "base/logging.h" | |
9 | |
10 namespace net { | |
11 | |
12 bool match(const std::string &str, const std::string &against) { | |
13 // TODO(snej): Use the full matching rules specified in RFC 5280 sec. 7.1 | |
14 // including trimming and case-folding: <http://www.ietf.org/rfc/rfc5280.txt>. | |
15 return against == str; | |
16 } | |
17 | |
18 bool match(const std::vector<std::string> &rdn1, | |
19 const std::vector<std::string> &rdn2) { | |
20 // "Two relative distinguished names RDN1 and RDN2 match if they have the | |
21 // same number of naming attributes and for each naming attribute in RDN1 | |
22 // there is a matching naming attribute in RDN2." --RFC 5280 sec. 7.1. | |
23 if (rdn1.size() != rdn2.size()) | |
24 return false; | |
25 for (unsigned i1 = 0; i1 < rdn1.size(); ++i1) { | |
26 unsigned i2; | |
27 for (i2 = 0; i2 < rdn2.size(); ++i2) { | |
28 if (match(rdn1[i1], rdn2[i2])) | |
29 break; | |
30 } | |
31 if (i2 == rdn2.size()) | |
32 return false; | |
33 } | |
34 return true; | |
35 } | |
36 | |
37 | |
38 bool CertPrincipal::Matches(const CertPrincipal& against) const { | |
39 return match(common_name, against.common_name) && | |
40 match(common_name, against.common_name) && | |
41 match(locality_name, against.locality_name) && | |
42 match(state_or_province_name, against.state_or_province_name) && | |
43 match(country_name, against.country_name) && | |
44 match(street_addresses, against.street_addresses) && | |
45 match(organization_names, against.organization_names) && | |
46 match(organization_unit_names, against.organization_unit_names) && | |
47 match(domain_components, against.domain_components); | |
48 } | |
49 | |
50 std::ostream& operator<<(std::ostream& s, const CertPrincipal& p) { | |
51 s << "CertPrincipal["; | |
52 if (!p.common_name.empty()) | |
53 s << "cn=\"" << p.common_name << "\" "; | |
54 for (unsigned i = 0; i < p.street_addresses.size(); ++i) | |
55 s << "street=\"" << p.street_addresses[i] << "\" "; | |
56 if (!p.locality_name.empty()) | |
57 s << "l=\"" << p.locality_name << "\" "; | |
58 for (unsigned i = 0; i < p.organization_names.size(); ++i) | |
59 s << "o=\"" << p.organization_names[i] << "\" "; | |
60 for (unsigned i = 0; i < p.organization_unit_names.size(); ++i) | |
61 s << "ou=\"" << p.organization_unit_names[i] << "\" "; | |
62 if (!p.state_or_province_name.empty()) | |
63 s << "st=\"" << p.state_or_province_name << "\" "; | |
64 if (!p.country_name.empty()) | |
65 s << "c=\"" << p.country_name << "\" "; | |
66 for (unsigned i = 0; i < p.domain_components.size(); ++i) | |
67 s << "dc=\"" << p.domain_components[i] << "\" "; | |
68 return s << "]"; | |
69 } | |
70 | |
71 CertPolicy::Judgment CertPolicy::Check( | |
72 X509Certificate* cert) const { | |
73 // It shouldn't matter which set we check first, but we check denied first | |
74 // in case something strange has happened. | |
75 | |
76 if (denied_.find(cert->fingerprint()) != denied_.end()) { | |
77 // DCHECK that the order didn't matter. | |
78 DCHECK(allowed_.find(cert->fingerprint()) == allowed_.end()); | |
79 return DENIED; | |
80 } | |
81 | |
82 if (allowed_.find(cert->fingerprint()) != allowed_.end()) { | |
83 // DCHECK that the order didn't matter. | |
84 DCHECK(denied_.find(cert->fingerprint()) == denied_.end()); | |
85 return ALLOWED; | |
86 } | |
87 | |
88 // We don't have a policy for this cert. | |
89 return UNKNOWN; | |
90 } | |
91 | |
92 void CertPolicy::Allow(X509Certificate* cert) { | |
93 // Put the cert in the allowed set and (maybe) remove it from the denied set. | |
94 denied_.erase(cert->fingerprint()); | |
95 allowed_.insert(cert->fingerprint()); | |
96 } | |
97 | |
98 void CertPolicy::Deny(X509Certificate* cert) { | |
99 // Put the cert in the denied set and (maybe) remove it from the allowed set. | |
100 allowed_.erase(cert->fingerprint()); | |
101 denied_.insert(cert->fingerprint()); | |
102 } | |
103 | |
104 bool CertPolicy::HasAllowedCert() const { | |
105 return !allowed_.empty(); | |
106 } | |
107 | |
108 bool CertPolicy::HasDeniedCert() const { | |
109 return !denied_.empty(); | |
110 } | |
111 | |
112 } // namespace net | |
OLD | NEW |