summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbeck <>2018-03-17 16:20:01 +0000
committerbeck <>2018-03-17 16:20:01 +0000
commitd18ae25f28e72831dc3c51f78e0735342540098b (patch)
tree12cc674e7652f4d67e57ec9e1882e6e824b4808d /src
parent736fdc7bedf7fe8b17717032f7380c5c6e247d0d (diff)
downloadopenbsd-d18ae25f28e72831dc3c51f78e0735342540098b.tar.gz
openbsd-d18ae25f28e72831dc3c51f78e0735342540098b.tar.bz2
openbsd-d18ae25f28e72831dc3c51f78e0735342540098b.zip
Bring in compatibility for OpenSSL 1.1 style init functions.
This adds OPENSSL_init_crypto and OPENSSL_init_ssl, as well thread safety modifications for the existing LibreSSL init functions. The initialization routines are called automatically by the normal entry points into the library, as in newer OpenSSL ok jsing@, nits by tb@ and deraadt@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/Makefile4
-rw-r--r--src/lib/libcrypto/Symbols.list1
-rw-r--r--src/lib/libcrypto/bio/b_posix.c4
-rw-r--r--src/lib/libcrypto/comp/c_zlib.c4
-rw-r--r--src/lib/libcrypto/conf/conf_sap.c46
-rw-r--r--src/lib/libcrypto/crypto.h36
-rw-r--r--src/lib/libcrypto/crypto_init.c56
-rw-r--r--src/lib/libcrypto/engine/eng_all.c20
-rw-r--r--src/lib/libcrypto/engine/eng_lib.c5
-rw-r--r--src/lib/libcrypto/err/err.c32
-rw-r--r--src/lib/libcrypto/err/err_all.c18
-rw-r--r--src/lib/libcrypto/evp/c_all.c23
-rw-r--r--src/lib/libcrypto/evp/names.c20
-rw-r--r--src/lib/libcrypto/ex_data.c5
-rw-r--r--src/lib/libssl/Makefile4
-rw-r--r--src/lib/libssl/Symbols.list3
-rw-r--r--src/lib/libssl/ssl.h15
-rw-r--r--src/lib/libssl/ssl_init.c50
-rw-r--r--src/lib/libssl/ssl_lib.c7
-rw-r--r--src/lib/libssl/ssl_sess.c7
20 files changed, 322 insertions, 38 deletions
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile
index 4817a4c260..8c5e46b169 100644
--- a/src/lib/libcrypto/Makefile
+++ b/src/lib/libcrypto/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.26 2018/03/17 15:19:12 tb Exp $ 1# $OpenBSD: Makefile,v 1.27 2018/03/17 16:20:01 beck Exp $
2 2
3LIB= crypto 3LIB= crypto
4LIBREBUILD=y 4LIBREBUILD=y
@@ -37,7 +37,7 @@ SYMBOL_LIST= ${.CURDIR}/Symbols.list
37# crypto/ 37# crypto/
38SRCS+= cryptlib.c malloc-wrapper.c mem_dbg.c cversion.c ex_data.c cpt_err.c 38SRCS+= cryptlib.c malloc-wrapper.c mem_dbg.c cversion.c ex_data.c cpt_err.c
39SRCS+= o_time.c o_str.c o_init.c 39SRCS+= o_time.c o_str.c o_init.c
40SRCS+= mem_clr.c 40SRCS+= mem_clr.c crypto_init.c
41 41
42# aes/ 42# aes/
43SRCS+= aes_misc.c aes_ecb.c aes_cfb.c aes_ofb.c 43SRCS+= aes_misc.c aes_ecb.c aes_cfb.c aes_ofb.c
diff --git a/src/lib/libcrypto/Symbols.list b/src/lib/libcrypto/Symbols.list
index 8931abaf52..eb22f62278 100644
--- a/src/lib/libcrypto/Symbols.list
+++ b/src/lib/libcrypto/Symbols.list
@@ -1898,6 +1898,7 @@ OPENSSL_cpu_caps
1898OPENSSL_cpuid_setup 1898OPENSSL_cpuid_setup
1899OPENSSL_ia32cap_P 1899OPENSSL_ia32cap_P
1900OPENSSL_init 1900OPENSSL_init
1901OPENSSL_init_crypto
1901OPENSSL_load_builtin_modules 1902OPENSSL_load_builtin_modules
1902OPENSSL_no_config 1903OPENSSL_no_config
1903OPENSSL_strcasecmp 1904OPENSSL_strcasecmp
diff --git a/src/lib/libcrypto/bio/b_posix.c b/src/lib/libcrypto/bio/b_posix.c
index a850bc6aea..aed51bd717 100644
--- a/src/lib/libcrypto/bio/b_posix.c
+++ b/src/lib/libcrypto/bio/b_posix.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: b_posix.c,v 1.1 2014/12/03 22:14:38 bcook Exp $ */ 1/* $OpenBSD: b_posix.c,v 1.2 2018/03/17 16:20:01 beck 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 *
@@ -68,6 +68,8 @@
68int 68int
69BIO_sock_init(void) 69BIO_sock_init(void)
70{ 70{
71 if (!OPENSSL_init_crypto(0, NULL)) /* XXX do we need this? */
72 return (0);
71 return (1); 73 return (1);
72} 74}
73 75
diff --git a/src/lib/libcrypto/comp/c_zlib.c b/src/lib/libcrypto/comp/c_zlib.c
index 1802cffd99..0cdbb205a4 100644
--- a/src/lib/libcrypto/comp/c_zlib.c
+++ b/src/lib/libcrypto/comp/c_zlib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: c_zlib.c,v 1.19 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: c_zlib.c,v 1.20 2018/03/17 16:20:01 beck Exp $ */
2#include <stdio.h> 2#include <stdio.h>
3#include <stdlib.h> 3#include <stdlib.h>
4#include <string.h> 4#include <string.h>
@@ -191,6 +191,8 @@ COMP_zlib(void)
191 if (zlib_stateful_ex_idx == -1) 191 if (zlib_stateful_ex_idx == -1)
192 goto err; 192 goto err;
193 } 193 }
194 if (!OPENSSL_init_crypto(0, NULL))
195 goto err;
194 196
195 meth = &zlib_stateful_method; 197 meth = &zlib_stateful_method;
196 } 198 }
diff --git a/src/lib/libcrypto/conf/conf_sap.c b/src/lib/libcrypto/conf/conf_sap.c
index a29acea7c1..f1844f69f4 100644
--- a/src/lib/libcrypto/conf/conf_sap.c
+++ b/src/lib/libcrypto/conf/conf_sap.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: conf_sap.c,v 1.11 2015/02/11 03:19:37 doug Exp $ */ 1/* $OpenBSD: conf_sap.c,v 1.12 2018/03/17 16:20:01 beck Exp $ */
2/* Written by Stephen Henson (steve@openssl.org) for the OpenSSL 2/* Written by Stephen Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
@@ -56,6 +56,7 @@
56 * 56 *
57 */ 57 */
58 58
59#include <pthread.h>
59#include <stdio.h> 60#include <stdio.h>
60 61
61#include <openssl/opensslconf.h> 62#include <openssl/opensslconf.h>
@@ -75,23 +76,24 @@
75 * unless this is overridden by calling OPENSSL_no_config() 76 * unless this is overridden by calling OPENSSL_no_config()
76 */ 77 */
77 78
78static int openssl_configured = 0; 79static pthread_once_t openssl_configured = PTHREAD_ONCE_INIT;
79 80
80void 81static const char *openssl_config_name;
81OPENSSL_config(const char *config_name)
82{
83 if (openssl_configured)
84 return;
85 82
83void ENGINE_load_builtin_engines_internal(void);
84
85static void
86OPENSSL_config_internal(void)
87{
86 OPENSSL_load_builtin_modules(); 88 OPENSSL_load_builtin_modules();
87#ifndef OPENSSL_NO_ENGINE 89#ifndef OPENSSL_NO_ENGINE
88 /* Need to load ENGINEs */ 90 /* Need to load ENGINEs */
89 ENGINE_load_builtin_engines(); 91 ENGINE_load_builtin_engines_internal();
90#endif 92#endif
91 /* Add others here? */ 93 /* Add others here? */
92 94
93 ERR_clear_error(); 95 ERR_clear_error();
94 if (CONF_modules_load_file(NULL, config_name, 96 if (CONF_modules_load_file(NULL, openssl_config_name,
95 CONF_MFLAGS_DEFAULT_SECTION|CONF_MFLAGS_IGNORE_MISSING_FILE) <= 0) { 97 CONF_MFLAGS_DEFAULT_SECTION|CONF_MFLAGS_IGNORE_MISSING_FILE) <= 0) {
96 BIO *bio_err; 98 BIO *bio_err;
97 ERR_load_crypto_strings(); 99 ERR_load_crypto_strings();
@@ -107,7 +109,31 @@ OPENSSL_config(const char *config_name)
107} 109}
108 110
109void 111void
112OPENSSL_config(const char *config_name)
113{
114 /* Don't override if NULL */
115 /*
116 * Note - multiple threads calling this with *different* config names
117 * is probably not advisable. One thread will win, but you don't know
118 * if it will be the same thread as wins the pthread_once.
119 */
120 if (config_name != NULL)
121 openssl_config_name = config_name;
122
123 (void) OPENSSL_init_crypto(0, NULL);
124
125 (void) pthread_once(&openssl_configured, OPENSSL_config_internal);
126
127 return;
128}
129
130static void
131OPENSSL_no_config_internal(void)
132{
133}
134
135void
110OPENSSL_no_config(void) 136OPENSSL_no_config(void)
111{ 137{
112 openssl_configured = 1; 138 (void) pthread_once(&openssl_configured, OPENSSL_no_config_internal);
113} 139}
diff --git a/src/lib/libcrypto/crypto.h b/src/lib/libcrypto/crypto.h
index 90c2875349..0eb29610b4 100644
--- a/src/lib/libcrypto/crypto.h
+++ b/src/lib/libcrypto/crypto.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto.h,v 1.42 2018/02/14 16:32:06 jsing Exp $ */ 1/* $OpenBSD: crypto.h,v 1.43 2018/03/17 16:20:01 beck 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 *
@@ -542,6 +542,40 @@ void ERR_load_CRYPTO_strings(void);
542#define CRYPTO_R_FIPS_MODE_NOT_SUPPORTED 101 542#define CRYPTO_R_FIPS_MODE_NOT_SUPPORTED 101
543#define CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK 100 543#define CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK 100
544 544
545/*
546 * OpenSSL compatible OPENSSL_INIT options.
547 */
548
549#define OPENSSL_INIT_NO_LOAD_CONFIG 0x00000001L
550
551/* LibreSSL specific */
552#define _OPENSSL_INIT_FLAG_NOOP 0x80000000L
553
554/*
555 * These are provided for compatibiliy, but have no effect
556 * on how LibreSSL is initialized.
557 */
558#define OPENSSL_INIT__LOAD_CONFIG _OPENSSL_INIT_FLAG_NOOP
559#define OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS _OPENSSL_INIT_FLAG_NOOP
560#define OPENSSL_INIT_LOAD_CRYPTO_STRINGS _OPENSSL_INIT_FLAG_NOOP
561#define OPENSSL_INIT_ADD_ALL_CIPHERS _OPENSSL_INIT_FLAG_NOOP
562#define OPENSSL_INIT_ADD_ALL_DIGESTS _OPENSSL_INIT_FLAG_NOOP
563#define OPENSSL_INIT_NO_ADD_ALL_CIPHERS _OPENSSL_INIT_FLAG_NOOP
564#define OPENSSL_INIT_NO_ADD_ALL_DIGESTS _OPENSSL_INIT_FLAG_NOOP
565#define OPENSSL_INIT_ASYNC _OPENSSL_INIT_FLAG_NOOP
566#define OPENSSL_INIT_ENGINE_RDRAND _OPENSSL_INIT_FLAG_NOOP
567#define OPENSSL_INIT_ENGINE_DYNAMIC _OPENSSL_INIT_FLAG_NOOP
568#define OPENSSL_INIT_ENGINE_OPENSSL _OPENSSL_INIT_FLAG_NOOP
569#define OPENSSL_INIT_ENGINE_CRYPTODEV _OPENSSL_INIT_FLAG_NOOP
570#define OPENSSL_INIT_ENGINE_CAPI _OPENSSL_INIT_FLAG_NOOP
571#define OPENSSL_INIT_ENGINE_PADLOCK _OPENSSL_INIT_FLAG_NOOP
572#define OPENSSL_INIT_ENGINE_AFALG _OPENSSL_INIT_FLAG_NOOP
573#define OPENSSL_INIT_reserved_internal _OPENSSL_INIT_FLAG_NOOP
574#define OPENSSL_INIT_ATFORK _OPENSSL_INIT_FLAG_NOOP
575#define OPENSSL_INIT_ENGINE_ALL_BUILTIN _OPENSSL_INIT_FLAG_NOOP
576
577int OPENSSL_init_crypto(uint64_t opts, const void *settings);
578
545#ifdef __cplusplus 579#ifdef __cplusplus
546} 580}
547#endif 581#endif
diff --git a/src/lib/libcrypto/crypto_init.c b/src/lib/libcrypto/crypto_init.c
new file mode 100644
index 0000000000..f3d1a2bce9
--- /dev/null
+++ b/src/lib/libcrypto/crypto_init.c
@@ -0,0 +1,56 @@
1/*
2 * Copyright (c) 2018 Bob Beck <beck@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* OpenSSL style init */
18
19#include <pthread.h>
20#include <stdio.h>
21
22#include <openssl/objects.h>
23#include <openssl/conf.h>
24#include <openssl/evp.h>
25#include <openssl/err.h>
26#include "cryptlib.h"
27
28static pthread_t crypto_init_thread;
29
30static void
31OPENSSL_init_crypto_internal(void)
32{
33 crypto_init_thread = pthread_self();
34 OPENSSL_cpuid_setup();
35 ERR_load_crypto_strings();
36 OpenSSL_add_all_ciphers();
37 OpenSSL_add_all_digests();
38 OPENSSL_config(NULL);
39}
40
41int
42OPENSSL_init_crypto(uint64_t opts, const void *settings)
43{
44 static pthread_once_t once = PTHREAD_ONCE_INIT;
45
46 if (pthread_equal(pthread_self(), crypto_init_thread))
47 return 1; /* don't recurse */
48
49 if (opts & OPENSSL_INIT_NO_LOAD_CONFIG)
50 OPENSSL_no_config();
51
52 if (pthread_once(&once, OPENSSL_init_crypto_internal) != 0)
53 return 0;
54
55 return 1;
56}
diff --git a/src/lib/libcrypto/engine/eng_all.c b/src/lib/libcrypto/engine/eng_all.c
index 7640cf7fcd..403ca6865d 100644
--- a/src/lib/libcrypto/engine/eng_all.c
+++ b/src/lib/libcrypto/engine/eng_all.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: eng_all.c,v 1.29 2015/07/19 22:34:27 doug Exp $ */ 1/* $OpenBSD: eng_all.c,v 1.30 2018/03/17 16:20:01 beck Exp $ */
2/* Written by Richard Levitte <richard@levitte.org> for the OpenSSL 2/* Written by Richard Levitte <richard@levitte.org> for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -56,17 +56,16 @@
56 * 56 *
57 */ 57 */
58 58
59#include <pthread.h>
60
59#include <openssl/opensslconf.h> 61#include <openssl/opensslconf.h>
60 62
61#include "cryptlib.h" 63#include "cryptlib.h"
62#include "eng_int.h" 64#include "eng_int.h"
63 65
64void 66void
65ENGINE_load_builtin_engines(void) 67ENGINE_load_builtin_engines_internal(void)
66{ 68{
67 /* Some ENGINEs need this */
68 OPENSSL_cpuid_setup();
69
70#ifndef OPENSSL_NO_STATIC_ENGINE 69#ifndef OPENSSL_NO_STATIC_ENGINE
71#ifndef OPENSSL_NO_HW 70#ifndef OPENSSL_NO_HW
72#ifndef OPENSSL_NO_HW_PADLOCK 71#ifndef OPENSSL_NO_HW_PADLOCK
@@ -76,3 +75,14 @@ ENGINE_load_builtin_engines(void)
76#endif 75#endif
77 ENGINE_register_all_complete(); 76 ENGINE_register_all_complete();
78} 77}
78
79void
80ENGINE_load_builtin_engines(void)
81{
82 static pthread_once_t once = PTHREAD_ONCE_INIT;
83
84 /* Prayer and clean living lets you ignore errors, OpenSSL style */
85 (void) OPENSSL_init_crypto(0, NULL);
86
87 (void) pthread_once(&once, ENGINE_load_builtin_engines_internal);
88}
diff --git a/src/lib/libcrypto/engine/eng_lib.c b/src/lib/libcrypto/engine/eng_lib.c
index d2da29fe69..11ad771109 100644
--- a/src/lib/libcrypto/engine/eng_lib.c
+++ b/src/lib/libcrypto/engine/eng_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: eng_lib.c,v 1.12 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: eng_lib.c,v 1.13 2018/03/17 16:20:01 beck Exp $ */
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL 2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -70,6 +70,9 @@ ENGINE_new(void)
70{ 70{
71 ENGINE *ret; 71 ENGINE *ret;
72 72
73 if (!OPENSSL_init_crypto(0, NULL))
74 return NULL;
75
73 ret = malloc(sizeof(ENGINE)); 76 ret = malloc(sizeof(ENGINE));
74 if (ret == NULL) { 77 if (ret == NULL) {
75 ENGINEerror(ERR_R_MALLOC_FAILURE); 78 ENGINEerror(ERR_R_MALLOC_FAILURE);
diff --git a/src/lib/libcrypto/err/err.c b/src/lib/libcrypto/err/err.c
index ffe25bf465..320078da66 100644
--- a/src/lib/libcrypto/err/err.c
+++ b/src/lib/libcrypto/err/err.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: err.c,v 1.45 2017/02/20 23:21:19 beck Exp $ */ 1/* $OpenBSD: err.c,v 1.46 2018/03/17 16:20:01 beck 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 *
@@ -109,6 +109,7 @@
109 * 109 *
110 */ 110 */
111 111
112#include <pthread.h>
112#include <stdarg.h> 113#include <stdarg.h>
113#include <stdio.h> 114#include <stdio.h>
114#include <string.h> 115#include <string.h>
@@ -282,6 +283,8 @@ static LHASH_OF(ERR_STATE) *int_thread_hash = NULL;
282static int int_thread_hash_references = 0; 283static int int_thread_hash_references = 0;
283static int int_err_library_number = ERR_LIB_USER; 284static int int_err_library_number = ERR_LIB_USER;
284 285
286static pthread_t err_init_thread;
287
285/* Internal function that checks whether "err_fns" is set and if not, sets it to 288/* Internal function that checks whether "err_fns" is set and if not, sets it to
286 * the defaults. */ 289 * the defaults. */
287static void 290static void
@@ -650,8 +653,9 @@ ERR_STATE_free(ERR_STATE *s)
650} 653}
651 654
652void 655void
653ERR_load_ERR_strings(void) 656ERR_load_ERR_strings_internal(void)
654{ 657{
658 err_init_thread = pthread_self();
655 err_fns_check(); 659 err_fns_check();
656#ifndef OPENSSL_NO_ERR 660#ifndef OPENSSL_NO_ERR
657 err_load_strings(0, ERR_str_libraries); 661 err_load_strings(0, ERR_str_libraries);
@@ -662,6 +666,21 @@ ERR_load_ERR_strings(void)
662#endif 666#endif
663} 667}
664 668
669
670void
671ERR_load_ERR_strings(void)
672{
673 static pthread_once_t once = PTHREAD_ONCE_INIT;
674
675 if (pthread_equal(pthread_self(), err_init_thread))
676 return; /* don't recurse */
677
678 /* Prayer and clean living lets you ignore errors, OpenSSL style */
679 (void) OPENSSL_init_crypto(0, NULL);
680
681 (void) pthread_once(&once, ERR_load_ERR_strings_internal);
682}
683
665static void 684static void
666err_load_strings(int lib, ERR_STRING_DATA *str) 685err_load_strings(int lib, ERR_STRING_DATA *str)
667{ 686{
@@ -683,6 +702,9 @@ ERR_load_strings(int lib, ERR_STRING_DATA *str)
683void 702void
684ERR_unload_strings(int lib, ERR_STRING_DATA *str) 703ERR_unload_strings(int lib, ERR_STRING_DATA *str)
685{ 704{
705 /* Prayer and clean living lets you ignore errors, OpenSSL style */
706 (void) OPENSSL_init_crypto(0, NULL);
707
686 while (str->error) { 708 while (str->error) {
687 if (lib) 709 if (lib)
688 str->error |= ERR_PACK(lib, 0, 0); 710 str->error |= ERR_PACK(lib, 0, 0);
@@ -694,6 +716,9 @@ ERR_unload_strings(int lib, ERR_STRING_DATA *str)
694void 716void
695ERR_free_strings(void) 717ERR_free_strings(void)
696{ 718{
719 /* Prayer and clean living lets you ignore errors, OpenSSL style */
720 (void) OPENSSL_init_crypto(0, NULL);
721
697 err_fns_check(); 722 err_fns_check();
698 ERRFN(err_del)(); 723 ERRFN(err_del)();
699} 724}
@@ -953,6 +978,9 @@ ERR_lib_error_string(unsigned long e)
953 ERR_STRING_DATA d, *p; 978 ERR_STRING_DATA d, *p;
954 unsigned long l; 979 unsigned long l;
955 980
981 if (!OPENSSL_init_crypto(0, NULL))
982 return NULL;
983
956 err_fns_check(); 984 err_fns_check();
957 l = ERR_GET_LIB(e); 985 l = ERR_GET_LIB(e);
958 d.error = ERR_PACK(l, 0, 0); 986 d.error = ERR_PACK(l, 0, 0);
diff --git a/src/lib/libcrypto/err/err_all.c b/src/lib/libcrypto/err/err_all.c
index 40009cbe88..24de3c9c15 100644
--- a/src/lib/libcrypto/err/err_all.c
+++ b/src/lib/libcrypto/err/err_all.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: err_all.c,v 1.23 2016/10/19 16:49:11 jsing Exp $ */ 1/* $OpenBSD: err_all.c,v 1.24 2018/03/17 16:20:01 beck 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 *
@@ -56,6 +56,7 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#include <pthread.h>
59#include <stdio.h> 60#include <stdio.h>
60 61
61#include <openssl/opensslconf.h> 62#include <openssl/opensslconf.h>
@@ -103,11 +104,13 @@
103#include <openssl/gost.h> 104#include <openssl/gost.h>
104#endif 105#endif
105 106
106void 107void ERR_load_ERR_strings_internal(void);
107ERR_load_crypto_strings(void) 108
109static void
110ERR_load_crypto_strings_internal(void)
108{ 111{
109#ifndef OPENSSL_NO_ERR 112#ifndef OPENSSL_NO_ERR
110 ERR_load_ERR_strings(); /* include error strings for SYSerr */ 113 ERR_load_ERR_strings_internal(); /* include error strings for SYSerr */
111 ERR_load_BN_strings(); 114 ERR_load_BN_strings();
112#ifndef OPENSSL_NO_RSA 115#ifndef OPENSSL_NO_RSA
113 ERR_load_RSA_strings(); 116 ERR_load_RSA_strings();
@@ -153,3 +156,10 @@ ERR_load_crypto_strings(void)
153#endif 156#endif
154#endif 157#endif
155} 158}
159
160void
161ERR_load_crypto_strings(void)
162{
163 static pthread_once_t loaded = PTHREAD_ONCE_INIT;
164 (void) pthread_once(&loaded, ERR_load_crypto_strings_internal);
165}
diff --git a/src/lib/libcrypto/evp/c_all.c b/src/lib/libcrypto/evp/c_all.c
index 8ab93fece8..87657eded3 100644
--- a/src/lib/libcrypto/evp/c_all.c
+++ b/src/lib/libcrypto/evp/c_all.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: c_all.c,v 1.21 2017/03/01 13:53:58 jsing Exp $ */ 1/* $OpenBSD: c_all.c,v 1.22 2018/03/17 16:20:01 beck 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 *
@@ -57,6 +57,7 @@
57 */ 57 */
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include <pthread.h>
60 61
61#include <openssl/opensslconf.h> 62#include <openssl/opensslconf.h>
62 63
@@ -66,8 +67,8 @@
66 67
67#include "cryptlib.h" 68#include "cryptlib.h"
68 69
69void 70static void
70OpenSSL_add_all_ciphers(void) 71OpenSSL_add_all_ciphers_internal(void)
71{ 72{
72#ifndef OPENSSL_NO_DES 73#ifndef OPENSSL_NO_DES
73 EVP_add_cipher(EVP_des_cfb()); 74 EVP_add_cipher(EVP_des_cfb());
@@ -226,7 +227,14 @@ OpenSSL_add_all_ciphers(void)
226} 227}
227 228
228void 229void
229OpenSSL_add_all_digests(void) 230OpenSSL_add_all_ciphers(void)
231{
232 static pthread_once_t add_all_ciphers_once = PTHREAD_ONCE_INIT;
233 (void) pthread_once(&add_all_ciphers_once, OpenSSL_add_all_ciphers_internal);
234}
235
236static void
237OpenSSL_add_all_digests_internal(void)
230{ 238{
231#ifndef OPENSSL_NO_MD4 239#ifndef OPENSSL_NO_MD4
232 EVP_add_digest(EVP_md4()); 240 EVP_add_digest(EVP_md4());
@@ -284,6 +292,13 @@ OpenSSL_add_all_digests(void)
284} 292}
285 293
286void 294void
295OpenSSL_add_all_digests(void)
296{
297 static pthread_once_t add_all_digests_once = PTHREAD_ONCE_INIT;
298 (void) pthread_once(&add_all_digests_once, OpenSSL_add_all_digests_internal);
299}
300
301void
287OPENSSL_add_all_algorithms_noconf(void) 302OPENSSL_add_all_algorithms_noconf(void)
288{ 303{
289 OPENSSL_cpuid_setup(); 304 OPENSSL_cpuid_setup();
diff --git a/src/lib/libcrypto/evp/names.c b/src/lib/libcrypto/evp/names.c
index ebaa3a2f6f..dfcf9ee225 100644
--- a/src/lib/libcrypto/evp/names.c
+++ b/src/lib/libcrypto/evp/names.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: names.c,v 1.13 2017/04/29 21:48:44 jsing Exp $ */ 1/* $OpenBSD: names.c,v 1.14 2018/03/17 16:20:01 beck 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 *
@@ -113,6 +113,9 @@ EVP_get_cipherbyname(const char *name)
113{ 113{
114 const EVP_CIPHER *cp; 114 const EVP_CIPHER *cp;
115 115
116 if (!OPENSSL_init_crypto(0, NULL))
117 return NULL;
118
116 cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH); 119 cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
117 return (cp); 120 return (cp);
118} 121}
@@ -122,6 +125,9 @@ EVP_get_digestbyname(const char *name)
122{ 125{
123 const EVP_MD *cp; 126 const EVP_MD *cp;
124 127
128 if (!OPENSSL_init_crypto(0, NULL))
129 return NULL;
130
125 cp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH); 131 cp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
126 return (cp); 132 return (cp);
127} 133}
@@ -167,6 +173,9 @@ EVP_CIPHER_do_all(void (*fn)(const EVP_CIPHER *ciph, const char *from,
167{ 173{
168 struct doall_cipher dc; 174 struct doall_cipher dc;
169 175
176 /* Prayer and clean living lets you ignore errors, OpenSSL style */
177 (void) OPENSSL_init_crypto(0, NULL);
178
170 dc.fn = fn; 179 dc.fn = fn;
171 dc.arg = arg; 180 dc.arg = arg;
172 OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc); 181 OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
@@ -178,6 +187,9 @@ EVP_CIPHER_do_all_sorted(void (*fn)(const EVP_CIPHER *ciph, const char *from,
178{ 187{
179 struct doall_cipher dc; 188 struct doall_cipher dc;
180 189
190 /* Prayer and clean living lets you ignore errors, OpenSSL style */
191 (void) OPENSSL_init_crypto(0, NULL);
192
181 dc.fn = fn; 193 dc.fn = fn;
182 dc.arg = arg; 194 dc.arg = arg;
183 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, 195 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
@@ -207,6 +219,9 @@ EVP_MD_do_all(void (*fn)(const EVP_MD *md, const char *from, const char *to,
207{ 219{
208 struct doall_md dc; 220 struct doall_md dc;
209 221
222 /* Prayer and clean living lets you ignore errors, OpenSSL style */
223 (void) OPENSSL_init_crypto(0, NULL);
224
210 dc.fn = fn; 225 dc.fn = fn;
211 dc.arg = arg; 226 dc.arg = arg;
212 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc); 227 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
@@ -218,6 +233,9 @@ EVP_MD_do_all_sorted(void (*fn)(const EVP_MD *md,
218{ 233{
219 struct doall_md dc; 234 struct doall_md dc;
220 235
236 /* Prayer and clean living lets you ignore errors, OpenSSL style */
237 (void) OPENSSL_init_crypto(0, NULL);
238
221 dc.fn = fn; 239 dc.fn = fn;
222 dc.arg = arg; 240 dc.arg = arg;
223 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc); 241 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
diff --git a/src/lib/libcrypto/ex_data.c b/src/lib/libcrypto/ex_data.c
index 63885af3af..b1e3913662 100644
--- a/src/lib/libcrypto/ex_data.c
+++ b/src/lib/libcrypto/ex_data.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ex_data.c,v 1.19 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: ex_data.c,v 1.20 2018/03/17 16:20:01 beck Exp $ */
2 2
3/* 3/*
4 * Overhaul notes; 4 * Overhaul notes;
@@ -312,6 +312,8 @@ def_get_class(int class_index)
312 EX_CLASS_ITEM d, *p, *gen; 312 EX_CLASS_ITEM d, *p, *gen;
313 EX_DATA_CHECK(return NULL;) 313 EX_DATA_CHECK(return NULL;)
314 d.class_index = class_index; 314 d.class_index = class_index;
315 if (!OPENSSL_init_crypto(0, NULL))
316 return NULL;
315 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); 317 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
316 p = lh_EX_CLASS_ITEM_retrieve(ex_data, &d); 318 p = lh_EX_CLASS_ITEM_retrieve(ex_data, &d);
317 if (!p) { 319 if (!p) {
@@ -500,6 +502,7 @@ int_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
500 EX_CLASS_ITEM *item; 502 EX_CLASS_ITEM *item;
501 void *ptr; 503 void *ptr;
502 CRYPTO_EX_DATA_FUNCS **storage = NULL; 504 CRYPTO_EX_DATA_FUNCS **storage = NULL;
505
503 if ((item = def_get_class(class_index)) == NULL) 506 if ((item = def_get_class(class_index)) == NULL)
504 return; 507 return;
505 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA); 508 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
diff --git a/src/lib/libssl/Makefile b/src/lib/libssl/Makefile
index 66dae58874..6a397a7df7 100644
--- a/src/lib/libssl/Makefile
+++ b/src/lib/libssl/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.38 2017/08/13 19:42:33 doug Exp $ 1# $OpenBSD: Makefile,v 1.39 2018/03/17 16:20:01 beck Exp $
2 2
3.include <bsd.own.mk> 3.include <bsd.own.mk>
4.ifndef NOMAN 4.ifndef NOMAN
@@ -33,7 +33,7 @@ SRCS= \
33 ssl_ciph.c ssl_stat.c ssl_rsa.c \ 33 ssl_ciph.c ssl_stat.c ssl_rsa.c \
34 ssl_asn1.c ssl_txt.c ssl_algs.c \ 34 ssl_asn1.c ssl_txt.c ssl_algs.c \
35 bio_ssl.c ssl_err.c \ 35 bio_ssl.c ssl_err.c \
36 ssl_packet.c ssl_tlsext.c ssl_versions.c pqueue.c 36 ssl_packet.c ssl_tlsext.c ssl_versions.c pqueue.c ssl_init.c
37SRCS+= s3_cbc.c 37SRCS+= s3_cbc.c
38SRCS+= bs_ber.c bs_cbb.c bs_cbs.c 38SRCS+= bs_ber.c bs_cbb.c bs_cbs.c
39 39
diff --git a/src/lib/libssl/Symbols.list b/src/lib/libssl/Symbols.list
index 581b292a74..3b513d5c28 100644
--- a/src/lib/libssl/Symbols.list
+++ b/src/lib/libssl/Symbols.list
@@ -298,3 +298,6 @@ SSL_version
298SSL_version_str 298SSL_version_str
299SSL_want 299SSL_want
300SSL_write 300SSL_write
301
302/* OpenSSL compatible init */
303OPENSSL_init_ssl
diff --git a/src/lib/libssl/ssl.h b/src/lib/libssl/ssl.h
index 05939f214d..97d1c40a66 100644
--- a/src/lib/libssl/ssl.h
+++ b/src/lib/libssl/ssl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl.h,v 1.152 2018/03/17 15:55:52 tb Exp $ */ 1/* $OpenBSD: ssl.h,v 1.153 2018/03/17 16:20:01 beck 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 *
@@ -2112,6 +2112,19 @@ void ERR_load_SSL_strings(void);
2112#define SSL_R_X509_VERIFICATION_SETUP_PROBLEMS 269 2112#define SSL_R_X509_VERIFICATION_SETUP_PROBLEMS 269
2113#define SSL_R_PEER_BEHAVING_BADLY 666 2113#define SSL_R_PEER_BEHAVING_BADLY 666
2114 2114
2115/*
2116 * OpenSSL compatible OPENSSL_INIT options
2117 */
2118
2119/*
2120 * These are provided for compatibiliy, but have no effect
2121 * on how LibreSSL is initialized.
2122 */
2123#define OPENSSL_INIT_LOAD_SSL_STRINGS _OPENSSL_INIT_FLAG_NOOP
2124#define OPENSSL_INIT_SSL_DEFAULT _OPENSSL_INIT_FLAG_NOOP
2125
2126int OPENSSL_init_ssl(uint64_t opts, const void *settings);
2127
2115#ifdef __cplusplus 2128#ifdef __cplusplus
2116} 2129}
2117#endif 2130#endif
diff --git a/src/lib/libssl/ssl_init.c b/src/lib/libssl/ssl_init.c
new file mode 100644
index 0000000000..0ef80956ed
--- /dev/null
+++ b/src/lib/libssl/ssl_init.c
@@ -0,0 +1,50 @@
1/*
2 * Copyright (c) 2018 Bob Beck <beck@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* OpenSSL style init */
18
19#include <pthread.h>
20#include <stdio.h>
21
22#include <openssl/objects.h>
23
24#include "ssl_locl.h"
25
26static pthread_t ssl_init_thread;
27
28static void
29OPENSSL_init_ssl_internal(void)
30{
31 ssl_init_thread = pthread_self();
32 SSL_load_error_strings();
33 SSL_library_init();
34}
35
36int
37OPENSSL_init_ssl(uint64_t opts, const void *settings)
38{
39 static pthread_once_t once = PTHREAD_ONCE_INIT;
40
41 if (pthread_equal(pthread_self(), ssl_init_thread))
42 return 1; /* don't recurse */
43
44 OPENSSL_init_crypto(opts, settings);
45
46 if (pthread_once(&once, OPENSSL_init_ssl_internal) != 0)
47 return 0;
48
49 return 1;
50}
diff --git a/src/lib/libssl/ssl_lib.c b/src/lib/libssl/ssl_lib.c
index eca3c97fac..573e63c934 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.181 2018/03/17 15:48:31 tb Exp $ */ 1/* $OpenBSD: ssl_lib.c,v 1.182 2018/03/17 16:20:01 beck 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 *
@@ -1791,6 +1791,11 @@ SSL_CTX_new(const SSL_METHOD *meth)
1791{ 1791{
1792 SSL_CTX *ret; 1792 SSL_CTX *ret;
1793 1793
1794 if (!OPENSSL_init_ssl(0, NULL)) {
1795 SSLerrorx(SSL_R_LIBRARY_BUG);
1796 return (NULL);
1797 }
1798
1794 if (meth == NULL) { 1799 if (meth == NULL) {
1795 SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED); 1800 SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED);
1796 return (NULL); 1801 return (NULL);
diff --git a/src/lib/libssl/ssl_sess.c b/src/lib/libssl/ssl_sess.c
index 4903719fb3..51aa2eac04 100644
--- a/src/lib/libssl/ssl_sess.c
+++ b/src/lib/libssl/ssl_sess.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl_sess.c,v 1.77 2018/03/17 15:55:53 tb Exp $ */ 1/* $OpenBSD: ssl_sess.c,v 1.78 2018/03/17 16:20:01 beck 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 *
@@ -199,6 +199,11 @@ SSL_SESSION_new(void)
199{ 199{
200 SSL_SESSION *ss; 200 SSL_SESSION *ss;
201 201
202 if (!OPENSSL_init_ssl(0, NULL)) {
203 SSLerrorx(SSL_R_LIBRARY_BUG);
204 return(NULL);
205 }
206
202 if ((ss = calloc(1, sizeof(*ss))) == NULL) { 207 if ((ss = calloc(1, sizeof(*ss))) == NULL) {
203 SSLerrorx(ERR_R_MALLOC_FAILURE); 208 SSLerrorx(ERR_R_MALLOC_FAILURE);
204 return (NULL); 209 return (NULL);