Coverage Report

Created: 2021-02-21 07:20

/src/botan/src/lib/utils/parsing.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Various string utils and parsing functions
3
* (C) 1999-2007,2013,2014,2015,2018 Jack Lloyd
4
* (C) 2015 Simon Warta (Kullo GmbH)
5
* (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9
10
#include <botan/internal/parsing.h>
11
#include <botan/internal/loadstor.h>
12
#include <botan/exceptn.h>
13
#include <algorithm>
14
#include <cctype>
15
16
namespace Botan {
17
18
uint16_t to_uint16(const std::string& str)
19
0
   {
20
0
   const uint32_t x = to_u32bit(str);
21
22
0
   if(x >> 16)
23
0
      throw Invalid_Argument("Integer value exceeds 16 bit range");
24
25
0
   return static_cast<uint16_t>(x);
26
0
   }
27
28
uint32_t to_u32bit(const std::string& str)
29
2.91M
   {
30
   // std::stoul is not strict enough. Ensure that str is digit only [0-9]*
31
2.91M
   for(const char chr : str)
32
4.37M
      {
33
4.37M
      if(chr < '0' || chr > '9')
34
430
         {
35
430
         std::string chrAsString(1, chr);
36
430
         throw Invalid_Argument("String contains non-digit char: " + chrAsString);
37
430
         }
38
4.37M
      }
39
40
2.91M
   const unsigned long int x = std::stoul(str);
41
42
2.91M
   if(sizeof(unsigned long int) > 4)
43
2.91M
      {
44
      // x might be uint64
45
2.91M
      if (x > std::numeric_limits<uint32_t>::max())
46
0
         {
47
0
         throw Invalid_Argument("Integer value of " + str + " exceeds 32 bit range");
48
0
         }
49
2.91M
      }
50
51
2.91M
   return static_cast<uint32_t>(x);
52
2.91M
   }
53
54
/*
55
* Parse a SCAN-style algorithm name
56
*/
57
std::vector<std::string> parse_algorithm_name(const std::string& namex)
58
1.71k
   {
59
1.71k
   if(namex.find('(') == std::string::npos &&
60
988
      namex.find(')') == std::string::npos)
61
988
      return std::vector<std::string>(1, namex);
62
63
731
   std::string name = namex, substring;
64
731
   std::vector<std::string> elems;
65
731
   size_t level = 0;
66
67
731
   elems.push_back(name.substr(0, name.find('(')));
68
731
   name = name.substr(name.find('('));
69
70
2.57k
   for(auto i = name.begin(); i != name.end(); ++i)
71
2.57k
      {
72
2.57k
      char c = *i;
73
74
2.57k
      if(c == '(')
75
731
         ++level;
76
2.57k
      if(c == ')')
77
731
         {
78
731
         if(level == 1 && i == name.end() - 1)
79
731
            {
80
731
            if(elems.size() == 1)
81
731
               elems.push_back(substring.substr(1));
82
0
            else
83
0
               elems.push_back(substring);
84
731
            return elems;
85
731
            }
86
87
0
         if(level == 0 || (level == 1 && i != name.end() - 1))
88
0
            throw Invalid_Algorithm_Name(namex);
89
0
         --level;
90
0
         }
91
92
1.84k
      if(c == ',' && level == 1)
93
0
         {
94
0
         if(elems.size() == 1)
95
0
            elems.push_back(substring.substr(1));
96
0
         else
97
0
            elems.push_back(substring);
98
0
         substring.clear();
99
0
         }
100
1.84k
      else
101
1.84k
         substring += c;
102
1.84k
      }
103
104
0
   if(!substring.empty())
105
0
      throw Invalid_Algorithm_Name(namex);
106
107
0
   return elems;
108
0
   }
109
110
std::vector<std::string> split_on(const std::string& str, char delim)
111
32.8k
   {
112
32.8k
   std::vector<std::string> elems;
113
32.8k
   if(str.empty()) return elems;
114
115
30.0k
   std::string substr;
116
313k
   for(auto i = str.begin(); i != str.end(); ++i)
117
283k
      {
118
283k
      if(*i == delim)
119
10.6k
         {
120
10.6k
         if(!substr.empty())
121
10.6k
            elems.push_back(substr);
122
10.6k
         substr.clear();
123
10.6k
         }
124
273k
      else
125
273k
         substr += *i;
126
283k
      }
127
128
30.0k
   if(substr.empty())
129
0
      throw Invalid_Argument("Unable to split string: " + str);
130
30.0k
   elems.push_back(substr);
131
132
30.0k
   return elems;
133
30.0k
   }
134
135
/*
136
* Join a string
137
*/
138
std::string string_join(const std::vector<std::string>& strs, char delim)
139
0
   {
140
0
   std::string out = "";
141
142
0
   for(size_t i = 0; i != strs.size(); ++i)
143
0
      {
144
0
      if(i != 0)
145
0
         out += delim;
146
0
      out += strs[i];
147
0
      }
148
149
0
   return out;
150
0
   }
151
152
/*
153
* Convert a decimal-dotted string to binary IP
154
*/
155
uint32_t string_to_ipv4(const std::string& str)
156
0
   {
157
0
   std::vector<std::string> parts = split_on(str, '.');
158
159
0
   if(parts.size() != 4)
160
0
      throw Decoding_Error("Invalid IP string " + str);
161
162
0
   uint32_t ip = 0;
163
164
0
   for(auto part = parts.begin(); part != parts.end(); ++part)
165
0
      {
166
0
      uint32_t octet = to_u32bit(*part);
167
168
0
      if(octet > 255)
169
0
         throw Decoding_Error("Invalid IP string " + str);
170
171
0
      ip = (ip << 8) | (octet & 0xFF);
172
0
      }
173
174
0
   return ip;
175
0
   }
176
177
/*
178
* Convert an IP address to decimal-dotted string
179
*/
180
std::string ipv4_to_string(uint32_t ip)
181
5.40k
   {
182
5.40k
   std::string str;
183
184
27.0k
   for(size_t i = 0; i != sizeof(ip); ++i)
185
21.6k
      {
186
21.6k
      if(i)
187
16.2k
         str += ".";
188
21.6k
      str += std::to_string(get_byte(i, ip));
189
21.6k
      }
190
191
5.40k
   return str;
192
5.40k
   }
193
194
namespace {
195
196
std::string tolower_string(const std::string& in)
197
0
   {
198
0
   std::string s = in;
199
0
   for(size_t i = 0; i != s.size(); ++i)
200
0
      {
201
0
      const int cu = static_cast<unsigned char>(s[i]);
202
0
      if(std::isalpha(cu))
203
0
         s[i] = static_cast<char>(std::tolower(cu));
204
0
      }
205
0
   return s;
206
0
   }
207
208
}
209
210
bool host_wildcard_match(const std::string& issued_, const std::string& host_)
211
0
   {
212
0
   const std::string issued = tolower_string(issued_);
213
0
   const std::string host = tolower_string(host_);
214
215
0
   if(host.empty() || issued.empty())
216
0
      return false;
217
218
   /*
219
   If there are embedded nulls in your issued name
220
   Well I feel bad for you son
221
   */
222
0
   if(std::count(issued.begin(), issued.end(), char(0)) > 0)
223
0
      return false;
224
225
   // If more than one wildcard, then issued name is invalid
226
0
   const size_t stars = std::count(issued.begin(), issued.end(), '*');
227
0
   if(stars > 1)
228
0
      return false;
229
230
   // '*' is not a valid character in DNS names so should not appear on the host side
231
0
   if(std::count(host.begin(), host.end(), '*') != 0)
232
0
      return false;
233
234
   // Similarly a DNS name can't end in .
235
0
   if(host[host.size() - 1] == '.')
236
0
      return false;
237
238
   // And a host can't have an empty name component, so reject that
239
0
   if(host.find("..") != std::string::npos)
240
0
      return false;
241
242
   // Exact match: accept
243
0
   if(issued == host)
244
0
      {
245
0
      return true;
246
0
      }
247
248
   /*
249
   Otherwise it might be a wildcard
250
251
   If the issued size is strictly longer than the hostname size it
252
   couldn't possibly be a match, even if the issued value is a
253
   wildcard. The only exception is when the wildcard ends up empty
254
   (eg www.example.com matches www*.example.com)
255
   */
256
0
   if(issued.size() > host.size() + 1)
257
0
      {
258
0
      return false;
259
0
      }
260
261
   // If no * at all then not a wildcard, and so not a match
262
0
   if(stars != 1)
263
0
      {
264
0
      return false;
265
0
      }
266
267
   /*
268
   Now walk through the issued string, making sure every character
269
   matches. When we come to the (singular) '*', jump forward in the
270
   hostname by the corresponding amount. We know exactly how much
271
   space the wildcard takes because it must be exactly `len(host) -
272
   len(issued) + 1 chars`.
273
274
   We also verify that the '*' comes in the leftmost component, and
275
   doesn't skip over any '.' in the hostname.
276
   */
277
0
   size_t dots_seen = 0;
278
0
   size_t host_idx = 0;
279
280
0
   for(size_t i = 0; i != issued.size(); ++i)
281
0
      {
282
0
      dots_seen += (issued[i] == '.');
283
284
0
      if(issued[i] == '*')
285
0
         {
286
         // Fail: wildcard can only come in leftmost component
287
0
         if(dots_seen > 0)
288
0
            {
289
0
            return false;
290
0
            }
291
292
         /*
293
         Since there is only one * we know the tail of the issued and
294
         hostname must be an exact match. In this case advance host_idx
295
         to match.
296
         */
297
0
         const size_t advance = (host.size() - issued.size() + 1);
298
299
0
         if(host_idx + advance > host.size()) // shouldn't happen
300
0
            return false;
301
302
         // Can't be any intervening .s that we would have skipped
303
0
         if(std::count(host.begin() + host_idx,
304
0
                       host.begin() + host_idx + advance, '.') != 0)
305
0
            return false;
306
307
0
         host_idx += advance;
308
0
         }
309
0
      else
310
0
         {
311
0
         if(issued[i] != host[host_idx])
312
0
            {
313
0
            return false;
314
0
            }
315
316
0
         host_idx += 1;
317
0
         }
318
0
      }
319
320
   // Wildcard issued name must have at least 3 components
321
0
   if(dots_seen < 2)
322
0
      {
323
0
      return false;
324
0
      }
325
326
0
   return true;
327
0
   }
328
329
}