From 33a37054e8dff1720354305b5cd117181d939007 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 1 Apr 2012 16:43:23 +0000 Subject: [PATCH] fixed several memory leaks git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5071 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/pgp/pgphandler.cc | 17 ++++++++++++++++- openpgpsdk/src/create.c | 3 +++ openpgpsdk/src/keyring.c | 18 +++++++++++++++++- openpgpsdk/src/openssl_crypto.c | 1 + openpgpsdk/src/packet-parse.c | 6 ++++++ openpgpsdk/src/signature.c | 1 + 6 files changed, 44 insertions(+), 2 deletions(-) diff --git a/libretroshare/src/pgp/pgphandler.cc b/libretroshare/src/pgp/pgphandler.cc index 551c7c365..e8900a29b 100644 --- a/libretroshare/src/pgp/pgphandler.cc +++ b/libretroshare/src/pgp/pgphandler.cc @@ -185,12 +185,14 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri static const int KEY_NUMBITS = 2048 ; ops_user_id_t uid ; - const char *s = strdup((name + " " + email + " (Generated by RetroShare)").c_str()) ; + char *s = strdup((name + " " + email + " (Generated by RetroShare)").c_str()) ; uid.user_id = (unsigned char *)s ; unsigned long int e = 17 ; // some prime number ops_keydata_t *key = ops_rsa_create_selfsigned_keypair(KEY_NUMBITS,e,&uid) ; + free(s) ; + if(!key) return false ; @@ -206,6 +208,9 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri ops_write_transferable_secret_key(key,(unsigned char *)passphrase.c_str(),passphrase.length(),ops_false,cinfo); + ops_keydata_free(key) ; + free(key) ; + // 3 - read the file into a keyring ops_keyring_t *tmp_keyring = allocateOPSKeyring() ; @@ -299,13 +304,16 @@ bool PGPHandler::LoadCertificateFromString(const std::string& pgp_cert,PGPIdType if(!ops_keyring_read_from_mem(tmp_keyring,ops_true,mem)) { ops_keyring_free(tmp_keyring) ; + free(tmp_keyring) ; ops_memory_release(mem) ; + free(mem) ; std::cerr << "Could not read key. Format error?" << std::endl; error_string = std::string("Could not read key. Format error?") ; return false ; } ops_memory_release(mem) ; + free(mem) ; error_string.clear() ; std::cerr << "Key read correctly: " << std::endl; @@ -325,6 +333,8 @@ bool PGPHandler::LoadCertificateFromString(const std::string& pgp_cert,PGPIdType std::cerr << "Added the key in the main public keyring." << std::endl; ops_keyring_free(tmp_keyring) ; + free(tmp_keyring) ; + return true ; } @@ -362,6 +372,11 @@ bool PGPHandler::SignDataBin(const PGPIdType& id,const void *data, const uint32_ memcpy(sign,ops_memory_get_data(memres),tlen) ; *signlen = tlen ; + ops_memory_release(memres) ; + free(memres) ; + ops_secret_key_free(secret_key) ; + free(secret_key) ; + return true ; } diff --git a/openpgpsdk/src/create.c b/openpgpsdk/src/create.c index bae9d6ca0..83861eeee 100644 --- a/openpgpsdk/src/create.c +++ b/openpgpsdk/src/create.c @@ -397,6 +397,9 @@ static ops_boolean_t write_secret_key_body(const ops_secret_key_t *key, return ops_false; ops_writer_pop(info); + + free(crypt.encrypt_key) ; + free(crypt.decrypt_key) ; return ops_true; } diff --git a/openpgpsdk/src/keyring.c b/openpgpsdk/src/keyring.c index aee005bd3..1e1243b3f 100644 --- a/openpgpsdk/src/keyring.c +++ b/openpgpsdk/src/keyring.c @@ -86,12 +86,19 @@ void ops_keydata_free(ops_keydata_t *keydata) keydata->packets=NULL; keydata->npackets=0; +/* for(n=0;nnsigs;++n) + { + ops_user_id_free(keydata->sigs[n].userid) ; + ops_packet_free(keydata->sigs[n].packet) ; + }*/ + free(keydata->sigs) ; + if(keydata->type == OPS_PTAG_CT_PUBLIC_KEY) ops_public_key_free(&keydata->key.pkey); else ops_secret_key_free(&keydata->key.skey); - free(keydata); +/* free(keydata); */ } // \todo check where userid pointers are copied @@ -145,18 +152,21 @@ void ops_keydata_copy(ops_keydata_t *dst,const ops_keydata_t *src) dst->uids = (ops_user_id_t*)ops_mallocz(src->nuids * sizeof(ops_user_id_t)) ; dst->nuids = src->nuids ; + dst->nuids_allocated = src->nuids ; for(n=0 ; n < src->nuids ; ++n) ops_copy_userid(&dst->uids[n],&src->uids[n]) ; dst->packets = (ops_packet_t*)ops_mallocz(src->npackets * sizeof(ops_packet_t)) ; dst->npackets = src->npackets ; + dst->npackets_allocated = src->npackets ; for(n=0 ; n < src->npackets ; ++n) ops_copy_packet(&(dst->packets[n]),&(src->packets[n])); dst->nsigs = src->nsigs ; dst->sigs = (sigpacket_t*)ops_mallocz(src->nsigs * sizeof(sigpacket_t)) ; + dst->nsigs_allocated = src->nsigs ; for(n=0 ; n < src->nsigs ; ++n) { @@ -351,6 +361,8 @@ ops_secret_key_t *ops_decrypt_secret_key_from_data(const ops_keydata_t *key, ops_parse(pinfo); + ops_parse_info_delete(pinfo); + return arg.skey; } @@ -792,6 +804,10 @@ ops_boolean_t ops_keyring_read_from_mem(ops_keyring_t *keyring, const ops_boolea */ void ops_keyring_free(ops_keyring_t *keyring) { + int n; + for(n=0;nnkeys;++n) + ops_keydata_free(&keyring->keys[n]) ; + free(keyring->keys); keyring->keys=NULL; keyring->nkeys=0; diff --git a/openpgpsdk/src/openssl_crypto.c b/openpgpsdk/src/openssl_crypto.c index c45c68491..b8a5cc28c 100644 --- a/openpgpsdk/src/openssl_crypto.c +++ b/openpgpsdk/src/openssl_crypto.c @@ -742,6 +742,7 @@ ops_keydata_t* ops_rsa_create_selfsigned_keypair(const int numbits, const unsign || ops_add_selfsigned_userid_to_keydata(keydata, userid) != ops_true) { ops_keydata_free(keydata); + free(keydata) ; return NULL; } diff --git a/openpgpsdk/src/packet-parse.c b/openpgpsdk/src/packet-parse.c index c328e263e..a32148403 100644 --- a/openpgpsdk/src/packet-parse.c +++ b/openpgpsdk/src/packet-parse.c @@ -1251,6 +1251,8 @@ static int parse_user_id(ops_region_t *region,ops_parse_info_t *pinfo) CBP(pinfo,OPS_PTAG_CT_USER_ID,&content); + free(C.user_id.user_id) ; + return 1; } @@ -1904,6 +1906,8 @@ static int parse_v4_signature(ops_region_t *region,ops_parse_info_t *pinfo) CBP(pinfo,OPS_PTAG_CT_SIGNATURE_FOOTER,&content); + free(C.signature.info.v4_hashed_data) ; + return 1; } @@ -3030,6 +3034,8 @@ static int ops_parse_one_packet(ops_parse_info_t *pinfo, } pinfo->rinfo.alength=0; + free(C.packet.raw) ; + if(r < 0) return -1; diff --git a/openpgpsdk/src/signature.c b/openpgpsdk/src/signature.c index 2430dc80f..f510ea7dd 100644 --- a/openpgpsdk/src/signature.c +++ b/openpgpsdk/src/signature.c @@ -1314,6 +1314,7 @@ ops_memory_t* ops_sign_buf(const void* input, const size_t input_len, const ops_ // tidy up ops_writer_close(cinfo); + free(cinfo) ; ops_create_signature_delete(sig); return mem;