aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 079a9e7ef1ea18b488ef2c1415b01e00048d4380 (plain)
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// TODO: Secret stored in memory must be encrypted with a key derived from the
// user password or consider making password manager stateless (no passwords
// stored locally)

#include "main.h"

struct Password passwords[MAX_PASSWORDS];
int numPasswords = 0;

FILE *log_file;

void log_message(const char *message) {
  if (log_file == NULL) {
    printf("Error: Log file not available.\n");
    return;
  }

  time_t current_time;
  struct tm *time_info;
  char time_string[80];

  time(&current_time);
  time_info = localtime(&current_time);

  strftime(time_string, sizeof(time_string), "[%Y-%m-%d %H:%M:%S] ", time_info);
  fprintf(log_file, "%s %s\n", time_string,
          message); // Print timestamp and message
}

void initialize_log() {
  log_file = fopen("cerberus.log", "a");
  if (log_file == NULL) {
    printf("Error: Could not open log file.\n");
  } else {
    log_message("=== cerberus log ==="); // Initial log message
  }
}

void close_log() {
  if (log_file != NULL) {
    fclose(log_file);
  }
}

void error_exit(const char *error_message) {
  log_message(error_message);
  if (log_file != NULL) {
    fclose(log_file);
  }
  exit(1);
}

void clear_input_buffer() {
  int c;
  while ((c = getchar()) != '\n' && c != EOF)
    ;
}

void check_password_policy(const char *password) {
  int length = strlen(password);
  if (length < 8) {
    error_exit("Password must be at least 8 characters long.");
  }

  bool has_upper = false, has_lower = false, has_digit = false,
       has_special = false;
  for (int i = 0; i < length; i++) {
    if (isupper(password[i])) {
      has_upper = true;
    } else if (islower(password[i])) {
      has_lower = true;
    } else if (isdigit(password[i])) {
      has_digit = true;
    } else {
      has_special = true;
    }
  }

  if (!has_upper || !has_lower || !has_digit || !has_special) {
    error_exit("Password must contain at least one uppercase letter, one "
               "lowercase letter, one digit, and one special character.");
  }
}

void generate_password(char *password, int length) {
  if (length < 8 || length > MAX_PASSWORD_LENGTH - 1) {
    printf("Invalid password length. Please enter a length between 8 and %d.\n",
           MAX_PASSWORD_LENGTH - 1);
    return;
  }

  char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456"
                   "789!@#$%^&*()-_=+[]{}|;:',.<>?";
  int charset_size = strlen(charset);

  srand(time(NULL));

  // Initialize flags to check if each required character type is included
  bool has_upper = false, has_lower = false, has_digit = false,
       has_special = false;

  // Create password with at least one character from each required type
  password[0] = charset[rand() % 26];      // at least one uppercase letter
  password[1] = charset[26 + rand() % 26]; // at least one lowercase letter
  password[2] = charset[52 + rand() % 10]; // at least one digit
  password[3] = charset[62 + rand() % 14]; // at least one special character

  // Fill the rest of the password randomly from the character set
  for (int i = 4; i < length; i++) {
    password[i] = charset[rand() % charset_size];
  }

  password[length] = '\0';

  // Shuffle the password characters
  for (int i = 0; i < length; i++) {
    int j = rand() % length;
    char temp = password[i];
    password[i] = password[j];
    password[j] = temp;
  }

  printf("Generated Password: %s\n", password);
}

void derive_key(unsigned char *key, const char *password, unsigned char *salt) {
  PKCS5_PBKDF2_HMAC(password, strlen(password), salt, SALT_SIZE,
                    PBKDF2_ITERATIONS, EVP_sha256(), AES_KEY_SIZE, key);
}

void encrypt_password(struct Password *password, const char *user_password) {
  unsigned char iv[AES_BLOCK_SIZE];
  unsigned char encrypted_password[MAX_PASSWORD_LENGTH];

  RAND_bytes(iv, AES_BLOCK_SIZE);

  unsigned char key[AES_KEY_SIZE];
  derive_key(key, user_password, iv);

  EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, key, iv);

  int outlen, tmplen;
  EVP_EncryptUpdate(ctx, encrypted_password, &outlen,
                    (unsigned char *)password->password,
                    strlen(password->password));
  EVP_EncryptFinal_ex(ctx, encrypted_password + outlen, &tmplen);

  memcpy(password->password, encrypted_password, outlen + tmplen);
  memcpy(password->username, iv, AES_BLOCK_SIZE);

  EVP_CIPHER_CTX_free(ctx);
}

void decrypt_password(struct Password *password, const char *user_password) {
  unsigned char iv[AES_BLOCK_SIZE];
  unsigned char decrypted_password[MAX_PASSWORD_LENGTH];
  int decrypted_len = 0;

  memcpy(iv, password->username, AES_BLOCK_SIZE);

  unsigned char key[AES_KEY_SIZE];
  derive_key(key, user_password, iv);

  EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, key, iv);

  int outlen, tmplen;
  EVP_DecryptUpdate(ctx, decrypted_password, &outlen,
                    (unsigned char *)password->password,
                    strlen(password->password));
  EVP_DecryptFinal_ex(ctx, decrypted_password + outlen, &tmplen);

  decrypted_password[outlen + tmplen] = '\0';

  strcpy(password->password, (char *)decrypted_password);

  EVP_CIPHER_CTX_free(ctx);
}

void save_password(struct Password *password, const char *website,
                   const char *dir, const char *user_password) {
  unsigned char iv[AES_BLOCK_SIZE];
  RAND_bytes(iv, AES_BLOCK_SIZE);

  unsigned char key[AES_KEY_SIZE];
  derive_key(key, user_password, iv);

  unsigned char encrypted_password[MAX_PASSWORD_LENGTH];
  EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, key, iv);

  int outlen, tmplen;
  EVP_EncryptUpdate(ctx, encrypted_password, &outlen,
                    (unsigned char *)password->password,
                    strlen(password->password));
  EVP_EncryptFinal_ex(ctx, encrypted_password + outlen, &tmplen);

  char filename[256];
  snprintf(filename, sizeof(filename), "%s/%s_%s.dat", dir, website,
           password->username);
  FILE *fp = fopen(filename, "wb");
  if (fp == NULL) {
    printf("Error: could not open file %s for writing.\n", filename);
    return;
  }
  fwrite(iv, sizeof(iv), 1, fp);
  fwrite(encrypted_password, outlen + tmplen, 1, fp);

  fclose(fp);
  EVP_CIPHER_CTX_free(ctx);
}

void edit_password(struct Password *password) {
  int choice;
  printf("\nEdit Password:\n");
  printf("1. Website: %s\n", password->website);
  printf("2. Username: %s\n", password->username);
  printf("3. Password: %s\n", password->password);
  printf("4. Cancel\n");
  printf("Enter your choice: ");
  scanf("%d", &choice);
  clear_input_buffer();

  switch (choice) {
  case 1: {
    char old_filename[256];
    snprintf(old_filename, sizeof(old_filename), "tmp/%s_%s.dat",
             password->website, password->username);
    if (remove(old_filename) != 0) {
      printf("Error deleting old password file.\n");
    }

    printf("Enter new website: ");
    fgets(password->website, sizeof(password->website), stdin);
    password->website[strcspn(password->website, "\n")] =
        0; // Remove newline character
    printf("Website updated.\n");
    save_password(password, password->website, "tmp", "user_password");
    break;
  }
  case 2: {
    char old_filename[256];
    snprintf(old_filename, sizeof(old_filename), "tmp/%s_%s.dat",
             password->website, password->username);
    if (remove(old_filename) != 0) {
      printf("Error deleting old password file.\n");
    }

    printf("Enter new username: ");
    fgets(password->username, sizeof(password->username), stdin);
    password->username[strcspn(password->username, "\n")] =
        0; // Remove newline character
    printf("Username updated.\n");
    save_password(password, password->website, "tmp", "user_password");
    break;
  }
  case 3: {
    int length;
    do {
      printf("Enter new password length: ");
      scanf("%d", &length);
      clear_input_buffer();
      if (length < 8 || length > MAX_PASSWORD_LENGTH - 1) {
        printf("Invalid password length. Please enter a length between 8 and "
               "%d.\n",
               MAX_PASSWORD_LENGTH - 1);
      }
    } while (length < 8 || length > MAX_PASSWORD_LENGTH - 1);
    generate_password(password->password, length);
    printf("Password updated.\n");
    save_password(password, password->website, "tmp", "user_password");
    break;
  }
  case 4:
    printf("Canceled.\n");
    break;
  default:
    printf("Invalid choice.\n");
    break;
  }
}

void load_passwords(const char *dir, const char *user_password) {
  printf("Available Passwords:\n");
  DIR *d;
  struct dirent *dir_entry;
  d = opendir(dir);
  if (d) {
    int count = 0;
    while ((dir_entry = readdir(d)) != NULL) {
      if (dir_entry->d_type == DT_REG) { // If it's a regular file
        char filename[256];
        snprintf(filename, sizeof(filename), "%s/%s", dir, dir_entry->d_name);
        FILE *file = fopen(filename, "rb");
        if (file != NULL) {
          struct Password password;

          unsigned char iv[AES_BLOCK_SIZE];
          fread(iv, sizeof(iv), 1, file);

          unsigned char encrypted_password[MAX_PASSWORD_LENGTH];
          int len = fread(encrypted_password, 1, MAX_PASSWORD_LENGTH, file);

          unsigned char key[AES_KEY_SIZE];
          derive_key(key, user_password, iv);

          unsigned char decrypted_password[MAX_PASSWORD_LENGTH];
          EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
          EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, key, iv);

          int outlen, tmplen;
          EVP_DecryptUpdate(ctx, decrypted_password, &outlen,
                            encrypted_password, len);
          EVP_DecryptFinal_ex(ctx, decrypted_password + outlen, &tmplen);

          decrypted_password[outlen + tmplen] = '\0';

          fclose(file);
          EVP_CIPHER_CTX_free(ctx);

          // Extract website and username from filename
          char website[MAX_WEBSITE_LENGTH];
          char username[MAX_USERNAME_LENGTH];
          sscanf(dir_entry->d_name, "%[^_]_%[^.]", website, username);

          // Copy decrypted password to password field
          strncpy(password.website, website, MAX_WEBSITE_LENGTH - 1);
          password.website[MAX_WEBSITE_LENGTH - 1] = '\0';
          strncpy(password.username, username, MAX_USERNAME_LENGTH - 1);
          password.username[MAX_USERNAME_LENGTH - 1] = '\0';
          strncpy(password.password, decrypted_password,
                  MAX_PASSWORD_LENGTH - 1);
          password.password[MAX_PASSWORD_LENGTH - 1] = '\0';

          passwords[count++] = password;
          printf("%d. Website: %s, Username: %s\n", count, password.website,
                 password.username);
        }
      }
    }
    numPasswords = count;
    closedir(d);
    if (count == 0) {
      printf("No passwords found.\n");
    }
  }
}

void copy_to_clipboard(char *text) {
  // Use xclip command to copy text to clipboard
  FILE *pipe = popen("xclip -selection clipboard", "w");
  if (pipe != NULL) {
    fprintf(pipe, "%s", text);
    pclose(pipe);
    printf("Copied to clipboard: %s\n", text);
  } else {
    printf("Error: Could not copy to clipboard.\n");
  }
}

void print_ascii_art(const char *filename) {
  FILE *file = fopen(filename, "r");
  if (file == NULL) {
    printf("Unable to open ASCII art file.\n");
    return;
  }

  // Get console width
  struct winsize w;
  ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
  int console_width = w.ws_col;

  char line[256];
  while (fgets(line, sizeof(line), file)) {
    // Calculate padding for centering
    int padding = (console_width - strlen(line)) / 2;
    if (padding > 0) {
      for (int i = 0; i < padding; i++) {
        printf(" ");
      }
    }
    printf("%s", line);
  }

  fclose(file);
}

int main() {
  initialize_log();

  const char *dir = "tmp"; // Directory to store password files
  mkdir(dir, 0700);        // Create directory if it doesn't exist

  char user_password[MAX_PASSWORD_LENGTH];

  FILE *key_file = fopen("master.key", "rb");
  if (key_file == NULL) {
    printf("Enter your new master password: ");
    fgets(user_password, sizeof(user_password), stdin);
    user_password[strcspn(user_password, "\n")] = 0;

    // Save master password to file
    key_file = fopen("master.key", "wb");
    if (key_file == NULL) {
      printf("Error: Could not create master password file.\n");
      return 1;
    }
    fwrite(user_password, sizeof(user_password), 1, key_file);
    fclose(key_file);
  } else {
    // If master key exists, ask for the password
    printf("Enter your master password: ");
    fgets(user_password, sizeof(user_password), stdin);
    user_password[strcspn(user_password, "\n")] = 0; // Remove newline character

    // Check if the entered password matches the stored master password
    char stored_password[MAX_PASSWORD_LENGTH];
    fread(stored_password, sizeof(stored_password), 1, key_file);
    fclose(key_file);
    if (strcmp(user_password, stored_password) != 0) {
      printf("Invalid master password. Exiting...\n");
      return 1;
    }
  }

  // Print ASCII art
  print_ascii_art("ascii-art.txt");

  int choice;
  while (1) {
    printf("\n1. Create New Password\n");
    printf("2. Show Passwords\n");
    printf("3. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);
    clear_input_buffer(); // Clear input buffer after scanf

    switch (choice) {
    case 1: {
      struct Password password;
      int length;
      do {
        printf("Enter password length: ");
        scanf("%d", &length);
        clear_input_buffer();
        if (length < 8 || length > MAX_PASSWORD_LENGTH - 1) {
          printf("Invalid password length. Please enter a length between 8 and "
                 "%d.\n",
                 MAX_PASSWORD_LENGTH - 1);
        }
      } while (length < 8 || length > MAX_PASSWORD_LENGTH - 1);
      generate_password(password.password, length);
      printf("Enter website: ");
      fgets(password.website, sizeof(password.website), stdin);
      password.website[strcspn(password.website, "\n")] =
          0; // Remove newline character
      printf("Enter username: ");
      fgets(password.username, sizeof(password.username), stdin);
      password.username[strcspn(password.username, "\n")] =
          0; // Remove newline character
      save_password(&password, password.website, dir, user_password);
      printf("Password information saved to %s\n", password.website);
      break;
    }
    case 2: {
      load_passwords(dir, user_password);
      printf("Enter the number of the password to view or 0 to cancel: ");
      int selection;
      scanf("%d", &selection);
      clear_input_buffer();
      if (selection > 0 && selection <= numPasswords) {
        struct Password selected_password = passwords[selection - 1];
        printf("Selected Password:\n");
        printf("- Website: %s\n", selected_password.website);
        printf("- Username: %s\n", selected_password.username);
        printf("- Password: %s\n", selected_password.password);
        printf("1. Copy Username and/or Password\n");
        printf("2. Edit the Password\n");
        printf("3. Delete the Password\n");
        printf("0. Cancel\n");
        printf("Enter your choice: ");
        int copy_choice;
        scanf("%d", &copy_choice);
        clear_input_buffer();
        switch (copy_choice) {
        case 1:
          // Provide options for copying username and/or password
          printf("1. Copy Username and Password\n");
          printf("2. Copy just the Password\n");
          printf("3. Copy just the Username\n");
          printf("0. Cancel\n");
          printf("Enter your choice: ");
          int sub_copy_choice;
          scanf("%d", &sub_copy_choice);
          clear_input_buffer();
          switch (sub_copy_choice) {
          case 1:
            // Copy both username and password
            {
              char copy_text[MAX_PASSWORD_LENGTH * 2 +
                             2]; // Max length of both plus space
              snprintf(copy_text, sizeof(copy_text),
                       "Username: %s, Password: %s", selected_password.username,
                       selected_password.password);
              copy_to_clipboard(copy_text);
              printf("Username and Password copied to clipboard.\n");
            }
            break;
          case 2:
            // Copy just the password
            copy_to_clipboard(selected_password.password);
            printf("Password copied to clipboard.\n");
            break;
          case 3:
            // Copy just the username
            copy_to_clipboard(selected_password.username);
            printf("Username copied to clipboard.\n");
            break;
          case 0:
            printf("Canceled.\n");
            break;
          default:
            printf("Invalid choice.\n");
          }
          break;
        case 2:
          edit_password(&selected_password);
          save_password(&selected_password, selected_password.website, dir,
                        user_password);
          printf("Password updated.\n");
          break;
        case 3: {
          struct Password selected_password = passwords[selection - 1];
          printf("Are you sure you want to delete this password? (y/n): ");
          char confirm;
          scanf(" %c", &confirm);
          clear_input_buffer();
          if (confirm == 'y' || confirm == 'Y') {
            char filename[256];
            snprintf(filename, sizeof(filename), "%s/%s_%s.dat", dir,
                     selected_password.website, selected_password.username);
            if (remove(filename) != 0) {
              printf("Error deleting password file.\n");
            } else {
              printf("Password deleted.\n");
              // Shift remaining passwords to fill the gap
              for (int i = selection - 1; i < numPasswords - 1; i++) {
                passwords[i] = passwords[i + 1];
              }
              numPasswords--;
            }
          } else {
            printf("Password not deleted.\n");
          }
          break;
        }

        case 0:
          printf("Canceled.\n");
          break;
        default:
          printf("Invalid choice.\n");
        }
      } else if (selection != 0) {
        printf("Invalid selection.\n");
      }
      break;
    }
    case 3:
      printf("Exiting program...\n");
      close_log();
      return 0;
    default:
      printf("Invalid choice. Please enter 1, 2, or 3.\n");
    }
  }

  close_log();
  return 0;
}