172c33676SMaxim Ag /* $OpenBSD: conf_sap.c,v 1.14 2018/03/19 03:56:08 beck Exp $ */
2f5b1c8a1SJohn Marino /* Written by Stephen Henson (steve@openssl.org) for the OpenSSL
3f5b1c8a1SJohn Marino  * project 2001.
4f5b1c8a1SJohn Marino  */
5f5b1c8a1SJohn Marino /* ====================================================================
6f5b1c8a1SJohn Marino  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
7f5b1c8a1SJohn Marino  *
8f5b1c8a1SJohn Marino  * Redistribution and use in source and binary forms, with or without
9f5b1c8a1SJohn Marino  * modification, are permitted provided that the following conditions
10f5b1c8a1SJohn Marino  * are met:
11f5b1c8a1SJohn Marino  *
12f5b1c8a1SJohn Marino  * 1. Redistributions of source code must retain the above copyright
13f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer.
14f5b1c8a1SJohn Marino  *
15f5b1c8a1SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
16f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer in
17f5b1c8a1SJohn Marino  *    the documentation and/or other materials provided with the
18f5b1c8a1SJohn Marino  *    distribution.
19f5b1c8a1SJohn Marino  *
20f5b1c8a1SJohn Marino  * 3. All advertising materials mentioning features or use of this
21f5b1c8a1SJohn Marino  *    software must display the following acknowledgment:
22f5b1c8a1SJohn Marino  *    "This product includes software developed by the OpenSSL Project
23f5b1c8a1SJohn Marino  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24f5b1c8a1SJohn Marino  *
25f5b1c8a1SJohn Marino  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26f5b1c8a1SJohn Marino  *    endorse or promote products derived from this software without
27f5b1c8a1SJohn Marino  *    prior written permission. For written permission, please contact
28f5b1c8a1SJohn Marino  *    licensing@OpenSSL.org.
29f5b1c8a1SJohn Marino  *
30f5b1c8a1SJohn Marino  * 5. Products derived from this software may not be called "OpenSSL"
31f5b1c8a1SJohn Marino  *    nor may "OpenSSL" appear in their names without prior written
32f5b1c8a1SJohn Marino  *    permission of the OpenSSL Project.
33f5b1c8a1SJohn Marino  *
34f5b1c8a1SJohn Marino  * 6. Redistributions of any form whatsoever must retain the following
35f5b1c8a1SJohn Marino  *    acknowledgment:
36f5b1c8a1SJohn Marino  *    "This product includes software developed by the OpenSSL Project
37f5b1c8a1SJohn Marino  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38f5b1c8a1SJohn Marino  *
39f5b1c8a1SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40f5b1c8a1SJohn Marino  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41f5b1c8a1SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42f5b1c8a1SJohn Marino  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43f5b1c8a1SJohn Marino  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44f5b1c8a1SJohn Marino  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45f5b1c8a1SJohn Marino  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46f5b1c8a1SJohn Marino  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47f5b1c8a1SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48f5b1c8a1SJohn Marino  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49f5b1c8a1SJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50f5b1c8a1SJohn Marino  * OF THE POSSIBILITY OF SUCH DAMAGE.
51f5b1c8a1SJohn Marino  * ====================================================================
52f5b1c8a1SJohn Marino  *
53f5b1c8a1SJohn Marino  * This product includes cryptographic software written by Eric Young
54f5b1c8a1SJohn Marino  * (eay@cryptsoft.com).  This product includes software written by Tim
55f5b1c8a1SJohn Marino  * Hudson (tjh@cryptsoft.com).
56f5b1c8a1SJohn Marino  *
57f5b1c8a1SJohn Marino  */
58f5b1c8a1SJohn Marino 
5972c33676SMaxim Ag #include <pthread.h>
60f5b1c8a1SJohn Marino #include <stdio.h>
61f5b1c8a1SJohn Marino 
62f5b1c8a1SJohn Marino #include <openssl/opensslconf.h>
63f5b1c8a1SJohn Marino 
64f5b1c8a1SJohn Marino #include <openssl/asn1.h>
65f5b1c8a1SJohn Marino #include <openssl/conf.h>
66f5b1c8a1SJohn Marino #include <openssl/crypto.h>
67f5b1c8a1SJohn Marino #include <openssl/err.h>
68f5b1c8a1SJohn Marino #include <openssl/x509.h>
69f5b1c8a1SJohn Marino 
70f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_ENGINE
71f5b1c8a1SJohn Marino #include <openssl/engine.h>
72f5b1c8a1SJohn Marino #endif
73f5b1c8a1SJohn Marino 
74f5b1c8a1SJohn Marino /* This is the automatic configuration loader: it is called automatically by
75f5b1c8a1SJohn Marino  * OpenSSL when any of a number of standard initialisation functions are called,
76f5b1c8a1SJohn Marino  * unless this is overridden by calling OPENSSL_no_config()
77f5b1c8a1SJohn Marino  */
78f5b1c8a1SJohn Marino 
7972c33676SMaxim Ag static pthread_once_t openssl_configured = PTHREAD_ONCE_INIT;
80f5b1c8a1SJohn Marino 
8172c33676SMaxim Ag static const char *openssl_config_name;
8272c33676SMaxim Ag 
8372c33676SMaxim Ag static void
OPENSSL_config_internal(void)8472c33676SMaxim Ag OPENSSL_config_internal(void)
85f5b1c8a1SJohn Marino {
86f5b1c8a1SJohn Marino 	OPENSSL_load_builtin_modules();
87f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_ENGINE
88f5b1c8a1SJohn Marino 	/* Need to load ENGINEs */
89f5b1c8a1SJohn Marino 	ENGINE_load_builtin_engines();
90f5b1c8a1SJohn Marino #endif
91f5b1c8a1SJohn Marino 	/* Add others here? */
92f5b1c8a1SJohn Marino 
93f5b1c8a1SJohn Marino 	ERR_clear_error();
9472c33676SMaxim Ag 	if (CONF_modules_load_file(NULL, openssl_config_name,
95f5b1c8a1SJohn Marino 	    CONF_MFLAGS_DEFAULT_SECTION|CONF_MFLAGS_IGNORE_MISSING_FILE) <= 0) {
96f5b1c8a1SJohn Marino 		BIO *bio_err;
97f5b1c8a1SJohn Marino 		ERR_load_crypto_strings();
98f5b1c8a1SJohn Marino 		if ((bio_err = BIO_new_fp(stderr, BIO_NOCLOSE)) != NULL) {
99f5b1c8a1SJohn Marino 			BIO_printf(bio_err, "Auto configuration failed\n");
100f5b1c8a1SJohn Marino 			ERR_print_errors(bio_err);
101f5b1c8a1SJohn Marino 			BIO_free(bio_err);
102f5b1c8a1SJohn Marino 		}
103f5b1c8a1SJohn Marino 		exit(1);
104f5b1c8a1SJohn Marino 	}
105f5b1c8a1SJohn Marino 
106f5b1c8a1SJohn Marino 	return;
107f5b1c8a1SJohn Marino }
108f5b1c8a1SJohn Marino 
10972c33676SMaxim Ag int
OpenSSL_config(const char * config_name)11072c33676SMaxim Ag OpenSSL_config(const char *config_name)
11172c33676SMaxim Ag {
11272c33676SMaxim Ag 	/* Don't override if NULL */
11372c33676SMaxim Ag 	/*
11472c33676SMaxim Ag 	 * Note - multiple threads calling this with *different* config names
11572c33676SMaxim Ag 	 * is probably not advisable.  One thread will win, but you don't know
11672c33676SMaxim Ag 	 * if it will be the same thread as wins the pthread_once.
11772c33676SMaxim Ag 	 */
11872c33676SMaxim Ag 	if (config_name != NULL)
11972c33676SMaxim Ag 		openssl_config_name = config_name;
12072c33676SMaxim Ag 
12172c33676SMaxim Ag 	if (OPENSSL_init_crypto(0, NULL) == 0)
12272c33676SMaxim Ag 		return 0;
12372c33676SMaxim Ag 
12472c33676SMaxim Ag 	if (pthread_once(&openssl_configured, OPENSSL_config_internal) != 0)
12572c33676SMaxim Ag 		return 0;
12672c33676SMaxim Ag 
12772c33676SMaxim Ag 	return 1;
12872c33676SMaxim Ag }
12972c33676SMaxim Ag 
13072c33676SMaxim Ag void
OPENSSL_config(const char * config_name)13172c33676SMaxim Ag OPENSSL_config(const char *config_name)
13272c33676SMaxim Ag {
13372c33676SMaxim Ag 	(void) OpenSSL_config(config_name);
13472c33676SMaxim Ag }
13572c33676SMaxim Ag 
13672c33676SMaxim Ag static void
OPENSSL_no_config_internal(void)13772c33676SMaxim Ag OPENSSL_no_config_internal(void)
13872c33676SMaxim Ag {
13972c33676SMaxim Ag }
14072c33676SMaxim Ag 
14172c33676SMaxim Ag int
OpenSSL_no_config(void)14272c33676SMaxim Ag OpenSSL_no_config(void)
14372c33676SMaxim Ag {
14472c33676SMaxim Ag 	if (pthread_once(&openssl_configured, OPENSSL_no_config_internal) != 0)
14572c33676SMaxim Ag 		return 0;
14672c33676SMaxim Ag 
14772c33676SMaxim Ag 	return 1;
14872c33676SMaxim Ag }
14972c33676SMaxim Ag 
150f5b1c8a1SJohn Marino void
OPENSSL_no_config(void)151f5b1c8a1SJohn Marino OPENSSL_no_config(void)
152f5b1c8a1SJohn Marino {
15372c33676SMaxim Ag 	(void) OpenSSL_no_config();
154f5b1c8a1SJohn Marino }
155