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) | |
wtc
2010/03/24 23:52:05
Nit: add curly braces around the for loop's body b
| |
28 if (match(rdn1[i1], rdn2[i2])) | |
29 break; | |
30 if (i2 == rdn2.size()) | |
31 return false; | |
32 } | |
33 return true; | |
34 } | |
35 | |
36 | |
37 bool CertPrincipal::Matches(const CertPrincipal& against) const { | |
38 return match(common_name, against.common_name) && | |
39 match(common_name, against.common_name) && | |
40 match(locality_name, against.locality_name) && | |
41 match(state_or_province_name, against.state_or_province_name) && | |
42 match(country_name, against.country_name) && | |
43 match(street_addresses, against.street_addresses) && | |
44 match(organization_names, against.organization_names) && | |
45 match(organization_unit_names, against.organization_unit_names) && | |
46 match(domain_components, against.domain_components); | |
47 } | |
48 | |
49 std::ostream& operator<<(std::ostream& s, const CertPrincipal& p) { | |
wtc
2010/03/24 23:52:05
I seem to recall domainComponent is printed as "dc
rsleevi-old
2010/03/25 05:34:11
Historically, RFC1485 [1] established IANA as the
| |
50 s << "CertPrincipal["; | |
51 if (!p.common_name.empty()) | |
52 s << "cn=\"" << p.common_name << "\" "; | |
53 for (unsigned i = 0; i < p.street_addresses.size(); ++i) | |
54 s << "addr=\"" << p.street_addresses[i] << "\" "; | |
55 if (!p.locality_name.empty()) | |
56 s << "loc=\"" << p.locality_name << "\" "; | |
57 for (unsigned i = 0; i < p.organization_names.size(); ++i) | |
58 s << "o=\"" << p.organization_names[i] << "\" "; | |
59 for (unsigned i = 0; i < p.organization_unit_names.size(); ++i) | |
60 s << "ou=\"" << p.organization_unit_names[i] << "\" "; | |
61 if (!p.state_or_province_name.empty()) | |
62 s << "state=\"" << p.state_or_province_name << "\" "; | |
63 if (!p.country_name.empty()) | |
64 s << "country=\"" << p.country_name << "\" "; | |
65 for (unsigned i = 0; i < p.domain_components.size(); ++i) | |
66 s << "dom=\"" << p.domain_components[i] << "\" "; | |
67 return s << "]"; | |
68 } | |
69 | |
70 X509Certificate::Policy::Judgment X509Certificate::Policy::Check( | |
wtc
2010/03/24 23:52:05
In the rest of the file, X509Certificate::Policy s
| |
71 X509Certificate* cert) const { | |
72 // It shouldn't matter which set we check first, but we check denied first | |
73 // in case something strange has happened. | |
74 | |
75 if (denied_.find(cert->fingerprint()) != denied_.end()) { | |
76 // DCHECK that the order didn't matter. | |
77 DCHECK(allowed_.find(cert->fingerprint()) == allowed_.end()); | |
78 return DENIED; | |
79 } | |
80 | |
81 if (allowed_.find(cert->fingerprint()) != allowed_.end()) { | |
82 // DCHECK that the order didn't matter. | |
83 DCHECK(denied_.find(cert->fingerprint()) == denied_.end()); | |
84 return ALLOWED; | |
85 } | |
86 | |
87 // We don't have a policy for this cert. | |
88 return UNKNOWN; | |
89 } | |
90 | |
91 void X509Certificate::Policy::Allow(X509Certificate* cert) { | |
92 // Put the cert in the allowed set and (maybe) remove it from the denied set. | |
93 denied_.erase(cert->fingerprint()); | |
94 allowed_.insert(cert->fingerprint()); | |
95 } | |
96 | |
97 void X509Certificate::Policy::Deny(X509Certificate* cert) { | |
98 // Put the cert in the denied set and (maybe) remove it from the allowed set. | |
99 allowed_.erase(cert->fingerprint()); | |
100 denied_.insert(cert->fingerprint()); | |
101 } | |
102 | |
103 bool X509Certificate::Policy::HasAllowedCert() const { | |
104 return !allowed_.empty(); | |
105 } | |
106 | |
107 bool X509Certificate::Policy::HasDeniedCert() const { | |
108 return !denied_.empty(); | |
109 } | |
110 | |
111 } // namespace net | |
OLD | NEW |