forked from rindeal/SQLite3-Encryption
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_enc_func.patch
129 lines (128 loc) · 3.89 KB
/
add_enc_func.patch
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
121
122
123
124
125
126
127
128
129
diff --git a/src/shell.c b/src/shell.c
index 5740ee6..9d2c663 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -223,6 +223,14 @@ static void setTextMode(FILE *file, int isOutput){
/* True if the timer is enabled */
static int enableTimer = 0;
+#ifdef SQLITE_HAS_CODEC
+/* Database encryption support
+* Types were defined according to sqlite3_key() prototype.
+*/
+static void *encryption_key = NULL;
+static int encryption_key_length = 0;
+#endif
+
/* Return the current wall-clock time */
static sqlite3_int64 timeOfDay(void){
static sqlite3_vfs *clockVfs = 0;
@@ -11038,6 +11046,14 @@ static void open_db(ShellState *p, int keepAlive){
}
}
globalDb = p->db;
+
+#ifdef SQLITE_HAS_CODEC
+ if (encryption_key != NULL) {
+ sqlite3_activate_see("7bb07b8d471d642e");
+ sqlite3_key(p->db, encryption_key, encryption_key_length);
+ }
+#endif
+
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
p->zDbFilename, sqlite3_errmsg(p->db));
@@ -15611,6 +15627,9 @@ static const char zOptions[] =
#ifdef SQLITE_ENABLE_VFSTRACE
" -vfstrace enable tracing of all VFS calls\n"
#endif
+#ifdef SQLITE_HAS_CODEC
+ " -key hexvalue set encryption key (hexadecimal, no quotes)\n"
+#endif
#ifdef SQLITE_HAVE_ZLIB
" -zip open the file as a ZIP Archive\n"
#endif
@@ -15876,6 +15895,55 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
data.openMode = SHELL_OPEN_APPENDVFS;
}else if( strcmp(z,"-readonly")==0 ){
data.openMode = SHELL_OPEN_READONLY;
+
+ }else if (strcmp(argv[i], "-key") == 0) {
+#ifdef SQLITE_HAS_CODEC
+ /* Process hex key from command-line.
+ ** Message to SQLite developers: your parsers are NOT robust!
+ */
+ unsigned int idx;
+ unsigned int tmpLength;
+
+ i++;
+ /* compute key length */
+ idx = i;
+ while ((argv[i][idx] != ' ') && (argv[i][idx] != '\x00')) {
+ char c = (argv[i][idx]);
+ if (!((isdigit(c)) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F')))) {
+ fprintf(stderr, "Expecting hexadecimal values for encryption key!\r\n");
+ return 1;
+ }
+ idx++;
+ }
+ tmpLength = idx;
+ if ((tmpLength % 2) != 0) {
+ fprintf(stderr, "Expecting an even number of characters for encryption key!\r\n");
+ return 1;
+ }
+
+ /* allocate key memory */
+ encryption_key_length = tmpLength / 2;
+ encryption_key = malloc(encryption_key_length);
+ if (encryption_key == NULL) {
+ fprintf(stderr, "Out of memory!\r\n");
+ return 1;
+ }
+
+ /* convert hex string into values */
+ idx = 0;
+ while ((argv[i][idx] != ' ') && (argv[i][idx] != '\x00')) {
+ char hex[3];
+ hex[0] = (argv[i][idx++]);
+ hex[1] = (argv[i][idx++]);
+ hex[2] = '\x00';
+
+ ((unsigned char*)encryption_key)[(idx - 2) / 2] = (unsigned char)(0xFF & strtoul(hex, NULL, 16));
+ }
+#else
+ fprintf(stderr, "Sorry, encryption support is not available\r\n");
+ fprintf(stderr, "HINT: define 'SQLITE_HAS_CODEC' at compile time\r\n");
+ return 1;
+#endif
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
}else if( strncmp(z, "-A",2)==0 ){
/* All remaining command-line arguments are passed to the ".archive"
@@ -16029,6 +16097,14 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
if( bail_on_error ) return rc;
}
}
+
+#ifdef SQLITE_HAS_CODEC
+ }
+ else if (strcmp(z, "-key") == 0) {
+ /* encryption key has already been set */
+ i++;
+#endif
+
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
}else if( strncmp(z, "-A", 2)==0 ){
if( nCmd>0 ){
@@ -16131,5 +16207,13 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
for(i=0; i<argc; i++) free(argv[i]);
free(argv);
#endif
+
+#ifdef SQLITE_HAS_CODEC
+ if (encryption_key != NULL) {
+ free(encryption_key);
+ encryption_key = NULL;
+ }
+#endif
+
return rc;
}