Coverage Report

Created: 2020-11-21 08:34

/src/botan/src/lib/utils/socket/uri.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2019 Nuno Goncalves <nunojpg@gmail.com>
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include <botan/internal/uri.h>
8
#include <botan/exceptn.h>
9
10
#include <regex>
11
12
#if defined(BOTAN_TARGET_OS_HAS_SOCKETS)
13
   #include <arpa/inet.h>
14
   #include <sys/socket.h>
15
   #include <netinet/in.h>
16
#elif defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
17
   #include <ws2tcpip.h>
18
#endif
19
20
#if defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
21
22
namespace {
23
24
constexpr bool isdigit(char ch)
25
195k
   {
26
195k
   return ch >= '0' && ch <= '9';
27
195k
   }
28
29
bool isDomain(const std::string& domain)
30
488
   {
31
#if defined(__GLIBCXX__) && (__GLIBCXX__ < 20160726)
32
   // GCC 4.8 does not support regex
33
   BOTAN_UNUSED(domain);
34
   return true;
35
#else
36
488
   std::regex re(
37
488
      R"(^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$)");
38
488
   std::cmatch m;
39
488
   return std::regex_match(domain.c_str(), m, re);
40
488
#endif
41
488
   }
42
43
bool isIPv4(const std::string& ip)
44
675
   {
45
675
   sockaddr_storage inaddr;
46
675
   return !!inet_pton(AF_INET, ip.c_str(), &inaddr);
47
675
   }
48
49
bool isIPv6(const std::string& ip)
50
59
   {
51
59
   sockaddr_storage in6addr;
52
59
   return !!inet_pton(AF_INET6, ip.c_str(), &in6addr);
53
59
   }
54
}
55
56
namespace Botan {
57
58
URI URI::fromDomain(const std::string& uri)
59
505
   {
60
505
   unsigned port = 0;
61
505
   const auto port_pos = uri.find(':');
62
505
   if(port_pos != std::string::npos)
63
78
      {
64
78
      for(char c : uri.substr(port_pos+1))
65
856
         {
66
856
         if(!isdigit(c))
67
9
            { throw Invalid_Argument("invalid"); }
68
847
         port = port*10 + c - '0';
69
847
         if(port > 65535)
70
7
            { throw Invalid_Argument("invalid"); }
71
847
         }
72
78
      }
73
489
   const auto domain = uri.substr(0, port_pos);
74
489
   if(isIPv4(domain))
75
1
      { throw Invalid_Argument("invalid"); }
76
488
   if(!isDomain(domain))
77
377
      { throw Invalid_Argument("invalid"); }
78
111
   return {Type::Domain, domain, uint16_t(port)};
79
111
   }
80
81
URI URI::fromIPv4(const std::string& uri)
82
49
   {
83
49
   unsigned port = 0;
84
49
   const auto port_pos = uri.find(':');
85
49
   if(port_pos != std::string::npos)
86
48
      {
87
48
      for(char c : uri.substr(port_pos+1))
88
345
         {
89
345
         if(!isdigit(c))
90
3
            { throw Invalid_Argument("invalid"); }
91
342
         port = port*10 + c - '0';
92
342
         if(port > 65535)
93
6
            { throw Invalid_Argument("invalid"); }
94
342
         }
95
48
      }
96
40
   const auto ip = uri.substr(0, port_pos);
97
40
   if(!isIPv4(ip))
98
0
      { throw Invalid_Argument("invalid"); }
99
40
   return { Type::IPv4, ip, uint16_t(port) };
100
40
   }
101
102
URI URI::fromIPv6(const std::string& uri)
103
114
   {
104
114
   unsigned port = 0;
105
114
   const auto port_pos = uri.find(']');
106
114
   const bool with_braces = (port_pos != std::string::npos);
107
114
   if((uri[0]=='[') != with_braces)
108
12
      { throw Invalid_Argument("invalid"); }
109
110
102
   if(with_braces && (uri.size() > port_pos + 1))
111
84
      {
112
84
      if(uri[port_pos+1]!=':')
113
20
         { throw Invalid_Argument("invalid"); }
114
64
      for(char c : uri.substr(port_pos+2))
115
388
         {
116
388
         if(!isdigit(c))
117
16
            { throw Invalid_Argument("invalid"); }
118
372
         port = port*10 + c - '0';
119
372
         if(port > 65535)
120
7
            { throw Invalid_Argument("invalid"); }
121
372
         }
122
64
      }
123
59
   const auto ip = uri.substr((with_braces ? 1 : 0), port_pos - with_braces);
124
59
   if(!isIPv6(ip))
125
55
      { throw Invalid_Argument("invalid"); }
126
4
   return { Type::IPv6, ip, uint16_t(port) };
127
4
   }
128
129
URI URI::fromAny(const std::string& uri)
130
668
   {
131
132
668
   bool colon_seen=false;
133
668
   bool non_number=false;
134
668
   if(uri[0]=='[')
135
93
      { return fromIPv6(uri); }
136
575
   for(auto c : uri)
137
194k
      {
138
194k
      if(c == ':')
139
168
         {
140
168
         if(colon_seen) //seen two ':'
141
21
            { return fromIPv6(uri); }
142
147
         colon_seen = true;
143
147
         }
144
194k
      else if(!isdigit(c) && c !=  '.')
145
165k
         {
146
165k
         non_number=true;
147
165k
         }
148
194k
      }
149
554
   if(!non_number)
150
146
      {
151
146
      if(isIPv4(uri.substr(0, uri.find(':'))))
152
49
         {
153
49
         return fromIPv4(uri);
154
49
         }
155
505
      }
156
505
   return fromDomain(uri);
157
505
   }
158
159
std::string URI::to_string() const
160
0
   {
161
0
   if(type == Type::NotSet)
162
0
      {
163
0
      throw Invalid_Argument("not set");
164
0
      }
165
166
0
   if(port != 0)
167
0
      {
168
0
      if(type == Type::IPv6)
169
0
         { return "[" + host + "]:" + std::to_string(port); }
170
0
      return host + ":" + std::to_string(port);
171
0
      }
172
0
   return host;
173
0
   }
174
175
}
176
177
#else
178
179
namespace Botan {
180
181
URI URI::fromDomain(const std::string&) {throw Not_Implemented("No socket support enabled in build");}
182
URI URI::fromIPv4(const std::string&) {throw Not_Implemented("No socket support enabled in build");}
183
URI URI::fromIPv6(const std::string&) {throw Not_Implemented("No socket support enabled in build");}
184
URI URI::fromAny(const std::string&) {throw Not_Implemented("No socket support enabled in build");}
185
186
}
187
188
#endif