diff --git a/libretroshare/src/pgp/pgphandler.cc b/libretroshare/src/pgp/pgphandler.cc index b3865887f..437f70884 100644 --- a/libretroshare/src/pgp/pgphandler.cc +++ b/libretroshare/src/pgp/pgphandler.cc @@ -17,6 +17,8 @@ extern "C" { #include "pgphandler.h" #include "retroshare/rsiface.h" // For rsicontrol. +PassphraseCallback PGPHandler::_passphrase_callback = NULL ; + std::string PGPIdType::toStdString() const { static const char out[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' } ; @@ -162,9 +164,20 @@ ops_keyring_t *PGPHandler::allocateOPSKeyring() return kr ; } -PGPHandler::PGPHandler(const std::string& pubring, const std::string& secring,PassphraseCallback cb) - : pgphandlerMtx(std::string("PGPHandler")), _pubring_path(pubring),_secring_path(secring),_passphrase_callback(cb) +void PGPHandler::setPassphraseCallback(PassphraseCallback cb) { + _passphrase_callback = cb ; +} + +PGPHandler::PGPHandler(const std::string& pubring, const std::string& secring) + : pgphandlerMtx(std::string("PGPHandler")), _pubring_path(pubring),_secring_path(secring) +{ + if(_passphrase_callback == NULL) + { + std::cerr << "WARNING: before created a PGPHandler, you need to init the passphrase callback using PGPHandler::setPassphraseCallback()" << std::endl; + exit(-1) ; + } + // Allocate public and secret keyrings. // _pubring = allocateOPSKeyring() ; @@ -228,6 +241,7 @@ void PGPHandler::initCertificateInfo(PGPCertificateInfo& cert,const ops_keydata_ } cert._trustLvl = 1 ; // to be setup accordingly + cert._validLvl = 1 ; // to be setup accordingly cert._key_index = index ; cert._flags = 0 ; @@ -310,7 +324,7 @@ bool PGPHandler::availableGPGCertificatesWithPrivateKeys(std::list& i return true ; } -static ops_parse_cb_return_t cb_get_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)// __attribute__((unused))) +ops_parse_cb_return_t cb_get_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)// __attribute__((unused))) { const ops_parser_content_union_t *content=&content_->content; // validate_key_cb_arg_t *arg=ops_parse_cb_get_arg(cbinfo); @@ -324,10 +338,12 @@ static ops_parse_cb_return_t cb_get_passphrase(const ops_parser_content_t *conte case OPS_PARSER_CMD_GET_SK_PASSPHRASE: { std::string passwd; - std::string uid_hint = std::string((const char *)cbinfo->cryptinfo.keydata->uids[0].user_id) + "(" + PGPIdType(cbinfo->cryptinfo.keydata->key_id).toStdString()+")" ; + std::string uid_hint = std::string((const char *)cbinfo->cryptinfo.keydata->uids[0].user_id) ; + uid_hint += "(" + PGPIdType(cbinfo->cryptinfo.keydata->key_id).toStdString()+")" ; - if (rsicontrol->getNotify().askForPassword(uid_hint, prev_was_bad, passwd) == false) - return OPS_RELEASE_MEMORY; + passwd = PGPHandler::passphraseCallback()(NULL,uid_hint.c_str(),NULL,prev_was_bad) ; +// if (rsicontrol->getNotify().askForPassword(uid_hint, prev_was_bad, passwd) == false) +// return OPS_RELEASE_MEMORY; *(content->secret_key_passphrase.passphrase)= (char *)ops_mallocz(passwd.length()+1) ; memcpy(*(content->secret_key_passphrase.passphrase),passwd.c_str(),passwd.length()) ; @@ -646,7 +662,7 @@ bool PGPHandler::SignDataBin(const PGPIdType& id,const void *data, const uint32_ // then do the signature. - ops_memory_t *memres = ops_sign_buf(data,len,(ops_sig_type_t)0x10,secret_key,ops_false) ; + ops_memory_t *memres = ops_sign_buf(data,len,(ops_sig_type_t)0x00,secret_key,ops_false,ops_false) ; if(!memres) return false ; diff --git a/libretroshare/src/pgp/pgphandler.h b/libretroshare/src/pgp/pgphandler.h index 897b83f94..a3c945f03 100644 --- a/libretroshare/src/pgp/pgphandler.h +++ b/libretroshare/src/pgp/pgphandler.h @@ -93,7 +93,7 @@ class PGPCertificateInfo class PGPHandler { public: - PGPHandler(const std::string& path_to_public_keyring, const std::string& path_to_secret_keyring,PassphraseCallback cb) ; + PGPHandler(const std::string& path_to_public_keyring, const std::string& path_to_secret_keyring) ; virtual ~PGPHandler() ; @@ -129,6 +129,9 @@ class PGPHandler bool isGPGSigned(const std::string &id); bool isGPGAccepted(const std::string &id); + static void setPassphraseCallback(PassphraseCallback cb) ; + + static PassphraseCallback passphraseCallback() { return _passphrase_callback ; } private: static std::string makeRadixEncodedPGPKey(const ops_keydata_t *key) ; static ops_keyring_t *allocateOPSKeyring() ; @@ -150,6 +153,6 @@ class PGPHandler const std::string _pubring_path ; const std::string _secring_path ; - PassphraseCallback _passphrase_callback ; + static PassphraseCallback _passphrase_callback ; }; diff --git a/libretroshare/src/pgp/test_pgp_handler.cc b/libretroshare/src/pgp/test_pgp_handler.cc index 96d6b6562..95e4d1044 100644 --- a/libretroshare/src/pgp/test_pgp_handler.cc +++ b/libretroshare/src/pgp/test_pgp_handler.cc @@ -1,5 +1,6 @@ -// COMPILE_LINE: g++ -o test_pgp_handler test_pgp_handler.cc -I../../../openpgpsdk/include -I../ -L../lib -lretroshare ../../../openpgpsdk/lib/libops.a -lssl -lcrypto -lbz2 +// COMPILE_LINE: g++ -o test_pgp_handler test_pgp_handler.cc -I../../../openpgpsdk/include -I../ -L../lib -lretroshare ../../../libbitdht/src/lib/libbitdht.a ../../../openpgpsdk/lib/libops.a -lgnome-keyring -lupnp -lssl -lcrypto -lbz2 // +#include #include #include "pgphandler.h" @@ -8,6 +9,21 @@ static std::string passphrase_callback(void *data,const char *uid_info,const cha return std::string(getpass(what)) ; } +static std::string stringFromBytes(unsigned char *bytes,size_t len) +{ + static const char out[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' } ; + + std::string res ; + + for(int j = 0; j < len; j++) + { + res += out[ (bytes[j]>>4) ] ; + res += out[ bytes[j] & 0xf ] ; + } + + return res ; +} + int main(int argc,char *argv[]) { // test pgp ids. @@ -24,7 +40,9 @@ int main(int argc,char *argv[]) static const std::string pubring = "pubring.gpg" ; static const std::string secring = "secring.gpg" ; - PGPHandler pgph(pubring,secring,&passphrase_callback) ; + PGPHandler::setPassphraseCallback(&passphrase_callback) ; + PGPHandler pgph(pubring,secring) ; + pgph.printKeys() ; std::cerr << std::endl ; @@ -76,27 +94,37 @@ int main(int argc,char *argv[]) std::cerr << "Password = \"" << pass << "\"" << std::endl; std::cerr << "Testing signature with keypair " << newid.toStdString() << std::endl; - char test_bin[14] = "34f4fhuif3489" ; - unsigned char sign[100] ; - uint32_t signlen = 100 ; + static const size_t BUFF_LEN = 25 ; + unsigned char *test_bin = new unsigned char[BUFF_LEN] ; + for(size_t i=0;i& list,bool (*filter)(con for(std::list::const_iterator it(ids.begin());it!=ids.end();++it) list.push_back( (*it).toStdString() ) ; + + return true ; } static bool filter_Validity(const PGPCertificateInfo& info) { return true ; } //{ return info._validLvl >= PGPCertificateInfo::GPGME_VALIDITY_MARGINAL ; } diff --git a/openpgpsdk/include/openpgpsdk/signature.h b/openpgpsdk/include/openpgpsdk/signature.h index 66294451c..0a8db71ea 100644 --- a/openpgpsdk/include/openpgpsdk/signature.h +++ b/openpgpsdk/include/openpgpsdk/signature.h @@ -90,7 +90,7 @@ void ops_signature_add_primary_user_id(ops_create_signature_t *sig, ops_boolean_t ops_sign_file_as_cleartext(const char* input_filename, const char* output_filename, const ops_secret_key_t *skey, const ops_boolean_t overwrite); ops_boolean_t ops_sign_buf_as_cleartext(const char* input, const size_t len, ops_memory_t** output, const ops_secret_key_t *skey); ops_boolean_t ops_sign_file(const char* input_filename, const char* output_filename, const ops_secret_key_t *skey, const ops_boolean_t use_armour, const ops_boolean_t overwrite); -ops_memory_t * ops_sign_buf(const void* input, const size_t input_len, const ops_sig_type_t sig_type, const ops_secret_key_t *skey, const ops_boolean_t use_armour); +ops_memory_t * ops_sign_buf(const void* input, const size_t input_len, const ops_sig_type_t sig_type, const ops_secret_key_t *skey, const ops_boolean_t use_armour,ops_boolean_t include_data); ops_boolean_t ops_writer_push_signed(ops_create_info_t *cinfo, const ops_sig_type_t sig_type, const ops_secret_key_t *skey); #endif diff --git a/openpgpsdk/src/signature.c b/openpgpsdk/src/signature.c index 7c7895176..e24460484 100644 --- a/openpgpsdk/src/signature.c +++ b/openpgpsdk/src/signature.c @@ -1266,7 +1266,8 @@ void example(const ops_secret_key_t *skey) ops_memory_t* ops_sign_buf(const void* input, const size_t input_len, const ops_sig_type_t sig_type, const ops_secret_key_t *skey, - const ops_boolean_t use_armour) + const ops_boolean_t use_armour, + ops_boolean_t include_data) { // \todo allow choice of hash algorithams // enforce use of SHA1 for now @@ -1313,7 +1314,8 @@ ops_memory_t* ops_sign_buf(const void* input, const size_t input_len, if (debug) fprintf(stderr,"** Writing out data now\n"); - ops_write_literal_data_from_buf(input, input_len, ld_type, cinfo); + if(include_data) + ops_write_literal_data_from_buf(input, input_len, ld_type, cinfo); if (debug) fprintf(stderr,"** After Writing out data now\n");