Botan  2.6.0
Crypto and TLS for C++11
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  multimap_insert(names, i->first, i->second);
73 
74  for(auto i = m_othernames.begin(); i != m_othernames.end(); ++i)
75  multimap_insert(names, OIDS::lookup(i->first), i->second.value());
76 
77  return names;
78  }
79 
80 bool AlternativeName::has_field(const std::string& attr) const
81  {
82  auto range = m_alt_info.equal_range(attr);
83  return (range.first != range.second);
84  }
85 
86 std::string AlternativeName::get_first_attribute(const std::string& attr) const
87  {
88  auto i = m_alt_info.lower_bound(attr);
89  if(i != m_alt_info.end() && i->first == attr)
90  return i->second;
91 
92  return "";
93  }
94 
95 std::vector<std::string> AlternativeName::get_attribute(const std::string& attr) const
96  {
97  std::vector<std::string> results;
98  auto range = m_alt_info.equal_range(attr);
99  for(auto i = range.first; i != range.second; ++i)
100  results.push_back(i->second);
101  return results;
102  }
103 
104 /*
105 * Return if this object has anything useful
106 */
108  {
109  return (m_alt_info.size() > 0 || m_othernames.size() > 0);
110  }
111 
112 namespace {
113 
114 /*
115 * DER encode an AlternativeName entry
116 */
117 void encode_entries(DER_Encoder& encoder,
118  const std::multimap<std::string, std::string>& attr,
119  const std::string& type, ASN1_Tag tagging)
120  {
121  auto range = attr.equal_range(type);
122 
123  for(auto i = range.first; i != range.second; ++i)
124  {
125  if(type == "RFC822" || type == "DNS" || type == "URI")
126  {
127  ASN1_String asn1_string(i->second, IA5_STRING);
128  encoder.add_object(tagging, CONTEXT_SPECIFIC, asn1_string.value());
129  }
130  else if(type == "IP")
131  {
132  const uint32_t ip = string_to_ipv4(i->second);
133  uint8_t ip_buf[4] = { 0 };
134  store_be(ip, ip_buf);
135  encoder.add_object(tagging, CONTEXT_SPECIFIC, ip_buf, 4);
136  }
137  else if (type == "DN")
138  {
139  std::stringstream ss(i->second);
140  X509_DN dn;
141  ss >> dn;
142  encoder.encode(dn);
143  }
144  }
145  }
146 
147 }
148 
149 /*
150 * DER encode an AlternativeName extension
151 */
153  {
154  der.start_cons(SEQUENCE);
155 
156  encode_entries(der, m_alt_info, "RFC822", ASN1_Tag(1));
157  encode_entries(der, m_alt_info, "DNS", ASN1_Tag(2));
158  encode_entries(der, m_alt_info, "DN", ASN1_Tag(4));
159  encode_entries(der, m_alt_info, "URI", ASN1_Tag(6));
160  encode_entries(der, m_alt_info, "IP", ASN1_Tag(7));
161 
162  for(auto i = m_othernames.begin(); i != m_othernames.end(); ++i)
163  {
164  der.start_explicit(0)
165  .encode(i->first)
166  .start_explicit(0)
167  .encode(i->second)
168  .end_explicit()
169  .end_explicit();
170  }
171 
172  der.end_cons();
173  }
174 
175 /*
176 * Decode a BER encoded AlternativeName
177 */
179  {
180  BER_Decoder names = source.start_cons(SEQUENCE);
181 
182  // FIXME this is largely a duplication of GeneralName::decode_from
183 
184  while(names.more_items())
185  {
186  BER_Object obj = names.get_next_object();
187 
188  if(obj.is_a(0, CONTEXT_SPECIFIC))
189  {
190  BER_Decoder othername(obj);
191 
192  OID oid;
193  othername.decode(oid);
194  if(othername.more_items())
195  {
196  BER_Object othername_value_outer = othername.get_next_object();
197  othername.verify_end();
198 
199  if(othername_value_outer.is_a(0, ASN1_Tag(CONTEXT_SPECIFIC | CONSTRUCTED)) == false)
200  throw Decoding_Error("Invalid tags on otherName value");
201 
202  BER_Decoder othername_value_inner(othername_value_outer);
203 
204  BER_Object value = othername_value_inner.get_next_object();
205  othername_value_inner.verify_end();
206 
207  if(ASN1_String::is_string_type(value.type()) && value.get_class() == UNIVERSAL)
208  {
209  add_othername(oid, ASN1::to_string(value), value.type());
210  }
211  }
212  }
213  if(obj.is_a(1, CONTEXT_SPECIFIC))
214  {
215  add_attribute("RFC822", ASN1::to_string(obj));
216  }
217  else if(obj.is_a(2, CONTEXT_SPECIFIC))
218  {
219  add_attribute("DNS", ASN1::to_string(obj));
220  }
221  else if(obj.is_a(6, CONTEXT_SPECIFIC))
222  {
223  add_attribute("URI", ASN1::to_string(obj));
224  }
225  else if(obj.is_a(4, ASN1_Tag(CONTEXT_SPECIFIC | CONSTRUCTED)))
226  {
227  BER_Decoder dec(obj);
228  X509_DN dn;
229  std::stringstream ss;
230 
231  dec.decode(dn);
232  ss << dn;
233 
234  add_attribute("DN", ss.str());
235  }
236  else if(obj.is_a(7, CONTEXT_SPECIFIC))
237  {
238  if(obj.length() == 4)
239  {
240  const uint32_t ip = load_be<uint32_t>(obj.bits(), 0);
241  add_attribute("IP", ipv4_to_string(ip));
242  }
243  }
244 
245  }
246  }
247 
248 }
void encode_into(class DER_Encoder &) const override
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:347
static bool is_string_type(ASN1_Tag tag)
Definition: asn1_str.cpp:68
void add_othername(const OID &oid, const std::string &value, ASN1_Tag type)
void store_be(uint16_t in, uint8_t out[2])
Definition: loadstor.h:434
bool is_a(ASN1_Tag type_tag, ASN1_Tag class_tag) const
Definition: asn1_obj.cpp:31
void add_attribute(const std::string &type, const std::string &value)
uint32_t load_be< uint32_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:177
std::multimap< std::string, std::string > contents() const
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:338
DER_Encoder & end_explicit()
Definition: der_enc.cpp:173
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:145
ASN1_Tag
Definition: asn1_obj.h:22
MechanismType type
DER_Encoder & end_cons()
Definition: der_enc.cpp:146
ASN1_Tag type() const
Definition: asn1_obj.h:97
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202
ASN1_Tag get_class() const
Definition: asn1_obj.h:98
std::string ipv4_to_string(uint32_t ip)
Definition: parsing.cpp:294
std::vector< std::string > get_attribute(const std::string &attr) const
AlternativeName(const std::string &email_addr="", const std::string &uri="", const std::string &dns="", const std::string &ip_address="")
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:239
size_t length() const
Definition: asn1_obj.h:102
Definition: alg_id.cpp:13
uint32_t string_to_ipv4(const std::string &str)
Definition: parsing.cpp:269
BER_Object get_next_object()
Definition: ber_dec.cpp:184
const uint8_t * bits() const
Definition: asn1_obj.h:100
std::string get_first_attribute(const std::string &attr) const
BER_Decoder & verify_end()
Definition: ber_dec.cpp:163
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:136
void multimap_insert(std::multimap< K, V > &multimap, const K &key, const V &value)
Definition: stl_util.h:76
bool has_field(const std::string &attr) const
bool more_items() const
Definition: ber_dec.cpp:153
std::string lookup(const OID &oid)
Definition: oids.cpp:113
void decode_from(class BER_Decoder &) override
DER_Encoder & start_explicit(uint16_t type_tag)
Definition: der_enc.cpp:160