-
Notifications
You must be signed in to change notification settings - Fork 25
/
attach.c
120 lines (113 loc) · 4.6 KB
/
attach.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* rm *.db
* cc attach.c -I../sqlcipher-static/sqlcipher -DSQLITE_HAS_CODEC -DSQLCIPHER_CRYPTO_CC -framework Security -DSQLITE_ENABLE_FTS3=1 -DSQLITE_TEMP_STORE=2 -o attach
* OR
* cc attach.c -I../sqlcipher-static/sqlcipher -DSQLITE_HAS_CODEC -DSQLITE_ENABLE_FTS3=1 -DSQLITE_TEMP_STORE=2 -l crypto -o attach
* // -I../sqlcipher/src
* ./attach
*/
#include <sqlite3.c>
#include <stdio.h>
#define ERROR(X) {printf("[ERROR]: "); printf X;fflush(stdout);}
int main( int argc, const char* argv[])
{
const char* key = "123";
const char* create = "CREATE TABLE t1( id TEXT PRIMARY KEY, a TEXT, b TEXT);";
const char* insert = "INSERT INTO t1 (id,a,b) VALUES (1,'a','b');";
const char* localPath = "./attach-local.db";
const char* remotePath = "./attach-remote.db";
const char* rekeyPath = "./attach-rekey.db";
const char* attach_sql = "ATTACH DATABASE ? AS rekey KEY ?;";
int rc;
sqlite3 *local_db, *remote_db;
char *errorPointer;
sqlite3_stmt *stmt;
// create the remote db with legacy settings
if (sqlite3_open(remotePath, &remote_db) != SQLITE_OK) {
ERROR(("sqlite3_open failed for %s: %s\n", remotePath, sqlite3_errmsg(remote_db)))
exit(1);
}
sqlite3_key(remote_db, key, strlen(key));
sqlite3_exec(remote_db, "PRAGMA kdf_iter = 4000;", NULL, 0, NULL);
sqlite3_exec(remote_db, create, NULL, 0, NULL);
sqlite3_exec(remote_db, insert, NULL, 0, NULL);
// close it to write it out to storage...
sqlite3_close(remote_db);
// create the local db with current settings
if (sqlite3_open(localPath, &local_db) != SQLITE_OK) {
ERROR(("sqlite3_open failed for %s: %s\n", localPath, sqlite3_errmsg(local_db)))
exit(1);
}
sqlite3_key(local_db, key, strlen(key));
sqlite3_exec(local_db, "PRAGMA kdf_iter = 64000;", NULL, 0, NULL);
sqlite3_exec(local_db, create, NULL, 0, NULL);
sqlite3_exec(local_db, insert, NULL, 0, NULL);
// close it to write it out to storage...
sqlite3_close(local_db);
// upgrade the remote db via rekey
if (sqlite3_open(remotePath, &remote_db) != SQLITE_OK) {
ERROR(("sqlite3_open failed for %s: %s\n", remotePath, sqlite3_errmsg(remote_db)))
exit(1);
}
sqlite3_key(remote_db, key, strlen(key));
sqlite3_exec(remote_db, "PRAGMA kdf_iter = 4000;", NULL, 0, NULL);
if (sqlite3_exec(remote_db, "SELECT * FROM sqlite_master;", NULL, 0, &errorPointer) != SQLITE_OK) {
ERROR(("select from sqlite_master failed: %s\n", errorPointer));
sqlite3_free(errorPointer);
exit(1);
}
// prepare stmt for attach
rc = sqlite3_prepare_v2(remote_db, attach_sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
ERROR(("sqlite3_prepare_v2 failed for %s: %s\n", attach_sql, sqlite3_errmsg(remote_db)))
exit(1);
}
// "ATTACH DATABASE ? AS rekey KEY ?;";
sqlite3_bind_text(stmt, 1, rekeyPath, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, key, -1, SQLITE_TRANSIENT);
if (sqlite3_step(stmt) != SQLITE_DONE) {
ERROR(("rekey attach failed: %s\n", sqlite3_errmsg(remote_db)));
sqlite3_free(errorPointer);
sqlite3_finalize(stmt);
exit(1);
}
sqlite3_finalize(stmt);
sqlite3_exec(remote_db, "PRAGMA rekey.kdf_iter = 64000;", NULL, 0, NULL);
rc = sqlite3_exec(remote_db, "SELECT sqlcipher_export('rekey');", NULL, 0, NULL);
if (rc != SQLITE_OK) {
ERROR(("sqlcipher_export failed: %s\n", sqlite3_errmsg(remote_db)))
exit(1);
}
sqlite3_close(remote_db);
// re-open local_db and attach rekeyed database
if (sqlite3_open(localPath, &local_db) != SQLITE_OK) {
ERROR(("sqlite3_open failed for %s: %s\n", localPath, sqlite3_errmsg(local_db)))
exit(1);
}
sqlite3_key(local_db, key, strlen(key));
sqlite3_exec(local_db, "PRAGMA kdf_iter = 64000;", NULL, 0, NULL);
if (sqlite3_exec(local_db, "SELECT * FROM sqlite_master;", NULL, 0, &errorPointer) != SQLITE_OK) {
ERROR(("select from sqlite_master failed: %s\n", errorPointer));
sqlite3_free(errorPointer);
exit(1);
}
// prepare stmt for attach on local_db
// This is the heart of our test: does attaching this rekey'd db succeed or fail?
rc = sqlite3_prepare_v2(local_db, attach_sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
ERROR(("sqlite3_prepare_v2 failed for %s: %s\n", attach_sql, sqlite3_errmsg(remote_db)))
exit(1);
}
// "ATTACH DATABASE ? AS rekey KEY ?;";
sqlite3_bind_text(stmt, 1, rekeyPath, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, key, -1, SQLITE_TRANSIENT);
if (sqlite3_step(stmt) != SQLITE_DONE) {
ERROR(("rekey attach to local failed: %s\n", sqlite3_errmsg(local_db)));
sqlite3_free(errorPointer);
sqlite3_finalize(stmt);
exit(1);
}
sqlite3_finalize(stmt);
sqlite3_close(local_db);
return 0;
}