/src/openssl41/crypto/rand/rand_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 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 | | /* We need to use some RAND deprecated APIs */ |
11 | | #define OPENSSL_SUPPRESS_DEPRECATED |
12 | | |
13 | | #include <openssl/err.h> |
14 | | #include <openssl/opensslconf.h> |
15 | | #include <openssl/core_names.h> |
16 | | #include <openssl/provider.h> |
17 | | #include "internal/cryptlib.h" |
18 | | #include "internal/provider.h" |
19 | | #include "internal/thread_once.h" |
20 | | #include "internal/threads_common.h" |
21 | | #include "crypto/rand.h" |
22 | | #include "crypto/cryptlib.h" |
23 | | #include "rand_local.h" |
24 | | #include "crypto/context.h" |
25 | | #include "internal/provider.h" |
26 | | #include "internal/common.h" |
27 | | |
28 | | typedef struct rand_global_st { |
29 | | /* |
30 | | * The three shared DRBG instances |
31 | | * |
32 | | * There are three shared DRBG instances: <primary>, <public>, and |
33 | | * <private>. The <public> and <private> DRBGs are secondary ones. |
34 | | * These are used for non-secret (e.g. nonces) and secret |
35 | | * (e.g. private keys) data respectively. |
36 | | */ |
37 | | CRYPTO_RWLOCK *lock; |
38 | | |
39 | | EVP_RAND_CTX *seed; |
40 | | |
41 | | /* |
42 | | * The <primary> DRBG |
43 | | * |
44 | | * Not used directly by the application, only for reseeding the two other |
45 | | * DRBGs. It reseeds itself by pulling either randomness from os entropy |
46 | | * sources or by consuming randomness which was added by RAND_add(). |
47 | | * |
48 | | * The <primary> DRBG is a global instance which is accessed concurrently by |
49 | | * all threads. The necessary locking is managed automatically by its child |
50 | | * DRBG instances during reseeding. |
51 | | */ |
52 | | EVP_RAND_CTX *primary; |
53 | | |
54 | | /* |
55 | | * The provider which we'll use to generate randomness. |
56 | | */ |
57 | | #ifndef FIPS_MODULE |
58 | | OSSL_PROVIDER *random_provider; |
59 | | char *random_provider_name; |
60 | | #endif /* !FIPS_MODULE */ |
61 | | |
62 | | /* Which RNG is being used by default and it's configuration settings */ |
63 | | char *rng_name; |
64 | | char *rng_cipher; |
65 | | char *rng_digest; |
66 | | char *rng_propq; |
67 | | |
68 | | /* Allow the randomness source to be changed */ |
69 | | char *seed_name; |
70 | | char *seed_propq; |
71 | | |
72 | | /* |
73 | | * Whether the seed source may never fall back to the OS entropy: |
74 | | * 1 strict, 0 not strict, -1 unset (strict only for JITTER) |
75 | | */ |
76 | | int seed_strict; |
77 | | } RAND_GLOBAL; |
78 | | |
79 | | static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl); |
80 | | static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl); |
81 | | static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl); |
82 | | |
83 | | static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx) |
84 | 1.93M | { |
85 | 1.93M | return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX); |
86 | 1.93M | } |
87 | | |
88 | | #ifndef FIPS_MODULE |
89 | | #include <stdio.h> |
90 | | #include <time.h> |
91 | | #include <limits.h> |
92 | | #include <openssl/conf.h> |
93 | | #include <openssl/trace.h> |
94 | | #include "internal/conf.h" |
95 | | #include "crypto/rand_pool.h" |
96 | | #include "prov/seeding.h" |
97 | | #include "internal/e_os.h" |
98 | | #include "internal/property.h" |
99 | | |
100 | | /* |
101 | | * The default name for the random provider. |
102 | | * This ensures that the FIPS provider will supply libcrypto's random byte |
103 | | * requirements. |
104 | | */ |
105 | | static const char random_provider_fips_name[] = "fips"; |
106 | | |
107 | | static int set_random_provider_name(RAND_GLOBAL *dgbl, const char *name) |
108 | 0 | { |
109 | 0 | if (dgbl->random_provider_name != NULL |
110 | 0 | && OPENSSL_strcasecmp(dgbl->random_provider_name, name) == 0) |
111 | 0 | return 1; |
112 | | |
113 | 0 | OPENSSL_free(dgbl->random_provider_name); |
114 | 0 | dgbl->random_provider_name = OPENSSL_strdup(name); |
115 | 0 | return dgbl->random_provider_name != NULL; |
116 | 0 | } |
117 | | |
118 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
119 | | static CRYPTO_RWLOCK *rand_meth_lock; |
120 | | static const RAND_METHOD *default_RAND_meth; |
121 | | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
122 | | static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT; |
123 | | |
124 | | static int rand_inited = 0; |
125 | | |
126 | | DEFINE_RUN_ONCE_STATIC(do_rand_init) |
127 | 33 | { |
128 | 33 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
129 | 33 | rand_meth_lock = CRYPTO_THREAD_lock_new(); |
130 | 33 | if (rand_meth_lock == NULL) |
131 | 0 | goto err; |
132 | 33 | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
133 | | |
134 | 33 | if (!ossl_rand_pool_init()) |
135 | 0 | goto err; |
136 | | |
137 | 33 | rand_inited = 1; |
138 | 33 | return 1; |
139 | | |
140 | 0 | err: |
141 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
142 | 0 | CRYPTO_THREAD_lock_free(rand_meth_lock); |
143 | 0 | rand_meth_lock = NULL; |
144 | 0 | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
145 | 0 | return 0; |
146 | 33 | } |
147 | | |
148 | | void ossl_rand_cleanup_int(void) |
149 | 154 | { |
150 | 154 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
151 | 154 | const RAND_METHOD *meth = default_RAND_meth; |
152 | | |
153 | 154 | if (!rand_inited) |
154 | 110 | return; |
155 | | |
156 | 44 | if (meth != NULL && meth->cleanup != NULL) |
157 | 0 | meth->cleanup(); |
158 | 44 | RAND_set_rand_method(NULL); |
159 | 44 | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
160 | 44 | ossl_rand_pool_cleanup(); |
161 | 44 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
162 | 44 | CRYPTO_THREAD_lock_free(rand_meth_lock); |
163 | 44 | rand_meth_lock = NULL; |
164 | 44 | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
165 | 44 | ossl_release_default_drbg_ctx(); |
166 | 44 | rand_inited = 0; |
167 | 44 | } |
168 | | |
169 | | /* |
170 | | * RAND_close_seed_files() ensures that any seed file descriptors are |
171 | | * closed after use. This only applies to libcrypto/default provider, |
172 | | * it does not apply to other providers. |
173 | | */ |
174 | | void RAND_keep_random_devices_open(int keep) |
175 | 0 | { |
176 | 0 | if (RUN_ONCE(&rand_init, do_rand_init)) |
177 | 0 | ossl_rand_pool_keep_random_devices_open(keep); |
178 | 0 | } |
179 | | |
180 | | /* |
181 | | * RAND_poll() reseeds the default RNG using random input |
182 | | * |
183 | | * The random input is obtained from polling various entropy |
184 | | * sources which depend on the operating system and are |
185 | | * configurable via the --with-rand-seed configure option. |
186 | | */ |
187 | | int RAND_poll(void) |
188 | 0 | { |
189 | 0 | static const char salt[] = "polling"; |
190 | |
|
191 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
192 | 0 | const RAND_METHOD *meth = RAND_get_rand_method(); |
193 | 0 | int ret = meth == RAND_OpenSSL(); |
194 | |
|
195 | 0 | if (meth == NULL) |
196 | 0 | return 0; |
197 | | |
198 | 0 | if (!ret) { |
199 | | /* fill random pool and seed the current legacy RNG */ |
200 | 0 | RAND_POOL *pool = ossl_rand_pool_new(RAND_DRBG_STRENGTH, 1, |
201 | 0 | (RAND_DRBG_STRENGTH + 7) / 8, |
202 | 0 | RAND_POOL_MAX_LENGTH); |
203 | |
|
204 | 0 | if (pool == NULL) |
205 | 0 | return 0; |
206 | | |
207 | 0 | if (ossl_pool_acquire_entropy(pool) == 0) |
208 | 0 | goto err; |
209 | | |
210 | 0 | if (meth->add == NULL |
211 | 0 | || meth->add(ossl_rand_pool_buffer(pool), |
212 | 0 | (int)ossl_rand_pool_length(pool), |
213 | 0 | (ossl_rand_pool_entropy(pool) / 8.0)) |
214 | 0 | == 0) |
215 | 0 | goto err; |
216 | | |
217 | 0 | ret = 1; |
218 | 0 | err: |
219 | 0 | ossl_rand_pool_free(pool); |
220 | 0 | return ret; |
221 | 0 | } |
222 | 0 | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
223 | | |
224 | 0 | RAND_seed(salt, sizeof(salt)); |
225 | 0 | return 1; |
226 | 0 | } |
227 | | |
228 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
229 | | static int rand_set_rand_method_internal(const RAND_METHOD *meth, |
230 | | ENGINE *e) |
231 | 0 | { |
232 | 0 | if (!ossl_assert(e == NULL)) |
233 | 0 | return 0; |
234 | 0 | if (!RUN_ONCE(&rand_init, do_rand_init)) |
235 | 0 | return 0; |
236 | 0 | if (!CRYPTO_atomic_store_ptr((void **)&default_RAND_meth, (void **)&meth, |
237 | 0 | rand_meth_lock)) |
238 | 0 | return 0; |
239 | 0 | return 1; |
240 | 0 | } |
241 | | |
242 | | int RAND_set_rand_method(const RAND_METHOD *meth) |
243 | 44 | { |
244 | 44 | return rand_set_rand_method_internal(meth, NULL); |
245 | 44 | } |
246 | | |
247 | | const RAND_METHOD *RAND_get_rand_method(void) |
248 | 536k | { |
249 | 536k | const RAND_METHOD *tmp_meth = NULL; |
250 | 536k | int lock_failed; |
251 | | |
252 | 536k | if (!RUN_ONCE(&rand_init, do_rand_init)) |
253 | 0 | goto end; |
254 | | |
255 | 536k | if (CRYPTO_atomic_load_ptr((void **)&default_RAND_meth, (void **)&tmp_meth, |
256 | 536k | rand_meth_lock)) { |
257 | 536k | if (tmp_meth != NULL) |
258 | 536k | return tmp_meth; |
259 | 536k | } else { |
260 | 0 | return NULL; |
261 | 0 | } |
262 | | |
263 | | /* |
264 | | * We atomically compare and exchange default_RAND_meth |
265 | | * if default_RAND_meth is NULL, we assign ossl_rand_meth to it |
266 | | * If this returns 1, then the exchange was successful, and we can just |
267 | | * return &ossl_rand_meth |
268 | | * If it fails, then the contents of default_RAND_meth are written to tmp_meth |
269 | | * which we can just return as is |
270 | | */ |
271 | 19 | if (CRYPTO_atomic_cmp_exch_ptr((void **)&default_RAND_meth, (void **)&tmp_meth, |
272 | 19 | (void *)&ossl_rand_meth, rand_meth_lock, &lock_failed)) { |
273 | 19 | tmp_meth = &ossl_rand_meth; |
274 | 19 | } else { |
275 | 0 | if (lock_failed == 1) |
276 | 0 | return NULL; |
277 | 0 | } |
278 | 19 | end: |
279 | 19 | return tmp_meth; |
280 | 19 | } |
281 | | #endif /* OPENSSL_NO_DEPRECATED_3_0 */ |
282 | | |
283 | | void RAND_seed(const void *buf, int num) |
284 | 0 | { |
285 | 0 | EVP_RAND_CTX *drbg; |
286 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
287 | 0 | const RAND_METHOD *meth = RAND_get_rand_method(); |
288 | |
|
289 | 0 | if (meth != NULL && meth->seed != NULL) { |
290 | 0 | meth->seed(buf, num); |
291 | 0 | return; |
292 | 0 | } |
293 | 0 | #endif |
294 | | |
295 | 0 | drbg = RAND_get0_primary(NULL); |
296 | 0 | if (drbg != NULL && num > 0) |
297 | 0 | EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num); |
298 | 0 | } |
299 | | |
300 | | void RAND_add(const void *buf, int num, double randomness) |
301 | 0 | { |
302 | 0 | EVP_RAND_CTX *drbg; |
303 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
304 | 0 | const RAND_METHOD *meth = RAND_get_rand_method(); |
305 | |
|
306 | 0 | if (meth != NULL && meth->add != NULL) { |
307 | 0 | meth->add(buf, num, randomness); |
308 | 0 | return; |
309 | 0 | } |
310 | 0 | #endif |
311 | 0 | drbg = RAND_get0_primary(NULL); |
312 | 0 | if (drbg != NULL && num > 0) |
313 | | #ifdef OPENSSL_RAND_SEED_NONE |
314 | | /* Without an entropy source, we have to rely on the user */ |
315 | | EVP_RAND_reseed(drbg, 0, buf, num, NULL, 0); |
316 | | #else |
317 | | /* With an entropy source, we downgrade this to additional input */ |
318 | 0 | EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num); |
319 | 0 | #endif |
320 | 0 | } |
321 | | |
322 | | #if !defined(OPENSSL_NO_DEPRECATED_1_1_0) |
323 | | int RAND_pseudo_bytes(unsigned char *buf, int num) |
324 | 0 | { |
325 | 0 | const RAND_METHOD *meth = RAND_get_rand_method(); |
326 | |
|
327 | 0 | if (meth != NULL && meth->pseudorand != NULL) |
328 | 0 | return meth->pseudorand(buf, num); |
329 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED); |
330 | 0 | return -1; |
331 | 0 | } |
332 | | #endif |
333 | | |
334 | | int RAND_status(void) |
335 | 0 | { |
336 | 0 | EVP_RAND_CTX *rand; |
337 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
338 | 0 | const RAND_METHOD *meth = RAND_get_rand_method(); |
339 | |
|
340 | 0 | if (meth != NULL && meth != RAND_OpenSSL()) |
341 | 0 | return meth->status != NULL ? meth->status() : 0; |
342 | 0 | #endif |
343 | | |
344 | 0 | if ((rand = RAND_get0_primary(NULL)) == NULL) |
345 | 0 | return 0; |
346 | 0 | return EVP_RAND_get_state(rand) == EVP_RAND_STATE_READY; |
347 | 0 | } |
348 | | #else /* !FIPS_MODULE */ |
349 | | |
350 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
351 | | const RAND_METHOD *RAND_get_rand_method(void) |
352 | | { |
353 | | return NULL; |
354 | | } |
355 | | #endif |
356 | | #endif /* !FIPS_MODULE */ |
357 | | |
358 | | /* |
359 | | * This function is not part of RAND_METHOD, so if we're not using |
360 | | * the default method, then just call RAND_bytes(). Otherwise make |
361 | | * sure we're instantiated and use the private DRBG. |
362 | | */ |
363 | | int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num, |
364 | | unsigned int strength) |
365 | 769k | { |
366 | 769k | RAND_GLOBAL *dgbl; |
367 | 769k | EVP_RAND_CTX *rand; |
368 | 769k | #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE) |
369 | 769k | const RAND_METHOD *meth = RAND_get_rand_method(); |
370 | | |
371 | 769k | if (meth != NULL && meth != RAND_OpenSSL()) { |
372 | 0 | if (num > INT_MAX) { |
373 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ARGUMENT_OUT_OF_RANGE); |
374 | 0 | return -1; |
375 | 0 | } |
376 | 0 | if (meth->bytes != NULL) |
377 | 0 | return meth->bytes(buf, (int)num); |
378 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED); |
379 | 0 | return -1; |
380 | 0 | } |
381 | 769k | #endif |
382 | | |
383 | 769k | dgbl = rand_get_global(ctx); |
384 | 769k | if (dgbl == NULL) |
385 | 0 | return 0; |
386 | 769k | #ifndef FIPS_MODULE |
387 | 769k | if (dgbl->random_provider != NULL) |
388 | 0 | return ossl_provider_random_bytes(dgbl->random_provider, |
389 | 0 | OSSL_PROV_RANDOM_PRIVATE, |
390 | 0 | buf, num, strength); |
391 | 769k | #endif /* !FIPS_MODULE */ |
392 | 769k | rand = rand_get0_private(ctx, dgbl); |
393 | 769k | if (rand != NULL) |
394 | 769k | return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0); |
395 | | |
396 | 0 | return 0; |
397 | 769k | } |
398 | | |
399 | | int RAND_priv_bytes(unsigned char *buf, int num) |
400 | 0 | { |
401 | 0 | if (num < 0) |
402 | 0 | return 0; |
403 | 0 | return RAND_priv_bytes_ex(NULL, buf, (size_t)num, 0); |
404 | 0 | } |
405 | | |
406 | | int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num, |
407 | | unsigned int strength) |
408 | 357k | { |
409 | 357k | RAND_GLOBAL *dgbl; |
410 | 357k | EVP_RAND_CTX *rand; |
411 | 357k | #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE) |
412 | 357k | const RAND_METHOD *meth = RAND_get_rand_method(); |
413 | | |
414 | 357k | if (meth != NULL && meth != RAND_OpenSSL()) { |
415 | 0 | if (num > INT_MAX) { |
416 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ARGUMENT_OUT_OF_RANGE); |
417 | 0 | return -1; |
418 | 0 | } |
419 | 0 | if (meth->bytes != NULL) |
420 | 0 | return meth->bytes(buf, (int)num); |
421 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED); |
422 | 0 | return -1; |
423 | 0 | } |
424 | 357k | #endif |
425 | | |
426 | 357k | dgbl = rand_get_global(ctx); |
427 | 357k | if (dgbl == NULL) |
428 | 0 | return 0; |
429 | 357k | #ifndef FIPS_MODULE |
430 | 357k | if (dgbl->random_provider != NULL) |
431 | 0 | return ossl_provider_random_bytes(dgbl->random_provider, |
432 | 0 | OSSL_PROV_RANDOM_PUBLIC, |
433 | 0 | buf, num, strength); |
434 | 357k | #endif /* !FIPS_MODULE */ |
435 | | |
436 | 357k | rand = rand_get0_public(ctx, dgbl); |
437 | 357k | if (rand != NULL) |
438 | 357k | return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0); |
439 | | |
440 | 0 | return 0; |
441 | 357k | } |
442 | | |
443 | | int RAND_bytes(unsigned char *buf, int num) |
444 | 985 | { |
445 | 985 | if (num < 0) |
446 | 0 | return 0; |
447 | 985 | return RAND_bytes_ex(NULL, buf, (size_t)num, 0); |
448 | 985 | } |
449 | | |
450 | | /* |
451 | | * Initialize the OSSL_LIB_CTX global DRBGs on first use. |
452 | | * Returns the allocated global data on success or NULL on failure. |
453 | | */ |
454 | | void *ossl_rand_ctx_new(OSSL_LIB_CTX *libctx) |
455 | 274 | { |
456 | 274 | RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl)); |
457 | | |
458 | 274 | if (dgbl == NULL) |
459 | 0 | return NULL; |
460 | | |
461 | 274 | dgbl->seed_strict = -1; |
462 | | |
463 | 274 | #ifndef FIPS_MODULE |
464 | | /* |
465 | | * We need to ensure that base libcrypto thread handling has been |
466 | | * initialised. |
467 | | */ |
468 | 274 | OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL); |
469 | | |
470 | | /* Prepopulate the random provider name */ |
471 | 274 | dgbl->random_provider_name = OPENSSL_strdup(random_provider_fips_name); |
472 | 274 | if (dgbl->random_provider_name == NULL) |
473 | 0 | goto err0; |
474 | 274 | #endif |
475 | | |
476 | 274 | dgbl->lock = CRYPTO_THREAD_lock_new(); |
477 | 274 | if (dgbl->lock == NULL) |
478 | 0 | goto err1; |
479 | | |
480 | 274 | return dgbl; |
481 | | |
482 | 0 | err1: |
483 | 0 | CRYPTO_THREAD_lock_free(dgbl->lock); |
484 | 0 | #ifndef FIPS_MODULE |
485 | 0 | err0: |
486 | 0 | OPENSSL_free(dgbl->random_provider_name); |
487 | 0 | #endif |
488 | 0 | OPENSSL_free(dgbl); |
489 | 0 | return NULL; |
490 | 0 | } |
491 | | |
492 | | void ossl_rand_ctx_free(void *vdgbl) |
493 | 154 | { |
494 | 154 | RAND_GLOBAL *dgbl = vdgbl; |
495 | | |
496 | 154 | if (dgbl == NULL) |
497 | 0 | return; |
498 | | |
499 | 154 | CRYPTO_THREAD_lock_free(dgbl->lock); |
500 | 154 | EVP_RAND_CTX_free(dgbl->primary); |
501 | 154 | EVP_RAND_CTX_free(dgbl->seed); |
502 | 154 | #ifndef FIPS_MODULE |
503 | 154 | OPENSSL_free(dgbl->random_provider_name); |
504 | 154 | #endif /* !FIPS_MODULE */ |
505 | 154 | OPENSSL_free(dgbl->rng_name); |
506 | 154 | OPENSSL_free(dgbl->rng_cipher); |
507 | 154 | OPENSSL_free(dgbl->rng_digest); |
508 | 154 | OPENSSL_free(dgbl->rng_propq); |
509 | 154 | OPENSSL_free(dgbl->seed_name); |
510 | 154 | OPENSSL_free(dgbl->seed_propq); |
511 | | |
512 | 154 | OPENSSL_free(dgbl); |
513 | 154 | } |
514 | | |
515 | | static void rand_delete_thread_state(void *arg) |
516 | 44 | { |
517 | 44 | OSSL_LIB_CTX *ctx = arg; |
518 | 44 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
519 | 44 | EVP_RAND_CTX *rand; |
520 | | |
521 | 44 | if (dgbl == NULL) |
522 | 0 | return; |
523 | | |
524 | 44 | rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx); |
525 | 44 | CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, NULL); |
526 | 44 | EVP_RAND_CTX_free(rand); |
527 | | |
528 | 44 | rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx); |
529 | 44 | CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, NULL); |
530 | 44 | EVP_RAND_CTX_free(rand); |
531 | 44 | } |
532 | | |
533 | | #if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER) |
534 | | /* |
535 | | * Return 1 if the seed source must always be used and never be silently |
536 | | * substituted by the operating system entropy sources: requested via the |
537 | | * seed_strict option of the [random] configuration section, defaulting |
538 | | * to strict for the JITTER seed source or implied by an |
539 | | * enable-fips-jitter build which hard-wires the JITTER seed source. |
540 | | */ |
541 | | static int rand_seed_source_strict(ossl_unused RAND_GLOBAL *dgbl) |
542 | 805 | { |
543 | 805 | #ifdef OPENSSL_NO_FIPS_JITTER |
544 | 805 | const char *name; |
545 | | |
546 | 805 | if (dgbl->seed_strict >= 0) |
547 | 0 | return dgbl->seed_strict; |
548 | 805 | name = dgbl->seed_name != NULL ? dgbl->seed_name : OPENSSL_SEED_SRC_NAME; |
549 | 805 | return OPENSSL_strcasecmp(name, "JITTER") == 0; |
550 | | #else /* !OPENSSL_NO_FIPS_JITTER */ |
551 | | return 1; |
552 | | #endif /* OPENSSL_NO_FIPS_JITTER */ |
553 | 805 | } |
554 | | |
555 | | static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx) |
556 | 19 | { |
557 | 19 | EVP_RAND *rand; |
558 | 19 | const char *propq; |
559 | 19 | char *name; |
560 | 19 | EVP_RAND_CTX *ctx = NULL; |
561 | 19 | int fallback = 0; |
562 | 19 | #ifdef OPENSSL_NO_FIPS_JITTER |
563 | 19 | RAND_GLOBAL *dgbl = rand_get_global(libctx); |
564 | | |
565 | 19 | if (dgbl == NULL) |
566 | 0 | return NULL; |
567 | 19 | propq = dgbl->seed_propq; |
568 | | #ifdef OPENSSL_DEFAULT_SEED_PROPQ |
569 | | if (propq == NULL) |
570 | | propq = OPENSSL_MSTR(OPENSSL_DEFAULT_SEED_PROPQ); |
571 | | #endif /* OPENSSL_DEFAULT_SEED_PROPQ */ |
572 | 19 | if (dgbl->seed_name != NULL) { |
573 | 0 | name = dgbl->seed_name; |
574 | 19 | } else { |
575 | 19 | fallback = !rand_seed_source_strict(dgbl); |
576 | 19 | name = OPENSSL_SEED_SRC_NAME; |
577 | 19 | } |
578 | | #else /* !OPENSSL_NO_FIPS_JITTER */ |
579 | | name = OPENSSL_SEED_SRC_NAME; |
580 | | propq = ""; |
581 | | #endif /* OPENSSL_NO_FIPS_JITTER */ |
582 | | |
583 | 19 | ERR_set_mark(); |
584 | 19 | rand = EVP_RAND_fetch(libctx, name, propq); |
585 | 19 | ERR_pop_to_mark(); |
586 | 19 | if (rand == NULL) { |
587 | 0 | if (!fallback) |
588 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG); |
589 | 0 | goto err; |
590 | 0 | } |
591 | 19 | ctx = EVP_RAND_CTX_new(rand, NULL); |
592 | 19 | EVP_RAND_free(rand); |
593 | 19 | if (ctx == NULL) { |
594 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG); |
595 | 0 | goto err; |
596 | 0 | } |
597 | 19 | if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) { |
598 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG); |
599 | 0 | goto err; |
600 | 0 | } |
601 | 19 | return ctx; |
602 | 0 | err: |
603 | 0 | EVP_RAND_CTX_free(ctx); |
604 | 0 | return NULL; |
605 | 19 | } |
606 | | |
607 | | /* |
608 | | * Get the global seed source, creating and storing it if it does not |
609 | | * exist yet. If several threads race here, exactly one instance is |
610 | | * kept and returned to all of them. |
611 | | */ |
612 | | static EVP_RAND_CTX *rand_get0_seed(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) |
613 | 33 | { |
614 | 33 | EVP_RAND_CTX *ret, *seed; |
615 | | |
616 | 33 | if (!CRYPTO_THREAD_read_lock(dgbl->lock)) |
617 | 0 | return NULL; |
618 | 33 | ret = dgbl->seed; |
619 | 33 | CRYPTO_THREAD_unlock(dgbl->lock); |
620 | 33 | if (ret != NULL) |
621 | 0 | return ret; |
622 | | |
623 | 33 | seed = rand_new_seed(ctx); |
624 | 33 | if (seed == NULL) |
625 | 0 | return NULL; |
626 | | |
627 | 33 | if (!CRYPTO_THREAD_write_lock(dgbl->lock)) { |
628 | 0 | EVP_RAND_CTX_free(seed); |
629 | 0 | return NULL; |
630 | 0 | } |
631 | 33 | if (dgbl->seed == NULL) { |
632 | 33 | dgbl->seed = seed; |
633 | 33 | seed = NULL; |
634 | 33 | } |
635 | 33 | ret = dgbl->seed; |
636 | 33 | CRYPTO_THREAD_unlock(dgbl->lock); |
637 | | /* Free the instance that lost a creation race */ |
638 | 33 | EVP_RAND_CTX_free(seed); |
639 | 33 | return ret; |
640 | 33 | } |
641 | | #endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */ |
642 | | |
643 | | #ifndef FIPS_MODULE |
644 | | EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx) |
645 | 3.42k | { |
646 | 3.42k | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
647 | 3.42k | EVP_RAND_CTX *ret; |
648 | | |
649 | 3.42k | if (dgbl == NULL) |
650 | 0 | return NULL; |
651 | | |
652 | 3.42k | if (!CRYPTO_THREAD_read_lock(dgbl->lock)) |
653 | 0 | return NULL; |
654 | 3.42k | ret = dgbl->seed; |
655 | 3.42k | CRYPTO_THREAD_unlock(dgbl->lock); |
656 | 3.42k | return ret; |
657 | 3.42k | } |
658 | | |
659 | | EVP_RAND_CTX *ossl_rand_get0_seed(OSSL_LIB_CTX *ctx) |
660 | 0 | { |
661 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
662 | |
|
663 | 0 | if (dgbl == NULL) |
664 | 0 | return NULL; |
665 | 0 | return rand_get0_seed(ctx, dgbl); |
666 | 0 | } |
667 | | |
668 | | int ossl_rand_seed_source_strict(OSSL_LIB_CTX *ctx) |
669 | 786 | { |
670 | 786 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
671 | | |
672 | 786 | return dgbl != NULL && rand_seed_source_strict(dgbl); |
673 | 786 | } |
674 | | #endif /* !FIPS_MODULE */ |
675 | | |
676 | | static EVP_RAND_CTX *rand_new_drbg(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent, |
677 | | unsigned int reseed_interval, |
678 | | time_t reseed_time_interval) |
679 | 201 | { |
680 | 201 | EVP_RAND *rand; |
681 | 201 | RAND_GLOBAL *dgbl = rand_get_global(libctx); |
682 | 201 | EVP_RAND_CTX *ctx; |
683 | 201 | OSSL_PARAM params[9], *p = params; |
684 | 201 | const OSSL_PARAM *settables; |
685 | 201 | char *name, *cipher; |
686 | 201 | int use_df = 1; |
687 | | |
688 | 201 | if (dgbl == NULL) |
689 | 0 | return NULL; |
690 | 201 | name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG"; |
691 | 201 | rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq); |
692 | 201 | if (rand == NULL) { |
693 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG); |
694 | 0 | return NULL; |
695 | 0 | } |
696 | 201 | ctx = EVP_RAND_CTX_new(rand, parent); |
697 | 201 | EVP_RAND_free(rand); |
698 | 201 | if (ctx == NULL) { |
699 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG); |
700 | 0 | return NULL; |
701 | 0 | } |
702 | | |
703 | 201 | settables = EVP_RAND_CTX_settable_params(ctx); |
704 | 201 | if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_CIPHER)) { |
705 | 52 | cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR"; |
706 | 52 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER, |
707 | 52 | cipher, 0); |
708 | 52 | } |
709 | 201 | if (dgbl->rng_digest != NULL |
710 | 0 | && OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_DIGEST)) |
711 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST, |
712 | 0 | dgbl->rng_digest, 0); |
713 | 201 | if (dgbl->rng_propq != NULL) |
714 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, |
715 | 0 | dgbl->rng_propq, 0); |
716 | 201 | if (OSSL_PARAM_locate_const(settables, OSSL_ALG_PARAM_MAC)) |
717 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0); |
718 | 201 | if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_USE_DF)) |
719 | 52 | *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &use_df); |
720 | 201 | *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, |
721 | 201 | &reseed_interval); |
722 | 201 | *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, |
723 | 201 | &reseed_time_interval); |
724 | 201 | *p = OSSL_PARAM_construct_end(); |
725 | 201 | if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) { |
726 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG); |
727 | 0 | EVP_RAND_CTX_free(ctx); |
728 | 0 | return NULL; |
729 | 0 | } |
730 | 201 | return ctx; |
731 | 201 | } |
732 | | |
733 | | #if defined(FIPS_MODULE) |
734 | | static EVP_RAND_CTX *rand_new_crngt(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent) |
735 | | { |
736 | | EVP_RAND *rand; |
737 | | EVP_RAND_CTX *ctx; |
738 | | |
739 | | rand = EVP_RAND_fetch(libctx, "CRNG-TEST", "-fips"); |
740 | | if (rand == NULL) { |
741 | | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG); |
742 | | return NULL; |
743 | | } |
744 | | ctx = EVP_RAND_CTX_new(rand, parent); |
745 | | EVP_RAND_free(rand); |
746 | | if (ctx == NULL) { |
747 | | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG); |
748 | | return NULL; |
749 | | } |
750 | | |
751 | | if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) { |
752 | | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG); |
753 | | EVP_RAND_CTX_free(ctx); |
754 | | return NULL; |
755 | | } |
756 | | return ctx; |
757 | | } |
758 | | #endif /* FIPS_MODULE */ |
759 | | |
760 | | /* |
761 | | * Get the primary random generator. |
762 | | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. |
763 | | * |
764 | | */ |
765 | | static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) |
766 | 53 | { |
767 | 53 | EVP_RAND_CTX *ret, *seed = NULL, *primary; |
768 | | |
769 | 53 | if (dgbl == NULL) |
770 | 0 | return NULL; |
771 | | |
772 | 53 | if (!CRYPTO_THREAD_read_lock(dgbl->lock)) |
773 | 0 | return NULL; |
774 | | |
775 | 53 | ret = dgbl->primary; |
776 | 53 | CRYPTO_THREAD_unlock(dgbl->lock); |
777 | | |
778 | 53 | if (ret != NULL) |
779 | 20 | return ret; |
780 | | |
781 | 33 | #if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER) |
782 | | /* Create a seed source for libcrypto or jitter enabled FIPS provider */ |
783 | 33 | ERR_set_mark(); |
784 | 33 | seed = rand_get0_seed(ctx, dgbl); |
785 | 33 | if (seed == NULL && ERR_count_to_mark() > 0) { |
786 | 0 | ERR_clear_last_mark(); |
787 | 0 | return NULL; |
788 | 0 | } |
789 | 33 | ERR_pop_to_mark(); |
790 | 33 | #endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */ |
791 | | |
792 | | #if defined(FIPS_MODULE) |
793 | | /* The FIPS provider has entropy health tests instead of the primary */ |
794 | | ret = rand_new_crngt(ctx, seed); |
795 | | #else /* FIPS_MODULE */ |
796 | 33 | ret = rand_new_drbg(ctx, seed, PRIMARY_RESEED_INTERVAL, |
797 | 33 | PRIMARY_RESEED_TIME_INTERVAL); |
798 | 33 | #endif /* FIPS_MODULE */ |
799 | | |
800 | 33 | if (ret == NULL) |
801 | 0 | return NULL; |
802 | | |
803 | | /* |
804 | | * The primary DRBG may be shared between multiple threads so we must |
805 | | * enable locking. |
806 | | */ |
807 | 33 | if (!EVP_RAND_enable_locking(ret)) { |
808 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING); |
809 | 0 | EVP_RAND_CTX_free(ret); |
810 | 0 | return NULL; |
811 | 0 | } |
812 | | |
813 | 33 | if (!CRYPTO_THREAD_write_lock(dgbl->lock)) { |
814 | 0 | EVP_RAND_CTX_free(ret); |
815 | 0 | return NULL; |
816 | 0 | } |
817 | | |
818 | 33 | primary = dgbl->primary; |
819 | 33 | if (primary != NULL) { |
820 | 0 | CRYPTO_THREAD_unlock(dgbl->lock); |
821 | 0 | EVP_RAND_CTX_free(ret); |
822 | 0 | return primary; |
823 | 0 | } |
824 | 33 | dgbl->primary = ret; |
825 | 33 | CRYPTO_THREAD_unlock(dgbl->lock); |
826 | | |
827 | 33 | return ret; |
828 | 33 | } |
829 | | |
830 | | /* |
831 | | * Get the primary random generator. |
832 | | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. |
833 | | * |
834 | | */ |
835 | | EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx) |
836 | 0 | { |
837 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
838 | |
|
839 | 0 | return dgbl == NULL ? NULL : rand_get0_primary(ctx, dgbl); |
840 | 0 | } |
841 | | |
842 | | static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) |
843 | 357k | { |
844 | 357k | EVP_RAND_CTX *rand, *primary; |
845 | 357k | OSSL_LIB_CTX *origctx = ctx; |
846 | | |
847 | 357k | ctx = ossl_lib_ctx_get_concrete(ctx); |
848 | | |
849 | 357k | if (ctx == NULL) |
850 | 0 | return NULL; |
851 | | |
852 | 357k | if (dgbl == NULL) |
853 | 0 | return NULL; |
854 | | |
855 | 357k | rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx); |
856 | 357k | if (rand == NULL) { |
857 | 34 | primary = rand_get0_primary(origctx, dgbl); |
858 | 34 | if (primary == NULL) |
859 | 0 | return NULL; |
860 | | |
861 | | /* |
862 | | * If the private is also NULL then this is the first time we've |
863 | | * used this thread. |
864 | | */ |
865 | 34 | if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx) == NULL |
866 | 28 | && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state)) |
867 | 0 | return NULL; |
868 | 34 | rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL, |
869 | 34 | SECONDARY_RESEED_TIME_INTERVAL); |
870 | 34 | if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) { |
871 | 0 | EVP_RAND_CTX_free(rand); |
872 | 0 | rand = NULL; |
873 | 0 | } |
874 | 34 | } |
875 | 357k | return rand; |
876 | 357k | } |
877 | | |
878 | | /* |
879 | | * Get the public random generator. |
880 | | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. |
881 | | */ |
882 | | EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx) |
883 | | { |
884 | | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
885 | | |
886 | | return dgbl == NULL ? NULL : rand_get0_public(ctx, dgbl); |
887 | | } |
888 | | |
889 | | static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) |
890 | 769k | { |
891 | 769k | EVP_RAND_CTX *rand, *primary; |
892 | 769k | OSSL_LIB_CTX *origctx = ctx; |
893 | | |
894 | 769k | ctx = ossl_lib_ctx_get_concrete(ctx); |
895 | 769k | if (ctx == NULL) |
896 | 0 | return NULL; |
897 | | |
898 | 769k | rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx); |
899 | 769k | if (rand == NULL) { |
900 | 45 | primary = rand_get0_primary(origctx, dgbl); |
901 | 45 | if (primary == NULL) |
902 | 0 | return NULL; |
903 | | |
904 | | /* |
905 | | * If the public is also NULL then this is the first time we've |
906 | | * used this thread. |
907 | | */ |
908 | 45 | if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx) == NULL |
909 | 21 | && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state)) |
910 | 0 | return NULL; |
911 | 45 | rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL, |
912 | 45 | SECONDARY_RESEED_TIME_INTERVAL); |
913 | 45 | if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) { |
914 | 0 | EVP_RAND_CTX_free(rand); |
915 | 0 | rand = NULL; |
916 | 0 | } |
917 | 45 | } |
918 | 769k | return rand; |
919 | 769k | } |
920 | | |
921 | | /* |
922 | | * Get the private random generator. |
923 | | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. |
924 | | */ |
925 | | EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx) |
926 | | { |
927 | | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
928 | | |
929 | | return dgbl == NULL ? NULL : rand_get0_private(ctx, dgbl); |
930 | | } |
931 | | |
932 | | #ifdef FIPS_MODULE |
933 | | EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx) |
934 | | { |
935 | | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
936 | | |
937 | | if (dgbl == NULL) |
938 | | return NULL; |
939 | | |
940 | | return CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx); |
941 | | } |
942 | | #endif |
943 | | |
944 | | int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand) |
945 | 0 | { |
946 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
947 | 0 | EVP_RAND_CTX *old; |
948 | 0 | int r; |
949 | |
|
950 | 0 | if (dgbl == NULL) |
951 | 0 | return 0; |
952 | 0 | old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx); |
953 | 0 | if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) > 0) |
954 | 0 | EVP_RAND_CTX_free(old); |
955 | 0 | return r; |
956 | 0 | } |
957 | | |
958 | | int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand) |
959 | 0 | { |
960 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
961 | 0 | EVP_RAND_CTX *old; |
962 | 0 | int r; |
963 | |
|
964 | 0 | if (dgbl == NULL) |
965 | 0 | return 0; |
966 | 0 | old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx); |
967 | 0 | if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) > 0) |
968 | 0 | EVP_RAND_CTX_free(old); |
969 | 0 | return r; |
970 | 0 | } |
971 | | |
972 | | #ifndef FIPS_MODULE |
973 | | static int random_set_string(char **p, const char *s) |
974 | 560 | { |
975 | 560 | char *d = NULL; |
976 | | |
977 | 560 | if (s != NULL) { |
978 | 140 | d = OPENSSL_strdup(s); |
979 | 140 | if (d == NULL) |
980 | 0 | return 0; |
981 | 140 | } |
982 | 560 | OPENSSL_free(*p); |
983 | 560 | *p = d; |
984 | 560 | return 1; |
985 | 560 | } |
986 | | |
987 | | static int random_set_bool(int *p, const CONF_VALUE *cval) |
988 | 0 | { |
989 | 0 | if (!ossl_conf_parse_bool(cval->value, p)) { |
990 | 0 | ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR, |
991 | 0 | "name=%s, value=%s", cval->name, cval->value); |
992 | 0 | return 0; |
993 | 0 | } |
994 | 0 | return 1; |
995 | 0 | } |
996 | | |
997 | | /* |
998 | | * Load the DRBG definitions from a configuration file. |
999 | | */ |
1000 | | static int random_conf_init(CONF_IMODULE *md, const CONF *cnf) |
1001 | 0 | { |
1002 | 0 | STACK_OF(CONF_VALUE) *elist; |
1003 | 0 | CONF_VALUE *cval; |
1004 | 0 | OSSL_LIB_CTX *libctx = NCONF_get0_libctx((CONF *)cnf); |
1005 | 0 | RAND_GLOBAL *dgbl = rand_get_global(libctx); |
1006 | 0 | int i, r = 1; |
1007 | |
|
1008 | 0 | OSSL_TRACE1(CONF, "Loading random module: section %s\n", |
1009 | 0 | CONF_imodule_get_value(md)); |
1010 | | |
1011 | | /* Value is a section containing RANDOM configuration */ |
1012 | 0 | elist = NCONF_get_section(cnf, CONF_imodule_get_value(md)); |
1013 | 0 | if (elist == NULL) { |
1014 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR); |
1015 | 0 | return 0; |
1016 | 0 | } |
1017 | | |
1018 | 0 | if (dgbl == NULL) |
1019 | 0 | return 0; |
1020 | | |
1021 | 0 | for (i = 0; i < sk_CONF_VALUE_num(elist); i++) { |
1022 | 0 | cval = sk_CONF_VALUE_value(elist, i); |
1023 | 0 | if (OPENSSL_strcasecmp(cval->name, "random") == 0) { |
1024 | 0 | if (!random_set_string(&dgbl->rng_name, cval->value)) |
1025 | 0 | return 0; |
1026 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) { |
1027 | 0 | if (!random_set_string(&dgbl->rng_cipher, cval->value)) |
1028 | 0 | return 0; |
1029 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) { |
1030 | 0 | if (!random_set_string(&dgbl->rng_digest, cval->value)) |
1031 | 0 | return 0; |
1032 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) { |
1033 | 0 | if (!random_set_string(&dgbl->rng_propq, cval->value)) |
1034 | 0 | return 0; |
1035 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) { |
1036 | 0 | if (!random_set_string(&dgbl->seed_name, cval->value)) |
1037 | 0 | return 0; |
1038 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) { |
1039 | 0 | if (!random_set_string(&dgbl->seed_propq, cval->value)) |
1040 | 0 | return 0; |
1041 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "seed_strict") == 0) { |
1042 | 0 | if (!random_set_bool(&dgbl->seed_strict, cval)) |
1043 | 0 | return 0; |
1044 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "random_provider") == 0) { |
1045 | 0 | #ifndef FIPS_MODULE |
1046 | 0 | OSSL_PROVIDER *prov = ossl_provider_find(libctx, cval->value, 0); |
1047 | |
|
1048 | 0 | if (prov != NULL) { |
1049 | 0 | if (!RAND_set1_random_provider(libctx, prov)) { |
1050 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1051 | 0 | OSSL_PROVIDER_unload(prov); |
1052 | 0 | return 0; |
1053 | 0 | } |
1054 | | /* |
1055 | | * We need to release the reference from ossl_provider_find because |
1056 | | * we don't want to keep a reference counted handle to the provider. |
1057 | | * |
1058 | | * The provider unload code checks for the random provider and, |
1059 | | * if present, our reference will be NULLed when it is fully freed. |
1060 | | * The provider load code, conversely, checks the provider name |
1061 | | * and re-hooks our reference if required. This means that a load, |
1062 | | * hook random provider, use, unload, reload, reuse sequence will |
1063 | | * work as expected. |
1064 | | */ |
1065 | 0 | OSSL_PROVIDER_unload(prov); |
1066 | 0 | } else if (!set_random_provider_name(dgbl, cval->value)) |
1067 | 0 | return 0; |
1068 | 0 | #endif |
1069 | 0 | } else { |
1070 | 0 | ERR_raise_data(ERR_LIB_CRYPTO, |
1071 | 0 | CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION, |
1072 | 0 | "name=%s, value=%s", cval->name, cval->value); |
1073 | 0 | r = 0; |
1074 | 0 | } |
1075 | 0 | } |
1076 | 0 | return r; |
1077 | 0 | } |
1078 | | |
1079 | | static void random_conf_deinit(CONF_IMODULE *md) |
1080 | 0 | { |
1081 | 0 | OSSL_TRACE(CONF, "Cleaned up random\n"); |
1082 | 0 | } |
1083 | | |
1084 | | void ossl_random_add_conf_module(void) |
1085 | 0 | { |
1086 | 0 | OSSL_TRACE(CONF, "Adding config module 'random'\n"); |
1087 | 0 | CONF_module_add("random", random_conf_init, random_conf_deinit); |
1088 | 0 | } |
1089 | | |
1090 | | int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq, |
1091 | | const char *cipher, const char *digest) |
1092 | 140 | { |
1093 | 140 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
1094 | | |
1095 | 140 | if (dgbl == NULL) |
1096 | 0 | return 0; |
1097 | 140 | if (dgbl->primary != NULL) { |
1098 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED); |
1099 | 0 | return 0; |
1100 | 0 | } |
1101 | 140 | return random_set_string(&dgbl->rng_name, drbg) |
1102 | 140 | && random_set_string(&dgbl->rng_propq, propq) |
1103 | 140 | && random_set_string(&dgbl->rng_cipher, cipher) |
1104 | 140 | && random_set_string(&dgbl->rng_digest, digest); |
1105 | 140 | } |
1106 | | |
1107 | | int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed, |
1108 | | const char *propq) |
1109 | 0 | { |
1110 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
1111 | |
|
1112 | 0 | if (dgbl == NULL) |
1113 | 0 | return 0; |
1114 | 0 | if (dgbl->seed != NULL) { |
1115 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED); |
1116 | 0 | return 0; |
1117 | 0 | } |
1118 | 0 | return random_set_string(&dgbl->seed_name, seed) |
1119 | 0 | && random_set_string(&dgbl->seed_propq, propq); |
1120 | 0 | } |
1121 | | |
1122 | | int RAND_set1_random_provider(OSSL_LIB_CTX *ctx, OSSL_PROVIDER *prov) |
1123 | 0 | { |
1124 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
1125 | |
|
1126 | 0 | if (dgbl == NULL) |
1127 | 0 | return 0; |
1128 | | |
1129 | 0 | if (prov == NULL) { |
1130 | 0 | OPENSSL_free(dgbl->random_provider_name); |
1131 | 0 | dgbl->random_provider_name = NULL; |
1132 | 0 | dgbl->random_provider = NULL; |
1133 | 0 | return 1; |
1134 | 0 | } |
1135 | | |
1136 | 0 | if (dgbl->random_provider == prov) |
1137 | 0 | return 1; |
1138 | | |
1139 | 0 | if (!set_random_provider_name(dgbl, OSSL_PROVIDER_get0_name(prov))) |
1140 | 0 | return 0; |
1141 | | |
1142 | 0 | dgbl->random_provider = prov; |
1143 | 0 | return 1; |
1144 | 0 | } |
1145 | | |
1146 | | /* |
1147 | | * When a new provider is loaded, we need to check to see if it is the |
1148 | | * designated randomness provider and register it if it is. |
1149 | | */ |
1150 | | int ossl_rand_check_random_provider_on_load(OSSL_LIB_CTX *ctx, |
1151 | | OSSL_PROVIDER *prov) |
1152 | 0 | { |
1153 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
1154 | |
|
1155 | 0 | if (dgbl == NULL) |
1156 | 0 | return 0; |
1157 | | |
1158 | | /* No random provider name specified, or one is installed already */ |
1159 | 0 | if (dgbl->random_provider_name == NULL || dgbl->random_provider != NULL) |
1160 | 0 | return 1; |
1161 | | |
1162 | | /* Does this provider match the name we're using? */ |
1163 | 0 | if (strcmp(dgbl->random_provider_name, OSSL_PROVIDER_get0_name(prov)) != 0) |
1164 | 0 | return 1; |
1165 | | |
1166 | 0 | dgbl->random_provider = prov; |
1167 | 0 | return 1; |
1168 | 0 | } |
1169 | | |
1170 | | /* |
1171 | | * When a provider is being unloaded, if it is the randomness provider, |
1172 | | * we need to deregister it. |
1173 | | */ |
1174 | | int ossl_rand_check_random_provider_on_unload(OSSL_LIB_CTX *ctx, |
1175 | | OSSL_PROVIDER *prov) |
1176 | 0 | { |
1177 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
1178 | |
|
1179 | 0 | if (dgbl == NULL) |
1180 | 0 | return 0; |
1181 | | |
1182 | 0 | if (dgbl->random_provider == prov) |
1183 | 0 | dgbl->random_provider = NULL; |
1184 | 0 | return 1; |
1185 | 0 | } |
1186 | | |
1187 | | #endif /* !FIPS_MODULE */ |