4040#include <openssl/hmac.h>
4141#include <openssl/err.h>
4242
43- typedef struct {
44- EVP_CIPHER * evp_cipher ;
45- } openssl_ctx ;
46-
47- static unsigned int openssl_external_init = 0 ;
4843static unsigned int openssl_init_count = 0 ;
4944
5045#if (defined(OPENSSL_VERSION_NUMBER ) && OPENSSL_VERSION_NUMBER < 0x10100000L ) || (defined(LIBRESSL_VERSION_NUMBER ) && LIBRESSL_VERSION_NUMBER < 0x20700000L )
@@ -85,7 +80,7 @@ static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
8580 return SQLITE_OK ;
8681}
8782
88- #define OPENSSL_CIPHER "aes-256-cbc"
83+ #define OPENSSL_CIPHER EVP_aes_256_cbc()
8984
9085
9186/* activate and initialize sqlcipher. Most importantly, this will automatically
@@ -103,13 +98,6 @@ static int sqlcipher_openssl_activate(void *ctx) {
10398 sqlite3_mutex_enter (sqlcipher_mutex (SQLCIPHER_MUTEX_PROVIDER_ACTIVATE ));
10499 CODEC_TRACE_MUTEX ("sqlcipher_openssl_activate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE\n" );
105100
106- if (openssl_init_count == 0 && EVP_get_cipherbyname (OPENSSL_CIPHER ) != NULL ) {
107- /* if openssl has not yet been initialized by this library, but
108- a call to get_cipherbyname works, then the openssl library
109- has been initialized externally already. */
110- openssl_external_init = 1 ;
111- }
112-
113101#ifdef SQLCIPHER_FIPS
114102 if (!FIPS_mode ()){
115103 if (!FIPS_mode_set (1 )){
@@ -126,13 +114,6 @@ static int sqlcipher_openssl_activate(void *ctx) {
126114 }
127115#endif
128116
129- if (openssl_init_count == 0 && openssl_external_init == 0 ) {
130- /* if the library was not externally initialized, then should be now */
131- #if (defined(OPENSSL_VERSION_NUMBER ) && OPENSSL_VERSION_NUMBER < 0x10100000L ) || (defined(LIBRESSL_VERSION_NUMBER ) && LIBRESSL_VERSION_NUMBER < 0x20700000L )
132- OpenSSL_add_all_algorithms ();
133- #endif
134- }
135-
136117 openssl_init_count ++ ;
137118 CODEC_TRACE_MUTEX ("sqlcipher_openssl_activate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE\n" );
138119 sqlite3_mutex_leave (sqlcipher_mutex (SQLCIPHER_MUTEX_PROVIDER_ACTIVATE ));
@@ -147,22 +128,8 @@ static int sqlcipher_openssl_deactivate(void *ctx) {
147128 CODEC_TRACE_MUTEX ("sqlcipher_openssl_deactivate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE\n" );
148129 sqlite3_mutex_enter (sqlcipher_mutex (SQLCIPHER_MUTEX_PROVIDER_ACTIVATE ));
149130 CODEC_TRACE_MUTEX ("sqlcipher_openssl_deactivate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE\n" );
150- openssl_init_count -- ;
151131
152- if (openssl_init_count == 0 ) {
153- if (openssl_external_init == 0 ) {
154- /* if OpenSSL hasn't be initialized externally, and the counter reaches zero
155- after it's decremented, release EVP memory
156- Note: this code will only be reached if OpensSSL_add_all_algorithms()
157- is called by SQLCipher internally. This should prevent SQLCipher from
158- "cleaning up" openssl when it was initialized externally by the program */
159- #if (defined(OPENSSL_VERSION_NUMBER ) && OPENSSL_VERSION_NUMBER < 0x10100000L ) || (defined(LIBRESSL_VERSION_NUMBER ) && LIBRESSL_VERSION_NUMBER < 0x20700000L )
160- EVP_cleanup ();
161- #endif
162- } else {
163- openssl_external_init = 0 ;
164- }
165- }
132+ openssl_init_count -- ;
166133
167134 CODEC_TRACE_MUTEX ("sqlcipher_openssl_deactivate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE\n" );
168135 sqlite3_mutex_leave (sqlcipher_mutex (SQLCIPHER_MUTEX_PROVIDER_ACTIVATE ));
@@ -267,7 +234,7 @@ static int sqlcipher_openssl_cipher(void *ctx, int mode, unsigned char *key, int
267234 int tmp_csz , csz , rc = SQLITE_OK ;
268235 EVP_CIPHER_CTX * ectx = EVP_CIPHER_CTX_new ();
269236 if (ectx == NULL ) goto error ;
270- if (!EVP_CipherInit_ex (ectx , (( openssl_ctx * ) ctx ) -> evp_cipher , NULL , NULL , NULL , mode )) goto error ;
237+ if (!EVP_CipherInit_ex (ectx , OPENSSL_CIPHER , NULL , NULL , NULL , mode )) goto error ;
271238 if (!EVP_CIPHER_CTX_set_padding (ectx , 0 )) goto error ; /* no padding */
272239 if (!EVP_CipherInit_ex (ectx , NULL , NULL , key , iv , mode )) goto error ;
273240 if (!EVP_CipherUpdate (ectx , out , & tmp_csz , in , in_sz )) goto error ;
@@ -286,19 +253,19 @@ static int sqlcipher_openssl_cipher(void *ctx, int mode, unsigned char *key, int
286253}
287254
288255static const char * sqlcipher_openssl_get_cipher (void * ctx ) {
289- return OBJ_nid2sn (EVP_CIPHER_nid ((( openssl_ctx * ) ctx ) -> evp_cipher ));
256+ return OBJ_nid2sn (EVP_CIPHER_nid (OPENSSL_CIPHER ));
290257}
291258
292259static int sqlcipher_openssl_get_key_sz (void * ctx ) {
293- return EVP_CIPHER_key_length ((( openssl_ctx * ) ctx ) -> evp_cipher );
260+ return EVP_CIPHER_key_length (OPENSSL_CIPHER );
294261}
295262
296263static int sqlcipher_openssl_get_iv_sz (void * ctx ) {
297- return EVP_CIPHER_iv_length ((( openssl_ctx * ) ctx ) -> evp_cipher );
264+ return EVP_CIPHER_iv_length (OPENSSL_CIPHER );
298265}
299266
300267static int sqlcipher_openssl_get_block_sz (void * ctx ) {
301- return EVP_CIPHER_block_size ((( openssl_ctx * ) ctx ) -> evp_cipher );
268+ return EVP_CIPHER_block_size (OPENSSL_CIPHER );
302269}
303270
304271static int sqlcipher_openssl_get_hmac_sz (void * ctx , int algorithm ) {
@@ -318,21 +285,11 @@ static int sqlcipher_openssl_get_hmac_sz(void *ctx, int algorithm) {
318285}
319286
320287static int sqlcipher_openssl_ctx_init (void * * ctx ) {
321- openssl_ctx * o_ctx ;
322-
323- * ctx = sqlcipher_malloc (sizeof (openssl_ctx ));
324- if (* ctx == NULL ) return SQLITE_NOMEM ;
325- sqlcipher_openssl_activate (* ctx );
326-
327- o_ctx = (openssl_ctx * )* ctx ;
328- o_ctx -> evp_cipher = (EVP_CIPHER * ) EVP_get_cipherbyname (OPENSSL_CIPHER );
329- return o_ctx -> evp_cipher != NULL ? SQLITE_OK : SQLITE_ERROR ;
288+ return sqlcipher_openssl_activate (* ctx );
330289}
331290
332291static int sqlcipher_openssl_ctx_free (void * * ctx ) {
333- sqlcipher_openssl_deactivate (* ctx );
334- sqlcipher_free (* ctx , sizeof (openssl_ctx ));
335- return SQLITE_OK ;
292+ return sqlcipher_openssl_deactivate (NULL );
336293}
337294
338295static int sqlcipher_openssl_fips_status (void * ctx ) {
0 commit comments