3 ** http://sqlcipher.net
5 ** Copyright (c) 2008 - 2013, ZETETIC LLC
6 ** All rights reserved.
8 ** Redistribution and use in source and binary forms, with or without
9 ** modification, are permitted provided that the following conditions are met:
10 ** * Redistributions of source code must retain the above copyright
11 ** notice, this list of conditions and the following disclaimer.
12 ** * Redistributions in binary form must reproduce the above copyright
13 ** notice, this list of conditions and the following disclaimer in the
14 ** documentation and/or other materials provided with the distribution.
15 ** * Neither the name of the ZETETIC LLC nor the
16 ** names of its contributors may be used to endorse or promote products
17 ** derived from this software without specific prior written permission.
19 ** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
20 ** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 ** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
23 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifdef SQLITE_HAS_CODEC
35 #include "sqlcipher.h"
39 #include "sqlcipher_ext.h"
42 void sqlcipher_vdbe_return_string(Parse
*pParse
, const char *zLabel
, const char *value
, int value_type
){
43 Vdbe
*v
= sqlite3GetVdbe(pParse
);
44 sqlite3VdbeSetNumCols(v
, 1);
45 sqlite3VdbeSetColName(v
, 0, COLNAME_NAME
, zLabel
, SQLITE_STATIC
);
46 sqlite3VdbeAddOp4(v
, OP_String8
, 0, 1, 0, value
, value_type
);
47 sqlite3VdbeAddOp2(v
, OP_ResultRow
, 1, 1);
50 static int codec_set_btree_to_codec_pagesize(sqlite3
*db
, Db
*pDb
, codec_ctx
*ctx
) {
51 int rc
, page_sz
, reserve_sz
;
53 page_sz
= sqlcipher_codec_ctx_get_pagesize(ctx
);
54 reserve_sz
= sqlcipher_codec_ctx_get_reservesize(ctx
);
56 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize() size=%d reserve=%d", page_sz
, reserve_sz
);
58 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "codec_set_btree_to_codec_pagesize: entering database mutex %p", db
->mutex
);
59 sqlite3_mutex_enter(db
->mutex
);
60 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "codec_set_btree_to_codec_pagesize: entered database mutex %p", db
->mutex
);
61 db
->nextPagesize
= page_sz
;
63 /* before forcing the page size we need to unset the BTS_PAGESIZE_FIXED flag, else
64 sqliteBtreeSetPageSize will block the change */
65 pDb
->pBt
->pBt
->btsFlags
&= ~BTS_PAGESIZE_FIXED
;
66 rc
= sqlite3BtreeSetPageSize(pDb
->pBt
, page_sz
, reserve_sz
, 0);
68 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize returned %d", rc
);
70 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "codec_set_btree_to_codec_pagesize: leaving database mutex %p", db
->mutex
);
71 sqlite3_mutex_leave(db
->mutex
);
72 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "codec_set_btree_to_codec_pagesize: left database mutex %p", db
->mutex
);
77 static int codec_set_pass_key(sqlite3
* db
, int nDb
, const void *zKey
, int nKey
, int for_ctx
) {
78 struct Db
*pDb
= &db
->aDb
[nDb
];
79 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "codec_set_pass_key: db=%p nDb=%d for_ctx=%d", db
, nDb
, for_ctx
);
81 codec_ctx
*ctx
= (codec_ctx
*) sqlcipherPagerGetCodec(pDb
->pBt
->pBt
->pPager
);
84 return sqlcipher_codec_ctx_set_pass(ctx
, zKey
, nKey
, for_ctx
);
86 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "codec_set_pass_key: error ocurred fetching codec from pager on db %d", nDb
);
90 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "codec_set_pass_key: no btree present on db %d", nDb
);
94 int sqlcipher_codec_pragma(sqlite3
* db
, int iDb
, Parse
*pParse
, const char *zLeft
, const char *zRight
) {
95 struct Db
*pDb
= &db
->aDb
[iDb
];
96 codec_ctx
*ctx
= NULL
;
100 ctx
= (codec_ctx
*) sqlcipherPagerGetCodec(pDb
->pBt
->pBt
->pPager
);
103 if(sqlite3_stricmp(zLeft
, "key") !=0 && sqlite3_stricmp(zLeft
, "rekey") != 0) {
104 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipher_codec_pragma: db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p", db
, iDb
, pParse
, zLeft
, zRight
, ctx
);
108 if(sqlcipher_ext_pragma(db
, iDb
, pParse
, zLeft
, zRight
)) {
109 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipher_codec_pragma: PRAGMA handled by sqlcipher_ext_pragma");
112 #ifdef SQLCIPHER_TEST
113 if( sqlite3_stricmp(zLeft
,"cipher_test_on")==0 ){
115 unsigned int flags
= sqlcipher_get_test_flags();
116 if(sqlite3_stricmp(zRight
, "fail_encrypt")==0) {
117 SQLCIPHER_FLAG_SET(flags
,TEST_FAIL_ENCRYPT
);
119 if(sqlite3_stricmp(zRight
, "fail_decrypt")==0) {
120 SQLCIPHER_FLAG_SET(flags
,TEST_FAIL_DECRYPT
);
122 if(sqlite3_stricmp(zRight
, "fail_migrate")==0) {
123 SQLCIPHER_FLAG_SET(flags
,TEST_FAIL_MIGRATE
);
125 sqlcipher_set_test_flags(flags
);
128 if( sqlite3_stricmp(zLeft
,"cipher_test_off")==0 ){
130 unsigned int flags
= sqlcipher_get_test_flags();
131 if(sqlite3_stricmp(zRight
, "fail_encrypt")==0) {
132 SQLCIPHER_FLAG_UNSET(flags
,TEST_FAIL_ENCRYPT
);
134 if(sqlite3_stricmp(zRight
, "fail_decrypt")==0) {
135 SQLCIPHER_FLAG_UNSET(flags
,TEST_FAIL_DECRYPT
);
137 if(sqlite3_stricmp(zRight
, "fail_migrate")==0) {
138 SQLCIPHER_FLAG_UNSET(flags
,TEST_FAIL_MIGRATE
);
140 sqlcipher_set_test_flags(flags
);
143 if( sqlite3_stricmp(zLeft
,"cipher_test")==0 ){
144 char *flags
= sqlite3_mprintf("%u", sqlcipher_get_test_flags());
145 sqlcipher_vdbe_return_string(pParse
, "cipher_test", flags
, P4_DYNAMIC
);
147 if( sqlite3_stricmp(zLeft
,"cipher_test_rand")==0 ){
149 int rand
= atoi(zRight
);
150 sqlcipher_set_test_rand(rand
);
152 char *rand
= sqlite3_mprintf("%d", sqlcipher_get_test_rand());
153 sqlcipher_vdbe_return_string(pParse
, "cipher_test_rand", rand
, P4_DYNAMIC
);
157 if( sqlite3_stricmp(zLeft
, "cipher_fips_status")== 0 && !zRight
){
159 char *fips_mode_status
= sqlite3_mprintf("%d", sqlcipher_codec_fips_status(ctx
));
160 sqlcipher_vdbe_return_string(pParse
, "cipher_fips_status", fips_mode_status
, P4_DYNAMIC
);
163 if( sqlite3_stricmp(zLeft
, "cipher_store_pass")==0 && zRight
) {
165 char *deprecation
= "PRAGMA cipher_store_pass is deprecated, please remove from use";
166 sqlcipher_codec_set_store_pass(ctx
, sqlite3GetBoolean(zRight
, 1));
167 sqlcipher_vdbe_return_string(pParse
, "cipher_store_pass", deprecation
, P4_TRANSIENT
);
168 sqlite3_log(SQLITE_WARNING
, deprecation
);
171 if( sqlite3_stricmp(zLeft
, "cipher_store_pass")==0 && !zRight
) {
173 char *store_pass_value
= sqlite3_mprintf("%d", sqlcipher_codec_get_store_pass(ctx
));
174 sqlcipher_vdbe_return_string(pParse
, "cipher_store_pass", store_pass_value
, P4_DYNAMIC
);
177 if( sqlite3_stricmp(zLeft
, "cipher_profile")== 0 && zRight
){
178 char *profile_status
= sqlite3_mprintf("%d", sqlcipher_cipher_profile(db
, zRight
));
179 sqlcipher_vdbe_return_string(pParse
, "cipher_profile", profile_status
, P4_DYNAMIC
);
181 if( sqlite3_stricmp(zLeft
, "cipher_add_random")==0 && zRight
){
183 char *add_random_status
= sqlite3_mprintf("%d", sqlcipher_codec_add_random(ctx
, zRight
, sqlite3Strlen30(zRight
)));
184 sqlcipher_vdbe_return_string(pParse
, "cipher_add_random", add_random_status
, P4_DYNAMIC
);
187 if( sqlite3_stricmp(zLeft
, "cipher_migrate")==0 && !zRight
){
189 int status
= sqlcipher_codec_ctx_migrate(ctx
);
190 char *migrate_status
= sqlite3_mprintf("%d", status
);
191 sqlcipher_vdbe_return_string(pParse
, "cipher_migrate", migrate_status
, P4_DYNAMIC
);
192 if(status
!= SQLITE_OK
) {
193 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlcipher_codec_pragma: error occurred during cipher_migrate: %d", status
);
194 sqlcipher_codec_ctx_set_error(ctx
, status
);
198 if( sqlite3_stricmp(zLeft
, "cipher_provider")==0 && !zRight
){
199 if(ctx
) { sqlcipher_vdbe_return_string(pParse
, "cipher_provider",
200 sqlcipher_codec_get_cipher_provider(ctx
), P4_TRANSIENT
);
203 if( sqlite3_stricmp(zLeft
, "cipher_provider_version")==0 && !zRight
){
204 if(ctx
) { sqlcipher_vdbe_return_string(pParse
, "cipher_provider_version",
205 sqlcipher_codec_get_provider_version(ctx
), P4_TRANSIENT
);
208 if( sqlite3_stricmp(zLeft
, "cipher_version")==0 && !zRight
){
209 sqlcipher_vdbe_return_string(pParse
, "cipher_version", sqlcipher_version(), P4_DYNAMIC
);
211 if( sqlite3_stricmp(zLeft
, "cipher")==0 ){
214 const char* message
= "PRAGMA cipher is no longer supported.";
215 sqlcipher_vdbe_return_string(pParse
, "cipher", message
, P4_TRANSIENT
);
216 sqlite3_log(SQLITE_WARNING
, message
);
218 sqlcipher_vdbe_return_string(pParse
, "cipher", sqlcipher_codec_ctx_get_cipher(ctx
), P4_TRANSIENT
);
222 if( sqlite3_stricmp(zLeft
, "rekey_cipher")==0 && zRight
){
223 const char* message
= "PRAGMA rekey_cipher is no longer supported.";
224 sqlcipher_vdbe_return_string(pParse
, "rekey_cipher", message
, P4_TRANSIENT
);
225 sqlite3_log(SQLITE_WARNING
, message
);
227 if( sqlite3_stricmp(zLeft
,"cipher_default_kdf_iter")==0 ){
229 sqlcipher_set_default_kdf_iter(atoi(zRight
)); /* change default KDF iterations */
231 char *kdf_iter
= sqlite3_mprintf("%d", sqlcipher_get_default_kdf_iter());
232 sqlcipher_vdbe_return_string(pParse
, "cipher_default_kdf_iter", kdf_iter
, P4_DYNAMIC
);
235 if( sqlite3_stricmp(zLeft
, "kdf_iter")==0 ){
238 sqlcipher_codec_ctx_set_kdf_iter(ctx
, atoi(zRight
)); /* change of RW PBKDF2 iteration */
240 char *kdf_iter
= sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_kdf_iter(ctx
));
241 sqlcipher_vdbe_return_string(pParse
, "kdf_iter", kdf_iter
, P4_DYNAMIC
);
245 if( sqlite3_stricmp(zLeft
, "fast_kdf_iter")==0){
248 char *deprecation
= "PRAGMA fast_kdf_iter is deprecated, please remove from use";
249 sqlcipher_codec_ctx_set_fast_kdf_iter(ctx
, atoi(zRight
)); /* change of RW PBKDF2 iteration */
250 sqlcipher_vdbe_return_string(pParse
, "fast_kdf_iter", deprecation
, P4_TRANSIENT
);
251 sqlite3_log(SQLITE_WARNING
, deprecation
);
253 char *fast_kdf_iter
= sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_fast_kdf_iter(ctx
));
254 sqlcipher_vdbe_return_string(pParse
, "fast_kdf_iter", fast_kdf_iter
, P4_DYNAMIC
);
258 if( sqlite3_stricmp(zLeft
, "rekey_kdf_iter")==0 && zRight
){
259 const char* message
= "PRAGMA rekey_kdf_iter is no longer supported.";
260 sqlcipher_vdbe_return_string(pParse
, "rekey_kdf_iter", message
, P4_TRANSIENT
);
261 sqlite3_log(SQLITE_WARNING
, message
);
263 if( sqlite3_stricmp(zLeft
,"page_size")==0 || sqlite3_stricmp(zLeft
,"cipher_page_size")==0 ){
264 /* PRAGMA cipher_page_size will alter the size of the database pages while ensuring that the
265 required reserve space is allocated at the end of each page. This will also override the
266 standard SQLite PRAGMA page_size behavior if a codec context is attached to the database handle.
267 If PRAGMA page_size is invoked but a codec context is not attached (i.e. dealing with a standard
268 unencrypted database) then return early and allow the standard PRAGMA page_size logic to apply. */
271 int size
= atoi(zRight
);
272 rc
= sqlcipher_codec_ctx_set_pagesize(ctx
, size
);
273 if(rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, rc
);
274 rc
= codec_set_btree_to_codec_pagesize(db
, pDb
, ctx
);
275 if(rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, rc
);
277 char * page_size
= sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_pagesize(ctx
));
278 sqlcipher_vdbe_return_string(pParse
, "cipher_page_size", page_size
, P4_DYNAMIC
);
281 return 0; /* return early so that the PragTyp_PAGE_SIZE case logic in pragma.c will take effect */
284 if( sqlite3_stricmp(zLeft
,"cipher_default_page_size")==0 ){
286 sqlcipher_set_default_pagesize(atoi(zRight
));
288 char *default_page_size
= sqlite3_mprintf("%d", sqlcipher_get_default_pagesize());
289 sqlcipher_vdbe_return_string(pParse
, "cipher_default_page_size", default_page_size
, P4_DYNAMIC
);
292 if( sqlite3_stricmp(zLeft
,"cipher_default_use_hmac")==0 ){
294 sqlcipher_set_default_use_hmac(sqlite3GetBoolean(zRight
,1));
296 char *default_use_hmac
= sqlite3_mprintf("%d", sqlcipher_get_default_use_hmac());
297 sqlcipher_vdbe_return_string(pParse
, "cipher_default_use_hmac", default_use_hmac
, P4_DYNAMIC
);
300 if( sqlite3_stricmp(zLeft
,"cipher_use_hmac")==0 ){
303 rc
= sqlcipher_codec_ctx_set_use_hmac(ctx
, sqlite3GetBoolean(zRight
,1));
304 if(rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, rc
);
305 /* since the use of hmac has changed, the page size may also change */
306 rc
= codec_set_btree_to_codec_pagesize(db
, pDb
, ctx
);
307 if(rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, rc
);
309 char *hmac_flag
= sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_use_hmac(ctx
));
310 sqlcipher_vdbe_return_string(pParse
, "cipher_use_hmac", hmac_flag
, P4_DYNAMIC
);
314 if( sqlite3_stricmp(zLeft
,"cipher_hmac_pgno")==0 ){
317 char *deprecation
= "PRAGMA cipher_hmac_pgno is deprecated, please remove from use";
318 /* clear both pgno endian flags */
319 if(sqlite3_stricmp(zRight
, "le") == 0) {
320 SQLCIPHER_FLAG_UNSET(ctx
->flags
, CIPHER_FLAG_BE_PGNO
);
321 SQLCIPHER_FLAG_SET(ctx
->flags
, CIPHER_FLAG_LE_PGNO
);
322 } else if(sqlite3_stricmp(zRight
, "be") == 0) {
323 SQLCIPHER_FLAG_UNSET(ctx
->flags
, CIPHER_FLAG_LE_PGNO
);
324 SQLCIPHER_FLAG_SET(ctx
->flags
, CIPHER_FLAG_BE_PGNO
);
325 } else if(sqlite3_stricmp(zRight
, "native") == 0) {
326 SQLCIPHER_FLAG_UNSET(ctx
->flags
, CIPHER_FLAG_LE_PGNO
);
327 SQLCIPHER_FLAG_UNSET(ctx
->flags
, CIPHER_FLAG_BE_PGNO
);
329 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_pgno", deprecation
, P4_TRANSIENT
);
330 sqlite3_log(SQLITE_WARNING
, deprecation
);
333 if(SQLCIPHER_FLAG_GET(ctx
->flags
, CIPHER_FLAG_LE_PGNO
)) {
334 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_pgno", "le", P4_TRANSIENT
);
335 } else if(SQLCIPHER_FLAG_GET(ctx
->flags
, CIPHER_FLAG_BE_PGNO
)) {
336 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_pgno", "be", P4_TRANSIENT
);
338 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_pgno", "native", P4_TRANSIENT
);
343 if( sqlite3_stricmp(zLeft
,"cipher_hmac_salt_mask")==0 ){
346 char *deprecation
= "PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use";
347 if (sqlite3StrNICmp(zRight
,"x'", 2) == 0 && sqlite3Strlen30(zRight
) == 5) {
348 unsigned char mask
= 0;
349 const unsigned char *hex
= (const unsigned char *)zRight
+2;
350 cipher_hex2bin(hex
,2,&mask
);
351 sqlcipher_set_hmac_salt_mask(mask
);
353 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_salt_mask", deprecation
, P4_TRANSIENT
);
354 sqlite3_log(SQLITE_WARNING
, deprecation
);
356 char *hmac_salt_mask
= sqlite3_mprintf("%02x", sqlcipher_get_hmac_salt_mask());
357 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_salt_mask", hmac_salt_mask
, P4_DYNAMIC
);
361 if( sqlite3_stricmp(zLeft
,"cipher_plaintext_header_size")==0 ){
364 int size
= atoi(zRight
);
365 /* deliberately ignore result code, if size is invalid it will be set to -1
366 and trip the error later in the codec */
367 sqlcipher_codec_ctx_set_plaintext_header_size(ctx
, size
);
369 char *size
= sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_plaintext_header_size(ctx
));
370 sqlcipher_vdbe_return_string(pParse
, "cipher_plaintext_header_size", size
, P4_DYNAMIC
);
374 if( sqlite3_stricmp(zLeft
,"cipher_default_plaintext_header_size")==0 ){
376 sqlcipher_set_default_plaintext_header_size(atoi(zRight
));
378 char *size
= sqlite3_mprintf("%d", sqlcipher_get_default_plaintext_header_size());
379 sqlcipher_vdbe_return_string(pParse
, "cipher_default_plaintext_header_size", size
, P4_DYNAMIC
);
382 if( sqlite3_stricmp(zLeft
,"cipher_salt")==0 ){
385 if (sqlite3StrNICmp(zRight
,"x'", 2) == 0 && sqlite3Strlen30(zRight
) == (FILE_HEADER_SZ
*2)+3) {
386 unsigned char *salt
= (unsigned char*) sqlite3_malloc(FILE_HEADER_SZ
);
387 const unsigned char *hex
= (const unsigned char *)zRight
+2;
388 cipher_hex2bin(hex
,FILE_HEADER_SZ
*2,salt
);
389 sqlcipher_codec_ctx_set_kdf_salt(ctx
, salt
, FILE_HEADER_SZ
);
394 char *hexsalt
= (char*) sqlite3_malloc((FILE_HEADER_SZ
*2)+1);
395 if((rc
= sqlcipher_codec_ctx_get_kdf_salt(ctx
, &salt
)) == SQLITE_OK
) {
396 cipher_bin2hex(salt
, FILE_HEADER_SZ
, hexsalt
);
397 sqlcipher_vdbe_return_string(pParse
, "cipher_salt", hexsalt
, P4_DYNAMIC
);
399 sqlite3_free(hexsalt
);
400 sqlcipher_codec_ctx_set_error(ctx
, rc
);
405 if( sqlite3_stricmp(zLeft
,"cipher_hmac_algorithm")==0 ){
409 if(sqlite3_stricmp(zRight
, SQLCIPHER_HMAC_SHA1_LABEL
) == 0) {
410 rc
= sqlcipher_codec_ctx_set_hmac_algorithm(ctx
, SQLCIPHER_HMAC_SHA1
);
411 } else if(sqlite3_stricmp(zRight
, SQLCIPHER_HMAC_SHA256_LABEL
) == 0) {
412 rc
= sqlcipher_codec_ctx_set_hmac_algorithm(ctx
, SQLCIPHER_HMAC_SHA256
);
413 } else if(sqlite3_stricmp(zRight
, SQLCIPHER_HMAC_SHA512_LABEL
) == 0) {
414 rc
= sqlcipher_codec_ctx_set_hmac_algorithm(ctx
, SQLCIPHER_HMAC_SHA512
);
416 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
417 rc
= codec_set_btree_to_codec_pagesize(db
, pDb
, ctx
);
418 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
420 int algorithm
= sqlcipher_codec_ctx_get_hmac_algorithm(ctx
);
421 if(algorithm
== SQLCIPHER_HMAC_SHA1
) {
422 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL
, P4_TRANSIENT
);
423 } else if(algorithm
== SQLCIPHER_HMAC_SHA256
) {
424 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL
, P4_TRANSIENT
);
425 } else if(algorithm
== SQLCIPHER_HMAC_SHA512
) {
426 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL
, P4_TRANSIENT
);
431 if( sqlite3_stricmp(zLeft
,"cipher_default_hmac_algorithm")==0 ){
434 if(sqlite3_stricmp(zRight
, SQLCIPHER_HMAC_SHA1_LABEL
) == 0) {
435 rc
= sqlcipher_set_default_hmac_algorithm(SQLCIPHER_HMAC_SHA1
);
436 } else if(sqlite3_stricmp(zRight
, SQLCIPHER_HMAC_SHA256_LABEL
) == 0) {
437 rc
= sqlcipher_set_default_hmac_algorithm(SQLCIPHER_HMAC_SHA256
);
438 } else if(sqlite3_stricmp(zRight
, SQLCIPHER_HMAC_SHA512_LABEL
) == 0) {
439 rc
= sqlcipher_set_default_hmac_algorithm(SQLCIPHER_HMAC_SHA512
);
442 int algorithm
= sqlcipher_get_default_hmac_algorithm();
443 if(algorithm
== SQLCIPHER_HMAC_SHA1
) {
444 sqlcipher_vdbe_return_string(pParse
, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL
, P4_TRANSIENT
);
445 } else if(algorithm
== SQLCIPHER_HMAC_SHA256
) {
446 sqlcipher_vdbe_return_string(pParse
, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL
, P4_TRANSIENT
);
447 } else if(algorithm
== SQLCIPHER_HMAC_SHA512
) {
448 sqlcipher_vdbe_return_string(pParse
, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL
, P4_TRANSIENT
);
452 if( sqlite3_stricmp(zLeft
,"cipher_kdf_algorithm")==0 ){
456 if(sqlite3_stricmp(zRight
, SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL
) == 0) {
457 rc
= sqlcipher_codec_ctx_set_kdf_algorithm(ctx
, SQLCIPHER_PBKDF2_HMAC_SHA1
);
458 } else if(sqlite3_stricmp(zRight
, SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL
) == 0) {
459 rc
= sqlcipher_codec_ctx_set_kdf_algorithm(ctx
, SQLCIPHER_PBKDF2_HMAC_SHA256
);
460 } else if(sqlite3_stricmp(zRight
, SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL
) == 0) {
461 rc
= sqlcipher_codec_ctx_set_kdf_algorithm(ctx
, SQLCIPHER_PBKDF2_HMAC_SHA512
);
463 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
465 int algorithm
= sqlcipher_codec_ctx_get_kdf_algorithm(ctx
);
466 if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA1
) {
467 sqlcipher_vdbe_return_string(pParse
, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL
, P4_TRANSIENT
);
468 } else if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA256
) {
469 sqlcipher_vdbe_return_string(pParse
, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL
, P4_TRANSIENT
);
470 } else if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA512
) {
471 sqlcipher_vdbe_return_string(pParse
, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL
, P4_TRANSIENT
);
476 if( sqlite3_stricmp(zLeft
,"cipher_default_kdf_algorithm")==0 ){
479 if(sqlite3_stricmp(zRight
, SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL
) == 0) {
480 rc
= sqlcipher_set_default_kdf_algorithm(SQLCIPHER_PBKDF2_HMAC_SHA1
);
481 } else if(sqlite3_stricmp(zRight
, SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL
) == 0) {
482 rc
= sqlcipher_set_default_kdf_algorithm(SQLCIPHER_PBKDF2_HMAC_SHA256
);
483 } else if(sqlite3_stricmp(zRight
, SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL
) == 0) {
484 rc
= sqlcipher_set_default_kdf_algorithm(SQLCIPHER_PBKDF2_HMAC_SHA512
);
487 int algorithm
= sqlcipher_get_default_kdf_algorithm();
488 if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA1
) {
489 sqlcipher_vdbe_return_string(pParse
, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL
, P4_TRANSIENT
);
490 } else if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA256
) {
491 sqlcipher_vdbe_return_string(pParse
, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL
, P4_TRANSIENT
);
492 } else if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA512
) {
493 sqlcipher_vdbe_return_string(pParse
, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL
, P4_TRANSIENT
);
497 if( sqlite3_stricmp(zLeft
,"cipher_compatibility")==0 ){
500 int version
= atoi(zRight
);
504 rc
= sqlcipher_codec_ctx_set_pagesize(ctx
, 1024);
505 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
506 rc
= sqlcipher_codec_ctx_set_hmac_algorithm(ctx
, SQLCIPHER_HMAC_SHA1
);
507 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
508 rc
= sqlcipher_codec_ctx_set_kdf_algorithm(ctx
, SQLCIPHER_PBKDF2_HMAC_SHA1
);
509 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
510 rc
= sqlcipher_codec_ctx_set_kdf_iter(ctx
, 4000);
511 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
512 rc
= sqlcipher_codec_ctx_set_use_hmac(ctx
, 0);
513 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
517 rc
= sqlcipher_codec_ctx_set_pagesize(ctx
, 1024);
518 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
519 rc
= sqlcipher_codec_ctx_set_hmac_algorithm(ctx
, SQLCIPHER_HMAC_SHA1
);
520 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
521 rc
= sqlcipher_codec_ctx_set_kdf_algorithm(ctx
, SQLCIPHER_PBKDF2_HMAC_SHA1
);
522 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
523 rc
= sqlcipher_codec_ctx_set_kdf_iter(ctx
, 4000);
524 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
525 rc
= sqlcipher_codec_ctx_set_use_hmac(ctx
, 1);
526 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
530 rc
= sqlcipher_codec_ctx_set_pagesize(ctx
, 1024);
531 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
532 rc
= sqlcipher_codec_ctx_set_hmac_algorithm(ctx
, SQLCIPHER_HMAC_SHA1
);
533 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
534 rc
= sqlcipher_codec_ctx_set_kdf_algorithm(ctx
, SQLCIPHER_PBKDF2_HMAC_SHA1
);
535 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
536 rc
= sqlcipher_codec_ctx_set_kdf_iter(ctx
, 64000);
537 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
538 rc
= sqlcipher_codec_ctx_set_use_hmac(ctx
, 1);
539 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
543 rc
= sqlcipher_codec_ctx_set_pagesize(ctx
, 4096);
544 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
545 rc
= sqlcipher_codec_ctx_set_hmac_algorithm(ctx
, SQLCIPHER_HMAC_SHA512
);
546 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
547 rc
= sqlcipher_codec_ctx_set_kdf_algorithm(ctx
, SQLCIPHER_PBKDF2_HMAC_SHA512
);
548 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
549 rc
= sqlcipher_codec_ctx_set_kdf_iter(ctx
, 256000);
550 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
551 rc
= sqlcipher_codec_ctx_set_use_hmac(ctx
, 1);
552 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
556 rc
= codec_set_btree_to_codec_pagesize(db
, pDb
, ctx
);
557 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
561 if( sqlite3_stricmp(zLeft
,"cipher_default_compatibility")==0 ){
563 int version
= atoi(zRight
);
566 sqlcipher_set_default_pagesize(1024);
567 sqlcipher_set_default_hmac_algorithm(SQLCIPHER_HMAC_SHA1
);
568 sqlcipher_set_default_kdf_algorithm(SQLCIPHER_PBKDF2_HMAC_SHA1
);
569 sqlcipher_set_default_kdf_iter(4000);
570 sqlcipher_set_default_use_hmac(0);
574 sqlcipher_set_default_pagesize(1024);
575 sqlcipher_set_default_hmac_algorithm(SQLCIPHER_HMAC_SHA1
);
576 sqlcipher_set_default_kdf_algorithm(SQLCIPHER_PBKDF2_HMAC_SHA1
);
577 sqlcipher_set_default_kdf_iter(4000);
578 sqlcipher_set_default_use_hmac(1);
582 sqlcipher_set_default_pagesize(1024);
583 sqlcipher_set_default_hmac_algorithm(SQLCIPHER_HMAC_SHA1
);
584 sqlcipher_set_default_kdf_algorithm(SQLCIPHER_PBKDF2_HMAC_SHA1
);
585 sqlcipher_set_default_kdf_iter(64000);
586 sqlcipher_set_default_use_hmac(1);
590 sqlcipher_set_default_pagesize(4096);
591 sqlcipher_set_default_hmac_algorithm(SQLCIPHER_HMAC_SHA512
);
592 sqlcipher_set_default_kdf_algorithm(SQLCIPHER_PBKDF2_HMAC_SHA512
);
593 sqlcipher_set_default_kdf_iter(256000);
594 sqlcipher_set_default_use_hmac(1);
599 if( sqlite3_stricmp(zLeft
,"cipher_memory_security")==0 ){
601 sqlcipher_set_mem_security(sqlite3GetBoolean(zRight
,1));
603 char *on
= sqlite3_mprintf("%d", sqlcipher_get_mem_security());
604 sqlcipher_vdbe_return_string(pParse
, "cipher_memory_security", on
, P4_DYNAMIC
);
607 if( sqlite3_stricmp(zLeft
,"cipher_settings")==0 ){
612 pragma
= sqlite3_mprintf("PRAGMA kdf_iter = %d;", sqlcipher_codec_ctx_get_kdf_iter(ctx
));
613 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
615 pragma
= sqlite3_mprintf("PRAGMA cipher_page_size = %d;", sqlcipher_codec_ctx_get_pagesize(ctx
));
616 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
618 pragma
= sqlite3_mprintf("PRAGMA cipher_use_hmac = %d;", sqlcipher_codec_ctx_get_use_hmac(ctx
));
619 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
621 pragma
= sqlite3_mprintf("PRAGMA cipher_plaintext_header_size = %d;", sqlcipher_codec_ctx_get_plaintext_header_size(ctx
));
622 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
624 algorithm
= sqlcipher_codec_ctx_get_hmac_algorithm(ctx
);
626 if(algorithm
== SQLCIPHER_HMAC_SHA1
) {
627 pragma
= sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA1_LABEL
);
628 } else if(algorithm
== SQLCIPHER_HMAC_SHA256
) {
629 pragma
= sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA256_LABEL
);
630 } else if(algorithm
== SQLCIPHER_HMAC_SHA512
) {
631 pragma
= sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA512_LABEL
);
633 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
635 algorithm
= sqlcipher_codec_ctx_get_kdf_algorithm(ctx
);
637 if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA1
) {
638 pragma
= sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL
);
639 } else if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA256
) {
640 pragma
= sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL
);
641 } else if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA512
) {
642 pragma
= sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL
);
644 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
648 if( sqlite3_stricmp(zLeft
,"cipher_default_settings")==0 ){
652 pragma
= sqlite3_mprintf("PRAGMA cipher_default_kdf_iter = %d;", sqlcipher_get_default_kdf_iter());
653 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
655 pragma
= sqlite3_mprintf("PRAGMA cipher_default_page_size = %d;", sqlcipher_get_default_pagesize());
656 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
658 pragma
= sqlite3_mprintf("PRAGMA cipher_default_use_hmac = %d;", sqlcipher_get_default_use_hmac());
659 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
661 pragma
= sqlite3_mprintf("PRAGMA cipher_default_plaintext_header_size = %d;", sqlcipher_get_default_plaintext_header_size());
662 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
664 algorithm
= sqlcipher_get_default_hmac_algorithm();
666 if(algorithm
== SQLCIPHER_HMAC_SHA1
) {
667 pragma
= sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA1_LABEL
);
668 } else if(algorithm
== SQLCIPHER_HMAC_SHA256
) {
669 pragma
= sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA256_LABEL
);
670 } else if(algorithm
== SQLCIPHER_HMAC_SHA512
) {
671 pragma
= sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA512_LABEL
);
673 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
675 algorithm
= sqlcipher_get_default_kdf_algorithm();
677 if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA1
) {
678 pragma
= sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL
);
679 } else if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA256
) {
680 pragma
= sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL
);
681 } else if(algorithm
== SQLCIPHER_PBKDF2_HMAC_SHA512
) {
682 pragma
= sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL
);
684 sqlcipher_vdbe_return_string(pParse
, "pragma", pragma
, P4_DYNAMIC
);
686 if( sqlite3_stricmp(zLeft
,"cipher_integrity_check")==0 ){
688 sqlcipher_codec_ctx_integrity_check(ctx
, pParse
, "cipher_integrity_check");
691 if( sqlite3_stricmp(zLeft
, "cipher_log_level")==0 && zRight
){
692 unsigned int level
= SQLCIPHER_LOG_NONE
;
693 if(sqlite3_stricmp(zRight
, "ERROR")==0) level
= SQLCIPHER_LOG_ERROR
;
694 else if(sqlite3_stricmp(zRight
, "WARN" )==0) level
= SQLCIPHER_LOG_WARN
;
695 else if(sqlite3_stricmp(zRight
, "INFO" )==0) level
= SQLCIPHER_LOG_INFO
;
696 else if(sqlite3_stricmp(zRight
, "DEBUG")==0) level
= SQLCIPHER_LOG_DEBUG
;
697 else if(sqlite3_stricmp(zRight
, "TRACE")==0) level
= SQLCIPHER_LOG_TRACE
;
698 sqlcipher_set_log_level(level
);
699 sqlcipher_vdbe_return_string(pParse
, "cipher_log_level", sqlite3_mprintf("%u", level
), P4_DYNAMIC
);
701 if( sqlite3_stricmp(zLeft
, "cipher_log_subsystem")==0 && zRight
){
702 unsigned int subsys
= SQLCIPHER_LOG_ALL
;
703 if(sqlite3_stricmp(zRight
, "NONE" )==0) subsys
= SQLCIPHER_LOG_NONE
;
704 else if(sqlite3_stricmp(zRight
, "ALL" )==0) subsys
= SQLCIPHER_LOG_ALL
;
705 else if(sqlite3_stricmp(zRight
, "CORE" )==0) subsys
= SQLCIPHER_LOG_CORE
;
706 else if(sqlite3_stricmp(zRight
, "MEMORY" )==0) subsys
= SQLCIPHER_LOG_MEMORY
;
707 else if(sqlite3_stricmp(zRight
, "MUTEX" )==0) subsys
= SQLCIPHER_LOG_MUTEX
;
708 else if(sqlite3_stricmp(zRight
, "PROVIDER")==0) subsys
= SQLCIPHER_LOG_PROVIDER
;
709 sqlcipher_set_log_subsystem(subsys
);
710 sqlcipher_vdbe_return_string(pParse
, "cipher_log_subsystem", sqlite3_mprintf("%u", subsys
), P4_DYNAMIC
);
712 if( sqlite3_stricmp(zLeft
, "cipher_log")== 0 && zRight
){
713 char *status
= sqlite3_mprintf("%d", sqlcipher_set_log(zRight
));
714 sqlcipher_vdbe_return_string(pParse
, "cipher_log", status
, P4_DYNAMIC
);
721 /* these constants are used internally within SQLite's pager.c to differentiate between
722 operations on the main database or journal pages. This is important in the context
723 of a rekey operations, where the journal must be written using the original key
724 material (to allow a transactional rollback), while the new database pages are being
725 written with the new key material*/
726 #define CODEC_READ_OP 3
727 #define CODEC_WRITE_OP 6
728 #define CODEC_JOURNAL_OP 7
731 * sqlite3Codec can be called in multiple modes.
732 * encrypt mode - expected to return a pointer to the
733 * encrypted data without altering pData.
734 * decrypt mode - expected to return a pointer to pData, with
735 * the data decrypted in the input buffer
737 static void* sqlite3Codec(void *iCtx
, void *data
, Pgno pgno
, int mode
) {
738 codec_ctx
*ctx
= (codec_ctx
*) iCtx
;
739 int offset
= 0, rc
= 0;
740 int page_sz
= sqlcipher_codec_ctx_get_pagesize(ctx
);
741 unsigned char *pData
= (unsigned char *) data
;
742 void *buffer
= sqlcipher_codec_ctx_get_data(ctx
);
743 int plaintext_header_sz
= sqlcipher_codec_ctx_get_plaintext_header_size(ctx
);
744 int cctx
= CIPHER_READ_CTX
;
746 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlite3Codec: pgno=%d, mode=%d, page_sz=%d", pgno
, mode
, page_sz
);
749 if(sqlcipher_license_check(ctx
) != SQLITE_OK
) return NULL
;
752 /* call to derive keys if not present yet */
753 if((rc
= sqlcipher_codec_key_derive(ctx
)) != SQLITE_OK
) {
754 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3Codec: error occurred during key derivation: %d", rc
);
755 sqlcipher_codec_ctx_set_error(ctx
, rc
);
759 /* if the plaintext_header_size is negative that means an invalid size was set via
760 PRAGMA. We can't set the error state on the pager at that point because the pager
761 may not be open yet. However, this is a fatal error state, so abort the codec */
762 if(plaintext_header_sz
< 0) {
763 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3Codec: error invalid plaintext_header_sz: %d", plaintext_header_sz
);
764 sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
768 if(pgno
== 1) /* adjust starting pointers in data page for header offset on first page*/
769 offset
= plaintext_header_sz
? plaintext_header_sz
: FILE_HEADER_SZ
;
772 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlite3Codec: switch mode=%d offset=%d", mode
, offset
);
774 case CODEC_READ_OP
: /* decrypt */
775 if(pgno
== 1) /* copy initial part of file header or SQLite magic to buffer */
776 memcpy(buffer
, plaintext_header_sz
? pData
: (void *) SQLITE_FILE_HEADER
, offset
);
778 rc
= sqlcipher_page_cipher(ctx
, cctx
, pgno
, CIPHER_DECRYPT
, page_sz
- offset
, pData
+ offset
, (unsigned char*)buffer
+ offset
);
779 #ifdef SQLCIPHER_TEST
780 if((sqlcipher_get_test_flags() & TEST_FAIL_DECRYPT
) > 0 && sqlcipher_get_test_fail()) {
782 sqlcipher_log(SQLCIPHER_LOG_WARN
, SQLCIPHER_LOG_CORE
, "sqlite3Codec: simulating decryption failure for pgno=%d, mode=%d, page_sz=%d\n", pgno
, mode
, page_sz
);
785 if(rc
!= SQLITE_OK
) {
786 /* failure to decrypt a page is considered a permanent error and will render the pager unusable
787 in order to prevent inconsistent data being loaded into page cache */
788 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3Codec: error decrypting page %d data: %d", pgno
, rc
);
789 sqlcipher_memset((unsigned char*) buffer
+offset
, 0, page_sz
-offset
);
790 sqlcipher_codec_ctx_set_error(ctx
, rc
);
792 SQLCIPHER_FLAG_SET(ctx
->flags
, CIPHER_FLAG_KEY_USED
);
794 memcpy(pData
, buffer
, page_sz
); /* copy buffer data back to pData and return */
798 case CODEC_WRITE_OP
: /* encrypt database page, operate on write context and fall through to case 7, so the write context is used*/
799 cctx
= CIPHER_WRITE_CTX
;
801 case CODEC_JOURNAL_OP
: /* encrypt journal page, operate on read context use to get the original page data from the database */
802 if(pgno
== 1) { /* copy initial part of file header or salt to buffer */
803 void *kdf_salt
= NULL
;
804 /* retrieve the kdf salt */
805 if((rc
= sqlcipher_codec_ctx_get_kdf_salt(ctx
, &kdf_salt
)) != SQLITE_OK
) {
806 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3Codec: error retrieving salt: %d", rc
);
807 sqlcipher_codec_ctx_set_error(ctx
, rc
);
810 memcpy(buffer
, plaintext_header_sz
? pData
: kdf_salt
, offset
);
812 rc
= sqlcipher_page_cipher(ctx
, cctx
, pgno
, CIPHER_ENCRYPT
, page_sz
- offset
, pData
+ offset
, (unsigned char*)buffer
+ offset
);
813 #ifdef SQLCIPHER_TEST
814 if((sqlcipher_get_test_flags() & TEST_FAIL_ENCRYPT
) > 0 && sqlcipher_get_test_fail()) {
816 sqlcipher_log(SQLCIPHER_LOG_WARN
, SQLCIPHER_LOG_CORE
, "sqlite3Codec: simulating encryption failure for pgno=%d, mode=%d, page_sz=%d\n", pgno
, mode
, page_sz
);
819 if(rc
!= SQLITE_OK
) {
820 /* failure to encrypt a page is considered a permanent error and will render the pager unusable
821 in order to prevent corrupted pages from being written to the main databased when using WAL */
822 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3Codec: error encrypting page %d data: %d", pgno
, rc
);
823 sqlcipher_memset((unsigned char*)buffer
+offset
, 0, page_sz
-offset
);
824 sqlcipher_codec_ctx_set_error(ctx
, rc
);
827 SQLCIPHER_FLAG_SET(ctx
->flags
, CIPHER_FLAG_KEY_USED
);
828 return buffer
; /* return persistent buffer data, pData remains intact */
832 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3Codec: error unsupported codec mode %d", mode
);
833 sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
); /* unsupported mode, set error */
839 static void sqlite3FreeCodecArg(void *pCodecArg
) {
840 codec_ctx
*ctx
= (codec_ctx
*) pCodecArg
;
841 if(pCodecArg
== NULL
) return;
842 sqlcipher_codec_ctx_free(&ctx
); /* wipe and free allocated memory for the context */
843 sqlcipher_deactivate(); /* cleanup related structures, OpenSSL etc, when codec is detatched */
846 int sqlcipherCodecAttach(sqlite3
* db
, int nDb
, const void *zKey
, int nKey
) {
847 struct Db
*pDb
= &db
->aDb
[nDb
];
849 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipherCodecAttach: db=%p, nDb=%d", db
, nDb
);
851 if(nKey
&& zKey
&& pDb
->pBt
) {
853 Pager
*pPager
= pDb
->pBt
->pBt
->pPager
;
857 ctx
= (codec_ctx
*) sqlcipherPagerGetCodec(pDb
->pBt
->pBt
->pPager
);
859 if(ctx
!= NULL
&& SQLCIPHER_FLAG_GET(ctx
->flags
, CIPHER_FLAG_KEY_USED
)) {
860 /* there is already a codec attached to this database, so we should not proceed */
861 sqlcipher_log(SQLCIPHER_LOG_WARN
, SQLCIPHER_LOG_CORE
, "sqlcipherCodecAttach: no codec attached to db");
865 /* check if the sqlite3_file is open, and if not force handle to NULL */
866 if((fd
= sqlite3PagerFile(pPager
))->pMethods
== 0) fd
= NULL
;
868 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipherCodecAttach: calling sqlcipher_activate()");
869 sqlcipher_activate(); /* perform internal initialization for sqlcipher */
871 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "sqlcipherCodecAttach: entering database mutex %p", db
->mutex
);
872 sqlite3_mutex_enter(db
->mutex
);
873 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "sqlcipherCodecAttach: entered database mutex %p", db
->mutex
);
876 if((rc
= sqlite3_set_authorizer(db
, sqlcipher_license_authorizer
, db
)) != SQLITE_OK
) {
877 sqlite3_mutex_leave(db
->mutex
);
882 /* point the internal codec argument against the contet to be prepared */
883 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipherCodecAttach: calling sqlcipher_codec_ctx_init()");
884 rc
= sqlcipher_codec_ctx_init(&ctx
, pDb
, pDb
->pBt
->pBt
->pPager
, zKey
, nKey
);
886 if(rc
!= SQLITE_OK
) {
887 /* initialization failed, do not attach potentially corrupted context */
888 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlcipherCodecAttach: context initialization failed, forcing error state with rc=%d", rc
);
889 /* force an error at the pager level, such that even the upstream caller ignores the return code
890 the pager will be in an error state and will process no further operations */
891 sqlite3pager_error(pPager
, rc
);
892 pDb
->pBt
->pBt
->db
->errCode
= rc
;
893 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "sqlcipherCodecAttach: leaving database mutex %p (early return on rc=%d)", db
->mutex
, rc
);
894 sqlite3_mutex_leave(db
->mutex
);
895 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "sqlcipherCodecAttach: left database mutex %p (early return on rc=%d)", db
->mutex
, rc
);
899 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipherCodecAttach: calling sqlcipherPagerSetCodec()");
900 sqlcipherPagerSetCodec(sqlite3BtreePager(pDb
->pBt
), sqlite3Codec
, NULL
, sqlite3FreeCodecArg
, (void *) ctx
);
902 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipherCodecAttach: calling codec_set_btree_to_codec_pagesize()");
903 codec_set_btree_to_codec_pagesize(db
, pDb
, ctx
);
905 /* force secure delete. This has the benefit of wiping internal data when deleted
906 and also ensures that all pages are written to disk (i.e. not skipped by
907 sqlite3PagerDontWrite optimizations) */
908 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipherCodecAttach: calling sqlite3BtreeSecureDelete()");
909 sqlite3BtreeSecureDelete(pDb
->pBt
, 1);
911 /* if fd is null, then this is an in-memory database and
912 we dont' want to overwrite the AutoVacuum settings
913 if not null, then set to the default */
915 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipherCodecAttach: calling sqlite3BtreeSetAutoVacuum()");
916 sqlite3BtreeSetAutoVacuum(pDb
->pBt
, SQLITE_DEFAULT_AUTOVACUUM
);
918 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "sqlcipherCodecAttach: leaving database mutex %p", db
->mutex
);
919 sqlite3_mutex_leave(db
->mutex
);
920 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "sqlcipherCodecAttach: left database mutex %p", db
->mutex
);
925 int sqlcipher_find_db_index(sqlite3
*db
, const char *zDb
) {
930 for(db_index
= 0; db_index
< db
->nDb
; db_index
++) {
931 struct Db
*pDb
= &db
->aDb
[db_index
];
932 if(strcmp(pDb
->zDbSName
, zDb
) == 0) {
939 void sqlite3_activate_see(const char* in
) {
940 /* do nothing, security enhancements are always active */
943 int sqlite3_key(sqlite3
*db
, const void *pKey
, int nKey
) {
944 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlite3_key: db=%p", db
);
945 return sqlite3_key_v2(db
, "main", pKey
, nKey
);
948 int sqlite3_key_v2(sqlite3
*db
, const char *zDb
, const void *pKey
, int nKey
) {
949 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlite3_key_v2: db=%p zDb=%s", db
, zDb
);
950 /* attach key if db and pKey are not null and nKey is > 0 */
951 if(db
&& pKey
&& nKey
) {
952 int db_index
= sqlcipher_find_db_index(db
, zDb
);
953 return sqlcipherCodecAttach(db
, db_index
, pKey
, nKey
);
955 sqlcipher_log(SQLCIPHER_LOG_WARN
, SQLCIPHER_LOG_CORE
, "sqlite3_key_v2: no key provided");
959 int sqlite3_rekey(sqlite3
*db
, const void *pKey
, int nKey
) {
960 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlite3_rekey: db=%p", db
);
961 return sqlite3_rekey_v2(db
, "main", pKey
, nKey
);
965 ** Given a database, this will reencrypt the database using a new key.
966 ** There is only one possible modes of operation - to encrypt a database
967 ** that is already encrpyted. If the database is not already encrypted
968 ** this should do nothing
969 ** The proposed logic for this function follows:
970 ** 1. Determine if the database is already encryptped
971 ** 2. If there is NOT already a key present do nothing
972 ** 3. If there is a key present, re-encrypt the database with the new key
974 int sqlite3_rekey_v2(sqlite3
*db
, const char *zDb
, const void *pKey
, int nKey
) {
975 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlite3_rekey_v2: db=%p zDb=%s", db
, zDb
);
976 if(db
&& pKey
&& nKey
) {
977 int db_index
= sqlcipher_find_db_index(db
, zDb
);
978 struct Db
*pDb
= &db
->aDb
[db_index
];
979 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlite3_rekey_v2: database zDb=%p db_index:%d", zDb
, db_index
);
985 Pager
*pPager
= pDb
->pBt
->pBt
->pPager
;
987 ctx
= (codec_ctx
*) sqlcipherPagerGetCodec(pDb
->pBt
->pBt
->pPager
);
990 /* there was no codec attached to this database, so this should do nothing! */
991 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3_rekey_v2: no codec attached to db %s: rekey can't be used on an unencrypted database", zDb
);
992 return SQLITE_MISUSE
;
995 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "sqlite3_rekey_v2: entering database mutex %p", db
->mutex
);
996 sqlite3_mutex_enter(db
->mutex
);
997 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "sqlite3_rekey_v2: entered database mutex %p", db
->mutex
);
999 codec_set_pass_key(db
, db_index
, pKey
, nKey
, CIPHER_WRITE_CTX
);
1001 /* do stuff here to rewrite the database
1002 ** 1. Create a transaction on the database
1003 ** 2. Iterate through each page, reading it and then writing it.
1004 ** 3. If that goes ok then commit and put ctx->rekey into ctx->key
1005 ** note: don't deallocate rekey since it may be used in a subsequent iteration
1007 rc
= sqlite3BtreeBeginTrans(pDb
->pBt
, 1, 0); /* begin write transaction */
1008 sqlite3PagerPagecount(pPager
, &page_count
);
1009 for(pgno
= 1; rc
== SQLITE_OK
&& pgno
<= (unsigned int)page_count
; pgno
++) { /* pgno's start at 1 see pager.c:pagerAcquire */
1010 if(!sqlite3pager_is_sj_pgno(pPager
, pgno
)) { /* skip this page (see pager.c:pagerAcquire for reasoning) */
1011 rc
= sqlite3PagerGet(pPager
, pgno
, &page
, 0);
1012 if(rc
== SQLITE_OK
) { /* write page see pager_incr_changecounter for example */
1013 rc
= sqlite3PagerWrite(page
);
1014 if(rc
== SQLITE_OK
) {
1015 sqlite3PagerUnref(page
);
1017 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3_rekey_v2: error %d occurred writing page %d", rc
, pgno
);
1020 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3_rekey_v2: error %d occurred reading page %d", rc
, pgno
);
1025 /* if commit was successful commit and copy the rekey data to current key, else rollback to release locks */
1026 if(rc
== SQLITE_OK
) {
1027 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlite3_rekey_v2: committing");
1028 rc
= sqlite3BtreeCommit(pDb
->pBt
);
1029 sqlcipher_codec_key_copy(ctx
, CIPHER_WRITE_CTX
);
1031 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlite3_rekey_v2: rollback");
1032 sqlite3BtreeRollback(pDb
->pBt
, SQLITE_ABORT_ROLLBACK
, 0);
1035 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "sqlite3_rekey_v2: leaving database mutex %p", db
->mutex
);
1036 sqlite3_mutex_leave(db
->mutex
);
1037 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MUTEX
, "sqlite3_rekey_v2: left database mutex %p", db
->mutex
);
1041 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3_rekey_v2: no key provided for db %s: rekey can't be used to decrypt an encrypted database", zDb
);
1042 return SQLITE_ERROR
;
1045 void sqlcipherCodecGetKey(sqlite3
* db
, int nDb
, void **zKey
, int *nKey
) {
1046 struct Db
*pDb
= &db
->aDb
[nDb
];
1047 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipherCodecGetKey:db=%p, nDb=%d", db
, nDb
);
1049 codec_ctx
*ctx
= (codec_ctx
*) sqlcipherPagerGetCodec(pDb
->pBt
->pBt
->pPager
);
1052 /* pass back the keyspec from the codec, unless PRAGMA cipher_store_pass
1053 is set or keyspec has not yet been derived, in which case pass
1054 back the password key material */
1055 sqlcipher_codec_get_keyspec(ctx
, zKey
, nKey
);
1056 if(sqlcipher_codec_get_store_pass(ctx
) == 1 || *zKey
== NULL
) {
1057 sqlcipher_codec_get_pass(ctx
, zKey
, nKey
);
1067 * Implementation of an "export" function that allows a caller
1068 * to duplicate the main database to an attached database. This is intended
1069 * as a conveneince for users who need to:
1071 * 1. migrate from an non-encrypted database to an encrypted database
1072 * 2. move from an encrypted database to a non-encrypted database
1073 * 3. convert beween the various flavors of encrypted databases.
1075 * This implementation is based heavily on the procedure and code used
1076 * in vacuum.c, but is exposed as a function that allows export to any
1077 * named attached database.
1081 ** Finalize a prepared statement. If there was an error, store the
1082 ** text of the error message in *pzErrMsg. Return the result code.
1084 ** Based on vacuumFinalize from vacuum.c
1086 static int sqlcipher_finalize(sqlite3
*db
, sqlite3_stmt
*pStmt
, char **pzErrMsg
){
1088 rc
= sqlite3VdbeFinalize((Vdbe
*)pStmt
);
1090 sqlite3SetString(pzErrMsg
, db
, sqlite3_errmsg(db
));
1096 ** Execute zSql on database db. Return an error code.
1098 ** Based on execSql from vacuum.c
1100 static int sqlcipher_execSql(sqlite3
*db
, char **pzErrMsg
, const char *zSql
){
1101 sqlite3_stmt
*pStmt
;
1104 return SQLITE_NOMEM
;
1106 if( SQLITE_OK
!=sqlite3_prepare(db
, zSql
, -1, &pStmt
, 0) ){
1107 sqlite3SetString(pzErrMsg
, db
, sqlite3_errmsg(db
));
1108 return sqlite3_errcode(db
);
1110 VVA_ONLY( rc
= ) sqlite3_step(pStmt
);
1111 assert( rc
!=SQLITE_ROW
);
1112 return sqlcipher_finalize(db
, pStmt
, pzErrMsg
);
1116 ** Execute zSql on database db. The statement returns exactly
1117 ** one column. Execute this as SQL on the same database.
1119 ** Based on execExecSql from vacuum.c
1121 static int sqlcipher_execExecSql(sqlite3
*db
, char **pzErrMsg
, const char *zSql
){
1122 sqlite3_stmt
*pStmt
;
1125 rc
= sqlite3_prepare(db
, zSql
, -1, &pStmt
, 0);
1126 if( rc
!=SQLITE_OK
) return rc
;
1128 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
1129 rc
= sqlcipher_execSql(db
, pzErrMsg
, (char*)sqlite3_column_text(pStmt
, 0));
1130 if( rc
!=SQLITE_OK
){
1131 sqlcipher_finalize(db
, pStmt
, pzErrMsg
);
1136 return sqlcipher_finalize(db
, pStmt
, pzErrMsg
);
1140 * copy database and schema from the main database to an attached database
1142 * Based on sqlite3RunVacuum from vacuum.c
1144 void sqlcipher_exportFunc(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
) {
1145 sqlite3
*db
= sqlite3_context_db_handle(context
);
1146 const char* targetDb
, *sourceDb
;
1147 int targetDb_idx
= 0;
1148 u64 saved_flags
= db
->flags
; /* Saved value of the db->flags */
1149 u32 saved_mDbFlags
= db
->mDbFlags
; /* Saved value of the db->mDbFlags */
1150 int saved_nChange
= db
->nChange
; /* Saved value of db->nChange */
1151 int saved_nTotalChange
= db
->nTotalChange
; /* Saved value of db->nTotalChange */
1152 u8 saved_mTrace
= db
->mTrace
; /* Saved value of db->mTrace */
1153 int rc
= SQLITE_OK
; /* Return code from service routines */
1154 char *zSql
= NULL
; /* SQL statements */
1155 char *pzErrMsg
= NULL
;
1157 if(argc
!= 1 && argc
!= 2) {
1159 pzErrMsg
= sqlite3_mprintf("invalid number of arguments (%d) passed to sqlcipher_export", argc
);
1163 if(sqlite3_value_type(argv
[0]) == SQLITE_NULL
) {
1165 pzErrMsg
= sqlite3_mprintf("target database can't be NULL");
1169 targetDb
= (const char*) sqlite3_value_text(argv
[0]);
1173 if(sqlite3_value_type(argv
[1]) == SQLITE_NULL
) {
1175 pzErrMsg
= sqlite3_mprintf("target database can't be NULL");
1178 sourceDb
= (char *) sqlite3_value_text(argv
[1]);
1182 /* if the name of the target is not main, but the index returned is zero
1183 there is a mismatch and we should not proceed */
1184 targetDb_idx
= sqlcipher_find_db_index(db
, targetDb
);
1185 if(targetDb_idx
== 0 && targetDb
!= NULL
&& sqlite3_stricmp("main", targetDb
) != 0) {
1187 pzErrMsg
= sqlite3_mprintf("unknown database %s", targetDb
);
1190 db
->init
.iDb
= targetDb_idx
;
1192 db
->flags
|= SQLITE_WriteSchema
| SQLITE_IgnoreChecks
;
1193 db
->mDbFlags
|= DBFLAG_PreferBuiltin
| DBFLAG_Vacuum
;
1194 db
->flags
&= ~(u64
)(SQLITE_ForeignKeys
| SQLITE_ReverseOrder
| SQLITE_Defensive
| SQLITE_CountRows
);
1197 /* Query the schema of the main database. Create a mirror schema
1198 ** in the temporary database.
1200 zSql
= sqlite3_mprintf(
1202 " FROM %s.sqlite_schema WHERE type='table' AND name!='sqlite_sequence'"
1205 rc
= (zSql
== NULL
) ? SQLITE_NOMEM
: sqlcipher_execExecSql(db
, &pzErrMsg
, zSql
);
1206 if( rc
!=SQLITE_OK
) goto end_of_export
;
1209 zSql
= sqlite3_mprintf(
1211 " FROM %s.sqlite_schema WHERE sql LIKE 'CREATE INDEX %%' "
1213 rc
= (zSql
== NULL
) ? SQLITE_NOMEM
: sqlcipher_execExecSql(db
, &pzErrMsg
, zSql
);
1214 if( rc
!=SQLITE_OK
) goto end_of_export
;
1217 zSql
= sqlite3_mprintf(
1219 " FROM %s.sqlite_schema WHERE sql LIKE 'CREATE UNIQUE INDEX %%'"
1221 rc
= (zSql
== NULL
) ? SQLITE_NOMEM
: sqlcipher_execExecSql(db
, &pzErrMsg
, zSql
);
1222 if( rc
!=SQLITE_OK
) goto end_of_export
;
1225 /* Loop through the tables in the main database. For each, do
1226 ** an "INSERT INTO rekey_db.xxx SELECT * FROM main.xxx;" to copy
1227 ** the contents to the temporary database.
1229 zSql
= sqlite3_mprintf(
1230 "SELECT 'INSERT INTO %s.' || quote(name) "
1231 "|| ' SELECT * FROM %s.' || quote(name) || ';'"
1232 "FROM %s.sqlite_schema "
1233 "WHERE type = 'table' AND name!='sqlite_sequence' "
1235 , targetDb
, sourceDb
, sourceDb
);
1236 rc
= (zSql
== NULL
) ? SQLITE_NOMEM
: sqlcipher_execExecSql(db
, &pzErrMsg
, zSql
);
1237 if( rc
!=SQLITE_OK
) goto end_of_export
;
1240 /* Copy over the contents of the sequence table
1242 zSql
= sqlite3_mprintf(
1243 "SELECT 'INSERT INTO %s.' || quote(name) "
1244 "|| ' SELECT * FROM %s.' || quote(name) || ';' "
1245 "FROM %s.sqlite_schema WHERE name=='sqlite_sequence';"
1246 , targetDb
, sourceDb
, targetDb
);
1247 rc
= (zSql
== NULL
) ? SQLITE_NOMEM
: sqlcipher_execExecSql(db
, &pzErrMsg
, zSql
);
1248 if( rc
!=SQLITE_OK
) goto end_of_export
;
1251 /* Copy the triggers, views, and virtual tables from the main database
1252 ** over to the temporary database. None of these objects has any
1253 ** associated storage, so all we have to do is copy their entries
1254 ** from the SQLITE_MASTER table.
1256 zSql
= sqlite3_mprintf(
1257 "INSERT INTO %s.sqlite_schema "
1258 " SELECT type, name, tbl_name, rootpage, sql"
1259 " FROM %s.sqlite_schema"
1260 " WHERE type='view' OR type='trigger'"
1261 " OR (type='table' AND rootpage=0)"
1262 , targetDb
, sourceDb
);
1263 rc
= (zSql
== NULL
) ? SQLITE_NOMEM
: sqlcipher_execSql(db
, &pzErrMsg
, zSql
);
1264 if( rc
!=SQLITE_OK
) goto end_of_export
;
1270 db
->flags
= saved_flags
;
1271 db
->mDbFlags
= saved_mDbFlags
;
1272 db
->nChange
= saved_nChange
;
1273 db
->nTotalChange
= saved_nTotalChange
;
1274 db
->mTrace
= saved_mTrace
;
1276 if(zSql
) sqlite3_free(zSql
);
1279 if(pzErrMsg
!= NULL
) {
1280 sqlite3_result_error(context
, pzErrMsg
, -1);
1281 sqlite3DbFree(db
, pzErrMsg
);
1283 sqlite3_result_error(context
, sqlite3ErrStr(rc
), -1);