summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2022-09-03 17:47:47 +0000
committerjsing <>2022-09-03 17:47:47 +0000
commit08cad1d5d4e840186d932d4ee28fa6e1463e3f28 (patch)
tree6da083f1c09ce39a19ff47ac81c6e23adb5057bc /src
parent828c98a83649e3798a814f40f12bf41e09a605a6 (diff)
downloadopenbsd-08cad1d5d4e840186d932d4ee28fa6e1463e3f28.tar.gz
openbsd-08cad1d5d4e840186d932d4ee28fa6e1463e3f28.tar.bz2
openbsd-08cad1d5d4e840186d932d4ee28fa6e1463e3f28.zip
Prepare to provide OPENSSL_cleanup.
OPENSSL_cleanup() cleans up and deallocates memory in use by the library. There are a couple of use cases for this, primarily related to memory leak testing. This will not be called automatically in LibreSSL, which means that OpenSSL's OPENSSL_NO_INIT_ATEXIT is implied. If code wants to clean up then they need to explicitly call this themselves. ok tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/crypto.h5
-rw-r--r--src/lib/libcrypto/crypto_init.c28
-rw-r--r--src/lib/libcrypto/x509/x509_issuer_cache.h3
3 files changed, 30 insertions, 6 deletions
diff --git a/src/lib/libcrypto/crypto.h b/src/lib/libcrypto/crypto.h
index 82372537e1..2d934413ea 100644
--- a/src/lib/libcrypto/crypto.h
+++ b/src/lib/libcrypto/crypto.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto.h,v 1.55 2022/07/12 14:42:48 kn Exp $ */ 1/* $OpenBSD: crypto.h,v 1.56 2022/09/03 17:47:47 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -562,6 +562,9 @@ void ERR_load_CRYPTO_strings(void);
562#define OPENSSL_INIT_ENGINE_ALL_BUILTIN _OPENSSL_INIT_FLAG_NOOP 562#define OPENSSL_INIT_ENGINE_ALL_BUILTIN _OPENSSL_INIT_FLAG_NOOP
563 563
564int OPENSSL_init_crypto(uint64_t opts, const void *settings); 564int OPENSSL_init_crypto(uint64_t opts, const void *settings);
565#if defined(LIBRESSL_NEXT_API) || defined(LIBRESSL_INTERNAL)
566void OPENSSL_cleanup(void);
567#endif
565 568
566#ifdef __cplusplus 569#ifdef __cplusplus
567} 570}
diff --git a/src/lib/libcrypto/crypto_init.c b/src/lib/libcrypto/crypto_init.c
index 67e7920890..69ba62991e 100644
--- a/src/lib/libcrypto/crypto_init.c
+++ b/src/lib/libcrypto/crypto_init.c
@@ -19,17 +19,21 @@
19#include <pthread.h> 19#include <pthread.h>
20#include <stdio.h> 20#include <stdio.h>
21 21
22#include <openssl/objects.h>
23#include <openssl/conf.h> 22#include <openssl/conf.h>
24#include <openssl/evp.h> 23#include <openssl/engine.h>
25#include <openssl/err.h> 24#include <openssl/err.h>
25#include <openssl/evp.h>
26#include <openssl/objects.h>
26 27
27#include "cryptlib.h" 28#include "cryptlib.h"
29#include "x509_issuer_cache.h"
28 30
29int OpenSSL_config(const char *); 31int OpenSSL_config(const char *);
30int OpenSSL_no_config(void); 32int OpenSSL_no_config(void);
31 33
34static pthread_once_t crypto_init_once = PTHREAD_ONCE_INIT;
32static pthread_t crypto_init_thread; 35static pthread_t crypto_init_thread;
36static int crypto_init_cleaned_up;
33 37
34static void 38static void
35OPENSSL_init_crypto_internal(void) 39OPENSSL_init_crypto_internal(void)
@@ -45,12 +49,15 @@ OPENSSL_init_crypto_internal(void)
45int 49int
46OPENSSL_init_crypto(uint64_t opts, const void *settings) 50OPENSSL_init_crypto(uint64_t opts, const void *settings)
47{ 51{
48 static pthread_once_t once = PTHREAD_ONCE_INIT; 52 if (crypto_init_cleaned_up) {
53 CRYPTOerror(ERR_R_INIT_FAIL);
54 return 0;
55 }
49 56
50 if (pthread_equal(pthread_self(), crypto_init_thread)) 57 if (pthread_equal(pthread_self(), crypto_init_thread))
51 return 1; /* don't recurse */ 58 return 1; /* don't recurse */
52 59
53 if (pthread_once(&once, OPENSSL_init_crypto_internal) != 0) 60 if (pthread_once(&crypto_init_once, OPENSSL_init_crypto_internal) != 0)
54 return 0; 61 return 0;
55 62
56 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) && 63 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) &&
@@ -63,3 +70,16 @@ OPENSSL_init_crypto(uint64_t opts, const void *settings)
63 70
64 return 1; 71 return 1;
65} 72}
73
74void
75OPENSSL_cleanup(void)
76{
77 /* This currently calls init... */
78 ERR_free_strings();
79
80 ENGINE_cleanup();
81 EVP_cleanup();
82 x509_issuer_cache_free();
83
84 crypto_init_cleaned_up = 1;
85}
diff --git a/src/lib/libcrypto/x509/x509_issuer_cache.h b/src/lib/libcrypto/x509/x509_issuer_cache.h
index 6dedde75f1..3afe65bd49 100644
--- a/src/lib/libcrypto/x509/x509_issuer_cache.h
+++ b/src/lib/libcrypto/x509/x509_issuer_cache.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: x509_issuer_cache.h,v 1.1 2020/09/11 14:30:51 beck Exp $ */ 1/* $OpenBSD: x509_issuer_cache.h,v 1.2 2022/09/03 17:47:47 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2020 Bob Beck <beck@openbsd.org> 3 * Copyright (c) 2020 Bob Beck <beck@openbsd.org>
4 * 4 *
@@ -41,6 +41,7 @@ int x509_issuer_cache_set_max(size_t max);
41int x509_issuer_cache_find(unsigned char *parent_md, unsigned char *child_md); 41int x509_issuer_cache_find(unsigned char *parent_md, unsigned char *child_md);
42void x509_issuer_cache_add(unsigned char *parent_md, unsigned char *child_md, 42void x509_issuer_cache_add(unsigned char *parent_md, unsigned char *child_md,
43 int valid); 43 int valid);
44void x509_issuer_cache_free();
44 45
45__END_HIDDEN_DECLS 46__END_HIDDEN_DECLS
46 47