Coverage Report

Created: 2020-02-14 15:38

/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.0k
   {
30
35.0k
   add_attribute("RFC822", email_addr);
31
35.0k
   add_attribute("DNS", dns);
32
35.0k
   add_attribute("URI", uri);
33
35.0k
   add_attribute("IP", ip);
34
35.0k
   }
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
157k
   {
42
157k
   if(type.empty() || value.empty())
43
140k
      return;
44
17.1k
45
17.1k
   auto range = m_alt_info.equal_range(type);
46
137k
   for(auto j = range.first; j != range.second; ++j)
47
124k
      if(j->second == value)
48
3.29k
         return;
49
17.1k
50
17.1k
   multimap_insert(m_alt_info, type, value);
51
13.8k
   }
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.39k
   {
59
1.39k
   if(value.empty())
60
40
      return;
61
1.35k
   multimap_insert(m_othernames, oid, ASN1_String(value, type));
62
1.35k
   }
63
64
/*
65
* Return all of the alternative names
66
*/
67
std::multimap<std::string, std::string> AlternativeName::contents() const
68
1.05k
   {
69
1.05k
   std::multimap<std::string, std::string> names;
70
1.05k
71
7.36k
   for(auto i = m_alt_info.begin(); i != m_alt_info.end(); ++i)
72
6.31k
      {
73
6.31k
      multimap_insert(names, i->first, i->second);
74
6.31k
      }
75
1.05k
76
1.86k
   for(auto i = m_othernames.begin(); i != m_othernames.end(); ++i)
77
818
      {
78
818
      multimap_insert(names, i->first.to_formatted_string(), i->second.value());
79
818
      }
80
1.05k
81
1.05k
   return names;
82
1.05k
   }
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.43k
   {
184
4.43k
   BER_Decoder names = source.start_cons(SEQUENCE);
185
4.43k
186
4.43k
   // FIXME this is largely a duplication of GeneralName::decode_from
187
4.43k
188
42.2k
   while(names.more_items())
189
37.9k
      {
190
37.9k
      BER_Object obj = names.get_next_object();
191
37.9k
192
37.9k
      if(obj.is_a(0, CONTEXT_SPECIFIC))
193
1.97k
         {
194
1.97k
         BER_Decoder othername(obj);
195
1.97k
196
1.97k
         OID oid;
197
1.97k
         othername.decode(oid);
198
1.97k
         if(othername.more_items())
199
1.79k
            {
200
1.79k
            BER_Object othername_value_outer = othername.get_next_object();
201
1.79k
            othername.verify_end();
202
1.79k
203
1.79k
            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.75k
206
1.75k
            BER_Decoder othername_value_inner(othername_value_outer);
207
1.75k
208
1.75k
            BER_Object value = othername_value_inner.get_next_object();
209
1.75k
            othername_value_inner.verify_end();
210
1.75k
211
1.75k
            if(ASN1_String::is_string_type(value.type()) && value.get_class() == UNIVERSAL)
212
1.39k
               {
213
1.39k
               add_othername(oid, ASN1::to_string(value), value.type());
214
1.39k
               }
215
1.75k
            }
216
1.97k
         }
217
37.9k
      if(obj.is_a(1, CONTEXT_SPECIFIC))
218
6.38k
         {
219
6.38k
         add_attribute("RFC822", ASN1::to_string(obj));
220
6.38k
         }
221
31.4k
      else if(obj.is_a(2, CONTEXT_SPECIFIC))
222
1.95k
         {
223
1.95k
         add_attribute("DNS", ASN1::to_string(obj));
224
1.95k
         }
225
29.5k
      else if(obj.is_a(6, CONTEXT_SPECIFIC))
226
2.67k
         {
227
2.67k
         add_attribute("URI", ASN1::to_string(obj));
228
2.67k
         }
229
26.8k
      else if(obj.is_a(4, ASN1_Tag(CONTEXT_SPECIFIC | CONSTRUCTED)))
230
4.20k
         {
231
4.20k
         BER_Decoder dec(obj);
232
4.20k
         X509_DN dn;
233
4.20k
         std::stringstream ss;
234
4.20k
235
4.20k
         dec.decode(dn);
236
4.20k
         ss << dn;
237
4.20k
238
4.20k
         add_attribute("DN", ss.str());
239
4.20k
         }
240
22.6k
      else if(obj.is_a(7, CONTEXT_SPECIFIC))
241
3.67k
         {
242
3.67k
         if(obj.length() == 4)
243
3.31k
            {
244
3.31k
            const uint32_t ip = load_be<uint32_t>(obj.bits(), 0);
245
3.31k
            add_attribute("IP", ipv4_to_string(ip));
246
3.31k
            }
247
3.67k
         }
248
37.8k
249
37.8k
      }
250
4.43k
   }
251
252
}