/
main.cpp
1131 lines (960 loc) · 39.5 KB
/
main.cpp
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <filesystem>
#include <vector>
#include <sstream>
#include <fstream>
#include <unordered_map>
#include <algorithm>
#include "RSAKeyPairGenerator.h"
#include "RSAKeyPairValidator.h"
#include "RSAEncrypter.h"
#include "AESEncrypter.h"
#include <stdexcept>
std::string usernameChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
std::string filenameChars = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"/" // std::filesystem will resolve this
"0123456789"
"!#$%&'()*+,-.:;<=>?@[\\]^_`{|}~ "
"\""; // Double quote needs to be escaped and is added separately
// Function to check if the string contains only allowed characters
bool containsOnlyAllowedChars(const std::string &input, const std::string &allowedChars)
{
return std::all_of(input.begin(), input.end(), [&allowedChars](char c)
{
// Return true if the character is found in the allowedChars string, false otherwise
return allowedChars.find(c) != std::string::npos; });
}
// use this function when concatenating user input and path
std::filesystem::path sanitizePath(const std::filesystem::path basePath, const std::filesystem::path userPath)
{
std::filesystem::path sanitizedPath;
try
{
std::filesystem::path normalizedBasePath = std::filesystem::weakly_canonical(basePath);
std::filesystem::path normalizedUserPath = std::filesystem::weakly_canonical(normalizedBasePath / userPath);
std::filesystem::path absoluteBasePath = std::filesystem::absolute(normalizedBasePath);
std::filesystem::path absoluteUserPath = std::filesystem::absolute(normalizedUserPath);
if (absoluteUserPath.string().find(absoluteBasePath.string()) != 0)
{
throw std::runtime_error("Path escape attempt detected.");
}
return absoluteUserPath;
}
catch (const std::filesystem::filesystem_error &e)
{
throw std::runtime_error("Invalid path");
}
}
class User
{
public:
std::string username;
std::filesystem::path rootDir;
std::filesystem::path personalDir;
std::filesystem::path sharedDir;
std::filesystem::path privateKeyDir;
bool isAdmin = false;
User(const std::string &_username, const std::filesystem::path &_privateKeyDir, const bool _isAdmin) : username(_username), privateKeyDir(_privateKeyDir), isAdmin(_isAdmin)
{
if (isAdmin)
{
rootDir = std::filesystem::current_path() / "filesystem";
personalDir = rootDir;
sharedDir = rootDir;
if (!std::filesystem::exists(rootDir))
{
std::filesystem::create_directories(rootDir);
}
if (!std::filesystem::exists(personalDir))
{
std::filesystem::create_directories(personalDir);
}
if (!std::filesystem::exists(sharedDir))
{
std::filesystem::create_directories(sharedDir);
}
}
else
{
rootDir = std::filesystem::current_path() / "filesystem" / username;
personalDir = rootDir / "personal";
sharedDir = rootDir / "shared";
if (!std::filesystem::exists(rootDir))
{
std::filesystem::create_directories(rootDir);
}
if (!std::filesystem::exists(personalDir))
{
std::filesystem::create_directories(personalDir);
}
if (!std::filesystem::exists(sharedDir))
{
std::filesystem::create_directories(sharedDir);
}
}
}
std::filesystem::path getPublicKeyPath()
{
std::filesystem::path pkeyPath = sanitizePath(std::filesystem::current_path() / "public_keys",
username + "_public.pem");
if (std::filesystem::exists(pkeyPath))
{
return pkeyPath;
}
else
{
throw std::runtime_error("User: Cannot find public key of user " + username + ".");
}
}
std::filesystem::path getPrivateKeyPath()
{
std::filesystem::path pkeyPath = sanitizePath(std::filesystem::current_path(), privateKeyDir);
if (std::filesystem::exists(pkeyPath))
{
return pkeyPath;
}
else
{
throw std::runtime_error("User: Cannot find public key of user " + username + ".");
}
}
};
class EncryptedFileSystem
{
private:
std::unordered_map<std::string, User> users;
User *currentUser;
std::string currentUserName;
std::filesystem::path currentDirectory;
bool isOutOfBound = false;
static bool generateUserKeyPair(const std::string &username, const std::string &privateKeyPath)
{
std::filesystem::path userPrivateKeyPath = privateKeyPath;
std::filesystem::path userPublicKeyPath;
try
{
userPublicKeyPath = sanitizePath(std::filesystem::current_path() / "public_keys", username + "_public.pem");
}
catch (const std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
return false;
}
// std::filesystem::path userPublicKeyPath = std::filesystem::current_path()/"public_keys"/ (username + "_public.pem");
RSAKeyPairGenerator generator(2048);
if (!generator.generateKeyPair())
{
std::cerr << "Failed to generate RSA key pair." << std::endl;
return false;
}
if (!generator.savePrivateKey(userPrivateKeyPath))
{
std::cerr << "Failed to save private key." << std::endl;
return false;
}
if (!generator.savePublicKey(userPublicKeyPath))
{
std::filesystem::remove(userPublicKeyPath);
std::cerr << "Failed to save public key." << std::endl;
return false;
}
std::cout << "RSA key pair generated and saved successfully." << std::endl;
return true;
}
static bool isUserExist(const std::string &username)
{
std::filesystem::path userPublicKeyPath = std::filesystem::current_path() / "public_keys" / (username + "_public.pem");
return std::filesystem::exists(userPublicKeyPath);
}
/*
bool isOutOfBound(const std::string &directory)
{
if (directory == "/" || directory == "~" || directory == "")
{
return false; // Go to root directory
}
std::filesystem::path userRoot = std::filesystem::weakly_canonical(currentUser->rootDir);
std::filesystem::path destDir = std::filesystem::weakly_canonical(currentDirectory / directory);
if (directory[0] == '/')
{
destDir = std::filesystem::weakly_canonical(currentUser->rootDir / directory.substr(1));
}
if (destDir.string().find(userRoot.string()) != 0)
{
// out of bound
return true;
}
return false;
}
*/
std::filesystem::path safePath(const std::string &directory)
{
if (directory == "/" || directory == "~" || directory == "")
{
return currentUser->rootDir; // Go to root directory
}
std::filesystem::path userRoot = std::filesystem::weakly_canonical(currentUser->rootDir);
std::filesystem::path newDir = std::filesystem::weakly_canonical(currentDirectory / directory);
if (directory[0] == '/')
{
newDir = std::filesystem::weakly_canonical(currentUser->rootDir / directory.substr(1));
}
if (newDir.string().find(userRoot.string()) != 0)
{
// out of bound
isOutOfBound = true;
newDir = currentUser->rootDir;
}
return newDir;
}
bool isInPersonalDir(const std::filesystem::path &dir) //, const std::string& username="")
{
/*
if(currentUser->isAdmin && !username.empty()) {
std::string personalDir = std::filesystem::absolute(sanitizePath(std::filesystem::current_path() / "filesystem", std::filesystem::path(username) / "/personal")).string();
std::string newDir = std::filesystem::absolute(dir).string();
return newDir.find(personalDir) == 0;
}
*/
std::string personalDir = std::filesystem::absolute(currentUser->personalDir).string();
std::string newDir = std::filesystem::absolute(dir).string();
return newDir.find(personalDir) == 0;
}
bool isInSharedDir(const std::filesystem::path &dir) //, const std::string& username="")
{
/*
if(currentUser->isAdmin && !username.empty()) {
std::string personalDir = std::filesystem::absolute(sanitizePath(std::filesystem::current_path() / "filesystem", std::filesystem::path(username) / "/shared")).string();
std::string newDir = std::filesystem::absolute(dir).string();
return newDir.find(personalDir) == 0;
}
*/
std::string sharedDir = std::filesystem::absolute(currentUser->sharedDir).string();
std::string newDir = std::filesystem::absolute(dir).string();
return newDir.find(sharedDir) == 0;
}
std::vector<unsigned char> encryptContents(const std::string& adminPublicKeyPath, const std::string& userPublicKeyPath, const std::vector<unsigned char> aesKey, const std::vector<unsigned char> contents)
{
// AES Key encryption flow
RSAEncrypter rsa;
// Encrypt the AES key using user's public RSA key file
std::vector<unsigned char> adminEncryptedAesKey = rsa.encrypt(adminPublicKeyPath, aesKey);
if (adminEncryptedAesKey.empty())
return {}; // TODO: need error message
std::vector<unsigned char> userEncryptedAesKey = rsa.encrypt(userPublicKeyPath, aesKey);
if (userEncryptedAesKey.empty())
return {}; // TODO: need error message
// File content encryption flow
// Encrypt contents using AES key and random IV
std::vector<unsigned char> encryptedContents = AESEncrypter::encrypt(contents, aesKey);
// combine encrypted AES key and encrypted contents
std::vector<unsigned char> combinedContents(adminEncryptedAesKey.size() + userEncryptedAesKey.size() + encryptedContents.size());
std::memcpy(combinedContents.data(), adminEncryptedAesKey.data(), adminEncryptedAesKey.size());
std::memcpy(combinedContents.data() + adminEncryptedAesKey.size(), userEncryptedAesKey.data(), userEncryptedAesKey.size());
std::memcpy(combinedContents.data() + adminEncryptedAesKey.size() + userEncryptedAesKey.size(), encryptedContents.data(), encryptedContents.size());
return combinedContents;
}
std::vector<unsigned char> decryptContents(const std::string& privateKeyPath, const bool isAdmin, const std::vector<unsigned char> encryptedBytes)
{
// allocate memory to separate data
std::vector<unsigned char> adminEncryptedAesKey(256); // MUST be 256 for AES-256 encryption (32 characters for AES-256)
std::vector<unsigned char> userEncryptedAesKey(256); // MUST be 256 for AES-256 encryption (32 characters for AES-256)
std::vector<unsigned char> encryptedContents(encryptedBytes.size() - 256 - 256);
// Separate encrypted AES key and encrypted data
std::memcpy(adminEncryptedAesKey.data(), encryptedBytes.data(), adminEncryptedAesKey.size());
std::memcpy(userEncryptedAesKey.data(), encryptedBytes.data() + adminEncryptedAesKey.size(), userEncryptedAesKey.size());
std::memcpy(encryptedContents.data(), encryptedBytes.data() + adminEncryptedAesKey.size() + userEncryptedAesKey.size(), encryptedContents.size());
// AES Key decryption flow
RSAEncrypter rsa;
std::vector<unsigned char> aesKey(32); // MUST be 32 for AES-256 encryption (32 characters for AES-256)
if(isAdmin)
{
// Decrypt the AES key using user's private RSA key file
aesKey = rsa.decrypt(privateKeyPath, adminEncryptedAesKey);
if (aesKey.empty())
return {}; // TODO: need error message
}
else
{
// Decrypt the AES key using user's private RSA key file
aesKey = rsa.decrypt(privateKeyPath, userEncryptedAesKey);
if (aesKey.empty())
return {}; // TODO: need error message
}
// File content decryption flow
// Decrypt contents using decrypted AES key
std::vector<unsigned char> decryptedContents = AESEncrypter::decrypt(encryptedContents, aesKey);
return decryptedContents;
}
std::vector<unsigned char> decryptContentsUsingAesKey(const std::vector<unsigned char> aesKey, const std::vector<unsigned char> encryptedBytes)
{
// allocate memory to separate data
std::vector<unsigned char> encryptedContents(encryptedBytes.size() - 256 - 256);
// Separate encrypted data
std::memcpy(encryptedContents.data(), encryptedBytes.data() + 256 + 256, encryptedContents.size());
// File content decryption flow
// Decrypt contents using decrypted AES key
std::vector<unsigned char> decryptedContents = AESEncrypter::decrypt(encryptedContents, aesKey);
return decryptedContents;
}
std::vector<unsigned char> decryptAesKey(const std::string& privateKeyPath, const bool isAdmin, const std::vector<unsigned char> encryptedBytes)
{
// allocate memory to separate data
std::vector<unsigned char> encryptedAesKey(256); // MUST be 256 for AES-256 encryption (32 characters for AES-256)
// Choose correct encrypted AES key
if(isAdmin)
{
std::memcpy(encryptedAesKey.data(), encryptedBytes.data(), encryptedAesKey.size());
}
else
{
std::memcpy(encryptedAesKey.data(), encryptedBytes.data() + encryptedAesKey.size(), encryptedAesKey.size());
}
// AES Key decryption flow
RSAEncrypter rsa;
// Decrypt the AES key using user's private RSA key file
std::vector<unsigned char> aesKey = rsa.decrypt(privateKeyPath, encryptedAesKey);
if (aesKey.empty())
return {}; // TODO: need error message
return aesKey;
}
public:
EncryptedFileSystem() : currentUser(nullptr)
{
createFileSystemDirectory();
createPublicKeysDirectory();
// create admin
if (!isUserExist("admin"))
{
std::string username = "admin";
if (isUserExist(username))
{
std::cerr << "User " << username << " already exists." << std::endl;
return;
}
std::string adminKeyPath;
std::cout << "Generating admin keys, please provide the path to save the private key:\n";
std::cin >> adminKeyPath;
if (!generateUserKeyPair(username, adminKeyPath))
{
std::cerr << "Error in generating admin keys.\n";
return;
}
}
}
void createPublicKeysDirectory()
{
std::filesystem::path publicKeysDir = std::filesystem::current_path() / "public_keys";
if (!std::filesystem::exists(publicKeysDir))
{
std::filesystem::create_directory(publicKeysDir);
}
}
void createFileSystemDirectory()
{
std::filesystem::path fileSystemDir = std::filesystem::current_path() / "filesystem";
if (!std::filesystem::exists(fileSystemDir))
{
std::filesystem::create_directory(fileSystemDir);
}
}
bool addUser(const std::string &username)
{
// limit username length
if (username.length() > 20)
{
std::cerr << "adduser: Please use a short username (less than 20 characters)." << std::endl;
return false;
}
if (!containsOnlyAllowedChars(username, usernameChars))
{
std::cerr << "adduser: Invalid character in username. Please use another one." << std::endl;
return false;
}
if (!currentUser->isAdmin)
{
std::cerr << "adduser: Forbidden" << std::endl;
return false;
}
if (username == "admin")
{
std::cerr << "adduser: you cannot add another admin." << std::endl;
return false;
}
if (isUserExist(username))
{
std::cerr << "User " << username << " already exists." << std::endl;
return false;
}
std::string userKeyPath;
std::cout << "Generating user keys, please provide the path to save the private key:\n";
std::cin >> userKeyPath;
if (!generateUserKeyPair(username, userKeyPath))
{
std::cerr << "Error in generating user keys.\n";
return false;
}
// initialize user directories
User newUser(username, userKeyPath, false);
return true;
}
bool authenticateUser(const std::string &username, const std::string &keyfilePath)
{
std::filesystem::path userPublicKeyPath;
try
{
userPublicKeyPath = sanitizePath(std::filesystem::current_path() / "public_keys", username + "_public.pem");
}
catch (std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
return false;
}
std::filesystem::path userPrivateKeyPath = keyfilePath;
RSAKeyPairValidator validator(userPublicKeyPath, userPrivateKeyPath);
if (!validator.loadKeys())
{ // If invalid key file, return error
std::cerr << "auth: Failed to load RSA keys." << std::endl;
return false;
}
if (!validator.validateKeyPair())
{
std::cerr << "auth: User validation failed." << std::endl;
return false; // Keyfile does not match, authentication failed
}
std::cout << "User authenticated" << std::endl;
// Check if the user exists in the system
bool userExist = isUserExist(username);
if (!userExist)
{
std::cerr << "auth: User " << username << " does not exist" << std::endl;
return false;
}
if (username == "admin")
{
currentUser = new User("admin", userPrivateKeyPath, true);
}
else
{
currentUser = new User(username, userPrivateKeyPath, false);
}
currentDirectory = currentUser->rootDir;
return true; // Authentication successful
}
void cd(const std::string &directory)
{
// out of bound check
std::filesystem::path newDir = safePath(directory);
if (isOutOfBound)
{
std::cerr << "cd: Destination is out of bound. Changing directory to user root /" << std::endl;
isOutOfBound = false;
}
if (std::filesystem::exists(newDir) && std::filesystem::is_directory(newDir))
{
currentDirectory = std::filesystem::canonical(newDir);
}
else
{
std::cout << "cd: Directory does not exist" << std::endl;
}
}
void pwd() const
{
// need to be changed - don't show path beyond user's root folder
std::filesystem::path relativePath = std::filesystem::relative(currentDirectory, currentUser->rootDir);
if (relativePath.string() == ".")
{
std::cout << "/" << std::endl;
}
else
{
std::cout << "/" << relativePath.string() << std::endl;
}
}
void ls() const
{
if (!std::filesystem::exists(currentDirectory) || !std::filesystem::is_directory(currentDirectory))
{
std::cerr << "ls: Invalid directory." << std::endl;
return;
}
// Show the current directory (.)
std::cout << "d -> ." << std::endl;
// Show the parent directory (..), but only if it's not the root of the filesystem
if (currentDirectory != currentUser->rootDir)
{
std::cout << "d -> .." << std::endl;
}
for (const auto &entry : std::filesystem::directory_iterator(currentDirectory))
{
std::cout << (entry.is_directory() ? "d" : "f") << " -> " << entry.path().filename().string() << std::endl;
}
}
void mkdir(const std::string &directoryName)
{
if (!containsOnlyAllowedChars(directoryName, filenameChars))
{
std::cerr << "mkdir: Invalid character in directory name. Please use another one." << std::endl;
return;
}
try
{
if (currentUser->isAdmin)
{
std::cerr << "mkdir: Forbidden. Admin only has read permission." << std::endl;
return;
}
std::filesystem::path newDir = safePath(directoryName);
if (isInPersonalDir(newDir))
{
if (std::filesystem::exists(newDir))
{
std::cerr << "mkdir: Directory already exists" << std::endl;
}
else
{
std::filesystem::create_directories(newDir);
}
}
else
{
std::cerr << "mkdir: Forbidden. User has no write permission on the destination." << std::endl;
}
}
catch (std::filesystem::filesystem_error &e)
{
std::cerr << "mkdir: Please use a shorter directory name." << std::endl;
}
}
void mkfile(const std::string &filename, const std::string &contents)
{
if (!containsOnlyAllowedChars(filename, filenameChars))
{
std::cerr << "mkfile: Invalid character in file name. Please use another one." << std::endl;
return;
}
try
{
if (currentUser->isAdmin)
{
std::cerr << "mkfile: Admin only has read permission." << std::endl;
return;
}
std::filesystem::path newFilePath = safePath(filename);
std::filesystem::path parentDir = newFilePath.parent_path();
if (!isInPersonalDir(parentDir))
{
std::cerr << "mkfile: Forbidden. Cannot create file here." << std::endl;
return;
}
if (std::filesystem::is_directory(newFilePath))
{
std::cerr << "mkfile: Destination is a directory." << std::endl;
return;
}
try
{
if (!std::filesystem::exists(parentDir))
{
if (!std::filesystem::create_directories(parentDir))
{
std::cerr << "mkfile: Failed to create parent directory for the new file." << std::endl;
return;
}
}
}
catch (const std::filesystem::filesystem_error &e)
{
std::cerr << "mkfile: Error in creating destination directory." << std::endl;
return;
}
bool fileExists = std::filesystem::exists(newFilePath);
// allocate memory to AES key
std::vector<unsigned char> aesKey(32); // MUST be 32 for AES-256 encryption (32 characters for AES-256)
if (fileExists)
{
// IMPORTANT: If the file exists, it should use the existing AES key to re-encrypt the content
// read binary from original file
std::ifstream ifileStream(newFilePath, std::ios::binary | std::ios::ate);
// Determine the file size
std::streamsize size = ifileStream.tellg();
ifileStream.seekg(0, std::ios::beg);
// determine shared file
std::vector<unsigned char> sharedFileMarker(1);
bool isSharedFile = false;
if (!ifileStream.read(reinterpret_cast<char *>(sharedFileMarker.data()), 1))
{
std::cerr << "mkfile: Failed to read the file: " << std::filesystem::relative(newFilePath, currentUser->rootDir) << std::endl;
return;
}
if (sharedFileMarker[0] == 1)
isSharedFile = true;
// Read content from file as binary
std::vector<unsigned char> encryptedBytes(size - 1);
if (!ifileStream.read(reinterpret_cast<char *>(encryptedBytes.data()), size - 1))
{
std::cerr << "mkfile: Failed to read the file: " << std::filesystem::relative(newFilePath, currentUser->rootDir) << std::endl;
return;
}
ifileStream.close();
if (isSharedFile)
{
std::cerr << "mkfile: cannot modify a shared file" << std::endl;
return;
}
// AES key decryption flow
std::string privateKeyFile = currentUser->getPrivateKeyPath();
aesKey = decryptAesKey(privateKeyFile, false, encryptedBytes);
}
else
{
// If the file is newly created we then randomly generate the AES key
aesKey = AESEncrypter::generateKey(32); // random AES key (32 characters for AES-256)
}
std::ofstream fileStream(newFilePath, std::ios::binary | std::ios::out | std::ios::trunc);
if (fileStream)
{
std::vector<unsigned char> contentsBinary(contents.begin(), contents.end()); // TODO: This should be the user input as binary, may need sanitize
std::string adminPublicKeyFile = sanitizePath(std::filesystem::current_path() / "public_keys", "admin_public.pem");
std::string userPublicKeyFile = currentUser->getPublicKeyPath();
// File content encryption flow
std::vector<unsigned char> encryptedContents = encryptContents(adminPublicKeyFile, userPublicKeyFile, aesKey, contentsBinary);
// write binary to file
const char newSharedFileMarker = 0x00;
fileStream.write(&newSharedFileMarker, 1);
fileStream.write(reinterpret_cast<const char *>(encryptedContents.data()), encryptedContents.size());
fileStream.close();
}
else
{
std::cerr << "mkfile: Forbidden. User has no write permission on the destination." << std::endl;
}
}
catch (std::filesystem::filesystem_error &e)
{
std::cerr << "mkfile: Please use a shorter filename." << std::endl;
}
}
void cat(const std::string &filename)
{
// admin user
/*
if (currentUser->isAdmin)
{
std::cerr << "cat: pending implementation for admin user to read user files." << std::endl;
return;
}
*/
// boundary check
std::filesystem::path filePath = safePath(filename);
if (!std::filesystem::exists(filePath))
{
std::cerr << "cat: File not found." << std::endl;
return;
}
if (std::filesystem::is_directory(filePath))
{
std::cerr << "cat: Destination is a directory." << std::endl;
return;
}
if (!isInPersonalDir(filePath) && !isInSharedDir(filePath))
{
std::cerr << "cat: Forbidden. Access denied." << std::endl;
return;
}
std::ifstream fileStream(filePath, std::ios::binary | std::ios::ate);
if (fileStream)
{
// Determine the file size
std::streamsize size = fileStream.tellg();
fileStream.seekg(0, std::ios::beg);
// determine shared file
std::vector<unsigned char> sharedFileMarker(1);
bool isSharedFile = false;
if (!fileStream.read(reinterpret_cast<char *>(sharedFileMarker.data()), 1))
{
std::cerr << "cat: Failed to read the file: " << std::filesystem::relative(filePath, currentUser->rootDir) << std::endl;
return;
}
if (sharedFileMarker[0] == 1)
isSharedFile = true;
// Read content from file as binary
std::vector<unsigned char> encryptedBytes(size - 1);
if (!fileStream.read(reinterpret_cast<char *>(encryptedBytes.data()), size - 1))
{
std::cerr << "cat: Failed to read the file: " << std::filesystem::relative(filePath, currentUser->rootDir) << std::endl;
return;
}
fileStream.close();
// decryption flow
std::string privateKeyFile = currentUser->getPrivateKeyPath();
// Decrypt contents and AES key
std::vector<unsigned char> aesKey = decryptAesKey(privateKeyFile, currentUser->isAdmin, encryptedBytes);
std::vector<unsigned char> decryptedContents = decryptContents(privateKeyFile, currentUser->isAdmin, encryptedBytes);
// If cat a shared file, locate and decrypt the original file
if (isSharedFile)
{
std::string originalFilePath(decryptedContents.begin(), decryptedContents.end()); // TODO: May need sanitize
std::ifstream oringialFileStream(originalFilePath, std::ios::binary | std::ios::ate);
if (oringialFileStream)
{
// Determine the file size
std::streamsize size = oringialFileStream.tellg();
oringialFileStream.seekg(0, std::ios::beg);
// Although it cannot be shared file, we still have to read the first byte
std::vector<unsigned char> sharedFileMarker2(1);
if (!oringialFileStream.read(reinterpret_cast<char *>(sharedFileMarker2.data()), 1))
{
std::cerr << "cat: Failed to read the file: " << std::filesystem::relative(originalFilePath, currentUser->rootDir) << std::endl;
return;
}
// Read content from file as binary
std::vector<unsigned char> originalEncryptedBytes(size - 1);
if (!oringialFileStream.read(reinterpret_cast<char *>(originalEncryptedBytes.data()), size - 1))
{
std::cerr << "cat: Failed to read the file: " << std::filesystem::relative(originalFilePath, currentUser->rootDir) << std::endl;
return;
}
oringialFileStream.close();
// decryption flow
// Decrypt contents
std::vector<unsigned char> decryptedOriginalContents = decryptContentsUsingAesKey(aesKey, originalEncryptedBytes);
std::cout << std::string(decryptedOriginalContents.begin(), decryptedOriginalContents.end()) << std::endl;
}
else
{
std::cerr << "cat: Error in reading original file." << std::endl;
}
}
// If cat a personal file, display decrypted contents
else
{
std::cout << std::string(decryptedContents.begin(), decryptedContents.end()) << std::endl;
}
}
else
{
std::cerr << "cat: Error reading file" << std::endl;
}
}
void share(const std::string &filename, const std::string &username)
{
if (currentUser->isAdmin)
{
std::cerr << "share: Admin only has read permission." << std::endl;
return;
}
if (username == "admin")
{
std::cerr << "share: Cannot share a file to admin." << std::endl;
return;
}
std::filesystem::path filePath = safePath(filename);
if (!std::filesystem::exists(filePath))
{
std::cerr << "share: File " << filename << " not exist." << std::endl;
return;
}
if (std::filesystem::is_directory(filePath))
{
std::cerr << "share: " << filename << " is a directory." << std::endl;
return;
}
std::filesystem::path sharedDir = sanitizePath(std::filesystem::current_path() / "filesystem", username + "/shared");
if (!isUserExist(username) || !std::filesystem::exists(sharedDir)) // bypass directory traversal
{
std::cerr << "share: User " << username << " not exist." << std::endl;
return;
}
std::filesystem::path targetPath = sanitizePath(sharedDir, currentUser->username + "_" + filename);
try
{
// This case we did not copy file content, we only store re-encrypted AES key and a link (encrypted) to original file
std::ifstream ifileStream(filePath, std::ios::binary | std::ios::ate);
// Determine the file size
std::streamsize size = ifileStream.tellg();
ifileStream.seekg(0, std::ios::beg);
// determine shared file
std::vector<unsigned char> sharedFileMarker(1);
bool isSharedFile = false;
if (!ifileStream.read(reinterpret_cast<char *>(sharedFileMarker.data()), 1))
{
std::cerr << "share: Failed to read the file: " << std::filesystem::relative(filePath, currentUser->rootDir) << std::endl;
return;
}
if (sharedFileMarker[0] == 1)
isSharedFile = true;
// Read content from file as binary
std::vector<unsigned char> encryptedBytes(size - 1);
if (!ifileStream.read(reinterpret_cast<char *>(encryptedBytes.data()), size - 1))
{
std::cerr << "share: Failed to read the file: " << std::filesystem::relative(filePath, currentUser->rootDir) << std::endl;
return;
}
ifileStream.close();
if (isSharedFile)
{
std::cerr << "share: cannot share a shared file" << std::endl;
return;
}
// allocate memory to AES key
std::vector<unsigned char> aesKey(32); // MUST be 32 for AES-256 encryption (32 characters for AES-256)
// AES key decryption flow
std::string privateKeyFile = currentUser->getPrivateKeyPath();
aesKey = decryptAesKey(privateKeyFile, false, encryptedBytes);
// AES key and file link re-encryption flow
std::string receiverPublicKeyFile = sanitizePath(std::filesystem::current_path() / "public_keys", username + "_public.pem");
std::string adminPublicKeyFile = sanitizePath(std::filesystem::current_path() / "public_keys", "admin_public.pem");
// TODO: Generate link to actual file and save full path as binary
std::string originalFilePathString = filePath.string();
std::vector<unsigned char> contentsBinary(originalFilePathString.begin(), originalFilePathString.end());
// File content encryption flow
std::vector<unsigned char> encryptedContents = encryptContents(adminPublicKeyFile, receiverPublicKeyFile, aesKey, contentsBinary);
// write binary to file
std::ofstream ofileStream(targetPath, std::ios::binary | std::ios::out | std::ios::trunc);
const char newSharedFileMarker = 0x01; // shared file
ofileStream.write(&newSharedFileMarker, 1);
ofileStream.write(reinterpret_cast<const char *>(encryptedContents.data()), encryptedContents.size());
ofileStream.close();
}
catch (std::filesystem::filesystem_error &e)
{
std::cerr << "share: Error during sharing a file." << std::endl;
return;
}
std::cout << "File shared successfully." << std::endl;
}
};
void processCommand(EncryptedFileSystem &filesystem, const std::string &command)
{
std::istringstream iss(command);
std::string cmd;
iss >> cmd;
if (cmd == "cd")
{
std::string dir;
iss >> dir;
filesystem.cd(dir);
}
else if (cmd == "pwd")
{
filesystem.pwd();