From 6e3bd954e5f278f7d3135ef2322f43ffaefa8e38 Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Sat, 26 Aug 2023 02:05:10 +0200 Subject: [PATCH] Remove string size limit --- Password_manager/include/Buffer.h | 3 +- Password_manager/include/arg_func.h | 6 +- Password_manager/include/func.h | 26 +++- Password_manager/source/arg_func.cpp | 210 ++++++++++++++++----------- Password_manager/source/buffer.cpp | 12 +- Password_manager/source/func.cpp | 95 +++++++++--- Password_manager/source/main.cpp | 161 ++++++++++---------- 7 files changed, 320 insertions(+), 193 deletions(-) diff --git a/Password_manager/include/Buffer.h b/Password_manager/include/Buffer.h index 04b7900..a09fb9d 100644 --- a/Password_manager/include/Buffer.h +++ b/Password_manager/include/Buffer.h @@ -12,7 +12,8 @@ public: Buffer(); ~Buffer(); bool resize(size_t new_size); - int add(uint8_t* data, size_t data_size); + int add_end(uint8_t* data, size_t data_size); + int add_middle(uint8_t* data, size_t data_size, size_t index); // Removes data from buffer without checking if it's in the buffer void remove_fast(uint8_t* data, size_t data_size); diff --git a/Password_manager/include/arg_func.h b/Password_manager/include/arg_func.h index 6fa089b..09c94fe 100644 --- a/Password_manager/include/arg_func.h +++ b/Password_manager/include/arg_func.h @@ -5,9 +5,9 @@ class Buffer; class Cryptography; #include -Pass* arg_get(Buffer& decrypted_buffer, const char* label); +const char* arg_get(Buffer& decrypted_buffer, const char* label); -Pass* arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto); +const char* arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto); void arg_list(Buffer& decrypted_buffer); @@ -15,7 +15,7 @@ void arg_delete(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* void arg_print_all_p(Buffer& decrypted_buffer, std::string& user_pass); -Pass* arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto); +const char* arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto); void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string& user_pass, Cryptography& crypto); diff --git a/Password_manager/include/func.h b/Password_manager/include/func.h index 064b942..1f50783 100644 --- a/Password_manager/include/func.h +++ b/Password_manager/include/func.h @@ -1,16 +1,20 @@ #pragma once #include -constexpr auto MAX_STRING_SIZE = 21; +constexpr auto MAX_STRING_SIZE = 15; class Buffer; struct Pass { - char label[MAX_STRING_SIZE]; - char password[MAX_STRING_SIZE]; + uint32_t label; + uint32_t password; +}; - Pass() = default; +struct Index +{ + uint32_t count; + uint32_t offset; }; enum class Arg @@ -26,13 +30,19 @@ enum class Arg Error // error }; -Pass* find_password(Buffer* buff, const char* label); +int find_password_in_buffer(Buffer& buff, const char* label); -void generate_password(char* password); +void delete_password_from_buffer(Buffer& in, Buffer& out, int index_of_pass); -bool save_buffer_to_file(Buffer* buffer); +void add_password_to_buffer(Buffer& in, const char* label, const char* password); -bool load_buffer_from_file(Buffer* buffer); +const char* get_password_from_buffer(Buffer& decrypted_buffer, int label); + +void generate_password(std::string& password); + +bool save_buffer_to_file(Buffer& buffer); + +bool load_buffer_from_file(Buffer& buffer); void print_usage(); diff --git a/Password_manager/source/arg_func.cpp b/Password_manager/source/arg_func.cpp index 2458832..d047f56 100644 --- a/Password_manager/source/arg_func.cpp +++ b/Password_manager/source/arg_func.cpp @@ -8,67 +8,145 @@ #include "cryptography.h" -Pass* arg_get(Buffer& decrypted_buffer, const char* label) +const char*arg_get(Buffer& decrypted_buffer, const char* label) { - Pass* pass = find_password(&decrypted_buffer, label); - if (!pass) printf_s("Password not found\n"); - return pass; + int pass = find_password_in_buffer(decrypted_buffer, label); + if (pass < 0) { + printf_s("Password not found\n"); + return nullptr; + } + return get_password_from_buffer(decrypted_buffer, pass); } -Pass* arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto) +const char* arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto) { + if (decrypted_buffer.taken < 8) { + Index index = { 0, 0 }; + index.offset = sizeof(Index); + decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index)); + } + printf_s("Generating password for %s\n", label); - Pass* pass = find_password(&decrypted_buffer, label); - Pass new_pass = { 0 }; - if (pass != nullptr) generate_password(pass->password); + + int pass = find_password_in_buffer(decrypted_buffer, label); + + std::string password(MAX_STRING_SIZE, 0); + + generate_password(password); + + + if (pass < 0) + { + add_password_to_buffer(decrypted_buffer, label, password.c_str()); + crypto.encrypt(&decrypted_buffer, &encrypted_buffer); + save_buffer_to_file(encrypted_buffer); + + Index* index = (Index*)decrypted_buffer.buffer; + + return get_password_from_buffer(decrypted_buffer, index->count - 1); + + } else { - generate_password(new_pass.password); - strcpy_s(new_pass.label, MAX_STRING_SIZE, label); - int position = decrypted_buffer.add((uint8_t*)&new_pass, sizeof(Pass)); - if (position < 0) - { - printf_s("Error adding password\n"); - return nullptr; - } - pass = (Pass*)(decrypted_buffer.buffer + position); + + delete_password_from_buffer(decrypted_buffer, encrypted_buffer, pass); + add_password_to_buffer(encrypted_buffer, label, password.c_str()); + crypto.encrypt(&encrypted_buffer, &decrypted_buffer); + save_buffer_to_file(decrypted_buffer); + + Index* index = (Index*)encrypted_buffer.buffer; + + return get_password_from_buffer(encrypted_buffer, index->count - 1); + } - crypto.encrypt(&decrypted_buffer, &encrypted_buffer); - save_buffer_to_file(&encrypted_buffer); - return pass; + + return 0; +} + +const char* arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto) +{ + if (decrypted_buffer.taken < 8) { + Index index = { 0, 0 }; + index.offset = sizeof(Index); + decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index)); + } + + printf_s("Input password for %s:", label); + + std::string new_string = get_user_password(); + + if (new_string.empty()) + { + printf_s("error getting password\n"); + return nullptr; + } + + int pass = find_password_in_buffer(decrypted_buffer, label); + + if (pass < 0) + { + add_password_to_buffer(decrypted_buffer, label, new_string.c_str()); + crypto.encrypt(&decrypted_buffer, &encrypted_buffer); + save_buffer_to_file(encrypted_buffer); + + Index* index = (Index*)encrypted_buffer.buffer; + + return get_password_from_buffer(encrypted_buffer, index->count - 1); + + + } + else + { + delete_password_from_buffer(decrypted_buffer, encrypted_buffer, pass); + add_password_to_buffer(encrypted_buffer, label, new_string.c_str()); + crypto.encrypt(&encrypted_buffer, &decrypted_buffer); + save_buffer_to_file(decrypted_buffer); + + Index* index = (Index*)decrypted_buffer.buffer; + + return get_password_from_buffer(decrypted_buffer, index->count - 1); + + } + + return 0; } void arg_list(Buffer& decrypted_buffer) { - Pass* passwords = (Pass*)decrypted_buffer.buffer; - for (int i = 0; i < decrypted_buffer.taken / sizeof(Pass); i++) + Index* index = (Index*)decrypted_buffer.buffer; + Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index)); + + for (size_t i = 0; i < index->count; i++) { - printf_s("%s\n", passwords[i].label); + printf_s("label: %s\n", decrypted_buffer.buffer + pass[i].label + index->offset); } } -void arg_delete(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto) +void arg_delete(Buffer& in_buffer, Buffer& out_buffer, const char* label_to_del, Cryptography& crypto) { - printf_s("Deleting password for %s\n", label); - Pass* pass = find_password(&decrypted_buffer, label); - if (!pass) + printf_s("Deleting password for %s\n", label_to_del); + int pass = find_password_in_buffer(in_buffer, label_to_del); + if (pass < 0) { printf_s("Password not found\n"); return; } - else - { - decrypted_buffer.remove_fast((uint8_t*)pass, sizeof(Pass)); - } - crypto.encrypt(&decrypted_buffer, &encrypted_buffer); - save_buffer_to_file(&encrypted_buffer); + + arg_list(in_buffer); + + delete_password_from_buffer(in_buffer, out_buffer, pass); + + arg_list(out_buffer); + + crypto.encrypt(&out_buffer, &in_buffer); + save_buffer_to_file(in_buffer); printf_s("Password deleted\n"); } void arg_print_all_p(Buffer& decrypted_buffer, std::string& user_pass) { - printf_s("Input main password for confirmation:\n"); - std::string new_string = get_passwd(); + printf_s("Input main password for confirmation:"); + std::string new_string = get_user_password(); if (new_string.empty()) { printf_s("Error getting password\n"); @@ -79,57 +157,24 @@ void arg_print_all_p(Buffer& decrypted_buffer, std::string& user_pass) printf_s("Wrong password\n"); return; } - Pass* passwords = (Pass*)decrypted_buffer.buffer; - for (int i = 0; i < decrypted_buffer.taken / sizeof(Pass); i++) + + Index* index = (Index*)decrypted_buffer.buffer; + Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index)); + + for (size_t i = 0; i < index->count; i++) { - printf_s("%s: %s\n", passwords[i].label, passwords[i].password); + printf_s("label: %s\t\t\t", decrypted_buffer.buffer + pass[i].label + index->offset); + printf_s("password: %s\n", decrypted_buffer.buffer + pass[i].password + index->offset); } } -Pass* arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto) -{ - printf_s("Input password for %s:\n", label); - std::string new_string = get_passwd(); - - - if (new_string.empty()) - { - printf_s("Error getting password\n"); - return nullptr; - } - - Pass* pass = find_password(&decrypted_buffer, label); - Pass new_pass = { 0 }; - - - if (pass != nullptr) - { - strcpy_s(pass->password, MAX_STRING_SIZE, new_string.c_str()); - } - else - { - strcpy_s(new_pass.password, MAX_STRING_SIZE, new_string.c_str()); - strcpy_s(new_pass.label, MAX_STRING_SIZE, label); - int position = decrypted_buffer.add((uint8_t*)&new_pass, sizeof(Pass)); - if (position < 0) - { - printf_s("Error adding password\n"); - return nullptr; - } - pass = (Pass*)(decrypted_buffer.buffer + position); - } - - crypto.encrypt(&decrypted_buffer, &encrypted_buffer); - save_buffer_to_file(&encrypted_buffer); - return pass; -} void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string& user_pass, Cryptography& crypto) { printf_s("Input main password for confirmation:"); - std::string new_string = get_passwd(); + std::string new_string = get_user_password(); if (new_string.empty()) { printf_s("Error getting password\n"); @@ -143,7 +188,7 @@ void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string& } printf_s("Input new password:"); - new_string = get_passwd(); + new_string = get_user_password(); if (new_string.empty()) { printf_s("Error getting password\n"); @@ -151,7 +196,7 @@ void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string& } printf_s("Input new password again:"); - user_pass = get_passwd(); + user_pass = get_user_password(); if (user_pass.empty()) { printf_s("Error getting password\n"); @@ -166,15 +211,16 @@ void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string& crypto.generate_key_and_iv_from_password(new_string.c_str(), new_string.size()); crypto.encrypt(&decrypted_buffer, &encrypted_buffer); - save_buffer_to_file(&encrypted_buffer); + save_buffer_to_file(encrypted_buffer); printf_s("Password changed\n"); } void arg_show(Buffer& decrypted_buffer, const char* label) { - Pass* pass = find_password(&decrypted_buffer, label); - if (!pass) + int pass = find_password_in_buffer(decrypted_buffer, label); + + if (pass < 0) printf_s("Password not found\n"); else - printf_s("Password: %s\n", pass->password); + printf_s("Password: %s\n", get_password_from_buffer(decrypted_buffer, pass)); } \ No newline at end of file diff --git a/Password_manager/source/buffer.cpp b/Password_manager/source/buffer.cpp index 60e48aa..26770f7 100644 --- a/Password_manager/source/buffer.cpp +++ b/Password_manager/source/buffer.cpp @@ -33,7 +33,7 @@ bool Buffer::resize(size_t new_size) return true; } -int Buffer::add(uint8_t* data, size_t data_size) +int Buffer::add_end(uint8_t* data, size_t data_size) { if (taken + data_size > size) if (!resize(size + data_size)) return -1; @@ -43,6 +43,16 @@ int Buffer::add(uint8_t* data, size_t data_size) return taken - data_size; } +int Buffer::add_middle(uint8_t* data, size_t data_size, size_t index) +{ + if (taken + data_size > size) + if (!resize(size + data_size)) return -1; + memmove_s(buffer + index + data_size, size - index - data_size, buffer + index, taken - index); + memcpy_s(buffer + index, size - index, data, data_size); + taken += data_size; + return index; +} + void Buffer::remove_fast(uint8_t* data, size_t data_size) { int64_t index = data - buffer; diff --git a/Password_manager/source/func.cpp b/Password_manager/source/func.cpp index 7712ff8..fe4d793 100644 --- a/Password_manager/source/func.cpp +++ b/Password_manager/source/func.cpp @@ -2,21 +2,76 @@ #include "glob.h" #include "Buffer.h" -Pass* find_password(Buffer* buff, const char* label) +int find_password_in_buffer(Buffer& decrypted_buffer, const char* label) { - Pass* passwords = (Pass*)buff->buffer; + Index* index = (Index*)decrypted_buffer.buffer; + Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index)); - for (int i = 0; i < buff->taken / sizeof(Pass); i++) + for (size_t i = 0; i < index->count; i++) { - if (glob(label, passwords[i].label) == Glob_Result::MATCHED) - { - return &passwords[i]; - } + if (glob(label, (const char*) decrypted_buffer.buffer + pass[i].label + index->offset) == Glob_Result::MATCHED) + return i; } - return nullptr; + + return -1; } -void generate_password(char* password) +void delete_password_from_buffer(Buffer& in, Buffer& out, int index_of_pass) +{ + Index* index = (Index*)in.buffer; + Pass* pass = (Pass*)(in.buffer + sizeof(Index)); + + int size = sizeof(Index) + sizeof(Pass) * (index->count - 1); + + out.resize(size); + out.taken = size; + + ((Index*)out.buffer)[0].count = index->count - 1; + ((Index*)out.buffer)[0].offset = size; + int count = 0; + for (size_t i = 0; i < index->count; i++) + { + if (i == index_of_pass) continue; + + uint8_t* label = in.buffer + pass[i].label + index->offset; + size_t label_start = out.add_end(label, strlen((const char*)label) + 1); + + uint8_t* password = in.buffer + pass[i].password + index->offset; + size_t password_start = out.add_end(password, strlen((const char*)password) + 1); + + + pass = (Pass*)(out.buffer + sizeof(Index)); + pass[count].label = label_start - size; + pass[count].password = password_start - size; + count++; + } +} + +void add_password_to_buffer(Buffer& in, const char* label, const char* password) +{ + int label_start = in.add_end((uint8_t*)label, strlen(label) + 1); + int password_start = in.add_end((uint8_t*)password, strlen(password) + 1); + + Index* index = (Index*)in.buffer; + + label_start = label_start - index->offset; + password_start = password_start - index->offset; + Pass pass = { label_start, password_start }; + index->count++; + index->offset += sizeof(Pass); + + in.add_middle((uint8_t*)&pass, sizeof(Pass), index->offset - sizeof(Pass)); + +} + +const char* get_password_from_buffer(Buffer& decrypted_buffer, int label) +{ + Index* index = (Index*)decrypted_buffer.buffer; + Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index)); + return (const char*)decrypted_buffer.buffer + pass[label].password + index->offset; +} + +void generate_password(std::string& password) { srand(time(NULL)); char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_-+={[}]|:;<,>.?"; @@ -27,33 +82,34 @@ void generate_password(char* password) password[MAX_STRING_SIZE - 1] = '\0'; } -bool save_buffer_to_file(Buffer* buffer) +bool save_buffer_to_file(Buffer& buffer) { - std::ofstream file("passwords.bin", std::ios::binary); + std::ofstream file("passwords.bin", std::ios::binary | std::ios::out); if (!file.is_open()) { printf_s("Error saving file\n"); return false; } - file.write((char*)buffer->buffer, buffer->taken); + file.write((char*)buffer.buffer, buffer.taken); file.close(); return true; } -bool load_buffer_from_file(Buffer* buffer) +bool load_buffer_from_file(Buffer& buffer) { - std::ifstream file("passwords.bin", std::ios::binary); + std::ifstream file("passwords.bin", std::ios::binary | std::ios::in); if (!file.is_open()) return false; file.seekg(0, std::ios::end); - buffer->resize(file.tellg()); + size_t file_size = file.tellg(); + buffer.resize(file_size); file.seekg(0, std::ios::beg); - file.read((char*)buffer->buffer, buffer->size); + file.read((char*)buffer.buffer, buffer.size); if (file) - buffer->taken = buffer->size; + buffer.taken = file_size; else - buffer->taken = file.gcount(); + buffer.taken = file.gcount(); file.close(); return true; @@ -64,13 +120,12 @@ void print_usage() { printf_s(" Usage:\n\n"); printf_s(" password_manager.exe [flags]\n\n"); - printf_s(" Note: