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