Botan 3.12.0
Crypto and TLS for C&
Botan::X509_DN Class Referencefinal

#include <pkix_types.h>

Inheritance diagram for Botan::X509_DN:
Botan::ASN1_Object

Public Member Functions

void add_attribute (const OID &oid, const ASN1_String &val)
void add_attribute (const OID &oid, std::string_view val)
void add_attribute (std::string_view key, std::string_view val)
std::vector< uint8_t > BER_encode () const
std::multimap< std::string, std::string > contents () const
size_t count () const
void decode_from (BER_Decoder &from) override
std::vector< uint8_t > DER_encode () const
const std::vector< std::pair< OID, ASN1_String > > & dn_info () const
bool empty () const
void encode_into (DER_Encoder &to) const override
std::vector< std::string > get_attribute (std::string_view attr) const
std::multimap< OID, std::string > get_attributes () const
const std::vector< uint8_t > & get_bits () const
ASN1_String get_first_attribute (const OID &oid) const
std::string get_first_attribute (std::string_view attr) const
bool has_field (const OID &oid) const
bool has_field (std::string_view attr) const
std::string to_string () const
 X509_DN ()=default
 X509_DN (const std::multimap< OID, std::string > &args)
 X509_DN (const std::multimap< std::string, std::string > &args)
 X509_DN (std::initializer_list< std::pair< std::string_view, std::string_view > > args)

Static Public Member Functions

static std::string deref_info_field (std::string_view key)
static size_t lookup_ub (const OID &oid)

Detailed Description

Distinguished Name

Definition at line 43 of file pkix_types.h.

Constructor & Destructor Documentation

◆ X509_DN() [1/4]

Botan::X509_DN::X509_DN ( )
default

Referenced by X509_DN(), and X509_DN().

◆ X509_DN() [2/4]

Botan::X509_DN::X509_DN ( std::initializer_list< std::pair< std::string_view, std::string_view > > args)
inline

Definition at line 47 of file pkix_types.h.

47 {
48 for(const auto& i : args) {
49 add_attribute(i.first, i.second);
50 }
51 }
void add_attribute(std::string_view key, std::string_view val)
Definition x509_dn.cpp:102

References add_attribute().

◆ X509_DN() [3/4]

Botan::X509_DN::X509_DN ( const std::multimap< OID, std::string > & args)
inlineexplicit

Since DN matching for Name Constraints requires preserving order and multimaps have sorted keys, this constructor is deprecated.

Definition at line 58 of file pkix_types.h.

58 {
59 for(const auto& i : args) {
60 add_attribute(i.first, i.second);
61 }
62 }

References add_attribute(), and X509_DN().

◆ X509_DN() [4/4]

Botan::X509_DN::X509_DN ( const std::multimap< std::string, std::string > & args)
inlineexplicit

Since DN matching for Name Constraints requires preserving order and multimaps have sorted keys, this constructor is deprecated.

Definition at line 69 of file pkix_types.h.

69 {
70 for(const auto& i : args) {
71 add_attribute(i.first, i.second);
72 }
73 }

References add_attribute(), and X509_DN().

Member Function Documentation

◆ add_attribute() [1/3]

void Botan::X509_DN::add_attribute ( const OID & oid,
const ASN1_String & val )

Definition at line 109 of file x509_dn.cpp.

109 {
110 if(str.empty()) {
111 return;
112 }
113
114 m_rdn.push_back(std::make_pair(oid, str));
115 m_dn_bits.clear();
116}

References Botan::ASN1_String::empty().

◆ add_attribute() [2/3]

void Botan::X509_DN::add_attribute ( const OID & oid,
std::string_view val )
inline

Definition at line 109 of file pkix_types.h.

109{ add_attribute(oid, ASN1_String(val)); }

References add_attribute().

Referenced by add_attribute().

◆ add_attribute() [3/3]

void Botan::X509_DN::add_attribute ( std::string_view key,
std::string_view val )

Definition at line 102 of file x509_dn.cpp.

102 {
104}
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:86

References add_attribute(), and Botan::OID::from_string().

Referenced by add_attribute(), decode_from(), Botan::operator>>(), X509_DN(), X509_DN(), and X509_DN().

◆ BER_encode()

std::vector< uint8_t > Botan::ASN1_Object::BER_encode ( ) const
inherited

Return the encoding of this object. This is a convenience method when just one object needs to be serialized. Use DER_Encoder for complicated encodings.

Definition at line 20 of file asn1_obj.cpp.

20 {
21 std::vector<uint8_t> output;
22 DER_Encoder der(output);
23 this->encode_into(der);
24 return output;
25}
virtual void encode_into(DER_Encoder &to) const =0

References encode_into().

Referenced by decode_from(), Botan::Certificate_Store_In_SQL::find_all_certs(), Botan::Certificate_Store_In_SQL::find_cert(), Botan::X509_Certificate::fingerprint(), Botan::Certificate_Store_In_SQL::insert_cert(), Botan::X509_Object::PEM_encode(), Botan::PSS_Params::PSS_Params(), and Botan::Certificate_Store_In_SQL::revoke_cert().

◆ contents()

std::multimap< std::string, std::string > Botan::X509_DN::contents ( ) const

Definition at line 133 of file x509_dn.cpp.

133 {
134 std::multimap<std::string, std::string> retval;
135
136 for(const auto& i : m_rdn) {
137 retval.emplace(i.first.to_formatted_string(), i.second.value());
138 }
139 return retval;
140}

◆ count()

size_t Botan::X509_DN::count ( ) const
inline

Definition at line 90 of file pkix_types.h.

90{ return m_rdn.size(); }

Referenced by Botan::NameConstraints::is_excluded(), and Botan::NameConstraints::is_permitted().

◆ decode_from()

void Botan::X509_DN::decode_from ( BER_Decoder & from)
overridevirtual

Decode whatever this object is from from

Parameters
fromthe BER_Decoder that will be read from

Implements Botan::ASN1_Object.

Definition at line 338 of file x509_dn.cpp.

338 {
339 std::vector<uint8_t> bits;
340
341 source.start_sequence().raw_bytes(bits).end_cons();
342
343 BER_Decoder sequence(bits, source.limits());
344
345 m_rdn.clear();
346
347 while(sequence.more_items()) {
348 BER_Decoder rdn = sequence.start_set();
349
350 while(rdn.more_items()) {
351 OID oid;
352 ASN1_String str;
353
354 rdn.start_sequence()
355 .decode(oid)
356 .decode(str) // TODO support Any
357 .end_cons();
358
359 add_attribute(oid, str);
360 }
361 }
362
363 // Have to assign last as add_attribute zaps m_dn_bits
364 m_dn_bits = bits;
365}

References add_attribute(), Botan::BER_Decoder::decode(), Botan::BER_Decoder::end_cons(), Botan::BER_Decoder::limits(), Botan::BER_Decoder::more_items(), Botan::BER_Decoder::raw_bytes(), Botan::BER_Decoder::start_sequence(), and Botan::BER_Decoder::start_set().

Referenced by Botan::Certificate_Store_In_SQL::all_subjects(), and Botan::GeneralName::decode_from().

◆ DER_encode()

std::vector< uint8_t > Botan::X509_DN::DER_encode ( ) const

Definition at line 307 of file x509_dn.cpp.

307 {
308 std::vector<uint8_t> result;
309 DER_Encoder der(result);
310 this->encode_into(der);
311 return result;
312}
void encode_into(DER_Encoder &to) const override
Definition x509_dn.cpp:317

References encode_into().

◆ deref_info_field()

std::string Botan::X509_DN::deref_info_field ( std::string_view key)
static

Definition at line 198 of file x509_dn.cpp.

198 {
199 if(info == "Name" || info == "CommonName" || info == "CN") {
200 return "X520.CommonName";
201 }
202 if(info == "SerialNumber" || info == "SN") {
203 return "X520.SerialNumber";
204 }
205 if(info == "Country" || info == "C") {
206 return "X520.Country";
207 }
208 if(info == "Organization" || info == "O") {
209 return "X520.Organization";
210 }
211 if(info == "Organizational Unit" || info == "OrgUnit" || info == "OU") {
212 return "X520.OrganizationalUnit";
213 }
214 if(info == "Locality" || info == "L") {
215 return "X520.Locality";
216 }
217 if(info == "State" || info == "Province" || info == "ST") {
218 return "X520.State";
219 }
220 if(info == "Email") {
221 return "RFC822";
222 }
223 return std::string(info);
224}

Referenced by get_attribute(), get_first_attribute(), has_field(), and Botan::operator>>().

◆ dn_info()

const std::vector< std::pair< OID, ASN1_String > > & Botan::X509_DN::dn_info ( ) const
inline

Return the DN components as a vector. Note that the order of the components is preserved only when using the initializer list constructor.

Definition at line 98 of file pkix_types.h.

98{ return m_rdn; }

Referenced by Botan::PKIX::check_chain(), and Botan::operator<<().

◆ empty()

bool Botan::X509_DN::empty ( ) const
inline

Definition at line 88 of file pkix_types.h.

88{ return m_rdn.empty(); }

Referenced by Botan::NameConstraints::is_permitted().

◆ encode_into()

void Botan::X509_DN::encode_into ( DER_Encoder & to) const
overridevirtual

Encode whatever this object is into to

Parameters
tothe DER_Encoder that will be written to

Implements Botan::ASN1_Object.

Definition at line 317 of file x509_dn.cpp.

317 {
318 der.start_sequence();
319
320 if(!m_dn_bits.empty()) {
321 /*
322 If we decoded this from somewhere, encode it back exactly as
323 we received it
324 */
325 der.raw_bytes(m_dn_bits);
326 } else {
327 for(const auto& dn : m_rdn) {
328 der.start_set().start_sequence().encode(dn.first).encode(dn.second).end_cons().end_cons();
329 }
330 }
331
332 der.end_cons();
333}

References Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::raw_bytes(), Botan::DER_Encoder::start_sequence(), and Botan::DER_Encoder::start_set().

Referenced by DER_encode().

◆ get_attribute()

std::vector< std::string > Botan::X509_DN::get_attribute ( std::string_view attr) const

Definition at line 181 of file x509_dn.cpp.

181 {
182 const OID oid = OID::from_string(deref_info_field(attr));
183
184 std::vector<std::string> values;
185
186 for(const auto& i : m_rdn) {
187 if(i.first == oid) {
188 values.push_back(i.second.value());
189 }
190 }
191
192 return values;
193}
static std::string deref_info_field(std::string_view key)
Definition x509_dn.cpp:198

References deref_info_field(), and Botan::OID::from_string().

Referenced by Botan::GeneralName::matches().

◆ get_attributes()

std::multimap< OID, std::string > Botan::X509_DN::get_attributes ( ) const

Definition at line 121 of file x509_dn.cpp.

121 {
122 std::multimap<OID, std::string> retval;
123
124 for(const auto& i : m_rdn) {
125 retval.emplace(i.first, i.second.value());
126 }
127 return retval;
128}

Referenced by Botan::operator<(), and Botan::operator==().

◆ get_bits()

const std::vector< uint8_t > & Botan::X509_DN::get_bits ( ) const
inline

Definition at line 84 of file pkix_types.h.

84{ return m_dn_bits; }

Referenced by botan_x509_crl_view_binary_values().

◆ get_first_attribute() [1/2]

ASN1_String Botan::X509_DN::get_first_attribute ( const OID & oid) const

Definition at line 168 of file x509_dn.cpp.

168 {
169 for(const auto& i : m_rdn) {
170 if(i.first == oid) {
171 return i.second;
172 }
173 }
174
175 return ASN1_String();
176}

Referenced by get_first_attribute().

◆ get_first_attribute() [2/2]

std::string Botan::X509_DN::get_first_attribute ( std::string_view attr) const

Definition at line 163 of file x509_dn.cpp.

163 {
164 const OID oid = OID::from_string(deref_info_field(attr));
165 return get_first_attribute(oid).value();
166}
const std::string & value() const
Definition asn1_obj.h:365
ASN1_String get_first_attribute(const OID &oid) const
Definition x509_dn.cpp:168

References deref_info_field(), Botan::OID::from_string(), get_first_attribute(), and Botan::ASN1_String::value().

◆ has_field() [1/2]

bool Botan::X509_DN::has_field ( const OID & oid) const

Definition at line 153 of file x509_dn.cpp.

153 {
154 for(const auto& i : m_rdn) {
155 if(i.first == oid) {
156 return true;
157 }
158 }
159
160 return false;
161}

Referenced by has_field().

◆ has_field() [2/2]

bool Botan::X509_DN::has_field ( std::string_view attr) const

Definition at line 142 of file x509_dn.cpp.

142 {
143 try {
144 const OID o = OID::from_string(deref_info_field(attr));
145 if(o.has_value()) {
146 return has_field(o);
147 }
148 } catch(Lookup_Error&) {}
149
150 return false;
151}
bool has_field(const OID &oid) const
Definition x509_dn.cpp:153

References deref_info_field(), Botan::OID::from_string(), has_field(), and Botan::OID::has_value().

◆ lookup_ub()

size_t Botan::X509_DN::lookup_ub ( const OID & oid)
static

Lookup upper bounds in characters for the length of distinguished name fields as given in RFC 5280, Appendix A.

Parameters
oidthe oid of the DN to lookup
Returns
the upper bound, or zero if no ub is known to Botan

Definition at line 16 of file x509_dn_ub.cpp.

16 {
17 /*
18 * See RFC 5280 Appendix A.1 starting with comment "-- Upper Bounds"
19 */
20
21 // NOLINTBEGIN(*-branch-clone)
22 if(auto iso_dn = is_sub_element_of(oid, {2, 5, 4})) {
23 switch(*iso_dn) {
24 case 3:
25 // X520.CommonName
26 return 64;
27 case 4:
28 // X520.Surname
29 return 40;
30 case 5:
31 // X520.SerialNumber
32 return 64;
33 case 6:
34 // X520.Country
35 return 3;
36 case 7:
37 // X520.Locality
38 return 128;
39 case 8:
40 // X520.State
41 return 128;
42 case 9:
43 // X520.StreetAddress
44 return 128;
45 case 10:
46 // X520.Organization
47 return 64;
48 case 11:
49 // X520.OrganizationalUnit
50 return 64;
51 case 12:
52 // X520.Title
53 return 64;
54 case 42:
55 // X520.GivenName
56 return 16;
57 case 43:
58 // X520.Initials
59 return 5;
60 case 44:
61 // X520.GenerationalQualifier
62 return 3;
63 case 46:
64 // X520.DNQualifier
65 return 64;
66 case 65:
67 // X520.Pseudonym
68 return 128;
69 default:
70 return 0;
71 }
72 }
73
74 // NOLINTEND(*-branch-clone)
75
76 return 0;
77}
std::optional< uint32_t > is_sub_element_of(const OID &oid, std::initializer_list< uint32_t > prefix)
Definition x509_utils.h:16

References Botan::is_sub_element_of().

Referenced by Botan::PKIX::check_chain().

◆ to_string()

std::string Botan::X509_DN::to_string ( ) const

Definition at line 393 of file x509_dn.cpp.

393 {
394 std::ostringstream out;
395 out << *this;
396 return out.str();
397}

Referenced by Botan::Flatfile_Certificate_Store::Flatfile_Certificate_Store().


The documentation for this class was generated from the following files: