Botan 3.12.0
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 return string_to_ipv4(ip).has_value();
41}
42
43bool is_ipv6(std::string_view ip) {
44 return string_to_ipv6(ip).has_value();
45}
46
47uint16_t parse_port_number(const char* func_name, std::string_view uri, size_t pos) {
48 if(pos == std::string::npos || uri.empty()) {
49 return 0;
50 }
51
52 BOTAN_ARG_CHECK(pos < uri.size(), "URI invalid port specifier");
53
54 uint32_t port = 0;
55
56 for(const char c : uri.substr(pos + 1)) {
57 const size_t digit = c - '0';
58 if(digit >= 10) {
59 throw Invalid_Argument(fmt("URI::{} invalid port field in {}", func_name, uri));
60 }
61 port = port * 10 + (c - '0');
62 if(port > 65535) {
63 throw Invalid_Argument(fmt("URI::{} invalid port field in {}", func_name, uri));
64 }
65 }
66
67 return static_cast<uint16_t>(port);
68}
69
70} // namespace
71
72URI URI::from_domain(std::string_view uri) {
73 BOTAN_ARG_CHECK(!uri.empty(), "URI::from_domain empty URI is invalid");
74
75 uint16_t port = 0;
76 const auto port_pos = uri.find(':');
77 if(port_pos != std::string::npos) {
78 port = parse_port_number("from_domain", uri, port_pos);
79 }
80 const auto domain = uri.substr(0, port_pos);
81 if(is_ipv4(domain)) {
82 throw Invalid_Argument("URI::from_domain domain name should not be IP address");
83 }
84 if(!is_domain_name(domain)) {
85 throw Invalid_Argument(fmt("URI::from_domain domain name '{}' not valid", domain));
86 }
87
88 return URI(Type::Domain, domain, port);
89}
90
91URI URI::from_ipv4(std::string_view uri) {
92 BOTAN_ARG_CHECK(!uri.empty(), "URI::from_ipv4 empty URI is invalid");
93
94 const auto port_pos = uri.find(':');
95 const uint16_t port = parse_port_number("from_ipv4", uri, port_pos);
96 const auto ip = uri.substr(0, port_pos);
97 if(!is_ipv4(ip)) {
98 throw Invalid_Argument("URI::from_ipv4: Invalid IPv4 specifier");
99 }
100 return URI(Type::IPv4, ip, port);
101}
102
103URI URI::from_ipv6(std::string_view uri) {
104 BOTAN_ARG_CHECK(!uri.empty(), "URI::from_ipv6 empty URI is invalid");
105
106 const auto port_pos = uri.find(']');
107 const bool with_braces = (port_pos != std::string::npos);
108 if((uri[0] == '[') != with_braces) {
109 throw Invalid_Argument("URI::from_ipv6 Invalid IPv6 address with mismatch braces");
110 }
111
112 uint16_t port = 0;
113 if(with_braces && (uri.size() > port_pos + 1)) {
114 if(uri[port_pos + 1] != ':') {
115 throw Invalid_Argument("URI::from_ipv6 Invalid IPv6 address");
116 }
117
118 port = parse_port_number("from_ipv6", uri, port_pos + 1);
119 }
120 const auto ip = with_braces ? uri.substr(1, port_pos - 1) : uri.substr(0, port_pos);
121
122 if(!is_ipv6(ip)) {
123 throw Invalid_Argument("URI::from_ipv6 URI has invalid IPv6 address");
124 }
125 return URI(Type::IPv6, ip, port);
126}
127
128URI URI::from_any(std::string_view uri) {
129 BOTAN_ARG_CHECK(!uri.empty(), "URI::from_any empty URI is invalid");
130
131 try {
132 return URI::from_ipv4(uri);
133 } catch(Invalid_Argument&) {}
134
135 try {
136 return URI::from_ipv6(uri);
137 } catch(Invalid_Argument&) {}
138
139 return URI::from_domain(uri);
140}
141
142std::string URI::to_string() const {
143 if(m_port != 0) {
144 if(m_type == Type::IPv6) {
145 return "[" + m_host + "]:" + std::to_string(m_port);
146 }
147 return m_host + ":" + std::to_string(m_port);
148 }
149 return m_host;
150}
151
152} // namespace Botan
153
154#else
155
156namespace Botan {
157
158URI URI::from_domain(std::string_view) {
159 throw Not_Implemented("No socket support enabled in build");
160}
161
162URI URI::from_ipv4(std::string_view) {
163 throw Not_Implemented("No socket support enabled in build");
164}
165
166URI URI::from_ipv6(std::string_view) {
167 throw Not_Implemented("No socket support enabled in build");
168}
169
170URI URI::from_any(std::string_view) {
171 throw Not_Implemented("No socket support enabled in build");
172}
173
174} // namespace Botan
175
176#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
static URI from_ipv4(std::string_view uri)
Definition uri.cpp:162
static URI from_domain(std::string_view uri)
Definition uri.cpp:158
static URI from_ipv6(std::string_view uri)
Definition uri.cpp:166
uint16_t port() const
Definition uri.h:38
URI(Type type, std::string_view host, uint16_t port)
Definition uri.h:30
static URI from_any(std::string_view uri)
Definition uri.cpp:170
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:531
std::optional< std::array< uint8_t, 16 > > string_to_ipv6(std::string_view str)
Definition parsing.cpp:221
std::optional< uint32_t > string_to_ipv4(std::string_view str)
Definition parsing.cpp:155