Botan 3.9.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

◆ 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:100

References add_attribute().

◆ X509_DN() [3/4]

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

Definition at line 51 of file pkix_types.h.

51 {
52 for(const auto& i : args) {
53 add_attribute(i.first, i.second);
54 }
55 }

References add_attribute().

◆ X509_DN() [4/4]

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

Definition at line 57 of file pkix_types.h.

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

References add_attribute().

Member Function Documentation

◆ add_attribute() [1/3]

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

Definition at line 107 of file x509_dn.cpp.

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

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

93{ 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 100 of file x509_dn.cpp.

100 {
102}
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 19 of file asn1_obj.cpp.

19 {
20 std::vector<uint8_t> output;
21 DER_Encoder der(output);
22 this->encode_into(der);
23 return output;
24}
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 131 of file x509_dn.cpp.

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

◆ count()

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

Definition at line 78 of file pkix_types.h.

78{ 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 354 of file x509_dn.cpp.

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

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

323 {
324 std::vector<uint8_t> result;
325 DER_Encoder der(result);
326 this->encode_into(der);
327 return result;
328}
void encode_into(DER_Encoder &to) const override
Definition x509_dn.cpp:333

References encode_into().

◆ deref_info_field()

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

Definition at line 196 of file x509_dn.cpp.

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

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

Definition at line 82 of file pkix_types.h.

82{ return m_rdn; }

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

◆ empty()

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

Definition at line 76 of file pkix_types.h.

76{ 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 333 of file x509_dn.cpp.

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

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

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

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

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

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

◆ get_bits()

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

Definition at line 72 of file pkix_types.h.

72{ return m_dn_bits; }

◆ get_first_attribute() [1/2]

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

Definition at line 166 of file x509_dn.cpp.

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

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

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

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

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

Referenced by has_field().

◆ has_field() [2/2]

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

Definition at line 140 of file x509_dn.cpp.

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

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

409 {
410 std::ostringstream out;
411 out << *this;
412 return out.str();
413}

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


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