From f2a29a3be14de787f303d84cc444d01027d62b74 Mon Sep 17 00:00:00 2001 From: Susant Sahani Date: Mon, 13 May 2024 12:49:11 +0530 Subject: [PATCH] DTLS: Enable certificate verification --- src/netlog/netlog-dtls.c | 23 +++++++++++++++++------ src/netlog/netlog-dtls.h | 5 ++++- src/netlog/systemd-netlogd.c | 4 ++-- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/netlog/netlog-dtls.c b/src/netlog/netlog-dtls.c index be511ba..b4114d8 100644 --- a/src/netlog/netlog-dtls.c +++ b/src/netlog/netlog-dtls.c @@ -125,6 +125,17 @@ int dtls_connect(DTLSManager *m, SocketAddress *address) { BIO_ctrl(bio, BIO_CTRL_DGRAM_SET_CONNECTED, 0, &address); SSL_set_bio(ssl , bio, bio); + /* Cerification verification */ + if (m->auth_mode != OPEN_SSL_CERTIFICATE_AUTH_MODE_NONE && m->auth_mode != OPEN_SSL_CERTIFICATE_AUTH_MODE_INVALID) { + log_debug("TLS: enable certificate verification"); + + SSL_set_ex_data(ssl, 0, m); + SSL_set_verify(ssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, ssl_verify_certificate_validity); + } else { + log_debug("TLS: disable certificate verification"); + SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); + } + r = SSL_connect(ssl); if (r <= 0) return log_error_errno(SYNTHETIC_ERRNO(ENOMEM), @@ -150,10 +161,6 @@ int dtls_connect(DTLSManager *m, SocketAddress *address) { log_debug("DTLS: No certificates."); } - /* Set reference in SSL obj */ - SSL_set_ex_data(ssl, 0, NULL); - SSL_set_ex_data(ssl, 1, NULL); - /* Set and activate timeouts */ BIO_ctrl(bio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); @@ -192,13 +199,17 @@ void dtls_manager_free(DTLSManager *m) { free(m); } -int dtls_manager_init(DTLSManager **ret) { +int dtls_manager_init(OpenSSLCertificateAuthMode auth_mode, DTLSManager **ret) { _cleanup_(dtls_manager_freep) DTLSManager *m = NULL; - m = new0(DTLSManager, 1); + m = new(DTLSManager, 1); if (!m) return log_oom(); + *m = (DTLSManager) { + .auth_mode = auth_mode, + }; + *ret = TAKE_PTR(m); return 0; } diff --git a/src/netlog/netlog-dtls.h b/src/netlog/netlog-dtls.h index e8db2bb..ba530d9 100644 --- a/src/netlog/netlog-dtls.h +++ b/src/netlog/netlog-dtls.h @@ -7,6 +7,7 @@ #include "socket-util.h" #include "openssl-util.h" +#include "netlog-tls.h" typedef struct DTLSManager DTLSManager; @@ -17,10 +18,12 @@ struct DTLSManager { int fd; bool connected; + + OpenSSLCertificateAuthMode auth_mode; }; void dtls_manager_free(DTLSManager *m); -int dtls_manager_init(DTLSManager **ret); +int dtls_manager_init(OpenSSLCertificateAuthMode auth_mode, DTLSManager **ret); int dtls_connect(DTLSManager *m, SocketAddress *addr); void dtls_disconnect(DTLSManager *m); diff --git a/src/netlog/systemd-netlogd.c b/src/netlog/systemd-netlogd.c index d17637b..fcbaa4e 100644 --- a/src/netlog/systemd-netlogd.c +++ b/src/netlog/systemd-netlogd.c @@ -173,10 +173,10 @@ int main(int argc, char **argv) { switch (m->protocol) { case SYSLOG_TRANSMISSION_PROTOCOL_DTLS: - r = dtls_manager_init(&m->dtls); + r = dtls_manager_init(m->auth_mode, &m->dtls); break; case SYSLOG_TRANSMISSION_PROTOCOL_TLS: - r = tls_manager_init( m->auth_mode, &m->tls); + r = tls_manager_init(m->auth_mode, &m->tls); break; default: break;