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
18 namespace Botan {
19
20 namespace {
21
22 /*
23 * Check if type is a known ASN.1 string type
24 */
25 bool 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 */
41 AlternativeName::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 */
55 void 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 */
73 void 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 */
84 std::multimap<std::string, std::string> AlternativeName::get_attributes() const
85 {
86 return alt_info;
87 }
88
89 /*
90 * Get the otherNames
91 */
92 std::multimap<OID, ASN1_String> AlternativeName::get_othernames() const
93 {
94 return othernames;
95 }
96
97 /*
98 * Return all of the alternative names
99 */
100 std::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 */
118 bool AlternativeName::has_items() const
119 {
120 return (alt_info.size() > 0 || othernames.size() > 0);
121 }
122
123 namespace {
124
125 /*
126 * DER encode an AlternativeName entry
127 */
128 void 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 */
157 void AlternativeName::encode_into(DER_Encoder& der) const
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)
171 .start_explicit(0)
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 */
183 void AlternativeName::decode_from(BER_Decoder& source)
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) &&
191 (obj.class_tag != (CONTEXT_SPECIFIC | CONSTRUCTED)))
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 !=
209 (CONTEXT_SPECIFIC | CONSTRUCTED)
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),
227 LATIN1_CHARSET,
228 LOCAL_CHARSET);
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 }
Botan::BMP_STRING
Definition: asn1_int.h:44
Botan::AlternativeName::add_attribute
void add_attribute(const std::string &, const std::string &)
Definition: asn1_alt.cpp:55
Botan::CONTEXT_SPECIFIC
Definition: asn1_int.h:22
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
Botan::AlternativeName::get_attributes
std::multimap< std::string, std::string > get_attributes() const
Definition: asn1_alt.cpp:84
Botan::AlternativeName::contents
std::multimap< std::string, std::string > contents() const
Definition: asn1_alt.cpp:100
Botan::ipv4_to_string
std::string ipv4_to_string(u32bit ip)
Definition: parsing.cpp:279
Botan::AlternativeName::encode_into
void encode_into(class DER_Encoder &) const
Definition: asn1_alt.cpp:157
Botan::DER_Encoder::end_explicit
DER_Encoder & end_explicit()
Definition: der_enc.cpp:172
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_int.cpp:46
Botan::PRINTABLE_STRING
Definition: asn1_int.h:40
Botan::DER_Encoder::start_explicit
DER_Encoder & start_explicit(u16bit type_tag)
Definition: der_enc.cpp:159
Botan::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:145
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:238
Botan::ASN1_String
Definition: asn1_str.h:18
Botan::UNIVERSAL
Definition: asn1_int.h:20
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::AlternativeName::decode_from
void decode_from(class BER_Decoder &)
Definition: asn1_alt.cpp:183
Botan::CONSTRUCTED
Definition: asn1_int.h:25
Botan::Charset::transcode
std::string transcode(const std::string &str, Character_Set to, Character_Set from)
Definition: charset.cpp:103
Botan::T61_STRING
Definition: asn1_int.h:41
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::ASN1_Tag
ASN1_Tag
Definition: asn1_int.h:19
Botan::LATIN1_CHARSET
Definition: charset.h:23
Botan::Decoding_Error
Definition: exceptn.h:138
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan
Definition: algo_base.h:14
Botan::BER_Object::value
SecureVector< byte > value
Definition: asn1_int.h:83
Botan::load_be< u32bit >
u32bit load_be< u32bit >(const byte in[], size_t off)
Definition: loadstor.h:166
Botan::AlternativeName::add_othername
void add_othername(const OID &, const std::string &, ASN1_Tag)
Definition: asn1_alt.cpp:73
Botan::BER_Decoder::get_next_object
BER_Object get_next_object()
Definition: ber_dec.cpp:196
Botan::VISIBLE_STRING
Definition: asn1_int.h:43
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::AlternativeName::AlternativeName
AlternativeName(const std::string &="", const std::string &="", const std::string &="", const std::string &="")
Definition: asn1_alt.cpp:41
Botan::BER_Object::class_tag
ASN1_Tag class_tag
Definition: asn1_int.h:82
Botan::LOCAL_CHARSET
Definition: charset.h:20
Botan::AlternativeName::get_othernames
std::multimap< OID, ASN1_String > get_othernames() const
Definition: asn1_alt.cpp:92
Botan::BER_Object::type_tag
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::store_be
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
Botan::BER_Decoder::verify_end
BER_Decoder & verify_end()
Definition: ber_dec.cpp:163
Botan::NUMERIC_STRING
Definition: asn1_int.h:39
Botan::UTF8_STRING
Definition: asn1_int.h:38
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
Botan::AlternativeName::has_items
bool has_items() const
Definition: asn1_alt.cpp:118
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::more_items
bool more_items() const
Definition: ber_dec.cpp:153
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::BER_Object
Definition: asn1_int.h:77
Botan::IA5_STRING
Definition: asn1_int.h:42
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition: der_enc.cpp:346
Botan::u32bit
unsigned int u32bit
Definition: types.h:32
oid
OID oid
Definition: x509_ext.cpp:446
Botan::string_to_ipv4
u32bit string_to_ipv4(const std::string &str)
Definition: parsing.cpp:254