mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-12-30 09:56:14 -05:00
improvements to openpgp-sdk integration. Added/tested key generation, keyring output
git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5052 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
4299d09741
commit
765b6b9486
@ -3,20 +3,28 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
extern "C" {
|
||||
#include <openpgpsdk/util.h>
|
||||
#include <openpgpsdk/crypto.h>
|
||||
#include <openpgpsdk/keyring.h>
|
||||
}
|
||||
#include "pgphandler.h"
|
||||
|
||||
std::string PGPIdType::toStdString() const
|
||||
{
|
||||
std::ostringstream tmpout;
|
||||
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 < KEY_ID_SIZE; j++)
|
||||
tmpout << std::setw(2) << std::setfill('0') << std::hex << (int)bytes[j] ;
|
||||
{
|
||||
res += out[ (bytes[j]>>4) ] ;
|
||||
res += out[ bytes[j] & 0xf ] ;
|
||||
}
|
||||
|
||||
return tmpout.str() ;
|
||||
return res ;
|
||||
}
|
||||
|
||||
PGPIdType::PGPIdType(const std::string& s)
|
||||
@ -45,6 +53,11 @@ PGPIdType::PGPIdType(const std::string& s)
|
||||
}
|
||||
}
|
||||
|
||||
PGPIdType::PGPIdType(const unsigned char b[])
|
||||
{
|
||||
memcpy(bytes,b,8) ;
|
||||
}
|
||||
|
||||
uint64_t PGPIdType::toUInt64() const
|
||||
{
|
||||
uint64_t res = 0 ;
|
||||
@ -56,8 +69,7 @@ uint64_t PGPIdType::toUInt64() const
|
||||
}
|
||||
|
||||
PGPHandler::PGPHandler(const std::string& pubring, const std::string& secring)
|
||||
:_pubring_path(pubring),_secring_path(secring),
|
||||
pgphandlerMtx(std::string("PGPHandler"))
|
||||
: pgphandlerMtx(std::string("PGPHandler")), _pubring_path(pubring),_secring_path(secring)
|
||||
{
|
||||
// Allocate public and secret keyrings.
|
||||
//
|
||||
@ -87,3 +99,75 @@ PGPHandler::~PGPHandler()
|
||||
free(_pubring) ;
|
||||
free(_secring) ;
|
||||
}
|
||||
|
||||
void PGPHandler::printKeys() const
|
||||
{
|
||||
std::cerr << "Public keyring: " << std::endl;
|
||||
ops_keyring_list(_pubring) ;
|
||||
|
||||
std::cerr << "Secret keyring: " << std::endl;
|
||||
ops_keyring_list(_secring) ;
|
||||
}
|
||||
|
||||
bool PGPHandler::availableGPGCertificatesWithPrivateKeys(std::list<PGPIdType>& ids)
|
||||
{
|
||||
// go through secret keyring, and check that we have the pubkey as well.
|
||||
//
|
||||
|
||||
const ops_keydata_t *keydata = NULL ;
|
||||
int i=0 ;
|
||||
|
||||
while( (keydata = ops_keyring_get_key_by_index(_secring,i++)) != NULL )
|
||||
{
|
||||
// check that the key is in the pubring as well
|
||||
|
||||
if(ops_keyring_find_key_by_id(_pubring,keydata->key_id) != NULL)
|
||||
ids.push_back(PGPIdType(keydata->key_id)) ;
|
||||
}
|
||||
|
||||
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)))
|
||||
{
|
||||
const ops_parser_content_union_t *content=&content_->content;
|
||||
// validate_key_cb_arg_t *arg=ops_parse_cb_get_arg(cbinfo);
|
||||
// ops_error_t **errors=ops_parse_cb_get_errors(cbinfo);
|
||||
|
||||
switch(content_->tag)
|
||||
{
|
||||
case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
|
||||
/*
|
||||
Doing this so the test can be automated.
|
||||
*/
|
||||
*(content->secret_key_passphrase.passphrase)=ops_malloc_passphrase("hello");
|
||||
return OPS_KEEP_MEMORY;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return OPS_RELEASE_MEMORY;
|
||||
}
|
||||
|
||||
bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passwd, PGPIdType& pgpId, std::string& errString)
|
||||
{
|
||||
static const int KEY_NUMBITS = 2048 ;
|
||||
|
||||
ops_user_id_t uid ;
|
||||
const char *s = (name + " " + email).c_str() ;
|
||||
uid.user_id = (unsigned char *)s ;
|
||||
unsigned long int e = 44497 ; // some prime number
|
||||
|
||||
ops_keydata_t *key = ops_rsa_create_selfsigned_keypair(KEY_NUMBITS,e,&uid) ;
|
||||
|
||||
if(!key)
|
||||
return false ;
|
||||
|
||||
pgpId = PGPIdType(key->key_id) ;
|
||||
|
||||
ops_keydata_free(key) ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
extern "C" {
|
||||
#include <openpgpsdk/types.h>
|
||||
#include <openpgpsdk/keyring.h>
|
||||
#include <openpgpsdk/keyring_local.h>
|
||||
}
|
||||
|
||||
class PGPIdType
|
||||
@ -15,6 +16,7 @@ class PGPIdType
|
||||
public:
|
||||
static const int KEY_ID_SIZE = 8 ;
|
||||
|
||||
PGPIdType() {}
|
||||
PGPIdType(const std::string& hex_string) ;
|
||||
PGPIdType(const unsigned char bytes[]) ;
|
||||
|
||||
@ -48,7 +50,7 @@ class PGPHandler
|
||||
virtual bool VerifySignBin(const void*, uint32_t, unsigned char*, unsigned int, const std::string &withfingerprint) { return false ; }
|
||||
|
||||
// Debug stuff.
|
||||
virtual bool printKeys() { return false;}
|
||||
virtual void printKeys() const ;
|
||||
|
||||
private:
|
||||
RsMutex pgphandlerMtx ;
|
||||
|
@ -15,11 +15,41 @@ int main(int argc,char *argv[])
|
||||
// test PGPHandler
|
||||
//
|
||||
// 0 - init
|
||||
|
||||
|
||||
static const std::string pubring = "pubring.gpg" ;
|
||||
static const std::string secring = "secring.gpg" ;
|
||||
|
||||
PGPHandler pgph(pubring,secring) ;
|
||||
pgph.printKeys() ;
|
||||
|
||||
std::cerr << std::endl ;
|
||||
std::cerr << std::endl ;
|
||||
|
||||
std::cerr << "Looking for keys with complete secret/public key pair: " << std::endl;
|
||||
|
||||
std::list<PGPIdType> lst ;
|
||||
pgph.availableGPGCertificatesWithPrivateKeys(lst) ;
|
||||
|
||||
for(std::list<PGPIdType>::const_iterator it(lst.begin());it!=lst.end();++it)
|
||||
std::cerr << "Found id : " << (*it).toStdString() << std::endl;
|
||||
|
||||
std::string email_str("test@gmail.com") ;
|
||||
std::string name_str("test") ;
|
||||
std::string passw_str("test00") ;
|
||||
|
||||
std::cerr << "Now generating a new PGP certificate: " << std::endl;
|
||||
std::cerr << " email: " << email_str << std::endl;
|
||||
std::cerr << " passw: " << passw_str << std::endl;
|
||||
std::cerr << " name : " << name_str << std::endl;
|
||||
|
||||
PGPIdType newid ;
|
||||
std::string errString ;
|
||||
|
||||
if(!pgph.GeneratePGPCertificate(name_str, email_str, passw_str, newid, errString))
|
||||
std::cerr << "Generation of certificate returned error: " << errString << std::endl;
|
||||
else
|
||||
std::cerr << "Certificate generation success. New id = " << newid.toStdString() << std::endl;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user