Coverage Report

Created: 2023-06-08 06:40

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