8000 Merge branch 'prerelease' · githubzhaoliang/sqlcipher@c61663d · GitHub
[go: up one dir, main page]

Skip to content

Commit c61663d

Browse files
committed
Merge branch 'prerelease'
2 parents a1fb0d2 + 3ef19cf commit c61663d

File tree

5 files changed

+105
-38
lines changed

5 files changed

+105
-38
lines changed

README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ an iPhone data vault and password manager (http://getstrip.com).
1818
- Good security practices (CBC mode, key derivation)
1919
- Zero-configuration and application level cryptography
2020
- Algorithms provided by the peer reviewed OpenSSL crypto library.
21+
- Configurable crypto providers
2122

2223
[Compiling]
2324

src/crypto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#define FILE_HEADER_SZ 16
4545

4646
#ifndef CIPHER_VERSION
47-
#define CIPHER_VERSION "2.2.0"
47+
#define CIPHER_VERSION "2.2.1"
4848
#endif
4949

5050
#ifndef CIPHER

src/crypto_impl.c

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ typedef struct {
6666

6767
static unsigned int default_flags = DEFAULT_CIPHER_FLAGS;
6868
static unsigned char hmac_salt_mask = HMAC_SALT_MASK;
69-
69+
static unsigned int sqlcipher_activate_count = 0;
70+
static sqlite3_mutex* sqlcipher_provider_mutex = NULL;
7071
static sqlcipher_provider *default_provider = NULL;
7172

7273
struct codec_ctx {
@@ -81,13 +82,15 @@ struct codec_ctx {
8182
};
8283

8384
int sqlcipher_register_provider(sqlcipher_provider *p) {
85+
sqlite3_mutex_enter(sqlcipher_provider_mutex);
8486
if(default_provider != NULL && default_provider != p) {
8587
/* only free the current registerd provider if it has been initialized
8688
and it isn't a pointer to the same provider passed to the function
8789
(i.e. protect against a caller calling register twice for the same provider) */
8890
sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
8991
}
9092
default_provider = p;
93+
sqlite3_mutex_leave(sqlcipher_provider_mutex);
9194
return SQLITE_OK;
9295
}
9396

@@ -99,35 +102,57 @@ sqlcipher_provider* sqlcipher_get_provider() {
99102
}
100103

101104
void sqlcipher_activate() {
102-
sqlcipher_provider *p;
103-
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
104-
p = sqlcipher_malloc(sizeof(sqlcipher_provider));
105-
{
105+
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
106+
107+
if(sqlcipher_provider_mutex == NULL) {
108+
/* allocate a new mutex to guard access to the provider */
109+
sqlcipher_provider_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
110+
}
111+
112+
/* check to see if there is a provider registered at this point
113+
if there no provider registered at this point, register the
114+
default provider */
115+
if(sqlcipher_get_provider() == NULL) {
116+
sqlcipher_provider *p = sqlcipher_malloc(sizeof(sqlcipher_provider));
106117
#if defined (SQLCIPHER_CRYPTO_CC)
107-
extern int sqlcipher_cc_setup(sqlcipher_provider *p);
108-
sqlcipher_cc_setup(p);
118+
extern int sqlcipher_cc_setup(sqlcipher_provider *p);
119+
sqlcipher_cc_setup(p);
109120
#elif defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT)
110-
extern int sqlcipher_ltc_setup(sqlcipher_provider *p);
111-
sqlcipher_ltc_setup(p);
121+
extern int sqlcipher_ltc_setup(sqlcipher_provider *p);
122+
sqlcipher_ltc_setup(p);
112123
#elif defined (SQLCIPHER_CRYPTO_OPENSSL)
113-
extern int sqlcipher_openssl_setup(sqlcipher_provider *p);
114-
sqlcipher_openssl_setup(p);
124+
extern int sqlcipher_openssl_setup(sqlcipher_provider *p);
125+
sqlcipher_openssl_setup(p);
115126
#else
116127
#error "NO DEFAULT SQLCIPHER CRYPTO PROVIDER DEFINED"
117128
#endif
129+
sqlcipher_register_provider(p);
118130
}
119-
sqlcipher_register_provider(p);
120131

121-
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
132+
sqlcipher_activate_count++; /* increment activation count */
133+
134+
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
122135
}
123136

124137
void sqlcipher_deactivate() {
125-
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
126-
if(default_provider != NULL) {
127-
sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
128-
default_provider = NULL;
138+
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
139+
sqlcipher_activate_count--;
140+
/* if no connections are using sqlcipher, cleanup globals */
141+
if(sqlcipher_activate_count < 1) {
142+
sqlite3_mutex_enter(sqlcipher_provider_mutex);
143+
if(default_provider != NULL) {
144+
sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
145+
default_provider = NULL;
146+
}
147+
sqlite3_mutex_leave(sqlcipher_provider_mutex);
148+
149+
/* last connection closed, free provider mutex*/
150+
sqlite3_mutex_free(sqlcipher_provider_mutex);
151+
sqlcipher_provider_mutex = NULL;
152+
153+
sqlcipher_activate_count = 0; /* reset activation count */
129154
}
130-
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
155+
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
131156
}
132157

133158
/* constant time memset using volitile to avoid having the memset
@@ -235,7 +260,11 @@ static int sqlcipher_cipher_ctx_init(cipher_ctx **iCtx) {
235260

236261
ctx->provider = (sqlcipher_provider *) sqlcipher_malloc(sizeof(sqlcipher_provider));
237262
if(ctx->provider == NULL) return SQLITE_NOMEM;
263+
264+
/* make a copy of the provider to be used for the duration of the context */
265+
sqlite3_mutex_enter(sqlcipher_provider_mutex);
238266
memcpy(ctx->provider, default_provider, sizeof(sqlcipher_provider));
267+
sqlite3_mutex_leave(sqlcipher_provider_mutex);
239268

240269
if((rc = ctx->provider->ctx_init(&ctx->provider_ctx)) != SQLITE_OK) return rc;
241270
ctx->key = (unsigned char *) sqlcipher_malloc(CIPHER_MAX_KEY_SZ);

src/crypto_openssl.c

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,16 @@ typedef struct {
4545

4646
static unsigned int openssl_external_init = 0;
4747
static unsigned int openssl_init_count = 0;
48-
48+
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
52+
sqlite3_mutex_enter(openssl_rand_mutex);
53+
#endif
5154
RAND_add(buffer, length, 0);
55+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
56+
sqlite3_mutex_leave(openssl_rand_mutex);
57+
#endif
5258
return SQLITE_OK;
5359
}
5460

@@ -59,40 +65,57 @@ static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
5965
sqlcipher_openssl_deactivate() will free the EVP structures.
6066
*/
6167
static int sqlcipher_openssl_activate(void *ctx) {
62-
/* we'll initialize openssl and increment the internal init counter
68+
/* initialize openssl and increment the internal init counter
6369
but only if it hasn't been initalized outside of SQLCipher by this program
6470
e.g. on startup */
71+
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
72+
6573
if(openssl_init_count == 0 && EVP_get_cipherbyname(CIPHER) != NULL) {
74+
/* if openssl has not yet been initialized by this library, but
75+
a call to get_cipherbyname works, then the openssl library
76+
has been initialized externally already. */
6677
openssl_external_init = 1;
6778
}
6879

69-
if(openssl_external_init == 0) {
70-
if(openssl_init_count == 0) {
71-
OpenSSL_add_all_algorithms();
72-
}
73-
openssl_init_count++;
80+
if(openssl_init_count == 0 && openssl_external_init == 0) {
81+
/* if the library was not externally initialized, then should be now */
82+
OpenSSL_add_all_algorithms();
7483
}
84+
85+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
86+
if(openssl_rand_mutex == NULL) {
87+
/* allocate a mutex to guard against concurrent calls to RAND_bytes() */
88+
openssl_rand_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
89+
}
90+
#endif
91+
92+
openssl_init_count++;
93+
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
7594
return SQLITE_OK;
7695
}
7796

7897
/* deactivate SQLCipher, most imporantly decremeting the activation count and
7998
freeing the EVP structures on the final deactivation to ensure that
8099
OpenSSL memory is cleaned up */
81100
static int sqlcipher_openssl_deactivate(void *ctx) {
82-
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
83-
/* If it is initialized externally, then the init counter should never be greater than zero.
84-
This should prevent SQLCipher from "cleaning up" openssl
85-
when something else in the program might be using it. */
86-
if(openssl_external_init == 0) {
87-
openssl_init_count--;
88-
/* if the counter reaches zero after it's decremented release EVP memory
101+
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
102+
openssl_init_count--;
103+
104+
if(openssl_init_count == 0) {
105+
if(openssl_external_init == 0) {
106+
/* if OpenSSL hasn't be initialized externally, and the counter reaches zero
107+
after it's decremented, release EVP memory
89108
Note: this code will only be reached if OpensSSL_add_all_algorithms()
90-
is called by SQLCipher internally. */
91-
if(openssl_init_count == 0) {
109+
is called by SQLCipher internally. This should prevent SQLCipher from
110+
"cleaning up" openssl when it was initialized externally by the program */
92111
EVP_cleanup();
93112
}
113+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
114+
sqlite3_mutex_free(openssl_rand_mutex);
115+
openssl_rand_mutex = NULL;
116+
#endif
94117
}
95-
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
118+
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
96119
return SQLITE_OK;
97120
}
98121

@@ -102,7 +125,21 @@ static const char* sqlcipher_openssl_get_provider_name(void *ctx) {
102125

103126
/* generate a defined number of random bytes */
104127
static int sqlcipher_openssl_random (void *ctx, void *buffer, int length) {
105-
return (RAND_bytes((unsigned char *)buffer, length) == 1) ? SQLITE_OK : SQLITE_ERROR;
128+
int rc = 0;
129+
/* concurrent calls to RAND_bytes can cause a crash under some openssl versions when a
130+
naive application doesn't use CRYPTO_set_locking_callback and
131+
CRYPTO_THREADID_set_callback to ensure openssl thread safety.
132+
This is simple workaround to prevent this common crash
133+
but a more proper solution is that applications setup platform-appropriate
134+
thread saftey in openssl externally */
135+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
136+
sqlite3_mutex_enter(openssl_rand_mutex);
137+
#endif
138+
rc = RAND_bytes((unsigned char *)buffer, length);
139+
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
140+
sqlite3_mutex_leave(openssl_rand_mutex);
141+
#endif
142+
return (rc == 1) ? SQLITE_OK : SQLITE_ERROR;
106143
}
107144

108145
static int sqlcipher_openssl_hmac(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out) {

test/crypto.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ do_test verify-pragma-cipher-version {
14181418
execsql {
14191419
PRAGMA cipher_version;
14201420
}
1421-
} {2.2.0}
1421+
} {2.2.1}
14221422
db close
14231423
file delete -force test.db
14241424

0 commit comments

Comments
 (0)
0