I am working on embedded system and the device has linux kernel with sqlite database. Wanted to know if the sqlite database can be partitioned with secure and normal partitions.
How can the encryption be achieved for sqlite database file in linux.
Maybe I am too late to answer this question, but I was facing this issue from couple of days and couldn't find any solid solution online. I have found solution hence I am sharing it.
//Steps to make sqlite database authenticated
download sqlite3 amalgamation zip file
unzip the file. The file should contain shell.c, sqlite3.c, sqlite3.h, sqlite3ext.h
click on find the link here
3a. Open userauth.c and copy the entire code and paste it at the end of your sqlite3.c file.
3b. Open sqlite3userauth.h and copy the entire code and pase it at the end of your sqlite3.h file.
gcc -o sqlite3Exe shell.c sqlite3.c -DSQLITE_USER_AUTHENTICATION -ldl -lpthread
4a. Youll get error no such file "sqlite3userauth.h" in your shell.c file: solution: go to that file and comment th line.(this is because youve already included the necessary code when you copied sqlite3auth.h into sqlite3.h)
4b. Test your output file by running ./sqlite3Exe
(this is the name youve given to the output file generated in previous step). you'll get sqlite console.
4c. Create a database and on the authentication flag:
command1: .open dbname.db
command2: .auth on
command3: .exit
//command 3 is optional
command: gcc -o sqlite3.o -c sqlite3.c -DSQLITE_USER_AUTHENTICATION
With this command, we generate object file which we can use to compile our c file.
Create c file to authenticate your database:
//authUser.c
#include "stdio.h"
#include "stdlib.h"
#include "sqlite3.h"
int main(int argc,char * argv[]){
int a = 10;
int rtn, rtn2;
sqlite3 *db;
char *sql, *zErMsg;
rtn = sqlite3_open("dbname.db", &db);
rtn = sqlite3_user_add(db,"username","password",2, 1);//last but one param is for number of bytes for password, last param is for weather the user is admin or not
if(rtn){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}else{
fprintf(stderr, "Protected database successfully\n");
}
sqlite3_close(db);
return 0;
}
Compiling the program
//Compiling the program
command1: gcc authUser.c sqlite3.o -lpthread -ldl
command2: ./a.out
//Output:protected database successfully
create c file to create table if the user is authenticated
//createTable.c #include "stdio.h" #include "stdlib.h" #include "sqlite3.h" static int callback(void *NotUsed, int argc, char **argv, char **azColName){ int i; for(i=0; i less then argc; i++){ printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; } int main(int argc,char * argv[]){ int a = 10; int rtn, rtn2; sqlite3 *db; char *sql, *zErMsg; rtn = sqlite3_open("dbname.db", &db); rtn = sqlite3_user_authenticate(db, "user","password",2); if(rtn){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); return(0); }else{ fprintf(stderr, "Opened database successfully\n"); } sql = "create table newtable(id int not null primary key, name varchar(100) not null)"; //sql = "insert into newtable values(5, 'ishwar')"; rtn = sqlite3_exec(db, sql, callback, 0, &zErMsg); if(rtn != SQLITE_OK){ sqlite3_free(zErMsg); }else{ fprintf(stdout, "Table created successfully \n"); //fprintf(stdout, "inserted successfully \n"); } sqlite3_close(db); return 0; }`
compiling the program //Compiling the program
command1: gcc createTable.c sqlite3.o -lpthread -ldl
command2: ./a.out
//Output:Table created successfully
from the previous code, you can see two sql variable and two fprintf inside else, now uncomment the commented line and comment the other one. and runt the same command as above output: Inserted successfully
And youre done, try experimenting with the code, change the values of sqlite3_user_authenticate function you wont be able to do these operations,at max you may be able to open database(when you comment the sqlite3_user_authenticate functon.nothing else)
Run the command: ./sqlite3Exe
(the output file we created in step 4)
command1: .open dbname.db
command2: .tables
//you should get error, user_auth
Thank you(please feel free to mail me in case of any problem: ishwar.rimal@gmail.com)
for encryption to be achieved with SQLite, you need to license some extensions from the SQLite author.