a1a5d0cbd12a75838d042d1986bf6c24d60ca658
[openssl.git] / crypto / context.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "internal/cryptlib.h"
11 #include "internal/thread_once.h"
12
13 struct openssl_ctx_onfree_list_st {
14     openssl_ctx_onfree_fn fn;
15     struct openssl_ctx_onfree_list_st *next;
16 };
17
18 struct openssl_ctx_st {
19     CRYPTO_RWLOCK *lock;
20     CRYPTO_EX_DATA data;
21     int run_once_done[MAX_OPENSSL_CTX_RUN_ONCE];
22     int run_once_ret[MAX_OPENSSL_CTX_RUN_ONCE];
23     struct openssl_ctx_onfree_list_st *onfreelist;
24 };
25
26 static OPENSSL_CTX default_context;
27
28 static int context_init(OPENSSL_CTX *ctx)
29 {
30     return (ctx->lock = CRYPTO_THREAD_lock_new()) != NULL
31         && CRYPTO_new_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
32                               &ctx->data);
33 }
34
35 static int context_deinit(OPENSSL_CTX *ctx)
36 {
37     struct openssl_ctx_onfree_list_st *tmp, *onfree = ctx->onfreelist;
38
39     while (onfree != NULL) {
40         onfree->fn(ctx);
41         tmp = onfree;
42         onfree = onfree->next;
43         OPENSSL_free(tmp);
44     }
45     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data);
46     CRYPTO_THREAD_lock_free(ctx->lock);
47     return 1;
48 }
49
50 static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
51 static void do_default_context_deinit(void)
52 {
53     context_deinit(&default_context);
54 }
55 DEFINE_RUN_ONCE_STATIC(do_default_context_init)
56 {
57     return OPENSSL_init_crypto(0, NULL)
58         && context_init(&default_context)
59         && OPENSSL_atexit(do_default_context_deinit);
60 }
61
62 OPENSSL_CTX *OPENSSL_CTX_new(void)
63 {
64     OPENSSL_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
65
66     if (ctx != NULL && !context_init(ctx)) {
67         OPENSSL_CTX_free(ctx);
68         ctx = NULL;
69     }
70     return ctx;
71 }
72
73 void OPENSSL_CTX_free(OPENSSL_CTX *ctx)
74 {
75     if (ctx != NULL)
76         context_deinit(ctx);
77     OPENSSL_free(ctx);
78 }
79
80 static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign,
81                                     CRYPTO_EX_DATA *ad, int index,
82                                     long argl_ign, void *argp)
83 {
84     const OPENSSL_CTX_METHOD *meth = argp;
85     void *ptr = meth->new_func();
86
87     if (ptr != NULL)
88         CRYPTO_set_ex_data(ad, index, ptr);
89 }
90 static void openssl_ctx_generic_free(void *parent_ign, void *ptr,
91                                      CRYPTO_EX_DATA *ad, int index,
92                                      long argl_ign, void *argp)
93 {
94     const OPENSSL_CTX_METHOD *meth = argp;
95
96     meth->free_func(ptr);
97 }
98 int openssl_ctx_new_index(const OPENSSL_CTX_METHOD *meth)
99 {
100     return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_OPENSSL_CTX, 0, (void *)meth,
101                                    openssl_ctx_generic_new, NULL,
102                                    openssl_ctx_generic_free);
103 }
104
105 void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index)
106 {
107     void *data = NULL;
108
109     if (ctx == NULL) {
110         if (!RUN_ONCE(&default_context_init, do_default_context_init))
111             return NULL;
112         ctx = &default_context;
113     }
114     if (ctx == NULL)
115         return NULL;
116
117     CRYPTO_THREAD_read_lock(ctx->lock);
118
119     /* The alloc call ensures there's a value there */
120     if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
121                              &ctx->data, index))
122         data = CRYPTO_get_ex_data(&ctx->data, index);
123
124     CRYPTO_THREAD_unlock(ctx->lock);
125
126     return data;
127 }
128
129 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
130                          openssl_ctx_run_once_fn run_once_fn)
131 {
132     int done = 0, ret = 0;
133
134 #ifndef FIPS_MODE
135     if (ctx == NULL) {
136         if (!RUN_ONCE(&default_context_init, do_default_context_init))
137             return 0;
138         ctx = &default_context;
139     }
140 #endif
141     if (ctx == NULL)
142         return 0;
143
144     CRYPTO_THREAD_read_lock(ctx->lock);
145     done = ctx->run_once_done[idx];
146     if (done)
147         ret = ctx->run_once_ret[idx];
148     CRYPTO_THREAD_unlock(ctx->lock);
149
150     if (done)
151         return ret;
152
153     CRYPTO_THREAD_write_lock(ctx->lock);
154     if (ctx->run_once_done[idx]) {
155         ret = ctx->run_once_ret[idx];
156         CRYPTO_THREAD_unlock(ctx->lock);
157         return ret;
158     }
159
160     ret = run_once_fn(ctx);
161     ctx->run_once_done[idx] = 1;
162     ctx->run_once_ret[idx] = ret;
163     CRYPTO_THREAD_unlock(ctx->lock);
164
165     return ret;
166 }
167
168 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn)
169 {
170     struct openssl_ctx_onfree_list_st *newonfree
171         = OPENSSL_malloc(sizeof(*newonfree));
172
173     if (newonfree == NULL)
174         return 0;
175
176     newonfree->fn = onfreefn;
177     newonfree->next = ctx->onfreelist;
178     ctx->onfreelist = newonfree;
179
180     return 1;
181 }