1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #if !ENABLE_DNS_OVER_TLS || !DNS_OVER_TLS_USE_OPENSSL
4 #error This source file requires DNS-over-TLS to be enabled and OpenSSL to be available.
7 #include <openssl/bio.h>
8 #include <openssl/err.h>
11 #include "resolved-dns-stream.h"
12 #include "resolved-dnstls.h"
14 DEFINE_TRIVIAL_CLEANUP_FUNC(SSL
*, SSL_free
);
15 DEFINE_TRIVIAL_CLEANUP_FUNC(BIO
*, BIO_free
);
17 static int dnstls_flush_write_buffer(DnsStream
*stream
) {
21 assert(stream
->encrypted
);
23 if (stream
->dnstls_data
.write_buffer
->length
> 0) {
24 assert(stream
->dnstls_data
.write_buffer
->data
);
27 iov
[0] = IOVEC_MAKE(stream
->dnstls_data
.write_buffer
->data
,
28 stream
->dnstls_data
.write_buffer
->length
);
29 ss
= dns_stream_writev(stream
, iov
, 1, DNS_STREAM_WRITE_TLS_DATA
);
32 stream
->dnstls_events
|= EPOLLOUT
;
36 stream
->dnstls_data
.write_buffer
->length
-= ss
;
37 stream
->dnstls_data
.write_buffer
->data
+= ss
;
39 if (stream
->dnstls_data
.write_buffer
->length
> 0) {
40 stream
->dnstls_events
|= EPOLLOUT
;
49 int dnstls_stream_connect_tls(DnsStream
*stream
, DnsServer
*server
) {
50 _cleanup_(BIO_freep
) BIO
*rb
= NULL
, *wb
= NULL
;
51 _cleanup_(SSL_freep
) SSL
*s
= NULL
;
57 rb
= BIO_new_socket(stream
->fd
, 0);
61 wb
= BIO_new(BIO_s_mem());
65 BIO_get_mem_ptr(wb
, &stream
->dnstls_data
.write_buffer
);
67 s
= SSL_new(server
->dnstls_data
.ctx
);
71 SSL_set_connect_state(s
);
72 SSL_set_session(s
, server
->dnstls_data
.session
);
73 SSL_set_bio(s
, TAKE_PTR(rb
), TAKE_PTR(wb
));
76 stream
->dnstls_data
.handshake
= SSL_do_handshake(s
);
77 if (stream
->dnstls_data
.handshake
<= 0) {
78 error
= SSL_get_error(s
, stream
->dnstls_data
.handshake
);
79 if (!IN_SET(error
, SSL_ERROR_WANT_READ
, SSL_ERROR_WANT_WRITE
)) {
82 ERR_error_string_n(error
, errbuf
, sizeof(errbuf
));
83 log_debug("Failed to invoke SSL_do_handshake: %s", errbuf
);
88 stream
->encrypted
= true;
90 r
= dnstls_flush_write_buffer(stream
);
91 if (r
< 0 && r
!= -EAGAIN
)
94 stream
->dnstls_data
.ssl
= TAKE_PTR(s
);
99 void dnstls_stream_free(DnsStream
*stream
) {
101 assert(stream
->encrypted
);
103 if (stream
->dnstls_data
.ssl
)
104 SSL_free(stream
->dnstls_data
.ssl
);
107 int dnstls_stream_on_io(DnsStream
*stream
, uint32_t revents
) {
111 assert(stream
->encrypted
);
112 assert(stream
->dnstls_data
.ssl
);
114 /* Flush write buffer when requested by OpenSSL */
115 if ((revents
& EPOLLOUT
) && (stream
->dnstls_events
& EPOLLOUT
)) {
116 r
= dnstls_flush_write_buffer(stream
);
121 if (stream
->dnstls_data
.shutdown
) {
123 r
= SSL_shutdown(stream
->dnstls_data
.ssl
);
125 stream
->dnstls_events
= 0;
127 r
= dnstls_flush_write_buffer(stream
);
133 error
= SSL_get_error(stream
->dnstls_data
.ssl
, r
);
134 if (IN_SET(error
, SSL_ERROR_WANT_READ
, SSL_ERROR_WANT_WRITE
)) {
135 stream
->dnstls_events
= error
== SSL_ERROR_WANT_READ
? EPOLLIN
: EPOLLOUT
;
137 r
= dnstls_flush_write_buffer(stream
);
142 } else if (error
== SSL_ERROR_SYSCALL
) {
144 log_debug_errno(errno
, "Failed to invoke SSL_shutdown, ignoring: %m");
148 ERR_error_string_n(error
, errbuf
, sizeof(errbuf
));
149 log_debug("Failed to invoke SSL_shutdown, ignoring: %s", errbuf
);
153 stream
->dnstls_events
= 0;
154 stream
->dnstls_data
.shutdown
= false;
156 r
= dnstls_flush_write_buffer(stream
);
160 dns_stream_unref(stream
);
161 return DNSTLS_STREAM_CLOSED
;
162 } else if (stream
->dnstls_data
.handshake
<= 0) {
164 stream
->dnstls_data
.handshake
= SSL_do_handshake(stream
->dnstls_data
.ssl
);
165 if (stream
->dnstls_data
.handshake
<= 0) {
166 error
= SSL_get_error(stream
->dnstls_data
.ssl
, stream
->dnstls_data
.handshake
);
167 if (IN_SET(error
, SSL_ERROR_WANT_READ
, SSL_ERROR_WANT_WRITE
)) {
168 stream
->dnstls_events
= error
== SSL_ERROR_WANT_READ
? EPOLLIN
: EPOLLOUT
;
169 r
= dnstls_flush_write_buffer(stream
);
177 ERR_error_string_n(error
, errbuf
, sizeof(errbuf
));
178 return log_debug_errno(SYNTHETIC_ERRNO(ECONNREFUSED
),
179 "Failed to invoke SSL_do_handshake: %s",
184 stream
->dnstls_events
= 0;
185 r
= dnstls_flush_write_buffer(stream
);
193 int dnstls_stream_shutdown(DnsStream
*stream
, int error
) {
198 assert(stream
->encrypted
);
199 assert(stream
->dnstls_data
.ssl
);
201 if (stream
->server
) {
202 s
= SSL_get1_session(stream
->dnstls_data
.ssl
);
204 if (stream
->server
->dnstls_data
.session
)
205 SSL_SESSION_free(stream
->server
->dnstls_data
.session
);
207 stream
->server
->dnstls_data
.session
= s
;
211 if (error
== ETIMEDOUT
) {
213 r
= SSL_shutdown(stream
->dnstls_data
.ssl
);
215 if (!stream
->dnstls_data
.shutdown
) {
216 stream
->dnstls_data
.shutdown
= true;
217 dns_stream_ref(stream
);
220 stream
->dnstls_events
= 0;
222 r
= dnstls_flush_write_buffer(stream
);
228 ssl_error
= SSL_get_error(stream
->dnstls_data
.ssl
, r
);
229 if (IN_SET(ssl_error
, SSL_ERROR_WANT_READ
, SSL_ERROR_WANT_WRITE
)) {
230 stream
->dnstls_events
= ssl_error
== SSL_ERROR_WANT_READ
? EPOLLIN
: EPOLLOUT
;
231 r
= dnstls_flush_write_buffer(stream
);
232 if (r
< 0 && r
!= -EAGAIN
)
235 if (!stream
->dnstls_data
.shutdown
) {
236 stream
->dnstls_data
.shutdown
= true;
237 dns_stream_ref(stream
);
240 } else if (ssl_error
== SSL_ERROR_SYSCALL
) {
242 log_debug_errno(errno
, "Failed to invoke SSL_shutdown, ignoring: %m");
246 ERR_error_string_n(ssl_error
, errbuf
, sizeof(errbuf
));
247 log_debug("Failed to invoke SSL_shutdown, ignoring: %s", errbuf
);
251 stream
->dnstls_events
= 0;
252 r
= dnstls_flush_write_buffer(stream
);
260 ssize_t
dnstls_stream_write(DnsStream
*stream
, const char *buf
, size_t count
) {
265 assert(stream
->encrypted
);
266 assert(stream
->dnstls_data
.ssl
);
270 ss
= r
= SSL_write(stream
->dnstls_data
.ssl
, buf
, count
);
272 error
= SSL_get_error(stream
->dnstls_data
.ssl
, r
);
273 if (IN_SET(error
, SSL_ERROR_WANT_READ
, SSL_ERROR_WANT_WRITE
)) {
274 stream
->dnstls_events
= error
== SSL_ERROR_WANT_READ
? EPOLLIN
: EPOLLOUT
;
276 } else if (error
== SSL_ERROR_ZERO_RETURN
) {
277 stream
->dnstls_events
= 0;
282 ERR_error_string_n(error
, errbuf
, sizeof(errbuf
));
283 log_debug("Failed to invoke SSL_write: %s", errbuf
);
284 stream
->dnstls_events
= 0;
288 stream
->dnstls_events
= 0;
290 r
= dnstls_flush_write_buffer(stream
);
297 ssize_t
dnstls_stream_read(DnsStream
*stream
, void *buf
, size_t count
) {
302 assert(stream
->encrypted
);
303 assert(stream
->dnstls_data
.ssl
);
307 ss
= r
= SSL_read(stream
->dnstls_data
.ssl
, buf
, count
);
309 error
= SSL_get_error(stream
->dnstls_data
.ssl
, r
);
310 if (IN_SET(error
, SSL_ERROR_WANT_READ
, SSL_ERROR_WANT_WRITE
)) {
311 stream
->dnstls_events
= error
== SSL_ERROR_WANT_READ
? EPOLLIN
: EPOLLOUT
;
313 } else if (error
== SSL_ERROR_ZERO_RETURN
) {
314 stream
->dnstls_events
= 0;
319 ERR_error_string_n(error
, errbuf
, sizeof(errbuf
));
320 log_debug("Failed to invoke SSL_read: %s", errbuf
);
321 stream
->dnstls_events
= 0;
325 stream
->dnstls_events
= 0;
327 /* flush write buffer in cache of renegotiation */
328 r
= dnstls_flush_write_buffer(stream
);
335 void dnstls_server_init(DnsServer
*server
) {
338 server
->dnstls_data
.ctx
= SSL_CTX_new(TLS_client_method());
339 if (server
->dnstls_data
.ctx
) {
340 SSL_CTX_set_min_proto_version(server
->dnstls_data
.ctx
, TLS1_2_VERSION
);
341 SSL_CTX_set_options(server
->dnstls_data
.ctx
, SSL_OP_NO_COMPRESSION
);
345 void dnstls_server_free(DnsServer
*server
) {
348 if (server
->dnstls_data
.ctx
)
349 SSL_CTX_free(server
->dnstls_data
.ctx
);
351 if (server
->dnstls_data
.session
)
352 SSL_SESSION_free(server
->dnstls_data
.session
);