From c56dcb8315a74ec8720f77f7a09408e4587fbeaf Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Sat, 1 Mar 2025 15:27:50 +0100 Subject: [PATCH] pcryptofolder namespace and cleanup --- pclsync/pcloudcrypto.h | 113 -------------------- pclsync/{pcloudcrypto.c => pcryptofolder.c} | 58 +++++----- pclsync/pcryptofolder.h | 77 +++++++++++++ pclsync/pfolder.c | 16 ++- pclsync/pfs.c | 40 ++++--- pclsync/pfscrypto.c | 2 +- pclsync/pfscrypto.h | 2 +- pclsync/pfsfolder.c | 28 +++-- pclsync/pfstasks.c | 12 ++- pclsync/pmemlock.c | 6 +- pclsync/ppagecache.c | 16 ++- pclsync/ppathstatus.c | 17 ++- pclsync/psynclib.c | 32 +++--- 13 files changed, 219 insertions(+), 200 deletions(-) delete mode 100644 pclsync/pcloudcrypto.h rename pclsync/{pcloudcrypto.c => pcryptofolder.c} (98%) create mode 100644 pclsync/pcryptofolder.h diff --git a/pclsync/pcloudcrypto.h b/pclsync/pcloudcrypto.h deleted file mode 100644 index 1bef73c..0000000 --- a/pclsync/pcloudcrypto.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - Copyright (c) 2014 Anton Titov. - - Copyright (c) 2014 pCloud Ltd. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: Redistributions of source code must retain the above - copyright notice, this list of conditions and the following - disclaimer. Redistributions in binary form must reproduce the - above copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. Neither the name of pCloud Ltd nor the - names of its contributors may be used to endorse or promote - products derived from this software without specific prior written - permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL pCloud - Ltd BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - DAMAGE. -*/ - -#ifndef _PCLOUD_CRYPTO_H -#define _PCLOUD_CRYPTO_H - -#include "papi.h" -#include "pcrypto.h" -#include "pfsfolder.h" -#include "psynclib.h" - -#define PSYNC_CRYPTO_SYM_FLAG_ISDIR 1 - -#define PSYNC_CRYPTO_SECTOR_SIZE 4096 - -#define PSYNC_CRYPTO_MAX_ERROR 511 - -#define PSYNC_CRYPTO_UNLOADED_SECTOR_ENCODER \ - ((psync_crypto_aes256_sector_encoder_decoder_t)(PSYNC_CRYPTO_MAX_ERROR + 1)) -#define PSYNC_CRYPTO_LOADING_SECTOR_ENCODER \ - ((psync_crypto_aes256_sector_encoder_decoder_t)(PSYNC_CRYPTO_MAX_ERROR + 2)) -#define PSYNC_CRYPTO_FAILED_SECTOR_ENCODER \ - ((psync_crypto_aes256_sector_encoder_decoder_t)(PSYNC_CRYPTO_MAX_ERROR + 3)) - -void psync_cloud_crypto_clean_cachef(); - -int psync_cloud_crypto_setup(const char *password, const char *hint); -int psync_cloud_crypto_get_hint(char **hint); -int psync_cloud_crypto_start(const char *password); -int psync_cloud_crypto_stop(); -int psync_cloud_crypto_isstarted(); -int psync_cloud_crypto_reset(); -int psync_cloud_crypto_mkdir(psync_folderid_t folderid, const char *name, - const char **err, psync_folderid_t *newfolderid); -void psync_cloud_crypto_clean_cache(); - -psync_crypto_aes256_text_decoder_t -psync_cloud_crypto_get_folder_decoder(psync_fsfolderid_t folderid); -void psync_cloud_crypto_release_folder_decoder( - psync_fsfolderid_t folderid, psync_crypto_aes256_text_decoder_t decoder); -char * -psync_cloud_crypto_decode_filename(psync_crypto_aes256_text_decoder_t decoder, - const char *name); - -psync_crypto_aes256_text_encoder_t -psync_cloud_crypto_get_folder_encoder(psync_fsfolderid_t folderid); -void psync_cloud_crypto_release_folder_encoder( - psync_fsfolderid_t folderid, psync_crypto_aes256_text_encoder_t encoder); -char * -psync_cloud_crypto_encode_filename(psync_crypto_aes256_text_encoder_t encoder, - const char *name); - -psync_crypto_aes256_sector_encoder_decoder_t -psync_cloud_crypto_get_file_encoder(psync_fsfileid_t fileid, uint64_t hash, - int nonetwork); -psync_crypto_aes256_sector_encoder_decoder_t -psync_cloud_crypto_get_file_encoder_from_binresult(psync_fileid_t fileid, - binresult *res); -void psync_cloud_crypto_release_file_encoder( - psync_fsfileid_t fileid, uint64_t hash, - psync_crypto_aes256_sector_encoder_decoder_t encoder); - -char *psync_cloud_crypto_get_file_encoded_key(psync_fsfileid_t fileid, - uint64_t hash, size_t *keylen); -char *psync_cloud_crypto_get_new_encoded_key(uint32_t flags, size_t *keylen); -char * -psync_cloud_crypto_get_new_encoded_and_plain_key(uint32_t flags, size_t *keylen, - psync_symmetric_key_t *deckey); - -static inline int psync_crypto_is_error(const void *ptr) { - return (uintptr_t)ptr <= PSYNC_CRYPTO_MAX_ERROR; -} - -static inline int psync_crypto_to_error(const void *ptr) { - return -((int)(uintptr_t)ptr); -} - -int psync_crypto_change_passphrase(const char *oldpassphrase, - const char *newpassphrase, uint32_t flags, - char **privenc, char **sign); -int psync_crypto_change_passphrase_unlocked(const char *newpassphrase, - uint32_t flags, char **privenc, - char **sign); - -#endif diff --git a/pclsync/pcloudcrypto.c b/pclsync/pcryptofolder.c similarity index 98% rename from pclsync/pcloudcrypto.c rename to pclsync/pcryptofolder.c index e68ef07..de36436 100644 --- a/pclsync/pcloudcrypto.c +++ b/pclsync/pcryptofolder.c @@ -40,7 +40,7 @@ #include "papi.h" #include "pcache.h" -#include "pcloudcrypto.h" +#include "pcryptofolder.h" #include "pcrypto.h" #include "pfileops.h" #include "pfolder.h" @@ -96,11 +96,19 @@ typedef struct { void sha1_hex_null_term(const void *data, size_t len, char *out); -void psync_cloud_crypto_clean_cache() { +void pcryptofolder_clean_cache() { const char *prefixes[] = {"DKEY", "FKEY", "FLDE", "FLDD", "SEEN"}; pcache_clean_oneof(prefixes, ARRAY_SIZE(prefixes)); } +static inline int psync_crypto_is_error(const void *ptr) { + return (uintptr_t)ptr <= PSYNC_CRYPTO_MAX_ERROR; +} + +static inline int psync_crypto_to_error(const void *ptr) { + return -((int)(uintptr_t)ptr); +} + static void psync_cloud_crypto_setup_save_to_db( const unsigned char *rsapriv, size_t rsaprivlen, const unsigned char *rsapub, size_t rsapublen, const unsigned char *salt, @@ -358,7 +366,7 @@ psync_cloud_crypto_setup_upload(const unsigned char *rsapriv, size_t rsaprivlen, * */ -int psync_cloud_crypto_setup(const char *password, const char *hint) { +int pcryptofolder_setup(const char *password, const char *hint) { unsigned char salt[PSYNC_CRYPTO_PBKDF2_SALT_LEN]; char publicsha1[PSYNC_SHA1_DIGEST_HEXLEN + 2], privatesha1[PSYNC_SHA1_DIGEST_HEXLEN + 2]; @@ -445,7 +453,7 @@ int psync_cloud_crypto_setup(const char *password, const char *hint) { return PSYNC_CRYPTO_SETUP_SUCCESS; } -int psync_cloud_crypto_get_hint(char **hint) { +int pcryptofolder_get_hint(char **hint) { binparam params[] = {PAPI_STR("auth", psync_my_auth)}; psock_t *api; binresult *res; @@ -517,7 +525,7 @@ static int crypto_keys_match() { return res; } -int psync_cloud_crypto_start(const char *password) { +int pcryptofolder_unlock(const char *password) { char publicsha1[PSYNC_SHA1_DIGEST_HEXLEN + 2], privatesha1[PSYNC_SHA1_DIGEST_HEXLEN + 2]; psync_sql_res *res; @@ -664,7 +672,7 @@ static void psync_fs_refresh_crypto_folders() { psync_free(fids); } -int psync_cloud_crypto_stop() { +int pcryptofolder_lock() { crypto_started_un = 0; pthread_rwlock_wrlock(&crypto_lock); if (!crypto_started_l) { @@ -678,12 +686,12 @@ int psync_cloud_crypto_stop() { crypto_privkey = PSYNC_INVALID_RSA; pthread_rwlock_unlock(&crypto_lock); debug(D_NOTICE, "stopped crypto"); - psync_cloud_crypto_clean_cache(); + pcryptofolder_clean_cache(); psync_fs_refresh_crypto_folders(); return PSYNC_CRYPTO_STOP_SUCCESS; } -int psync_cloud_crypto_isstarted() { +int pcryptofolder_is_unlocked() { int ret; pthread_rwlock_rdlock(&crypto_lock); ret = crypto_started_l; @@ -691,7 +699,7 @@ int psync_cloud_crypto_isstarted() { return ret; } -int psync_cloud_crypto_reset() { +int pcryptofolder_reset() { binparam params[] = {PAPI_STR("auth", psync_my_auth)}; psock_t *api; binresult *res; @@ -1246,7 +1254,7 @@ psync_crypto_get_temp_folder_decoder_locked(psync_fsfolderid_t folderid) { } psync_crypto_aes256_text_decoder_t -psync_cloud_crypto_get_folder_decoder(psync_fsfolderid_t folderid) { +pcryptofolder_flddecoder_get(psync_fsfolderid_t folderid) { char buff[16]; psync_crypto_aes256_text_decoder_t dec; if (!crypto_started_un) @@ -1279,7 +1287,7 @@ static void psync_crypto_free_folder_decoder(void *ptr) { (psync_crypto_aes256_text_decoder_t)ptr); } -void psync_cloud_crypto_release_folder_decoder( +void pcryptofolder_flddecoder_release( psync_fsfolderid_t folderid, psync_crypto_aes256_text_decoder_t decoder) { char buff[16]; if (crypto_started_un && folderid >= 0) { @@ -1291,7 +1299,7 @@ void psync_cloud_crypto_release_folder_decoder( } char * -psync_cloud_crypto_decode_filename(psync_crypto_aes256_text_decoder_t decoder, +pcryptofolder_flddecode_filename(psync_crypto_aes256_text_decoder_t decoder, const char *name) { unsigned char *filenameenc, *filenamedec; size_t filenameenclen; @@ -1319,7 +1327,7 @@ static void psync_crypto_release_folder_encoder_locked( } psync_crypto_aes256_text_encoder_t -psync_cloud_crypto_get_folder_encoder(psync_fsfolderid_t folderid) { +pcryptofolder_fldencoder_get(psync_fsfolderid_t folderid) { char buff[16]; psync_crypto_aes256_text_encoder_t enc; if (!crypto_started_un) { @@ -1351,7 +1359,7 @@ psync_cloud_crypto_get_folder_encoder(psync_fsfolderid_t folderid) { return enc; } -void psync_cloud_crypto_release_folder_encoder( +void pcryptofolder_fldencoder_release( psync_fsfolderid_t folderid, psync_crypto_aes256_text_encoder_t encoder) { char buff[16]; if (crypto_started_un && folderid >= 0) { @@ -1363,7 +1371,7 @@ void psync_cloud_crypto_release_folder_encoder( } char * -psync_cloud_crypto_encode_filename(psync_crypto_aes256_text_encoder_t encoder, +pcryptofolder_fldencode_filename(psync_crypto_aes256_text_encoder_t encoder, const char *name) { unsigned char *filenameenc, *filenameb32; size_t filenameenclen; @@ -1485,7 +1493,7 @@ psync_crypto_get_temp_file_encoder_locked(psync_fsfileid_t fileid, } psync_crypto_aes256_sector_encoder_decoder_t -psync_cloud_crypto_get_file_encoder(psync_fsfileid_t fileid, uint64_t hash, +pcryptofolder_filencoder_get(psync_fsfileid_t fileid, uint64_t hash, int nonetwork) { char buff[32]; psync_crypto_aes256_sector_encoder_decoder_t enc; @@ -1515,7 +1523,7 @@ psync_cloud_crypto_get_file_encoder(psync_fsfileid_t fileid, uint64_t hash, } psync_crypto_aes256_sector_encoder_decoder_t -psync_cloud_crypto_get_file_encoder_from_binresult(psync_fileid_t fileid, +pcryptofolder_filencoder_from_binresult(psync_fileid_t fileid, binresult *res) { const binresult *b64key; unsigned char *key; @@ -1558,7 +1566,7 @@ static void psync_crypto_free_file_encoder(void *ptr) { (psync_crypto_aes256_sector_encoder_decoder_t)ptr); } -void psync_cloud_crypto_release_file_encoder( +void pcryptofolder_filencoder_release( psync_fsfileid_t fileid, uint64_t hash, psync_crypto_aes256_sector_encoder_decoder_t encoder) { if (crypto_started_un && fileid >= 0) { @@ -1692,7 +1700,7 @@ int psync_cloud_crypto_send_mkdir(psync_folderid_t folderid, const char *name, return PSYNC_CRYPTO_SUCCESS; } -char *psync_cloud_crypto_get_file_encoded_key(psync_fsfileid_t fileid, +char *pcryptofolder_filencoder_key_get(psync_fsfileid_t fileid, uint64_t hash, size_t *keylen) { psync_encrypted_symmetric_key_t encsym; char *ret; @@ -1706,7 +1714,7 @@ char *psync_cloud_crypto_get_file_encoded_key(psync_fsfileid_t fileid, return ret; } -char *psync_cloud_crypto_get_new_encoded_key(uint32_t flags, size_t *keylen) { +char *pcryptofolder_filencoder_key_new(uint32_t flags, size_t *keylen) { psync_encrypted_symmetric_key_t encsym; sym_key_ver1 sym; char *ret; @@ -1734,7 +1742,7 @@ char *psync_cloud_crypto_get_new_encoded_key(uint32_t flags, size_t *keylen) { return ret; } -char *psync_cloud_crypto_get_new_encoded_and_plain_key( +char *pcryptofolder_filencoder_key_newplain( uint32_t flags, size_t *keylen, psync_symmetric_key_t *deckey) { psync_encrypted_symmetric_key_t encsym; sym_key_ver1 sym; @@ -1764,7 +1772,7 @@ char *psync_cloud_crypto_get_new_encoded_and_plain_key( return ret; } -int psync_cloud_crypto_mkdir(psync_folderid_t folderid, const char *name, +int pcryptofolder_mkdir(psync_folderid_t folderid, const char *name, const char **err, psync_folderid_t *newfolderid) { sym_key_ver1 sym; psync_encrypted_symmetric_key_t encsym; @@ -2003,7 +2011,7 @@ err_nm_0: return PERROR_NO_MEMORY; } -int psync_crypto_change_passphrase(const char *oldpassphrase, +int pcryptofolder_change_pass(const char *oldpassphrase, const char *newpassphrase, uint32_t flags, char **privenc, char **sign) { unsigned char *pubkey = NULL; @@ -2128,11 +2136,11 @@ ex: return cres; } -int psync_crypto_change_passphrase_unlocked(const char *newpassphrase, +int pcryptofolder_change_pass_unlocked(const char *newpassphrase, uint32_t flags, char **privenc, char **sign) { int cres; - if (unlikely(!psync_cloud_crypto_isstarted())) { + if (unlikely(!pcryptofolder_is_unlocked())) { return PSYNC_CRYPTO_NOT_STARTED; } if (!newpassphrase || !newpassphrase[0]) diff --git a/pclsync/pcryptofolder.h b/pclsync/pcryptofolder.h new file mode 100644 index 0000000..e3fd334 --- /dev/null +++ b/pclsync/pcryptofolder.h @@ -0,0 +1,77 @@ +/* + Copyright (c) 2014 Anton Titov. + + Copyright (c) 2014 pCloud Ltd. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: Redistributions of source code must retain the above + copyright notice, this list of conditions and the following + disclaimer. Redistributions in binary form must reproduce the + above copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. Neither the name of pCloud Ltd nor the + names of its contributors may be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL pCloud + Ltd BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + DAMAGE. +*/ + +#ifndef __PCRYPTOFOLDER_H +#define __PCRYPTOFOLDER_H + +#include "papi.h" +#include "pcrypto.h" +#include "pfsfolder.h" +#include "psynclib.h" + +#define PSYNC_CRYPTO_SYM_FLAG_ISDIR 1 + +#define PSYNC_CRYPTO_SECTOR_SIZE 4096 + +#define PSYNC_CRYPTO_MAX_ERROR 511 + +#define PSYNC_CRYPTO_UNLOADED_SECTOR_ENCODER \ + ((psync_crypto_aes256_sector_encoder_decoder_t)(PSYNC_CRYPTO_MAX_ERROR + 1)) +#define PSYNC_CRYPTO_LOADING_SECTOR_ENCODER \ + ((psync_crypto_aes256_sector_encoder_decoder_t)(PSYNC_CRYPTO_MAX_ERROR + 2)) +#define PSYNC_CRYPTO_FAILED_SECTOR_ENCODER \ + ((psync_crypto_aes256_sector_encoder_decoder_t)(PSYNC_CRYPTO_MAX_ERROR + 3)) + +void pcryptofolder_cache_cleanf(); +int pcryptofolder_change_pass(const char *oldpassphrase, const char *newpassphrase, uint32_t flags, char **privenc, char **sign); +int pcryptofolder_change_pass_unlocked(const char *newpassphrase, uint32_t flags, char **privenc, char **sign); +void pcryptofolder_clean_cache(); +psync_crypto_aes256_sector_encoder_decoder_t pcryptofolder_filencoder_from_binresult(psync_fileid_t fileid, binresult *res); +psync_crypto_aes256_sector_encoder_decoder_t pcryptofolder_filencoder_get(psync_fsfileid_t fileid, uint64_t hash, int nonetwork); +char *pcryptofolder_filencoder_key_get(psync_fsfileid_t fileid, uint64_t hash, size_t *keylen); +char *pcryptofolder_filencoder_key_new(uint32_t flags, size_t *keylen); +char * pcryptofolder_filencoder_key_newplain(uint32_t flags, size_t *keylen, psync_symmetric_key_t *deckey); +void pcryptofolder_filencoder_release(psync_fsfileid_t fileid, uint64_t hash, psync_crypto_aes256_sector_encoder_decoder_t encoder); +char * pcryptofolder_flddecode_filename(psync_crypto_aes256_text_decoder_t decoder, const char *name); +psync_crypto_aes256_text_decoder_t pcryptofolder_flddecoder_get(psync_fsfolderid_t folderid); +void pcryptofolder_flddecoder_release(psync_fsfolderid_t folderid, psync_crypto_aes256_text_decoder_t decoder); +char * pcryptofolder_fldencode_filename(psync_crypto_aes256_text_encoder_t encoder, const char *name); +psync_crypto_aes256_text_encoder_t pcryptofolder_fldencoder_get(psync_fsfolderid_t folderid); +void pcryptofolder_fldencoder_release(psync_fsfolderid_t folderid, psync_crypto_aes256_text_encoder_t encoder); +int pcryptofolder_get_hint(char **hint); +int pcryptofolder_is_unlocked(); +int pcryptofolder_lock(); +int pcryptofolder_mkdir(psync_folderid_t folderid, const char *name, const char **err, psync_folderid_t *newfolderid); +int pcryptofolder_reset(); +int pcryptofolder_setup(const char *password, const char *hint); +int pcryptofolder_unlock(const char *password); + +#endif diff --git a/pclsync/pfolder.c b/pclsync/pfolder.c index 73bb23b..127a939 100644 --- a/pclsync/pfolder.c +++ b/pclsync/pfolder.c @@ -38,7 +38,7 @@ #include #include "papi.h" -#include "pcloudcrypto.h" +#include "pcryptofolder.h" #include "pdiff.h" #include "pfolder.h" #include "pfsfolder.h" @@ -73,6 +73,14 @@ typedef struct _string_list { size_t len; } string_list; +static inline int psync_crypto_is_error(const void *ptr) { + return (uintptr_t)ptr <= PSYNC_CRYPTO_MAX_ERROR; +} + +static inline int psync_crypto_to_error(const void *ptr) { + return -((int)(uintptr_t)ptr); +} + psync_folderid_t psync_get_folderid_by_path(const char *path) { psync_folderid_t cfolderid; const char *sl; @@ -275,15 +283,15 @@ static int psync_add_path_to_list(psync_list *lst, psync_folderid_t folderid) { static string_list *str_list_decode(psync_folderid_t folderid, string_list *e) { psync_crypto_aes256_text_decoder_t dec; char *fn; - dec = psync_cloud_crypto_get_folder_decoder(folderid); + dec = pcryptofolder_flddecoder_get(folderid); if (psync_crypto_is_error(dec)) { psync_free(e); debug(D_WARNING, "got error %d getting decoder for folderid %lu", psync_crypto_to_error(dec), (unsigned long)folderid); return NULL; } - fn = psync_cloud_crypto_decode_filename(dec, e->str); - psync_cloud_crypto_release_folder_decoder(folderid, dec); + fn = pcryptofolder_flddecode_filename(dec, e->str); + pcryptofolder_flddecoder_release(folderid, dec); psync_free(e); if (unlikely_log(!fn)) return NULL; diff --git a/pclsync/pfs.c b/pclsync/pfs.c index 8feaa25..9dc947c 100644 --- a/pclsync/pfs.c +++ b/pclsync/pfs.c @@ -37,7 +37,7 @@ #include #include "pcompiler.h" -#include "pcloudcrypto.h" +#include "pcryptofolder.h" #include "pfile.h" #include "pfolder.h" @@ -125,6 +125,14 @@ static psync_tree *openfiles = PSYNC_TREE_EMPTY; static int psync_fs_ftruncate_of_locked(psync_openfile_t *of, fuse_off_t size); +static inline int psync_crypto_is_error(const void *ptr) { + return (uintptr_t)ptr <= PSYNC_CRYPTO_MAX_ERROR; +} + +static inline int psync_crypto_to_error(const void *ptr) { + return -((int)(uintptr_t)ptr); +} + static void delete_log_files(psync_openfile_t *of) { char fileidhex[sizeof(psync_fsfileid_t) * 2 + 2]; const char *cachepath; @@ -840,7 +848,7 @@ static int filler_decoded(psync_crypto_aes256_text_decoder_t dec, if (dec) { char *namedec; int ret; - namedec = psync_cloud_crypto_decode_filename(dec, name); + namedec = pcryptofolder_flddecode_filename(dec, name); if (!namedec) return 0; ret = filler(buf, namedec, st, off); @@ -876,7 +884,7 @@ static int psync_fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, return -PRINT_RETURN_CONST(ENOENT); } if (flags & PSYNC_FOLDER_FLAG_ENCRYPTED) { - dec = psync_cloud_crypto_get_folder_decoder(folderid); + dec = pcryptofolder_flddecoder_get(folderid); if (psync_crypto_is_error(dec)) { psync_sql_rdunlock(); return PRINT_RETURN( @@ -961,7 +969,7 @@ static int psync_fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, } psync_sql_rdunlock(); if (dec) - psync_cloud_crypto_release_folder_decoder(folderid, dec); + pcryptofolder_flddecoder_release(folderid, dec); return PRINT_RETURN(0); } @@ -1001,7 +1009,7 @@ psync_fs_create_file(psync_fsfileid_t fileid, psync_fsfileid_t remotefileid, psync_sql_unlock(); if (encoder != PSYNC_CRYPTO_INVALID_ENCODER && encoder != PSYNC_CRYPTO_UNLOADED_SECTOR_ENCODER) - psync_cloud_crypto_release_file_encoder(fileid, hash, encoder); + pcryptofolder_filencoder_release(fileid, hash, encoder); debug(D_NOTICE, "found open file %ld, refcnt %u, currentsize=%lu", (long int)fileid, (unsigned)fl->refcnt, (unsigned long)fl->currentsize); @@ -1317,7 +1325,7 @@ static int psync_fs_open(const char *path, struct fuse_file_info *fi) { if (type == PSYNC_FS_TASK_CREAT) { fileid = cr->fileid; if (fpath->flags & PSYNC_FOLDER_FLAG_ENCRYPTED) { - encoder = psync_cloud_crypto_get_file_encoder(fileid, hash, 1); + encoder = pcryptofolder_filencoder_get(fileid, hash, 1); if (unlikely_log(psync_crypto_is_error(encoder))) { ret = -psync_fs_crypto_err_to_errno(psync_crypto_to_error(encoder)); goto ex0; @@ -1371,7 +1379,7 @@ static int psync_fs_open(const char *path, struct fuse_file_info *fi) { goto ex0; } if (fpath->flags & PSYNC_FOLDER_FLAG_ENCRYPTED) { - encoder = psync_cloud_crypto_get_file_encoder(fileid, hash, 1); + encoder = pcryptofolder_filencoder_get(fileid, hash, 1); if (unlikely_log(psync_crypto_is_error(encoder))) { ret = -psync_fs_crypto_err_to_errno(psync_crypto_to_error(encoder)); goto ex0; @@ -1439,21 +1447,21 @@ static int psync_fs_open(const char *path, struct fuse_file_info *fi) { debug(D_NOTICE, "creating file %s", path); if (fpath->flags & PSYNC_FOLDER_FLAG_ENCRYPTED) { if (row) { - encoder = psync_cloud_crypto_get_file_encoder(fileid, hash, 0); + encoder = pcryptofolder_filencoder_get(fileid, hash, 0); if (unlikely_log(psync_crypto_is_error(encoder))) { ret = -psync_fs_crypto_err_to_errno(psync_crypto_to_error(encoder)); goto ex0; } - encsymkey = psync_cloud_crypto_get_file_encoded_key(fileid, hash, + encsymkey = pcryptofolder_filencoder_key_get(fileid, hash, &encsymkeylen); if (unlikely_log(psync_crypto_is_error(encsymkey))) { - psync_cloud_crypto_release_file_encoder(fileid, hash, encoder); + pcryptofolder_filencoder_release(fileid, hash, encoder); ret = -psync_fs_crypto_err_to_errno(psync_crypto_to_error(encsymkey)); goto ex0; } } else { psync_symmetric_key_t symkey; - encsymkey = psync_cloud_crypto_get_new_encoded_and_plain_key( + encsymkey = pcryptofolder_filencoder_key_newplain( 0, &encsymkeylen, &symkey); if (unlikely_log(psync_crypto_is_error(encsymkey))) { ret = -psync_fs_crypto_err_to_errno(psync_crypto_to_error(encsymkey)); @@ -1497,7 +1505,7 @@ static int psync_fs_open(const char *path, struct fuse_file_info *fi) { return 0; } else if (row) { if (fpath->flags & PSYNC_FOLDER_FLAG_ENCRYPTED) { - encoder = psync_cloud_crypto_get_file_encoder(fileid, hash, 1); + encoder = pcryptofolder_filencoder_get(fileid, hash, 1); if (unlikely_log(psync_crypto_is_error(encoder))) { ret = -psync_fs_crypto_err_to_errno(psync_crypto_to_error(encoder)); goto ex0; @@ -1622,7 +1630,7 @@ static int psync_fs_creat(const char *path, mode_t mode, psync_free(fpath); return -PRINT_RETURN_CONST(PSYNC_FS_ERR_CRYPTO_EXPIRED); } - encsymkey = psync_cloud_crypto_get_new_encoded_and_plain_key( + encsymkey = pcryptofolder_filencoder_key_newplain( 0, &encsymkeylen, &symkey); if (unlikely_log(psync_crypto_is_error(encsymkey))) { psync_fstask_release_folder_tasks_locked(folder); @@ -2102,14 +2110,14 @@ psync_fs_reopen_file_for_writing(psync_openfile_t *of) { // we should unlock of->mutex as it can deadlock with sqllock and taking // sqllock before network operation is not a good idea pthread_mutex_unlock(&of->mutex); - enc = psync_cloud_crypto_get_file_encoder(of->remotefileid, of->hash, 0); + enc = pcryptofolder_filencoder_get(of->remotefileid, of->hash, 0); if (unlikely(psync_crypto_is_error(enc))) return -psync_fs_crypto_err_to_errno(psync_crypto_to_error(enc)); psync_fs_lock_file(of); if (of->encoder == PSYNC_CRYPTO_UNLOADED_SECTOR_ENCODER) of->encoder = enc; else - psync_cloud_crypto_release_file_encoder(of->remotefileid, of->hash, enc); + pcryptofolder_filencoder_release(of->remotefileid, of->hash, enc); if (of->newfile || of->modified) return 1; } @@ -2130,7 +2138,7 @@ psync_fs_reopen_file_for_writing(psync_openfile_t *of) { return -PRINT_RETURN_CONST(PSYNC_FS_ERR_CRYPTO_EXPIRED); } size = psync_fs_crypto_crypto_size(of->initialsize); - encsymkey = psync_cloud_crypto_get_file_encoded_key(of->fileid, of->hash, + encsymkey = pcryptofolder_filencoder_key_get(of->fileid, of->hash, &encsymkeylen); if (unlikely_log(psync_crypto_is_error(encsymkey))) { psync_sql_unlock(); diff --git a/pclsync/pfscrypto.c b/pclsync/pfscrypto.c index f5a65c3..bf7fca4 100644 --- a/pclsync/pfscrypto.c +++ b/pclsync/pfscrypto.c @@ -39,7 +39,7 @@ #include #include -#include "pcloudcrypto.h" +#include "pcryptofolder.h" #include "pfile.h" #include "pfscrypto.h" #include "plibs.h" diff --git a/pclsync/pfscrypto.h b/pclsync/pfscrypto.h index 6d364e1..4fa3cb7 100644 --- a/pclsync/pfscrypto.h +++ b/pclsync/pfscrypto.h @@ -32,7 +32,7 @@ #ifndef _PSYNC_FSCRYPTO_H #define _PSYNC_FSCRYPTO_H -#include "pcloudcrypto.h" +#include "pcryptofolder.h" #include "pfs.h" #define PSYNC_CRYPTO_MAX_SECTORID (UINT32_MAX - 1) diff --git a/pclsync/pfsfolder.c b/pclsync/pfsfolder.c index 732c9ca..958e827 100644 --- a/pclsync/pfsfolder.c +++ b/pclsync/pfsfolder.c @@ -36,7 +36,7 @@ #include #include -#include "pcloudcrypto.h" +#include "pcryptofolder.h" #include "pfolder.h" #include "pfs.h" #include "pfsfolder.h" @@ -47,18 +47,26 @@ static PSYNC_THREAD int cryptoerr = 0; +static inline int psync_crypto_is_error(const void *ptr) { + return (uintptr_t)ptr <= PSYNC_CRYPTO_MAX_ERROR; +} + +static inline int psync_crypto_to_error(const void *ptr) { + return -((int)(uintptr_t)ptr); +} + static char *get_encname_for_folder(psync_fsfolderid_t folderid, const char *path, size_t len) { char *name, *encname; psync_crypto_aes256_text_encoder_t enc; - enc = psync_cloud_crypto_get_folder_encoder(folderid); + enc = pcryptofolder_fldencoder_get(folderid); if (psync_crypto_is_error(enc)) { cryptoerr = psync_crypto_to_error(enc); return NULL; } name = psync_strndup(path, len); - encname = psync_cloud_crypto_encode_filename(enc, name); - psync_cloud_crypto_release_folder_encoder(folderid, enc); + encname = pcryptofolder_fldencode_filename(enc, name); + pcryptofolder_fldencoder_release(folderid, enc); psync_free(name); return encname; } @@ -72,13 +80,13 @@ static psync_fspath_t *ret_folder_data(psync_fsfolderid_t folderid, psync_crypto_aes256_text_encoder_t enc; char *encname; size_t len; - enc = psync_cloud_crypto_get_folder_encoder(folderid); + enc = pcryptofolder_fldencoder_get(folderid); if (psync_crypto_is_error(enc)) { cryptoerr = psync_crypto_to_error(enc); return NULL; } - encname = psync_cloud_crypto_encode_filename(enc, name); - psync_cloud_crypto_release_folder_encoder(folderid, enc); + encname = pcryptofolder_fldencode_filename(enc, name); + pcryptofolder_fldencoder_release(folderid, enc); len = strlen(encname); ret = (psync_fspath_t *)psync_malloc(sizeof(psync_fspath_t) + len + 1); memcpy(ret + 1, encname, len + 1); @@ -103,14 +111,14 @@ char *get_decname_for_folder(psync_fsfolderid_t folderid, const char *path, size_t len) { char *name, *decname; psync_crypto_aes256_text_decoder_t dec; - dec = psync_cloud_crypto_get_folder_decoder(folderid); + dec = pcryptofolder_flddecoder_get(folderid); if (psync_crypto_is_error(dec)) { cryptoerr = psync_crypto_to_error(dec); return NULL; } name = psync_strndup(path, len); - decname = psync_cloud_crypto_decode_filename(dec, name); - psync_cloud_crypto_release_folder_decoder(folderid, dec); + decname = pcryptofolder_flddecode_filename(dec, name); + pcryptofolder_flddecoder_release(folderid, dec); psync_free(name); return decname; } diff --git a/pclsync/pfstasks.c b/pclsync/pfstasks.c index 900d0b8..160b64f 100644 --- a/pclsync/pfstasks.c +++ b/pclsync/pfstasks.c @@ -42,7 +42,7 @@ #include #include "pcache.h" -#include "pcloudcrypto.h" +#include "pcryptofolder.h" #include "putil.h" #include "pfolder.h" #include "pfs.h" @@ -62,6 +62,14 @@ typedef struct { static psync_tree *folders = PSYNC_TREE_EMPTY; static uint64_t psync_local_taskid = UINT64_MAX; +static inline int psync_crypto_is_error(const void *ptr) { + return (uintptr_t)ptr <= PSYNC_CRYPTO_MAX_ERROR; +} + +static inline int psync_crypto_to_error(const void *ptr) { + return -((int)(uintptr_t)ptr); +} + psync_fstask_folder_t * psync_fstask_get_or_create_folder_tasks(psync_fsfolderid_t folderid) { psync_fstask_folder_t *folder; @@ -410,7 +418,7 @@ int psync_fstask_mkdir(psync_fsfolderid_t folderid, const char *name, } ctime = psync_timer_time(); if (folderflags & PSYNC_FOLDER_FLAG_ENCRYPTED) { - key = psync_cloud_crypto_get_new_encoded_key(PSYNC_CRYPTO_SYM_FLAG_ISDIR, + key = pcryptofolder_filencoder_key_new(PSYNC_CRYPTO_SYM_FLAG_ISDIR, &klen); if (psync_crypto_is_error(key)) { psync_fstask_release_folder_tasks_locked(folder); diff --git a/pclsync/pmemlock.c b/pclsync/pmemlock.c index 0379380..644ee64 100644 --- a/pclsync/pmemlock.c +++ b/pclsync/pmemlock.c @@ -36,7 +36,7 @@ #include #include -#include "pcloudcrypto.h" +#include "pcryptofolder.h" #include "pintervaltree.h" #include "plibs.h" #include "pmem.h" @@ -123,7 +123,7 @@ retry: tryn++; pthread_mutex_unlock(&page_mutex); debug(D_NOTICE, "mlock failed, trying to clean cache"); - psync_cloud_crypto_clean_cache(); + pcryptofolder_clean_cache(); goto retry; } else { debug(D_WARNING, "mlock for page %lx failed even after cache clean", @@ -248,7 +248,7 @@ void *pmemlock_malloc(size_t size) { bestsize = ~((size_t)0); brange = NULL; boffset = 0; // just to make compilers happy - // psync_mem_lock may call psync_cloud_crypto_clean_cache(), which in turn can + // psync_mem_lock may call pcryptofolder_clean_cache(), which in turn can // call pmemlock_free() on few pointers, therefore allocator_mutex is // recursive pthread_mutex_lock(&allocator_mutex); diff --git a/pclsync/ppagecache.c b/pclsync/ppagecache.c index bbcf8aa..db27c36 100644 --- a/pclsync/ppagecache.c +++ b/pclsync/ppagecache.c @@ -208,6 +208,14 @@ static int flush_pages(int nosleep); static void flush_pages_noret() { flush_pages(0); } +static inline int psync_crypto_is_error(const void *ptr) { + return (uintptr_t)ptr <= PSYNC_CRYPTO_MAX_ERROR; +} + +static inline int psync_crypto_to_error(const void *ptr) { + return -((int)(uintptr_t)ptr); +} + static psync_cache_page_t *psync_pagecache_get_free_page_if_available() { psync_cache_page_t *page; int runthread; @@ -599,7 +607,7 @@ static int get_urls(psync_request_t *request, psync_urls_t *urls) { psync_process_api_error(result); goto err4; } - enc = psync_cloud_crypto_get_file_encoder_from_binresult(request->fileid, + enc = pcryptofolder_filencoder_from_binresult(request->fileid, ret); if (unlikely_log(psync_crypto_is_error(enc))) goto err4; @@ -611,7 +619,7 @@ static int get_urls(psync_request_t *request, psync_urls_t *urls) { request->of->encoder = enc; pthread_cond_broadcast(&enc_key_cond); } else - psync_cloud_crypto_release_file_encoder(request->fileid, request->hash, + pcryptofolder_filencoder_release(request->fileid, request->hash, enc); pthread_mutex_unlock(&request->of->mutex); request->needkey = 0; @@ -2090,7 +2098,7 @@ retry: } if (unlikely(request->needkey)) { enc = - psync_cloud_crypto_get_file_encoder(request->fileid, request->hash, 0); + pcryptofolder_filencoder_get(request->fileid, request->hash, 0); if (psync_crypto_to_error(enc)) { psync_pagecache_send_error(request, -EIO); return; @@ -2101,7 +2109,7 @@ retry: request->of->encoder = enc; pthread_cond_broadcast(&enc_key_cond); } else - psync_cloud_crypto_release_file_encoder(request->fileid, request->hash, + pcryptofolder_filencoder_release(request->fileid, request->hash, enc); pthread_mutex_unlock(&request->of->mutex); request->needkey = 0; diff --git a/pclsync/ppathstatus.c b/pclsync/ppathstatus.c index aea29b3..73e59b4 100644 --- a/pclsync/ppathstatus.c +++ b/pclsync/ppathstatus.c @@ -36,7 +36,7 @@ #include #include -#include "pcloudcrypto.h" +#include "pcryptofolder.h" #include "pfolder.h" #include "pfs.h" #include "plibs.h" @@ -45,7 +45,6 @@ #include "pstatus.h" #include "ptask.h" #include "ptree.h" -#include #include #define PATH_CACHE_SIZE 512 @@ -140,6 +139,14 @@ static psync_tree *folder_tasks = PSYNC_TREE_EMPTY; static void sync_data_free(sync_data_t *sd); static void load_sync_tasks(); +static inline int psync_crypto_is_error(const void *ptr) { + return (uintptr_t)ptr <= PSYNC_CRYPTO_MAX_ERROR; +} + +static inline int psync_crypto_to_error(const void *ptr) { + return -((int)(uintptr_t)ptr); +} + void psync_path_status_init() { size_t i; psync_sql_lock(); @@ -884,11 +891,11 @@ static int move_encname_to_buff(psync_folderid_t folderid, char *buff, return -1; memcpy(buff, name, namelen); buff[namelen] = 0; - enc = psync_cloud_crypto_get_folder_encoder(folderid); + enc = pcryptofolder_fldencoder_get(folderid); if (unlikely(psync_crypto_is_error(enc))) return -1; - encname = psync_cloud_crypto_encode_filename(enc, buff); - psync_cloud_crypto_release_folder_encoder(folderid, enc); + encname = pcryptofolder_fldencode_filename(enc, buff); + pcryptofolder_fldencoder_release(folderid, enc); len = strlen(encname); if (unlikely(len >= sizeof(buff))) return -1; diff --git a/pclsync/psynclib.c b/pclsync/psynclib.c index d2d2e4e..4cce660 100644 --- a/pclsync/psynclib.c +++ b/pclsync/psynclib.c @@ -46,7 +46,7 @@ #include "pcache.h" #include "ptevent.h" #include "pqevent.h" -#include "pcloudcrypto.h" +#include "pcryptofolder.h" #include "pcontacts.h" #include "pdevice.h" #include "pdevice_monitor.h" @@ -213,7 +213,7 @@ void psync_set_alloc(psync_malloc_t malloc_call, psync_realloc_t realloc_call, static void psync_stop_crypto_on_sleep() { if (psync_setting_get_bool(_PS(sleepstopcrypto)) && psync_crypto_isstarted()) { - psync_cloud_crypto_stop(); + pcryptofolder_lock(); debug(D_NOTICE, "stopped crypto due to sleep"); } } @@ -437,7 +437,7 @@ void psync_logout2(uint32_t auth_status, int doinvauth) { if (doinvauth) psync_invalidate_auth(psync_my_auth); memset(psync_my_auth, 0, sizeof(psync_my_auth)); - psync_cloud_crypto_stop(); + pcryptofolder_lock(); pthread_mutex_lock(&psync_my_auth_mutex); psync_free(psync_my_pass); psync_my_pass = NULL; @@ -552,7 +552,7 @@ void psync_unlink() { psync_status_recalc_to_download(); psync_status_recalc_to_upload(); psync_invalidate_auth(psync_my_auth); - psync_cloud_crypto_stop(); + pcryptofolder_lock(); psync_set_apiserver(PSYNC_API_HOST, PSYNC_LOCATIONID_DEFAULT); psys_sleep_milliseconds(20); psync_stop_localscan(); @@ -1583,7 +1583,7 @@ int psync_crypto_share_folder(psync_folderid_t folderid, const char *name, PAPI_NUM("strictmode", 1)}; return psync_run_command("sharefolder", params, err); } - if ((change_err = psync_crypto_change_passphrase_unlocked( + if ((change_err = pcryptofolder_change_pass_unlocked( temppass, PSYNC_CRYPTO_FLAG_TEMP_PASS, &priv_key, &signature))) { return change_err; } @@ -1633,7 +1633,7 @@ int psync_crypto_account_teamshare(psync_folderid_t folderid, const char *name, return psync_run_command("account_teamshare", params, err); } - if ((change_err = psync_crypto_change_passphrase_unlocked( + if ((change_err = pcryptofolder_change_pass_unlocked( temppass, PSYNC_CRYPTO_FLAG_TEMP_PASS, &priv_key, &signature))) { return change_err; } @@ -2185,30 +2185,30 @@ int psync_crypto_setup(const char *password, const char *hint) { if (psync_status_is_offline()) return PSYNC_CRYPTO_SETUP_CANT_CONNECT; else - return psync_cloud_crypto_setup(password, hint); + return pcryptofolder_setup(password, hint); } int psync_crypto_get_hint(char **hint) { if (psync_status_is_offline()) return PSYNC_CRYPTO_HINT_CANT_CONNECT; else - return psync_cloud_crypto_get_hint(hint); + return pcryptofolder_get_hint(hint); } int psync_crypto_start(const char *password) { - return psync_cloud_crypto_start(password); + return pcryptofolder_unlock(password); } -int psync_crypto_stop() { return psync_cloud_crypto_stop(); } +int psync_crypto_stop() { return pcryptofolder_lock(); } -int psync_crypto_isstarted() { return psync_cloud_crypto_isstarted(); } +int psync_crypto_isstarted() { return pcryptofolder_is_unlocked(); } int psync_crypto_mkdir(psync_folderid_t folderid, const char *name, const char **err, psync_folderid_t *newfolderid) { if (psync_status_is_offline()) return PSYNC_CRYPTO_CANT_CONNECT; else - return psync_cloud_crypto_mkdir(folderid, name, err, newfolderid); + return pcryptofolder_mkdir(folderid, name, err, newfolderid); } int psync_crypto_issetup() { @@ -2237,7 +2237,7 @@ int psync_crypto_reset() { if (psync_status_is_offline()) return PSYNC_CRYPTO_RESET_CANT_CONNECT; else - return psync_cloud_crypto_reset(); + return pcryptofolder_reset(); } psync_folderid_t psync_crypto_folderid() { @@ -2302,7 +2302,7 @@ int psync_crypto_change_crypto_pass(const char *oldpass, const char *newpass, char *priv_key = NULL; char *signature = NULL; - if ((err = psync_crypto_change_passphrase(oldpass, newpass, 0, &priv_key, + if ((err = pcryptofolder_change_pass(oldpass, newpass, 0, &priv_key, &signature))) { return err; } @@ -2349,7 +2349,7 @@ int psync_crypto_change_crypto_pass_unlocked(const char *newpass, char *priv_key = NULL; char *signature = NULL; - if ((err = psync_crypto_change_passphrase_unlocked(newpass, 0, &priv_key, + if ((err = pcryptofolder_change_pass_unlocked(newpass, 0, &priv_key, &signature))) { return err; } @@ -3238,7 +3238,7 @@ void psync_delete_cached_crypto_keys() { "'crypto_private_salt', 'crypto_private_sha1', 'crypto_public_sha1')"); if (psync_sql_affected_rows()) { debug(D_NOTICE, "deleted cached crypto keys"); - psync_cloud_crypto_clean_cache(); + pcryptofolder_clean_cache(); } psync_sql_statement("DELETE FROM cryptofolderkey"); psync_sql_statement("DELETE FROM cryptofilekey");