OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #ifndef NET_CERT_INTERNAL_PARSED_CERTIFICATE_H_ | |
6 #define NET_CERT_INTERNAL_PARSED_CERTIFICATE_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <vector> | |
11 | |
12 #include "base/memory/ref_counted.h" | |
13 #include "net/base/net_export.h" | |
14 #include "net/cert/internal/parse_certificate.h" | |
eroman
2016/05/12 18:12:30
This is a weird dependency, but I guess unavoidabl
| |
15 #include "net/der/input.h" | |
16 | |
17 namespace net { | |
18 | |
19 struct GeneralNames; | |
20 class NameConstraints; | |
21 class SignatureAlgorithm; | |
22 | |
23 // XXX Rename. | |
eroman
2016/05/12 18:12:29
TODO. That said I think you can just remove this l
mattm
2016/05/13 02:17:36
oops, yeah. I forgot to remove that.
| |
24 // Represents a certificate, including Certificate, TBSCertificate, and standard | |
25 // extensions. | |
26 // The certificate is not completely parsed and validated, only the | |
27 // validation performed by ParseCertificate, ParseTbsCertificate, | |
28 // NormalizeName, ParseBasicConstraints, ParseKeyUsage, | |
29 // GeneralNames::CreateFromDer(subjectAltName), and | |
30 // NameConstraints::CreateFromDer is done. | |
eroman
2016/05/12 18:12:29
Is there maybe a more general description we can g
mattm
2016/05/13 02:17:36
I'll try to come up with a better wording. In the
| |
31 class NET_EXPORT ParsedCertificate | |
32 : public base::RefCountedThreadSafe<ParsedCertificate> { | |
33 public: | |
34 // Map from OID to ParsedExtension. | |
35 using ExtensionsMap = std::map<der::Input, ParsedExtension>; | |
36 | |
37 // The certificate data for this trust anchor may either be owned internally | |
38 // (INTERNAL_COPY) or owned externally (EXTERNAL_REFERENCE). When it is | |
39 // owned internally the data is held by |cert_data_| | |
40 enum class DataSource { | |
41 INTERNAL_COPY, | |
42 EXTERNAL_REFERENCE, | |
43 }; | |
44 | |
45 // Creates a ParsedCertificate given a DER-encoded Certificate. Returns | |
46 // nullptr on | |
eroman
2016/05/12 18:12:30
line wrap.
mattm
2016/05/13 02:17:37
Done.
| |
47 // failure. Failure will occur if the standard certificate fields and | |
48 // supported extensions cannot be parsed. | |
49 // | |
50 // The provided certificate data is either copied, or aliased, depending on | |
51 // the value of |source|. See the comments for DataSource for details. | |
52 static scoped_refptr<ParsedCertificate> CreateFromCertificateData( | |
53 const uint8_t* data, | |
54 size_t length, | |
55 DataSource source); | |
56 static scoped_refptr<ParsedCertificate> CreateFromCertificateCopy( | |
57 const base::StringPiece& data); | |
58 | |
59 // Returns the DER-encoded certificate data for this cert. | |
60 const der::Input& der_cert() const { return cert_; } | |
61 | |
62 // Accessors for raw fields of the Certificate. | |
63 const der::Input& tbs_certificate_tlv() const { return tbs_certificate_tlv_; } | |
64 const der::Input& signature_algorithm_tlv() const { | |
65 return signature_algorithm_tlv_; | |
66 } | |
67 const der::BitString& signature_value() const { return signature_value_; } | |
68 // Accessor for struct containing raw fields of the TbsCertificate. | |
69 const ParsedTbsCertificate& parsed_tbs() const { return parsed_tbs_; } | |
70 | |
71 // Returns true if the signatureAlgorithm of the Certificate is supported. | |
72 bool has_supported_signature_algorithm() const { | |
eroman
2016/05/12 18:12:29
It is a bit ambiguous from the name how this relat
mattm
2016/05/13 02:17:36
Done.
| |
73 return signature_algorithm_ != nullptr; | |
74 } | |
75 // Returns the signatureAlgorithm of the Certificate (not the tbsCertificate). | |
76 // Must not be called if has_supported_signature_algorithm() is false. | |
77 const SignatureAlgorithm& signature_algorithm() const { | |
eroman
2016/05/12 18:12:29
Add a DCHECK() ?
(de-referencing wont' necessarily
mattm
2016/05/13 02:17:37
Done.
| |
78 return *signature_algorithm_; | |
79 } | |
80 | |
81 // Returns the DER-encoded normalized subject value (not including outer | |
82 // Sequence tag). | |
83 const std::string& normalized_subject() const { return normalized_subject_; } | |
eroman
2016/05/12 18:12:30
der::Input ?
mattm
2016/05/13 02:17:36
Done.
| |
84 // Returns the DER-encoded normalized issuer value (not including outer | |
85 // Sequence tag). | |
86 const std::string& normalized_issuer() const { return normalized_issuer_; } | |
eroman
2016/05/12 18:12:30
der::Input ?
mattm
2016/05/13 02:17:37
Done.
| |
87 | |
88 // Returns true if the certificate had a BasicConstraints extension. | |
89 bool has_basic_constraints() const { return has_basic_constraints_; } | |
90 // Returns the ParsedBasicConstraints struct. Caller should check | |
eroman
2016/05/12 18:12:30
should --> must
mattm
2016/05/13 02:17:36
Done.
| |
91 // has_basic_constraints() before accessing this. | |
92 const ParsedBasicConstraints& basic_constraints() const { | |
93 return basic_constraints_; | |
eroman
2016/05/12 18:12:29
Add a CHECK or DCHECK ?
mattm
2016/05/13 02:17:36
Done.
| |
94 } | |
95 | |
96 // Returns true if the certificate had a KeyUsage extension. | |
97 bool has_key_usage() const { return has_key_usage_; } | |
98 // Returns the KeyUsage BitString. Caller should check | |
eroman
2016/05/12 18:12:30
should--> must
Or alternately we could guarantee
mattm
2016/05/13 02:17:37
done.
| |
99 // has_key_usage() before accessing this. | |
100 const der::BitString& key_usage() const { return key_usage_; } | |
101 | |
102 // Returns true if the certificate had a SubjectAltName extension. | |
103 bool has_subject_alt_names() const { return subject_alt_names_ != nullptr; } | |
104 // Returns the ParsedExtension struct for the SubjectAltName extension. | |
105 // If the cert did not have a SubjectAltName extension, this will be a | |
106 // default-initialized ParsedExtension struct. | |
107 const ParsedExtension& subject_alt_names_extension() const { | |
108 return subject_alt_names_extension_; | |
109 } | |
110 // Returns the GeneralNames class parsed from SubjectAltName extension, or | |
111 // nullptr if no SubjectAltName extension was present. | |
112 const GeneralNames* subject_alt_names() const { | |
113 return subject_alt_names_.get(); | |
114 } | |
115 | |
116 // Returns true if the certificate had a NameConstraints extension. | |
eroman
2016/05/12 18:12:29
nit: had --> has.
All of these properties correspo
mattm
2016/05/13 02:17:37
Done.
| |
117 bool has_name_constraints() const { return name_constraints_ != nullptr; } | |
118 // Returns the parsed NameConstraints extension. Must not be called if | |
119 // has_name_constraints() is false. | |
120 const NameConstraints& name_constraints() const { return *name_constraints_; } | |
121 | |
122 // Returns a map of unhandled extensions (excludes the ones above). | |
123 const ExtensionsMap& unconsumed_extensions() const { | |
124 return unconsumed_extensions_; | |
125 } | |
126 | |
127 private: | |
128 friend class base::RefCountedThreadSafe<ParsedCertificate>; | |
129 ParsedCertificate(); | |
130 ~ParsedCertificate(); | |
131 | |
132 // The backing store for the certificate data. This is only applicable when | |
133 // the trust anchor was initialized using DataSource::INTERNAL_COPY. | |
134 std::vector<uint8_t> cert_data_; | |
135 | |
136 // Note that the backing data for |cert_| and |name_| may come either form | |
137 // |cert_data_| or some external buffer (depending on how the anchor was | |
138 // created). | |
139 | |
140 // Points to the raw certificate DER. | |
141 der::Input cert_; | |
142 | |
143 der::Input tbs_certificate_tlv_; | |
144 der::Input signature_algorithm_tlv_; | |
145 der::BitString signature_value_; | |
146 ParsedTbsCertificate parsed_tbs_; | |
147 | |
148 // The signatureAlgorithm from the Certificate. | |
149 std::unique_ptr<SignatureAlgorithm> signature_algorithm_; | |
150 | |
151 // Normalized DER-encoded Subject (not including outer Sequence tag). | |
152 std::string normalized_subject_; | |
153 // Normalized DER-encoded Issuer (not including outer Sequence tag). | |
154 std::string normalized_issuer_; | |
155 | |
156 // BasicConstraints extension. | |
157 bool has_basic_constraints_ = false; | |
158 ParsedBasicConstraints basic_constraints_; | |
159 | |
160 // KeyUsage extension. | |
161 bool has_key_usage_ = false; | |
162 der::BitString key_usage_; | |
163 | |
164 // Raw SubjectAltName extension. | |
165 ParsedExtension subject_alt_names_extension_; | |
166 // Parsed SubjectAltName extension. | |
167 std::unique_ptr<GeneralNames> subject_alt_names_; | |
168 | |
169 // NameConstraints extension. | |
170 bool has_name_constraints_ = false; | |
171 std::unique_ptr<NameConstraints> name_constraints_; | |
172 | |
173 // The remaining extensions (excludes the standard ones above). | |
174 ExtensionsMap unconsumed_extensions_; | |
eroman
2016/05/12 18:12:30
From this layer "unconsumed" is a bit ambiguous.
M
mattm
2016/05/13 02:17:36
Done.
| |
175 | |
176 DISALLOW_COPY_AND_ASSIGN(ParsedCertificate); | |
177 }; | |
178 | |
179 } // namespace net | |
180 | |
181 #endif // NET_CERT_INTERNAL_PARSED_CERTIFICATE_H_ | |
OLD | NEW |