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 |
| 55 // Creates a ParsedCertificate and appends it to |chain|. Returns true if the |
| 56 // certificate was successfully parsed and added. If false is return, |chain| |
| 57 // is unmodified. |
| 58 static bool CreateAndAddToVector( |
| 59 const uint8_t* data, |
| 60 size_t length, |
| 61 DataSource source, |
| 62 std::vector<scoped_refptr<net::ParsedCertificate>>* chain); |
| 63 |
| 64 // Creates a ParsedCertificate, copying the data from |data|. |
| 65 static scoped_refptr<ParsedCertificate> CreateFromCertificateCopy( |
| 66 const base::StringPiece& data); |
| 67 |
| 68 // Returns the DER-encoded certificate data for this cert. |
| 69 const der::Input& der_cert() const { return cert_; } |
| 70 |
| 71 // Accessors for raw fields of the Certificate. |
| 72 const der::Input& tbs_certificate_tlv() const { return tbs_certificate_tlv_; } |
| 73 |
| 74 const der::Input& signature_algorithm_tlv() const { |
| 75 return signature_algorithm_tlv_; |
| 76 } |
| 77 |
| 78 const der::BitString& signature_value() const { return signature_value_; } |
| 79 |
| 80 // Accessor for struct containing raw fields of the TbsCertificate. |
| 81 const ParsedTbsCertificate& tbs() const { return tbs_; } |
| 82 |
| 83 // Returns true if the signatureAlgorithm of the Certificate is supported and |
| 84 // valid. |
| 85 bool has_valid_supported_signature_algorithm() const { |
| 86 return signature_algorithm_ != nullptr; |
| 87 } |
| 88 |
| 89 // Returns the signatureAlgorithm of the Certificate (not the tbsCertificate). |
| 90 // Must not be called if has_valid_supported_signature_algorithm() is false. |
| 91 const SignatureAlgorithm& signature_algorithm() const { |
| 92 DCHECK(signature_algorithm_); |
| 93 return *signature_algorithm_; |
| 94 } |
| 95 |
| 96 // Returns the DER-encoded normalized subject 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 der::Input normalized_subject() const { |
| 100 return der::Input(&normalized_subject_); |
| 101 } |
| 102 // Returns the DER-encoded normalized issuer value (not including outer |
| 103 // Sequence tag). This is gauranteed to be valid DER, though the contents of |
| 104 // unhandled string types are treated as raw bytes. |
| 105 der::Input normalized_issuer() const { |
| 106 return der::Input(&normalized_issuer_); |
| 107 } |
| 108 |
| 109 // Returns true if the certificate has a BasicConstraints extension. |
| 110 bool has_basic_constraints() const { return has_basic_constraints_; } |
| 111 |
| 112 // Returns the ParsedBasicConstraints struct. Caller must check |
| 113 // has_basic_constraints() before accessing this. |
| 114 const ParsedBasicConstraints& basic_constraints() const { |
| 115 DCHECK(has_basic_constraints_); |
| 116 return basic_constraints_; |
| 117 } |
| 118 |
| 119 // Returns true if the certificate has a KeyUsage extension. |
| 120 bool has_key_usage() const { return has_key_usage_; } |
| 121 |
| 122 // Returns the KeyUsage BitString. Caller must check |
| 123 // has_key_usage() before accessing this. |
| 124 const der::BitString& key_usage() const { |
| 125 DCHECK(has_key_usage_); |
| 126 return key_usage_; |
| 127 } |
| 128 |
| 129 // Returns true if the certificate has a SubjectAltName extension. |
| 130 bool has_subject_alt_names() const { return subject_alt_names_ != nullptr; } |
| 131 |
| 132 // Returns the ParsedExtension struct for the SubjectAltName extension. |
| 133 // If the cert did not have a SubjectAltName extension, this will be a |
| 134 // default-initialized ParsedExtension struct. |
| 135 const ParsedExtension& subject_alt_names_extension() const { |
| 136 return subject_alt_names_extension_; |
| 137 } |
| 138 |
| 139 // Returns the GeneralNames class parsed from SubjectAltName extension, or |
| 140 // nullptr if no SubjectAltName extension was present. |
| 141 const GeneralNames* subject_alt_names() const { |
| 142 return subject_alt_names_.get(); |
| 143 } |
| 144 |
| 145 // Returns true if the certificate has a NameConstraints extension. |
| 146 bool has_name_constraints() const { return name_constraints_ != nullptr; } |
| 147 |
| 148 // Returns the parsed NameConstraints extension. Must not be called if |
| 149 // has_name_constraints() is false. |
| 150 const NameConstraints& name_constraints() const { |
| 151 DCHECK(name_constraints_); |
| 152 return *name_constraints_; |
| 153 } |
| 154 |
| 155 // Returns a map of unhandled extensions (excludes the ones above). |
| 156 const ExtensionsMap& unparsed_extensions() const { |
| 157 return unparsed_extensions_; |
| 158 } |
| 159 |
| 160 private: |
| 161 friend class base::RefCountedThreadSafe<ParsedCertificate>; |
| 162 ParsedCertificate(); |
| 163 ~ParsedCertificate(); |
| 164 |
| 165 // The backing store for the certificate data. This is only applicable when |
| 166 // the ParsedCertificate was initialized using DataSource::INTERNAL_COPY. |
| 167 std::vector<uint8_t> cert_data_; |
| 168 |
| 169 // Note that the backing data for |cert_| (and its may come either from |
| 170 // |cert_data_| or some external buffer (depending on how the |
| 171 // ParsedCertificate was created). |
| 172 |
| 173 // Points to the raw certificate DER. |
| 174 der::Input cert_; |
| 175 |
| 176 der::Input tbs_certificate_tlv_; |
| 177 der::Input signature_algorithm_tlv_; |
| 178 der::BitString signature_value_; |
| 179 ParsedTbsCertificate tbs_; |
| 180 |
| 181 // The signatureAlgorithm from the Certificate. |
| 182 std::unique_ptr<SignatureAlgorithm> signature_algorithm_; |
| 183 |
| 184 // Normalized DER-encoded Subject (not including outer Sequence tag). |
| 185 std::string normalized_subject_; |
| 186 // Normalized DER-encoded Issuer (not including outer Sequence tag). |
| 187 std::string normalized_issuer_; |
| 188 |
| 189 // BasicConstraints extension. |
| 190 bool has_basic_constraints_ = false; |
| 191 ParsedBasicConstraints basic_constraints_; |
| 192 |
| 193 // KeyUsage extension. |
| 194 bool has_key_usage_ = false; |
| 195 der::BitString key_usage_; |
| 196 |
| 197 // Raw SubjectAltName extension. |
| 198 ParsedExtension subject_alt_names_extension_; |
| 199 // Parsed SubjectAltName extension. |
| 200 std::unique_ptr<GeneralNames> subject_alt_names_; |
| 201 |
| 202 // NameConstraints extension. |
| 203 std::unique_ptr<NameConstraints> name_constraints_; |
| 204 |
| 205 // The remaining extensions (excludes the standard ones above). |
| 206 ExtensionsMap unparsed_extensions_; |
| 207 |
| 208 DISALLOW_COPY_AND_ASSIGN(ParsedCertificate); |
| 209 }; |
| 210 |
| 211 } // namespace net |
| 212 |
| 213 #endif // NET_CERT_INTERNAL_PARSED_CERTIFICATE_H_ |
OLD | NEW |