OLD | NEW |
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 #ifndef NET_CERT_INTERNAL_VERIFY_CERTIFICATE_CHAIN_H_ | 5 #ifndef NET_CERT_INTERNAL_VERIFY_CERTIFICATE_CHAIN_H_ |
6 #define NET_CERT_INTERNAL_VERIFY_CERTIFICATE_CHAIN_H_ | 6 #define NET_CERT_INTERNAL_VERIFY_CERTIFICATE_CHAIN_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <map> |
10 #include <memory> | 11 #include <memory> |
11 #include <string> | 12 #include <string> |
| 13 #include <unordered_map> |
12 #include <vector> | 14 #include <vector> |
13 | 15 |
14 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 17 #include "base/memory/ref_counted.h" |
15 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
| 19 #include "net/cert/internal/name_constraints.h" |
16 #include "net/cert/internal/parse_certificate.h" | 20 #include "net/cert/internal/parse_certificate.h" |
17 #include "net/der/input.h" | 21 #include "net/der/input.h" |
18 | 22 |
19 namespace net { | 23 namespace net { |
20 | 24 |
21 namespace der { | 25 namespace der { |
22 struct GeneralizedTime; | 26 struct GeneralizedTime; |
23 } | 27 } |
24 | 28 |
25 class SignaturePolicy; | 29 class SignaturePolicy; |
26 | 30 |
27 // Represents a trust anchor (i.e. a trusted root certificate). | 31 // XXX Rename, better comment |
28 class NET_EXPORT TrustAnchor { | 32 // Represents a certificate, including top-level parsing and normalized name |
| 33 // values. The certificate is not completely parsed and validated, only the |
| 34 // validation performed by ParseCertificate, ParseTbsCertificate and |
| 35 // NormalizeName is done. |
| 36 class NET_EXPORT CertThing : public base::RefCountedThreadSafe<CertThing> { |
29 public: | 37 public: |
| 38 // Map from OID to ParsedExtension. |
| 39 using ExtensionsMap = std::map<der::Input, ParsedExtension>; |
| 40 |
30 // The certificate data for this trust anchor may either be owned internally | 41 // The certificate data for this trust anchor may either be owned internally |
31 // (INTERNAL_COPY) or owned externally (EXTERNAL_REFERENCE). When it is | 42 // (INTERNAL_COPY) or owned externally (EXTERNAL_REFERENCE). When it is |
32 // owned internally the data is held by |cert_data_| | 43 // owned internally the data is held by |cert_data_| |
33 enum class DataSource { | 44 enum class DataSource { |
34 INTERNAL_COPY, | 45 INTERNAL_COPY, |
35 EXTERNAL_REFERENCE, | 46 EXTERNAL_REFERENCE, |
36 }; | 47 }; |
37 | 48 |
38 TrustAnchor(); | 49 // Creates a CertThing given a DER-encoded certificate. Returns nullptr on |
39 ~TrustAnchor(); | |
40 | |
41 // Creates a TrustAnchor given a DER-encoded certificate. Returns nullptr on | |
42 // failure. Failure will occur if the certificate data cannot be parsed to | 50 // failure. Failure will occur if the certificate data cannot be parsed to |
43 // find a subject. | 51 // find a subject. |
44 // | 52 // |
45 // The provided certificate data is either copied, or aliased, depending on | 53 // The provided certificate data is either copied, or aliased, depending on |
46 // the value of |source|. See the comments for DataSource for details. | 54 // the value of |source|. See the comments for DataSource for details. |
47 static std::unique_ptr<TrustAnchor> CreateFromCertificateData( | 55 static scoped_refptr<CertThing> CreateFromCertificateData(const uint8_t* data, |
48 const uint8_t* data, | 56 size_t length, |
49 size_t length, | 57 DataSource source); |
50 DataSource source); | 58 static scoped_refptr<CertThing> CreateFromCertificateCopy( |
| 59 const base::StringPiece& data); |
51 | 60 |
52 // Returns true if the trust anchor matches |name|. In other words, returns | 61 // Returns the DER-encoded certificate data for this cert. |
53 // true if the certificate's subject matches |name|. | 62 const der::Input& der_cert() const { return cert_; } |
54 bool MatchesName(const der::Input& name) const; | |
55 | 63 |
56 // Returns the DER-encoded certificate data for this trust anchor. | 64 const ParsedCertificate& parsed_cert() const { return parsed_cert_; } |
57 const der::Input& cert() const { return cert_; } | 65 const ParsedTbsCertificate& parsed_tbs() const { return parsed_tbs_; } |
| 66 |
| 67 // Returns the DER-encoded normalized subject value (not including outer |
| 68 // Sequence tag). |
| 69 const std::string& normalized_subject() const { return normalized_subject_; } |
| 70 // Returns the DER-encoded normalized issuer value (not including outer |
| 71 // Sequence tag). |
| 72 const std::string& normalized_issuer() const { return normalized_issuer_; } |
| 73 |
| 74 // Returns true if the certificate had a SubjectAltName extension. |
| 75 bool has_subject_alt_names() const { return subject_alt_names_ != nullptr; } |
| 76 // Returns the ParsedExtension struct for the SubjectAltName extension. |
| 77 // If the cert did not have a SubjectAltName extension, this will be a |
| 78 // default-initialized ParsedExtension struct. |
| 79 const ParsedExtension& subject_alt_names_extension() const { |
| 80 return subject_alt_names_extension_; |
| 81 } |
| 82 // Returns the GeneralNames class parsed from SubjectAltName extension, orr |
| 83 // nullptr if no SubjectAltName extension was present. |
| 84 const GeneralNames* subject_alt_names() const { |
| 85 return subject_alt_names_.get(); |
| 86 } |
| 87 |
| 88 // Returns a map of unhandled extensions (excludes the ones above). |
| 89 const ExtensionsMap& unconsumed_extensions() const { |
| 90 return unconsumed_extensions_; |
| 91 } |
58 | 92 |
59 private: | 93 private: |
| 94 friend class base::RefCountedThreadSafe<CertThing>; |
| 95 CertThing(); |
| 96 ~CertThing(); |
| 97 |
60 // The backing store for the certificate data. This is only applicable when | 98 // The backing store for the certificate data. This is only applicable when |
61 // the trust anchor was initialized using DataSource::INTERNAL_COPY. | 99 // the trust anchor was initialized using DataSource::INTERNAL_COPY. |
62 std::vector<uint8_t> cert_data_; | 100 std::vector<uint8_t> cert_data_; |
63 | 101 |
64 // Note that the backing data for |cert_| and |name_| may come either form | 102 // Note that the backing data for |cert_| and |name_| may come either form |
65 // |cert_data_| or some external buffer (depending on how the anchor was | 103 // |cert_data_| or some external buffer (depending on how the anchor was |
66 // created). | 104 // created). |
67 | 105 |
68 // Points to the raw certificate DER. | 106 // Points to the raw certificate DER. |
69 der::Input cert_; | 107 der::Input cert_; |
70 | 108 |
71 // Points to the subject TLV for the certificate. | 109 ParsedCertificate parsed_cert_; |
72 der::Input name_; | 110 ParsedTbsCertificate parsed_tbs_; |
73 | 111 |
74 DISALLOW_COPY_AND_ASSIGN(TrustAnchor); | 112 // Normalized DER-encoded Subject (not including outer Sequence tag). |
| 113 std::string normalized_subject_; |
| 114 // Normalized DER-encoded Issuer (not including outer Sequence tag). |
| 115 std::string normalized_issuer_; |
| 116 |
| 117 // Raw SubjectAltName extension. |
| 118 ParsedExtension subject_alt_names_extension_; |
| 119 // Parsed SubjectAltName extension. |
| 120 std::unique_ptr<GeneralNames> subject_alt_names_; |
| 121 |
| 122 // The remaining extensions (excludes the standard ones above). |
| 123 ExtensionsMap unconsumed_extensions_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(CertThing); |
75 }; | 126 }; |
76 | 127 |
| 128 using CertVector = std::vector<scoped_refptr<CertThing>>; |
| 129 |
77 // A very simple implementation of a TrustStore, which contains a set of | 130 // A very simple implementation of a TrustStore, which contains a set of |
78 // trusted certificates. | 131 // trusted certificates. |
79 class NET_EXPORT TrustStore { | 132 class NET_EXPORT TrustStore { |
80 public: | 133 public: |
81 TrustStore(); | 134 TrustStore(); |
82 ~TrustStore(); | 135 ~TrustStore(); |
83 | 136 |
84 // Empties the trust store, resetting it to original state. | 137 // Empties the trust store, resetting it to original state. |
85 void Clear(); | 138 void Clear(); |
86 | 139 |
87 // Adds a trusted certificate to the store. The trust store makes a copy of | 140 // Adds a trusted certificate to the store. |
88 // the provided certificate data. | 141 void AddTrustedCertificate(scoped_refptr<CertThing> anchor); |
89 bool AddTrustedCertificate(const uint8_t* data, | |
90 size_t length) WARN_UNUSED_RESULT; | |
91 bool AddTrustedCertificate(const base::StringPiece& data) WARN_UNUSED_RESULT; | |
92 | 142 |
93 // This function is the same as AddTrustedCertificate() except the underlying | 143 // Returns the trust anchors that match |name| in |*matches|, if any. |
94 // data is not copied. The caller is responsible for ensuring that the data | 144 void FindTrustAnchorsByNormalizedName(const std::string& normalized_name, |
95 // pointer remains alive and is not mutated for the lifetime of the | 145 CertVector* matches) const; |
96 // TrustStore. | |
97 bool AddTrustedCertificateWithoutCopying(const uint8_t* data, | |
98 size_t length) WARN_UNUSED_RESULT; | |
99 | 146 |
100 // Returns the trust anchor that matches |name|, or nullptr if there is none. | 147 // Returns true if |cert| matches a certificate in the TrustStore. |
101 // TODO(eroman): There may be multiple matches. | 148 bool IsTrustedCertificate(const CertThing* cert) const WARN_UNUSED_RESULT; |
102 const TrustAnchor* FindTrustAnchorByName(const der::Input& name) const | |
103 WARN_UNUSED_RESULT; | |
104 | |
105 // Returns true if |cert_der| matches a certificate in the TrustStore. | |
106 bool IsTrustedCertificate(const der::Input& cert_der) const | |
107 WARN_UNUSED_RESULT; | |
108 | 149 |
109 private: | 150 private: |
110 bool AddTrustedCertificate(const uint8_t* data, | 151 // Multimap from normalized subject -> CertThing. |
111 size_t length, | 152 std::unordered_multimap<base::StringPiece, |
112 TrustAnchor::DataSource source) WARN_UNUSED_RESULT; | 153 scoped_refptr<CertThing>, |
113 | 154 base::StringPieceHash> |
114 std::vector<std::unique_ptr<TrustAnchor>> anchors_; | 155 anchors_; |
115 | 156 |
116 DISALLOW_COPY_AND_ASSIGN(TrustStore); | 157 DISALLOW_COPY_AND_ASSIGN(TrustStore); |
117 }; | 158 }; |
118 | 159 |
119 // VerifyCertificateChain() verifies a certificate path (chain) based on the | 160 // VerifyCertificateChain() verifies a certificate path (chain) based on the |
120 // rules in RFC 5280. | 161 // rules in RFC 5280. |
121 // | 162 // |
122 // WARNING: This implementation is in progress, and is currently | 163 // WARNING: This implementation is in progress, and is currently |
123 // incomplete. DO NOT USE IT unless its limitations are acceptable for your use. | 164 // incomplete. DO NOT USE IT unless its limitations are acceptable for your use. |
124 // | 165 // |
(...skipping 23 matching lines...) Expand all Loading... |
148 // Outputs | 189 // Outputs |
149 // --------- | 190 // --------- |
150 // | 191 // |
151 // Returns true if the target certificate can be verified. | 192 // Returns true if the target certificate can be verified. |
152 NET_EXPORT bool VerifyCertificateChain(const std::vector<der::Input>& certs_der, | 193 NET_EXPORT bool VerifyCertificateChain(const std::vector<der::Input>& certs_der, |
153 const TrustStore& trust_store, | 194 const TrustStore& trust_store, |
154 const SignaturePolicy* signature_policy, | 195 const SignaturePolicy* signature_policy, |
155 const der::GeneralizedTime& time) | 196 const der::GeneralizedTime& time) |
156 WARN_UNUSED_RESULT; | 197 WARN_UNUSED_RESULT; |
157 | 198 |
| 199 // XXX docs. |
| 200 NET_EXPORT bool VerifyCertificateChainAssumingTrustedRoot( |
| 201 const std::vector<scoped_refptr<CertThing>>& certs, |
| 202 // The trust store is only used for assertions. |
| 203 const TrustStore& trust_store, |
| 204 const SignaturePolicy* signature_policy, |
| 205 const der::GeneralizedTime& time) WARN_UNUSED_RESULT; |
| 206 |
158 } // namespace net | 207 } // namespace net |
159 | 208 |
160 #endif // NET_CERT_INTERNAL_VERIFY_CERTIFICATE_CHAIN_H_ | 209 #endif // NET_CERT_INTERNAL_VERIFY_CERTIFICATE_CHAIN_H_ |
OLD | NEW |