Coverage Report

Created: 2023-06-08 06:41

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