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" | |
15 #include "net/der/input.h" | |
16 | |
17 namespace net { | |
18 | |
19 struct GeneralNames; | |
20 class NameConstraints; | |
21 class SignatureAlgorithm; | |
22 | |
23 // Represents an X.509 certificate, including Certificate, TBSCertificate, and | |
24 // standard extensions. | |
25 // Creating a ParsedCertificate does not completely parse and validate the | |
26 // certificate data. Presence of a member in this class implies the DER was | |
27 // parsed successfully to that level, but does not imply the contents of that | |
28 // member are valid, unless otherwise specified. See the documentation for each | |
29 // member or the documentation of the type it returns. | |
30 class NET_EXPORT ParsedCertificate | |
31 : public base::RefCountedThreadSafe<ParsedCertificate> { | |
32 public: | |
33 // Map from OID to ParsedExtension. | |
34 using ExtensionsMap = std::map<der::Input, ParsedExtension>; | |
35 | |
36 // The certificate data for may either be owned internally (INTERNAL_COPY) or | |
37 // owned externally (EXTERNAL_REFERENCE). When it is owned internally the data | |
38 // is held by |cert_data_| | |
39 enum class DataSource { | |
40 INTERNAL_COPY, | |
41 EXTERNAL_REFERENCE, | |
42 }; | |
43 | |
44 // Creates a ParsedCertificate given a DER-encoded Certificate. Returns | |
45 // nullptr on failure. Failure will occur if the standard certificate fields | |
46 // and supported extensions cannot be parsed. | |
47 // | |
48 // The provided certificate data is either copied, or aliased, depending on | |
49 // the value of |source|. See the comments for DataSource for details. | |
50 static scoped_refptr<ParsedCertificate> CreateFromCertificateData( | |
51 const uint8_t* data, | |
52 size_t length, | |
53 DataSource source); | |
54 // Creates a ParsedCertificate and appends it to |chain|. Returns true if the | |
eroman
2016/05/31 18:28:57
nit: add spaces between the method declarations (t
mattm
2016/05/31 19:48:10
Done.
| |
55 // certificate was successfully parsed and added. If false is return, |chain| | |
56 // is unmodified. | |
57 static bool CreateAndAddToVector( | |
58 const uint8_t* data, | |
59 size_t length, | |
60 DataSource source, | |
61 std::vector<scoped_refptr<net::ParsedCertificate>>* chain); | |
62 // Creates a ParsedCertificate, copying the data from |data|. | |
eroman
2016/05/31 18:28:57
same
mattm
2016/05/31 19:48:10
Done.
| |
63 static scoped_refptr<ParsedCertificate> CreateFromCertificateCopy( | |
64 const base::StringPiece& data); | |
65 | |
66 // Returns the DER-encoded certificate data for this cert. | |
67 const der::Input& der_cert() const { return cert_; } | |
68 | |
69 // Accessors for raw fields of the Certificate. | |
70 const der::Input& tbs_certificate_tlv() const { return tbs_certificate_tlv_; } | |
71 const der::Input& signature_algorithm_tlv() const { | |
72 return signature_algorithm_tlv_; | |
73 } | |
74 const der::BitString& signature_value() const { return signature_value_; } | |
75 // Accessor for struct containing raw fields of the TbsCertificate. | |
76 const ParsedTbsCertificate& tbs() const { return tbs_; } | |
77 | |
78 // Returns true if the signatureAlgorithm of the Certificate is supported and | |
79 // valid. | |
80 bool has_valid_supported_signature_algorithm() const { | |
81 return signature_algorithm_ != nullptr; | |
82 } | |
83 // Returns the signatureAlgorithm of the Certificate (not the tbsCertificate). | |
84 // Must not be called if has_valid_supported_signature_algorithm() is false. | |
85 const SignatureAlgorithm& signature_algorithm() const { | |
86 DCHECK(signature_algorithm_); | |
87 return *signature_algorithm_; | |
88 } | |
89 | |
90 // Returns the DER-encoded normalized subject value (not including outer | |
91 // Sequence tag). This is gauranteed to be valid DER, though the contents of | |
92 // unhandled string types are treated as raw bytes. | |
93 const der::Input normalized_subject() const { | |
eroman
2016/05/31 18:28:57
optional nit: remove the "const" from return type
mattm
2016/05/31 19:48:10
Done.
| |
94 return der::Input(&normalized_subject_); | |
95 } | |
96 // Returns the DER-encoded normalized issuer value (not including outer | |
97 // Sequence tag). This is gauranteed to be valid DER, though the contents of | |
98 // unhandled string types are treated as raw bytes. | |
99 const der::Input normalized_issuer() const { | |
100 return der::Input(&normalized_issuer_); | |
101 } | |
102 | |
103 // Returns true if the certificate has a BasicConstraints extension. | |
104 bool has_basic_constraints() const { return has_basic_constraints_; } | |
105 // Returns the ParsedBasicConstraints struct. Caller must check | |
106 // has_basic_constraints() before accessing this. | |
107 const ParsedBasicConstraints& basic_constraints() const { | |
108 DCHECK(has_basic_constraints_); | |
109 return basic_constraints_; | |
110 } | |
111 | |
112 // Returns true if the certificate has a KeyUsage extension. | |
113 bool has_key_usage() const { return has_key_usage_; } | |
114 // Returns the KeyUsage BitString. Caller must check | |
115 // has_key_usage() before accessing this. | |
116 const der::BitString& key_usage() const { | |
117 DCHECK(has_key_usage_); | |
118 return key_usage_; | |
119 } | |
120 | |
121 // Returns true if the certificate has a SubjectAltName extension. | |
122 bool has_subject_alt_names() const { return subject_alt_names_ != nullptr; } | |
123 // Returns the ParsedExtension struct for the SubjectAltName extension. | |
124 // If the cert did not have a SubjectAltName extension, this will be a | |
125 // default-initialized ParsedExtension struct. | |
126 const ParsedExtension& subject_alt_names_extension() const { | |
127 return subject_alt_names_extension_; | |
128 } | |
129 // Returns the GeneralNames class parsed from SubjectAltName extension, or | |
130 // nullptr if no SubjectAltName extension was present. | |
131 const GeneralNames* subject_alt_names() const { | |
132 return subject_alt_names_.get(); | |
133 } | |
134 | |
135 // Returns true if the certificate has a NameConstraints extension. | |
136 bool has_name_constraints() const { return name_constraints_ != nullptr; } | |
137 // Returns the parsed NameConstraints extension. Must not be called if | |
138 // has_name_constraints() is false. | |
139 const NameConstraints& name_constraints() const { | |
140 DCHECK(name_constraints_); | |
141 return *name_constraints_; | |
142 } | |
143 | |
144 // Returns a map of unhandled extensions (excludes the ones above). | |
145 const ExtensionsMap& unparsed_extensions() const { | |
146 return unparsed_extensions_; | |
147 } | |
148 | |
149 private: | |
150 friend class base::RefCountedThreadSafe<ParsedCertificate>; | |
151 ParsedCertificate(); | |
152 ~ParsedCertificate(); | |
153 | |
154 // The backing store for the certificate data. This is only applicable when | |
155 // the ParsedCertificate was initialized using DataSource::INTERNAL_COPY. | |
156 std::vector<uint8_t> cert_data_; | |
157 | |
158 // Note that the backing data for |cert_| (and its may come either from | |
159 // |cert_data_| or some external buffer (depending on how the | |
160 // ParsedCertificate was created). | |
161 | |
162 // Points to the raw certificate DER. | |
163 der::Input cert_; | |
164 | |
165 der::Input tbs_certificate_tlv_; | |
166 der::Input signature_algorithm_tlv_; | |
167 der::BitString signature_value_; | |
168 ParsedTbsCertificate tbs_; | |
169 | |
170 // The signatureAlgorithm from the Certificate. | |
171 std::unique_ptr<SignatureAlgorithm> signature_algorithm_; | |
172 | |
173 // Normalized DER-encoded Subject (not including outer Sequence tag). | |
174 std::string normalized_subject_; | |
175 // Normalized DER-encoded Issuer (not including outer Sequence tag). | |
176 std::string normalized_issuer_; | |
177 | |
178 // BasicConstraints extension. | |
179 bool has_basic_constraints_ = false; | |
180 ParsedBasicConstraints basic_constraints_; | |
181 | |
182 // KeyUsage extension. | |
183 bool has_key_usage_ = false; | |
184 der::BitString key_usage_; | |
185 | |
186 // Raw SubjectAltName extension. | |
187 ParsedExtension subject_alt_names_extension_; | |
188 // Parsed SubjectAltName extension. | |
189 std::unique_ptr<GeneralNames> subject_alt_names_; | |
190 | |
191 // NameConstraints extension. | |
192 bool has_name_constraints_ = false; | |
193 std::unique_ptr<NameConstraints> name_constraints_; | |
194 | |
195 // The remaining extensions (excludes the standard ones above). | |
196 ExtensionsMap unparsed_extensions_; | |
197 | |
198 DISALLOW_COPY_AND_ASSIGN(ParsedCertificate); | |
199 }; | |
200 | |
201 } // namespace net | |
202 | |
203 #endif // NET_CERT_INTERNAL_PARSED_CERTIFICATE_H_ | |
OLD | NEW |