mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 06:06:10 -04:00
patched openpgpsdkfor c++ compilation, added test program, started retroshare PGPHandler component
git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5050 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
7d06d19e40
commit
4299d09741
8 changed files with 341 additions and 121 deletions
89
libretroshare/src/pgp/pgphandler.cc
Normal file
89
libretroshare/src/pgp/pgphandler.cc
Normal file
|
@ -0,0 +1,89 @@
|
|||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <stdlib.h>
|
||||
|
||||
extern "C" {
|
||||
#include <openpgpsdk/util.h>
|
||||
}
|
||||
#include "pgphandler.h"
|
||||
|
||||
std::string PGPIdType::toStdString() const
|
||||
{
|
||||
std::ostringstream tmpout;
|
||||
|
||||
for(int j = 0; j < KEY_ID_SIZE; j++)
|
||||
tmpout << std::setw(2) << std::setfill('0') << std::hex << (int)bytes[j] ;
|
||||
|
||||
return tmpout.str() ;
|
||||
}
|
||||
|
||||
PGPIdType::PGPIdType(const std::string& s)
|
||||
{
|
||||
int n=0;
|
||||
if(s.length() != KEY_ID_SIZE*2)
|
||||
throw std::runtime_error("PGPIdType::PGPIdType: can only init from 16 chars hexadecimal string") ;
|
||||
|
||||
for(int i = 0; i < KEY_ID_SIZE; ++i)
|
||||
{
|
||||
bytes[i] = 0 ;
|
||||
|
||||
for(int k=0;k<2;++k)
|
||||
{
|
||||
char b = s[n++] ;
|
||||
|
||||
if(b >= 'A' && b <= 'F')
|
||||
bytes[i] += (b-'A'+10) << 4*(1-k) ;
|
||||
else if(b >= 'a' && b <= 'f')
|
||||
bytes[i] += (b-'a'+10) << 4*(1-k) ;
|
||||
else if(b >= '0' && b <= '9')
|
||||
bytes[i] += (b-'0') << 4*(1-k) ;
|
||||
else
|
||||
throw std::runtime_error("PGPIdType::Sha1CheckSum: can't init from non pure hexadecimal string") ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t PGPIdType::toUInt64() const
|
||||
{
|
||||
uint64_t res = 0 ;
|
||||
|
||||
for(int i=0;i<KEY_ID_SIZE;++i)
|
||||
res = (res << 8) + bytes[i] ;
|
||||
|
||||
return res ;
|
||||
}
|
||||
|
||||
PGPHandler::PGPHandler(const std::string& pubring, const std::string& secring)
|
||||
:_pubring_path(pubring),_secring_path(secring),
|
||||
pgphandlerMtx(std::string("PGPHandler"))
|
||||
{
|
||||
// Allocate public and secret keyrings.
|
||||
//
|
||||
_pubring = (ops_keyring_t*)malloc(sizeof(ops_keyring_t)) ;
|
||||
_secring = (ops_keyring_t*)malloc(sizeof(ops_keyring_t)) ;
|
||||
|
||||
// Read public and secret keyrings from supplied files.
|
||||
//
|
||||
if(ops_false == ops_keyring_read_from_file(_pubring, false, pubring.c_str()))
|
||||
throw std::runtime_error("PGPHandler::readKeyRing(): cannot read pubring.") ;
|
||||
|
||||
std::cerr << "Pubring read successfully." << std::endl;
|
||||
|
||||
if(ops_false == ops_keyring_read_from_file(_secring, false, secring.c_str()))
|
||||
throw std::runtime_error("PGPHandler::readKeyRing(): cannot read secring.") ;
|
||||
|
||||
std::cerr << "Secring read successfully." << std::endl;
|
||||
}
|
||||
|
||||
PGPHandler::~PGPHandler()
|
||||
{
|
||||
std::cerr << "Freeing PGPHandler. Deleting keyrings." << std::endl;
|
||||
|
||||
ops_keyring_free(_pubring) ;
|
||||
ops_keyring_free(_secring) ;
|
||||
|
||||
free(_pubring) ;
|
||||
free(_secring) ;
|
||||
}
|
61
libretroshare/src/pgp/pgphandler.h
Normal file
61
libretroshare/src/pgp/pgphandler.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
// This class implements an abstract pgp handler to be used in RetroShare.
|
||||
//
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <util/rsthreads.h>
|
||||
|
||||
extern "C" {
|
||||
#include <openpgpsdk/types.h>
|
||||
#include <openpgpsdk/keyring.h>
|
||||
}
|
||||
|
||||
class PGPIdType
|
||||
{
|
||||
public:
|
||||
static const int KEY_ID_SIZE = 8 ;
|
||||
|
||||
PGPIdType(const std::string& hex_string) ;
|
||||
PGPIdType(const unsigned char bytes[]) ;
|
||||
|
||||
std::string toStdString() const ;
|
||||
uint64_t toUInt64() const ;
|
||||
|
||||
private:
|
||||
unsigned char bytes[KEY_ID_SIZE] ;
|
||||
};
|
||||
|
||||
class PGPHandler
|
||||
{
|
||||
public:
|
||||
PGPHandler(const std::string& path_to_public_keyring, const std::string& path_to_secret_keyring) ;
|
||||
|
||||
virtual ~PGPHandler() ;
|
||||
|
||||
/**
|
||||
* @param ids list of gpg certificate ids (note, not the actual certificates)
|
||||
*/
|
||||
|
||||
bool availableGPGCertificatesWithPrivateKeys(std::list<PGPIdType>& ids);
|
||||
bool GeneratePGPCertificate(const std::string& name, const std::string& email, const std::string& passwd, PGPIdType& pgpId, std::string& errString) ;
|
||||
|
||||
bool LoadCertificateFromString(const std::string& pem, PGPIdType& gpg_id, std::string& error_string);
|
||||
std::string SaveCertificateToString(const PGPIdType& id,bool include_signatures) ;
|
||||
|
||||
bool TrustCertificate(const PGPIdType& id, int trustlvl);
|
||||
|
||||
virtual bool SignDataBin(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen) { return false ; }
|
||||
virtual bool VerifySignBin(const void*, uint32_t, unsigned char*, unsigned int, const std::string &withfingerprint) { return false ; }
|
||||
|
||||
// Debug stuff.
|
||||
virtual bool printKeys() { return false;}
|
||||
|
||||
private:
|
||||
RsMutex pgphandlerMtx ;
|
||||
|
||||
ops_keyring_t *_pubring ;
|
||||
ops_keyring_t *_secring ;
|
||||
|
||||
const std::string _pubring_path ;
|
||||
const std::string _secring_path ;
|
||||
};
|
25
libretroshare/src/pgp/test_pgp_handler.cc
Normal file
25
libretroshare/src/pgp/test_pgp_handler.cc
Normal file
|
@ -0,0 +1,25 @@
|
|||
// 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
|
||||
//
|
||||
#include <iostream>
|
||||
#include "pgphandler.h"
|
||||
|
||||
int main(int argc,char *argv[])
|
||||
{
|
||||
// test pgp ids.
|
||||
//
|
||||
PGPIdType id("3e5b22140ef56abb") ;
|
||||
|
||||
std::cerr << "Id is : " << std::hex << id.toUInt64() << std::endl;
|
||||
std::cerr << "Id st : " << id.toStdString() << std::endl;
|
||||
|
||||
// test PGPHandler
|
||||
//
|
||||
// 0 - init
|
||||
|
||||
static const std::string pubring = "pubring.gpg" ;
|
||||
static const std::string secring = "secring.gpg" ;
|
||||
|
||||
PGPHandler pgph(pubring,secring) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue