Botan 3.6.1
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 &) 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 &) 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)
 

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

Constructor & Destructor Documentation

◆ X509_DN() [1/3]

Botan::X509_DN::X509_DN ( )
default

◆ X509_DN() [2/3]

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

Definition at line 42 of file pkix_types.h.

42 {
43 for(const auto& i : args) {
44 add_attribute(i.first, i.second);
45 }
46 }
void add_attribute(std::string_view key, std::string_view val)
Definition x509_dn.cpp:93

◆ X509_DN() [3/3]

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

Definition at line 48 of file pkix_types.h.

48 {
49 for(const auto& i : args) {
50 add_attribute(i.first, i.second);
51 }
52 }

Member Function Documentation

◆ add_attribute() [1/3]

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

Definition at line 100 of file x509_dn.cpp.

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

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

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

93 {
95}
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(), and Botan::operator>>().

◆ 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 Botan::ASN1_Object::encode_into().

Referenced by Botan::PSS_Params::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(), and Botan::Certificate_Store_In_SQL::revoke_cert().

◆ contents()

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

Definition at line 124 of file x509_dn.cpp.

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

◆ count()

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

Definition at line 69 of file pkix_types.h.

69{ 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 347 of file x509_dn.cpp.

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

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

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

References encode_into().

◆ deref_info_field()

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

Definition at line 189 of file x509_dn.cpp.

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

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

73{ return m_rdn; }

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

◆ empty()

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

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

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

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

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

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

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

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

◆ get_bits()

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

Definition at line 63 of file pkix_types.h.

63{ return m_dn_bits; }

◆ get_first_attribute() [1/2]

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

Definition at line 159 of file x509_dn.cpp.

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

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

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

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

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

Referenced by has_field().

◆ has_field() [2/2]

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

Definition at line 133 of file x509_dn.cpp.

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

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 48 of file x509_dn_ub.cpp.

48 {
49 auto ub_entry = DN_UB.find(oid);
50 if(ub_entry != DN_UB.end()) {
51 return ub_entry->second;
52 } else {
53 return 0;
54 }
55}

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

◆ to_string()

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

Definition at line 402 of file x509_dn.cpp.

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

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


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