-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
438 lines (372 loc) · 14.4 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sqlite3.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
void generate_rsa_key(const char *private_key_path, const char *public_key_path) {
RSA *rsa = RSA_new();
FILE *private_key_file = fopen(private_key_path, "rb");
FILE *public_key_file = fopen(public_key_path, "rb");
if (private_key_file && public_key_file) {
fclose(private_key_file);
fclose(public_key_file);
RSA_free(rsa);
return;
}
if (!RSA_generate_key_ex(rsa, 2048, BN_new(), NULL)) {
fprintf(stderr, "Error: Unable to generate RSA key pair.\n");
ERR_print_errors_fp(stderr);
RSA_free(rsa);
exit(EXIT_FAILURE);
}
if (!RSA_set0_key(rsa, RSA_get0_n(rsa), RSA_get0_e(rsa), NULL)) {
fprintf(stderr, "Error: Unable to set public exponent.\n");
ERR_print_errors_fp(stderr);
RSA_free(rsa);
exit(EXIT_FAILURE);
}
private_key_file = fopen(private_key_path, "wb");
if (!private_key_file || !PEM_write_RSAPrivateKey(private_key_file, rsa, NULL, NULL, 0, NULL, NULL)) {
fprintf(stderr, "Error: Unable to write private key to file '%s'.\n", private_key_path);
RSA_free(rsa);
exit(EXIT_FAILURE);
}
fclose(private_key_file);
public_key_file = fopen(public_key_path, "wb");
if (!public_key_file || !PEM_write_RSAPublicKey(public_key_file, rsa)) {
fprintf(stderr, "Error: Unable to write public key to file '%s'.\n", public_key_path);
RSA_free(rsa);
exit(EXIT_FAILURE);
}
fclose(public_key_file);
printf("RSA key pair generated successfully.\n");
RSA_free(rsa);
}
void create_tables(sqlite3 *db) {
char *err_msg = 0;
const char *sql =
"CREATE TABLE IF NOT EXISTS Users ("
"UserID INTEGER PRIMARY KEY, "
"Username TEXT NOT NULL, "
"Password TEXT NOT NULL);"
"CREATE TABLE IF NOT EXISTS Memos ("
"MemoID INTEGER PRIMARY KEY, "
"UserID INTEGER, "
"Message TEXT, "
"FOREIGN KEY(UserID) REFERENCES Users(UserID));";
int rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
}
}
bool user_exists(sqlite3 *db, const char *username) {
sqlite3_stmt *stmt;
const char *sql = "SELECT COUNT(*) FROM Users WHERE Username = ?";
int result = 0;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
result = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);
} else {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
}
return result > 0;
}
void register_user(sqlite3 *db, const char *username, const char *password) {
if (user_exists(db, username)) {
printf("User already exists!\n");
return;
}
sqlite3_stmt *stmt;
const char *sql = "INSERT INTO Users (Username, Password) VALUES (?, ?);";
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, password, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) != SQLITE_DONE) {
fprintf(stderr, "Failed to add user: %s\n", sqlite3_errmsg(db));
} else {
printf("User registered successfully\n");
}
sqlite3_finalize(stmt);
} else {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
}
}
int authenticate(sqlite3 *db, const char *username, const char *password) {
sqlite3_stmt *stmt;
const char *sql = "SELECT Password FROM Users WHERE Username = ?;";
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char *db_password = sqlite3_column_text(stmt, 0);
if (strcmp(password, (const char*)db_password) == 0) {
printf("Authenticated successfully\n");
sqlite3_finalize(stmt);
return 1;
}
}
sqlite3_finalize(stmt);
} else {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
}
return 0;
}
int user_id_callback(void *data, int argc, char **argv, char **azColName) {
if (argc == 1) {
*(int*)data = atoi(argv[0]);
}
return 0;
}
int get_user_id(sqlite3 *db, const char *username) {
sqlite3_stmt *stmt;
const char *sql = "SELECT UserID FROM Users WHERE Username = ?;";
int user_id = -1;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
user_id = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);
} else {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
}
return user_id;
}
void encrypt_rsa(const char *public_key_path, const char *plain_text, unsigned char **encrypted_text, int *encrypted_len) {
FILE *pub_key_file = fopen(public_key_path, "rb");
if (!pub_key_file) {
fprintf(stderr, "Unable to open public key file\n");
return;
}
RSA *rsa = PEM_read_RSA_PUBKEY(pub_key_file, NULL, NULL, NULL);
fclose(pub_key_file);
if (!rsa) {
fprintf(stderr, "Unable to read public key\n");
return;
}
int rsa_size = RSA_size(rsa);
*encrypted_text = (unsigned char *)malloc(rsa_size);
if (!*encrypted_text) {
fprintf(stderr, "Memory allocation failed\n");
RSA_free(rsa);
return;
}
*encrypted_len = RSA_public_encrypt(strlen(plain_text), (unsigned char*)plain_text, *encrypted_text, rsa, RSA_PKCS1_PADDING);
RSA_free(rsa);
if (*encrypted_len == -1) {
fprintf(stderr, "Encryption failed\n");
free(*encrypted_text);
*encrypted_text = NULL;
}
}
void add_memo(sqlite3 *db, int user_id, const char *message) {
char *err_msg = 0;
unsigned char *encrypted_message = NULL;
int encrypted_len = 0;
encrypt_rsa("public_key.pem", message, &encrypted_message, &encrypted_len);
if (encrypted_message) {
char sql[1024];
snprintf(sql, sizeof(sql), "INSERT INTO Memos (UserID, Message) VALUES (%d, ?);", user_id);
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_blob(stmt, 1, encrypted_message, encrypted_len, SQLITE_TRANSIENT);
if (sqlite3_step(stmt) != SQLITE_DONE) {
fprintf(stderr, "Failed to add memo: %s\n", sqlite3_errmsg(db));
} else {
fprintf(stdout, "Memo added successfully\n");
}
sqlite3_finalize(stmt);
} else {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
}
free(encrypted_message);
}
}
void decrypt_rsa(const char *private_key_path, const unsigned char *encrypted_text, int encrypted_len, unsigned char **decrypted_text) {
FILE *priv_key_file = fopen(private_key_path, "rb");
if (!priv_key_file) {
fprintf(stderr, "Unable to open private key file\n");
return;
}
RSA *rsa = PEM_read_RSAPrivateKey(priv_key_file, NULL, NULL, NULL);
fclose(priv_key_file);
if (!rsa) {
fprintf(stderr, "Unable to read private key\n");
return;
}
int rsa_size = RSA_size(rsa);
*decrypted_text = (unsigned char *)malloc(rsa_size);
if (!*decrypted_text) {
fprintf(stderr, "Memory allocation failed\n");
RSA_free(rsa);
return;
}
int result = RSA_private_decrypt(encrypted_len, encrypted_text, *decrypted_text, rsa, RSA_PKCS1_PADDING);
RSA_free(rsa);
if (result == -1) {
fprintf(stderr, "Decryption failed\n");
free(*decrypted_text);
*decrypted_text = NULL;
} else {
(*decrypted_text)[result] = '\0';
}
}
void view_encrypted_memos(sqlite3 *db, int user_id) {
sqlite3_stmt *stmt;
const char *sql = "SELECT MemoID, Message FROM Memos WHERE UserID = ?;";
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
return;
}
sqlite3_bind_int(stmt, 1, user_id);
while (sqlite3_step(stmt) == SQLITE_ROW) {
int memo_id = sqlite3_column_int(stmt, 0);
const void *encrypted_memo = sqlite3_column_blob(stmt, 1);
int encrypted_len = sqlite3_column_bytes(stmt, 1);
printf("Memo ID: %d, Encrypted Content: ", memo_id);
for (int i = 0; i < encrypted_len; i++) {
printf("%02X", ((unsigned char*)encrypted_memo)[i]);
}
printf("\n");
}
sqlite3_finalize(stmt);
}
void view_decrypted_memos(sqlite3 *db, int user_id) {
sqlite3_stmt *stmt;
const char *sql = "SELECT MemoID, Message FROM Memos WHERE UserID = ?;";
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
return;
}
sqlite3_bind_int(stmt, 1, user_id);
while (sqlite3_step(stmt) == SQLITE_ROW) {
int memo_id = sqlite3_column_int(stmt, 0);
const void *encrypted_memo = sqlite3_column_blob(stmt, 1);
int encrypted_len = sqlite3_column_bytes(stmt, 1);
unsigned char *decrypted_memo = NULL;
decrypt_rsa("private_key.pem", encrypted_memo, encrypted_len, &decrypted_memo);
if (decrypted_memo) {
printf("Memo ID: %d, Decrypted Content: %s\n", memo_id, decrypted_memo);
free(decrypted_memo);
}
}
sqlite3_finalize(stmt);
}
void remove_memo(sqlite3 *db, int user_id, int memo_id) {
sqlite3_stmt *stmt;
const char *sql = "DELETE FROM Memos WHERE MemoID = ? AND UserID = ?;";
int rc = sqlite3_prepare_v2(db,sql,-1,&stmt,NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
return;
}
sqlite3_bind_int(stmt, 1, memo_id);
sqlite3_bind_int(stmt, 2, user_id);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "Failed to delete memo: %s\n", sqlite3_errmsg(db));
} else {
if (sqlite3_changes(db)==0){
printf("Error: Message does not exist or Memo doesn't belong to you.\n");
} else {
printf("Memo removed successfully\n");
}
sqlite3_finalize(stmt);
}
}
int main() {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("main.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
create_tables(db);
char option[20];
while (1) {
printf("Choose an option: register, login, exit: ");
scanf("%19s", option);
if (strcmp(option, "register") == 0) {
char username[50], password[50];
printf("Enter new username: ");
scanf("%49s", username);
printf("Enter new password: ");
scanf("%49s", password);
register_user(db, username, password);
} else if (strcmp(option, "login") == 0) {
char username[50], password[50];
printf("Enter username: ");
scanf("%49s", username);
printf("Enter password: ");
scanf("%49s", password);
if (authenticate(db, username, password)) {
printf("Login successful!\n");
int userid = get_user_id(db,username);
while (1) {
int choice;
printf("\nChoose an option:\n");
printf("1. add_memo\n");
printf("2. view_encrypted_memos\n");
printf("3. view_decrypted_memos\n");
printf("4. remove_memo\n");
printf("5. logout\n");
printf("Enter your choice: ");
scanf("%d*c", &choice);
switch (choice) {
case 1: {
char memo[256];
printf("Enter your memo: ");
scanf(" %[^\n]%*c", memo);
add_memo(db, userid, memo);
break;
}
case 2:
view_encrypted_memos(db, userid);
break;
case 3:
view_decrypted_memos(db, userid);
break;
case 4: {
int memo_id;
printf("Enter the ID of the memo to remove: ");
scanf("%d%*c", &memo_id);
remove_memo(db, userid, memo_id);
break;
}
case 5:
printf("Logging out...\n");
goto logout;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
logout:
}
else {
printf("Login failed. Please try again.\n");
}
} else if (strcmp(option, "exit") == 0) {
break;
}
}
sqlite3_close(db);
return 0;
}
/*
Citaions:
https://www.sqlite.org/cintro.html
https://www.openssl.org/docs/man1.1.1/man3/RSA_public_encrypt.html
https://www.openssl.org/docs/man1.1.1/man3/RSA_private_decrypt.html
https://www.openssl.org/docs/man1.1.1/man3/PEM_read_RSA_PUBKEY.html
https://www.openssl.org/docs/man1.1.1/man3/PEM_read_RSAPrivateKey.html
https://chats.openai.com
*/