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