Skip to content

Commit e07eeee

Browse files
committed
tls: omit SNI for IP literal hosts
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent 6474297 commit e07eeee

1 file changed

Lines changed: 94 additions & 3 deletions

File tree

src/tls/openssl.c

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <stdio.h>
2121
#include <stdlib.h>
22+
#include <string.h>
2223

2324
#include <fluent-bit/flb_info.h>
2425
#include <fluent-bit/flb_str.h>
@@ -40,11 +41,15 @@
4041
#ifdef FLB_SYSTEM_WINDOWS
4142
#define strtok_r(str, delimiter, context) \
4243
strtok_s(str, delimiter, context)
44+
#include <winsock2.h>
45+
#include <ws2tcpip.h>
4346
#include <wincrypt.h>
4447
#ifndef CERT_FIND_SHA256_HASH
4548
/* Older SDKs may not define this */
4649
#define CERT_FIND_SHA256_HASH 0x0001000d
4750
#endif
51+
#else
52+
#include <arpa/inet.h>
4853
#endif
4954

5055
/*
@@ -78,6 +83,83 @@ struct tls_session {
7883
struct tls_context *parent; /* parent struct tls_context ref */
7984
};
8085

86+
static int host_is_ip_literal(const char *hostname, char *normalized, size_t normalized_size)
87+
{
88+
char buffer[256];
89+
size_t hostname_len;
90+
size_t lookup_len;
91+
const char *lookup;
92+
const char *bracket_end;
93+
const char *zone_id;
94+
struct in_addr addr4;
95+
struct in6_addr addr6;
96+
int ret;
97+
98+
if (hostname == NULL || hostname[0] == '\0') {
99+
return FLB_FALSE;
100+
}
101+
102+
ret = FLB_FALSE;
103+
lookup = hostname;
104+
hostname_len = strlen(hostname);
105+
106+
if (hostname[0] == '[') {
107+
bracket_end = strchr(hostname + 1, ']');
108+
if (bracket_end == NULL) {
109+
return FLB_FALSE;
110+
}
111+
112+
lookup = hostname + 1;
113+
lookup_len = bracket_end - lookup;
114+
}
115+
else {
116+
lookup_len = hostname_len;
117+
}
118+
119+
zone_id = memchr(lookup, '%', lookup_len);
120+
if (zone_id != NULL) {
121+
lookup_len = zone_id - lookup;
122+
}
123+
124+
if (lookup_len == 0 || lookup_len >= sizeof(buffer)) {
125+
return FLB_FALSE;
126+
}
127+
128+
memcpy(buffer, lookup, lookup_len);
129+
buffer[lookup_len] = '\0';
130+
131+
if (inet_pton(AF_INET, buffer, &addr4) == 1) {
132+
ret = FLB_TRUE;
133+
}
134+
135+
if (inet_pton(AF_INET6, buffer, &addr6) == 1) {
136+
ret = FLB_TRUE;
137+
}
138+
139+
if (ret != FLB_TRUE) {
140+
return FLB_FALSE;
141+
}
142+
143+
if (normalized != NULL) {
144+
if (normalized_size <= lookup_len) {
145+
return FLB_FALSE;
146+
}
147+
148+
memcpy(normalized, buffer, lookup_len + 1);
149+
}
150+
151+
return FLB_TRUE;
152+
}
153+
154+
static void setup_sni(struct tls_session *session, const char *hostname)
155+
{
156+
if (host_is_ip_literal(hostname, NULL, 0) == FLB_TRUE) {
157+
return;
158+
}
159+
160+
SSL_set_tlsext_host_name(session->ssl, hostname);
161+
}
162+
81163
static int tls_init(void)
82164
{
83165
/*
@@ -1521,6 +1603,8 @@ static int tls_net_write(struct flb_tls_session *session,
15211603
int setup_hostname_validation(struct tls_session *session, const char *hostname)
15221604
{
15231605
X509_VERIFY_PARAM *param;
1606+
char normalized_ip[256];
1607+
int ret;
15241608

15251609
param = SSL_get0_param(session->ssl);
15261610

@@ -1530,7 +1614,14 @@ int setup_hostname_validation(struct tls_session *session, const char *hostname)
15301614
}
15311615

15321616
X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
1533-
if (!X509_VERIFY_PARAM_set1_host(param, hostname, 0)) {
1617+
if (host_is_ip_literal(hostname, normalized_ip, sizeof(normalized_ip)) == FLB_TRUE) {
1618+
ret = X509_VERIFY_PARAM_set1_ip_asc(param, normalized_ip);
1619+
}
1620+
else {
1621+
ret = X509_VERIFY_PARAM_set1_host(param, hostname, 0);
1622+
}
1623+
1624+
if (!ret) {
15341625
flb_error("[tls] error: hostname parameter vailidation is failed : %s",
15351626
hostname);
15361627
return -1;
@@ -1581,10 +1672,10 @@ static int tls_net_handshake(struct flb_tls *tls,
15811672
}
15821673

15831674
if (vhost != NULL) {
1584-
SSL_set_tlsext_host_name(session->ssl, vhost);
1675+
setup_sni(session, vhost);
15851676
}
15861677
else if (tls->vhost) {
1587-
SSL_set_tlsext_host_name(session->ssl, tls->vhost);
1678+
setup_sni(session, tls->vhost);
15881679
}
15891680
}
15901681

0 commit comments

Comments
 (0)