OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_CERT_INTERNAL_NAME_CONSTRAINTS_H_ |
| 6 #define NET_CERT_INTERNAL_NAME_CONSTRAINTS_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "net/base/ip_address_number.h" |
| 13 |
| 14 namespace net { |
| 15 |
| 16 namespace der { |
| 17 class Input; |
| 18 } // namespace der |
| 19 |
| 20 // Bitfield values for the GeneralName types defined in RFC 5280. The ordering |
| 21 // and exact values are not important, but match the order from the RFC for |
| 22 // convenience. |
| 23 enum GeneralNameTypes { |
| 24 GENERAL_NAME_NONE = 0, |
| 25 GENERAL_NAME_OTHER_NAME = 1 << 0, |
| 26 GENERAL_NAME_RFC822_NAME = 1 << 1, |
| 27 GENERAL_NAME_DNS_NAME = 1 << 2, |
| 28 GENERAL_NAME_X400_ADDRESS = 1 << 3, |
| 29 GENERAL_NAME_DIRECTORY_NAME = 1 << 4, |
| 30 GENERAL_NAME_EDI_PARTY_NAME = 1 << 5, |
| 31 GENERAL_NAME_UNIFORM_RESOURCE_IDENTIFIER = 1 << 6, |
| 32 GENERAL_NAME_IP_ADDRESS = 1 << 7, |
| 33 GENERAL_NAME_REGISTERED_ID = 1 << 8, |
| 34 }; |
| 35 |
| 36 // Parses a NameConstraints extension value and allows testing whether names are |
| 37 // allowed under those constraints as defined by RFC 5280 section 4.2.1.10. |
| 38 class NET_EXPORT NameConstraints { |
| 39 public: |
| 40 // Represents a GeneralNames structure. When processing GeneralNames, it is |
| 41 // often necessary to know which types of names were present, and to check |
| 42 // all the names of a certain type. Therefore, a bitfield of all the name |
| 43 // types is kept, and the names are split into members for each type. Only |
| 44 // name types that are handled by this code are stored (though all types are |
| 45 // recorded in the bitfield.) |
| 46 // TODO(mattm): This may need to be split out into a public class, since |
| 47 // GeneralNames is used other places in a certificate also... |
| 48 struct GeneralNames { |
| 49 GeneralNames(); |
| 50 ~GeneralNames(); |
| 51 |
| 52 // ASCII hostnames. |
| 53 std::vector<std::string> dns_names; |
| 54 |
| 55 // DER-encoded Name values (not including the Sequence tag). |
| 56 std::vector<std::vector<uint8_t>> directory_names; |
| 57 |
| 58 // iPAddresses as sequences of octets in network byte order. This will be |
| 59 // populated if the GeneralNames represents a Subject Alternative Name. |
| 60 std::vector<std::vector<uint8_t>> ip_addresses; |
| 61 |
| 62 // iPAddress ranges, as <IP, prefix length> pairs. This will be populated |
| 63 // if the GeneralNames represents a Name Constraints. |
| 64 std::vector<std::pair<std::vector<uint8_t>, unsigned>> ip_address_ranges; |
| 65 |
| 66 // Which name types were present, as a bitfield of GeneralNameTypes. |
| 67 // Includes both the supported and unsupported types (although unsupported |
| 68 // ones may not be recorded depending on the context, like non-critical name |
| 69 // constraints.) |
| 70 int present_name_types = GENERAL_NAME_NONE; |
| 71 }; |
| 72 |
| 73 ~NameConstraints(); |
| 74 |
| 75 // Parses a DER-encoded NameConstraints extension and initializes this object. |
| 76 // |extension_value| should be the extnValue from the extension (not including |
| 77 // the OCTET STRING tag). |is_critical| should be true if the extension was |
| 78 // marked critical. Returns nullptr if parsing the the extension failed. |
| 79 // The object lifetime is not bound to the lifetime of |extension_value| data. |
| 80 static scoped_ptr<NameConstraints> CreateFromDer( |
| 81 const der::Input& extension_value, |
| 82 bool is_critical); |
| 83 |
| 84 // Tests if a certificate is allowed by the name constraints. |
| 85 // |subject_rdn_sequence| should be the DER-encoded value of the subject's |
| 86 // RDNSequence (not including Sequence tag), and may be an empty ASN.1 |
| 87 // sequence. |subject_alt_name_extnvalue_tlv| should be the extnValue of the |
| 88 // subjectAltName extension (including the OCTET STRING tag & length), or |
| 89 // empty if the cert did not have a subjectAltName extension. |
| 90 // Note that this method does not check hostname or IP address in commonName, |
| 91 // which is deprecated (crbug.com/308330). |
| 92 bool IsPermittedCert(const der::Input& subject_rdn_sequence, |
| 93 const der::Input& subject_alt_name_extnvalue_tlv) const; |
| 94 |
| 95 // Returns true if the ASCII hostname |name| is permitted. |
| 96 // |name| may be a wildcard hostname (starts with "*."). Eg, "*.bar.com" |
| 97 // would not be permitted if "bar.com" is permitted and "foo.bar.com" is |
| 98 // excluded, while "*.baz.com" would only be permitted if "baz.com" is |
| 99 // permitted. |
| 100 bool IsPermittedDNSName(const std::string& name) const; |
| 101 |
| 102 // Returns true if the directoryName |name_rdn_sequence| is permitted. |
| 103 // |name_rdn_sequence| should be the DER-encoded RDNSequence value (not |
| 104 // including the Sequence tag.) |
| 105 bool IsPermittedDirectoryName(const der::Input& name_rdn_sequence) const; |
| 106 |
| 107 // Returns true if the iPAddress |ip| is permitted. |
| 108 bool IsPermittedIP(const IPAddressNumber& ip) const; |
| 109 |
| 110 // Returns a bitfield of GeneralNameTypes of all the types constrained by this |
| 111 // NameConstraints. Name types that aren't supported will only be present if |
| 112 // the name constraint they appeared in was marked critical. |
| 113 // |
| 114 // RFC 5280 section 4.2.1.10 says: |
| 115 // Applications conforming to this profile MUST be able to process name |
| 116 // constraints that are imposed on the directoryName name form and SHOULD be |
| 117 // able to process name constraints that are imposed on the rfc822Name, |
| 118 // uniformResourceIdentifier, dNSName, and iPAddress name forms. |
| 119 // If a name constraints extension that is marked as critical |
| 120 // imposes constraints on a particular name form, and an instance of |
| 121 // that name form appears in the subject field or subjectAltName |
| 122 // extension of a subsequent certificate, then the application MUST |
| 123 // either process the constraint or reject the certificate. |
| 124 int ConstrainedNameTypes() const; |
| 125 |
| 126 private: |
| 127 bool Parse(const der::Input& extension_value, |
| 128 bool is_critical) WARN_UNUSED_RESULT; |
| 129 |
| 130 GeneralNames permitted_subtrees_; |
| 131 GeneralNames excluded_subtrees_; |
| 132 }; |
| 133 |
| 134 } // namespace net |
| 135 |
| 136 #endif // NET_CERT_INTERNAL_NAME_CONSTRAINTS_H_ |
OLD | NEW |