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