/src/openssl31/crypto/rand/rand_lib.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2025 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  | 36  | { | 
50  | 36  | # ifndef OPENSSL_NO_ENGINE  | 
51  | 36  |     rand_engine_lock = CRYPTO_THREAD_lock_new();  | 
52  | 36  |     if (rand_engine_lock == NULL)  | 
53  | 0  |         return 0;  | 
54  | 36  | # endif  | 
55  |  |  | 
56  | 36  | # ifndef OPENSSL_NO_DEPRECATED_3_0  | 
57  | 36  |     rand_meth_lock = CRYPTO_THREAD_lock_new();  | 
58  | 36  |     if (rand_meth_lock == NULL)  | 
59  | 0  |         goto err;  | 
60  | 36  | # endif  | 
61  |  |  | 
62  | 36  |     if (!ossl_rand_pool_init())  | 
63  | 0  |         goto err;  | 
64  |  |  | 
65  | 36  |     rand_inited = 1;  | 
66  | 36  |     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  | 36  | }  | 
79  |  |  | 
80  |  | void ossl_rand_cleanup_int(void)  | 
81  | 133  | { | 
82  | 133  | # ifndef OPENSSL_NO_DEPRECATED_3_0  | 
83  | 133  |     const RAND_METHOD *meth = default_RAND_meth;  | 
84  |  |  | 
85  | 133  |     if (!rand_inited)  | 
86  | 97  |         return;  | 
87  |  |  | 
88  | 36  |     if (meth != NULL && meth->cleanup != NULL)  | 
89  | 0  |         meth->cleanup();  | 
90  | 36  |     RAND_set_rand_method(NULL);  | 
91  | 36  | # endif  | 
92  | 36  |     ossl_rand_pool_cleanup();  | 
93  | 36  | # ifndef OPENSSL_NO_ENGINE  | 
94  | 36  |     CRYPTO_THREAD_lock_free(rand_engine_lock);  | 
95  | 36  |     rand_engine_lock = NULL;  | 
96  | 36  | # endif  | 
97  | 36  | # ifndef OPENSSL_NO_DEPRECATED_3_0  | 
98  | 36  |     CRYPTO_THREAD_lock_free(rand_meth_lock);  | 
99  | 36  |     rand_meth_lock = NULL;  | 
100  | 36  | # endif  | 
101  | 36  |     ossl_release_default_drbg_ctx();  | 
102  | 36  |     rand_inited = 0;  | 
103  | 36  | }  | 
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  | 36  | { | 
167  | 36  |     if (!RUN_ONCE(&rand_init, do_rand_init))  | 
168  | 0  |         return 0;  | 
169  |  |  | 
170  | 36  |     if (!CRYPTO_THREAD_write_lock(rand_meth_lock))  | 
171  | 0  |         return 0;  | 
172  | 36  | #  ifndef OPENSSL_NO_ENGINE  | 
173  | 36  |     ENGINE_finish(funct_ref);  | 
174  | 36  |     funct_ref = e;  | 
175  | 36  | #  endif  | 
176  | 36  |     default_RAND_meth = meth;  | 
177  | 36  |     CRYPTO_THREAD_unlock(rand_meth_lock);  | 
178  | 36  |     return 1;  | 
179  | 36  | }  | 
180  |  |  | 
181  |  | int RAND_set_rand_method(const RAND_METHOD *meth)  | 
182  | 36  | { | 
183  | 36  |     return rand_set_rand_method_internal(meth, NULL);  | 
184  | 36  | }  | 
185  |  |  | 
186  |  | const RAND_METHOD *RAND_get_rand_method(void)  | 
187  | 390k  | { | 
188  | 390k  |     const RAND_METHOD *tmp_meth = NULL;  | 
189  |  |  | 
190  | 390k  |     if (!RUN_ONCE(&rand_init, do_rand_init))  | 
191  | 0  |         return NULL;  | 
192  |  |  | 
193  | 390k  |     if (!CRYPTO_THREAD_write_lock(rand_meth_lock))  | 
194  | 0  |         return NULL;  | 
195  | 390k  |     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  | 390k  |     tmp_meth = default_RAND_meth;  | 
213  | 390k  |     CRYPTO_THREAD_unlock(rand_meth_lock);  | 
214  | 390k  |     return tmp_meth;  | 
215  | 390k  | }  | 
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  | 512k  | { | 
330  | 512k  |     EVP_RAND_CTX *rand;  | 
331  | 512k  | #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)  | 
332  | 512k  |     const RAND_METHOD *meth = RAND_get_rand_method();  | 
333  |  |  | 
334  | 512k  |     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  | 512k  | #endif  | 
341  |  |  | 
342  | 512k  |     rand = RAND_get0_private(ctx);  | 
343  | 512k  |     if (rand != NULL)  | 
344  | 512k  |         return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);  | 
345  |  |  | 
346  | 0  |     return 0;  | 
347  | 512k  | }  | 
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  | 187k  | { | 
359  | 187k  |     EVP_RAND_CTX *rand;  | 
360  | 187k  | #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)  | 
361  | 187k  |     const RAND_METHOD *meth = RAND_get_rand_method();  | 
362  |  |  | 
363  | 187k  |     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  | 187k  | #endif  | 
370  |  |  | 
371  | 187k  |     rand = RAND_get0_public(ctx);  | 
372  | 187k  |     if (rand != NULL)  | 
373  | 187k  |         return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);  | 
374  |  |  | 
375  | 0  |     return 0;  | 
376  | 187k  | }  | 
377  |  |  | 
378  |  | int RAND_bytes(unsigned char *buf, int num)  | 
379  | 231  | { | 
380  | 231  |     if (num < 0)  | 
381  | 0  |         return 0;  | 
382  | 231  |     return RAND_bytes_ex(NULL, buf, (size_t)num, 0);  | 
383  | 231  | }  | 
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  | 70  | { | 
448  | 70  |     RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));  | 
449  |  |  | 
450  | 70  |     if (dgbl == NULL)  | 
451  | 0  |         return NULL;  | 
452  |  |  | 
453  | 70  | #ifndef FIPS_MODULE  | 
454  |  |     /*  | 
455  |  |      * We need to ensure that base libcrypto thread handling has been  | 
456  |  |      * initialised.  | 
457  |  |      */  | 
458  | 70  |      OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL);  | 
459  | 70  | #endif  | 
460  |  |  | 
461  | 70  |     dgbl->lock = CRYPTO_THREAD_lock_new();  | 
462  | 70  |     if (dgbl->lock == NULL)  | 
463  | 0  |         goto err1;  | 
464  |  |  | 
465  | 70  |     if (!CRYPTO_THREAD_init_local(&dgbl->private, NULL))  | 
466  | 0  |         goto err1;  | 
467  |  |  | 
468  | 70  |     if (!CRYPTO_THREAD_init_local(&dgbl->public, NULL))  | 
469  | 0  |         goto err2;  | 
470  |  |  | 
471  | 70  |     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  | 116  | { | 
483  | 116  |     RAND_GLOBAL *dgbl = vdgbl;  | 
484  |  |  | 
485  | 116  |     if (dgbl == NULL)  | 
486  | 6  |         return;  | 
487  |  |  | 
488  | 110  |     CRYPTO_THREAD_lock_free(dgbl->lock);  | 
489  | 110  |     CRYPTO_THREAD_cleanup_local(&dgbl->private);  | 
490  | 110  |     CRYPTO_THREAD_cleanup_local(&dgbl->public);  | 
491  | 110  |     EVP_RAND_CTX_free(dgbl->primary);  | 
492  | 110  |     EVP_RAND_CTX_free(dgbl->seed);  | 
493  | 110  |     OPENSSL_free(dgbl->rng_name);  | 
494  | 110  |     OPENSSL_free(dgbl->rng_cipher);  | 
495  | 110  |     OPENSSL_free(dgbl->rng_digest);  | 
496  | 110  |     OPENSSL_free(dgbl->rng_propq);  | 
497  | 110  |     OPENSSL_free(dgbl->seed_name);  | 
498  | 110  |     OPENSSL_free(dgbl->seed_propq);  | 
499  |  |  | 
500  | 110  |     OPENSSL_free(dgbl);  | 
501  | 110  | }  | 
502  |  |  | 
503  |  | static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx)  | 
504  | 1.19M  | { | 
505  | 1.19M  |     return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX);  | 
506  | 1.19M  | }  | 
507  |  |  | 
508  |  | static void rand_delete_thread_state(void *arg)  | 
509  | 36  | { | 
510  | 36  |     OSSL_LIB_CTX *ctx = arg;  | 
511  | 36  |     RAND_GLOBAL *dgbl = rand_get_global(ctx);  | 
512  | 36  |     EVP_RAND_CTX *rand;  | 
513  |  |  | 
514  | 36  |     if (dgbl == NULL)  | 
515  | 0  |         return;  | 
516  |  |  | 
517  | 36  |     rand = CRYPTO_THREAD_get_local(&dgbl->public);  | 
518  | 36  |     CRYPTO_THREAD_set_local(&dgbl->public, NULL);  | 
519  | 36  |     EVP_RAND_CTX_free(rand);  | 
520  |  |  | 
521  | 36  |     rand = CRYPTO_THREAD_get_local(&dgbl->private);  | 
522  | 36  |     CRYPTO_THREAD_set_local(&dgbl->private, NULL);  | 
523  | 36  |     EVP_RAND_CTX_free(rand);  | 
524  | 36  | }  | 
525  |  |  | 
526  |  | #ifndef FIPS_MODULE  | 
527  |  | static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx)  | 
528  | 14  | { | 
529  | 14  |     EVP_RAND *rand;  | 
530  | 14  |     RAND_GLOBAL *dgbl = rand_get_global(libctx);  | 
531  | 14  |     EVP_RAND_CTX *ctx = NULL;  | 
532  | 14  |     const char *propq;  | 
533  | 14  |     char *name, *props = NULL;  | 
534  | 14  |     size_t props_len;  | 
535  | 14  |     OSSL_PROPERTY_LIST *pl1, *pl2, *pl3 = NULL;  | 
536  |  |  | 
537  | 14  |     if (dgbl == NULL)  | 
538  | 0  |         return NULL;  | 
539  | 14  |     propq = dgbl->seed_propq;  | 
540  | 14  |     if (dgbl->seed_name != NULL) { | 
541  | 0  |         name = dgbl->seed_name;  | 
542  | 14  |     } 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  | 14  |         if (propq == NULL || *propq == '\0') { | 
548  | 14  |             propq = "-fips";  | 
549  | 14  |         } 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  | 14  |         name = "SEED-SRC";  | 
590  | 14  |     }  | 
591  |  |  | 
592  | 14  |     rand = EVP_RAND_fetch(libctx, name, propq);  | 
593  | 14  |     if (rand == NULL) { | 
594  | 0  |         ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);  | 
595  | 0  |         goto err;  | 
596  | 0  |     }  | 
597  | 14  |     ctx = EVP_RAND_CTX_new(rand, NULL);  | 
598  | 14  |     EVP_RAND_free(rand);  | 
599  | 14  |     if (ctx == NULL) { | 
600  | 0  |         ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);  | 
601  | 0  |         goto err;  | 
602  | 0  |     }  | 
603  | 14  |     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  | 14  |     OPENSSL_free(props);  | 
608  | 14  |     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  | 14  | }  | 
615  |  |  | 
616  |  | EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx)  | 
617  | 516  | { | 
618  | 516  |     RAND_GLOBAL *dgbl = rand_get_global(ctx);  | 
619  | 516  |     EVP_RAND_CTX *ret;  | 
620  |  |  | 
621  | 516  |     if (dgbl == NULL)  | 
622  | 0  |         return NULL;  | 
623  |  |  | 
624  | 516  |     if (!CRYPTO_THREAD_read_lock(dgbl->lock))  | 
625  | 0  |         return NULL;  | 
626  | 516  |     ret = dgbl->seed;  | 
627  | 516  |     CRYPTO_THREAD_unlock(dgbl->lock);  | 
628  | 516  |     return ret;  | 
629  | 516  | }  | 
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  | 30  | { | 
691  | 30  |     RAND_GLOBAL *dgbl = rand_get_global(ctx);  | 
692  | 30  |     EVP_RAND_CTX *ret;  | 
693  |  |  | 
694  | 30  |     if (dgbl == NULL)  | 
695  | 0  |         return NULL;  | 
696  |  |  | 
697  | 30  |     if (!CRYPTO_THREAD_read_lock(dgbl->lock))  | 
698  | 0  |         return NULL;  | 
699  |  |  | 
700  | 30  |     ret = dgbl->primary;  | 
701  | 30  |     CRYPTO_THREAD_unlock(dgbl->lock);  | 
702  |  |  | 
703  | 30  |     if (ret != NULL)  | 
704  | 10  |         return ret;  | 
705  |  |  | 
706  | 20  |     if (!CRYPTO_THREAD_write_lock(dgbl->lock))  | 
707  | 0  |         return NULL;  | 
708  |  |  | 
709  | 20  |     ret = dgbl->primary;  | 
710  | 20  |     if (ret != NULL) { | 
711  | 0  |         CRYPTO_THREAD_unlock(dgbl->lock);  | 
712  | 0  |         return ret;  | 
713  | 0  |     }  | 
714  |  |  | 
715  | 20  | #ifndef FIPS_MODULE  | 
716  | 20  |     if (dgbl->seed == NULL) { | 
717  | 20  |         ERR_set_mark();  | 
718  | 20  |         dgbl->seed = rand_new_seed(ctx);  | 
719  | 20  |         ERR_pop_to_mark();  | 
720  | 20  |     }  | 
721  | 20  | #endif  | 
722  |  |  | 
723  | 20  |     ret = dgbl->primary = rand_new_drbg(ctx, dgbl->seed,  | 
724  | 20  |                                         PRIMARY_RESEED_INTERVAL,  | 
725  | 20  |                                         PRIMARY_RESEED_TIME_INTERVAL);  | 
726  |  |     /*  | 
727  |  |     * The primary DRBG may be shared between multiple threads so we must  | 
728  |  |     * enable locking.  | 
729  |  |     */  | 
730  | 20  |     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  | 20  |     CRYPTO_THREAD_unlock(dgbl->lock);  | 
736  |  |  | 
737  | 20  |     return ret;  | 
738  | 20  | }  | 
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  | 140k  | { | 
746  | 140k  |     RAND_GLOBAL *dgbl = rand_get_global(ctx);  | 
747  | 140k  |     EVP_RAND_CTX *rand, *primary;  | 
748  |  |  | 
749  | 140k  |     if (dgbl == NULL)  | 
750  | 0  |         return NULL;  | 
751  |  |  | 
752  | 140k  |     rand = CRYPTO_THREAD_get_local(&dgbl->public);  | 
753  | 140k  |     if (rand == NULL) { | 
754  | 7  |         primary = RAND_get0_primary(ctx);  | 
755  | 7  |         if (primary == NULL)  | 
756  | 0  |             return NULL;  | 
757  |  |  | 
758  | 7  |         ctx = ossl_lib_ctx_get_concrete(ctx);  | 
759  |  |  | 
760  | 7  |         if (ctx == NULL)  | 
761  | 0  |             return NULL;  | 
762  |  |         /*  | 
763  |  |          * If the private is also NULL then this is the first time we've  | 
764  |  |          * used this thread.  | 
765  |  |          */  | 
766  | 7  |         if (CRYPTO_THREAD_get_local(&dgbl->private) == NULL  | 
767  | 7  |                 && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))  | 
768  | 0  |             return NULL;  | 
769  | 7  |         rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,  | 
770  | 7  |                              SECONDARY_RESEED_TIME_INTERVAL);  | 
771  | 7  |         CRYPTO_THREAD_set_local(&dgbl->public, rand);  | 
772  | 7  |     }  | 
773  | 140k  |     return rand;  | 
774  | 140k  | }  | 
775  |  |  | 
776  |  | /*  | 
777  |  |  * Get the private random generator.  | 
778  |  |  * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.  | 
779  |  |  */  | 
780  |  | EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx)  | 
781  | 365k  | { | 
782  | 365k  |     RAND_GLOBAL *dgbl = rand_get_global(ctx);  | 
783  | 365k  |     EVP_RAND_CTX *rand, *primary;  | 
784  |  |  | 
785  | 365k  |     if (dgbl == NULL)  | 
786  | 0  |         return NULL;  | 
787  |  |  | 
788  | 365k  |     rand = CRYPTO_THREAD_get_local(&dgbl->private);  | 
789  | 365k  |     if (rand == NULL) { | 
790  | 14  |         primary = RAND_get0_primary(ctx);  | 
791  | 14  |         if (primary == NULL)  | 
792  | 0  |             return NULL;  | 
793  |  |  | 
794  | 14  |         ctx = ossl_lib_ctx_get_concrete(ctx);  | 
795  |  |  | 
796  | 14  |         if (ctx == NULL)  | 
797  | 0  |             return NULL;  | 
798  |  |         /*  | 
799  |  |          * If the public is also NULL then this is the first time we've  | 
800  |  |          * used this thread.  | 
801  |  |          */  | 
802  | 14  |         if (CRYPTO_THREAD_get_local(&dgbl->public) == NULL  | 
803  | 14  |                 && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))  | 
804  | 0  |             return NULL;  | 
805  | 14  |         rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,  | 
806  | 14  |                              SECONDARY_RESEED_TIME_INTERVAL);  | 
807  | 14  |         CRYPTO_THREAD_set_local(&dgbl->private, rand);  | 
808  | 14  |     }  | 
809  | 365k  |     return rand;  | 
810  | 365k  | }  | 
811  |  |  | 
812  |  | #ifdef FIPS_MODULE  | 
813  |  | EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx)  | 
814  |  | { | 
815  |  |     RAND_GLOBAL *dgbl = rand_get_global(ctx);  | 
816  |  |  | 
817  |  |     if (dgbl == NULL)  | 
818  |  |         return NULL;  | 
819  |  |  | 
820  |  |     return CRYPTO_THREAD_get_local(&dgbl->private);  | 
821  |  | }  | 
822  |  | #endif  | 
823  |  |  | 
824  |  | int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)  | 
825  | 0  | { | 
826  | 0  |     RAND_GLOBAL *dgbl = rand_get_global(ctx);  | 
827  | 0  |     EVP_RAND_CTX *old;  | 
828  | 0  |     int r;  | 
829  |  | 
  | 
830  | 0  |     if (dgbl == NULL)  | 
831  | 0  |         return 0;  | 
832  | 0  |     old = CRYPTO_THREAD_get_local(&dgbl->public);  | 
833  | 0  |     if ((r = CRYPTO_THREAD_set_local(&dgbl->public, rand)) > 0)  | 
834  | 0  |         EVP_RAND_CTX_free(old);  | 
835  | 0  |     return r;  | 
836  | 0  | }  | 
837  |  |  | 
838  |  | int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)  | 
839  | 0  | { | 
840  | 0  |     RAND_GLOBAL *dgbl = rand_get_global(ctx);  | 
841  | 0  |     EVP_RAND_CTX *old;  | 
842  | 0  |     int r;  | 
843  |  | 
  | 
844  | 0  |     if (dgbl == NULL)  | 
845  | 0  |         return 0;  | 
846  | 0  |     old = CRYPTO_THREAD_get_local(&dgbl->private);  | 
847  | 0  |     if ((r = CRYPTO_THREAD_set_local(&dgbl->private, rand)) > 0)  | 
848  | 0  |         EVP_RAND_CTX_free(old);  | 
849  | 0  |     return r;  | 
850  | 0  | }  | 
851  |  |  | 
852  |  | #ifndef FIPS_MODULE  | 
853  |  | static int random_set_string(char **p, const char *s)  | 
854  | 240  | { | 
855  | 240  |     char *d = NULL;  | 
856  |  |  | 
857  | 240  |     if (s != NULL) { | 
858  | 60  |         d = OPENSSL_strdup(s);  | 
859  | 60  |         if (d == NULL) { | 
860  | 0  |             ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);  | 
861  | 0  |             return 0;  | 
862  | 0  |         }  | 
863  | 60  |     }  | 
864  | 240  |     OPENSSL_free(*p);  | 
865  | 240  |     *p = d;  | 
866  | 240  |     return 1;  | 
867  | 240  | }  | 
868  |  |  | 
869  |  | /*  | 
870  |  |  * Load the DRBG definitions from a configuration file.  | 
871  |  |  */  | 
872  |  | static int random_conf_init(CONF_IMODULE *md, const CONF *cnf)  | 
873  | 0  | { | 
874  | 0  |     STACK_OF(CONF_VALUE) *elist;  | 
875  | 0  |     CONF_VALUE *cval;  | 
876  | 0  |     RAND_GLOBAL *dgbl = rand_get_global(NCONF_get0_libctx((CONF *)cnf));  | 
877  | 0  |     int i, r = 1;  | 
878  |  | 
  | 
879  | 0  |     OSSL_TRACE1(CONF, "Loading random module: section %s\n",  | 
880  | 0  |                 CONF_imodule_get_value(md));  | 
881  |  |  | 
882  |  |     /* Value is a section containing RANDOM configuration */  | 
883  | 0  |     elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));  | 
884  | 0  |     if (elist == NULL) { | 
885  | 0  |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR);  | 
886  | 0  |         return 0;  | 
887  | 0  |     }  | 
888  |  |  | 
889  | 0  |     if (dgbl == NULL)  | 
890  | 0  |         return 0;  | 
891  |  |  | 
892  | 0  |     for (i = 0; i < sk_CONF_VALUE_num(elist); i++) { | 
893  | 0  |         cval = sk_CONF_VALUE_value(elist, i);  | 
894  | 0  |         if (OPENSSL_strcasecmp(cval->name, "random") == 0) { | 
895  | 0  |             if (!random_set_string(&dgbl->rng_name, cval->value))  | 
896  | 0  |                 return 0;  | 
897  | 0  |         } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) { | 
898  | 0  |             if (!random_set_string(&dgbl->rng_cipher, cval->value))  | 
899  | 0  |                 return 0;  | 
900  | 0  |         } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) { | 
901  | 0  |             if (!random_set_string(&dgbl->rng_digest, cval->value))  | 
902  | 0  |                 return 0;  | 
903  | 0  |         } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) { | 
904  | 0  |             if (!random_set_string(&dgbl->rng_propq, cval->value))  | 
905  | 0  |                 return 0;  | 
906  | 0  |         } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) { | 
907  | 0  |             if (!random_set_string(&dgbl->seed_name, cval->value))  | 
908  | 0  |                 return 0;  | 
909  | 0  |         } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) { | 
910  | 0  |             if (!random_set_string(&dgbl->seed_propq, cval->value))  | 
911  | 0  |                 return 0;  | 
912  | 0  |         } else { | 
913  | 0  |             ERR_raise_data(ERR_LIB_CRYPTO,  | 
914  | 0  |                            CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION,  | 
915  | 0  |                            "name=%s, value=%s", cval->name, cval->value);  | 
916  | 0  |             r = 0;  | 
917  | 0  |         }  | 
918  | 0  |     }  | 
919  | 0  |     return r;  | 
920  | 0  | }  | 
921  |  |  | 
922  |  |  | 
923  |  | static void random_conf_deinit(CONF_IMODULE *md)  | 
924  | 0  | { | 
925  | 0  |     OSSL_TRACE(CONF, "Cleaned up random\n");  | 
926  | 0  | }  | 
927  |  |  | 
928  |  | void ossl_random_add_conf_module(void)  | 
929  | 0  | { | 
930  | 0  |     OSSL_TRACE(CONF, "Adding config module 'random'\n");  | 
931  | 0  |     CONF_module_add("random", random_conf_init, random_conf_deinit); | 
932  | 0  | }  | 
933  |  |  | 
934  |  | int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq,  | 
935  |  |                        const char *cipher, const char *digest)  | 
936  | 60  | { | 
937  | 60  |     RAND_GLOBAL *dgbl = rand_get_global(ctx);  | 
938  |  |  | 
939  | 60  |     if (dgbl == NULL)  | 
940  | 0  |         return 0;  | 
941  | 60  |     if (dgbl->primary != NULL) { | 
942  | 0  |         ERR_raise(ERR_LIB_CRYPTO, RAND_R_ALREADY_INSTANTIATED);  | 
943  | 0  |         return 0;  | 
944  | 0  |     }  | 
945  | 60  |     return random_set_string(&dgbl->rng_name, drbg)  | 
946  | 60  |         && random_set_string(&dgbl->rng_propq, propq)  | 
947  | 60  |         && random_set_string(&dgbl->rng_cipher, cipher)  | 
948  | 60  |         && random_set_string(&dgbl->rng_digest, digest);  | 
949  | 60  | }  | 
950  |  |  | 
951  |  | int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed,  | 
952  |  |                               const char *propq)  | 
953  | 0  | { | 
954  | 0  |     RAND_GLOBAL *dgbl = rand_get_global(ctx);  | 
955  |  | 
  | 
956  | 0  |     if (dgbl == NULL)  | 
957  | 0  |         return 0;  | 
958  | 0  |     if (dgbl->seed != NULL) { | 
959  | 0  |         ERR_raise(ERR_LIB_CRYPTO, RAND_R_ALREADY_INSTANTIATED);  | 
960  | 0  |         return 0;  | 
961  | 0  |     }  | 
962  | 0  |     return random_set_string(&dgbl->seed_name, seed)  | 
963  | 0  |         && random_set_string(&dgbl->seed_propq, propq);  | 
964  | 0  | }  | 
965  |  |  | 
966  |  | #endif  |