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