libgnunetchat

library for GNUnet Messenger
Log | Files | Refs | README | LICENSE

commit 6a980a285fcac517bd266e326e0da68680c2dce4
parent 3190e88262a22b847b6f2c73ebf01ceae726c153
Author: Jacki <jacki@thejackimonster.de>
Date:   Sun, 21 Apr 2024 23:32:56 +0200

Allow files without additional encryption key

Signed-off-by: Jacki <jacki@thejackimonster.de>

Diffstat:
Msrc/gnunet_chat_file.c | 40++++++++++++++++++++++++++++++++++++----
Msrc/gnunet_chat_file.h | 2+-
Msrc/gnunet_chat_lib.c | 20++++++++------------
Msrc/gnunet_chat_util.c | 18+++++++++++++-----
4 files changed, 58 insertions(+), 22 deletions(-)

diff --git a/src/gnunet_chat_file.c b/src/gnunet_chat_file.c @@ -26,6 +26,7 @@ #include "gnunet_chat_context.h" #include <gnunet/gnunet_common.h> +#include <string.h> struct GNUNET_CHAT_File* file_create_from_message (struct GNUNET_CHAT_Handle *handle, @@ -35,10 +36,21 @@ file_create_from_message (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_File* file = GNUNET_new(struct GNUNET_CHAT_File); + if (!file) + return NULL; + file->handle = handle; file->name = GNUNET_strndup(message->name, NAME_MAX); - GNUNET_memcpy(&(file->key), &(message->key), sizeof(file->key)); + file->key = GNUNET_new(struct GNUNET_CRYPTO_SymmetricSessionKey); + + if (!(file->key)) + { + GNUNET_free(file); + return NULL; + } + + GNUNET_memcpy(&(file->key), &(message->key), sizeof(message->key)); GNUNET_memcpy(&(file->hash), &(message->hash), sizeof(file->hash)); file->meta = GNUNET_FS_meta_data_create(); @@ -78,10 +90,14 @@ file_create_from_chk_uri (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_File* file = GNUNET_new(struct GNUNET_CHAT_File); + if (!file) + return NULL; + file->handle = handle; file->name = NULL; - memset(&(file->key), 0, sizeof(file->key)); + file->key = NULL; + GNUNET_memcpy(&(file->hash), hash, sizeof(file->hash)); file->meta = GNUNET_FS_meta_data_create(); @@ -118,10 +134,21 @@ file_create_from_disk (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_File* file = GNUNET_new(struct GNUNET_CHAT_File); + if (!file) + return NULL; + file->handle = handle; file->name = GNUNET_strndup(name, NAME_MAX); - GNUNET_memcpy(&(file->key), key, sizeof(file->key)); + file->key = GNUNET_new(struct GNUNET_CRYPTO_SymmetricSessionKey); + + if (!(file->key)) + { + GNUNET_free(file); + return NULL; + } + + GNUNET_memcpy(file->key, key, sizeof(*key)); GNUNET_memcpy(&(file->hash), hash, sizeof(file->hash)); file->meta = GNUNET_FS_meta_data_create(); @@ -296,7 +323,12 @@ file_update_upload (struct GNUNET_CHAT_File *file, struct GNUNET_MESSENGER_Message msg; msg.header.kind = GNUNET_MESSENGER_KIND_FILE; - GNUNET_memcpy(&(msg.body.file.key), &(file->key), sizeof(file->key)); + + if (file->key) + GNUNET_memcpy(&(msg.body.file.key), file->key, sizeof(msg.body.file.key)); + else + memset(&(msg.body.file.key), 0, sizeof(msg.body.file.key)); + GNUNET_memcpy(&(msg.body.file.hash), &(file->hash), sizeof(file->hash)); GNUNET_strlcpy(msg.body.file.name, file->name, NAME_MAX); msg.body.file.uri = GNUNET_FS_uri_to_string(file->uri); diff --git a/src/gnunet_chat_file.h b/src/gnunet_chat_file.h @@ -76,7 +76,7 @@ struct GNUNET_CHAT_File char *name; struct GNUNET_HashCode hash; - struct GNUNET_CRYPTO_SymmetricSessionKey key; + struct GNUNET_CRYPTO_SymmetricSessionKey *key; struct GNUNET_FS_MetaData *meta; diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -833,22 +833,13 @@ GNUNET_CHAT_upload_file (struct GNUNET_CHAT_Handle *handle, return NULL; } - struct GNUNET_CRYPTO_SymmetricSessionKey key; - GNUNET_CRYPTO_symmetric_create_session_key(&key); - - if (GNUNET_OK != util_encrypt_file(filename, &hash, &key)) - { - GNUNET_free(filename); - return NULL; - } - char* p = GNUNET_strdup(path); file = file_create_from_disk( handle, basename(p), &hash, - &key + NULL ); GNUNET_free(p); @@ -1868,7 +1859,12 @@ GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context, struct GNUNET_MESSENGER_Message msg; msg.header.kind = GNUNET_MESSENGER_KIND_FILE; - GNUNET_memcpy(&(msg.body.file.key), &(file->key), sizeof(file->key)); + + if (file->key) + GNUNET_memcpy(&(msg.body.file.key), file->key, sizeof(msg.body.file.key)); + else + memset(&(msg.body.file.key), 0, sizeof(msg.body.file.key)); + GNUNET_memcpy(&(msg.body.file.hash), &(file->hash), sizeof(file->hash)); GNUNET_strlcpy(msg.body.file.name, file->name, NAME_MAX); msg.body.file.uri = GNUNET_FS_uri_to_string(file->uri); @@ -2417,7 +2413,7 @@ GNUNET_CHAT_file_open_preview (struct GNUNET_CHAT_File *file) if ((GNUNET_OK != GNUNET_DISK_file_copy(filename, file->preview)) || (GNUNET_OK != util_decrypt_file(file->preview, - &(file->hash), &(file->key)))) + &(file->hash), file->key))) { GNUNET_free(file->preview); file->preview = NULL; diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c @@ -110,7 +110,7 @@ util_encrypt_file (const char *filename, const struct GNUNET_HashCode *hash, const struct GNUNET_CRYPTO_SymmetricSessionKey *key) { - GNUNET_assert((filename) && (hash) && (key)); + GNUNET_assert((filename) && (hash)); uint64_t size; @@ -141,10 +141,13 @@ util_encrypt_file (const char *filename, struct GNUNET_CRYPTO_SymmetricInitializationVector iv; const uint64_t block_size = 1024*1024; - ssize_t result = -1; + ssize_t result = 0; const uint64_t blocks = ((size + block_size - 1) / block_size); + if (!key) + goto skip_encryption; + for (uint64_t i = 0; i < blocks; i++) { const uint64_t index = (blocks - i - 1); @@ -170,6 +173,7 @@ util_encrypt_file (const char *filename, break; } +skip_encryption: if (GNUNET_OK != GNUNET_DISK_file_unmap(mapping)) result = -1; @@ -190,7 +194,7 @@ util_decrypt_file (const char *filename, const struct GNUNET_HashCode *hash, const struct GNUNET_CRYPTO_SymmetricSessionKey *key) { - GNUNET_assert((filename) && (hash) && (key)); + GNUNET_assert((filename) && (hash)); uint64_t size; @@ -218,10 +222,14 @@ util_decrypt_file (const char *filename, struct GNUNET_CRYPTO_SymmetricInitializationVector iv; const uint64_t block_size = 1024*1024; - ssize_t result = -1; + struct GNUNET_HashCode check; + ssize_t result = 0; const uint64_t blocks = ((size + block_size - 1) / block_size); + if (!key) + goto skip_decryption; + for (uint64_t index = 0; index < blocks; index++) { const uint64_t offset = block_size * index; @@ -246,7 +254,7 @@ util_decrypt_file (const char *filename, break; } - struct GNUNET_HashCode check; +skip_decryption: GNUNET_CRYPTO_hash(data, size, &check); if (0 != GNUNET_CRYPTO_hash_cmp(hash, &check))