2aacef
From 57bdd8a488d544282dcc71e6a23987ded71ac64d Mon Sep 17 00:00:00 2001
2aacef
From: Benjamin Fogle <benfogle@gmail.com>
2aacef
Date: Thu, 17 Nov 2022 09:52:50 -0500
2aacef
Subject: [PATCH] resolved: Fix OpenSSL error messages
2aacef
2aacef
(cherry picked from commit f4a49d1c58578cb8d759dc6266a23d1acabdc38f)
2aacef
2aacef
Related: #2138081
2aacef
---
2aacef
 src/resolve/resolved-dnstls-openssl.c | 65 +++++++++++----------------
2aacef
 1 file changed, 26 insertions(+), 39 deletions(-)
2aacef
2aacef
diff --git a/src/resolve/resolved-dnstls-openssl.c b/src/resolve/resolved-dnstls-openssl.c
2aacef
index 4d3a88c8da..4a0132ad3d 100644
2aacef
--- a/src/resolve/resolved-dnstls-openssl.c
2aacef
+++ b/src/resolve/resolved-dnstls-openssl.c
2aacef
@@ -14,6 +14,19 @@
2aacef
 #include "resolved-dnstls.h"
2aacef
 #include "resolved-manager.h"
2aacef
 
2aacef
+static char *dnstls_error_string(int ssl_error, char *buf, size_t count) {
2aacef
+        assert(buf || count == 0);
2aacef
+        if (ssl_error == SSL_ERROR_SSL)
2aacef
+                ERR_error_string_n(ERR_get_error(), buf, count);
2aacef
+        else
2aacef
+                snprintf(buf, count, "SSL_get_error()=%d", ssl_error);
2aacef
+        return buf;
2aacef
+}
2aacef
+
2aacef
+#define DNSTLS_ERROR_BUFSIZE 256
2aacef
+#define DNSTLS_ERROR_STRING(error) \
2aacef
+        dnstls_error_string((error), (char[DNSTLS_ERROR_BUFSIZE]){}, DNSTLS_ERROR_BUFSIZE)
2aacef
+
2aacef
 static int dnstls_flush_write_buffer(DnsStream *stream) {
2aacef
         ssize_t ss;
2aacef
 
2aacef
@@ -97,26 +110,18 @@ int dnstls_stream_connect_tls(DnsStream *stream, DnsServer *server) {
2aacef
 
2aacef
         if (server->server_name) {
2aacef
                 r = SSL_set_tlsext_host_name(s, server->server_name);
2aacef
-                if (r <= 0) {
2aacef
-                        char errbuf[256];
2aacef
-
2aacef
-                        error = ERR_get_error();
2aacef
-                        ERR_error_string_n(error, errbuf, sizeof(errbuf));
2aacef
-                        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to set server name: %s", errbuf);
2aacef
-                }
2aacef
+                if (r <= 0)
2aacef
+                        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
2aacef
+                                               "Failed to set server name: %s", DNSTLS_ERROR_STRING(SSL_ERROR_SSL));
2aacef
         }
2aacef
 
2aacef
         ERR_clear_error();
2aacef
         stream->dnstls_data.handshake = SSL_do_handshake(s);
2aacef
         if (stream->dnstls_data.handshake <= 0) {
2aacef
                 error = SSL_get_error(s, stream->dnstls_data.handshake);
2aacef
-                if (!IN_SET(error, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE)) {
2aacef
-                        char errbuf[256];
2aacef
-
2aacef
-                        ERR_error_string_n(error, errbuf, sizeof(errbuf));
2aacef
+                if (!IN_SET(error, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE))
2aacef
                         return log_debug_errno(SYNTHETIC_ERRNO(ECONNREFUSED),
2aacef
-                                               "Failed to invoke SSL_do_handshake: %s", errbuf);
2aacef
-                }
2aacef
+                                               "Failed to invoke SSL_do_handshake: %s", DNSTLS_ERROR_STRING(error));
2aacef
         }
2aacef
 
2aacef
         stream->encrypted = true;
2aacef
@@ -177,12 +182,8 @@ int dnstls_stream_on_io(DnsStream *stream, uint32_t revents) {
2aacef
                         } else if (error == SSL_ERROR_SYSCALL) {
2aacef
                                 if (errno > 0)
2aacef
                                         log_debug_errno(errno, "Failed to invoke SSL_shutdown, ignoring: %m");
2aacef
-                        } else {
2aacef
-                                char errbuf[256];
2aacef
-
2aacef
-                                ERR_error_string_n(error, errbuf, sizeof(errbuf));
2aacef
-                                log_debug("Failed to invoke SSL_shutdown, ignoring: %s", errbuf);
2aacef
-                        }
2aacef
+                        } else
2aacef
+                                log_debug("Failed to invoke SSL_shutdown, ignoring: %s", DNSTLS_ERROR_STRING(error));
2aacef
                 }
2aacef
 
2aacef
                 stream->dnstls_events = 0;
2aacef
@@ -206,14 +207,10 @@ int dnstls_stream_on_io(DnsStream *stream, uint32_t revents) {
2aacef
                                         return r;
2aacef
 
2aacef
                                 return -EAGAIN;
2aacef
-                        } else {
2aacef
-                                char errbuf[256];
2aacef
-
2aacef
-                                ERR_error_string_n(error, errbuf, sizeof(errbuf));
2aacef
+                        } else
2aacef
                                 return log_debug_errno(SYNTHETIC_ERRNO(ECONNREFUSED),
2aacef
                                                        "Failed to invoke SSL_do_handshake: %s",
2aacef
-                                                       errbuf);
2aacef
-                        }
2aacef
+                                                       DNSTLS_ERROR_STRING(error));
2aacef
                 }
2aacef
 
2aacef
                 stream->dnstls_events = 0;
2aacef
@@ -275,12 +272,8 @@ int dnstls_stream_shutdown(DnsStream *stream, int error) {
2aacef
                         } else if (ssl_error == SSL_ERROR_SYSCALL) {
2aacef
                                 if (errno > 0)
2aacef
                                         log_debug_errno(errno, "Failed to invoke SSL_shutdown, ignoring: %m");
2aacef
-                        } else {
2aacef
-                                char errbuf[256];
2aacef
-
2aacef
-                                ERR_error_string_n(ssl_error, errbuf, sizeof(errbuf));
2aacef
-                                log_debug("Failed to invoke SSL_shutdown, ignoring: %s", errbuf);
2aacef
-                        }
2aacef
+                        } else
2aacef
+                                log_debug("Failed to invoke SSL_shutdown, ignoring: %s", DNSTLS_ERROR_STRING(ssl_error));
2aacef
                 }
2aacef
 
2aacef
                 stream->dnstls_events = 0;
2aacef
@@ -307,10 +300,7 @@ static ssize_t dnstls_stream_write(DnsStream *stream, const char *buf, size_t co
2aacef
                         stream->dnstls_events = 0;
2aacef
                         ss = 0;
2aacef
                 } else {
2aacef
-                        char errbuf[256];
2aacef
-
2aacef
-                        ERR_error_string_n(error, errbuf, sizeof(errbuf));
2aacef
-                        log_debug("Failed to invoke SSL_write: %s", errbuf);
2aacef
+                        log_debug("Failed to invoke SSL_write: %s", DNSTLS_ERROR_STRING(error));
2aacef
                         stream->dnstls_events = 0;
2aacef
                         ss = -EPIPE;
2aacef
                 }
2aacef
@@ -375,10 +365,7 @@ ssize_t dnstls_stream_read(DnsStream *stream, void *buf, size_t count) {
2aacef
                         stream->dnstls_events = 0;
2aacef
                         ss = 0;
2aacef
                 } else {
2aacef
-                        char errbuf[256];
2aacef
-
2aacef
-                        ERR_error_string_n(error, errbuf, sizeof(errbuf));
2aacef
-                        log_debug("Failed to invoke SSL_read: %s", errbuf);
2aacef
+                        log_debug("Failed to invoke SSL_read: %s", DNSTLS_ERROR_STRING(error));
2aacef
                         stream->dnstls_events = 0;
2aacef
                         ss = -EPIPE;
2aacef
                 }