From 034480aa7632e65817eed8cbb766d6f0d7ee5da9 Mon Sep 17 00:00:00 2001 From: Stephen Lombardo Date: Tue, 15 Jan 2019 13:38:44 -0500 Subject: [PATCH] adds PRAGMA cipher_settings to query current database codec settings --- src/crypto.c | 51 +++++++++++++++++++++++++++++++++++++ test/sqlcipher-pragmas.test | 22 ++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/crypto.c b/src/crypto.c index 69a9853..2e5b8ce 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -534,6 +534,57 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef char *on = sqlite3_mprintf("%d", sqlcipher_get_mem_security()); codec_vdbe_return_string(pParse, "cipher_memory_security", on, P4_DYNAMIC); } + }else + if( sqlite3StrICmp(zLeft,"cipher_settings")==0 ){ + if(ctx) { + int algorithm; + char *pragma; + pragma = sqlite3_mprintf("PRAGMA kdf_iter = %d;", sqlcipher_codec_ctx_get_kdf_iter(ctx)); + codec_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + pragma = sqlite3_mprintf("PRAGMA fast_kdf_iter = %d;", sqlcipher_codec_ctx_get_fast_kdf_iter(ctx)); + codec_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + pragma = sqlite3_mprintf("PRAGMA cipher_page_size = %d;", sqlcipher_codec_ctx_get_pagesize(ctx)); + codec_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + pragma = sqlite3_mprintf("PRAGMA cipher_use_hmac = %d;", sqlcipher_codec_ctx_get_use_hmac(ctx)); + codec_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + if(sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_LE_PGNO)) { + codec_vdbe_return_string(pParse, "pragma", "PRAGMA cipher_hmac_pgno = le;", P4_TRANSIENT); + } else if(sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_BE_PGNO)) { + codec_vdbe_return_string(pParse, "pragma", "PRAGMA cipher_hmac_pgno = be;", P4_TRANSIENT); + } else { + codec_vdbe_return_string(pParse, "pragma", "PRAGMA cipher_hmac_pgno = native;", P4_TRANSIENT); + } + + pragma = sqlite3_mprintf("PRAGMA cipher_hmac_salt_mask = %02x;", sqlcipher_get_hmac_salt_mask()); + codec_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_plaintext_header_size = %d;", sqlcipher_codec_ctx_get_plaintext_header_size(ctx)); + codec_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + algorithm = sqlcipher_codec_ctx_get_hmac_algorithm(ctx); + pragma = NULL; + if(algorithm == SQLCIPHER_HMAC_SHA1) { + pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA1_LABEL); + } else if(algorithm == SQLCIPHER_HMAC_SHA256) { + pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA256_LABEL); + } else if(algorithm == SQLCIPHER_HMAC_SHA512) { + pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA512_LABEL); + } + codec_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + algorithm = sqlcipher_codec_ctx_get_kdf_algorithm(ctx); + pragma = NULL; + if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) { + pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL); + } else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) { + pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL); + } else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) { + pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL); + } + codec_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + } }else { return 0; } diff --git a/test/sqlcipher-pragmas.test b/test/sqlcipher-pragmas.test index e1bd946..2eecdbd 100644 --- a/test/sqlcipher-pragmas.test +++ b/test/sqlcipher-pragmas.test @@ -245,6 +245,7 @@ do_test verify-pragma-hmac-salt-mask-reports-value-changed { PRAGMA key = 'test'; PRAGMA cipher_hmac_salt_mask = "x'11'"; PRAGMA cipher_hmac_salt_mask; + PRAGMA cipher_hmac_salt_mask = "x'3a'"; } } {11} db close @@ -376,4 +377,25 @@ if_built_with_commoncrypto verify-default-cipher { db close file delete -force test.db +do_test verify-cipher_settings_default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_settings; + } +} {{PRAGMA kdf_iter = 256000;} {PRAGMA fast_kdf_iter = 2;} {PRAGMA cipher_page_size = 4096;} {PRAGMA cipher_use_hmac = 1;} {PRAGMA cipher_hmac_pgno = le;} {PRAGMA cipher_hmac_salt_mask = 3a;} {PRAGMA cipher_plaintext_header_size = 0;} {PRAGMA cipher_hmac_algorithm = HMAC_SHA512;} {PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA512;}} +db close +file delete -force test.db + +do_test verify-cipher_settings_v1 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_compatibility = 1; + PRAGMA cipher_settings; + } +} {{PRAGMA kdf_iter = 4000;} {PRAGMA fast_kdf_iter = 2;} {PRAGMA cipher_page_size = 1024;} {PRAGMA cipher_use_hmac = 0;} {PRAGMA cipher_hmac_pgno = le;} {PRAGMA cipher_hmac_salt_mask = 3a;} {PRAGMA cipher_plaintext_header_size = 0;} {PRAGMA cipher_hmac_algorithm = HMAC_SHA1;} {PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;}} +db close +file delete -force test.db + finish_test