Botan 3.0.0-alpha0
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/internal/parsing.h>
15#include <botan/internal/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(const auto& name : m_alt_info)
71 {
72 multimap_insert(names, name.first, name.second);
73 }
74
75 for(const auto& othername : m_othernames)
76 {
77 multimap_insert(names,
78 othername.first.to_formatted_string(),
79 othername.second.value());
80 }
81
82 return names;
83 }
84
85bool AlternativeName::has_field(const std::string& attr) const
86 {
87 auto range = m_alt_info.equal_range(attr);
88 return (range.first != range.second);
89 }
90
91std::string AlternativeName::get_first_attribute(const std::string& attr) const
92 {
93 auto i = m_alt_info.lower_bound(attr);
94 if(i != m_alt_info.end() && i->first == attr)
95 return i->second;
96
97 return "";
98 }
99
100std::vector<std::string> AlternativeName::get_attribute(const std::string& attr) const
101 {
102 std::vector<std::string> results;
103 auto range = m_alt_info.equal_range(attr);
104 for(auto i = range.first; i != range.second; ++i)
105 results.push_back(i->second);
106 return results;
107 }
108
110 {
111 X509_DN dn;
112 auto range = m_alt_info.equal_range("DN");
113
114 for(auto i = range.first; i != range.second; ++i)
115 {
116 std::istringstream strm(i->second);
117 strm >> dn;
118 }
119
120 return dn;
121 }
122
123/*
124* Return if this object has anything useful
125*/
127 {
128 return (!m_alt_info.empty() || !m_othernames.empty());
129 }
130
131namespace {
132
133/*
134* DER encode an AlternativeName entry
135*/
136void encode_entries(DER_Encoder& encoder,
137 const std::multimap<std::string, std::string>& attr,
138 const std::string& type, ASN1_Type tagging)
139 {
140 auto range = attr.equal_range(type);
141
142 for(auto i = range.first; i != range.second; ++i)
143 {
144 if(type == "RFC822" || type == "DNS" || type == "URI")
145 {
146 ASN1_String asn1_string(i->second, ASN1_Type::Ia5String);
147 encoder.add_object(tagging, ASN1_Class::ContextSpecific, asn1_string.value());
148 }
149 else if(type == "IP")
150 {
151 const uint32_t ip = string_to_ipv4(i->second);
152 uint8_t ip_buf[4] = { 0 };
153 store_be(ip, ip_buf);
154 encoder.add_object(tagging, ASN1_Class::ContextSpecific, ip_buf, 4);
155 }
156 else if (type == "DN")
157 {
158 std::stringstream ss(i->second);
159 X509_DN dn;
160 ss >> dn;
161 encoder.encode(dn);
162 }
163 }
164 }
165
166}
167
168/*
169* DER encode an AlternativeName extension
170*/
172 {
173 der.start_sequence();
174
175 encode_entries(der, m_alt_info, "RFC822", ASN1_Type(1));
176 encode_entries(der, m_alt_info, "DNS", ASN1_Type(2));
177 encode_entries(der, m_alt_info, "DN", ASN1_Type(4));
178 encode_entries(der, m_alt_info, "URI", ASN1_Type(6));
179 encode_entries(der, m_alt_info, "IP", ASN1_Type(7));
180
181 for(const auto& othername : m_othernames)
182 {
183 der.start_explicit(0)
184 .encode(othername.first)
186 .encode(othername.second)
187 .end_explicit()
188 .end_explicit();
189 }
190
191 der.end_cons();
192 }
193
194/*
195* Decode a BER encoded AlternativeName
196*/
198 {
199 BER_Decoder names = source.start_sequence();
200
201 // FIXME this is largely a duplication of GeneralName::decode_from
202
203 while(names.more_items())
204 {
205 BER_Object obj = names.get_next_object();
206
208 {
209 BER_Decoder othername(obj);
210
211 OID oid;
212 othername.decode(oid);
213 if(othername.more_items())
214 {
215 BER_Object othername_value_outer = othername.get_next_object();
216 othername.verify_end();
217
218 if(othername_value_outer.is_a(0, ASN1_Class::ExplicitContextSpecific) == false)
219 throw Decoding_Error("Invalid tags on otherName value");
220
221 BER_Decoder othername_value_inner(othername_value_outer);
222
223 BER_Object value = othername_value_inner.get_next_object();
224 othername_value_inner.verify_end();
225
227 {
228 add_othername(oid, ASN1::to_string(value), value.type());
229 }
230 }
231 }
233 {
234 add_attribute("RFC822", ASN1::to_string(obj));
235 }
236 else if(obj.is_a(2, ASN1_Class::ContextSpecific))
237 {
238 add_attribute("DNS", ASN1::to_string(obj));
239 }
240 else if(obj.is_a(6, ASN1_Class::ContextSpecific))
241 {
242 add_attribute("URI", ASN1::to_string(obj));
243 }
245 {
246 BER_Decoder dec(obj);
247 X509_DN dn;
248 std::stringstream ss;
249
250 dec.decode(dn);
251 ss << dn;
252
253 add_attribute("DN", ss.str());
254 }
255 else if(obj.is_a(7, ASN1_Class::ContextSpecific))
256 {
257 if(obj.length() == 4)
258 {
259 const uint32_t ip = load_be<uint32_t>(obj.bits(), 0);
260 add_attribute("IP", ipv4_to_string(ip));
261 }
262 }
263
264 }
265 }
266
267}
static bool is_string_type(ASN1_Type 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
void add_othername(const OID &oid, const std::string &value, ASN1_Type type)
std::string get_first_attribute(const std::string &attr) const
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_Object get_next_object()
Definition: ber_dec.cpp:239
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:187
bool more_items() const
Definition: ber_dec.cpp:200
BER_Decoder & verify_end()
Definition: ber_dec.cpp:210
BER_Decoder start_sequence()
Definition: ber_dec.h:111
size_t length() const
Definition: asn1_obj.h:153
const uint8_t * bits() const
Definition: asn1_obj.h:151
bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const
Definition: asn1_obj.cpp:71
ASN1_Type type() const
Definition: asn1_obj.h:148
ASN1_Class get_class() const
Definition: asn1_obj.h:149
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:252
DER_Encoder & end_explicit()
Definition: der_enc.cpp:223
DER_Encoder & start_explicit(uint16_t type_tag)
Definition: der_enc.cpp:209
DER_Encoder & start_sequence()
Definition: der_enc.h:66
DER_Encoder & end_cons()
Definition: der_enc.cpp:194
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:288
std::string name
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:209
Definition: alg_id.cpp:13
ASN1_Type
Definition: asn1_obj.h:39
uint32_t string_to_ipv4(const std::string &str)
Definition: parsing.cpp:156
constexpr uint32_t load_be< uint32_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:190
constexpr void store_be(uint16_t in, uint8_t out[2])
Definition: loadstor.h:449
void multimap_insert(std::multimap< K, V > &multimap, const K &key, const V &value)
Definition: stl_util.h:79
std::string ipv4_to_string(uint32_t ip)
Definition: parsing.cpp:181
MechanismType type