Botan 3.8.1
Crypto and TLS for C&
uri.cpp
Go to the documentation of this file.
1/*
2* (C) 2019 Nuno Goncalves <nunojpg@gmail.com>
3* 2023,2024 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/uri.h>
9
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/parsing.h>
14#include <botan/internal/target_info.h>
15
16#if defined(BOTAN_TARGET_OS_HAS_SOCKETS)
17 #include <arpa/inet.h>
18 #include <netinet/in.h>
19 #include <sys/socket.h>
20#elif defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
21 #include <ws2tcpip.h>
22#endif
23
24#if defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
25
26namespace Botan {
27
28namespace {
29
30bool is_domain_name(std::string_view domain) {
31 try {
33 return true;
34 } catch(Decoding_Error&) {
35 return false;
36 }
37}
38
39bool is_ipv4(std::string_view ip) {
40 std::string ip_str(ip);
41 sockaddr_storage inaddr;
42 return !!inet_pton(AF_INET, ip_str.c_str(), &inaddr);
43}
44
45bool is_ipv6(std::string_view ip) {
46 std::string ip_str(ip);
47 sockaddr_storage in6addr;
48 return !!inet_pton(AF_INET6, ip_str.c_str(), &in6addr);
49}
50
51uint16_t parse_port_number(const char* func_name, std::string_view uri, size_t pos) {
52 if(pos == std::string::npos || uri.empty()) {
53 return 0;
54 }
55
56 BOTAN_ARG_CHECK(pos < uri.size(), "URI invalid port specifier");
57
58 uint32_t port = 0;
59
60 for(char c : uri.substr(pos + 1)) {
61 size_t digit = c - '0';
62 if(digit >= 10) {
63 throw Invalid_Argument(fmt("URI::{} invalid port field in {}", func_name, uri));
64 }
65 port = port * 10 + (c - '0');
66 if(port > 65535) {
67 throw Invalid_Argument(fmt("URI::{} invalid port field in {}", func_name, uri));
68 }
69 }
70
71 return static_cast<uint16_t>(port);
72}
73
74} // namespace
75
76URI URI::from_domain(std::string_view uri) {
77 BOTAN_ARG_CHECK(!uri.empty(), "URI::from_domain empty URI is invalid");
78
79 uint16_t port = 0;
80 const auto port_pos = uri.find(':');
81 if(port_pos != std::string::npos) {
82 port = parse_port_number("from_domain", uri, port_pos);
83 }
84 const auto domain = uri.substr(0, port_pos);
85 if(is_ipv4(domain)) {
86 throw Invalid_Argument("URI::from_domain domain name should not be IP address");
87 }
88 if(!is_domain_name(domain)) {
89 throw Invalid_Argument(fmt("URI::from_domain domain name '{}' not valid", domain));
90 }
91
92 return URI(Type::Domain, domain, port);
93}
94
95URI URI::from_ipv4(std::string_view uri) {
96 BOTAN_ARG_CHECK(!uri.empty(), "URI::from_ipv4 empty URI is invalid");
97
98 const auto port_pos = uri.find(':');
99 const uint16_t port = parse_port_number("from_ipv4", uri, port_pos);
100 const auto ip = uri.substr(0, port_pos);
101 if(!is_ipv4(ip)) {
102 throw Invalid_Argument("URI::from_ipv4: Invalid IPv4 specifier");
103 }
104 return URI(Type::IPv4, ip, port);
105}
106
107URI URI::from_ipv6(std::string_view uri) {
108 BOTAN_ARG_CHECK(!uri.empty(), "URI::from_ipv6 empty URI is invalid");
109
110 const auto port_pos = uri.find(']');
111 const bool with_braces = (port_pos != std::string::npos);
112 if((uri[0] == '[') != with_braces) {
113 throw Invalid_Argument("URI::from_ipv6 Invalid IPv6 address with mismatch braces");
114 }
115
116 uint16_t port = 0;
117 if(with_braces && (uri.size() > port_pos + 1)) {
118 if(uri[port_pos + 1] != ':') {
119 throw Invalid_Argument("URI::from_ipv6 Invalid IPv6 address");
120 }
121
122 port = parse_port_number("from_ipv6", uri, port_pos + 1);
123 }
124 const auto ip = uri.substr((with_braces ? 1 : 0), port_pos - with_braces);
125 if(!is_ipv6(ip)) {
126 throw Invalid_Argument("URI::from_ipv6 URI has invalid IPv6 address");
127 }
128 return URI(Type::IPv6, ip, port);
129}
130
131URI URI::from_any(std::string_view uri) {
132 BOTAN_ARG_CHECK(!uri.empty(), "URI::from_any empty URI is invalid");
133
134 try {
135 return URI::from_ipv4(uri);
136 } catch(Invalid_Argument&) {}
137
138 try {
139 return URI::from_ipv6(uri);
140 } catch(Invalid_Argument&) {}
141
142 return URI::from_domain(uri);
143}
144
145std::string URI::to_string() const {
146 if(m_port != 0) {
147 if(m_type == Type::IPv6) {
148 return "[" + m_host + "]:" + std::to_string(m_port);
149 }
150 return m_host + ":" + std::to_string(m_port);
151 }
152 return m_host;
153}
154
155} // namespace Botan
156
157#else
158
159namespace Botan {
160
161URI URI::from_domain(std::string_view) {
162 throw Not_Implemented("No socket support enabled in build");
163}
164
165URI URI::from_ipv4(std::string_view) {
166 throw Not_Implemented("No socket support enabled in build");
167}
168
169URI URI::from_ipv6(std::string_view) {
170 throw Not_Implemented("No socket support enabled in build");
171}
172
173URI URI::from_any(std::string_view) {
174 throw Not_Implemented("No socket support enabled in build");
175}
176
177} // namespace Botan
178
179#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:31
static URI from_ipv4(std::string_view uri)
Definition uri.cpp:165
static URI from_domain(std::string_view uri)
Definition uri.cpp:161
static URI from_ipv6(std::string_view uri)
Definition uri.cpp:169
uint16_t port() const
Definition uri.h:39
URI(Type type, std::string_view host, uint16_t port)
Definition uri.h:31
static URI from_any(std::string_view uri)
Definition uri.cpp:173
std::string to_string() const
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::string check_and_canonicalize_dns_name(std::string_view name)
Definition parsing.cpp:367