Botan
1.11.15
|
00001 /* 00002 * AlternativeName 00003 * (C) 1999-2007 Jack Lloyd 00004 * 2007 Yves Jerschow 00005 * 00006 * Botan is released under the Simplified BSD License (see license.txt) 00007 */ 00008 00009 #include <botan/asn1_alt_name.h> 00010 #include <botan/der_enc.h> 00011 #include <botan/ber_dec.h> 00012 #include <botan/oids.h> 00013 #include <botan/internal/stl_util.h> 00014 #include <botan/charset.h> 00015 #include <botan/parsing.h> 00016 #include <botan/loadstor.h> 00017 00018 namespace Botan { 00019 00020 namespace { 00021 00022 /* 00023 * Check if type is a known ASN.1 string type 00024 */ 00025 bool is_string_type(ASN1_Tag tag) 00026 { 00027 return (tag == NUMERIC_STRING || 00028 tag == PRINTABLE_STRING || 00029 tag == VISIBLE_STRING || 00030 tag == T61_STRING || 00031 tag == IA5_STRING || 00032 tag == UTF8_STRING || 00033 tag == BMP_STRING); 00034 } 00035 00036 } 00037 00038 /* 00039 * Create an AlternativeName 00040 */ 00041 AlternativeName::AlternativeName(const std::string& email_addr, 00042 const std::string& uri, 00043 const std::string& dns, 00044 const std::string& ip) 00045 { 00046 add_attribute("RFC822", email_addr); 00047 add_attribute("DNS", dns); 00048 add_attribute("URI", uri); 00049 add_attribute("IP", ip); 00050 } 00051 00052 /* 00053 * Add an attribute to an alternative name 00054 */ 00055 void AlternativeName::add_attribute(const std::string& type, 00056 const std::string& str) 00057 { 00058 if(type == "" || str == "") 00059 return; 00060 00061 auto range = alt_info.equal_range(type); 00062 for(auto j = range.first; j != range.second; ++j) 00063 if(j->second == str) 00064 return; 00065 00066 multimap_insert(alt_info, type, str); 00067 } 00068 00069 /* 00070 * Add an OtherName field 00071 */ 00072 void AlternativeName::add_othername(const OID& oid, const std::string& value, 00073 ASN1_Tag type) 00074 { 00075 if(value == "") 00076 return; 00077 multimap_insert(othernames, oid, ASN1_String(value, type)); 00078 } 00079 00080 /* 00081 * Get the attributes of this alternative name 00082 */ 00083 std::multimap<std::string, std::string> AlternativeName::get_attributes() const 00084 { 00085 return alt_info; 00086 } 00087 00088 /* 00089 * Get the otherNames 00090 */ 00091 std::multimap<OID, ASN1_String> AlternativeName::get_othernames() const 00092 { 00093 return othernames; 00094 } 00095 00096 /* 00097 * Return all of the alternative names 00098 */ 00099 std::multimap<std::string, std::string> AlternativeName::contents() const 00100 { 00101 std::multimap<std::string, std::string> names; 00102 00103 for(auto i = alt_info.begin(); i != alt_info.end(); ++i) 00104 multimap_insert(names, i->first, i->second); 00105 00106 for(auto i = othernames.begin(); i != othernames.end(); ++i) 00107 multimap_insert(names, OIDS::lookup(i->first), i->second.value()); 00108 00109 return names; 00110 } 00111 00112 /* 00113 * Return if this object has anything useful 00114 */ 00115 bool AlternativeName::has_items() const 00116 { 00117 return (alt_info.size() > 0 || othernames.size() > 0); 00118 } 00119 00120 namespace { 00121 00122 /* 00123 * DER encode an AlternativeName entry 00124 */ 00125 void encode_entries(DER_Encoder& encoder, 00126 const std::multimap<std::string, std::string>& attr, 00127 const std::string& type, ASN1_Tag tagging) 00128 { 00129 auto range = attr.equal_range(type); 00130 00131 for(auto i = range.first; i != range.second; ++i) 00132 { 00133 if(type == "RFC822" || type == "DNS" || type == "URI") 00134 { 00135 ASN1_String asn1_string(i->second, IA5_STRING); 00136 encoder.add_object(tagging, CONTEXT_SPECIFIC, asn1_string.iso_8859()); 00137 } 00138 else if(type == "IP") 00139 { 00140 const u32bit ip = string_to_ipv4(i->second); 00141 byte ip_buf[4] = { 0 }; 00142 store_be(ip, ip_buf); 00143 encoder.add_object(tagging, CONTEXT_SPECIFIC, ip_buf, 4); 00144 } 00145 } 00146 } 00147 00148 } 00149 00150 /* 00151 * DER encode an AlternativeName extension 00152 */ 00153 void AlternativeName::encode_into(DER_Encoder& der) const 00154 { 00155 der.start_cons(SEQUENCE); 00156 00157 encode_entries(der, alt_info, "RFC822", ASN1_Tag(1)); 00158 encode_entries(der, alt_info, "DNS", ASN1_Tag(2)); 00159 encode_entries(der, alt_info, "URI", ASN1_Tag(6)); 00160 encode_entries(der, alt_info, "IP", ASN1_Tag(7)); 00161 00162 for(auto i = othernames.begin(); i != othernames.end(); ++i) 00163 { 00164 der.start_explicit(0) 00165 .encode(i->first) 00166 .start_explicit(0) 00167 .encode(i->second) 00168 .end_explicit() 00169 .end_explicit(); 00170 } 00171 00172 der.end_cons(); 00173 } 00174 00175 /* 00176 * Decode a BER encoded AlternativeName 00177 */ 00178 void AlternativeName::decode_from(BER_Decoder& source) 00179 { 00180 BER_Decoder names = source.start_cons(SEQUENCE); 00181 00182 while(names.more_items()) 00183 { 00184 BER_Object obj = names.get_next_object(); 00185 if((obj.class_tag != CONTEXT_SPECIFIC) && 00186 (obj.class_tag != (CONTEXT_SPECIFIC | CONSTRUCTED))) 00187 continue; 00188 00189 const ASN1_Tag tag = obj.type_tag; 00190 00191 if(tag == 0) 00192 { 00193 BER_Decoder othername(obj.value); 00194 00195 OID oid; 00196 othername.decode(oid); 00197 if(othername.more_items()) 00198 { 00199 BER_Object othername_value_outer = othername.get_next_object(); 00200 othername.verify_end(); 00201 00202 if(othername_value_outer.type_tag != ASN1_Tag(0) || 00203 othername_value_outer.class_tag != 00204 (CONTEXT_SPECIFIC | CONSTRUCTED) 00205 ) 00206 throw Decoding_Error("Invalid tags on otherName value"); 00207 00208 BER_Decoder othername_value_inner(othername_value_outer.value); 00209 00210 BER_Object value = othername_value_inner.get_next_object(); 00211 othername_value_inner.verify_end(); 00212 00213 const ASN1_Tag value_type = value.type_tag; 00214 00215 if(is_string_type(value_type) && value.class_tag == UNIVERSAL) 00216 add_othername(oid, ASN1::to_string(value), value_type); 00217 } 00218 } 00219 else if(tag == 1 || tag == 2 || tag == 6) 00220 { 00221 const std::string value = Charset::transcode(ASN1::to_string(obj), 00222 LATIN1_CHARSET, 00223 LOCAL_CHARSET); 00224 00225 if(tag == 1) add_attribute("RFC822", value); 00226 if(tag == 2) add_attribute("DNS", value); 00227 if(tag == 6) add_attribute("URI", value); 00228 } 00229 else if(tag == 7) 00230 { 00231 if(obj.value.size() == 4) 00232 { 00233 const u32bit ip = load_be<u32bit>(&obj.value[0], 0); 00234 add_attribute("IP", ipv4_to_string(ip)); 00235 } 00236 } 00237 00238 } 00239 } 00240 00241 }