xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/pki/parse_name.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
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 BSSL_PKI_PARSE_NAME_H_
6 #define BSSL_PKI_PARSE_NAME_H_
7 
8 #include <vector>
9 
10 #include <openssl/base.h>
11 #include <openssl/bytestring.h>
12 
13 #include "input.h"
14 #include "parser.h"
15 
16 namespace bssl {
17 
18 // id-at-commonName: 2.5.4.3 (RFC 5280)
19 inline constexpr uint8_t kTypeCommonNameOid[] = {0x55, 0x04, 0x03};
20 // id-at-surname: 2.5.4.4 (RFC 5280)
21 inline constexpr uint8_t kTypeSurnameOid[] = {0x55, 0x04, 0x04};
22 // id-at-serialNumber: 2.5.4.5 (RFC 5280)
23 inline constexpr uint8_t kTypeSerialNumberOid[] = {0x55, 0x04, 0x05};
24 // id-at-countryName: 2.5.4.6 (RFC 5280)
25 inline constexpr uint8_t kTypeCountryNameOid[] = {0x55, 0x04, 0x06};
26 // id-at-localityName: 2.5.4.7 (RFC 5280)
27 inline constexpr uint8_t kTypeLocalityNameOid[] = {0x55, 0x04, 0x07};
28 // id-at-stateOrProvinceName: 2.5.4.8 (RFC 5280)
29 inline constexpr uint8_t kTypeStateOrProvinceNameOid[] = {0x55, 0x04, 0x08};
30 // street (streetAddress): 2.5.4.9 (RFC 4519)
31 inline constexpr uint8_t kTypeStreetAddressOid[] = {0x55, 0x04, 0x09};
32 // id-at-organizationName: 2.5.4.10 (RFC 5280)
33 inline constexpr uint8_t kTypeOrganizationNameOid[] = {0x55, 0x04, 0x0a};
34 // id-at-organizationalUnitName: 2.5.4.11 (RFC 5280)
35 inline constexpr uint8_t kTypeOrganizationUnitNameOid[] = {0x55, 0x04, 0x0b};
36 // id-at-title: 2.5.4.12 (RFC 5280)
37 inline constexpr uint8_t kTypeTitleOid[] = {0x55, 0x04, 0x0c};
38 // id-at-name: 2.5.4.41 (RFC 5280)
39 inline constexpr uint8_t kTypeNameOid[] = {0x55, 0x04, 0x29};
40 // id-at-givenName: 2.5.4.42 (RFC 5280)
41 inline constexpr uint8_t kTypeGivenNameOid[] = {0x55, 0x04, 0x2a};
42 // id-at-initials: 2.5.4.43 (RFC 5280)
43 inline constexpr uint8_t kTypeInitialsOid[] = {0x55, 0x04, 0x2b};
44 // id-at-generationQualifier: 2.5.4.44 (RFC 5280)
45 inline constexpr uint8_t kTypeGenerationQualifierOid[] = {0x55, 0x04, 0x2c};
46 // dc (domainComponent): 0.9.2342.19200300.100.1.25 (RFC 4519)
47 inline constexpr uint8_t kTypeDomainComponentOid[] = {
48     0x09, 0x92, 0x26, 0x89, 0x93, 0xF2, 0x2C, 0x64, 0x01, 0x19};
49 // RFC 5280 section A.1:
50 //
51 // pkcs-9 OBJECT IDENTIFIER ::=
52 //   { iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) 9 }
53 //
54 // id-emailAddress      AttributeType ::= { pkcs-9 1 }
55 //
56 // In dotted form: 1.2.840.113549.1.9.1
57 inline constexpr uint8_t kTypeEmailAddressOid[] = {0x2A, 0x86, 0x48, 0x86, 0xF7,
58                                                    0x0D, 0x01, 0x09, 0x01};
59 
60 // X509NameAttribute contains a representation of a DER-encoded RFC 2253
61 // "AttributeTypeAndValue".
62 //
63 // AttributeTypeAndValue ::= SEQUENCE {
64 //     type  AttributeType,
65 //     value AttributeValue
66 // }
67 struct OPENSSL_EXPORT X509NameAttribute {
X509NameAttributeX509NameAttribute68   X509NameAttribute(der::Input in_type, CBS_ASN1_TAG in_value_tag,
69                     der::Input in_value)
70       : type(in_type), value_tag(in_value_tag), value(in_value) {}
71 
72   // Configures handling of PrintableString in the attribute value. Do
73   // not use non-default handling without consulting //net owners. With
74   // kAsUTF8Hack, PrintableStrings are interpreted as UTF-8 strings.
75   enum class PrintableStringHandling { kDefault, kAsUTF8Hack };
76 
77   // Attempts to convert the value represented by this struct into a
78   // UTF-8 string and store it in |out|, returning whether the conversion
79   // was successful.
80   [[nodiscard]] bool ValueAsString(std::string *out) const;
81 
82   // Attempts to convert the value represented by this struct into a
83   // UTF-8 string and store it in |out|, returning whether the conversion
84   // was successful. Allows configuring some non-standard string handling
85   // options.
86   //
87   // Do not use without consulting //net owners.
88   [[nodiscard]] bool ValueAsStringWithUnsafeOptions(
89       PrintableStringHandling printable_string_handling,
90       std::string *out) const;
91 
92   // Attempts to convert the value represented by this struct into a
93   // std::string and store it in |out|, returning whether the conversion was
94   // successful. Due to some encodings being incompatible, the caller must
95   // verify the attribute |value_tag|.
96   //
97   // Note: Don't use this function unless you know what you're doing. Use
98   // ValueAsString instead.
99   //
100   // Note: The conversion doesn't verify that the value corresponds to the
101   // ASN.1 definition of the value type.
102   [[nodiscard]] bool ValueAsStringUnsafe(std::string *out) const;
103 
104   // Formats the NameAttribute per RFC2253 into an ASCII string and stores
105   // the result in |out|, returning whether the conversion was successful.
106   [[nodiscard]] bool AsRFC2253String(std::string *out) const;
107 
108   der::Input type;
109   CBS_ASN1_TAG value_tag;
110   der::Input value;
111 };
112 
113 typedef std::vector<X509NameAttribute> RelativeDistinguishedName;
114 typedef std::vector<RelativeDistinguishedName> RDNSequence;
115 
116 // Parses all the ASN.1 AttributeTypeAndValue elements in |parser| and stores
117 // each as an AttributeTypeAndValue object in |out|.
118 //
119 // AttributeTypeAndValue is defined in RFC 5280 section 4.1.2.4:
120 //
121 // AttributeTypeAndValue ::= SEQUENCE {
122 //   type     AttributeType,
123 //   value    AttributeValue }
124 //
125 // AttributeType ::= OBJECT IDENTIFIER
126 //
127 // AttributeValue ::= ANY -- DEFINED BY AttributeType
128 //
129 // DirectoryString ::= CHOICE {
130 //       teletexString           TeletexString (SIZE (1..MAX)),
131 //       printableString         PrintableString (SIZE (1..MAX)),
132 //       universalString         UniversalString (SIZE (1..MAX)),
133 //       utf8String              UTF8String (SIZE (1..MAX)),
134 //       bmpString               BMPString (SIZE (1..MAX)) }
135 //
136 // The type of the component AttributeValue is determined by the AttributeType;
137 // in general it will be a DirectoryString.
138 [[nodiscard]] OPENSSL_EXPORT bool ReadRdn(der::Parser *parser,
139                                           RelativeDistinguishedName *out);
140 
141 // Parses a DER-encoded "Name" as specified by 5280. Returns true on success
142 // and sets the results in |out|.
143 [[nodiscard]] OPENSSL_EXPORT bool ParseName(der::Input name_tlv,
144                                             RDNSequence *out);
145 // Parses a DER-encoded "Name" value (without the sequence tag & length) as
146 // specified by 5280. Returns true on success and sets the results in |out|.
147 [[nodiscard]] OPENSSL_EXPORT bool ParseNameValue(der::Input name_value,
148                                                  RDNSequence *out);
149 
150 // Formats a RDNSequence |rdn_sequence| per RFC2253 as an ASCII string and
151 // stores the result into |out|, and returns whether the conversion was
152 // successful.
153 [[nodiscard]] OPENSSL_EXPORT bool ConvertToRFC2253(
154     const RDNSequence &rdn_sequence, std::string *out);
155 }  // namespace bssl
156 
157 #endif  // BSSL_PKI_PARSE_NAME_H_
158