Skip to content

Commit 2cd5e45

Browse files
committed
Adds new PRAGMA cipher_status
PRAGMA cipher_status allows an application to verify that a database is properly setup for encyption (i.e. that operations on the database will be using SQLCipher). It returns a scalar result set "1" if the database has been keyed and is not in an error state. Otherwise, it will return a scalar result set of "0".
1 parent 8fd1ef4 commit 2cd5e45

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/sqlcipher.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,6 +2626,13 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
26262626
sqlcipher_vdbe_return_string(pParse, "cipher_fips_status", fips_mode_status, P4_DYNAMIC);
26272627
}
26282628
} else
2629+
if( sqlite3_stricmp(zLeft, "cipher_status")== 0 && !zRight ){
2630+
if(ctx && ctx->error == SQLITE_OK) {
2631+
sqlcipher_vdbe_return_string(pParse, "cipher_status", "1", P4_TRANSIENT);
2632+
} else {
2633+
sqlcipher_vdbe_return_string(pParse, "cipher_status", "0", P4_TRANSIENT);
2634+
}
2635+
} else
26292636
if( sqlite3_stricmp(zLeft, "cipher_store_pass")==0 && zRight ) {
26302637
if(ctx) {
26312638
char *deprecation = "PRAGMA cipher_store_pass is deprecated, please remove from use";

test/sqlcipher-core.test

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,5 +995,84 @@ do_test uri-key-2 {
995995
db close
996996
file delete -force test.db
997997

998+
# test cipher_status reports encrypted for database
999+
do_test cipher-status-encrypted {
1000+
sqlite_orig db test.db
1001+
1002+
execsql {
1003+
PRAGMA key = 'test';
1004+
PRAGMA cipher_status;
1005+
}
1006+
1007+
} {ok 1}
1008+
db close
1009+
file delete -force test.db
1010+
1011+
# test cipher_status reports not-encrypted for plaintext database
1012+
do_test cipher-status-plaintext {
1013+
sqlite_orig db test.db
1014+
1015+
execsql {
1016+
PRAGMA cipher_status;
1017+
}
1018+
1019+
} {0}
1020+
db close
1021+
file delete -force test.db
1022+
1023+
# test cipher_status reports encrypted for attached database
1024+
# when main database is plaintext and attached database is
1025+
# keyed
1026+
do_test cipher-status-attached-encrypted {
1027+
sqlite_orig db test.db
1028+
1029+
execsql {
1030+
PRAGMA cipher_status;
1031+
ATTACH DATABASE 'test2.db' AS test2 KEY 'test';
1032+
PRAGMA test2.cipher_status;
1033+
DETACH DATABASE test2;
1034+
}
1035+
1036+
} {0 1}
1037+
db close
1038+
file delete -force test.db
1039+
file delete -force test2.db
1040+
1041+
# test cipher_status reports plaintext for attached database
1042+
# when main database is keyed and attached database is not
1043+
do_test cipher-status-attached-plaintext {
1044+
sqlite_orig db test.db
1045+
1046+
execsql {
1047+
PRAGMA KEY = 'test';
1048+
PRAGMA cipher_status;
1049+
ATTACH DATABASE 'test2.db' AS test2 KEY '';
1050+
PRAGMA test2.cipher_status;
1051+
DETACH DATABASE test2;
1052+
}
1053+
1054+
} {ok 1 0}
1055+
db close
1056+
file delete -force test.db
1057+
file delete -force test2.db
1058+
1059+
# test cipher_status reports unencrypted if key derivation
1060+
# and operation fails
1061+
setup test.db "'testkey'"
1062+
do_test cipher-status-badkey {
1063+
sqlite_orig db test.db
1064+
1065+
catchsql {
1066+
PRAGMA key = 'test';
1067+
SELECT count(*) FROM sqlite_master;
1068+
}
1069+
execsql {
1070+
PRAGMA cipher_status;
1071+
}
1072+
1073+
} {0}
1074+
db close
1075+
file delete -force test.db
1076+
9981077
finish_test
9991078

0 commit comments

Comments
 (0)