Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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