Coverage Report

Created: 2023-04-12 06:22

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