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