Skip to content

Commit 44dd39b

Browse files
schwabecron2
authored andcommitted
Change ssl_ctx in struct tls_options to be a pointer
The SSL CTX is shared between all of the instances. So any change to the SSL CTX will affect all instances. Currently the CRL is also reloaded potentially multiple times as each copy of tls_root_ctx has its own crl_last_mtime and crl_last_size values that will be checked if the CRL reload is necessary. Changing it to a pointer will make it more clear that this is shared and also the CRL being reloaded multiple times. Change-Id: I21251a42f94fa1d9de083d2acd95b887658c5760 Signed-off-by: Arne Schwabe <[email protected]> Acked-by: MaxF <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1431 Message-Id: <[email protected]> URL: https://www.mail-archive.com/[email protected]/msg35116.html Signed-off-by: Gert Doering <[email protected]>
1 parent 18d1b1f commit 44dd39b

File tree

9 files changed

+35
-24
lines changed

9 files changed

+35
-24
lines changed

src/openvpn/init.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2964,9 +2964,10 @@ static void
29642964
key_schedule_free(struct key_schedule *ks, bool free_ssl_ctx)
29652965
{
29662966
free_key_ctx_bi(&ks->static_key);
2967-
if (tls_ctx_initialised(&ks->ssl_ctx) && free_ssl_ctx)
2967+
if (tls_ctx_initialised(ks->ssl_ctx) && free_ssl_ctx)
29682968
{
2969-
tls_ctx_free(&ks->ssl_ctx);
2969+
tls_ctx_free(ks->ssl_ctx);
2970+
free(ks->ssl_ctx);
29702971
free_key_ctx(&ks->auth_token_key);
29712972
}
29722973
CLEAR(*ks);
@@ -3121,14 +3122,15 @@ do_init_crypto_tls_c1(struct context *c)
31213122
{
31223123
const struct options *options = &c->options;
31233124

3124-
if (!tls_ctx_initialised(&c->c1.ks.ssl_ctx))
3125+
if (!tls_ctx_initialised(c->c1.ks.ssl_ctx))
31253126
{
31263127
/*
31273128
* Initialize the OpenSSL library's global
31283129
* SSL context.
31293130
*/
3130-
init_ssl(options, &(c->c1.ks.ssl_ctx), c->c0 && c->c0->uid_gid_chroot_set);
3131-
if (!tls_ctx_initialised(&c->c1.ks.ssl_ctx))
3131+
ASSERT(NULL == c->c1.ks.ssl_ctx);
3132+
c->c1.ks.ssl_ctx = init_ssl(options, c->c0 && c->c0->uid_gid_chroot_set);
3133+
if (!tls_ctx_initialised(c->c1.ks.ssl_ctx))
31323134
{
31333135
switch (auth_retry_get())
31343136
{

src/openvpn/openvpn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct key_schedule
6060
struct key_ctx_bi static_key;
6161

6262
/* our global SSL context */
63-
struct tls_root_ctx ssl_ctx;
63+
struct tls_root_ctx *ssl_ctx;
6464

6565
/* optional TLS control channel wrapping */
6666
struct key_type tls_auth_key_type;

src/openvpn/ssl.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -507,18 +507,19 @@ tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, bool crl_
507507
* Initialize SSL context.
508508
* All files are in PEM format.
509509
*/
510-
void
511-
init_ssl(const struct options *options, struct tls_root_ctx *new_ctx, bool in_chroot)
510+
struct tls_root_ctx *
511+
init_ssl(const struct options *options, bool in_chroot)
512512
{
513-
ASSERT(NULL != new_ctx);
514-
515513
tls_clear_error();
516514

517515
if (key_is_external(options))
518516
{
519517
load_xkey_provider();
520518
}
521519

520+
struct tls_root_ctx *new_ctx;
521+
ALLOC_OBJ_CLEAR(new_ctx, struct tls_root_ctx);
522+
522523
if (options->tls_server)
523524
{
524525
tls_ctx_server_new(new_ctx);
@@ -664,12 +665,13 @@ init_ssl(const struct options *options, struct tls_root_ctx *new_ctx, bool in_ch
664665
#endif
665666

666667
tls_clear_error();
667-
return;
668+
return new_ctx;
668669

669670
err:
670671
tls_clear_error();
671672
tls_ctx_free(new_ctx);
672-
return;
673+
free(new_ctx);
674+
return NULL;
673675
}
674676

675677
/*
@@ -821,7 +823,7 @@ key_state_init(struct tls_session *session, struct key_state *ks)
821823
* Build TLS object that reads/writes ciphertext
822824
* to/from memory BIOs.
823825
*/
824-
key_state_ssl_init(&ks->ks_ssl, &session->opt->ssl_ctx, session->opt->server, session);
826+
key_state_ssl_init(&ks->ks_ssl, session->opt->ssl_ctx, session->opt->server, session);
825827

826828
/* Set control-channel initiation mode */
827829
ks->initial_opcode = session->initial_opcode;
@@ -872,11 +874,12 @@ key_state_init(struct tls_session *session, struct key_state *ks)
872874

873875
/*
874876
* Attempt CRL reload before TLS negotiation. Won't be performed if
875-
* the file was not modified since the last reload
877+
* the file was not modified since the last reload. This affects
878+
* all instances (all instances share the same context).
876879
*/
877880
if (session->opt->crl_file && !(session->opt->ssl_flags & SSLF_CRL_VERIFY_DIR))
878881
{
879-
tls_ctx_reload_crl(&session->opt->ssl_ctx, session->opt->crl_file,
882+
tls_ctx_reload_crl(session->opt->ssl_ctx, session->opt->crl_file,
880883
session->opt->crl_file_inline);
881884
}
882885
}

src/openvpn/ssl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void free_ssl_lib(void);
144144
* Build master SSL context object that serves for the whole of OpenVPN
145145
* instantiation
146146
*/
147-
void init_ssl(const struct options *options, struct tls_root_ctx *ctx, bool in_chroot);
147+
struct tls_root_ctx *init_ssl(const struct options *options, bool in_chroot);
148148

149149
/** @addtogroup control_processor
150150
* @{ */

src/openvpn/ssl_common.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,10 @@ struct tls_wrap_ctx
305305
*/
306306
struct tls_options
307307
{
308-
/* our master TLS context from which all SSL objects derived */
309-
struct tls_root_ctx ssl_ctx;
308+
/* our master TLS context from which all SSL objects are derived,
309+
* this context is shared between all instances in p2pm with
310+
* inherit_context_child. */
311+
struct tls_root_ctx *ssl_ctx;
310312

311313
/* data channel cipher, hmac, and key lengths */
312314
struct key_type key_type;

src/openvpn/ssl_mbedtls.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ tls_ctx_free(struct tls_root_ctx *ctx)
157157
bool
158158
tls_ctx_initialised(struct tls_root_ctx *ctx)
159159
{
160-
ASSERT(NULL != ctx);
161-
return ctx->initialised;
160+
/* either this should be NULL or should be non-null and then have a
161+
* valid TLS ctx inside as well */
162+
ASSERT(NULL == ctx || ctx->initialised);
163+
return ctx != NULL;
162164
}
163165
#if !defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT)
164166
/*

src/openvpn/ssl_openssl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ tls_ctx_free(struct tls_root_ctx *ctx)
147147
bool
148148
tls_ctx_initialised(struct tls_root_ctx *ctx)
149149
{
150-
ASSERT(NULL != ctx);
151-
return NULL != ctx->ctx;
150+
/* either this should be NULL or should be non-null and then have a
151+
* valid TLS ctx inside as well */
152+
ASSERT(ctx == NULL || ctx->ctx != NULL);
153+
return ctx != NULL;
152154
}
153155

154156
bool

src/openvpn/ssl_verify_mbedtls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ bool
572572
tls_verify_crl_missing(const struct tls_options *opt)
573573
{
574574
if (opt->crl_file && !(opt->ssl_flags & SSLF_CRL_VERIFY_DIR)
575-
&& (opt->ssl_ctx.crl == NULL || opt->ssl_ctx.crl->version == 0))
575+
&& (opt->ssl_ctx->crl == NULL || opt->ssl_ctx->crl->version == 0))
576576
{
577577
return true;
578578
}

src/openvpn/ssl_verify_openssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ tls_verify_crl_missing(const struct tls_options *opt)
799799
return false;
800800
}
801801

802-
X509_STORE *store = SSL_CTX_get_cert_store(opt->ssl_ctx.ctx);
802+
X509_STORE *store = SSL_CTX_get_cert_store(opt->ssl_ctx->ctx);
803803
if (!store)
804804
{
805805
crypto_msg(M_FATAL, "Cannot get certificate store");

0 commit comments

Comments
 (0)