]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
fix: enhance deinitialization of TLS context while handling error
authorJan Hák <jan.hak@nic.cz>
Wed, 15 Nov 2023 12:57:26 +0000 (13:57 +0100)
committerDaniel Salzman <daniel.salzman@nic.cz>
Thu, 16 Nov 2023 07:25:21 +0000 (08:25 +0100)
src/utils/common/tls.c

index 7d7419c6a2a798a4233e3a62c3af1ff7ae46a2f2..a886ac238c6ed0d6b4eb96a3449d34d37aed9467 100644 (file)
@@ -513,6 +513,7 @@ int tls_ctx_init(tls_ctx_t *ctx, const tls_params_t *params,
                                     ctx->credentials);
        if (ret != GNUTLS_E_SUCCESS) {
                gnutls_deinit(ctx->session);
+               ctx->session = NULL;
                return KNOT_ERROR;
        }
 
@@ -529,7 +530,7 @@ int tls_ctx_setup_remote_endpoint(tls_ctx_t *ctx, const gnutls_datum_t *alpn,
        if (alpn != NULL) {
                ret = gnutls_alpn_set_protocols(ctx->session, alpn, alpn_size, 0);
                if (ret != GNUTLS_E_SUCCESS) {
-                       gnutls_deinit(ctx->session);
+                       tls_ctx_deinit(ctx);
                        return KNOT_NET_ECONNECT;
                }
        }
@@ -540,7 +541,7 @@ int tls_ctx_setup_remote_endpoint(tls_ctx_t *ctx, const gnutls_datum_t *alpn,
                ret = gnutls_set_default_priority(ctx->session);
        }
        if (ret != GNUTLS_E_SUCCESS) {
-               gnutls_deinit(ctx->session);
+               tls_ctx_deinit(ctx);
                return KNOT_EINVAL;
        }
 
@@ -548,7 +549,7 @@ int tls_ctx_setup_remote_endpoint(tls_ctx_t *ctx, const gnutls_datum_t *alpn,
                ret = gnutls_server_name_set(ctx->session, GNUTLS_NAME_DNS, remote,
                                             strlen(remote));
                if (ret != GNUTLS_E_SUCCESS) {
-                       gnutls_deinit(ctx->session);
+                       tls_ctx_deinit(ctx);
                        return KNOT_EINVAL;
                }
        }
@@ -570,7 +571,7 @@ int tls_ctx_connect(tls_ctx_t *ctx, int sockfd, bool fastopen,
                gnutls_transport_set_fastopen(ctx->session, sockfd, (struct sockaddr *)addr,
                                              sockaddr_len(addr), 0);
 #else
-               gnutls_deinit(ctx->session);
+               tls_ctx_deinit(ctx);
                return KNOT_ENOTSUP;
 #endif
        } else {
@@ -592,7 +593,7 @@ int tls_ctx_connect(tls_ctx_t *ctx, int sockfd, bool fastopen,
                if (ret != GNUTLS_E_SUCCESS && gnutls_error_is_fatal(ret) == 0) {
                        if (poll(&pfd, 1, 1000 * ctx->wait) != 1) {
                                WARN("TLS, peer took too long to respond");
-                               gnutls_deinit(ctx->session);
+                               tls_ctx_deinit(ctx);
                                return KNOT_NET_ETIMEOUT;
                        }
                }