Coverage Report

Created: 2020-06-30 13:58

/src/botan/src/lib/x509/asn1_alt_name.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* AlternativeName
3
* (C) 1999-2007 Jack Lloyd
4
*     2007 Yves Jerschow
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#include <botan/asn1_alt_name.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/parsing.h>
15
#include <botan/loadstor.h>
16
#include <botan/x509_dn.h>
17
18
#include <sstream>
19
20
namespace Botan {
21
22
/*
23
* Create an AlternativeName
24
*/
25
AlternativeName::AlternativeName(const std::string& email_addr,
26
                                 const std::string& uri,
27
                                 const std::string& dns,
28
                                 const std::string& ip)
29
35.2k
   {
30
35.2k
   add_attribute("RFC822", email_addr);
31
35.2k
   add_attribute("DNS", dns);
32
35.2k
   add_attribute("URI", uri);
33
35.2k
   add_attribute("IP", ip);
34
35.2k
   }
35
36
/*
37
* Add an attribute to an alternative name
38
*/
39
void AlternativeName::add_attribute(const std::string& type,
40
                                    const std::string& value)
41
159k
   {
42
159k
   if(type.empty() || value.empty())
43
141k
      return;
44
17.5k
45
17.5k
   auto range = m_alt_info.equal_range(type);
46
143k
   for(auto j = range.first; j != range.second; ++j)
47
129k
      if(j->second == value)
48
3.58k
         return;
49
17.5k
50
17.5k
   multimap_insert(m_alt_info, type, value);
51
13.9k
   }
52
53
/*
54
* Add an OtherName field
55
*/
56
void AlternativeName::add_othername(const OID& oid, const std::string& value,
57
                                    ASN1_Tag type)
58
1.35k
   {
59
1.35k
   if(value.empty())
60
42
      return;
61
1.31k
   multimap_insert(m_othernames, oid, ASN1_String(value, type));
62
1.31k
   }
63
64
/*
65
* Return all of the alternative names
66
*/
67
std::multimap<std::string, std::string> AlternativeName::contents() const
68
1.08k
   {
69
1.08k
   std::multimap<std::string, std::string> names;
70
1.08k
71
7.43k
   for(auto i = m_alt_info.begin(); i != m_alt_info.end(); ++i)
72
6.34k
      {
73
6.34k
      multimap_insert(names, i->first, i->second);
74
6.34k
      }
75
1.08k
76
1.93k
   for(auto i = m_othernames.begin(); i != m_othernames.end(); ++i)
77
848
      {
78
848
      multimap_insert(names, i->first.to_formatted_string(), i->second.value());
79
848
      }
80
1.08k
81
1.08k
   return names;
82
1.08k
   }
83
84
bool AlternativeName::has_field(const std::string& attr) const
85
0
   {
86
0
   auto range = m_alt_info.equal_range(attr);
87
0
   return (range.first != range.second);
88
0
   }
89
90
std::string AlternativeName::get_first_attribute(const std::string& attr) const
91
0
   {
92
0
   auto i = m_alt_info.lower_bound(attr);
93
0
   if(i != m_alt_info.end() && i->first == attr)
94
0
      return i->second;
95
0
96
0
   return "";
97
0
   }
98
99
std::vector<std::string> AlternativeName::get_attribute(const std::string& attr) const
100
0
   {
101
0
   std::vector<std::string> results;
102
0
   auto range = m_alt_info.equal_range(attr);
103
0
   for(auto i = range.first; i != range.second; ++i)
104
0
      results.push_back(i->second);
105
0
   return results;
106
0
   }
107
108
X509_DN AlternativeName::dn() const
109
0
   {
110
0
   X509_DN dn;
111
0
   auto range = m_alt_info.equal_range("DN");
112
0
113
0
   for(auto i = range.first; i != range.second; ++i)
114
0
      {
115
0
      std::istringstream strm(i->second);
116
0
      strm >> dn;
117
0
      }
118
0
119
0
   return dn;
120
0
   }
121
122
/*
123
* Return if this object has anything useful
124
*/
125
bool AlternativeName::has_items() const
126
0
   {
127
0
   return (m_alt_info.size() > 0 || m_othernames.size() > 0);
128
0
   }
129
130
namespace {
131
132
/*
133
* DER encode an AlternativeName entry
134
*/
135
void encode_entries(DER_Encoder& encoder,
136
                    const std::multimap<std::string, std::string>& attr,
137
                    const std::string& type, ASN1_Tag tagging)
138
0
   {
139
0
   auto range = attr.equal_range(type);
140
0
141
0
   for(auto i = range.first; i != range.second; ++i)
142
0
      {
143
0
      if(type == "RFC822" || type == "DNS" || type == "URI")
144
0
         {
145
0
         ASN1_String asn1_string(i->second, IA5_STRING);
146
0
         encoder.add_object(tagging, CONTEXT_SPECIFIC, asn1_string.value());
147
0
         }
148
0
      else if(type == "IP")
149
0
         {
150
0
         const uint32_t ip = string_to_ipv4(i->second);
151
0
         uint8_t ip_buf[4] = { 0 };
152
0
         store_be(ip, ip_buf);
153
0
         encoder.add_object(tagging, CONTEXT_SPECIFIC, ip_buf, 4);
154
0
         }
155
0
      else if (type == "DN")
156
0
         {
157
0
         std::stringstream ss(i->second);
158
0
         X509_DN dn;
159
0
         ss >> dn;
160
0
         encoder.encode(dn);
161
0
         }
162
0
      }
163
0
   }
164
165
}
166
167
/*
168
* DER encode an AlternativeName extension
169
*/
170
void AlternativeName::encode_into(DER_Encoder& der) const
171
0
   {
172
0
   der.start_cons(SEQUENCE);
173
0
174
0
   encode_entries(der, m_alt_info, "RFC822", ASN1_Tag(1));
175
0
   encode_entries(der, m_alt_info, "DNS", ASN1_Tag(2));
176
0
   encode_entries(der, m_alt_info, "DN", ASN1_Tag(4));
177
0
   encode_entries(der, m_alt_info, "URI", ASN1_Tag(6));
178
0
   encode_entries(der, m_alt_info, "IP", ASN1_Tag(7));
179
0
180
0
   for(auto i = m_othernames.begin(); i != m_othernames.end(); ++i)
181
0
      {
182
0
      der.start_explicit(0)
183
0
         .encode(i->first)
184
0
         .start_explicit(0)
185
0
            .encode(i->second)
186
0
         .end_explicit()
187
0
      .end_explicit();
188
0
      }
189
0
190
0
   der.end_cons();
191
0
   }
192
193
/*
194
* Decode a BER encoded AlternativeName
195
*/
196
void AlternativeName::decode_from(BER_Decoder& source)
197
4.62k
   {
198
4.62k
   BER_Decoder names = source.start_cons(SEQUENCE);
199
4.62k
200
4.62k
   // FIXME this is largely a duplication of GeneralName::decode_from
201
4.62k
202
41.9k
   while(names.more_items())
203
37.3k
      {
204
37.3k
      BER_Object obj = names.get_next_object();
205
37.3k
206
37.3k
      if(obj.is_a(0, CONTEXT_SPECIFIC))
207
2.05k
         {
208
2.05k
         BER_Decoder othername(obj);
209
2.05k
210
2.05k
         OID oid;
211
2.05k
         othername.decode(oid);
212
2.05k
         if(othername.more_items())
213
1.80k
            {
214
1.80k
            BER_Object othername_value_outer = othername.get_next_object();
215
1.80k
            othername.verify_end();
216
1.80k
217
1.80k
            if(othername_value_outer.is_a(0, ASN1_Tag(CONTEXT_SPECIFIC | CONSTRUCTED)) == false)
218
39
               throw Decoding_Error("Invalid tags on otherName value");
219
1.76k
220
1.76k
            BER_Decoder othername_value_inner(othername_value_outer);
221
1.76k
222
1.76k
            BER_Object value = othername_value_inner.get_next_object();
223
1.76k
            othername_value_inner.verify_end();
224
1.76k
225
1.76k
            if(ASN1_String::is_string_type(value.type()) && value.get_class() == UNIVERSAL)
226
1.35k
               {
227
1.35k
               add_othername(oid, ASN1::to_string(value), value.type());
228
1.35k
               }
229
1.76k
            }
230
2.05k
         }
231
37.3k
      if(obj.is_a(1, CONTEXT_SPECIFIC))
232
6.44k
         {
233
6.44k
         add_attribute("RFC822", ASN1::to_string(obj));
234
6.44k
         }
235
30.8k
      else if(obj.is_a(2, CONTEXT_SPECIFIC))
236
2.69k
         {
237
2.69k
         add_attribute("DNS", ASN1::to_string(obj));
238
2.69k
         }
239
28.1k
      else if(obj.is_a(6, CONTEXT_SPECIFIC))
240
2.58k
         {
241
2.58k
         add_attribute("URI", ASN1::to_string(obj));
242
2.58k
         }
243
25.5k
      else if(obj.is_a(4, ASN1_Tag(CONTEXT_SPECIFIC | CONSTRUCTED)))
244
3.98k
         {
245
3.98k
         BER_Decoder dec(obj);
246
3.98k
         X509_DN dn;
247
3.98k
         std::stringstream ss;
248
3.98k
249
3.98k
         dec.decode(dn);
250
3.98k
         ss << dn;
251
3.98k
252
3.98k
         add_attribute("DN", ss.str());
253
3.98k
         }
254
21.6k
      else if(obj.is_a(7, CONTEXT_SPECIFIC))
255
3.57k
         {
256
3.57k
         if(obj.length() == 4)
257
3.21k
            {
258
3.21k
            const uint32_t ip = load_be<uint32_t>(obj.bits(), 0);
259
3.21k
            add_attribute("IP", ipv4_to_string(ip));
260
3.21k
            }
261
3.57k
         }
262
37.3k
263
37.3k
      }
264
4.62k
   }
265
266
}