Coverage Report

Created: 2024-07-27 06:39

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