mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 06:06:10 -04:00
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:
parent
ce5e6d3949
commit
60fcd981c1
6 changed files with 72 additions and 20 deletions
|
@ -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<PGPIdType>& 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 ;
|
||||
|
|
|
@ -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 ;
|
||||
};
|
||||
|
||||
|
|
|
@ -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 "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<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;
|
||||
else
|
||||
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;
|
||||
|
||||
PGPFingerprintType fingerprint ;
|
||||
if(!pgph.getKeyFingerprint(newid,fingerprint) )
|
||||
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;
|
||||
else
|
||||
std::cerr << "Signature verification worked!" << std::endl;
|
||||
|
||||
delete[] test_bin ;
|
||||
|
||||
std::string outfile = "crypted_toto.pgp" ;
|
||||
std::string text_to_encrypt = "this is a secret message" ;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue