Botan 3.6.1
Crypto and TLS for C&
Botan::AlternativeName Class Referencefinal

#include <pkix_types.h>

Inheritance diagram for Botan::AlternativeName:
Botan::ASN1_Object

Public Member Functions

void add_attribute (std::string_view type, std::string_view value)
 
void add_dn (const X509_DN &dn)
 Add a directory name to this AlternativeName.
 
void add_dns (std::string_view dns)
 Add a DNS name to this AlternativeName.
 
void add_email (std::string_view addr)
 Add a URI to this AlternativeName.
 
void add_ipv4_address (uint32_t ipv4)
 Add an IP address to this alternative name.
 
void add_other_name (const OID &oid, const ASN1_String &value)
 Add an "OtherName" identified by object identifier to this AlternativeName.
 
void add_othername (const OID &oid, std::string_view value, ASN1_Type type)
 
void add_uri (std::string_view uri)
 Add a URI to this AlternativeName.
 
 AlternativeName ()
 Create an empty name.
 
 AlternativeName (std::string_view email_addr, std::string_view uri="", std::string_view dns="", std::string_view ip_address="")
 
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
 
const std::set< X509_DN > & directory_names () const
 Return the set of directory names included in this alternative name.
 
X509_DN dn () const
 
const std::set< std::string > & dns () const
 Return the set of DNS names included in this alternative name.
 
const std::set< std::string > & email () const
 Return the set of email addresses included in this alternative name.
 
void encode_into (DER_Encoder &) const override
 
std::vector< std::string > get_attribute (std::string_view attr) const
 
std::multimap< std::string, std::string, std::less<> > get_attributes () const
 
std::string get_first_attribute (std::string_view attr) const
 
std::multimap< OID, ASN1_Stringget_othernames () const
 
bool has_field (std::string_view attr) const
 
bool has_items () const
 Return true if this has any names set.
 
const std::set< uint32_t > & ipv4_address () const
 Return the set of IPv4 addresses included in this alternative name.
 
const std::set< std::pair< OID, ASN1_String > > & other_names () const
 Return the set of "other names" included in this alternative name.
 
const std::set< std::string > & uris () const
 Return the set of URIs included in this alternative name.
 

Detailed Description

Alternative Name

Definition at line 119 of file pkix_types.h.

Constructor & Destructor Documentation

◆ AlternativeName() [1/2]

Botan::AlternativeName::AlternativeName ( )
inline

Create an empty name.

Definition at line 125 of file pkix_types.h.

125{}

◆ AlternativeName() [2/2]

Botan::AlternativeName::AlternativeName ( std::string_view email_addr,
std::string_view uri = "",
std::string_view dns = "",
std::string_view ip_address = "" )

Definition at line 20 of file asn1_alt_name.cpp.

23 {
24 if(!email_addr.empty()) {
25 add_email(email_addr);
26 }
27 if(!dns.empty()) {
28 add_dns(dns);
29 }
30 if(!uri.empty()) {
31 add_uri(uri);
32 }
33 if(!ip.empty()) {
34 if(auto ipv4 = string_to_ipv4(ip)) {
35 add_ipv4_address(*ipv4);
36 } else {
37 throw Invalid_Argument(fmt("Invalid IPv4 address '{}'", ip));
38 }
39 }
40}
void add_dns(std::string_view dns)
Add a DNS name to this AlternativeName.
Definition alt_name.cpp:30
void add_ipv4_address(uint32_t ipv4)
Add an IP address to this alternative name.
Definition alt_name.cpp:44
void add_email(std::string_view addr)
Add a URI to this AlternativeName.
Definition alt_name.cpp:24
void add_uri(std::string_view uri)
Add a URI to this AlternativeName.
Definition alt_name.cpp:18
const std::set< std::string > & dns() const
Return the set of DNS names included in this alternative name.
Definition pkix_types.h:152
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::optional< uint32_t > string_to_ipv4(std::string_view str)
Definition parsing.cpp:156

References add_dns(), add_email(), add_ipv4_address(), add_uri(), dns(), Botan::fmt(), and Botan::string_to_ipv4().

Member Function Documentation

◆ add_attribute()

void Botan::AlternativeName::add_attribute ( std::string_view type,
std::string_view value )

Definition at line 45 of file asn1_alt_name.cpp.

45 {
46 if(type.empty() || value.empty()) {
47 return;
48 }
49
50 if(type == "DNS") {
51 this->add_dns(value);
52 } else if(type == "RFC822") {
53 this->add_email(value);
54 } else if(type == "URI") {
55 this->add_uri(value);
56 } else if(type == "DN") {
57 X509_DN dn;
58 std::istringstream ss{std::string(value)};
59 ss >> dn;
60 this->add_dn(dn);
61 } else if(type == "IP") {
62 if(auto ipv4 = string_to_ipv4(value)) {
63 add_ipv4_address(*ipv4);
64 } else {
65 throw Invalid_Argument(fmt("Invalid IPv4 address '{}'", value));
66 }
67 } else {
68 throw Not_Implemented(fmt("Unknown AlternativeName name type {}", type));
69 }
70}
void add_dn(const X509_DN &dn)
Add a directory name to this AlternativeName.
Definition alt_name.cpp:40

References add_dn(), add_dns(), add_email(), add_ipv4_address(), add_uri(), dn(), Botan::fmt(), and Botan::string_to_ipv4().

◆ add_dn()

void Botan::AlternativeName::add_dn ( const X509_DN & dn)

Add a directory name to this AlternativeName.

Definition at line 40 of file alt_name.cpp.

40 {
41 m_dn_names.insert(dn);
42}

References dn().

Referenced by add_attribute(), and decode_from().

◆ add_dns()

void Botan::AlternativeName::add_dns ( std::string_view dns)

Add a DNS name to this AlternativeName.

Definition at line 30 of file alt_name.cpp.

30 {
31 if(!dns.empty()) {
32 m_dns.insert(tolower_string(dns));
33 }
34}
std::string tolower_string(std::string_view in)
Definition parsing.cpp:241

References dns(), and Botan::tolower_string().

Referenced by add_attribute(), AlternativeName(), and decode_from().

◆ add_email()

void Botan::AlternativeName::add_email ( std::string_view addr)

Add a URI to this AlternativeName.

Definition at line 24 of file alt_name.cpp.

24 {
25 if(!addr.empty()) {
26 m_email.insert(std::string(addr));
27 }
28}

Referenced by add_attribute(), AlternativeName(), and decode_from().

◆ add_ipv4_address()

void Botan::AlternativeName::add_ipv4_address ( uint32_t ipv4)

Add an IP address to this alternative name.

Definition at line 44 of file alt_name.cpp.

44 {
45 m_ipv4_addr.insert(ip);
46}

Referenced by add_attribute(), AlternativeName(), and decode_from().

◆ add_other_name()

void Botan::AlternativeName::add_other_name ( const OID & oid,
const ASN1_String & value )

Add an "OtherName" identified by object identifier to this AlternativeName.

Definition at line 36 of file alt_name.cpp.

36 {
37 m_othernames.insert(std::make_pair(oid, value));
38}

Referenced by add_othername().

◆ add_othername()

void Botan::AlternativeName::add_othername ( const OID & oid,
std::string_view value,
ASN1_Type type )

Definition at line 75 of file asn1_alt_name.cpp.

75 {
76 if(value.empty()) {
77 return;
78 }
79 this->add_other_name(oid, ASN1_String(value, type));
80}
void add_other_name(const OID &oid, const ASN1_String &value)
Add an "OtherName" identified by object identifier to this AlternativeName.
Definition alt_name.cpp:36

References add_other_name().

Referenced by decode_from().

◆ add_uri()

void Botan::AlternativeName::add_uri ( std::string_view uri)

Add a URI to this AlternativeName.

Definition at line 18 of file alt_name.cpp.

18 {
19 if(!uri.empty()) {
20 m_uri.insert(std::string(uri));
21 }
22}

Referenced by add_attribute(), AlternativeName(), and decode_from().

◆ 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::AlternativeName::contents ( ) const

Definition at line 85 of file asn1_alt_name.cpp.

85 {
86 std::multimap<std::string, std::string> names;
87
88 for(const auto& nm : this->dns()) {
89 names.emplace("DNS", nm);
90 }
91
92 for(const auto& nm : this->email()) {
93 names.emplace("RFC822", nm);
94 }
95
96 for(const auto& nm : this->uris()) {
97 names.emplace("URI", nm);
98 }
99
100 for(uint32_t ipv4 : this->ipv4_address()) {
101 names.emplace("IP", ipv4_to_string(ipv4));
102 }
103
104 for(const auto& nm : this->directory_names()) {
105 names.emplace("DN", nm.to_string());
106 }
107
108 for(const auto& othername : this->other_names()) {
109 names.emplace(othername.first.to_formatted_string(), othername.second.value());
110 }
111
112 return names;
113}
const std::set< X509_DN > & directory_names() const
Return the set of directory names included in this alternative name.
Definition pkix_types.h:164
const std::set< uint32_t > & ipv4_address() const
Return the set of IPv4 addresses included in this alternative name.
Definition pkix_types.h:155
const std::set< std::pair< OID, ASN1_String > > & other_names() const
Return the set of "other names" included in this alternative name.
Definition pkix_types.h:159
const std::set< std::string > & uris() const
Return the set of URIs included in this alternative name.
Definition pkix_types.h:146
const std::set< std::string > & email() const
Return the set of email addresses included in this alternative name.
Definition pkix_types.h:149
std::string ipv4_to_string(uint32_t ip)
Definition parsing.cpp:225

References directory_names(), dns(), email(), ipv4_address(), Botan::ipv4_to_string(), other_names(), and uris().

Referenced by get_attributes().

◆ count()

size_t Botan::AlternativeName::count ( ) const

Return the total number of names in this AlternativeName

This only counts names which were parsed, ignoring names which were of some unknown type

Definition at line 48 of file alt_name.cpp.

48 {
49 const auto sum = checked_add(
50 m_dns.size(), m_uri.size(), m_email.size(), m_ipv4_addr.size(), m_dn_names.size(), m_othernames.size());
51
52 return BOTAN_ASSERT_IS_SOME(sum);
53}
constexpr std::optional< T > checked_add(T a, T b)
Definition int_utils.h:19
#define BOTAN_ASSERT_IS_SOME(v)
Definition stl_util.h:398

References BOTAN_ASSERT_IS_SOME, and Botan::checked_add().

Referenced by has_items(), and Botan::GeneralName::matches().

◆ decode_from()

void Botan::AlternativeName::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 112 of file alt_name.cpp.

112 {
113 BER_Decoder names = source.start_sequence();
114
115 while(names.more_items()) {
116 BER_Object obj = names.get_next_object();
117
118 if(obj.is_a(0, ASN1_Class::ExplicitContextSpecific)) {
119 BER_Decoder othername(obj);
120
121 OID oid;
122 othername.decode(oid);
123 if(othername.more_items()) {
124 BER_Object othername_value_outer = othername.get_next_object();
125 othername.verify_end();
126
127 if(!othername_value_outer.is_a(0, ASN1_Class::ExplicitContextSpecific)) {
128 throw Decoding_Error("Invalid tags on otherName value");
129 }
130
131 BER_Decoder othername_value_inner(othername_value_outer);
132
133 BER_Object value = othername_value_inner.get_next_object();
134 othername_value_inner.verify_end();
135
136 if(ASN1_String::is_string_type(value.type()) && value.get_class() == ASN1_Class::Universal) {
137 add_othername(oid, ASN1::to_string(value), value.type());
138 }
139 }
140 } else if(obj.is_a(1, ASN1_Class::ContextSpecific)) {
142 } else if(obj.is_a(2, ASN1_Class::ContextSpecific)) {
144 } else if(obj.is_a(4, ASN1_Class::ContextSpecific | ASN1_Class::Constructed)) {
145 BER_Decoder dec(obj);
146 X509_DN dn;
147 dec.decode(dn);
148 this->add_dn(dn);
149 } else if(obj.is_a(6, ASN1_Class::ContextSpecific)) {
150 this->add_uri(ASN1::to_string(obj));
151 } else if(obj.is_a(7, ASN1_Class::ContextSpecific)) {
152 if(obj.length() == 4) {
153 const uint32_t ip = load_be<uint32_t>(obj.bits(), 0);
154 this->add_ipv4_address(ip);
155 } else if(obj.length() != 16) {
156 throw Decoding_Error("Invalid IP constraint neither IPv4 or IPv6");
157 }
158 }
159 }
160}
static bool is_string_type(ASN1_Type tag)
Definition asn1_str.cpp:60
void add_othername(const OID &oid, std::string_view value, ASN1_Type type)
std::string to_string(const BER_Object &obj)
Definition asn1_obj.cpp:185
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:530

References add_dn(), add_dns(), add_email(), add_ipv4_address(), add_othername(), add_uri(), Botan::BER_Object::bits(), Botan::Constructed, Botan::ContextSpecific, Botan::BER_Decoder::decode(), dn(), Botan::ExplicitContextSpecific, Botan::BER_Object::get_class(), Botan::BER_Decoder::get_next_object(), Botan::BER_Object::is_a(), Botan::ASN1_String::is_string_type(), Botan::BER_Object::length(), Botan::load_be(), Botan::BER_Decoder::more_items(), Botan::BER_Decoder::start_sequence(), Botan::ASN1::to_string(), Botan::BER_Object::type(), Botan::Universal, and Botan::BER_Decoder::verify_end().

◆ directory_names()

const std::set< X509_DN > & Botan::AlternativeName::directory_names ( ) const
inline

Return the set of directory names included in this alternative name.

Definition at line 164 of file pkix_types.h.

164{ return m_dn_names; }

Referenced by contents(), dn(), get_attribute(), and Botan::GeneralName::matches().

◆ dn()

X509_DN Botan::AlternativeName::dn ( ) const

Definition at line 167 of file asn1_alt_name.cpp.

167 {
168 // This logic really does not make any sense, but it is
169 // how this function was historically implemented.
170
171 X509_DN combined_dn;
172
173 for(const auto& dn : this->directory_names()) {
174 std::ostringstream oss;
175 oss << dn;
176
177 std::istringstream iss(oss.str());
178 iss >> combined_dn;
179 }
180
181 return combined_dn;
182}

References directory_names(), and dn().

Referenced by add_attribute(), add_dn(), decode_from(), and dn().

◆ dns()

const std::set< std::string > & Botan::AlternativeName::dns ( ) const
inline

Return the set of DNS names included in this alternative name.

Definition at line 152 of file pkix_types.h.

152{ return m_dns; }

Referenced by add_dns(), AlternativeName(), contents(), get_attribute(), and Botan::GeneralName::matches().

◆ email()

const std::set< std::string > & Botan::AlternativeName::email ( ) const
inline

Return the set of email addresses included in this alternative name.

Definition at line 149 of file pkix_types.h.

149{ return m_email; }

Referenced by contents(), and get_attribute().

◆ encode_into()

void Botan::AlternativeName::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 59 of file alt_name.cpp.

59 {
60 der.start_sequence();
61
62 /*
63 GeneralName ::= CHOICE {
64 otherName [0] OtherName,
65 rfc822Name [1] IA5String,
66 dNSName [2] IA5String,
67 x400Address [3] ORAddress,
68 directoryName [4] Name,
69 ediPartyName [5] EDIPartyName,
70 uniformResourceIdentifier [6] IA5String,
71 iPAddress [7] OCTET STRING,
72 registeredID [8] OBJECT IDENTIFIER }
73 */
74
75 for(const auto& othername : m_othernames) {
76 der.start_explicit(0)
77 .encode(othername.first)
78 .start_explicit(0)
79 .encode(othername.second)
80 .end_explicit()
81 .end_explicit();
82 }
83
84 for(const auto& name : m_email) {
85 ASN1_String str(name, ASN1_Type::Ia5String);
86 der.add_object(ASN1_Type(1), ASN1_Class::ContextSpecific, str.value());
87 }
88
89 for(const auto& name : m_dns) {
90 ASN1_String str(name, ASN1_Type::Ia5String);
91 der.add_object(ASN1_Type(2), ASN1_Class::ContextSpecific, str.value());
92 }
93
94 for(const auto& name : m_dn_names) {
95 der.add_object(ASN1_Type(4), ASN1_Class::ExplicitContextSpecific, name.DER_encode());
96 }
97
98 for(const auto& name : m_uri) {
99 ASN1_String str(name, ASN1_Type::Ia5String);
100 der.add_object(ASN1_Type(6), ASN1_Class::ContextSpecific, str.value());
101 }
102
103 for(uint32_t ip : m_ipv4_addr) {
104 auto ip_buf = store_be(ip);
105 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
106 der.add_object(ASN1_Type(7), ASN1_Class::ContextSpecific, ip_buf.data(), 4);
107 }
108
109 der.end_cons();
110}
std::string name
ASN1_Type
Definition asn1_obj.h:44
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:773

References Botan::DER_Encoder::add_object(), Botan::ContextSpecific, Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::end_explicit(), Botan::ExplicitContextSpecific, Botan::Ia5String, name, Botan::DER_Encoder::start_explicit(), Botan::DER_Encoder::start_sequence(), Botan::store_be(), and Botan::ASN1_String::value().

◆ get_attribute()

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

Definition at line 139 of file asn1_alt_name.cpp.

139 {
140 auto set_to_vector = [](const std::set<std::string>& s) -> std::vector<std::string> { return {s.begin(), s.end()}; };
141
142 if(attr == "DNS") {
143 return set_to_vector(this->dns());
144 } else if(attr == "RFC822") {
145 return set_to_vector(this->email());
146 } else if(attr == "URI") {
147 return set_to_vector(this->uris());
148 } else if(attr == "DN") {
149 std::vector<std::string> ret;
150
151 for(const auto& nm : this->directory_names()) {
152 ret.push_back(nm.to_string());
153 }
154
155 return ret;
156 } else if(attr == "IP") {
157 std::vector<std::string> ip_str;
158 for(uint32_t ipv4 : this->ipv4_address()) {
159 ip_str.push_back(ipv4_to_string(ipv4));
160 }
161 return ip_str;
162 } else {
163 return {};
164 }
165}

References directory_names(), dns(), email(), ipv4_address(), Botan::ipv4_to_string(), and uris().

Referenced by get_first_attribute(), and has_field().

◆ get_attributes()

std::multimap< std::string, std::string, std::less<> > Botan::AlternativeName::get_attributes ( ) const

Definition at line 115 of file asn1_alt_name.cpp.

115 {
116 std::multimap<std::string, std::string, std::less<>> r;
117
118 for(const auto& c : this->contents()) {
119 r.emplace(c.first, c.second);
120 }
121
122 return r;
123}
std::multimap< std::string, std::string > contents() const

References contents().

◆ get_first_attribute()

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

Definition at line 129 of file asn1_alt_name.cpp.

129 {
130 auto attr = this->get_attribute(type);
131
132 if(!attr.empty()) {
133 return attr[0];
134 }
135
136 return "";
137}
std::vector< std::string > get_attribute(std::string_view attr) const

References get_attribute().

◆ get_othernames()

std::multimap< OID, ASN1_String > Botan::AlternativeName::get_othernames ( ) const

◆ has_field()

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

Definition at line 125 of file asn1_alt_name.cpp.

125 {
126 return !this->get_attribute(attr).empty();
127}

References get_attribute().

◆ has_items()

bool Botan::AlternativeName::has_items ( ) const

Return true if this has any names set.

Definition at line 55 of file alt_name.cpp.

55 {
56 return this->count() > 0;
57}
size_t count() const
Definition alt_name.cpp:48

References count().

◆ ipv4_address()

const std::set< uint32_t > & Botan::AlternativeName::ipv4_address ( ) const
inline

Return the set of IPv4 addresses included in this alternative name.

Definition at line 155 of file pkix_types.h.

155{ return m_ipv4_addr; }

Referenced by contents(), get_attribute(), Botan::GeneralName::matches(), and Botan::X509_Certificate::matches_dns_name().

◆ other_names()

const std::set< std::pair< OID, ASN1_String > > & Botan::AlternativeName::other_names ( ) const
inline

Return the set of "other names" included in this alternative name.

Definition at line 159 of file pkix_types.h.

159 {
160 return m_othernames;
161 }

Referenced by contents().

◆ uris()

const std::set< std::string > & Botan::AlternativeName::uris ( ) const
inline

Return the set of URIs included in this alternative name.

Definition at line 146 of file pkix_types.h.

146{ return m_uri; }

Referenced by contents(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::encode_into(), and get_attribute().


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