| 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> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <string> | |
| 12 #include <vector> | 8 #include <vector> |
| 13 | 9 |
| 14 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/ref_counted.h" |
| 15 #include "net/base/net_export.h" | 12 #include "net/base/net_export.h" |
| 16 #include "net/cert/internal/parse_certificate.h" | |
| 17 #include "net/der/input.h" | 13 #include "net/der/input.h" |
| 18 | 14 |
| 19 namespace net { | 15 namespace net { |
| 20 | 16 |
| 21 namespace der { | 17 namespace der { |
| 22 struct GeneralizedTime; | 18 struct GeneralizedTime; |
| 23 } | 19 } |
| 24 | 20 |
| 21 class ParsedCertificate; |
| 25 class SignaturePolicy; | 22 class SignaturePolicy; |
| 26 | 23 class TrustStore; |
| 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(); | |
| 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 | |
| 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); | |
| 51 | |
| 52 // Returns true if the trust anchor matches |name|. In other words, returns | |
| 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); | |
| 75 }; | |
| 76 | |
| 77 // A very simple implementation of a TrustStore, which contains a set of | |
| 78 // trusted certificates. | |
| 79 class NET_EXPORT TrustStore { | |
| 80 public: | |
| 81 TrustStore(); | |
| 82 ~TrustStore(); | |
| 83 | |
| 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); | |
| 117 }; | |
| 118 | 24 |
| 119 // VerifyCertificateChain() verifies a certificate path (chain) based on the | 25 // VerifyCertificateChain() verifies a certificate path (chain) based on the |
| 120 // rules in RFC 5280. | 26 // rules in RFC 5280. |
| 121 // | 27 // |
| 122 // WARNING: This implementation is in progress, and is currently incomplete. | 28 // WARNING: This implementation is in progress, and is currently incomplete. |
| 123 // Consult an OWNER before using it. | 29 // Consult an OWNER before using it. |
| 124 // | 30 // |
| 125 // --------- | 31 // --------- |
| 126 // Inputs | 32 // Inputs |
| 127 // --------- | 33 // --------- |
| (...skipping 15 matching lines...) Expand all Loading... |
| 143 // allowed, what length keys, what named curves, etc). | 49 // allowed, what length keys, what named curves, etc). |
| 144 // | 50 // |
| 145 // time: | 51 // time: |
| 146 // The UTC time to use for expiration checks. | 52 // The UTC time to use for expiration checks. |
| 147 // | 53 // |
| 148 // --------- | 54 // --------- |
| 149 // Outputs | 55 // Outputs |
| 150 // --------- | 56 // --------- |
| 151 // | 57 // |
| 152 // Returns true if the target certificate can be verified. | 58 // Returns true if the target certificate can be verified. |
| 153 NET_EXPORT bool VerifyCertificateChain(const std::vector<der::Input>& certs_der, | 59 NET_EXPORT bool VerifyCertificateChain( |
| 154 const TrustStore& trust_store, | 60 const std::vector<scoped_refptr<ParsedCertificate>>& cert_chain, |
| 155 const SignaturePolicy* signature_policy, | 61 const TrustStore& trust_store, |
| 156 const der::GeneralizedTime& time) | 62 const SignaturePolicy* signature_policy, |
| 157 WARN_UNUSED_RESULT; | 63 const der::GeneralizedTime& time) WARN_UNUSED_RESULT; |
| 158 | 64 |
| 159 } // namespace net | 65 } // namespace net |
| 160 | 66 |
| 161 #endif // NET_CERT_INTERNAL_VERIFY_CERTIFICATE_CHAIN_H_ | 67 #endif // NET_CERT_INTERNAL_VERIFY_CERTIFICATE_CHAIN_H_ |
| OLD | NEW |