Skip to content

Commit 8866a9f

Browse files
committed
allow -DSQLCIPHER_OPENSSL_NO_MUTEX_RAND to disable openssl rand mutex
1 parent f3389d2 commit 8866a9f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/crypto_openssl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ static unsigned int openssl_init_count = 0;
4848
static sqlite3_mutex* openssl_rand_mutex = NULL;
4949

5050
static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
51+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
5152
sqlite3_mutex_enter(openssl_rand_mutex);
53+
#endif
5254
RAND_add(buffer, length, 0);
55+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
5356
sqlite3_mutex_leave(openssl_rand_mutex);
57+
#endif
5458
return SQLITE_OK;
5559
}
5660

@@ -78,10 +82,12 @@ static int sqlcipher_openssl_activate(void *ctx) {
7882
OpenSSL_add_all_algorithms();
7983
}
8084

85+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
8186
if(openssl_rand_mutex == NULL) {
8287
/* allocate a mutex to guard against concurrent calls to RAND_bytes() */
8388
openssl_rand_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
8489
}
90+
#endif
8591

8692
openssl_init_count++;
8793
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
@@ -104,8 +110,10 @@ static int sqlcipher_openssl_deactivate(void *ctx) {
104110
"cleaning up" openssl when it was initialized externally by the program */
105111
EVP_cleanup();
106112
}
113+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
107114
sqlite3_mutex_free(openssl_rand_mutex);
108115
openssl_rand_mutex = NULL;
116+
#endif
109117
}
110118
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
111119
return SQLITE_OK;
@@ -124,9 +132,13 @@ static int sqlcipher_openssl_random (void *ctx, void *buffer, int length) {
124132
This is simple workaround to prevent this common crash
125133
but a more proper solution is that applications setup platform-appropriate
126134
thread saftey in openssl externally */
135+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
127136
sqlite3_mutex_enter(openssl_rand_mutex);
137+
#endif
128138
rc = RAND_bytes((unsigned char *)buffer, length);
139+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
129140
sqlite3_mutex_leave(openssl_rand_mutex);
141+
#endif
130142
return (rc == 1) ? SQLITE_OK : SQLITE_ERROR;
131143
}
132144

0 commit comments

Comments
 (0)