/src/openssl35/crypto/rand/rand_lib.c
Line | Count | Source |
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 <openssl/provider.h> |
17 | | #include "internal/cryptlib.h" |
18 | | #include "internal/provider.h" |
19 | | #include "internal/thread_once.h" |
20 | | #include "crypto/rand.h" |
21 | | #include "crypto/cryptlib.h" |
22 | | #include "rand_local.h" |
23 | | #include "crypto/context.h" |
24 | | #include "internal/provider.h" |
25 | | |
26 | | /* clang-format off */ |
27 | | #ifndef OPENSSL_DEFAULT_SEED_SRC |
28 | | #define OPENSSL_DEFAULT_SEED_SRC SEED-SRC |
29 | | #endif |
30 | | /* clang-format on */ |
31 | | |
32 | | typedef struct rand_global_st { |
33 | | /* |
34 | | * The three shared DRBG instances |
35 | | * |
36 | | * There are three shared DRBG instances: <primary>, <public>, and |
37 | | * <private>. The <public> and <private> DRBGs are secondary ones. |
38 | | * These are used for non-secret (e.g. nonces) and secret |
39 | | * (e.g. private keys) data respectively. |
40 | | */ |
41 | | CRYPTO_RWLOCK *lock; |
42 | | |
43 | | EVP_RAND_CTX *seed; |
44 | | |
45 | | /* |
46 | | * The <primary> DRBG |
47 | | * |
48 | | * Not used directly by the application, only for reseeding the two other |
49 | | * DRBGs. It reseeds itself by pulling either randomness from os entropy |
50 | | * sources or by consuming randomness which was added by RAND_add(). |
51 | | * |
52 | | * The <primary> DRBG is a global instance which is accessed concurrently by |
53 | | * all threads. The necessary locking is managed automatically by its child |
54 | | * DRBG instances during reseeding. |
55 | | */ |
56 | | EVP_RAND_CTX *primary; |
57 | | |
58 | | /* |
59 | | * The provider which we'll use to generate randomness. |
60 | | */ |
61 | | #ifndef FIPS_MODULE |
62 | | OSSL_PROVIDER *random_provider; |
63 | | char *random_provider_name; |
64 | | #endif /* !FIPS_MODULE */ |
65 | | |
66 | | /* |
67 | | * The <public> DRBG |
68 | | * |
69 | | * Used by default for generating random bytes using RAND_bytes(). |
70 | | * |
71 | | * The <public> secondary DRBG is thread-local, i.e., there is one instance |
72 | | * per thread. |
73 | | */ |
74 | | CRYPTO_THREAD_LOCAL public; |
75 | | |
76 | | /* |
77 | | * The <private> DRBG |
78 | | * |
79 | | * Used by default for generating private keys using RAND_priv_bytes() |
80 | | * |
81 | | * The <private> secondary DRBG is thread-local, i.e., there is one |
82 | | * instance per thread. |
83 | | */ |
84 | | CRYPTO_THREAD_LOCAL private; |
85 | | |
86 | | /* Which RNG is being used by default and it's configuration settings */ |
87 | | char *rng_name; |
88 | | char *rng_cipher; |
89 | | char *rng_digest; |
90 | | char *rng_propq; |
91 | | |
92 | | /* Allow the randomness source to be changed */ |
93 | | char *seed_name; |
94 | | char *seed_propq; |
95 | | } RAND_GLOBAL; |
96 | | |
97 | | static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl); |
98 | | static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl); |
99 | | static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl); |
100 | | |
101 | | static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx) |
102 | 2.15M | { |
103 | 2.15M | return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX); |
104 | 2.15M | } |
105 | | |
106 | | #ifndef FIPS_MODULE |
107 | | #include <stdio.h> |
108 | | #include <time.h> |
109 | | #include <limits.h> |
110 | | #include <openssl/conf.h> |
111 | | #include <openssl/trace.h> |
112 | | #include <openssl/engine.h> |
113 | | #include "crypto/rand_pool.h" |
114 | | #include "prov/seeding.h" |
115 | | #include "internal/e_os.h" |
116 | | #include "internal/property.h" |
117 | | |
118 | | /* |
119 | | * The default name for the random provider. |
120 | | * This ensures that the FIPS provider will supply libcrypto's random byte |
121 | | * requirements. |
122 | | */ |
123 | | static const char random_provider_fips_name[] = "fips"; |
124 | | |
125 | | static int set_random_provider_name(RAND_GLOBAL *dgbl, const char *name) |
126 | 0 | { |
127 | 0 | if (dgbl->random_provider_name != NULL |
128 | 0 | && OPENSSL_strcasecmp(dgbl->random_provider_name, name) == 0) |
129 | 0 | return 1; |
130 | | |
131 | 0 | OPENSSL_free(dgbl->random_provider_name); |
132 | 0 | dgbl->random_provider_name = OPENSSL_strdup(name); |
133 | 0 | return dgbl->random_provider_name != NULL; |
134 | 0 | } |
135 | | |
136 | | #ifndef OPENSSL_NO_ENGINE |
137 | | /* non-NULL if default_RAND_meth is ENGINE-provided */ |
138 | | static ENGINE *funct_ref; |
139 | | static CRYPTO_RWLOCK *rand_engine_lock; |
140 | | #endif /* !OPENSSL_NO_ENGINE */ |
141 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
142 | | static CRYPTO_RWLOCK *rand_meth_lock; |
143 | | static const RAND_METHOD *default_RAND_meth; |
144 | | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
145 | | static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT; |
146 | | |
147 | | static int rand_inited = 0; |
148 | | |
149 | | DEFINE_RUN_ONCE_STATIC(do_rand_init) |
150 | 61 | { |
151 | 61 | #ifndef OPENSSL_NO_ENGINE |
152 | 61 | rand_engine_lock = CRYPTO_THREAD_lock_new(); |
153 | 61 | if (rand_engine_lock == NULL) |
154 | 0 | return 0; |
155 | 61 | #endif /* !OPENSSL_NO_ENGINE */ |
156 | | |
157 | 61 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
158 | 61 | rand_meth_lock = CRYPTO_THREAD_lock_new(); |
159 | 61 | if (rand_meth_lock == NULL) |
160 | 0 | goto err; |
161 | 61 | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
162 | | |
163 | 61 | if (!ossl_rand_pool_init()) |
164 | 0 | goto err; |
165 | | |
166 | 61 | rand_inited = 1; |
167 | 61 | return 1; |
168 | | |
169 | 0 | err: |
170 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
171 | 0 | CRYPTO_THREAD_lock_free(rand_meth_lock); |
172 | 0 | rand_meth_lock = NULL; |
173 | 0 | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
174 | 0 | #ifndef OPENSSL_NO_ENGINE |
175 | 0 | CRYPTO_THREAD_lock_free(rand_engine_lock); |
176 | 0 | rand_engine_lock = NULL; |
177 | 0 | #endif /* !OPENSSL_NO_ENGINE */ |
178 | 0 | return 0; |
179 | 61 | } |
180 | | |
181 | | void ossl_rand_cleanup_int(void) |
182 | 220 | { |
183 | 220 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
184 | 220 | const RAND_METHOD *meth = default_RAND_meth; |
185 | | |
186 | 220 | if (!rand_inited) |
187 | 159 | return; |
188 | | |
189 | 61 | if (meth != NULL && meth->cleanup != NULL) |
190 | 0 | meth->cleanup(); |
191 | 61 | RAND_set_rand_method(NULL); |
192 | 61 | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
193 | 61 | ossl_rand_pool_cleanup(); |
194 | 61 | #ifndef OPENSSL_NO_ENGINE |
195 | 61 | CRYPTO_THREAD_lock_free(rand_engine_lock); |
196 | 61 | rand_engine_lock = NULL; |
197 | 61 | #endif /* !OPENSSL_NO_ENGINE */ |
198 | 61 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
199 | 61 | CRYPTO_THREAD_lock_free(rand_meth_lock); |
200 | 61 | rand_meth_lock = NULL; |
201 | 61 | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
202 | 61 | ossl_release_default_drbg_ctx(); |
203 | 61 | rand_inited = 0; |
204 | 61 | } |
205 | | |
206 | | /* |
207 | | * RAND_close_seed_files() ensures that any seed file descriptors are |
208 | | * closed after use. This only applies to libcrypto/default provider, |
209 | | * it does not apply to other providers. |
210 | | */ |
211 | | void RAND_keep_random_devices_open(int keep) |
212 | 0 | { |
213 | 0 | if (RUN_ONCE(&rand_init, do_rand_init)) |
214 | 0 | ossl_rand_pool_keep_random_devices_open(keep); |
215 | 0 | } |
216 | | |
217 | | /* |
218 | | * RAND_poll() reseeds the default RNG using random input |
219 | | * |
220 | | * The random input is obtained from polling various entropy |
221 | | * sources which depend on the operating system and are |
222 | | * configurable via the --with-rand-seed configure option. |
223 | | */ |
224 | | int RAND_poll(void) |
225 | 0 | { |
226 | 0 | static const char salt[] = "polling"; |
227 | |
|
228 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
229 | 0 | const RAND_METHOD *meth = RAND_get_rand_method(); |
230 | 0 | int ret = meth == RAND_OpenSSL(); |
231 | |
|
232 | 0 | if (meth == NULL) |
233 | 0 | return 0; |
234 | | |
235 | 0 | if (!ret) { |
236 | | /* fill random pool and seed the current legacy RNG */ |
237 | 0 | RAND_POOL *pool = ossl_rand_pool_new(RAND_DRBG_STRENGTH, 1, |
238 | 0 | (RAND_DRBG_STRENGTH + 7) / 8, |
239 | 0 | RAND_POOL_MAX_LENGTH); |
240 | |
|
241 | 0 | if (pool == NULL) |
242 | 0 | return 0; |
243 | | |
244 | 0 | if (ossl_pool_acquire_entropy(pool) == 0) |
245 | 0 | goto err; |
246 | | |
247 | 0 | if (meth->add == NULL |
248 | 0 | || meth->add(ossl_rand_pool_buffer(pool), |
249 | 0 | ossl_rand_pool_length(pool), |
250 | 0 | (ossl_rand_pool_entropy(pool) / 8.0)) |
251 | 0 | == 0) |
252 | 0 | goto err; |
253 | | |
254 | 0 | ret = 1; |
255 | 0 | err: |
256 | 0 | ossl_rand_pool_free(pool); |
257 | 0 | return ret; |
258 | 0 | } |
259 | 0 | #endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
260 | | |
261 | 0 | RAND_seed(salt, sizeof(salt)); |
262 | 0 | return 1; |
263 | 0 | } |
264 | | |
265 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
266 | | static int rand_set_rand_method_internal(const RAND_METHOD *meth, |
267 | | ossl_unused ENGINE *e) |
268 | 61 | { |
269 | 61 | if (!RUN_ONCE(&rand_init, do_rand_init)) |
270 | 0 | return 0; |
271 | | |
272 | 61 | if (!CRYPTO_THREAD_write_lock(rand_meth_lock)) |
273 | 0 | return 0; |
274 | 61 | #ifndef OPENSSL_NO_ENGINE |
275 | 61 | ENGINE_finish(funct_ref); |
276 | 61 | funct_ref = e; |
277 | 61 | #endif |
278 | 61 | default_RAND_meth = meth; |
279 | 61 | CRYPTO_THREAD_unlock(rand_meth_lock); |
280 | 61 | return 1; |
281 | 61 | } |
282 | | |
283 | | int RAND_set_rand_method(const RAND_METHOD *meth) |
284 | 61 | { |
285 | 61 | return rand_set_rand_method_internal(meth, NULL); |
286 | 61 | } |
287 | | |
288 | | const RAND_METHOD *RAND_get_rand_method(void) |
289 | 1.15M | { |
290 | 1.15M | const RAND_METHOD *tmp_meth = NULL; |
291 | | |
292 | 1.15M | if (!RUN_ONCE(&rand_init, do_rand_init)) |
293 | 0 | return NULL; |
294 | | |
295 | 1.15M | if (rand_meth_lock == NULL) |
296 | 0 | return NULL; |
297 | | |
298 | 1.15M | if (!CRYPTO_THREAD_read_lock(rand_meth_lock)) |
299 | 0 | return NULL; |
300 | 1.15M | tmp_meth = default_RAND_meth; |
301 | 1.15M | CRYPTO_THREAD_unlock(rand_meth_lock); |
302 | 1.15M | if (tmp_meth != NULL) |
303 | 1.15M | return tmp_meth; |
304 | | |
305 | 44 | if (!CRYPTO_THREAD_write_lock(rand_meth_lock)) |
306 | 0 | return NULL; |
307 | 44 | if (default_RAND_meth == NULL) { |
308 | 44 | #ifndef OPENSSL_NO_ENGINE |
309 | 44 | ENGINE *e; |
310 | | |
311 | | /* If we have an engine that can do RAND, use it. */ |
312 | 44 | if ((e = ENGINE_get_default_RAND()) != NULL |
313 | 0 | && (tmp_meth = ENGINE_get_RAND(e)) != NULL) { |
314 | 0 | funct_ref = e; |
315 | 0 | default_RAND_meth = tmp_meth; |
316 | 44 | } else { |
317 | 44 | ENGINE_finish(e); |
318 | 44 | default_RAND_meth = &ossl_rand_meth; |
319 | 44 | } |
320 | | #else |
321 | | default_RAND_meth = &ossl_rand_meth; |
322 | | #endif |
323 | 44 | } |
324 | 44 | tmp_meth = default_RAND_meth; |
325 | 44 | CRYPTO_THREAD_unlock(rand_meth_lock); |
326 | 44 | return tmp_meth; |
327 | 44 | } |
328 | | |
329 | | #if !defined(OPENSSL_NO_ENGINE) |
330 | | int RAND_set_rand_engine(ENGINE *engine) |
331 | 0 | { |
332 | 0 | const RAND_METHOD *tmp_meth = NULL; |
333 | |
|
334 | 0 | if (!RUN_ONCE(&rand_init, do_rand_init)) |
335 | 0 | return 0; |
336 | | |
337 | 0 | if (engine != NULL) { |
338 | 0 | if (!ENGINE_init(engine)) |
339 | 0 | return 0; |
340 | 0 | tmp_meth = ENGINE_get_RAND(engine); |
341 | 0 | if (tmp_meth == NULL) { |
342 | 0 | ENGINE_finish(engine); |
343 | 0 | return 0; |
344 | 0 | } |
345 | 0 | } |
346 | 0 | if (!CRYPTO_THREAD_write_lock(rand_engine_lock)) { |
347 | 0 | ENGINE_finish(engine); |
348 | 0 | return 0; |
349 | 0 | } |
350 | | |
351 | | /* This function releases any prior ENGINE so call it first */ |
352 | 0 | rand_set_rand_method_internal(tmp_meth, engine); |
353 | 0 | CRYPTO_THREAD_unlock(rand_engine_lock); |
354 | 0 | return 1; |
355 | 0 | } |
356 | | #endif |
357 | | #endif /* OPENSSL_NO_DEPRECATED_3_0 */ |
358 | | |
359 | | void RAND_seed(const void *buf, int num) |
360 | 0 | { |
361 | 0 | EVP_RAND_CTX *drbg; |
362 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
363 | 0 | const RAND_METHOD *meth = RAND_get_rand_method(); |
364 | |
|
365 | 0 | if (meth != NULL && meth->seed != NULL) { |
366 | 0 | meth->seed(buf, num); |
367 | 0 | return; |
368 | 0 | } |
369 | 0 | #endif |
370 | | |
371 | 0 | drbg = RAND_get0_primary(NULL); |
372 | 0 | if (drbg != NULL && num > 0) |
373 | 0 | EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num); |
374 | 0 | } |
375 | | |
376 | | void RAND_add(const void *buf, int num, double randomness) |
377 | 0 | { |
378 | 0 | EVP_RAND_CTX *drbg; |
379 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
380 | 0 | const RAND_METHOD *meth = RAND_get_rand_method(); |
381 | |
|
382 | 0 | if (meth != NULL && meth->add != NULL) { |
383 | 0 | meth->add(buf, num, randomness); |
384 | 0 | return; |
385 | 0 | } |
386 | 0 | #endif |
387 | 0 | drbg = RAND_get0_primary(NULL); |
388 | 0 | if (drbg != NULL && num > 0) |
389 | | #ifdef OPENSSL_RAND_SEED_NONE |
390 | | /* Without an entropy source, we have to rely on the user */ |
391 | | EVP_RAND_reseed(drbg, 0, buf, num, NULL, 0); |
392 | | #else |
393 | | /* With an entropy source, we downgrade this to additional input */ |
394 | 0 | EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num); |
395 | 0 | #endif |
396 | 0 | } |
397 | | |
398 | | #if !defined(OPENSSL_NO_DEPRECATED_1_1_0) |
399 | | int RAND_pseudo_bytes(unsigned char *buf, int num) |
400 | 0 | { |
401 | 0 | const RAND_METHOD *meth = RAND_get_rand_method(); |
402 | |
|
403 | 0 | if (meth != NULL && meth->pseudorand != NULL) |
404 | 0 | return meth->pseudorand(buf, num); |
405 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED); |
406 | 0 | return -1; |
407 | 0 | } |
408 | | #endif |
409 | | |
410 | | int RAND_status(void) |
411 | 0 | { |
412 | 0 | EVP_RAND_CTX *rand; |
413 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
414 | 0 | const RAND_METHOD *meth = RAND_get_rand_method(); |
415 | |
|
416 | 0 | if (meth != NULL && meth != RAND_OpenSSL()) |
417 | 0 | return meth->status != NULL ? meth->status() : 0; |
418 | 0 | #endif |
419 | | |
420 | 0 | if ((rand = RAND_get0_primary(NULL)) == NULL) |
421 | 0 | return 0; |
422 | 0 | return EVP_RAND_get_state(rand) == EVP_RAND_STATE_READY; |
423 | 0 | } |
424 | | #else /* !FIPS_MODULE */ |
425 | | |
426 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
427 | | const RAND_METHOD *RAND_get_rand_method(void) |
428 | | { |
429 | | return NULL; |
430 | | } |
431 | | #endif |
432 | | #endif /* !FIPS_MODULE */ |
433 | | |
434 | | /* |
435 | | * This function is not part of RAND_METHOD, so if we're not using |
436 | | * the default method, then just call RAND_bytes(). Otherwise make |
437 | | * sure we're instantiated and use the private DRBG. |
438 | | */ |
439 | | int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num, |
440 | | unsigned int strength) |
441 | 241k | { |
442 | 241k | RAND_GLOBAL *dgbl; |
443 | 241k | EVP_RAND_CTX *rand; |
444 | 241k | #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE) |
445 | 241k | const RAND_METHOD *meth = RAND_get_rand_method(); |
446 | | |
447 | 241k | if (meth != NULL && meth != RAND_OpenSSL()) { |
448 | 0 | if (meth->bytes != NULL) |
449 | 0 | return meth->bytes(buf, num); |
450 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED); |
451 | 0 | return -1; |
452 | 0 | } |
453 | 241k | #endif |
454 | | |
455 | 241k | dgbl = rand_get_global(ctx); |
456 | 241k | if (dgbl == NULL) |
457 | 0 | return 0; |
458 | 241k | #ifndef FIPS_MODULE |
459 | 241k | if (dgbl->random_provider != NULL) |
460 | 0 | return ossl_provider_random_bytes(dgbl->random_provider, |
461 | 0 | OSSL_PROV_RANDOM_PRIVATE, |
462 | 0 | buf, num, strength); |
463 | 241k | #endif /* !FIPS_MODULE */ |
464 | 241k | rand = rand_get0_private(ctx, dgbl); |
465 | 241k | if (rand != NULL) |
466 | 241k | return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0); |
467 | | |
468 | 0 | return 0; |
469 | 241k | } |
470 | | |
471 | | int RAND_priv_bytes(unsigned char *buf, int num) |
472 | 0 | { |
473 | 0 | if (num < 0) |
474 | 0 | return 0; |
475 | 0 | return RAND_priv_bytes_ex(NULL, buf, (size_t)num, 0); |
476 | 0 | } |
477 | | |
478 | | int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num, |
479 | | unsigned int strength) |
480 | 170k | { |
481 | 170k | RAND_GLOBAL *dgbl; |
482 | 170k | EVP_RAND_CTX *rand; |
483 | 170k | #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE) |
484 | 170k | const RAND_METHOD *meth = RAND_get_rand_method(); |
485 | | |
486 | 170k | if (meth != NULL && meth != RAND_OpenSSL()) { |
487 | 0 | if (meth->bytes != NULL) |
488 | 0 | return meth->bytes(buf, num); |
489 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED); |
490 | 0 | return -1; |
491 | 0 | } |
492 | 170k | #endif |
493 | | |
494 | 170k | dgbl = rand_get_global(ctx); |
495 | 170k | if (dgbl == NULL) |
496 | 0 | return 0; |
497 | 170k | #ifndef FIPS_MODULE |
498 | 170k | if (dgbl->random_provider != NULL) |
499 | 0 | return ossl_provider_random_bytes(dgbl->random_provider, |
500 | 0 | OSSL_PROV_RANDOM_PUBLIC, |
501 | 0 | buf, num, strength); |
502 | 170k | #endif /* !FIPS_MODULE */ |
503 | | |
504 | 170k | rand = rand_get0_public(ctx, dgbl); |
505 | 170k | if (rand != NULL) |
506 | 170k | return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0); |
507 | | |
508 | 0 | return 0; |
509 | 170k | } |
510 | | |
511 | | int RAND_bytes(unsigned char *buf, int num) |
512 | 765 | { |
513 | 765 | if (num < 0) |
514 | 0 | return 0; |
515 | 765 | return RAND_bytes_ex(NULL, buf, (size_t)num, 0); |
516 | 765 | } |
517 | | |
518 | | /* |
519 | | * Initialize the OSSL_LIB_CTX global DRBGs on first use. |
520 | | * Returns the allocated global data on success or NULL on failure. |
521 | | */ |
522 | | void *ossl_rand_ctx_new(OSSL_LIB_CTX *libctx) |
523 | 81 | { |
524 | 81 | RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl)); |
525 | | |
526 | 81 | if (dgbl == NULL) |
527 | 0 | return NULL; |
528 | | |
529 | 81 | #ifndef FIPS_MODULE |
530 | | /* |
531 | | * We need to ensure that base libcrypto thread handling has been |
532 | | * initialised. |
533 | | */ |
534 | 81 | OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL); |
535 | | |
536 | | /* Prepopulate the random provider name */ |
537 | 81 | dgbl->random_provider_name = OPENSSL_strdup(random_provider_fips_name); |
538 | 81 | if (dgbl->random_provider_name == NULL) |
539 | 0 | goto err0; |
540 | 81 | #endif |
541 | | |
542 | 81 | dgbl->lock = CRYPTO_THREAD_lock_new(); |
543 | 81 | if (dgbl->lock == NULL) |
544 | 0 | goto err1; |
545 | | |
546 | 81 | if (!CRYPTO_THREAD_init_local(&dgbl->private, NULL)) |
547 | 0 | goto err1; |
548 | | |
549 | 81 | if (!CRYPTO_THREAD_init_local(&dgbl->public, NULL)) |
550 | 0 | goto err2; |
551 | | |
552 | 81 | return dgbl; |
553 | | |
554 | 0 | err2: |
555 | 0 | CRYPTO_THREAD_cleanup_local(&dgbl->private); |
556 | 0 | err1: |
557 | 0 | CRYPTO_THREAD_lock_free(dgbl->lock); |
558 | 0 | #ifndef FIPS_MODULE |
559 | 0 | err0: |
560 | 0 | OPENSSL_free(dgbl->random_provider_name); |
561 | 0 | #endif |
562 | 0 | OPENSSL_free(dgbl); |
563 | 0 | return NULL; |
564 | 0 | } |
565 | | |
566 | | void ossl_rand_ctx_free(void *vdgbl) |
567 | 213 | { |
568 | 213 | RAND_GLOBAL *dgbl = vdgbl; |
569 | | |
570 | 213 | if (dgbl == NULL) |
571 | 6 | return; |
572 | | |
573 | 207 | CRYPTO_THREAD_lock_free(dgbl->lock); |
574 | 207 | CRYPTO_THREAD_cleanup_local(&dgbl->private); |
575 | 207 | CRYPTO_THREAD_cleanup_local(&dgbl->public); |
576 | 207 | EVP_RAND_CTX_free(dgbl->primary); |
577 | 207 | EVP_RAND_CTX_free(dgbl->seed); |
578 | 207 | #ifndef FIPS_MODULE |
579 | 207 | OPENSSL_free(dgbl->random_provider_name); |
580 | 207 | #endif /* !FIPS_MODULE */ |
581 | 207 | OPENSSL_free(dgbl->rng_name); |
582 | 207 | OPENSSL_free(dgbl->rng_cipher); |
583 | 207 | OPENSSL_free(dgbl->rng_digest); |
584 | 207 | OPENSSL_free(dgbl->rng_propq); |
585 | 207 | OPENSSL_free(dgbl->seed_name); |
586 | 207 | OPENSSL_free(dgbl->seed_propq); |
587 | | |
588 | 207 | OPENSSL_free(dgbl); |
589 | 207 | } |
590 | | |
591 | | static void rand_delete_thread_state(void *arg) |
592 | 61 | { |
593 | 61 | OSSL_LIB_CTX *ctx = arg; |
594 | 61 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
595 | 61 | EVP_RAND_CTX *rand; |
596 | | |
597 | 61 | if (dgbl == NULL) |
598 | 0 | return; |
599 | | |
600 | 61 | rand = CRYPTO_THREAD_get_local(&dgbl->public); |
601 | 61 | CRYPTO_THREAD_set_local(&dgbl->public, NULL); |
602 | 61 | EVP_RAND_CTX_free(rand); |
603 | | |
604 | 61 | rand = CRYPTO_THREAD_get_local(&dgbl->private); |
605 | 61 | CRYPTO_THREAD_set_local(&dgbl->private, NULL); |
606 | 61 | EVP_RAND_CTX_free(rand); |
607 | 61 | } |
608 | | |
609 | | #if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER) |
610 | | static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx) |
611 | 49 | { |
612 | 49 | EVP_RAND *rand; |
613 | 49 | const char *propq; |
614 | 49 | char *name; |
615 | 49 | EVP_RAND_CTX *ctx = NULL; |
616 | 49 | #ifdef OPENSSL_NO_FIPS_JITTER |
617 | 49 | RAND_GLOBAL *dgbl = rand_get_global(libctx); |
618 | | |
619 | 49 | if (dgbl == NULL) |
620 | 0 | return NULL; |
621 | 49 | propq = dgbl->seed_propq; |
622 | 49 | name = dgbl->seed_name != NULL ? dgbl->seed_name |
623 | 49 | : OPENSSL_MSTR(OPENSSL_DEFAULT_SEED_SRC); |
624 | | #else /* !OPENSSL_NO_FIPS_JITTER */ |
625 | | name = "JITTER"; |
626 | | propq = ""; |
627 | | #endif /* OPENSSL_NO_FIPS_JITTER */ |
628 | | |
629 | 49 | rand = EVP_RAND_fetch(libctx, name, propq); |
630 | 49 | if (rand == NULL) { |
631 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG); |
632 | 0 | goto err; |
633 | 0 | } |
634 | 49 | ctx = EVP_RAND_CTX_new(rand, NULL); |
635 | 49 | EVP_RAND_free(rand); |
636 | 49 | if (ctx == NULL) { |
637 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG); |
638 | 0 | goto err; |
639 | 0 | } |
640 | 49 | if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) { |
641 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG); |
642 | 0 | goto err; |
643 | 0 | } |
644 | 49 | return ctx; |
645 | 0 | err: |
646 | 0 | EVP_RAND_CTX_free(ctx); |
647 | 0 | return NULL; |
648 | 49 | } |
649 | | #endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */ |
650 | | |
651 | | #ifndef FIPS_MODULE |
652 | | EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx) |
653 | 2.46k | { |
654 | 2.46k | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
655 | 2.46k | EVP_RAND_CTX *ret; |
656 | | |
657 | 2.46k | if (dgbl == NULL) |
658 | 0 | return NULL; |
659 | | |
660 | 2.46k | if (!CRYPTO_THREAD_read_lock(dgbl->lock)) |
661 | 0 | return NULL; |
662 | 2.46k | ret = dgbl->seed; |
663 | 2.46k | CRYPTO_THREAD_unlock(dgbl->lock); |
664 | 2.46k | return ret; |
665 | 2.46k | } |
666 | | #endif /* !FIPS_MODULE */ |
667 | | |
668 | | static EVP_RAND_CTX *rand_new_drbg(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent, |
669 | | unsigned int reseed_interval, |
670 | | time_t reseed_time_interval) |
671 | 128 | { |
672 | 128 | EVP_RAND *rand; |
673 | 128 | RAND_GLOBAL *dgbl = rand_get_global(libctx); |
674 | 128 | EVP_RAND_CTX *ctx; |
675 | 128 | OSSL_PARAM params[9], *p = params; |
676 | 128 | const OSSL_PARAM *settables; |
677 | 128 | const char *prov_name; |
678 | 128 | char *name, *cipher; |
679 | 128 | int use_df = 1; |
680 | | |
681 | 128 | if (dgbl == NULL) |
682 | 0 | return NULL; |
683 | 128 | name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG"; |
684 | 128 | rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq); |
685 | 128 | if (rand == NULL) { |
686 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG); |
687 | 0 | return NULL; |
688 | 0 | } |
689 | 128 | prov_name = ossl_provider_name(EVP_RAND_get0_provider(rand)); |
690 | 128 | ctx = EVP_RAND_CTX_new(rand, parent); |
691 | 128 | EVP_RAND_free(rand); |
692 | 128 | if (ctx == NULL) { |
693 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG); |
694 | 0 | return NULL; |
695 | 0 | } |
696 | | |
697 | 128 | settables = EVP_RAND_CTX_settable_params(ctx); |
698 | 128 | if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_CIPHER)) { |
699 | 35 | cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR"; |
700 | 35 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER, |
701 | 35 | cipher, 0); |
702 | 35 | } |
703 | 128 | if (dgbl->rng_digest != NULL |
704 | 0 | && OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_DIGEST)) |
705 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST, |
706 | 0 | dgbl->rng_digest, 0); |
707 | 128 | if (prov_name != NULL) |
708 | 128 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_CORE_PROV_NAME, |
709 | 128 | (char *)prov_name, 0); |
710 | 128 | if (dgbl->rng_propq != NULL) |
711 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, |
712 | 0 | dgbl->rng_propq, 0); |
713 | 128 | if (OSSL_PARAM_locate_const(settables, OSSL_ALG_PARAM_MAC)) |
714 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0); |
715 | 128 | if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_USE_DF)) |
716 | 35 | *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &use_df); |
717 | 128 | *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, |
718 | 128 | &reseed_interval); |
719 | 128 | *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, |
720 | 128 | &reseed_time_interval); |
721 | 128 | *p = OSSL_PARAM_construct_end(); |
722 | 128 | if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) { |
723 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG); |
724 | 0 | EVP_RAND_CTX_free(ctx); |
725 | 0 | return NULL; |
726 | 0 | } |
727 | 128 | return ctx; |
728 | 128 | } |
729 | | |
730 | | #if defined(FIPS_MODULE) |
731 | | static EVP_RAND_CTX *rand_new_crngt(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent) |
732 | | { |
733 | | EVP_RAND *rand; |
734 | | EVP_RAND_CTX *ctx; |
735 | | |
736 | | rand = EVP_RAND_fetch(libctx, "CRNG-TEST", "-fips"); |
737 | | if (rand == NULL) { |
738 | | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG); |
739 | | return NULL; |
740 | | } |
741 | | ctx = EVP_RAND_CTX_new(rand, parent); |
742 | | EVP_RAND_free(rand); |
743 | | if (ctx == NULL) { |
744 | | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG); |
745 | | return NULL; |
746 | | } |
747 | | |
748 | | if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) { |
749 | | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG); |
750 | | EVP_RAND_CTX_free(ctx); |
751 | | return NULL; |
752 | | } |
753 | | return ctx; |
754 | | } |
755 | | #endif /* FIPS_MODULE */ |
756 | | |
757 | | /* |
758 | | * Get the primary random generator. |
759 | | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. |
760 | | * |
761 | | */ |
762 | | static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) |
763 | 79 | { |
764 | 79 | EVP_RAND_CTX *ret, *seed, *newseed = NULL, *primary; |
765 | | |
766 | 79 | if (dgbl == NULL) |
767 | 0 | return NULL; |
768 | | |
769 | 79 | if (!CRYPTO_THREAD_read_lock(dgbl->lock)) |
770 | 0 | return NULL; |
771 | | |
772 | 79 | ret = dgbl->primary; |
773 | 79 | seed = dgbl->seed; |
774 | 79 | CRYPTO_THREAD_unlock(dgbl->lock); |
775 | | |
776 | 79 | if (ret != NULL) |
777 | 30 | return ret; |
778 | | |
779 | 49 | #if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER) |
780 | | /* Create a seed source for libcrypto or jitter enabled FIPS provider */ |
781 | 49 | if (seed == NULL) { |
782 | 49 | ERR_set_mark(); |
783 | 49 | seed = newseed = rand_new_seed(ctx); |
784 | 49 | ERR_pop_to_mark(); |
785 | 49 | } |
786 | 49 | #endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */ |
787 | | |
788 | | #if defined(FIPS_MODULE) |
789 | | /* The FIPS provider has entropy health tests instead of the primary */ |
790 | | ret = rand_new_crngt(ctx, seed); |
791 | | #else /* FIPS_MODULE */ |
792 | 49 | ret = rand_new_drbg(ctx, seed, PRIMARY_RESEED_INTERVAL, |
793 | 49 | PRIMARY_RESEED_TIME_INTERVAL); |
794 | 49 | #endif /* FIPS_MODULE */ |
795 | | |
796 | | /* |
797 | | * The primary DRBG may be shared between multiple threads so we must |
798 | | * enable locking. |
799 | | */ |
800 | 49 | if (ret == NULL || !EVP_RAND_enable_locking(ret)) { |
801 | 0 | if (ret != NULL) { |
802 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING); |
803 | 0 | EVP_RAND_CTX_free(ret); |
804 | 0 | } |
805 | 0 | if (newseed == NULL) |
806 | 0 | return NULL; |
807 | | /* else carry on and store seed */ |
808 | 0 | ret = NULL; |
809 | 0 | } |
810 | | |
811 | 49 | if (!CRYPTO_THREAD_write_lock(dgbl->lock)) |
812 | 0 | return NULL; |
813 | | |
814 | 49 | primary = dgbl->primary; |
815 | 49 | if (primary != NULL) { |
816 | 0 | CRYPTO_THREAD_unlock(dgbl->lock); |
817 | 0 | EVP_RAND_CTX_free(ret); |
818 | 0 | EVP_RAND_CTX_free(newseed); |
819 | 0 | return primary; |
820 | 0 | } |
821 | 49 | if (newseed != NULL) |
822 | 49 | dgbl->seed = newseed; |
823 | 49 | dgbl->primary = ret; |
824 | 49 | CRYPTO_THREAD_unlock(dgbl->lock); |
825 | | |
826 | 49 | return ret; |
827 | 49 | } |
828 | | |
829 | | /* |
830 | | * Get the primary random generator. |
831 | | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. |
832 | | * |
833 | | */ |
834 | | EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx) |
835 | 0 | { |
836 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
837 | |
|
838 | 0 | return dgbl == NULL ? NULL : rand_get0_primary(ctx, dgbl); |
839 | 0 | } |
840 | | |
841 | | static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) |
842 | 170k | { |
843 | 170k | EVP_RAND_CTX *rand, *primary; |
844 | | |
845 | 170k | if (dgbl == NULL) |
846 | 0 | return NULL; |
847 | | |
848 | 170k | rand = CRYPTO_THREAD_get_local(&dgbl->public); |
849 | 170k | if (rand == NULL) { |
850 | 11 | primary = rand_get0_primary(ctx, dgbl); |
851 | 11 | if (primary == NULL) |
852 | 0 | return NULL; |
853 | | |
854 | 11 | ctx = ossl_lib_ctx_get_concrete(ctx); |
855 | | |
856 | 11 | if (ctx == NULL) |
857 | 0 | return NULL; |
858 | | /* |
859 | | * If the private is also NULL then this is the first time we've |
860 | | * used this thread. |
861 | | */ |
862 | 11 | if (CRYPTO_THREAD_get_local(&dgbl->private) == NULL |
863 | 9 | && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state)) |
864 | 0 | return NULL; |
865 | 11 | rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL, |
866 | 11 | SECONDARY_RESEED_TIME_INTERVAL); |
867 | 11 | CRYPTO_THREAD_set_local(&dgbl->public, rand); |
868 | 11 | } |
869 | 170k | return rand; |
870 | 170k | } |
871 | | |
872 | | /* |
873 | | * Get the public random generator. |
874 | | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. |
875 | | */ |
876 | | EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx) |
877 | | { |
878 | | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
879 | | |
880 | | return dgbl == NULL ? NULL : rand_get0_public(ctx, dgbl); |
881 | | } |
882 | | |
883 | | static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) |
884 | 241k | { |
885 | 241k | EVP_RAND_CTX *rand, *primary; |
886 | | |
887 | 241k | rand = CRYPTO_THREAD_get_local(&dgbl->private); |
888 | 241k | if (rand == NULL) { |
889 | 15 | primary = rand_get0_primary(ctx, dgbl); |
890 | 15 | if (primary == NULL) |
891 | 0 | return NULL; |
892 | | |
893 | 15 | ctx = ossl_lib_ctx_get_concrete(ctx); |
894 | | |
895 | 15 | if (ctx == NULL) |
896 | 0 | return NULL; |
897 | | /* |
898 | | * If the public is also NULL then this is the first time we've |
899 | | * used this thread. |
900 | | */ |
901 | 15 | if (CRYPTO_THREAD_get_local(&dgbl->public) == NULL |
902 | 7 | && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state)) |
903 | 0 | return NULL; |
904 | 15 | rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL, |
905 | 15 | SECONDARY_RESEED_TIME_INTERVAL); |
906 | 15 | CRYPTO_THREAD_set_local(&dgbl->private, rand); |
907 | 15 | } |
908 | 241k | return rand; |
909 | 241k | } |
910 | | |
911 | | /* |
912 | | * Get the private random generator. |
913 | | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. |
914 | | */ |
915 | | EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx) |
916 | | { |
917 | | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
918 | | |
919 | | return dgbl == NULL ? NULL : rand_get0_private(ctx, dgbl); |
920 | | } |
921 | | |
922 | | #ifdef FIPS_MODULE |
923 | | EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx) |
924 | | { |
925 | | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
926 | | |
927 | | if (dgbl == NULL) |
928 | | return NULL; |
929 | | |
930 | | return CRYPTO_THREAD_get_local(&dgbl->private); |
931 | | } |
932 | | #endif |
933 | | |
934 | | int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand) |
935 | 0 | { |
936 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
937 | 0 | EVP_RAND_CTX *old; |
938 | 0 | int r; |
939 | |
|
940 | 0 | if (dgbl == NULL) |
941 | 0 | return 0; |
942 | 0 | old = CRYPTO_THREAD_get_local(&dgbl->public); |
943 | 0 | if ((r = CRYPTO_THREAD_set_local(&dgbl->public, rand)) > 0) |
944 | 0 | EVP_RAND_CTX_free(old); |
945 | 0 | return r; |
946 | 0 | } |
947 | | |
948 | | int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand) |
949 | 0 | { |
950 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
951 | 0 | EVP_RAND_CTX *old; |
952 | 0 | int r; |
953 | |
|
954 | 0 | if (dgbl == NULL) |
955 | 0 | return 0; |
956 | 0 | old = CRYPTO_THREAD_get_local(&dgbl->private); |
957 | 0 | if ((r = CRYPTO_THREAD_set_local(&dgbl->private, rand)) > 0) |
958 | 0 | EVP_RAND_CTX_free(old); |
959 | 0 | return r; |
960 | 0 | } |
961 | | |
962 | | #ifndef FIPS_MODULE |
963 | | static int random_set_string(char **p, const char *s) |
964 | 544 | { |
965 | 544 | char *d = NULL; |
966 | | |
967 | 544 | if (s != NULL) { |
968 | 136 | d = OPENSSL_strdup(s); |
969 | 136 | if (d == NULL) |
970 | 0 | return 0; |
971 | 136 | } |
972 | 544 | OPENSSL_free(*p); |
973 | 544 | *p = d; |
974 | 544 | return 1; |
975 | 544 | } |
976 | | |
977 | | /* |
978 | | * Load the DRBG definitions from a configuration file. |
979 | | */ |
980 | | static int random_conf_init(CONF_IMODULE *md, const CONF *cnf) |
981 | 0 | { |
982 | 0 | STACK_OF(CONF_VALUE) *elist; |
983 | 0 | CONF_VALUE *cval; |
984 | 0 | OSSL_LIB_CTX *libctx = NCONF_get0_libctx((CONF *)cnf); |
985 | 0 | RAND_GLOBAL *dgbl = rand_get_global(libctx); |
986 | 0 | int i, r = 1; |
987 | |
|
988 | 0 | OSSL_TRACE1(CONF, "Loading random module: section %s\n", |
989 | 0 | CONF_imodule_get_value(md)); |
990 | | |
991 | | /* Value is a section containing RANDOM configuration */ |
992 | 0 | elist = NCONF_get_section(cnf, CONF_imodule_get_value(md)); |
993 | 0 | if (elist == NULL) { |
994 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR); |
995 | 0 | return 0; |
996 | 0 | } |
997 | | |
998 | 0 | if (dgbl == NULL) |
999 | 0 | return 0; |
1000 | | |
1001 | 0 | for (i = 0; i < sk_CONF_VALUE_num(elist); i++) { |
1002 | 0 | cval = sk_CONF_VALUE_value(elist, i); |
1003 | 0 | if (OPENSSL_strcasecmp(cval->name, "random") == 0) { |
1004 | 0 | if (!random_set_string(&dgbl->rng_name, cval->value)) |
1005 | 0 | return 0; |
1006 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) { |
1007 | 0 | if (!random_set_string(&dgbl->rng_cipher, cval->value)) |
1008 | 0 | return 0; |
1009 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) { |
1010 | 0 | if (!random_set_string(&dgbl->rng_digest, cval->value)) |
1011 | 0 | return 0; |
1012 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) { |
1013 | 0 | if (!random_set_string(&dgbl->rng_propq, cval->value)) |
1014 | 0 | return 0; |
1015 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) { |
1016 | 0 | if (!random_set_string(&dgbl->seed_name, cval->value)) |
1017 | 0 | return 0; |
1018 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) { |
1019 | 0 | if (!random_set_string(&dgbl->seed_propq, cval->value)) |
1020 | 0 | return 0; |
1021 | 0 | } else if (OPENSSL_strcasecmp(cval->name, "random_provider") == 0) { |
1022 | 0 | #ifndef FIPS_MODULE |
1023 | 0 | OSSL_PROVIDER *prov = ossl_provider_find(libctx, cval->value, 0); |
1024 | |
|
1025 | 0 | if (prov != NULL) { |
1026 | 0 | if (!RAND_set1_random_provider(libctx, prov)) { |
1027 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1028 | 0 | OSSL_PROVIDER_unload(prov); |
1029 | 0 | return 0; |
1030 | 0 | } |
1031 | | /* |
1032 | | * We need to release the reference from ossl_provider_find because |
1033 | | * we don't want to keep a reference counted handle to the provider. |
1034 | | * |
1035 | | * The provider unload code checks for the random provider and, |
1036 | | * if present, our reference will be NULLed when it is fully freed. |
1037 | | * The provider load code, conversely, checks the provider name |
1038 | | * and re-hooks our reference if required. This means that a load, |
1039 | | * hook random provider, use, unload, reload, reuse sequence will |
1040 | | * work as expected. |
1041 | | */ |
1042 | 0 | OSSL_PROVIDER_unload(prov); |
1043 | 0 | } else if (!set_random_provider_name(dgbl, cval->value)) |
1044 | 0 | return 0; |
1045 | 0 | #endif |
1046 | 0 | } else { |
1047 | 0 | ERR_raise_data(ERR_LIB_CRYPTO, |
1048 | 0 | CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION, |
1049 | 0 | "name=%s, value=%s", cval->name, cval->value); |
1050 | 0 | r = 0; |
1051 | 0 | } |
1052 | 0 | } |
1053 | 0 | return r; |
1054 | 0 | } |
1055 | | |
1056 | | static void random_conf_deinit(CONF_IMODULE *md) |
1057 | 0 | { |
1058 | 0 | OSSL_TRACE(CONF, "Cleaned up random\n"); |
1059 | 0 | } |
1060 | | |
1061 | | void ossl_random_add_conf_module(void) |
1062 | 0 | { |
1063 | 0 | OSSL_TRACE(CONF, "Adding config module 'random'\n"); |
1064 | 0 | CONF_module_add("random", random_conf_init, random_conf_deinit); |
1065 | 0 | } |
1066 | | |
1067 | | int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq, |
1068 | | const char *cipher, const char *digest) |
1069 | 136 | { |
1070 | 136 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
1071 | | |
1072 | 136 | if (dgbl == NULL) |
1073 | 0 | return 0; |
1074 | 136 | if (dgbl->primary != NULL) { |
1075 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED); |
1076 | 0 | return 0; |
1077 | 0 | } |
1078 | 136 | return random_set_string(&dgbl->rng_name, drbg) |
1079 | 136 | && random_set_string(&dgbl->rng_propq, propq) |
1080 | 136 | && random_set_string(&dgbl->rng_cipher, cipher) |
1081 | 136 | && random_set_string(&dgbl->rng_digest, digest); |
1082 | 136 | } |
1083 | | |
1084 | | int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed, |
1085 | | const char *propq) |
1086 | 0 | { |
1087 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
1088 | |
|
1089 | 0 | if (dgbl == NULL) |
1090 | 0 | return 0; |
1091 | 0 | if (dgbl->seed != NULL) { |
1092 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED); |
1093 | 0 | return 0; |
1094 | 0 | } |
1095 | 0 | return random_set_string(&dgbl->seed_name, seed) |
1096 | 0 | && random_set_string(&dgbl->seed_propq, propq); |
1097 | 0 | } |
1098 | | |
1099 | | int RAND_set1_random_provider(OSSL_LIB_CTX *ctx, OSSL_PROVIDER *prov) |
1100 | 0 | { |
1101 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
1102 | |
|
1103 | 0 | if (dgbl == NULL) |
1104 | 0 | return 0; |
1105 | | |
1106 | 0 | if (prov == NULL) { |
1107 | 0 | OPENSSL_free(dgbl->random_provider_name); |
1108 | 0 | dgbl->random_provider_name = NULL; |
1109 | 0 | dgbl->random_provider = NULL; |
1110 | 0 | return 1; |
1111 | 0 | } |
1112 | | |
1113 | 0 | if (dgbl->random_provider == prov) |
1114 | 0 | return 1; |
1115 | | |
1116 | 0 | if (!set_random_provider_name(dgbl, OSSL_PROVIDER_get0_name(prov))) |
1117 | 0 | return 0; |
1118 | | |
1119 | 0 | dgbl->random_provider = prov; |
1120 | 0 | return 1; |
1121 | 0 | } |
1122 | | |
1123 | | /* |
1124 | | * When a new provider is loaded, we need to check to see if it is the |
1125 | | * designated randomness provider and register it if it is. |
1126 | | */ |
1127 | | int ossl_rand_check_random_provider_on_load(OSSL_LIB_CTX *ctx, |
1128 | | OSSL_PROVIDER *prov) |
1129 | 0 | { |
1130 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
1131 | |
|
1132 | 0 | if (dgbl == NULL) |
1133 | 0 | return 0; |
1134 | | |
1135 | | /* No random provider name specified, or one is installed already */ |
1136 | 0 | if (dgbl->random_provider_name == NULL || dgbl->random_provider != NULL) |
1137 | 0 | return 1; |
1138 | | |
1139 | | /* Does this provider match the name we're using? */ |
1140 | 0 | if (strcmp(dgbl->random_provider_name, OSSL_PROVIDER_get0_name(prov)) != 0) |
1141 | 0 | return 1; |
1142 | | |
1143 | 0 | dgbl->random_provider = prov; |
1144 | 0 | return 1; |
1145 | 0 | } |
1146 | | |
1147 | | /* |
1148 | | * When a provider is being unloaded, if it is the randomness provider, |
1149 | | * we need to deregister it. |
1150 | | */ |
1151 | | int ossl_rand_check_random_provider_on_unload(OSSL_LIB_CTX *ctx, |
1152 | | OSSL_PROVIDER *prov) |
1153 | 0 | { |
1154 | 0 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
1155 | |
|
1156 | 0 | if (dgbl == NULL) |
1157 | 0 | return 0; |
1158 | | |
1159 | 0 | if (dgbl->random_provider == prov) |
1160 | 0 | dgbl->random_provider = NULL; |
1161 | 0 | return 1; |
1162 | 0 | } |
1163 | | |
1164 | | #endif /* !FIPS_MODULE */ |