Skip to content

Commit 70d4849

Browse files
committed
tls: openssl: add support for client certificate verification
Signed-off-by: Edward Lancaster <edward.lancaster@siemens.com>
1 parent 3c678d1 commit 70d4849

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

include/fluent-bit/tls/flb_tls.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct flb_tls_backend {
7676

7777
/* Additional settings */
7878
int (*context_alpn_set) (void *, const char *);
79+
int (*context_set_verify_client) (void *, int);
7980

8081
/* TLS Protocol version */
8182
int (*set_minmax_proto) (struct flb_tls *tls, const char *, const char *);
@@ -104,6 +105,7 @@ struct flb_tls_backend {
104105
/* Main TLS context */
105106
struct flb_tls {
106107
int verify; /* FLB_TRUE | FLB_FALSE */
108+
int verify_client; /* Verify client certificate */
107109
int debug; /* Debug level */
108110
char *vhost; /* Virtual hostname for SNI */
109111
int mode; /* Client or Server */
@@ -131,6 +133,7 @@ struct flb_tls *flb_tls_create(int mode,
131133
int flb_tls_destroy(struct flb_tls *tls);
132134

133135
int flb_tls_set_alpn(struct flb_tls *tls, const char *alpn);
136+
int flb_tls_set_verify_client(struct flb_tls *tls, int verify_client);
134137

135138
int flb_tls_set_verify_hostname(struct flb_tls *tls, int verify_hostname);
136139
#if defined(FLB_SYSTEM_WINDOWS)

src/tls/flb_tls.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ struct flb_config_map tls_configmap[] = {
3535
0, FLB_FALSE, 0,
3636
"Force certificate validation",
3737
},
38+
{
39+
FLB_CONFIG_MAP_BOOL, "tls.verify_client_cert", "off",
40+
0, FLB_FALSE, 0,
41+
"Enable or disable client certificate verification",
42+
},
3843
{
3944
FLB_CONFIG_MAP_INT, "tls.debug", "1",
4045
0, FLB_FALSE, 0,
@@ -285,6 +290,21 @@ int flb_tls_set_alpn(struct flb_tls *tls, const char *alpn)
285290
return 0;
286291
}
287292

293+
int flb_tls_set_verify_client(struct flb_tls *tls, int verify_client)
294+
{
295+
if (!tls) {
296+
return -1;
297+
}
298+
299+
tls->verify_client = verify_client;
300+
301+
if (tls->ctx && tls->api->context_set_verify_client) {
302+
return tls->api->context_set_verify_client(tls->ctx, verify_client);
303+
}
304+
305+
return 0;
306+
}
307+
288308
int flb_tls_set_verify_hostname(struct flb_tls *tls, int verify_hostname)
289309
{
290310
if (!tls) {

src/tls/openssl.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,20 @@ int tls_context_alpn_set(void *ctx_backend, const char *alpn)
301301
return result;
302302
}
303303

304+
static int tls_context_set_verify_client(void *ctx_backend, int verify_client)
305+
{
306+
struct tls_context *ctx = ctx_backend;
307+
int mode;
308+
309+
if (ctx->mode == FLB_TLS_SERVER_MODE && verify_client == FLB_TRUE) {
310+
mode = SSL_CTX_get_verify_mode(ctx->ctx);
311+
mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
312+
SSL_CTX_set_verify(ctx->ctx, mode, NULL);
313+
}
314+
315+
return 0;
316+
}
317+
304318
#ifdef _MSC_VER
305319
/* Parse certstore_name prefix like
306320
*
@@ -801,7 +815,8 @@ static void *tls_context_create(int verify,
801815
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, NULL);
802816
}
803817
else {
804-
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
818+
int verify_flags = SSL_VERIFY_PEER;
819+
SSL_CTX_set_verify(ssl_ctx, verify_flags, NULL);
805820
}
806821

807822
/* ca_path | ca_file */
@@ -1574,6 +1589,7 @@ static struct flb_tls_backend tls_openssl = {
15741589
.context_create = tls_context_create,
15751590
.context_destroy = tls_context_destroy,
15761591
.context_alpn_set = tls_context_alpn_set,
1592+
.context_set_verify_client = tls_context_set_verify_client,
15771593
.session_alpn_get = tls_session_alpn_get,
15781594
.set_minmax_proto = tls_set_minmax_proto,
15791595
.set_ciphers = tls_set_ciphers,

0 commit comments

Comments
 (0)