| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#if !ENABLE_DNS_OVER_TLS || !HAVE_OPENSSL |
| 4 |
#error This source file requires DNS-over-TLS to be enabled and OpenSSL to be available. |
| 5 |
#endif |
| 6 |
|
| 7 |
#include <openssl/x509v3.h> |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "crypto-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "resolved-dns-server.h" |
| 13 |
#include "resolved-dns-stream.h" |
| 14 |
#include "resolved-dnstls.h" |
| 15 |
#include "resolved-manager.h" |
| 16 |
#include "ssl-util.h" |
| 17 |
|
| 18 |
static char *dnstls_error_string(int ssl_error, char *buf, size_t count) { |
| 19 |
assert(buf || count == 0); |
| 20 |
if (ssl_error == SSL_ERROR_SSL) |
| 21 |
sym_ERR_error_string_n(sym_ERR_get_error(), buf, count); |
| 22 |
else |
| 23 |
snprintf(buf, count, "SSL_get_error()=%d", ssl_error); |
| 24 |
return buf; |
| 25 |
} |
| 26 |
|
| 27 |
#define DNSTLS_ERROR_BUFSIZE 256 |
| 28 |
#define DNSTLS_ERROR_STRING(error) \ |
| 29 |
dnstls_error_string((error), (char[DNSTLS_ERROR_BUFSIZE]){}, DNSTLS_ERROR_BUFSIZE) |
| 30 |
|
| 31 |
static int dnstls_flush_write_buffer(DnsStream *stream) { |
| 32 |
ssize_t ss; |
| 33 |
|
| 34 |
assert(stream); |
| 35 |
assert(stream->encrypted); |
| 36 |
|
| 37 |
if (stream->dnstls_data.buffer_offset < stream->dnstls_data.write_buffer->length) { |
| 38 |
assert(stream->dnstls_data.write_buffer->data); |
| 39 |
|
| 40 |
struct iovec iov[1]; |
| 41 |
iov[0] = IOVEC_MAKE(stream->dnstls_data.write_buffer->data + stream->dnstls_data.buffer_offset, |
| 42 |
stream->dnstls_data.write_buffer->length - stream->dnstls_data.buffer_offset); |
| 43 |
ss = dns_stream_writev(stream, iov, 1, DNS_STREAM_WRITE_TLS_DATA); |
| 44 |
if (ss < 0) { |
| 45 |
if (ss == -EAGAIN) |
| 46 |
stream->dnstls_events |= EPOLLOUT; |
| 47 |
|
| 48 |
return ss; |
| 49 |
} else { |
| 50 |
stream->dnstls_data.buffer_offset += ss; |
| 51 |
|
| 52 |
if (stream->dnstls_data.buffer_offset < stream->dnstls_data.write_buffer->length) { |
| 53 |
stream->dnstls_events |= EPOLLOUT; |
| 54 |
return -EAGAIN; |
| 55 |
} else { |
| 56 |
sym_BIO_reset(sym_SSL_get_wbio(stream->dnstls_data.ssl)); |
| 57 |
stream->dnstls_data.buffer_offset = 0; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
int dnstls_stream_connect_tls(DnsStream *stream, DnsServer *server) { |
| 66 |
_cleanup_(BIO_freep) BIO *rb = NULL, *wb = NULL; |
| 67 |
_cleanup_(SSL_freep) SSL *s = NULL; |
| 68 |
int error, r; |
| 69 |
|
| 70 |
assert(stream); |
| 71 |
assert(stream->manager); |
| 72 |
assert(server); |
| 73 |
|
| 74 |
rb = sym_BIO_new_socket(stream->fd, 0); |
| 75 |
if (!rb) |
| 76 |
return -ENOMEM; |
| 77 |
|
| 78 |
wb = sym_BIO_new(sym_BIO_s_mem()); |
| 79 |
if (!wb) |
| 80 |
return -ENOMEM; |
| 81 |
|
| 82 |
sym_BIO_get_mem_ptr(wb, &stream->dnstls_data.write_buffer); |
| 83 |
stream->dnstls_data.buffer_offset = 0; |
| 84 |
|
| 85 |
s = sym_SSL_new(stream->manager->dnstls_data.ctx); |
| 86 |
if (!s) |
| 87 |
return -ENOMEM; |
| 88 |
|
| 89 |
sym_SSL_set_connect_state(s); |
| 90 |
r = sym_SSL_set_session(s, server->dnstls_data.session); |
| 91 |
if (r == 0) |
| 92 |
return -EIO; |
| 93 |
sym_SSL_set_bio(s, TAKE_PTR(rb), TAKE_PTR(wb)); |
| 94 |
|
| 95 |
if (server->manager->dns_over_tls_mode == DNS_OVER_TLS_YES) { |
| 96 |
X509_VERIFY_PARAM *v; |
| 97 |
|
| 98 |
sym_SSL_set_verify(s, SSL_VERIFY_PEER, NULL); |
| 99 |
v = sym_SSL_get0_param(s); |
| 100 |
if (server->server_name) { |
| 101 |
sym_X509_VERIFY_PARAM_set_hostflags(v, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 102 |
if (sym_X509_VERIFY_PARAM_set1_host(v, server->server_name, 0) == 0) |
| 103 |
return -ECONNREFUSED; |
| 104 |
} else { |
| 105 |
const unsigned char *ip; |
| 106 |
ip = server->family == AF_INET ? (const unsigned char*) &server->address.in.s_addr : server->address.in6.s6_addr; |
| 107 |
if (sym_X509_VERIFY_PARAM_set1_ip(v, ip, FAMILY_ADDRESS_SIZE(server->family)) == 0) |
| 108 |
return -ECONNREFUSED; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (server->server_name) { |
| 113 |
r = sym_SSL_set_tlsext_host_name(s, server->server_name); |
| 114 |
if (r <= 0) |
| 115 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 116 |
"Failed to set server name: %s", DNSTLS_ERROR_STRING(SSL_ERROR_SSL)); |
| 117 |
} |
| 118 |
|
| 119 |
sym_ERR_clear_error(); |
| 120 |
stream->dnstls_data.handshake = sym_SSL_do_handshake(s); |
| 121 |
if (stream->dnstls_data.handshake <= 0) { |
| 122 |
error = sym_SSL_get_error(s, stream->dnstls_data.handshake); |
| 123 |
if (!IN_SET(error, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE)) |
| 124 |
return log_debug_errno(SYNTHETIC_ERRNO(ECONNREFUSED), |
| 125 |
"Failed to invoke SSL_do_handshake: %s", DNSTLS_ERROR_STRING(error)); |
| 126 |
} |
| 127 |
|
| 128 |
stream->encrypted = true; |
| 129 |
stream->dnstls_data.ssl = TAKE_PTR(s); |
| 130 |
|
| 131 |
r = dnstls_flush_write_buffer(stream); |
| 132 |
if (r < 0 && r != -EAGAIN) { |
| 133 |
sym_SSL_free(TAKE_PTR(stream->dnstls_data.ssl)); |
| 134 |
return r; |
| 135 |
} |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
void dnstls_stream_free(DnsStream *stream) { |
| 141 |
assert(stream); |
| 142 |
assert(stream->encrypted); |
| 143 |
|
| 144 |
if (stream->dnstls_data.ssl) |
| 145 |
sym_SSL_free(stream->dnstls_data.ssl); |
| 146 |
} |
| 147 |
|
| 148 |
int dnstls_stream_on_io(DnsStream *stream, uint32_t revents) { |
| 149 |
int error, r; |
| 150 |
|
| 151 |
assert(stream); |
| 152 |
assert(stream->encrypted); |
| 153 |
assert(stream->dnstls_data.ssl); |
| 154 |
|
| 155 |
/* Flush write buffer when requested by OpenSSL */ |
| 156 |
if ((revents & EPOLLOUT) && (stream->dnstls_events & EPOLLOUT)) { |
| 157 |
r = dnstls_flush_write_buffer(stream); |
| 158 |
if (r < 0) |
| 159 |
return r; |
| 160 |
} |
| 161 |
|
| 162 |
if (stream->dnstls_data.shutdown) { |
| 163 |
sym_ERR_clear_error(); |
| 164 |
r = sym_SSL_shutdown(stream->dnstls_data.ssl); |
| 165 |
if (r == 0) { |
| 166 |
stream->dnstls_events = 0; |
| 167 |
|
| 168 |
r = dnstls_flush_write_buffer(stream); |
| 169 |
if (r < 0) |
| 170 |
return r; |
| 171 |
|
| 172 |
return -EAGAIN; |
| 173 |
} else if (r < 0) { |
| 174 |
error = sym_SSL_get_error(stream->dnstls_data.ssl, r); |
| 175 |
if (IN_SET(error, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE)) { |
| 176 |
stream->dnstls_events = error == SSL_ERROR_WANT_READ ? EPOLLIN : EPOLLOUT; |
| 177 |
|
| 178 |
r = dnstls_flush_write_buffer(stream); |
| 179 |
if (r < 0) |
| 180 |
return r; |
| 181 |
|
| 182 |
return -EAGAIN; |
| 183 |
} else if (error == SSL_ERROR_SYSCALL) { |
| 184 |
if (errno > 0) |
| 185 |
log_debug_errno(errno, "Failed to invoke SSL_shutdown, ignoring: %m"); |
| 186 |
} else |
| 187 |
log_debug("Failed to invoke SSL_shutdown, ignoring: %s", DNSTLS_ERROR_STRING(error)); |
| 188 |
} |
| 189 |
|
| 190 |
stream->dnstls_events = 0; |
| 191 |
stream->dnstls_data.shutdown = false; |
| 192 |
|
| 193 |
r = dnstls_flush_write_buffer(stream); |
| 194 |
if (r < 0) |
| 195 |
return r; |
| 196 |
|
| 197 |
dns_stream_unref(stream); |
| 198 |
return DNSTLS_STREAM_CLOSED; |
| 199 |
} else if (stream->dnstls_data.handshake <= 0) { |
| 200 |
sym_ERR_clear_error(); |
| 201 |
stream->dnstls_data.handshake = sym_SSL_do_handshake(stream->dnstls_data.ssl); |
| 202 |
if (stream->dnstls_data.handshake <= 0) { |
| 203 |
error = sym_SSL_get_error(stream->dnstls_data.ssl, stream->dnstls_data.handshake); |
| 204 |
if (IN_SET(error, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE)) { |
| 205 |
stream->dnstls_events = error == SSL_ERROR_WANT_READ ? EPOLLIN : EPOLLOUT; |
| 206 |
r = dnstls_flush_write_buffer(stream); |
| 207 |
if (r < 0) |
| 208 |
return r; |
| 209 |
|
| 210 |
return -EAGAIN; |
| 211 |
} else |
| 212 |
return log_debug_errno(SYNTHETIC_ERRNO(ECONNREFUSED), |
| 213 |
"Failed to invoke SSL_do_handshake: %s", |
| 214 |
DNSTLS_ERROR_STRING(error)); |
| 215 |
} |
| 216 |
|
| 217 |
stream->dnstls_events = 0; |
| 218 |
r = dnstls_flush_write_buffer(stream); |
| 219 |
if (r < 0) |
| 220 |
return r; |
| 221 |
} |
| 222 |
|
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
int dnstls_stream_shutdown(DnsStream *stream, int error) { |
| 227 |
int ssl_error, r; |
| 228 |
SSL_SESSION *s; |
| 229 |
|
| 230 |
assert(stream); |
| 231 |
assert(stream->encrypted); |
| 232 |
assert(stream->dnstls_data.ssl); |
| 233 |
|
| 234 |
if (stream->server) { |
| 235 |
s = sym_SSL_get1_session(stream->dnstls_data.ssl); |
| 236 |
if (s) { |
| 237 |
if (stream->server->dnstls_data.session) |
| 238 |
sym_SSL_SESSION_free(stream->server->dnstls_data.session); |
| 239 |
|
| 240 |
stream->server->dnstls_data.session = s; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if (error == ETIMEDOUT) { |
| 245 |
sym_ERR_clear_error(); |
| 246 |
r = sym_SSL_shutdown(stream->dnstls_data.ssl); |
| 247 |
if (r == 0) { |
| 248 |
if (!stream->dnstls_data.shutdown) { |
| 249 |
stream->dnstls_data.shutdown = true; |
| 250 |
dns_stream_ref(stream); |
| 251 |
} |
| 252 |
|
| 253 |
stream->dnstls_events = 0; |
| 254 |
|
| 255 |
r = dnstls_flush_write_buffer(stream); |
| 256 |
if (r < 0) |
| 257 |
return r; |
| 258 |
|
| 259 |
return -EAGAIN; |
| 260 |
} else if (r < 0) { |
| 261 |
ssl_error = sym_SSL_get_error(stream->dnstls_data.ssl, r); |
| 262 |
if (IN_SET(ssl_error, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE)) { |
| 263 |
stream->dnstls_events = ssl_error == SSL_ERROR_WANT_READ ? EPOLLIN : EPOLLOUT; |
| 264 |
r = dnstls_flush_write_buffer(stream); |
| 265 |
if (r < 0 && r != -EAGAIN) |
| 266 |
return r; |
| 267 |
|
| 268 |
if (!stream->dnstls_data.shutdown) { |
| 269 |
stream->dnstls_data.shutdown = true; |
| 270 |
dns_stream_ref(stream); |
| 271 |
} |
| 272 |
return -EAGAIN; |
| 273 |
} else if (ssl_error == SSL_ERROR_SYSCALL) { |
| 274 |
if (errno > 0) |
| 275 |
log_debug_errno(errno, "Failed to invoke SSL_shutdown, ignoring: %m"); |
| 276 |
} else |
| 277 |
log_debug("Failed to invoke SSL_shutdown, ignoring: %s", DNSTLS_ERROR_STRING(ssl_error)); |
| 278 |
} |
| 279 |
|
| 280 |
stream->dnstls_events = 0; |
| 281 |
r = dnstls_flush_write_buffer(stream); |
| 282 |
if (r < 0) |
| 283 |
return r; |
| 284 |
} |
| 285 |
|
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
static ssize_t dnstls_stream_write(DnsStream *stream, const char *buf, size_t count) { |
| 290 |
int error, r; |
| 291 |
ssize_t ss; |
| 292 |
|
| 293 |
sym_ERR_clear_error(); |
| 294 |
ss = r = sym_SSL_write(stream->dnstls_data.ssl, buf, count); |
| 295 |
if (r <= 0) { |
| 296 |
error = sym_SSL_get_error(stream->dnstls_data.ssl, r); |
| 297 |
if (IN_SET(error, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE)) { |
| 298 |
stream->dnstls_events = error == SSL_ERROR_WANT_READ ? EPOLLIN : EPOLLOUT; |
| 299 |
ss = -EAGAIN; |
| 300 |
} else if (error == SSL_ERROR_ZERO_RETURN) { |
| 301 |
stream->dnstls_events = 0; |
| 302 |
ss = 0; |
| 303 |
} else { |
| 304 |
log_debug("Failed to invoke SSL_write: %s", DNSTLS_ERROR_STRING(error)); |
| 305 |
stream->dnstls_events = 0; |
| 306 |
ss = -EPIPE; |
| 307 |
} |
| 308 |
} else |
| 309 |
stream->dnstls_events = 0; |
| 310 |
|
| 311 |
r = dnstls_flush_write_buffer(stream); |
| 312 |
if (r < 0) |
| 313 |
return r; |
| 314 |
|
| 315 |
return ss; |
| 316 |
} |
| 317 |
|
| 318 |
ssize_t dnstls_stream_writev(DnsStream *stream, const struct iovec *iov, size_t iovcnt) { |
| 319 |
assert(stream); |
| 320 |
assert(stream->encrypted); |
| 321 |
assert(stream->dnstls_data.ssl); |
| 322 |
assert(iov); |
| 323 |
|
| 324 |
size_t size = iovec_total_size(iov, iovcnt); |
| 325 |
if (size == 0) |
| 326 |
return -EINVAL; |
| 327 |
if (size == SIZE_MAX) |
| 328 |
return -ENOBUFS; |
| 329 |
|
| 330 |
if (iovcnt == 1) |
| 331 |
return dnstls_stream_write(stream, iov[0].iov_base, iov[0].iov_len); |
| 332 |
|
| 333 |
/* As of now, OpenSSL cannot accumulate multiple writes, so join into a |
| 334 |
single buffer. Suboptimal, but better than multiple SSL_write calls. */ |
| 335 |
_cleanup_free_ char *buf = new(char, size); |
| 336 |
if (!buf) |
| 337 |
return -ENOMEM; |
| 338 |
|
| 339 |
for (size_t i = 0, pos = 0; i < iovcnt; pos += iov[i].iov_len, i++) |
| 340 |
memcpy(buf + pos, iov[i].iov_base, iov[i].iov_len); |
| 341 |
|
| 342 |
return dnstls_stream_write(stream, buf, size); |
| 343 |
} |
| 344 |
|
| 345 |
ssize_t dnstls_stream_read(DnsStream *stream, void *buf, size_t count) { |
| 346 |
int error, r; |
| 347 |
ssize_t ss; |
| 348 |
|
| 349 |
assert(stream); |
| 350 |
assert(stream->encrypted); |
| 351 |
assert(stream->dnstls_data.ssl); |
| 352 |
assert(buf); |
| 353 |
|
| 354 |
sym_ERR_clear_error(); |
| 355 |
ss = r = sym_SSL_read(stream->dnstls_data.ssl, buf, count); |
| 356 |
if (r <= 0) { |
| 357 |
error = sym_SSL_get_error(stream->dnstls_data.ssl, r); |
| 358 |
if (IN_SET(error, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE)) { |
| 359 |
/* If we receive SSL_ERROR_WANT_READ here, there are two possible scenarios: |
| 360 |
* OpenSSL needs to renegotiate (so we want to get an EPOLLIN event), or |
| 361 |
* There is no more application data is available, so we can just return |
| 362 |
And apparently there's no nice way to distinguish between the two. |
| 363 |
To handle this, never set EPOLLIN and just continue as usual. |
| 364 |
If OpenSSL really wants to read due to renegotiation, it will tell us |
| 365 |
again on SSL_write (at which point we will request EPOLLIN force a read); |
| 366 |
or we will just eventually read data anyway while we wait for a packet */ |
| 367 |
stream->dnstls_events = error == SSL_ERROR_WANT_READ ? 0 : EPOLLOUT; |
| 368 |
ss = -EAGAIN; |
| 369 |
} else if (error == SSL_ERROR_ZERO_RETURN) { |
| 370 |
stream->dnstls_events = 0; |
| 371 |
ss = 0; |
| 372 |
} else { |
| 373 |
log_debug("Failed to invoke SSL_read: %s", DNSTLS_ERROR_STRING(error)); |
| 374 |
stream->dnstls_events = 0; |
| 375 |
ss = -EPIPE; |
| 376 |
} |
| 377 |
} else |
| 378 |
stream->dnstls_events = 0; |
| 379 |
|
| 380 |
/* flush write buffer in cache of renegotiation */ |
| 381 |
r = dnstls_flush_write_buffer(stream); |
| 382 |
if (r < 0) |
| 383 |
return r; |
| 384 |
|
| 385 |
return ss; |
| 386 |
} |
| 387 |
|
| 388 |
void dnstls_server_free(DnsServer *server) { |
| 389 |
assert(server); |
| 390 |
|
| 391 |
if (server->dnstls_data.session) |
| 392 |
sym_SSL_SESSION_free(server->dnstls_data.session); |
| 393 |
} |
| 394 |
|
| 395 |
int dnstls_manager_init(Manager *manager) { |
| 396 |
int r; |
| 397 |
|
| 398 |
assert(manager); |
| 399 |
|
| 400 |
r = DLOPEN_LIBCRYPTO(LOG_WARNING, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 401 |
if (r < 0) |
| 402 |
return r; |
| 403 |
|
| 404 |
r = DLOPEN_LIBSSL(LOG_WARNING, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 405 |
if (r < 0) |
| 406 |
return r; |
| 407 |
|
| 408 |
manager->dnstls_data.ctx = sym_SSL_CTX_new(sym_TLS_client_method()); |
| 409 |
if (!manager->dnstls_data.ctx) |
| 410 |
return log_warning_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), |
| 411 |
"Failed to create SSL context: %s", |
| 412 |
sym_ERR_error_string(sym_ERR_get_error(), NULL)); |
| 413 |
|
| 414 |
r = sym_SSL_CTX_set_min_proto_version(manager->dnstls_data.ctx, TLS1_2_VERSION); |
| 415 |
if (r == 0) |
| 416 |
return log_warning_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), |
| 417 |
"Failed to set protocol version on SSL context: %s", |
| 418 |
sym_ERR_error_string(sym_ERR_get_error(), NULL)); |
| 419 |
|
| 420 |
(void) sym_SSL_CTX_set_options(manager->dnstls_data.ctx, SSL_OP_NO_COMPRESSION); |
| 421 |
|
| 422 |
r = sym_SSL_CTX_set_default_verify_paths(manager->dnstls_data.ctx); |
| 423 |
if (r == 0) |
| 424 |
return log_warning_errno(SYNTHETIC_ERRNO(EIO), |
| 425 |
"Failed to load system trust store: %s", |
| 426 |
sym_ERR_error_string(sym_ERR_get_error(), NULL)); |
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
void dnstls_manager_free(Manager *manager) { |
| 431 |
assert(manager); |
| 432 |
|
| 433 |
if (manager->dnstls_data.ctx) |
| 434 |
sym_SSL_CTX_free(manager->dnstls_data.ctx); |
| 435 |
} |