Skip to content

Commit 34c9f5e

Browse files
cosmo0920edsiper
authored andcommitted
tls: Verify vhost when tls.verify is enabled
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent cdbfe4c commit 34c9f5e

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/tls/openssl.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <openssl/ssl.h>
2626
#include <openssl/err.h>
2727
#include <openssl/opensslv.h>
28+
#include <openssl/x509v3.h>
2829

2930
#ifdef FLB_SYSTEM_WINDOWS
3031
#define strtok_r(str, delimiter, context) \
@@ -636,11 +637,33 @@ static int tls_net_write(struct flb_tls_session *session,
636637
return ret;
637638
}
638639

640+
int setup_hostname_validation(struct tls_session *session, const char *hostname)
641+
{
642+
X509_VERIFY_PARAM *param;
643+
644+
param = SSL_get0_param(session->ssl);
645+
646+
if (!param) {
647+
flb_error("[tls] error: ssl context is invalid");
648+
return -1;
649+
}
650+
651+
X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
652+
if (!X509_VERIFY_PARAM_set1_host(param, hostname, 0)) {
653+
flb_error("[tls] error: hostname parameter vailidation is failed : %s",
654+
hostname);
655+
return -1;
656+
}
657+
658+
return 0;
659+
}
660+
639661
static int tls_net_handshake(struct flb_tls *tls,
640662
char *vhost,
641663
void *ptr_session)
642664
{
643665
int ret = 0;
666+
long ssl_code = 0;
644667
char err_buf[256];
645668
struct tls_session *session = ptr_session;
646669
struct tls_context *ctx;
@@ -669,6 +692,20 @@ static int tls_net_handshake(struct flb_tls *tls,
669692
}
670693
}
671694

695+
if (tls->verify == FLB_TRUE) {
696+
if (vhost != NULL) {
697+
ret = setup_hostname_validation(session, vhost);
698+
}
699+
else if (tls->vhost) {
700+
ret = setup_hostname_validation(session, tls->vhost);
701+
}
702+
703+
if (ret != 0) {
704+
pthread_mutex_unlock(&ctx->mutex);
705+
return -1;
706+
}
707+
}
708+
672709
ERR_clear_error();
673710

674711
if (tls->mode == FLB_TLS_CLIENT_MODE) {
@@ -686,7 +723,14 @@ static int tls_net_handshake(struct flb_tls *tls,
686723
// The SSL_ERROR_SYSCALL with errno value of 0 indicates unexpected
687724
// EOF from the peer. This is fixed in OpenSSL 3.0.
688725
if (ret == 0) {
689-
flb_error("[tls] error: unexpected EOF");
726+
ssl_code = SSL_get_verify_result(session->ssl);
727+
if (ssl_code != X509_V_OK) {
728+
flb_error("[tls] error: unexpected EOF with reason: %s",
729+
ERR_reason_error_string(ERR_get_error()));
730+
}
731+
else {
732+
flb_error("[tls] error: unexpected EOF");
733+
}
690734
} else {
691735
ERR_error_string_n(ret, err_buf, sizeof(err_buf)-1);
692736
flb_error("[tls] error: %s", err_buf);

0 commit comments

Comments
 (0)