• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fillins/openssl_util.h"
9 #include <vector>
10 
11 
12 #include "input.h"
13 #include "parser.h"
14 #include "tag.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,
69                     der::Tag in_value_tag,
70                     der::Input in_value)
71       : type(in_type), value_tag(in_value_tag), value(in_value) {}
72 
73   // Configures handling of PrintableString in the attribute value. Do
74   // not use non-default handling without consulting //net owners. With
75   // kAsUTF8Hack, PrintableStrings are interpreted as UTF-8 strings.
76   enum class PrintableStringHandling { kDefault, kAsUTF8Hack };
77 
78   // Attempts to convert the value represented by this struct into a
79   // UTF-8 string and store it in |out|, returning whether the conversion
80   // was successful.
81   [[nodiscard]] bool ValueAsString(std::string* out) const;
82 
83   // Attempts to convert the value represented by this struct into a
84   // UTF-8 string and store it in |out|, returning whether the conversion
85   // was successful. Allows configuring some non-standard string handling
86   // options.
87   //
88   // Do not use without consulting //net owners.
89   [[nodiscard]] bool ValueAsStringWithUnsafeOptions(
90       PrintableStringHandling printable_string_handling,
91       std::string* out) const;
92 
93   // Attempts to convert the value represented by this struct into a
94   // std::string and store it in |out|, returning whether the conversion was
95   // successful. Due to some encodings being incompatible, the caller must
96   // verify the attribute |value_tag|.
97   //
98   // Note: Don't use this function unless you know what you're doing. Use
99   // ValueAsString instead.
100   //
101   // Note: The conversion doesn't verify that the value corresponds to the
102   // ASN.1 definition of the value type.
103   [[nodiscard]] bool ValueAsStringUnsafe(std::string* out) const;
104 
105   // Formats the NameAttribute per RFC2253 into an ASCII string and stores
106   // the result in |out|, returning whether the conversion was successful.
107   [[nodiscard]] bool AsRFC2253String(std::string* out) const;
108 
109   der::Input type;
110   der::Tag value_tag;
111   der::Input value;
112 };
113 
114 typedef std::vector<X509NameAttribute> RelativeDistinguishedName;
115 typedef std::vector<RelativeDistinguishedName> RDNSequence;
116 
117 // Parses all the ASN.1 AttributeTypeAndValue elements in |parser| and stores
118 // each as an AttributeTypeAndValue object in |out|.
119 //
120 // AttributeTypeAndValue is defined in RFC 5280 section 4.1.2.4:
121 //
122 // AttributeTypeAndValue ::= SEQUENCE {
123 //   type     AttributeType,
124 //   value    AttributeValue }
125 //
126 // AttributeType ::= OBJECT IDENTIFIER
127 //
128 // AttributeValue ::= ANY -- DEFINED BY AttributeType
129 //
130 // DirectoryString ::= CHOICE {
131 //       teletexString           TeletexString (SIZE (1..MAX)),
132 //       printableString         PrintableString (SIZE (1..MAX)),
133 //       universalString         UniversalString (SIZE (1..MAX)),
134 //       utf8String              UTF8String (SIZE (1..MAX)),
135 //       bmpString               BMPString (SIZE (1..MAX)) }
136 //
137 // The type of the component AttributeValue is determined by the AttributeType;
138 // in general it will be a DirectoryString.
139 [[nodiscard]] OPENSSL_EXPORT bool ReadRdn(der::Parser* parser,
140                                       RelativeDistinguishedName* out);
141 
142 // Parses a DER-encoded "Name" as specified by 5280. Returns true on success
143 // and sets the results in |out|.
144 [[nodiscard]] OPENSSL_EXPORT bool ParseName(const der::Input& name_tlv,
145                                         RDNSequence* out);
146 // Parses a DER-encoded "Name" value (without the sequence tag & length) as
147 // specified by 5280. Returns true on success and sets the results in |out|.
148 [[nodiscard]] OPENSSL_EXPORT bool ParseNameValue(const der::Input& name_value,
149                                              RDNSequence* out);
150 
151 // Formats a RDNSequence |rdn_sequence| per RFC2253 as an ASCII string and
152 // stores the result into |out|, and returns whether the conversion was
153 // successful.
154 [[nodiscard]] OPENSSL_EXPORT bool ConvertToRFC2253(const RDNSequence& rdn_sequence,
155                                                std::string* out);
156 }  // namespace net
157 
158 #endif  // BSSL_PKI_PARSE_NAME_H_
159