Botan 2.19.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#include <botan/pkix_types.h>
10#include <botan/der_enc.h>
11#include <botan/ber_dec.h>
12#include <botan/oids.h>
13#include <botan/internal/stl_util.h>
14#include <botan/parsing.h>
15#include <botan/loadstor.h>
16
17#include <sstream>
18
19namespace Botan {
20
21/*
22* Create an AlternativeName
23*/
24AlternativeName::AlternativeName(const std::string& email_addr,
25 const std::string& uri,
26 const std::string& dns,
27 const std::string& ip)
28 {
29 add_attribute("RFC822", email_addr);
30 add_attribute("DNS", dns);
31 add_attribute("URI", uri);
32 add_attribute("IP", ip);
33 }
34
35/*
36* Add an attribute to an alternative name
37*/
38void AlternativeName::add_attribute(const std::string& type,
39 const std::string& value)
40 {
41 if(type.empty() || value.empty())
42 return;
43
44 auto range = m_alt_info.equal_range(type);
45 for(auto j = range.first; j != range.second; ++j)
46 if(j->second == value)
47 return;
48
49 multimap_insert(m_alt_info, type, value);
50 }
51
52/*
53* Add an OtherName field
54*/
55void AlternativeName::add_othername(const OID& oid, const std::string& value,
57 {
58 if(value.empty())
59 return;
60 multimap_insert(m_othernames, oid, ASN1_String(value, type));
61 }
62
63/*
64* Return all of the alternative names
65*/
66std::multimap<std::string, std::string> AlternativeName::contents() const
67 {
68 std::multimap<std::string, std::string> names;
69
70 for(auto i = m_alt_info.begin(); i != m_alt_info.end(); ++i)
71 {
72 multimap_insert(names, i->first, i->second);
73 }
74
75 for(auto i = m_othernames.begin(); i != m_othernames.end(); ++i)
76 {
77 multimap_insert(names, i->first.to_formatted_string(), i->second.value());
78 }
79
80 return names;
81 }
82
83bool AlternativeName::has_field(const std::string& attr) const
84 {
85 auto range = m_alt_info.equal_range(attr);
86 return (range.first != range.second);
87 }
88
89std::string AlternativeName::get_first_attribute(const std::string& attr) const
90 {
91 auto i = m_alt_info.lower_bound(attr);
92 if(i != m_alt_info.end() && i->first == attr)
93 return i->second;
94
95 return "";
96 }
97
98std::vector<std::string> AlternativeName::get_attribute(const std::string& attr) const
99 {
100 std::vector<std::string> results;
101 auto range = m_alt_info.equal_range(attr);
102 for(auto i = range.first; i != range.second; ++i)
103 results.push_back(i->second);
104 return results;
105 }
106
108 {
109 X509_DN dn;
110 auto range = m_alt_info.equal_range("DN");
111
112 for(auto i = range.first; i != range.second; ++i)
113 {
114 std::istringstream strm(i->second);
115 strm >> dn;
116 }
117
118 return dn;
119 }
120
121/*
122* Return if this object has anything useful
123*/
125 {
126 return (m_alt_info.size() > 0 || m_othernames.size() > 0);
127 }
128
129namespace {
130
131/*
132* DER encode an AlternativeName entry
133*/
134void encode_entries(DER_Encoder& encoder,
135 const std::multimap<std::string, std::string>& attr,
136 const std::string& type, ASN1_Tag tagging)
137 {
138 auto range = attr.equal_range(type);
139
140 for(auto i = range.first; i != range.second; ++i)
141 {
142 if(type == "RFC822" || type == "DNS" || type == "URI")
143 {
144 ASN1_String asn1_string(i->second, IA5_STRING);
145 encoder.add_object(tagging, CONTEXT_SPECIFIC, asn1_string.value());
146 }
147 else if(type == "IP")
148 {
149 const uint32_t ip = string_to_ipv4(i->second);
150 uint8_t ip_buf[4] = { 0 };
151 store_be(ip, ip_buf);
152 encoder.add_object(tagging, CONTEXT_SPECIFIC, ip_buf, 4);
153 }
154 else if (type == "DN")
155 {
156 std::stringstream ss(i->second);
157 X509_DN dn;
158 ss >> dn;
159 encoder.encode(dn);
160 }
161 }
162 }
163
164}
165
166/*
167* DER encode an AlternativeName extension
168*/
170 {
171 der.start_cons(SEQUENCE);
172
173 encode_entries(der, m_alt_info, "RFC822", ASN1_Tag(1));
174 encode_entries(der, m_alt_info, "DNS", ASN1_Tag(2));
175 encode_entries(der, m_alt_info, "DN", ASN1_Tag(4));
176 encode_entries(der, m_alt_info, "URI", ASN1_Tag(6));
177 encode_entries(der, m_alt_info, "IP", ASN1_Tag(7));
178
179 for(auto i = m_othernames.begin(); i != m_othernames.end(); ++i)
180 {
181 der.start_explicit(0)
182 .encode(i->first)
184 .encode(i->second)
185 .end_explicit()
186 .end_explicit();
187 }
188
189 der.end_cons();
190 }
191
192/*
193* Decode a BER encoded AlternativeName
194*/
196 {
197 BER_Decoder names = source.start_cons(SEQUENCE);
198
199 // FIXME this is largely a duplication of GeneralName::decode_from
200
201 while(names.more_items())
202 {
203 BER_Object obj = names.get_next_object();
204
205 if(obj.is_a(0, CONTEXT_SPECIFIC))
206 {
207 BER_Decoder othername(obj);
208
209 OID oid;
210 othername.decode(oid);
211 if(othername.more_items())
212 {
213 BER_Object othername_value_outer = othername.get_next_object();
214 othername.verify_end();
215
216 if(othername_value_outer.is_a(0, ASN1_Tag(CONTEXT_SPECIFIC | CONSTRUCTED)) == false)
217 throw Decoding_Error("Invalid tags on otherName value");
218
219 BER_Decoder othername_value_inner(othername_value_outer);
220
221 BER_Object value = othername_value_inner.get_next_object();
222 othername_value_inner.verify_end();
223
224 if(ASN1_String::is_string_type(value.type()) && value.get_class() == UNIVERSAL)
225 {
226 add_othername(oid, ASN1::to_string(value), value.type());
227 }
228 }
229 }
230 if(obj.is_a(1, CONTEXT_SPECIFIC))
231 {
232 add_attribute("RFC822", ASN1::to_string(obj));
233 }
234 else if(obj.is_a(2, CONTEXT_SPECIFIC))
235 {
236 add_attribute("DNS", ASN1::to_string(obj));
237 }
238 else if(obj.is_a(6, CONTEXT_SPECIFIC))
239 {
240 add_attribute("URI", ASN1::to_string(obj));
241 }
242 else if(obj.is_a(4, ASN1_Tag(CONTEXT_SPECIFIC | CONSTRUCTED)))
243 {
244 BER_Decoder dec(obj);
245 X509_DN dn;
246 std::stringstream ss;
247
248 dec.decode(dn);
249 ss << dn;
250
251 add_attribute("DN", ss.str());
252 }
253 else if(obj.is_a(7, CONTEXT_SPECIFIC))
254 {
255 if(obj.length() == 4)
256 {
257 const uint32_t ip = load_be<uint32_t>(obj.bits(), 0);
258 add_attribute("IP", ipv4_to_string(ip));
259 }
260 }
261
262 }
263 }
264
265}
static bool is_string_type(ASN1_Tag tag)
Definition: asn1_str.cpp:68
std::multimap< std::string, std::string > contents() const
void encode_into(DER_Encoder &) const override
bool has_field(const std::string &attr) const
std::string get_first_attribute(const std::string &attr) const
void add_othername(const OID &oid, const std::string &value, ASN1_Tag type)
std::vector< std::string > get_attribute(const std::string &attr) const
void decode_from(BER_Decoder &) override
AlternativeName(const std::string &email_addr="", const std::string &uri="", const std::string &dns="", const std::string &ip_address="")
void add_attribute(const std::string &type, const std::string &value)
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:290
BER_Object get_next_object()
Definition: ber_dec.cpp:237
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170
bool more_items() const
Definition: ber_dec.cpp:198
BER_Decoder & verify_end()
Definition: ber_dec.cpp:208
size_t length() const
Definition: asn1_obj.h:121
ASN1_Tag type() const
Definition: asn1_obj.h:116
const uint8_t * bits() const
Definition: asn1_obj.h:119
bool is_a(ASN1_Tag type_tag, ASN1_Tag class_tag) const
Definition: asn1_obj.cpp:71
ASN1_Tag get_class() const
Definition: asn1_obj.h:117
DER_Encoder & end_explicit()
Definition: der_enc.cpp:220
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:249
DER_Encoder & start_explicit(uint16_t type_tag)
Definition: der_enc.cpp:206
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:181
DER_Encoder & end_cons()
Definition: der_enc.cpp:191
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:285
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
Definition: alg_id.cpp:13
void store_be(uint16_t in, uint8_t out[2])
Definition: loadstor.h:438
uint32_t string_to_ipv4(const std::string &str)
Definition: parsing.cpp:253
uint32_t load_be< uint32_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:179
void multimap_insert(std::multimap< K, V > &multimap, const K &key, const V &value)
Definition: stl_util.h:76
ASN1_Tag
Definition: asn1_obj.h:25
@ CONSTRUCTED
Definition: asn1_obj.h:30
@ IA5_STRING
Definition: asn1_obj.h:49
@ SEQUENCE
Definition: asn1_obj.h:42
@ CONTEXT_SPECIFIC
Definition: asn1_obj.h:28
@ UNIVERSAL
Definition: asn1_obj.h:26
std::string ipv4_to_string(uint32_t ip)
Definition: parsing.cpp:278
MechanismType type