fixed binary signature

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5128 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2012-05-01 08:53:32 +00:00
parent ce5e6d3949
commit 60fcd981c1
6 changed files with 72 additions and 20 deletions

View File

@ -17,6 +17,8 @@ extern "C" {
#include "pgphandler.h" #include "pgphandler.h"
#include "retroshare/rsiface.h" // For rsicontrol. #include "retroshare/rsiface.h" // For rsicontrol.
PassphraseCallback PGPHandler::_passphrase_callback = NULL ;
std::string PGPIdType::toStdString() const 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' } ; 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 ; return kr ;
} }
PGPHandler::PGPHandler(const std::string& pubring, const std::string& secring,PassphraseCallback cb) void PGPHandler::setPassphraseCallback(PassphraseCallback cb)
: pgphandlerMtx(std::string("PGPHandler")), _pubring_path(pubring),_secring_path(secring),_passphrase_callback(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. // Allocate public and secret keyrings.
// //
_pubring = allocateOPSKeyring() ; _pubring = allocateOPSKeyring() ;
@ -228,6 +241,7 @@ void PGPHandler::initCertificateInfo(PGPCertificateInfo& cert,const ops_keydata_
} }
cert._trustLvl = 1 ; // to be setup accordingly cert._trustLvl = 1 ; // to be setup accordingly
cert._validLvl = 1 ; // to be setup accordingly
cert._key_index = index ; cert._key_index = index ;
cert._flags = 0 ; cert._flags = 0 ;
@ -310,7 +324,7 @@ bool PGPHandler::availableGPGCertificatesWithPrivateKeys(std::list<PGPIdType>& i
return true ; 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; const ops_parser_content_union_t *content=&content_->content;
// validate_key_cb_arg_t *arg=ops_parse_cb_get_arg(cbinfo); // 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: case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
{ {
std::string passwd; 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) passwd = PGPHandler::passphraseCallback()(NULL,uid_hint.c_str(),NULL,prev_was_bad) ;
return OPS_RELEASE_MEMORY; // 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) ; *(content->secret_key_passphrase.passphrase)= (char *)ops_mallocz(passwd.length()+1) ;
memcpy(*(content->secret_key_passphrase.passphrase),passwd.c_str(),passwd.length()) ; 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. // 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) if(!memres)
return false ; return false ;

View File

@ -93,7 +93,7 @@ class PGPCertificateInfo
class PGPHandler class PGPHandler
{ {
public: 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() ; virtual ~PGPHandler() ;
@ -129,6 +129,9 @@ class PGPHandler
bool isGPGSigned(const std::string &id); bool isGPGSigned(const std::string &id);
bool isGPGAccepted(const std::string &id); bool isGPGAccepted(const std::string &id);
static void setPassphraseCallback(PassphraseCallback cb) ;
static PassphraseCallback passphraseCallback() { return _passphrase_callback ; }
private: private:
static std::string makeRadixEncodedPGPKey(const ops_keydata_t *key) ; static std::string makeRadixEncodedPGPKey(const ops_keydata_t *key) ;
static ops_keyring_t *allocateOPSKeyring() ; static ops_keyring_t *allocateOPSKeyring() ;
@ -150,6 +153,6 @@ class PGPHandler
const std::string _pubring_path ; const std::string _pubring_path ;
const std::string _secring_path ; const std::string _secring_path ;
PassphraseCallback _passphrase_callback ; static PassphraseCallback _passphrase_callback ;
}; };

View File

@ -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 <stdlib.h>
#include <iostream> #include <iostream>
#include "pgphandler.h" #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)) ; 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[]) int main(int argc,char *argv[])
{ {
// test pgp ids. // test pgp ids.
@ -24,7 +40,9 @@ int main(int argc,char *argv[])
static const std::string pubring = "pubring.gpg" ; static const std::string pubring = "pubring.gpg" ;
static const std::string secring = "secring.gpg" ; static const std::string secring = "secring.gpg" ;
PGPHandler pgph(pubring,secring,&passphrase_callback) ; PGPHandler::setPassphraseCallback(&passphrase_callback) ;
PGPHandler pgph(pubring,secring) ;
pgph.printKeys() ; pgph.printKeys() ;
std::cerr << std::endl ; std::cerr << std::endl ;
@ -76,27 +94,37 @@ int main(int argc,char *argv[])
std::cerr << "Password = \"" << pass << "\"" << std::endl; std::cerr << "Password = \"" << pass << "\"" << std::endl;
std::cerr << "Testing signature with keypair " << newid.toStdString() << std::endl; std::cerr << "Testing signature with keypair " << newid.toStdString() << std::endl;
char test_bin[14] = "34f4fhuif3489" ;
unsigned char sign[100] ; static const size_t BUFF_LEN = 25 ;
uint32_t signlen = 100 ; unsigned char *test_bin = new unsigned char[BUFF_LEN] ;
for(size_t i=0;i<BUFF_LEN;++i)
test_bin[i] = rand()%26 + 'a' ;
if(!pgph.SignDataBin(newid,test_bin,13,sign,&signlen)) std::cerr << "Text = \"" << std::string((char *)test_bin,BUFF_LEN) << "\"" << std::endl;
unsigned char sign[1000] ;
uint32_t signlen = 1000 ;
if(!pgph.SignDataBin(newid,test_bin,BUFF_LEN,sign,&signlen))
std::cerr << "Signature error." << std::endl; std::cerr << "Signature error." << std::endl;
else else
std::cerr << "Signature success." << std::endl; std::cerr << "Signature success." << std::endl;
std::cerr << "Signature length: " << signlen << std::endl;
std::cerr << "Signature: " << stringFromBytes(sign,signlen) << std::endl;
std::cerr << "Now verifying signature..." << std::endl; std::cerr << "Now verifying signature..." << std::endl;
PGPFingerprintType fingerprint ; PGPFingerprintType fingerprint ;
if(!pgph.getKeyFingerprint(newid,fingerprint) ) if(!pgph.getKeyFingerprint(newid,fingerprint) )
std::cerr << "Cannot find fingerprint of key id " << newid.toStdString() << std::endl; std::cerr << "Cannot find fingerprint of key id " << newid.toStdString() << std::endl;
if(!pgph.VerifySignBin(test_bin,13,sign,signlen,fingerprint)) if(!pgph.VerifySignBin(test_bin,BUFF_LEN,sign,signlen,fingerprint))
std::cerr << "Signature verification failed." << std::endl; std::cerr << "Signature verification failed." << std::endl;
else else
std::cerr << "Signature verification worked!" << std::endl; std::cerr << "Signature verification worked!" << std::endl;
delete[] test_bin ;
std::string outfile = "crypted_toto.pgp" ; std::string outfile = "crypted_toto.pgp" ;
std::string text_to_encrypt = "this is a secret message" ; std::string text_to_encrypt = "this is a secret message" ;

View File

@ -91,12 +91,13 @@ void AuthGPG::init(const std::string& path_to_public_keyring,const std::string&
if(_instance != NULL) if(_instance != NULL)
throw std::runtime_error("AuthGPG::init() called twice!") ; throw std::runtime_error("AuthGPG::init() called twice!") ;
PGPHandler::setPassphraseCallback(pgp_pwd_callback) ;
_instance = new AuthGPG(path_to_public_keyring,path_to_secret_keyring) ; _instance = new AuthGPG(path_to_public_keyring,path_to_secret_keyring) ;
} }
AuthGPG::AuthGPG(const std::string& path_to_public_keyring,const std::string& path_to_secret_keyring) AuthGPG::AuthGPG(const std::string& path_to_public_keyring,const std::string& path_to_secret_keyring)
:p3Config(CONFIG_TYPE_AUTHGPG), :p3Config(CONFIG_TYPE_AUTHGPG),
PGPHandler(path_to_public_keyring,path_to_secret_keyring,pgp_pwd_callback), PGPHandler(path_to_public_keyring,path_to_secret_keyring),
gpgMtxEngine("AuthGPG-engine"), gpgMtxEngine("AuthGPG-engine"),
gpgMtxData("AuthGPG-data"), gpgMtxData("AuthGPG-data"),
gpgKeySelected(false), gpgKeySelected(false),
@ -737,6 +738,8 @@ bool AuthGPG::getGPGFilteredList(std::list<std::string>& list,bool (*filter)(con
for(std::list<PGPIdType>::const_iterator it(ids.begin());it!=ids.end();++it) for(std::list<PGPIdType>::const_iterator it(ids.begin());it!=ids.end();++it)
list.push_back( (*it).toStdString() ) ; list.push_back( (*it).toStdString() ) ;
return true ;
} }
static bool filter_Validity(const PGPCertificateInfo& info) { return true ; } //{ return info._validLvl >= PGPCertificateInfo::GPGME_VALIDITY_MARGINAL ; } static bool filter_Validity(const PGPCertificateInfo& info) { return true ; } //{ return info._validLvl >= PGPCertificateInfo::GPGME_VALIDITY_MARGINAL ; }

View File

@ -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_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_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_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); 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 #endif

View File

@ -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, ops_memory_t* ops_sign_buf(const void* input, const size_t input_len,
const ops_sig_type_t sig_type, const ops_sig_type_t sig_type,
const ops_secret_key_t *skey, 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 // \todo allow choice of hash algorithams
// enforce use of SHA1 for now // 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) if (debug)
fprintf(stderr,"** Writing out data now\n"); 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) if (debug)
fprintf(stderr,"** After Writing out data now\n"); fprintf(stderr,"** After Writing out data now\n");