OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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_PARSED_CERTIFICATE_H_ | 5 #ifndef NET_CERT_INTERNAL_PARSED_CERTIFICATE_H_ |
6 #define NET_CERT_INTERNAL_PARSED_CERTIFICATE_H_ | 6 #define NET_CERT_INTERNAL_PARSED_CERTIFICATE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "net/base/net_export.h" | 13 #include "net/base/net_export.h" |
14 #include "net/cert/internal/parse_certificate.h" | 14 #include "net/cert/internal/parse_certificate.h" |
15 #include "net/der/input.h" | 15 #include "net/der/input.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 struct GeneralNames; | 19 struct GeneralNames; |
20 class NameConstraints; | 20 class NameConstraints; |
21 class ParsedCertificate; | 21 class ParsedCertificate; |
22 class SignatureAlgorithm; | 22 class SignatureAlgorithm; |
| 23 class CertErrors; |
23 | 24 |
24 using ParsedCertificateList = std::vector<scoped_refptr<ParsedCertificate>>; | 25 using ParsedCertificateList = std::vector<scoped_refptr<ParsedCertificate>>; |
25 | 26 |
26 // Represents an X.509 certificate, including Certificate, TBSCertificate, and | 27 // Represents an X.509 certificate, including Certificate, TBSCertificate, and |
27 // standard extensions. | 28 // standard extensions. |
28 // Creating a ParsedCertificate does not completely parse and validate the | 29 // Creating a ParsedCertificate does not completely parse and validate the |
29 // certificate data. Presence of a member in this class implies the DER was | 30 // certificate data. Presence of a member in this class implies the DER was |
30 // parsed successfully to that level, but does not imply the contents of that | 31 // parsed successfully to that level, but does not imply the contents of that |
31 // member are valid, unless otherwise specified. See the documentation for each | 32 // member are valid, unless otherwise specified. See the documentation for each |
32 // member or the documentation of the type it returns. | 33 // member or the documentation of the type it returns. |
33 class NET_EXPORT ParsedCertificate | 34 class NET_EXPORT ParsedCertificate |
34 : public base::RefCountedThreadSafe<ParsedCertificate> { | 35 : public base::RefCountedThreadSafe<ParsedCertificate> { |
35 public: | 36 public: |
36 // Map from OID to ParsedExtension. | 37 // Map from OID to ParsedExtension. |
37 using ExtensionsMap = std::map<der::Input, ParsedExtension>; | 38 using ExtensionsMap = std::map<der::Input, ParsedExtension>; |
38 | 39 |
39 // The certificate data for may either be owned internally (INTERNAL_COPY) or | |
40 // owned externally (EXTERNAL_REFERENCE). When it is owned internally the data | |
41 // is held by |cert_data_| | |
42 enum class DataSource { | |
43 INTERNAL_COPY, | |
44 EXTERNAL_REFERENCE, | |
45 }; | |
46 | |
47 // Creates a ParsedCertificate given a DER-encoded Certificate. Returns | 40 // Creates a ParsedCertificate given a DER-encoded Certificate. Returns |
48 // nullptr on failure. Failure will occur if the standard certificate fields | 41 // nullptr on failure. Failure will occur if the standard certificate fields |
49 // and supported extensions cannot be parsed. | 42 // and supported extensions cannot be parsed. |
50 // | 43 // |
51 // The provided certificate data is either copied, or aliased, depending on | 44 // The provided certificate data is copied, so |data| needn't remain valid |
52 // the value of |source|. See the comments for DataSource for details. | 45 // after this call. |
53 static scoped_refptr<ParsedCertificate> CreateFromCertificateData( | 46 // |
| 47 // On either success or failure, if |errors| is non-null it may have error |
| 48 // information added to it. |
| 49 static scoped_refptr<ParsedCertificate> Create( |
54 const uint8_t* data, | 50 const uint8_t* data, |
55 size_t length, | 51 size_t length, |
56 DataSource source, | 52 const ParseCertificateOptions& options, |
57 const ParseCertificateOptions& options); | 53 CertErrors* errors); |
58 | 54 |
59 // Creates a ParsedCertificate and appends it to |chain|. Returns true if the | 55 // Overload that takes a StringPiece. |
60 // certificate was successfully parsed and added. If false is return, |chain| | 56 static scoped_refptr<ParsedCertificate> Create( |
61 // is unmodified. | 57 const base::StringPiece& data, |
| 58 const ParseCertificateOptions& options, |
| 59 CertErrors* errors); |
| 60 |
| 61 // Creates a ParsedCertificate by copying the provided |data|, and appends it |
| 62 // to |chain|. Returns true if the certificate was successfully parsed and |
| 63 // added. If false is return, |chain| is unmodified. |
| 64 // |
| 65 // On either success or failure, if |errors| is non-null it may have error |
| 66 // information added to it. |
62 static bool CreateAndAddToVector( | 67 static bool CreateAndAddToVector( |
63 const uint8_t* data, | 68 const uint8_t* data, |
64 size_t length, | 69 size_t length, |
65 DataSource source, | |
66 const ParseCertificateOptions& options, | 70 const ParseCertificateOptions& options, |
67 std::vector<scoped_refptr<net::ParsedCertificate>>* chain); | 71 std::vector<scoped_refptr<net::ParsedCertificate>>* chain, |
| 72 CertErrors* errors); |
68 | 73 |
69 // Creates a ParsedCertificate, copying the data from |data|. | 74 // Overload that takes a StringPiece. |
70 static scoped_refptr<ParsedCertificate> CreateFromCertificateCopy( | 75 static bool CreateAndAddToVector( |
71 const base::StringPiece& data, | 76 const base::StringPiece& data, |
72 const ParseCertificateOptions& options); | 77 const ParseCertificateOptions& options, |
| 78 std::vector<scoped_refptr<net::ParsedCertificate>>* chain, |
| 79 CertErrors* errors); |
| 80 |
| 81 // Like Create() this builds a ParsedCertificate given a DER-encoded |
| 82 // Certificate and returns nullptr on failure. |
| 83 // |
| 84 // However a copy of |data| is NOT made. |
| 85 // |
| 86 // This is a dangerous way to create as ParsedCertificate and should only be |
| 87 // used with care when saving a copy is really worth it, or the data is known |
| 88 // to come from static storage (and hence remain valid for entire life of |
| 89 // process). |
| 90 // |
| 91 // ParsedCertificate is reference counted, so it is easy to extend the life |
| 92 // and and end up with a ParsedCertificate referencing feed memory. |
| 93 // |
| 94 // On either success or failure, if |errors| is non-null it may have error |
| 95 // information added to it. |
| 96 static scoped_refptr<ParsedCertificate> CreateWithoutCopyingUnsafe( |
| 97 const uint8_t* data, |
| 98 size_t length, |
| 99 const ParseCertificateOptions& options, |
| 100 CertErrors* errors); |
73 | 101 |
74 // Returns the DER-encoded certificate data for this cert. | 102 // Returns the DER-encoded certificate data for this cert. |
75 const der::Input& der_cert() const { return cert_; } | 103 const der::Input& der_cert() const { return cert_; } |
76 | 104 |
77 // Accessors for raw fields of the Certificate. | 105 // Accessors for raw fields of the Certificate. |
78 const der::Input& tbs_certificate_tlv() const { return tbs_certificate_tlv_; } | 106 const der::Input& tbs_certificate_tlv() const { return tbs_certificate_tlv_; } |
79 | 107 |
80 const der::Input& signature_algorithm_tlv() const { | 108 const der::Input& signature_algorithm_tlv() const { |
81 return signature_algorithm_tlv_; | 109 return signature_algorithm_tlv_; |
82 } | 110 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 | 201 |
174 // Returns any OCSP URIs from the AuthorityInfoAccess extension. | 202 // Returns any OCSP URIs from the AuthorityInfoAccess extension. |
175 const std::vector<base::StringPiece>& ocsp_uris() const { return ocsp_uris_; } | 203 const std::vector<base::StringPiece>& ocsp_uris() const { return ocsp_uris_; } |
176 | 204 |
177 // Returns a map of unhandled extensions (excludes the ones above). | 205 // Returns a map of unhandled extensions (excludes the ones above). |
178 const ExtensionsMap& unparsed_extensions() const { | 206 const ExtensionsMap& unparsed_extensions() const { |
179 return unparsed_extensions_; | 207 return unparsed_extensions_; |
180 } | 208 } |
181 | 209 |
182 private: | 210 private: |
| 211 // The certificate data for may either be owned internally (INTERNAL_COPY) or |
| 212 // owned externally (EXTERNAL_REFERENCE). When it is owned internally the data |
| 213 // is held by |cert_data_| |
| 214 enum class DataSource { |
| 215 INTERNAL_COPY, |
| 216 EXTERNAL_REFERENCE, |
| 217 }; |
| 218 |
183 friend class base::RefCountedThreadSafe<ParsedCertificate>; | 219 friend class base::RefCountedThreadSafe<ParsedCertificate>; |
184 ParsedCertificate(); | 220 ParsedCertificate(); |
185 ~ParsedCertificate(); | 221 ~ParsedCertificate(); |
186 | 222 |
| 223 static scoped_refptr<ParsedCertificate> CreateInternal( |
| 224 const uint8_t* data, |
| 225 size_t length, |
| 226 DataSource source, |
| 227 const ParseCertificateOptions& options, |
| 228 CertErrors* errors); |
| 229 |
187 // The backing store for the certificate data. This is only applicable when | 230 // The backing store for the certificate data. This is only applicable when |
188 // the ParsedCertificate was initialized using DataSource::INTERNAL_COPY. | 231 // the ParsedCertificate was initialized using DataSource::INTERNAL_COPY. |
189 std::vector<uint8_t> cert_data_; | 232 std::vector<uint8_t> cert_data_; |
190 | 233 |
191 // Note that the backing data for |cert_| (and its may come either from | 234 // Note that the backing data for |cert_| (and its may come either from |
192 // |cert_data_| or some external buffer (depending on how the | 235 // |cert_data_| or some external buffer (depending on how the |
193 // ParsedCertificate was created). | 236 // ParsedCertificate was created). |
194 | 237 |
195 // Points to the raw certificate DER. | 238 // Points to the raw certificate DER. |
196 der::Input cert_; | 239 der::Input cert_; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 | 278 |
236 // The remaining extensions (excludes the standard ones above). | 279 // The remaining extensions (excludes the standard ones above). |
237 ExtensionsMap unparsed_extensions_; | 280 ExtensionsMap unparsed_extensions_; |
238 | 281 |
239 DISALLOW_COPY_AND_ASSIGN(ParsedCertificate); | 282 DISALLOW_COPY_AND_ASSIGN(ParsedCertificate); |
240 }; | 283 }; |
241 | 284 |
242 } // namespace net | 285 } // namespace net |
243 | 286 |
244 #endif // NET_CERT_INTERNAL_PARSED_CERTIFICATE_H_ | 287 #endif // NET_CERT_INTERNAL_PARSED_CERTIFICATE_H_ |
OLD | NEW |