monotone

monotone Mtn Source Tree

Root/botan/asn1_alt.cpp

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

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status