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 <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
| 16 #include "net/cert/internal/parse_certificate.h" |
| 17 #include "net/der/input.h" |
16 | 18 |
17 namespace net { | 19 namespace net { |
18 | 20 |
19 namespace der { | 21 namespace der { |
20 class Input; | |
21 struct GeneralizedTime; | 22 struct GeneralizedTime; |
22 } | 23 } |
23 | 24 |
24 class SignaturePolicy; | 25 class SignaturePolicy; |
25 | 26 |
26 struct NET_EXPORT TrustAnchor { | 27 // Represents a trust anchor (i.e. a trusted root certificate). |
| 28 class NET_EXPORT TrustAnchor { |
| 29 public: |
| 30 // The certificate data for this trust anchor may either be owned internally |
| 31 // (INTERNAL_COPY) or owned externally (EXTERNAL_REFERENCE). When it is |
| 32 // owned internally the data is held by |cert_data_| |
| 33 enum class DataSource { |
| 34 INTERNAL_COPY, |
| 35 EXTERNAL_REFERENCE, |
| 36 }; |
| 37 |
| 38 TrustAnchor(); |
27 ~TrustAnchor(); | 39 ~TrustAnchor(); |
28 | 40 |
29 // DER-encoded SubjectPublicKeyInfo for the trusted key. | 41 // Creates a TrustAnchor given a DER-encoded certificate. Returns nullptr on |
30 std::string spki; | 42 // failure. Failure will occur if the certificate data cannot be parsed to |
| 43 // find a subject. |
| 44 // |
| 45 // The provided certificate data is either copied, or aliased, depending on |
| 46 // the value of |source|. See the comments for DataSource for details. |
| 47 static std::unique_ptr<TrustAnchor> CreateFromCertificateData( |
| 48 const uint8_t* data, |
| 49 size_t length, |
| 50 DataSource source); |
31 | 51 |
32 // DER-encoded "Name" corresponding to the key. | 52 // Returns true if the trust anchor matches |name|. In other words, returns |
33 std::string name; | 53 // true if the certificate's subject matches |name|. |
| 54 bool MatchesName(const der::Input& name) const; |
| 55 |
| 56 // Returns the DER-encoded certificate data for this trust anchor. |
| 57 const der::Input& cert() const { return cert_; } |
| 58 |
| 59 private: |
| 60 // The backing store for the certificate data. This is only applicable when |
| 61 // the trust anchor was initialized using DataSource::INTERNAL_COPY. |
| 62 std::vector<uint8_t> cert_data_; |
| 63 |
| 64 // 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 |
| 66 // created). |
| 67 |
| 68 // Points to the raw certificate DER. |
| 69 der::Input cert_; |
| 70 |
| 71 // Points to the subject TLV for the certificate. |
| 72 der::Input name_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(TrustAnchor); |
34 }; | 75 }; |
35 | 76 |
36 // A very simple implementation of a TrustStore, which contains mappings from | 77 // A very simple implementation of a TrustStore, which contains a set of |
37 // names to trusted public keys. | 78 // trusted certificates. |
38 struct NET_EXPORT TrustStore { | 79 class NET_EXPORT TrustStore { |
| 80 public: |
39 TrustStore(); | 81 TrustStore(); |
40 TrustStore(const TrustStore& other); | |
41 ~TrustStore(); | 82 ~TrustStore(); |
42 | 83 |
43 std::vector<TrustAnchor> anchors; | 84 // Empties the trust store, resetting it to original state. |
| 85 void Clear(); |
| 86 |
| 87 // Adds a trusted certificate to the store. The trust store makes a copy of |
| 88 // the provided certificate data. |
| 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 |
| 93 // This function is the same as AddTrustedCertificate() except the underlying |
| 94 // data is not copied. The caller is responsible for ensuring that the data |
| 95 // pointer remains alive and is not mutated for the lifetime of the |
| 96 // TrustStore. |
| 97 bool AddTrustedCertificateWithoutCopying(const uint8_t* data, |
| 98 size_t length) WARN_UNUSED_RESULT; |
| 99 |
| 100 // Returns the trust anchor that matches |name|, or nullptr if there is none. |
| 101 // TODO(eroman): There may be multiple matches. |
| 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 |
| 109 private: |
| 110 bool AddTrustedCertificate(const uint8_t* data, |
| 111 size_t length, |
| 112 TrustAnchor::DataSource source) WARN_UNUSED_RESULT; |
| 113 |
| 114 std::vector<std::unique_ptr<TrustAnchor>> anchors_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(TrustStore); |
44 }; | 117 }; |
45 | 118 |
46 // VerifyCertificateChain() verifies a certificate path (chain) based on the | 119 // VerifyCertificateChain() verifies a certificate path (chain) based on the |
47 // rules in RFC 5280. | 120 // rules in RFC 5280. |
48 // | 121 // |
49 // WARNING: This implementation is in progress, and is currently | 122 // WARNING: This implementation is in progress, and is currently |
50 // incomplete. DO NOT USE IT unless its limitations are acceptable for your use. | 123 // incomplete. DO NOT USE IT unless its limitations are acceptable for your use. |
51 // | 124 // |
52 // --------- | 125 // --------- |
53 // Inputs | 126 // Inputs |
(...skipping 24 matching lines...) Expand all Loading... |
78 // Returns true if the target certificate can be verified. | 151 // Returns true if the target certificate can be verified. |
79 NET_EXPORT bool VerifyCertificateChain(const std::vector<der::Input>& certs_der, | 152 NET_EXPORT bool VerifyCertificateChain(const std::vector<der::Input>& certs_der, |
80 const TrustStore& trust_store, | 153 const TrustStore& trust_store, |
81 const SignaturePolicy* signature_policy, | 154 const SignaturePolicy* signature_policy, |
82 const der::GeneralizedTime& time) | 155 const der::GeneralizedTime& time) |
83 WARN_UNUSED_RESULT; | 156 WARN_UNUSED_RESULT; |
84 | 157 |
85 } // namespace net | 158 } // namespace net |
86 | 159 |
87 #endif // NET_CERT_INTERNAL_VERIFY_CERTIFICATE_CHAIN_H_ | 160 #endif // NET_CERT_INTERNAL_VERIFY_CERTIFICATE_CHAIN_H_ |
OLD | NEW |