Make getting and setting the RAND default method thread safe
[openssl.git] / crypto / rand / rand_lib.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #include <stdio.h>
11 #include <time.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/opensslconf.h>
14 #include "internal/rand.h"
15 #include <openssl/engine.h>
16 #include "internal/thread_once.h"
17
18 #ifndef OPENSSL_NO_ENGINE
19 /* non-NULL if default_RAND_meth is ENGINE-provided */
20 static ENGINE *funct_ref = NULL;
21 static CRYPTO_RWLOCK *rand_engine_lock = NULL;
22 #endif
23 static const RAND_METHOD *default_RAND_meth = NULL;
24 static CRYPTO_RWLOCK *rand_meth_lock = NULL;
25 static CRYPTO_ONCE rand_lock_init = CRYPTO_ONCE_STATIC_INIT;
26
27 DEFINE_RUN_ONCE_STATIC(do_rand_lock_init)
28 {
29 #ifndef OPENSSL_NO_ENGINE
30     rand_engine_lock = CRYPTO_THREAD_lock_new();
31 #endif
32     rand_meth_lock = CRYPTO_THREAD_lock_new();
33     return rand_engine_lock != NULL && rand_meth_lock != NULL;
34 }
35
36 int RAND_set_rand_method(const RAND_METHOD *meth)
37 {
38     if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
39         return 0;
40
41     CRYPTO_THREAD_write_lock(rand_meth_lock);
42 #ifndef OPENSSL_NO_ENGINE
43     ENGINE_finish(funct_ref);
44     funct_ref = NULL;
45 #endif
46     default_RAND_meth = meth;
47     CRYPTO_THREAD_unlock(rand_meth_lock);
48     return 1;
49 }
50
51 const RAND_METHOD *RAND_get_rand_method(void)
52 {
53     const RAND_METHOD *tmp_meth = NULL;
54
55     if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
56         return NULL;
57
58     CRYPTO_THREAD_write_lock(rand_meth_lock);
59     if (!default_RAND_meth) {
60 #ifndef OPENSSL_NO_ENGINE
61         ENGINE *e = ENGINE_get_default_RAND();
62         if (e) {
63             default_RAND_meth = ENGINE_get_RAND(e);
64             if (default_RAND_meth == NULL) {
65                 ENGINE_finish(e);
66                 e = NULL;
67             }
68         }
69         if (e)
70             funct_ref = e;
71         else
72 #endif
73             default_RAND_meth = RAND_OpenSSL();
74     }
75     tmp_meth = default_RAND_meth;
76     CRYPTO_THREAD_unlock(rand_meth_lock);
77     return tmp_meth;
78 }
79
80 #ifndef OPENSSL_NO_ENGINE
81 int RAND_set_rand_engine(ENGINE *engine)
82 {
83     const RAND_METHOD *tmp_meth = NULL;
84
85     if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
86         return 0;
87
88     if (engine) {
89         if (!ENGINE_init(engine))
90             return 0;
91         tmp_meth = ENGINE_get_RAND(engine);
92         if (tmp_meth == NULL) {
93             ENGINE_finish(engine);
94             return 0;
95         }
96     }
97     CRYPTO_THREAD_write_lock(rand_engine_lock);
98     /* This function releases any prior ENGINE so call it first */
99     RAND_set_rand_method(tmp_meth);
100     funct_ref = engine;
101     CRYPTO_THREAD_unlock(rand_engine_lock);
102     return 1;
103 }
104 #endif
105
106 void rand_cleanup_int(void)
107 {
108     const RAND_METHOD *meth = default_RAND_meth;
109     if (meth && meth->cleanup)
110         meth->cleanup();
111     RAND_set_rand_method(NULL);
112     CRYPTO_THREAD_lock_free(rand_meth_lock);
113     CRYPTO_THREAD_lock_free(rand_engine_lock);
114 }
115
116 void RAND_seed(const void *buf, int num)
117 {
118     const RAND_METHOD *meth = RAND_get_rand_method();
119     if (meth && meth->seed)
120         meth->seed(buf, num);
121 }
122
123 void RAND_add(const void *buf, int num, double entropy)
124 {
125     const RAND_METHOD *meth = RAND_get_rand_method();
126     if (meth && meth->add)
127         meth->add(buf, num, entropy);
128 }
129
130 int RAND_bytes(unsigned char *buf, int num)
131 {
132     const RAND_METHOD *meth = RAND_get_rand_method();
133     if (meth && meth->bytes)
134         return meth->bytes(buf, num);
135     return (-1);
136 }
137
138 #if OPENSSL_API_COMPAT < 0x10100000L
139 int RAND_pseudo_bytes(unsigned char *buf, int num)
140 {
141     const RAND_METHOD *meth = RAND_get_rand_method();
142     if (meth && meth->pseudorand)
143         return meth->pseudorand(buf, num);
144     return (-1);
145 }
146 #endif
147
148 int RAND_status(void)
149 {
150     const RAND_METHOD *meth = RAND_get_rand_method();
151     if (meth && meth->status)
152         return meth->status();
153     return 0;
154 }