Coverage Report

Created: 2025-06-13 06:58

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