Botan 1.10.17
asn1_alt.cpp
Go to the documentation of this file.
1/*
2* AlternativeName
3* (C) 1999-2007 Jack Lloyd
4* 2007 Yves Jerschow
5*
6* Distributed under the terms of the Botan license
7*/
8
9#include <botan/asn1_obj.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/charset.h>
15#include <botan/parsing.h>
16#include <botan/loadstor.h>
17
18namespace Botan {
19
20namespace {
21
22/*
23* Check if type is a known ASN.1 string type
24*/
25bool is_string_type(ASN1_Tag tag)
26 {
27 return (tag == NUMERIC_STRING ||
28 tag == PRINTABLE_STRING ||
29 tag == VISIBLE_STRING ||
30 tag == T61_STRING ||
31 tag == IA5_STRING ||
32 tag == UTF8_STRING ||
33 tag == BMP_STRING);
34 }
35
36}
37
38/*
39* Create an AlternativeName
40*/
41AlternativeName::AlternativeName(const std::string& email_addr,
42 const std::string& uri,
43 const std::string& dns,
44 const std::string& ip)
45 {
46 add_attribute("RFC822", email_addr);
47 add_attribute("DNS", dns);
48 add_attribute("URI", uri);
49 add_attribute("IP", ip);
50 }
51
52/*
53* Add an attribute to an alternative name
54*/
55void AlternativeName::add_attribute(const std::string& type,
56 const std::string& str)
57 {
58 if(type == "" || str == "")
59 return;
60
61 typedef std::multimap<std::string, std::string>::iterator iter;
62 std::pair<iter, iter> range = alt_info.equal_range(type);
63 for(iter j = range.first; j != range.second; ++j)
64 if(j->second == str)
65 return;
66
67 multimap_insert(alt_info, type, str);
68 }
69
70/*
71* Add an OtherName field
72*/
73void AlternativeName::add_othername(const OID& oid, const std::string& value,
74 ASN1_Tag type)
75 {
76 if(value == "")
77 return;
78 multimap_insert(othernames, oid, ASN1_String(value, type));
79 }
80
81/*
82* Get the attributes of this alternative name
83*/
84std::multimap<std::string, std::string> AlternativeName::get_attributes() const
85 {
86 return alt_info;
87 }
88
89/*
90* Get the otherNames
91*/
92std::multimap<OID, ASN1_String> AlternativeName::get_othernames() const
93 {
94 return othernames;
95 }
96
97/*
98* Return all of the alternative names
99*/
100std::multimap<std::string, std::string> AlternativeName::contents() const
101 {
102 std::multimap<std::string, std::string> names;
103
104 typedef std::multimap<std::string, std::string>::const_iterator rdn_iter;
105 for(rdn_iter j = alt_info.begin(); j != alt_info.end(); ++j)
106 multimap_insert(names, j->first, j->second);
107
108 typedef std::multimap<OID, ASN1_String>::const_iterator on_iter;
109 for(on_iter j = othernames.begin(); j != othernames.end(); ++j)
110 multimap_insert(names, OIDS::lookup(j->first), j->second.value());
111
112 return names;
113 }
114
115/*
116* Return if this object has anything useful
117*/
119 {
120 return (alt_info.size() > 0 || othernames.size() > 0);
121 }
122
123namespace {
124
125/*
126* DER encode an AlternativeName entry
127*/
128void encode_entries(DER_Encoder& encoder,
129 const std::multimap<std::string, std::string>& attr,
130 const std::string& type, ASN1_Tag tagging)
131 {
132 typedef std::multimap<std::string, std::string>::const_iterator iter;
133
134 std::pair<iter, iter> range = attr.equal_range(type);
135 for(iter j = range.first; j != range.second; ++j)
136 {
137 if(type == "RFC822" || type == "DNS" || type == "URI")
138 {
139 ASN1_String asn1_string(j->second, IA5_STRING);
140 encoder.add_object(tagging, CONTEXT_SPECIFIC, asn1_string.iso_8859());
141 }
142 else if(type == "IP")
143 {
144 const u32bit ip = string_to_ipv4(j->second);
145 byte ip_buf[4] = { 0 };
146 store_be(ip, ip_buf);
147 encoder.add_object(tagging, CONTEXT_SPECIFIC, ip_buf, 4);
148 }
149 }
150 }
151
152}
153
154/*
155* DER encode an AlternativeName extension
156*/
158 {
159 der.start_cons(SEQUENCE);
160
161 encode_entries(der, alt_info, "RFC822", ASN1_Tag(1));
162 encode_entries(der, alt_info, "DNS", ASN1_Tag(2));
163 encode_entries(der, alt_info, "URI", ASN1_Tag(6));
164 encode_entries(der, alt_info, "IP", ASN1_Tag(7));
165
166 std::multimap<OID, ASN1_String>::const_iterator i;
167 for(i = othernames.begin(); i != othernames.end(); ++i)
168 {
169 der.start_explicit(0)
170 .encode(i->first)
172 .encode(i->second)
173 .end_explicit()
174 .end_explicit();
175 }
176
177 der.end_cons();
178 }
179
180/*
181* Decode a BER encoded AlternativeName
182*/
184 {
185 BER_Decoder names = source.start_cons(SEQUENCE);
186
187 while(names.more_items())
188 {
189 BER_Object obj = names.get_next_object();
190 if((obj.class_tag != CONTEXT_SPECIFIC) &&
192 continue;
193
194 const ASN1_Tag tag = obj.type_tag;
195
196 if(tag == 0)
197 {
198 BER_Decoder othername(obj.value);
199
200 OID oid;
201 othername.decode(oid);
202 if(othername.more_items())
203 {
204 BER_Object othername_value_outer = othername.get_next_object();
205 othername.verify_end();
206
207 if(othername_value_outer.type_tag != ASN1_Tag(0) ||
208 othername_value_outer.class_tag !=
210 )
211 throw Decoding_Error("Invalid tags on otherName value");
212
213 BER_Decoder othername_value_inner(othername_value_outer.value);
214
215 BER_Object value = othername_value_inner.get_next_object();
216 othername_value_inner.verify_end();
217
218 const ASN1_Tag value_type = value.type_tag;
219
220 if(is_string_type(value_type) && value.class_tag == UNIVERSAL)
221 add_othername(oid, ASN1::to_string(value), value_type);
222 }
223 }
224 else if(tag == 1 || tag == 2 || tag == 6)
225 {
226 const std::string value = Charset::transcode(ASN1::to_string(obj),
229
230 if(tag == 1) add_attribute("RFC822", value);
231 if(tag == 2) add_attribute("DNS", value);
232 if(tag == 6) add_attribute("URI", value);
233 }
234 else if(tag == 7)
235 {
236 if(obj.value.size() == 4)
237 {
238 const u32bit ip = load_be<u32bit>(&obj.value[0], 0);
239 add_attribute("IP", ipv4_to_string(ip));
240 }
241 }
242
243 }
244 }
245
246}
ASN1_String(const std::string &="")
Definition asn1_str.cpp:86
void encode_into(class DER_Encoder &) const
Definition asn1_alt.cpp:157
std::multimap< std::string, std::string > get_attributes() const
Definition asn1_alt.cpp:84
std::multimap< std::string, std::string > contents() const
Definition asn1_alt.cpp:100
bool has_items() const
Definition asn1_alt.cpp:118
void add_othername(const OID &, const std::string &, ASN1_Tag)
Definition asn1_alt.cpp:73
void add_attribute(const std::string &, const std::string &)
Definition asn1_alt.cpp:55
std::multimap< OID, ASN1_String > get_othernames() const
Definition asn1_alt.cpp:92
void decode_from(class BER_Decoder &)
Definition asn1_alt.cpp:183
AlternativeName(const std::string &="", const std::string &="", const std::string &="", const std::string &="")
Definition asn1_alt.cpp:41
BER_Object get_next_object()
Definition ber_dec.cpp:196
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition ber_dec.cpp:238
bool more_items() const
Definition ber_dec.cpp:153
BER_Decoder & decode(bool &)
Definition ber_dec.cpp:344
BER_Decoder & verify_end()
Definition ber_dec.cpp:163
SecureVector< byte > value
Definition asn1_int.h:83
ASN1_Tag class_tag
Definition asn1_int.h:82
ASN1_Tag type_tag
Definition asn1_int.h:82
DER_Encoder & end_explicit()
Definition der_enc.cpp:172
DER_Encoder & start_explicit(u16bit type_tag)
Definition der_enc.cpp:159
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition der_enc.cpp:346
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition der_enc.cpp:135
DER_Encoder & end_cons()
Definition der_enc.cpp:145
DER_Encoder & encode(bool b)
Definition der_enc.cpp:209
size_t size() const
Definition secmem.h:29
std::string to_string(const BER_Object &obj)
Definition asn1_int.cpp:46
std::string transcode(const std::string &str, Character_Set to, Character_Set from)
Definition charset.cpp:103
std::string lookup(const OID &oid)
Definition oids.cpp:31
std::string ipv4_to_string(u32bit ip)
Definition parsing.cpp:279
@ LOCAL_CHARSET
Definition charset.h:20
@ LATIN1_CHARSET
Definition charset.h:23
u32bit string_to_ipv4(const std::string &str)
Definition parsing.cpp:254
T load_be(const byte in[], size_t off)
Definition loadstor.h:100
ASN1_Tag
Definition asn1_int.h:19
@ CONSTRUCTED
Definition asn1_int.h:25
@ IA5_STRING
Definition asn1_int.h:42
@ PRINTABLE_STRING
Definition asn1_int.h:40
@ T61_STRING
Definition asn1_int.h:41
@ UTF8_STRING
Definition asn1_int.h:38
@ SEQUENCE
Definition asn1_int.h:35
@ NUMERIC_STRING
Definition asn1_int.h:39
@ CONTEXT_SPECIFIC
Definition asn1_int.h:22
@ BMP_STRING
Definition asn1_int.h:44
@ UNIVERSAL
Definition asn1_int.h:20
@ VISIBLE_STRING
Definition asn1_int.h:43
void multimap_insert(std::multimap< K, V > &multimap, const K &key, const V &value)
Definition stl_util.h:76
unsigned int u32bit
Definition types.h:32
void store_be(u16bit in, byte out[2])
Definition loadstor.h:412
Decoding_Error(const std::string &name)
Definition exceptn.h:140