summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschwarze <>2020-09-18 16:18:56 +0000
committerschwarze <>2020-09-18 16:18:56 +0000
commit4e89e6316f0d83fc4353025be7399e53c4722ddf (patch)
tree22cef703eb6773ec5445f164a36eb80856fb0432
parentac86bcc7f040728eac3e6d706f159657d41072f3 (diff)
downloadopenbsd-4e89e6316f0d83fc4353025be7399e53c4722ddf.tar.gz
openbsd-4e89e6316f0d83fc4353025be7399e53c4722ddf.tar.bz2
openbsd-4e89e6316f0d83fc4353025be7399e53c4722ddf.zip
If ssl_cert_dup() fails in SSL_set_SSL_CTX(3), return failure
rather than silently leaving a NULL pointer in ssl->cert. Kurt Roeckx fixed the same bug similarly in OpenSSL in 2015. While here, (1) make the code easier to read and more robust by returning right away when ssl still uses the context it was created from and the ctx argument is NULL, rather than doing a lot of work that changes nothing unless data is already corrupt, and (2) use the shorter and more inituitive SSL_CTX_up_ref(3) rather than manually calling CRYPTO_add(3), which means no functional change and is also in the OpenSSL 1.1 branch. OK tb@
-rw-r--r--src/lib/libssl/ssl_lib.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/lib/libssl/ssl_lib.c b/src/lib/libssl/ssl_lib.c
index 73bc05e967..c184f75abe 100644
--- a/src/lib/libssl/ssl_lib.c
+++ b/src/lib/libssl/ssl_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl_lib.c,v 1.229 2020/09/16 07:25:15 schwarze Exp $ */ 1/* $OpenBSD: ssl_lib.c,v 1.230 2020/09/18 16:18:56 schwarze Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -2726,17 +2726,22 @@ SSL_get_SSL_CTX(const SSL *ssl)
2726SSL_CTX * 2726SSL_CTX *
2727SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) 2727SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2728{ 2728{
2729 if (ssl->ctx == ctx) 2729 CERT *new_cert;
2730 return (ssl->ctx); 2730
2731 if (ctx == NULL) 2731 if (ctx == NULL)
2732 ctx = ssl->initial_ctx; 2732 ctx = ssl->initial_ctx;
2733 if (ssl->ctx == ctx)
2734 return (ssl->ctx);
2733 2735
2736 if ((new_cert = ssl_cert_dup(ctx->internal->cert)) == NULL)
2737 return NULL;
2734 ssl_cert_free(ssl->cert); 2738 ssl_cert_free(ssl->cert);
2735 ssl->cert = ssl_cert_dup(ctx->internal->cert); 2739 ssl->cert = new_cert;
2736 2740
2737 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); 2741 SSL_CTX_up_ref(ctx);
2738 SSL_CTX_free(ssl->ctx); /* decrement reference count */ 2742 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2739 ssl->ctx = ctx; 2743 ssl->ctx = ctx;
2744
2740 return (ssl->ctx); 2745 return (ssl->ctx);
2741} 2746}
2742 2747