1
/*************************************************
2
* AlternativeName Source File *
3
* (C) 1999-2007 The Botan Project *
4
*************************************************/
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>
15
/*************************************************
16
* Create an AlternativeName *
17
*************************************************/
18
AlternativeName::AlternativeName(const std::string& email_addr,
19
const std::string& uri,
20
const std::string& dns)
22
add_attribute("RFC822", email_addr);
23
add_attribute("DNS", dns);
24
add_attribute("URI", uri);
27
/*************************************************
28
* Add an attribute to an alternative name *
29
*************************************************/
30
void AlternativeName::add_attribute(const std::string& type,
31
const std::string& str)
33
if(type == "" || str == "")
36
typedef std::multimap<std::string, std::string>::iterator iter;
37
std::pair<iter, iter> range = alt_info.equal_range(type);
38
for(iter j = range.first; j != range.second; ++j)
42
multimap_insert(alt_info, type, str);
45
/*************************************************
46
* Add an OtherName field *
47
*************************************************/
48
void AlternativeName::add_othername(const OID& oid, const std::string& value,
53
multimap_insert(othernames, oid, ASN1_String(value, type));
56
/*************************************************
57
* Get the attributes of this alternative name *
58
*************************************************/
59
std::multimap<std::string, std::string> AlternativeName::get_attributes() const
64
/*************************************************
65
* Get the otherNames *
66
*************************************************/
67
std::multimap<OID, ASN1_String> AlternativeName::get_othernames() const
72
/*************************************************
73
* Return all of the alternative names *
74
*************************************************/
75
std::multimap<std::string, std::string> AlternativeName::contents() const
77
std::multimap<std::string, std::string> names;
79
typedef std::multimap<std::string, std::string>::const_iterator rdn_iter;
80
for(rdn_iter j = alt_info.begin(); j != alt_info.end(); ++j)
81
multimap_insert(names, j->first, j->second);
83
typedef std::multimap<OID, ASN1_String>::const_iterator on_iter;
84
for(on_iter j = othernames.begin(); j != othernames.end(); ++j)
85
multimap_insert(names, OIDS::lookup(j->first), j->second.value());
90
/*************************************************
91
* Return if this object has anything useful *
92
*************************************************/
93
bool AlternativeName::has_items() const
95
return (alt_info.size() > 0 || othernames.size() > 0);
100
/*************************************************
101
* DER encode an AlternativeName entry *
102
*************************************************/
103
void encode_entries(DER_Encoder& encoder,
104
const std::multimap<std::string, std::string>& attr,
105
const std::string& type, ASN1_Tag tagging)
107
typedef std::multimap<std::string, std::string>::const_iterator iter;
109
std::pair<iter, iter> range = attr.equal_range(type);
110
for(iter j = range.first; j != range.second; ++j)
112
ASN1_String asn1_string(j->second, IA5_STRING);
113
encoder.add_object(tagging, CONTEXT_SPECIFIC, asn1_string.iso_8859());
119
/*************************************************
120
* DER encode an AlternativeName extension *
121
*************************************************/
122
void AlternativeName::encode_into(DER_Encoder& der) const
124
der.start_cons(SEQUENCE);
126
encode_entries(der, alt_info, "RFC822", ASN1_Tag(1));
127
encode_entries(der, alt_info, "DNS", ASN1_Tag(2));
128
encode_entries(der, alt_info, "URI", ASN1_Tag(6));
130
std::multimap<OID, ASN1_String>::const_iterator i;
131
for(i = othernames.begin(); i != othernames.end(); ++i)
133
der.start_explicit(0)
144
/*************************************************
145
* Decode a BER encoded AlternativeName *
146
*************************************************/
147
void AlternativeName::decode_from(BER_Decoder& source)
149
BER_Decoder names = source.start_cons(SEQUENCE);
151
while(names.more_items())
153
BER_Object obj = names.get_next_object();
154
if((obj.class_tag != CONTEXT_SPECIFIC) &&
155
(obj.class_tag != (CONTEXT_SPECIFIC | CONSTRUCTED)))
158
ASN1_Tag tag = obj.type_tag;
162
BER_Decoder othername(obj.value);
165
othername.decode(oid);
166
if(othername.more_items())
168
BER_Object othername_value_outer = othername.get_next_object();
169
othername.verify_end();
171
if(othername_value_outer.type_tag != ASN1_Tag(0) ||
172
othername_value_outer.class_tag !=
173
(CONTEXT_SPECIFIC | CONSTRUCTED)
175
throw Decoding_Error("Invalid tags on otherName value");
177
BER_Decoder othername_value_inner(othername_value_outer.value);
179
BER_Object value = othername_value_inner.get_next_object();
180
othername_value_inner.verify_end();
182
ASN1_Tag value_type = value.type_tag;
184
if(is_string_type(value_type) && value.class_tag == UNIVERSAL)
185
add_othername(oid, ASN1::to_string(value), value_type);
188
else if(tag == 1 || tag == 2 || tag == 6)
190
const std::string value = Charset::transcode(ASN1::to_string(obj),
194
if(tag == 1) add_attribute("RFC822", value);
195
if(tag == 2) add_attribute("DNS", value);
196
if(tag == 6) add_attribute("URI", value);