VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/crypto/rand/rand_lib.c@ 107935

Last change on this file since 107935 was 104078, checked in by vboxsync, 11 months ago

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

File size: 26.4 KB
Line 
1/*
2 * Copyright 1995-2023 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 engine 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 "internal/cryptlib.h"
17#include "internal/thread_once.h"
18#include "crypto/rand.h"
19#include "crypto/cryptlib.h"
20#include "rand_local.h"
21#include "crypto/context.h"
22
23#ifndef FIPS_MODULE
24# include <stdio.h>
25# include <time.h>
26# include <limits.h>
27# include <openssl/conf.h>
28# include <openssl/trace.h>
29# include <openssl/engine.h>
30# include "crypto/rand_pool.h"
31# include "prov/seeding.h"
32# include "internal/e_os.h"
33# include "internal/property.h"
34
35# ifndef OPENSSL_NO_ENGINE
36/* non-NULL if default_RAND_meth is ENGINE-provided */
37static ENGINE *funct_ref;
38static CRYPTO_RWLOCK *rand_engine_lock;
39# endif
40# ifndef OPENSSL_NO_DEPRECATED_3_0
41static CRYPTO_RWLOCK *rand_meth_lock;
42static const RAND_METHOD *default_RAND_meth;
43# endif
44static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
45
46static int rand_inited = 0;
47
48DEFINE_RUN_ONCE_STATIC(do_rand_init)
49{
50# ifndef OPENSSL_NO_ENGINE
51 rand_engine_lock = CRYPTO_THREAD_lock_new();
52 if (rand_engine_lock == NULL)
53 return 0;
54# endif
55
56# ifndef OPENSSL_NO_DEPRECATED_3_0
57 rand_meth_lock = CRYPTO_THREAD_lock_new();
58 if (rand_meth_lock == NULL)
59 goto err;
60# endif
61
62 if (!ossl_rand_pool_init())
63 goto err;
64
65 rand_inited = 1;
66 return 1;
67
68 err:
69# ifndef OPENSSL_NO_DEPRECATED_3_0
70 CRYPTO_THREAD_lock_free(rand_meth_lock);
71 rand_meth_lock = NULL;
72# endif
73# ifndef OPENSSL_NO_ENGINE
74 CRYPTO_THREAD_lock_free(rand_engine_lock);
75 rand_engine_lock = NULL;
76# endif
77 return 0;
78}
79
80void ossl_rand_cleanup_int(void)
81{
82# ifndef OPENSSL_NO_DEPRECATED_3_0
83 const RAND_METHOD *meth = default_RAND_meth;
84
85 if (!rand_inited)
86 return;
87
88 if (meth != NULL && meth->cleanup != NULL)
89 meth->cleanup();
90 RAND_set_rand_method(NULL);
91# endif
92 ossl_rand_pool_cleanup();
93# ifndef OPENSSL_NO_ENGINE
94 CRYPTO_THREAD_lock_free(rand_engine_lock);
95 rand_engine_lock = NULL;
96# endif
97# ifndef OPENSSL_NO_DEPRECATED_3_0
98 CRYPTO_THREAD_lock_free(rand_meth_lock);
99 rand_meth_lock = NULL;
100# endif
101 ossl_release_default_drbg_ctx();
102 rand_inited = 0;
103}
104
105/*
106 * RAND_close_seed_files() ensures that any seed file descriptors are
107 * closed after use. This only applies to libcrypto/default provider,
108 * it does not apply to other providers.
109 */
110void RAND_keep_random_devices_open(int keep)
111{
112 if (RUN_ONCE(&rand_init, do_rand_init))
113 ossl_rand_pool_keep_random_devices_open(keep);
114}
115
116/*
117 * RAND_poll() reseeds the default RNG using random input
118 *
119 * The random input is obtained from polling various entropy
120 * sources which depend on the operating system and are
121 * configurable via the --with-rand-seed configure option.
122 */
123int RAND_poll(void)
124{
125 static const char salt[] = "polling";
126
127# ifndef OPENSSL_NO_DEPRECATED_3_0
128 const RAND_METHOD *meth = RAND_get_rand_method();
129 int ret = meth == RAND_OpenSSL();
130
131 if (meth == NULL)
132 return 0;
133
134 if (!ret) {
135 /* fill random pool and seed the current legacy RNG */
136 RAND_POOL *pool = ossl_rand_pool_new(RAND_DRBG_STRENGTH, 1,
137 (RAND_DRBG_STRENGTH + 7) / 8,
138 RAND_POOL_MAX_LENGTH);
139
140 if (pool == NULL)
141 return 0;
142
143 if (ossl_pool_acquire_entropy(pool) == 0)
144 goto err;
145
146 if (meth->add == NULL
147 || meth->add(ossl_rand_pool_buffer(pool),
148 ossl_rand_pool_length(pool),
149 (ossl_rand_pool_entropy(pool) / 8.0)) == 0)
150 goto err;
151
152 ret = 1;
153 err:
154 ossl_rand_pool_free(pool);
155 return ret;
156 }
157# endif
158
159 RAND_seed(salt, sizeof(salt));
160 return 1;
161}
162
163# ifndef OPENSSL_NO_DEPRECATED_3_0
164static int rand_set_rand_method_internal(const RAND_METHOD *meth,
165 ossl_unused ENGINE *e)
166{
167 if (!RUN_ONCE(&rand_init, do_rand_init))
168 return 0;
169
170 if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
171 return 0;
172# ifndef OPENSSL_NO_ENGINE
173 ENGINE_finish(funct_ref);
174 funct_ref = e;
175# endif
176 default_RAND_meth = meth;
177 CRYPTO_THREAD_unlock(rand_meth_lock);
178 return 1;
179}
180
181int RAND_set_rand_method(const RAND_METHOD *meth)
182{
183 return rand_set_rand_method_internal(meth, NULL);
184}
185
186const RAND_METHOD *RAND_get_rand_method(void)
187{
188 const RAND_METHOD *tmp_meth = NULL;
189
190 if (!RUN_ONCE(&rand_init, do_rand_init))
191 return NULL;
192
193 if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
194 return NULL;
195 if (default_RAND_meth == NULL) {
196# ifndef OPENSSL_NO_ENGINE
197 ENGINE *e;
198
199 /* If we have an engine that can do RAND, use it. */
200 if ((e = ENGINE_get_default_RAND()) != NULL
201 && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
202 funct_ref = e;
203 default_RAND_meth = tmp_meth;
204 } else {
205 ENGINE_finish(e);
206 default_RAND_meth = &ossl_rand_meth;
207 }
208# else
209 default_RAND_meth = &ossl_rand_meth;
210# endif
211 }
212 tmp_meth = default_RAND_meth;
213 CRYPTO_THREAD_unlock(rand_meth_lock);
214 return tmp_meth;
215}
216
217# if !defined(OPENSSL_NO_ENGINE)
218int RAND_set_rand_engine(ENGINE *engine)
219{
220 const RAND_METHOD *tmp_meth = NULL;
221
222 if (!RUN_ONCE(&rand_init, do_rand_init))
223 return 0;
224
225 if (engine != NULL) {
226 if (!ENGINE_init(engine))
227 return 0;
228 tmp_meth = ENGINE_get_RAND(engine);
229 if (tmp_meth == NULL) {
230 ENGINE_finish(engine);
231 return 0;
232 }
233 }
234 if (!CRYPTO_THREAD_write_lock(rand_engine_lock)) {
235 ENGINE_finish(engine);
236 return 0;
237 }
238
239 /* This function releases any prior ENGINE so call it first */
240 rand_set_rand_method_internal(tmp_meth, engine);
241 CRYPTO_THREAD_unlock(rand_engine_lock);
242 return 1;
243}
244# endif
245# endif /* OPENSSL_NO_DEPRECATED_3_0 */
246
247void RAND_seed(const void *buf, int num)
248{
249 EVP_RAND_CTX *drbg;
250# ifndef OPENSSL_NO_DEPRECATED_3_0
251 const RAND_METHOD *meth = RAND_get_rand_method();
252
253 if (meth != NULL && meth->seed != NULL) {
254 meth->seed(buf, num);
255 return;
256 }
257# endif
258
259 drbg = RAND_get0_primary(NULL);
260 if (drbg != NULL && num > 0)
261 EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
262}
263
264void RAND_add(const void *buf, int num, double randomness)
265{
266 EVP_RAND_CTX *drbg;
267# ifndef OPENSSL_NO_DEPRECATED_3_0
268 const RAND_METHOD *meth = RAND_get_rand_method();
269
270 if (meth != NULL && meth->add != NULL) {
271 meth->add(buf, num, randomness);
272 return;
273 }
274# endif
275 drbg = RAND_get0_primary(NULL);
276 if (drbg != NULL && num > 0)
277# ifdef OPENSSL_RAND_SEED_NONE
278 /* Without an entropy source, we have to rely on the user */
279 EVP_RAND_reseed(drbg, 0, buf, num, NULL, 0);
280# else
281 /* With an entropy source, we downgrade this to additional input */
282 EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
283# endif
284}
285
286# if !defined(OPENSSL_NO_DEPRECATED_1_1_0)
287int RAND_pseudo_bytes(unsigned char *buf, int num)
288{
289 const RAND_METHOD *meth = RAND_get_rand_method();
290
291 if (meth != NULL && meth->pseudorand != NULL)
292 return meth->pseudorand(buf, num);
293 ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
294 return -1;
295}
296# endif
297
298int RAND_status(void)
299{
300 EVP_RAND_CTX *rand;
301# ifndef OPENSSL_NO_DEPRECATED_3_0
302 const RAND_METHOD *meth = RAND_get_rand_method();
303
304 if (meth != NULL && meth != RAND_OpenSSL())
305 return meth->status != NULL ? meth->status() : 0;
306# endif
307
308 if ((rand = RAND_get0_primary(NULL)) == NULL)
309 return 0;
310 return EVP_RAND_get_state(rand) == EVP_RAND_STATE_READY;
311}
312# else /* !FIPS_MODULE */
313
314# ifndef OPENSSL_NO_DEPRECATED_3_0
315const RAND_METHOD *RAND_get_rand_method(void)
316{
317 return NULL;
318}
319# endif
320#endif /* !FIPS_MODULE */
321
322/*
323 * This function is not part of RAND_METHOD, so if we're not using
324 * the default method, then just call RAND_bytes(). Otherwise make
325 * sure we're instantiated and use the private DRBG.
326 */
327int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
328 unsigned int strength)
329{
330 EVP_RAND_CTX *rand;
331#if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
332 const RAND_METHOD *meth = RAND_get_rand_method();
333
334 if (meth != NULL && meth != RAND_OpenSSL()) {
335 if (meth->bytes != NULL)
336 return meth->bytes(buf, num);
337 ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
338 return -1;
339 }
340#endif
341
342 rand = RAND_get0_private(ctx);
343 if (rand != NULL)
344 return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
345
346 return 0;
347}
348
349int RAND_priv_bytes(unsigned char *buf, int num)
350{
351 if (num < 0)
352 return 0;
353 return RAND_priv_bytes_ex(NULL, buf, (size_t)num, 0);
354}
355
356int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
357 unsigned int strength)
358{
359 EVP_RAND_CTX *rand;
360#if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
361 const RAND_METHOD *meth = RAND_get_rand_method();
362
363 if (meth != NULL && meth != RAND_OpenSSL()) {
364 if (meth->bytes != NULL)
365 return meth->bytes(buf, num);
366 ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
367 return -1;
368 }
369#endif
370
371 rand = RAND_get0_public(ctx);
372 if (rand != NULL)
373 return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
374
375 return 0;
376}
377
378int RAND_bytes(unsigned char *buf, int num)
379{
380 if (num < 0)
381 return 0;
382 return RAND_bytes_ex(NULL, buf, (size_t)num, 0);
383}
384
385typedef struct rand_global_st {
386 /*
387 * The three shared DRBG instances
388 *
389 * There are three shared DRBG instances: <primary>, <public>, and
390 * <private>. The <public> and <private> DRBGs are secondary ones.
391 * These are used for non-secret (e.g. nonces) and secret
392 * (e.g. private keys) data respectively.
393 */
394 CRYPTO_RWLOCK *lock;
395
396 EVP_RAND_CTX *seed;
397
398 /*
399 * The <primary> DRBG
400 *
401 * Not used directly by the application, only for reseeding the two other
402 * DRBGs. It reseeds itself by pulling either randomness from os entropy
403 * sources or by consuming randomness which was added by RAND_add().
404 *
405 * The <primary> DRBG is a global instance which is accessed concurrently by
406 * all threads. The necessary locking is managed automatically by its child
407 * DRBG instances during reseeding.
408 */
409 EVP_RAND_CTX *primary;
410
411 /*
412 * The <public> DRBG
413 *
414 * Used by default for generating random bytes using RAND_bytes().
415 *
416 * The <public> secondary DRBG is thread-local, i.e., there is one instance
417 * per thread.
418 */
419 CRYPTO_THREAD_LOCAL public;
420
421 /*
422 * The <private> DRBG
423 *
424 * Used by default for generating private keys using RAND_priv_bytes()
425 *
426 * The <private> secondary DRBG is thread-local, i.e., there is one
427 * instance per thread.
428 */
429 CRYPTO_THREAD_LOCAL private;
430
431 /* Which RNG is being used by default and it's configuration settings */
432 char *rng_name;
433 char *rng_cipher;
434 char *rng_digest;
435 char *rng_propq;
436
437 /* Allow the randomness source to be changed */
438 char *seed_name;
439 char *seed_propq;
440} RAND_GLOBAL;
441
442/*
443 * Initialize the OSSL_LIB_CTX global DRBGs on first use.
444 * Returns the allocated global data on success or NULL on failure.
445 */
446void *ossl_rand_ctx_new(OSSL_LIB_CTX *libctx)
447{
448 RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));
449
450 if (dgbl == NULL)
451 return NULL;
452
453#ifndef FIPS_MODULE
454 /*
455 * We need to ensure that base libcrypto thread handling has been
456 * initialised.
457 */
458 OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL);
459#endif
460
461 dgbl->lock = CRYPTO_THREAD_lock_new();
462 if (dgbl->lock == NULL)
463 goto err1;
464
465 if (!CRYPTO_THREAD_init_local(&dgbl->private, NULL))
466 goto err1;
467
468 if (!CRYPTO_THREAD_init_local(&dgbl->public, NULL))
469 goto err2;
470
471 return dgbl;
472
473 err2:
474 CRYPTO_THREAD_cleanup_local(&dgbl->private);
475 err1:
476 CRYPTO_THREAD_lock_free(dgbl->lock);
477 OPENSSL_free(dgbl);
478 return NULL;
479}
480
481void ossl_rand_ctx_free(void *vdgbl)
482{
483 RAND_GLOBAL *dgbl = vdgbl;
484
485 if (dgbl == NULL)
486 return;
487
488 CRYPTO_THREAD_lock_free(dgbl->lock);
489 CRYPTO_THREAD_cleanup_local(&dgbl->private);
490 CRYPTO_THREAD_cleanup_local(&dgbl->public);
491 EVP_RAND_CTX_free(dgbl->primary);
492 EVP_RAND_CTX_free(dgbl->seed);
493 OPENSSL_free(dgbl->rng_name);
494 OPENSSL_free(dgbl->rng_cipher);
495 OPENSSL_free(dgbl->rng_digest);
496 OPENSSL_free(dgbl->rng_propq);
497 OPENSSL_free(dgbl->seed_name);
498 OPENSSL_free(dgbl->seed_propq);
499
500 OPENSSL_free(dgbl);
501}
502
503static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx)
504{
505 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX);
506}
507
508static void rand_delete_thread_state(void *arg)
509{
510 OSSL_LIB_CTX *ctx = arg;
511 RAND_GLOBAL *dgbl = rand_get_global(ctx);
512 EVP_RAND_CTX *rand;
513
514 if (dgbl == NULL)
515 return;
516
517 rand = CRYPTO_THREAD_get_local(&dgbl->public);
518 CRYPTO_THREAD_set_local(&dgbl->public, NULL);
519 EVP_RAND_CTX_free(rand);
520
521 rand = CRYPTO_THREAD_get_local(&dgbl->private);
522 CRYPTO_THREAD_set_local(&dgbl->private, NULL);
523 EVP_RAND_CTX_free(rand);
524}
525
526#ifndef FIPS_MODULE
527static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx)
528{
529 EVP_RAND *rand;
530 RAND_GLOBAL *dgbl = rand_get_global(libctx);
531 EVP_RAND_CTX *ctx = NULL;
532 const char *propq;
533 char *name, *props = NULL;
534 size_t props_len;
535 OSSL_PROPERTY_LIST *pl1, *pl2, *pl3 = NULL;
536
537 if (dgbl == NULL)
538 return NULL;
539 propq = dgbl->seed_propq;
540 if (dgbl->seed_name != NULL) {
541 name = dgbl->seed_name;
542 } else {
543 /*
544 * Default to our internal seed source. This isn't part of the FIPS
545 * provider so we need to override any FIPS properties.
546 */
547 if (propq == NULL || *propq == '\0') {
548 propq = "-fips";
549 } else {
550 pl1 = ossl_parse_query(libctx, propq, 1);
551 if (pl1 == NULL) {
552 ERR_raise(ERR_LIB_RAND, RAND_R_INVALID_PROPERTY_QUERY);
553 return NULL;
554 }
555 pl2 = ossl_parse_query(libctx, "-fips", 1);
556 if (pl2 == NULL) {
557 ossl_property_free(pl1);
558 ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);
559 return NULL;
560 }
561 pl3 = ossl_property_merge(pl2, pl1);
562 ossl_property_free(pl1);
563 ossl_property_free(pl2);
564 if (pl3 == NULL) {
565 ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);
566 return NULL;
567 }
568 props_len = ossl_property_list_to_string(libctx, pl3, NULL, 0);
569 if (props_len == 0) {
570 /* Shouldn't happen since we added a query element */
571 ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);
572 goto err;
573 } else {
574 props = OPENSSL_malloc(props_len);
575 if (props == NULL) {
576 ERR_raise(ERR_LIB_RAND, ERR_R_MALLOC_FAILURE);
577 goto err;
578 }
579 if (ossl_property_list_to_string(libctx, pl3,
580 props, props_len) == 0) {
581 ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);
582 goto err;
583 }
584 ossl_property_free(pl3);
585 pl3 = NULL;
586 propq = props;
587 }
588 }
589 name = "SEED-SRC";
590 }
591
592 rand = EVP_RAND_fetch(libctx, name, propq);
593 if (rand == NULL) {
594 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
595 goto err;
596 }
597 ctx = EVP_RAND_CTX_new(rand, NULL);
598 EVP_RAND_free(rand);
599 if (ctx == NULL) {
600 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
601 goto err;
602 }
603 if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
604 ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
605 goto err;
606 }
607 OPENSSL_free(props);
608 return ctx;
609 err:
610 EVP_RAND_CTX_free(ctx);
611 ossl_property_free(pl3);
612 OPENSSL_free(props);
613 return NULL;
614}
615
616EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx)
617{
618 RAND_GLOBAL *dgbl = rand_get_global(ctx);
619 EVP_RAND_CTX *ret;
620
621 if (dgbl == NULL)
622 return NULL;
623
624 if (!CRYPTO_THREAD_read_lock(dgbl->lock))
625 return NULL;
626 ret = dgbl->seed;
627 CRYPTO_THREAD_unlock(dgbl->lock);
628 return ret;
629}
630#endif
631
632static EVP_RAND_CTX *rand_new_drbg(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent,
633 unsigned int reseed_interval,
634 time_t reseed_time_interval)
635{
636 EVP_RAND *rand;
637 RAND_GLOBAL *dgbl = rand_get_global(libctx);
638 EVP_RAND_CTX *ctx;
639 OSSL_PARAM params[7], *p = params;
640 char *name, *cipher;
641
642 if (dgbl == NULL)
643 return NULL;
644 name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG";
645 rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq);
646 if (rand == NULL) {
647 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
648 return NULL;
649 }
650 ctx = EVP_RAND_CTX_new(rand, parent);
651 EVP_RAND_free(rand);
652 if (ctx == NULL) {
653 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
654 return NULL;
655 }
656
657 /*
658 * Rather than trying to decode the DRBG settings, just pass them through
659 * and rely on the other end to ignore those it doesn't care about.
660 */
661 cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR";
662 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
663 cipher, 0);
664 if (dgbl->rng_digest != NULL)
665 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
666 dgbl->rng_digest, 0);
667 if (dgbl->rng_propq != NULL)
668 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES,
669 dgbl->rng_propq, 0);
670 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0);
671 *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS,
672 &reseed_interval);
673 *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
674 &reseed_time_interval);
675 *p = OSSL_PARAM_construct_end();
676 if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) {
677 ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
678 EVP_RAND_CTX_free(ctx);
679 return NULL;
680 }
681 return ctx;
682}
683
684/*
685 * Get the primary random generator.
686 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
687 *
688 */
689EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx)
690{
691 RAND_GLOBAL *dgbl = rand_get_global(ctx);
692 EVP_RAND_CTX *ret;
693
694 if (dgbl == NULL)
695 return NULL;
696
697 if (!CRYPTO_THREAD_read_lock(dgbl->lock))
698 return NULL;
699
700 ret = dgbl->primary;
701 CRYPTO_THREAD_unlock(dgbl->lock);
702
703 if (ret != NULL)
704 return ret;
705
706 if (!CRYPTO_THREAD_write_lock(dgbl->lock))
707 return NULL;
708
709 ret = dgbl->primary;
710 if (ret != NULL) {
711 CRYPTO_THREAD_unlock(dgbl->lock);
712 return ret;
713 }
714
715#ifndef FIPS_MODULE
716 if (dgbl->seed == NULL) {
717 ERR_set_mark();
718 dgbl->seed = rand_new_seed(ctx);
719 ERR_pop_to_mark();
720 }
721#endif
722
723 ret = dgbl->primary = rand_new_drbg(ctx, dgbl->seed,
724 PRIMARY_RESEED_INTERVAL,
725 PRIMARY_RESEED_TIME_INTERVAL);
726 /*
727 * The primary DRBG may be shared between multiple threads so we must
728 * enable locking.
729 */
730 if (ret != NULL && !EVP_RAND_enable_locking(ret)) {
731 ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING);
732 EVP_RAND_CTX_free(ret);
733 ret = dgbl->primary = NULL;
734 }
735 CRYPTO_THREAD_unlock(dgbl->lock);
736
737 return ret;
738}
739
740/*
741 * Get the public random generator.
742 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
743 */
744EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx)
745{
746 RAND_GLOBAL *dgbl = rand_get_global(ctx);
747 EVP_RAND_CTX *rand, *primary;
748
749 if (dgbl == NULL)
750 return NULL;
751
752 rand = CRYPTO_THREAD_get_local(&dgbl->public);
753 if (rand == NULL) {
754 primary = RAND_get0_primary(ctx);
755 if (primary == NULL)
756 return NULL;
757
758 ctx = ossl_lib_ctx_get_concrete(ctx);
759 /*
760 * If the private is also NULL then this is the first time we've
761 * used this thread.
762 */
763 if (CRYPTO_THREAD_get_local(&dgbl->private) == NULL
764 && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
765 return NULL;
766 rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
767 SECONDARY_RESEED_TIME_INTERVAL);
768 CRYPTO_THREAD_set_local(&dgbl->public, rand);
769 }
770 return rand;
771}
772
773/*
774 * Get the private random generator.
775 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
776 */
777EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx)
778{
779 RAND_GLOBAL *dgbl = rand_get_global(ctx);
780 EVP_RAND_CTX *rand, *primary;
781
782 if (dgbl == NULL)
783 return NULL;
784
785 rand = CRYPTO_THREAD_get_local(&dgbl->private);
786 if (rand == NULL) {
787 primary = RAND_get0_primary(ctx);
788 if (primary == NULL)
789 return NULL;
790
791 ctx = ossl_lib_ctx_get_concrete(ctx);
792 /*
793 * If the public is also NULL then this is the first time we've
794 * used this thread.
795 */
796 if (CRYPTO_THREAD_get_local(&dgbl->public) == NULL
797 && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
798 return NULL;
799 rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
800 SECONDARY_RESEED_TIME_INTERVAL);
801 CRYPTO_THREAD_set_local(&dgbl->private, rand);
802 }
803 return rand;
804}
805
806#ifdef FIPS_MODULE
807EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx)
808{
809 RAND_GLOBAL *dgbl = rand_get_global(ctx);
810
811 if (dgbl == NULL)
812 return NULL;
813
814 return CRYPTO_THREAD_get_local(&dgbl->private);
815}
816#endif
817
818int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
819{
820 RAND_GLOBAL *dgbl = rand_get_global(ctx);
821 EVP_RAND_CTX *old;
822 int r;
823
824 if (dgbl == NULL)
825 return 0;
826 old = CRYPTO_THREAD_get_local(&dgbl->public);
827 if ((r = CRYPTO_THREAD_set_local(&dgbl->public, rand)) > 0)
828 EVP_RAND_CTX_free(old);
829 return r;
830}
831
832int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
833{
834 RAND_GLOBAL *dgbl = rand_get_global(ctx);
835 EVP_RAND_CTX *old;
836 int r;
837
838 if (dgbl == NULL)
839 return 0;
840 old = CRYPTO_THREAD_get_local(&dgbl->private);
841 if ((r = CRYPTO_THREAD_set_local(&dgbl->private, rand)) > 0)
842 EVP_RAND_CTX_free(old);
843 return r;
844}
845
846#ifndef FIPS_MODULE
847static int random_set_string(char **p, const char *s)
848{
849 char *d = NULL;
850
851 if (s != NULL) {
852 d = OPENSSL_strdup(s);
853 if (d == NULL) {
854 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
855 return 0;
856 }
857 }
858 OPENSSL_free(*p);
859 *p = d;
860 return 1;
861}
862
863/*
864 * Load the DRBG definitions from a configuration file.
865 */
866static int random_conf_init(CONF_IMODULE *md, const CONF *cnf)
867{
868 STACK_OF(CONF_VALUE) *elist;
869 CONF_VALUE *cval;
870 RAND_GLOBAL *dgbl = rand_get_global(NCONF_get0_libctx((CONF *)cnf));
871 int i, r = 1;
872
873 OSSL_TRACE1(CONF, "Loading random module: section %s\n",
874 CONF_imodule_get_value(md));
875
876 /* Value is a section containing RANDOM configuration */
877 elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
878 if (elist == NULL) {
879 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR);
880 return 0;
881 }
882
883 if (dgbl == NULL)
884 return 0;
885
886 for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
887 cval = sk_CONF_VALUE_value(elist, i);
888 if (OPENSSL_strcasecmp(cval->name, "random") == 0) {
889 if (!random_set_string(&dgbl->rng_name, cval->value))
890 return 0;
891 } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) {
892 if (!random_set_string(&dgbl->rng_cipher, cval->value))
893 return 0;
894 } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) {
895 if (!random_set_string(&dgbl->rng_digest, cval->value))
896 return 0;
897 } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) {
898 if (!random_set_string(&dgbl->rng_propq, cval->value))
899 return 0;
900 } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) {
901 if (!random_set_string(&dgbl->seed_name, cval->value))
902 return 0;
903 } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) {
904 if (!random_set_string(&dgbl->seed_propq, cval->value))
905 return 0;
906 } else {
907 ERR_raise_data(ERR_LIB_CRYPTO,
908 CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION,
909 "name=%s, value=%s", cval->name, cval->value);
910 r = 0;
911 }
912 }
913 return r;
914}
915
916
917static void random_conf_deinit(CONF_IMODULE *md)
918{
919 OSSL_TRACE(CONF, "Cleaned up random\n");
920}
921
922void ossl_random_add_conf_module(void)
923{
924 OSSL_TRACE(CONF, "Adding config module 'random'\n");
925 CONF_module_add("random", random_conf_init, random_conf_deinit);
926}
927
928int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq,
929 const char *cipher, const char *digest)
930{
931 RAND_GLOBAL *dgbl = rand_get_global(ctx);
932
933 if (dgbl == NULL)
934 return 0;
935 if (dgbl->primary != NULL) {
936 ERR_raise(ERR_LIB_CRYPTO, RAND_R_ALREADY_INSTANTIATED);
937 return 0;
938 }
939 return random_set_string(&dgbl->rng_name, drbg)
940 && random_set_string(&dgbl->rng_propq, propq)
941 && random_set_string(&dgbl->rng_cipher, cipher)
942 && random_set_string(&dgbl->rng_digest, digest);
943}
944
945int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed,
946 const char *propq)
947{
948 RAND_GLOBAL *dgbl = rand_get_global(ctx);
949
950 if (dgbl == NULL)
951 return 0;
952 if (dgbl->seed != NULL) {
953 ERR_raise(ERR_LIB_CRYPTO, RAND_R_ALREADY_INSTANTIATED);
954 return 0;
955 }
956 return random_set_string(&dgbl->seed_name, seed)
957 && random_set_string(&dgbl->seed_propq, propq);
958}
959
960#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette