Coverage Report

Created: 2020-05-23 13:54

/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
30.9k
   {
30
30.9k
   add_attribute("RFC822", email_addr);
31
30.9k
   add_attribute("DNS", dns);
32
30.9k
   add_attribute("URI", uri);
33
30.9k
   add_attribute("IP", ip);
34
30.9k
   }
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
138k
   {
42
138k
   if(type.empty() || value.empty())
43
124k
      return;
44
14.2k
45
14.2k
   auto range = m_alt_info.equal_range(type);
46
115k
   for(auto j = range.first; j != range.second; ++j)
47
103k
      if(j->second == value)
48
2.49k
         return;
49
14.2k
50
14.2k
   multimap_insert(m_alt_info, type, value);
51
11.7k
   }
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.10k
   {
59
1.10k
   if(value.empty())
60
45
      return;
61
1.06k
   multimap_insert(m_othernames, oid, ASN1_String(value, type));
62
1.06k
   }
63
64
/*
65
* Return all of the alternative names
66
*/
67
std::multimap<std::string, std::string> AlternativeName::contents() const
68
922
   {
69
922
   std::multimap<std::string, std::string> names;
70
922
71
6.03k
   for(auto i = m_alt_info.begin(); i != m_alt_info.end(); ++i)
72
5.11k
      {
73
5.11k
      multimap_insert(names, i->first, i->second);
74
5.11k
      }
75
922
76
1.53k
   for(auto i = m_othernames.begin(); i != m_othernames.end(); ++i)
77
616
      {
78
616
      multimap_insert(names, i->first.to_formatted_string(), i->second.value());
79
616
      }
80
922
81
922
   return names;
82
922
   }
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
/*
109
* Return if this object has anything useful
110
*/
111
bool AlternativeName::has_items() const
112
0
   {
113
0
   return (m_alt_info.size() > 0 || m_othernames.size() > 0);
114
0
   }
115
116
namespace {
117
118
/*
119
* DER encode an AlternativeName entry
120
*/
121
void encode_entries(DER_Encoder& encoder,
122
                    const std::multimap<std::string, std::string>& attr,
123
                    const std::string& type, ASN1_Tag tagging)
124
0
   {
125
0
   auto range = attr.equal_range(type);
126
0
127
0
   for(auto i = range.first; i != range.second; ++i)
128
0
      {
129
0
      if(type == "RFC822" || type == "DNS" || type == "URI")
130
0
         {
131
0
         ASN1_String asn1_string(i->second, IA5_STRING);
132
0
         encoder.add_object(tagging, CONTEXT_SPECIFIC, asn1_string.value());
133
0
         }
134
0
      else if(type == "IP")
135
0
         {
136
0
         const uint32_t ip = string_to_ipv4(i->second);
137
0
         uint8_t ip_buf[4] = { 0 };
138
0
         store_be(ip, ip_buf);
139
0
         encoder.add_object(tagging, CONTEXT_SPECIFIC, ip_buf, 4);
140
0
         }
141
0
      else if (type == "DN")
142
0
         {
143
0
         std::stringstream ss(i->second);
144
0
         X509_DN dn;
145
0
         ss >> dn;
146
0
         encoder.encode(dn);
147
0
         }
148
0
      }
149
0
   }
150
151
}
152
153
/*
154
* DER encode an AlternativeName extension
155
*/
156
void AlternativeName::encode_into(DER_Encoder& der) const
157
0
   {
158
0
   der.start_cons(SEQUENCE);
159
0
160
0
   encode_entries(der, m_alt_info, "RFC822", ASN1_Tag(1));
161
0
   encode_entries(der, m_alt_info, "DNS", ASN1_Tag(2));
162
0
   encode_entries(der, m_alt_info, "DN", ASN1_Tag(4));
163
0
   encode_entries(der, m_alt_info, "URI", ASN1_Tag(6));
164
0
   encode_entries(der, m_alt_info, "IP", ASN1_Tag(7));
165
0
166
0
   for(auto i = m_othernames.begin(); i != m_othernames.end(); ++i)
167
0
      {
168
0
      der.start_explicit(0)
169
0
         .encode(i->first)
170
0
         .start_explicit(0)
171
0
            .encode(i->second)
172
0
         .end_explicit()
173
0
      .end_explicit();
174
0
      }
175
0
176
0
   der.end_cons();
177
0
   }
178
179
/*
180
* Decode a BER encoded AlternativeName
181
*/
182
void AlternativeName::decode_from(BER_Decoder& source)
183
4.18k
   {
184
4.18k
   BER_Decoder names = source.start_cons(SEQUENCE);
185
4.18k
186
4.18k
   // FIXME this is largely a duplication of GeneralName::decode_from
187
4.18k
188
35.3k
   while(names.more_items())
189
31.1k
      {
190
31.1k
      BER_Object obj = names.get_next_object();
191
31.1k
192
31.1k
      if(obj.is_a(0, CONTEXT_SPECIFIC))
193
1.70k
         {
194
1.70k
         BER_Decoder othername(obj);
195
1.70k
196
1.70k
         OID oid;
197
1.70k
         othername.decode(oid);
198
1.70k
         if(othername.more_items())
199
1.49k
            {
200
1.49k
            BER_Object othername_value_outer = othername.get_next_object();
201
1.49k
            othername.verify_end();
202
1.49k
203
1.49k
            if(othername_value_outer.is_a(0, ASN1_Tag(CONTEXT_SPECIFIC | CONSTRUCTED)) == false)
204
37
               throw Decoding_Error("Invalid tags on otherName value");
205
1.46k
206
1.46k
            BER_Decoder othername_value_inner(othername_value_outer);
207
1.46k
208
1.46k
            BER_Object value = othername_value_inner.get_next_object();
209
1.46k
            othername_value_inner.verify_end();
210
1.46k
211
1.46k
            if(ASN1_String::is_string_type(value.type()) && value.get_class() == UNIVERSAL)
212
1.10k
               {
213
1.10k
               add_othername(oid, ASN1::to_string(value), value.type());
214
1.10k
               }
215
1.46k
            }
216
1.70k
         }
217
31.1k
      if(obj.is_a(1, CONTEXT_SPECIFIC))
218
5.31k
         {
219
5.31k
         add_attribute("RFC822", ASN1::to_string(obj));
220
5.31k
         }
221
25.8k
      else if(obj.is_a(2, CONTEXT_SPECIFIC))
222
1.94k
         {
223
1.94k
         add_attribute("DNS", ASN1::to_string(obj));
224
1.94k
         }
225
23.8k
      else if(obj.is_a(6, CONTEXT_SPECIFIC))
226
2.15k
         {
227
2.15k
         add_attribute("URI", ASN1::to_string(obj));
228
2.15k
         }
229
21.7k
      else if(obj.is_a(4, ASN1_Tag(CONTEXT_SPECIFIC | CONSTRUCTED)))
230
3.41k
         {
231
3.41k
         BER_Decoder dec(obj);
232
3.41k
         X509_DN dn;
233
3.41k
         std::stringstream ss;
234
3.41k
235
3.41k
         dec.decode(dn);
236
3.41k
         ss << dn;
237
3.41k
238
3.41k
         add_attribute("DN", ss.str());
239
3.41k
         }
240
18.3k
      else if(obj.is_a(7, CONTEXT_SPECIFIC))
241
2.82k
         {
242
2.82k
         if(obj.length() == 4)
243
2.51k
            {
244
2.51k
            const uint32_t ip = load_be<uint32_t>(obj.bits(), 0);
245
2.51k
            add_attribute("IP", ipv4_to_string(ip));
246
2.51k
            }
247
2.82k
         }
248
31.1k
249
31.1k
      }
250
4.18k
   }
251
252
}