From eb448cbaaf6c9fef0ac346ae95765daa9e54eae2 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 1 Apr 2012 21:10:54 +0000 Subject: [PATCH] fixed memory issue, added signature verification (uncomplete) git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5072 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/pgp/pgphandler.cc | 25 ++++++++++++++++++++++- libretroshare/src/pgp/pgphandler.h | 2 +- libretroshare/src/pgp/test_pgp_handler.cc | 7 +++++++ openpgpsdk/src/packet-parse.c | 3 +++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/libretroshare/src/pgp/pgphandler.cc b/libretroshare/src/pgp/pgphandler.cc index e8900a29b..a3eb9ee47 100644 --- a/libretroshare/src/pgp/pgphandler.cc +++ b/libretroshare/src/pgp/pgphandler.cc @@ -11,6 +11,7 @@ extern "C" { #include #include #include +#include } #include "pgphandler.h" @@ -229,7 +230,14 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri std::cerr << "Added new secret key with id " << pgpId.toStdString() << " to secret keyring." << std::endl; - // We should store it in the keyring. + // 5 - copy the private key to the public keyring + + addNewKeyToOPSKeyring(_pubring,tmp_keyring->keys[0]) ; + _public_keyring_map[ pgpId.toUInt64() ] = _pubring->nkeys-1 ; + + std::cerr << "Added new public key with id " << pgpId.toStdString() << " to public keyring." << std::endl; + + // 6 - clean ops_keyring_free(tmp_keyring) ; free(tmp_keyring) ; @@ -380,3 +388,18 @@ bool PGPHandler::SignDataBin(const PGPIdType& id,const void *data, const uint32_ return true ; } +bool PGPHandler::VerifySignBin(const void *data, uint32_t data_len, unsigned char *sign, unsigned int sign_len, const std::string &withfingerprint) +{ + ops_memory_t *mem = ops_memory_new() ; + ops_memory_add(mem,(unsigned char *)sign,sign_len) ; + + ops_validate_result_t *result = (ops_validate_result_t*)ops_mallocz(sizeof(ops_validate_result_t)) ; + ops_boolean_t res = ops_validate_mem(result, mem, ops_false, _pubring); + + ops_validate_result_free(result) ; + + // no need to clear mem. It's already deleted by ops_validate_mem (weird but true). + + return res ; +} + diff --git a/libretroshare/src/pgp/pgphandler.h b/libretroshare/src/pgp/pgphandler.h index 86459c677..05a4131d1 100644 --- a/libretroshare/src/pgp/pgphandler.h +++ b/libretroshare/src/pgp/pgphandler.h @@ -51,7 +51,7 @@ class PGPHandler bool TrustCertificate(const PGPIdType& id, int trustlvl); bool SignDataBin(const PGPIdType& id,const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen) ; - bool VerifySignBin(const void*, uint32_t, unsigned char*, unsigned int, const std::string &withfingerprint) { return false ; } + bool VerifySignBin(const void *data, uint32_t data_len, unsigned char *sign, unsigned int sign_len, const std::string &withfingerprint) ; // Debug stuff. virtual void printKeys() const ; diff --git a/libretroshare/src/pgp/test_pgp_handler.cc b/libretroshare/src/pgp/test_pgp_handler.cc index 0e7e75983..5cb230bb0 100644 --- a/libretroshare/src/pgp/test_pgp_handler.cc +++ b/libretroshare/src/pgp/test_pgp_handler.cc @@ -84,6 +84,13 @@ int main(int argc,char *argv[]) std::cerr << "Signature error." << std::endl; else std::cerr << "Signature success." << std::endl; + + std::cerr << "Now verifying signature..." << std::endl; + + if(!pgph.VerifySignBin(test_bin,13,sign,signlen,"")) + std::cerr << "Signature verification failed." << std::endl; + else + std::cerr << "Signature verification worked!" << std::endl; return 0 ; } diff --git a/openpgpsdk/src/packet-parse.c b/openpgpsdk/src/packet-parse.c index a32148403..9ab743ab3 100644 --- a/openpgpsdk/src/packet-parse.c +++ b/openpgpsdk/src/packet-parse.c @@ -3032,6 +3032,9 @@ static int ops_parse_one_packet(ops_parse_info_t *pinfo, pinfo->rinfo.asize=0; CBP(pinfo,OPS_PARSER_PACKET_END,&content); } + else + C.packet.raw = NULL ; + pinfo->rinfo.alength=0; free(C.packet.raw) ;