Botan 3.14.1
Crypto and TLS for C&
asn1_alt_name.cpp
Go to the documentation of this file.
1/*
2* AlternativeName
3* (C) 1999-2007 Jack Lloyd
4* 2007 Yves Jerschow
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9// TODO(Botan4) this entire file can be deleted
10
11#include <botan/pkix_types.h>
12
13#include <botan/internal/fmt.h>
14
15namespace Botan {
16
17/*
18* Create an AlternativeName
19*/
20AlternativeName::AlternativeName(std::string_view email_addr,
21 std::string_view uri,
22 std::string_view dns,
23 std::string_view ip) {
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 = IPv4Address::from_string(ip)) {
35 add_ipv4_address(*ipv4);
36 } else {
37 throw Invalid_Argument(fmt("Invalid IPv4 address '{}'", ip));
38 }
39 }
40}
41
42/*
43* Add an attribute to an alternative name
44*/
45void AlternativeName::add_attribute(std::string_view type, std::string_view value) {
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 if(auto dn = X509_DN::parse(value)) {
58 this->add_dn(*dn);
59 } else {
60 throw Invalid_Argument(fmt("Invalid X.509 DN '{}'", value));
61 }
62 } else if(type == "IP") {
63 if(auto ipv4 = IPv4Address::from_string(value)) {
64 add_ipv4_address(*ipv4);
65 } else {
66 throw Invalid_Argument(fmt("Invalid IPv4 address '{}'", value));
67 }
68 } else {
69 throw Not_Implemented(fmt("Unknown AlternativeName name type {}", type));
70 }
71}
72
73/*
74* Add an OtherName field
75*/
76void AlternativeName::add_othername(const OID& oid, std::string_view value, ASN1_Type type) {
77 if(value.empty()) {
78 return;
79 }
80 this->add_other_name(oid, ASN1_String(value, type));
81}
82
83/*
84* Return all of the alternative names
85*/
86std::multimap<std::string, std::string> AlternativeName::contents() const {
87 std::multimap<std::string, std::string> names;
88
89 for(const auto& nm : this->dns()) {
90 names.emplace("DNS", nm);
91 }
92
93 for(const auto& nm : this->email()) {
94 names.emplace("RFC822", nm);
95 }
96
97 for(const auto& nm : this->uris()) {
98 names.emplace("URI", nm);
99 }
100
101 for(const auto& ipv4 : this->ipv4_addresses()) {
102 names.emplace("IP", ipv4.to_string());
103 }
104
105 for(const auto& ipv6 : this->ipv6_addresses()) {
106 names.emplace("IPv6", ipv6.to_string());
107 }
108
109 for(const auto& nm : this->directory_names()) {
110 names.emplace("DN", nm.to_string());
111 }
112
113 for(const auto& othername : this->other_names()) {
114 names.emplace(othername.first.to_formatted_string(), othername.second.value());
115 }
116
117 return names;
118}
119
120std::multimap<std::string, std::string, std::less<>> AlternativeName::get_attributes() const {
121 std::multimap<std::string, std::string, std::less<>> r;
122
123 for(const auto& c : this->contents()) {
124 r.emplace(c.first, c.second);
125 }
126
127 return r;
128}
129
130bool AlternativeName::has_field(std::string_view attr) const {
131 return !this->get_attribute(attr).empty();
132}
133
134std::string AlternativeName::get_first_attribute(std::string_view type) const {
135 auto attr = this->get_attribute(type);
136
137 if(!attr.empty()) {
138 return attr[0];
139 }
140
141 return "";
142}
143
144std::vector<std::string> AlternativeName::get_attribute(std::string_view attr) const {
145 const auto set_to_vector = [](const std::set<std::string>& s) -> std::vector<std::string> {
146 return {s.begin(), s.end()};
147 };
148
149 if(attr == "DNS") {
150 return set_to_vector(this->dns());
151 } else if(attr == "RFC822") {
152 return set_to_vector(this->email());
153 } else if(attr == "URI") {
154 return set_to_vector(this->uris());
155 } else if(attr == "DN") {
156 std::vector<std::string> ret;
157
158 for(const auto& nm : this->directory_names()) {
159 ret.push_back(nm.to_string());
160 }
161
162 return ret;
163 } else if(attr == "IP") {
164 std::vector<std::string> ip_str;
165 for(const auto& ipv4 : this->ipv4_addresses()) {
166 ip_str.push_back(ipv4.to_string());
167 }
168 return ip_str;
169 } else {
170 return {};
171 }
172}
173
175 // This logic really does not make any sense, but it is
176 // how this function was historically implemented.
177
178 X509_DN combined_dn;
179
180 for(const auto& dn : this->directory_names()) {
181 for(const auto& rdn : dn.rdns()) {
182 combined_dn.add_rdn(rdn);
183 }
184 }
185
186 return combined_dn;
187}
188
189std::set<uint32_t> AlternativeName::ipv4_address() const {
190 std::set<uint32_t> out;
191 for(const auto& a : m_ipv4_addrs) {
192 out.insert(a.address());
193 }
194 return out;
195}
196
197} // namespace Botan
const std::set< IPv6Address > & ipv6_addresses() const
Return the set of IPv6 addresses included in this alternative name.
Definition pkix_types.h:421
const std::set< X509_DN > & directory_names() const
Return the set of directory names included in this alternative name.
Definition pkix_types.h:441
std::string get_first_attribute(std::string_view attr) const
void add_dns(std::string_view dns)
Add a DNS name to this AlternativeName, parsing and validating the input.
Definition alt_name.cpp:63
std::set< std::string > dns() const
Definition alt_name.cpp:78
void add_ipv4_address(uint32_t ipv4)
Add an IP address to this alternative name.
Definition pkix_types.h:372
void add_email(std::string_view addr)
Add an email address to this AlternativeName, parsing and validating the input.
Definition alt_name.cpp:40
std::set< uint32_t > ipv4_address() const
Return the set of IPv4 addresses included in this alternative name.
std::multimap< std::string, std::string > contents() const
std::vector< std::string > get_attribute(std::string_view attr) const
const std::set< std::pair< OID, ASN1_String > > & other_names() const
Return the set of "other names" whose value was a recognized ASN1_String type.
Definition pkix_types.h:425
void add_uri(std::string_view uri)
Add a URI to this AlternativeName, parsing and validating the input.
Definition alt_name.cpp:17
void add_attribute(std::string_view type, std::string_view value)
std::multimap< std::string, std::string, std::less<> > get_attributes() const
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:86
std::set< std::string > uris() const
Definition alt_name.cpp:32
void add_othername(const OID &oid, std::string_view value, ASN1_Type type)
std::set< std::string > email() const
Definition alt_name.cpp:55
const std::set< IPv4Address > & ipv4_addresses() const
Return the set of IPv4 addresses included in this alternative name.
Definition pkix_types.h:418
void add_dn(const X509_DN &dn)
Add a directory name to this AlternativeName.
Definition alt_name.cpp:101
AlternativeName()=default
Create an empty name.
bool has_field(std::string_view attr) const
static std::optional< IPv4Address > from_string(std::string_view str)
void add_rdn(std::vector< std::pair< OID, ASN1_String > > rdn)
Definition x509_dn.cpp:200
static std::optional< X509_DN > parse(std::string_view str)
Definition x509_dn.cpp:551
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
ASN1_Type
Definition asn1_obj.h:47