| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include "general_names.h" |
| 16 |
| 17 | #include <openssl/base.h> |
| 18 | #include <openssl/bytestring.h> |
| 19 |
| 20 | #include <climits> |
| 21 | #include <cstring> |
| 22 |
| 23 | #include "cert_error_params.h" |
| 24 | #include "cert_errors.h" |
| 25 | #include "input.h" |
| 26 | #include "ip_util.h" |
| 27 | #include "parser.h" |
| 28 | #include "string_util.h" |
| 29 |
| 30 | BSSL_NAMESPACE_BEGIN |
| 31 |
| 32 | DEFINE_CERT_ERROR_ID(kFailedParsingGeneralName, "Failed parsing GeneralName"); |
| 33 |
| 34 | namespace { |
| 35 |
| 36 | DEFINE_CERT_ERROR_ID(kRFC822NameNotAscii, "rfc822Name is not ASCII"); |
| 37 | DEFINE_CERT_ERROR_ID(kDnsNameNotAscii, "dNSName is not ASCII"); |
| 38 | DEFINE_CERT_ERROR_ID(kURINotAscii, "uniformResourceIdentifier is not ASCII"); |
| 39 | DEFINE_CERT_ERROR_ID(kFailedParsingIp, "Failed parsing iPAddress"); |
| 40 | DEFINE_CERT_ERROR_ID(kUnknownGeneralNameType, "Unknown GeneralName type"); |
| 41 | DEFINE_CERT_ERROR_ID(kFailedReadingGeneralNames, |
| 42 | "Failed reading GeneralNames SEQUENCE"); |
| 43 | DEFINE_CERT_ERROR_ID(kGeneralNamesTrailingData, |
| 44 | "GeneralNames contains trailing data after the sequence"); |
| 45 | DEFINE_CERT_ERROR_ID(kGeneralNamesEmpty, |
| 46 | "GeneralNames is a sequence of 0 elements"); |
| 47 | DEFINE_CERT_ERROR_ID(kFailedReadingGeneralName, |
| 48 | "Failed reading GeneralName TLV"); |
| 49 |
| 50 | } // namespace |
| 51 |
| 52 | GeneralNames::GeneralNames() = default; |
| 53 |
| 54 | GeneralNames::~GeneralNames() = default; |
| 55 |
| 56 | // static |
| 57 | std::unique_ptr<GeneralNames> GeneralNames::Create(der::Input general_names_tlv, |
| 58 | CertErrors *errors) { |
| 59 | BSSL_CHECK(errors); |
| 60 |
| 61 | // RFC 5280 section 4.2.1.6: |
| 62 | // GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName |
| 63 | der::Parser parser(general_names_tlv); |
| 64 | der::Input sequence_value; |
| 65 | if (!parser.ReadTag(CBS_ASN1_SEQUENCE, &sequence_value)) { |
| 66 | errors->AddError(kFailedReadingGeneralNames); |
| 67 | return nullptr; |
| 68 | } |
| 69 | // Should not have trailing data after GeneralNames sequence. |
| 70 | if (parser.HasMore()) { |
| 71 | errors->AddError(kGeneralNamesTrailingData); |
| 72 | return nullptr; |
| 73 | } |
| 74 | return CreateFromValue(sequence_value, errors); |
| 75 | } |
| 76 |
| 77 | // static |
| 78 | std::unique_ptr<GeneralNames> GeneralNames::CreateFromValue( |
| 79 | der::Input general_names_value, CertErrors *errors) { |
| 80 | BSSL_CHECK(errors); |
| 81 |
| 82 | auto general_names = std::make_unique<GeneralNames>(); |
| 83 |
| 84 | der::Parser sequence_parser(general_names_value); |
| 85 | // The GeneralNames sequence should have at least 1 element. |
| 86 | if (!sequence_parser.HasMore()) { |
| 87 | errors->AddError(kGeneralNamesEmpty); |
| 88 | return nullptr; |
| 89 | } |
| 90 |
| 91 | while (sequence_parser.HasMore()) { |
| 92 | der::Input raw_general_name; |
| 93 | if (!sequence_parser.ReadRawTLV(&raw_general_name)) { |
| 94 | errors->AddError(kFailedReadingGeneralName); |
| 95 | return nullptr; |
| 96 | } |
| 97 |
| 98 | if (!ParseGeneralName(raw_general_name, IP_ADDRESS_ONLY, |
| 99 | general_names.get(), errors)) { |
| 100 | errors->AddError(kFailedParsingGeneralName); |
| 101 | return nullptr; |
| 102 | } |
| 103 | } |
| 104 |
| 105 | return general_names; |
| 106 | } |
| 107 |
| 108 | [[nodiscard]] bool ParseGeneralName( |
| 109 | der::Input input, |
| 110 | GeneralNames::ParseGeneralNameIPAddressType ip_address_type, |
| 111 | GeneralNames *subtrees, CertErrors *errors) { |
| 112 | BSSL_CHECK(errors); |
| 113 | der::Parser parser(input); |
| 114 | CBS_ASN1_TAG tag; |
| 115 | der::Input value; |
| 116 | if (!parser.ReadTagAndValue(&tag, &value)) { |
| 117 | return false; |
| 118 | } |
| 119 | GeneralNameTypes name_type = GENERAL_NAME_NONE; |
| 120 | if (tag == (CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) { |
| 121 | // otherName [0] OtherName, |
| 122 | name_type = GENERAL_NAME_OTHER_NAME; |
| 123 | subtrees->other_names.push_back(value); |
| 124 | } else if (tag == (CBS_ASN1_CONTEXT_SPECIFIC | 1)) { |
| 125 | // rfc822Name [1] IA5String, |
| 126 | name_type = GENERAL_NAME_RFC822_NAME; |
| 127 | const std::string_view s = BytesAsStringView(value); |
| 128 | if (!bssl::string_util::IsAscii(s)) { |
| 129 | errors->AddError(kRFC822NameNotAscii); |
| 130 | return false; |
| 131 | } |
| 132 | subtrees->rfc822_names.push_back(s); |
| 133 | } else if (tag == (CBS_ASN1_CONTEXT_SPECIFIC | 2)) { |
| 134 | // dNSName [2] IA5String, |
| 135 | name_type = GENERAL_NAME_DNS_NAME; |
| 136 | const std::string_view s = BytesAsStringView(value); |
| 137 | if (!bssl::string_util::IsAscii(s)) { |
| 138 | errors->AddError(kDnsNameNotAscii); |
| 139 | return false; |
| 140 | } |
| 141 | subtrees->dns_names.push_back(s); |
| 142 | } else if (tag == (CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 3)) { |
| 143 | // x400Address [3] ORAddress, |
| 144 | name_type = GENERAL_NAME_X400_ADDRESS; |
| 145 | subtrees->x400_addresses.push_back(value); |
| 146 | } else if (tag == (CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 4)) { |
| 147 | // directoryName [4] Name, |
| 148 | name_type = GENERAL_NAME_DIRECTORY_NAME; |
| 149 | // Name is a CHOICE { rdnSequence RDNSequence }, therefore the SEQUENCE |
| 150 | // tag is explicit. Remove it, since the name matching functions expect |
| 151 | // only the value portion. |
| 152 | der::Parser name_parser(value); |
| 153 | der::Input name_value; |
| 154 | if (!name_parser.ReadTag(CBS_ASN1_SEQUENCE, &name_value) || |
| 155 | parser.HasMore()) { |
| 156 | return false; |
| 157 | } |
| 158 | subtrees->directory_names.push_back(name_value); |
| 159 | } else if (tag == (CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 5)) { |
| 160 | // ediPartyName [5] EDIPartyName, |
| 161 | name_type = GENERAL_NAME_EDI_PARTY_NAME; |
| 162 | subtrees->edi_party_names.push_back(value); |
| 163 | } else if (tag == (CBS_ASN1_CONTEXT_SPECIFIC | 6)) { |
| 164 | // uniformResourceIdentifier [6] IA5String, |
| 165 | name_type = GENERAL_NAME_UNIFORM_RESOURCE_IDENTIFIER; |
| 166 | const std::string_view s = BytesAsStringView(value); |
| 167 | if (!bssl::string_util::IsAscii(s)) { |
| 168 | errors->AddError(kURINotAscii); |
| 169 | return false; |
| 170 | } |
| 171 | subtrees->uniform_resource_identifiers.push_back(s); |
| 172 | } else if (tag == (CBS_ASN1_CONTEXT_SPECIFIC | 7)) { |
| 173 | // iPAddress [7] OCTET STRING, |
| 174 | name_type = GENERAL_NAME_IP_ADDRESS; |
| 175 | if (ip_address_type == GeneralNames::IP_ADDRESS_ONLY) { |
| 176 | // RFC 5280 section 4.2.1.6: |
| 177 | // When the subjectAltName extension contains an iPAddress, the address |
| 178 | // MUST be stored in the octet string in "network byte order", as |
| 179 | // specified in [RFC791]. The least significant bit (LSB) of each octet |
| 180 | // is the LSB of the corresponding byte in the network address. For IP |
| 181 | // version 4, as specified in [RFC791], the octet string MUST contain |
| 182 | // exactly four octets. For IP version 6, as specified in [RFC2460], |
| 183 | // the octet string MUST contain exactly sixteen octets. |
| 184 | if ((value.size() != kIPv4AddressSize && |
| 185 | value.size() != kIPv6AddressSize)) { |
| 186 | errors->AddError(kFailedParsingIp); |
| 187 | return false; |
| 188 | } |
| 189 | subtrees->ip_addresses.push_back(value); |
| 190 | } else { |
| 191 | BSSL_CHECK(ip_address_type == GeneralNames::IP_ADDRESS_AND_NETMASK); |
| 192 | // RFC 5280 section 4.2.1.10: |
| 193 | // The syntax of iPAddress MUST be as described in Section 4.2.1.6 with |
| 194 | // the following additions specifically for name constraints. For IPv4 |
| 195 | // addresses, the iPAddress field of GeneralName MUST contain eight (8) |
| 196 | // octets, encoded in the style of RFC 4632 (CIDR) to represent an |
| 197 | // address range [RFC4632]. For IPv6 addresses, the iPAddress field |
| 198 | // MUST contain 32 octets similarly encoded. For example, a name |
| 199 | // constraint for "class C" subnet 192.0.2.0 is represented as the |
| 200 | // octets C0 00 02 00 FF FF FF 00, representing the CIDR notation |
| 201 | // 192.0.2.0/24 (mask 255.255.255.0). |
| 202 | if (value.size() != kIPv4AddressSize * 2 && |
| 203 | value.size() != kIPv6AddressSize * 2) { |
| 204 | errors->AddError(kFailedParsingIp); |
| 205 | return false; |
| 206 | } |
| 207 | der::Input addr = value.first(value.size() / 2); |
| 208 | der::Input mask = value.subspan(value.size() / 2); |
| 209 | if (!IsValidNetmask(mask)) { |
| 210 | errors->AddError(kFailedParsingIp); |
| 211 | return false; |
| 212 | } |
| 213 | subtrees->ip_address_ranges.emplace_back(addr, mask); |
| 214 | } |
| 215 | } else if (tag == (CBS_ASN1_CONTEXT_SPECIFIC | 8)) { |
| 216 | // registeredID [8] OBJECT IDENTIFIER } |
| 217 | name_type = GENERAL_NAME_REGISTERED_ID; |
| 218 | subtrees->registered_ids.push_back(value); |
| 219 | } else { |
| 220 | errors->AddError(kUnknownGeneralNameType, |
| 221 | CreateCertErrorParams1SizeT("tag", tag)); |
| 222 | return false; |
| 223 | } |
| 224 | BSSL_CHECK(GENERAL_NAME_NONE != name_type); |
| 225 | subtrees->present_name_types |= name_type; |
| 226 | return true; |
| 227 | } |
| 228 |
| 229 | BSSL_NAMESPACE_END |
| 230 |