Botan 3.11.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 41 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 45 of file pkix_types.h.

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

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 56 of file pkix_types.h.

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

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 67 of file pkix_types.h.

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

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 101 of file x509_dn.cpp.

101 {
102 if(str.empty()) {
103 return;
104 }
105
106 m_rdn.push_back(std::make_pair(oid, str));
107 m_dn_bits.clear();
108}

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 107 of file pkix_types.h.

107{ 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 94 of file x509_dn.cpp.

94 {
96}
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::Certificate_Store_Windows::find_cert_by_issuer_dn_and_serial_number(), 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 125 of file x509_dn.cpp.

125 {
126 std::multimap<std::string, std::string> retval;
127
128 for(const auto& i : m_rdn) {
129 retval.emplace(i.first.to_formatted_string(), i.second.value());
130 }
131 return retval;
132}

◆ count()

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

Definition at line 88 of file pkix_types.h.

88{ 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 348 of file x509_dn.cpp.

348 {
349 std::vector<uint8_t> bits;
350
351 source.start_sequence().raw_bytes(bits).end_cons();
352
353 BER_Decoder sequence(bits);
354
355 m_rdn.clear();
356
357 while(sequence.more_items()) {
358 BER_Decoder rdn = sequence.start_set();
359
360 while(rdn.more_items()) {
361 OID oid;
362 ASN1_String str;
363
364 rdn.start_sequence()
365 .decode(oid)
366 .decode(str) // TODO support Any
367 .end_cons();
368
369 add_attribute(oid, str);
370 }
371 }
372
373 // Have to assign last as add_attribute zaps m_dn_bits
374 m_dn_bits = bits;
375}

References add_attribute(), Botan::BER_Decoder::decode(), Botan::BER_Decoder::end_cons(), 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(), Botan::Certificate_Store_Windows::all_subjects(), and Botan::GeneralName::decode_from().

◆ DER_encode()

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

Definition at line 317 of file x509_dn.cpp.

317 {
318 std::vector<uint8_t> result;
319 DER_Encoder der(result);
320 this->encode_into(der);
321 return result;
322}
void encode_into(DER_Encoder &to) const override
Definition x509_dn.cpp:327

References encode_into().

◆ deref_info_field()

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

Definition at line 190 of file x509_dn.cpp.

190 {
191 if(info == "Name" || info == "CommonName" || info == "CN") {
192 return "X520.CommonName";
193 }
194 if(info == "SerialNumber" || info == "SN") {
195 return "X520.SerialNumber";
196 }
197 if(info == "Country" || info == "C") {
198 return "X520.Country";
199 }
200 if(info == "Organization" || info == "O") {
201 return "X520.Organization";
202 }
203 if(info == "Organizational Unit" || info == "OrgUnit" || info == "OU") {
204 return "X520.OrganizationalUnit";
205 }
206 if(info == "Locality" || info == "L") {
207 return "X520.Locality";
208 }
209 if(info == "State" || info == "Province" || info == "ST") {
210 return "X520.State";
211 }
212 if(info == "Email") {
213 return "RFC822";
214 }
215 return std::string(info);
216}

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 96 of file pkix_types.h.

96{ return m_rdn; }

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

◆ empty()

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

Definition at line 86 of file pkix_types.h.

86{ return m_rdn.empty(); }

◆ 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 327 of file x509_dn.cpp.

327 {
328 der.start_sequence();
329
330 if(!m_dn_bits.empty()) {
331 /*
332 If we decoded this from somewhere, encode it back exactly as
333 we received it
334 */
335 der.raw_bytes(m_dn_bits);
336 } else {
337 for(const auto& dn : m_rdn) {
338 der.start_set().start_sequence().encode(dn.first).encode(dn.second).end_cons().end_cons();
339 }
340 }
341
342 der.end_cons();
343}

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 173 of file x509_dn.cpp.

173 {
174 const OID oid = OID::from_string(deref_info_field(attr));
175
176 std::vector<std::string> values;
177
178 for(const auto& i : m_rdn) {
179 if(i.first == oid) {
180 values.push_back(i.second.value());
181 }
182 }
183
184 return values;
185}
static std::string deref_info_field(std::string_view key)
Definition x509_dn.cpp:190

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 113 of file x509_dn.cpp.

113 {
114 std::multimap<OID, std::string> retval;
115
116 for(const auto& i : m_rdn) {
117 retval.emplace(i.first, i.second.value());
118 }
119 return retval;
120}

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

◆ get_bits()

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

Definition at line 82 of file pkix_types.h.

82{ 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 160 of file x509_dn.cpp.

160 {
161 for(const auto& i : m_rdn) {
162 if(i.first == oid) {
163 return i.second;
164 }
165 }
166
167 return ASN1_String();
168}

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 155 of file x509_dn.cpp.

155 {
156 const OID oid = OID::from_string(deref_info_field(attr));
157 return get_first_attribute(oid).value();
158}
const std::string & value() const
Definition asn1_obj.h:365
ASN1_String get_first_attribute(const OID &oid) const
Definition x509_dn.cpp:160

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 145 of file x509_dn.cpp.

145 {
146 for(const auto& i : m_rdn) {
147 if(i.first == oid) {
148 return true;
149 }
150 }
151
152 return false;
153}

Referenced by has_field().

◆ has_field() [2/2]

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

Definition at line 134 of file x509_dn.cpp.

134 {
135 try {
136 const OID o = OID::from_string(deref_info_field(attr));
137 if(o.has_value()) {
138 return has_field(o);
139 }
140 } catch(Lookup_Error&) {}
141
142 return false;
143}
bool has_field(const OID &oid) const
Definition x509_dn.cpp:145

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 403 of file x509_dn.cpp.

403 {
404 std::ostringstream out;
405 out << *this;
406 return out.str();
407}

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


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