2012-03-26 17:17:04 -04:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <stdlib.h>
|
2012-03-27 16:48:21 -04:00
|
|
|
#include <string.h>
|
2012-03-26 17:17:04 -04:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <openpgpsdk/util.h>
|
2012-03-27 16:48:21 -04:00
|
|
|
#include <openpgpsdk/crypto.h>
|
2012-03-27 17:45:43 -04:00
|
|
|
#include <openpgpsdk/armour.h>
|
2012-03-27 16:48:21 -04:00
|
|
|
#include <openpgpsdk/keyring.h>
|
2012-03-27 17:45:43 -04:00
|
|
|
#include <openpgpsdk/readerwriter.h>
|
2012-03-26 17:17:04 -04:00
|
|
|
}
|
|
|
|
#include "pgphandler.h"
|
|
|
|
|
|
|
|
std::string PGPIdType::toStdString() const
|
|
|
|
{
|
2012-03-27 16:48:21 -04:00
|
|
|
static const char out[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' } ;
|
|
|
|
|
|
|
|
std::string res ;
|
2012-03-26 17:17:04 -04:00
|
|
|
|
|
|
|
for(int j = 0; j < KEY_ID_SIZE; j++)
|
2012-03-27 16:48:21 -04:00
|
|
|
{
|
|
|
|
res += out[ (bytes[j]>>4) ] ;
|
|
|
|
res += out[ bytes[j] & 0xf ] ;
|
|
|
|
}
|
2012-03-26 17:17:04 -04:00
|
|
|
|
2012-03-27 16:48:21 -04:00
|
|
|
return res ;
|
2012-03-26 17:17:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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") ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-27 16:48:21 -04:00
|
|
|
PGPIdType::PGPIdType(const unsigned char b[])
|
|
|
|
{
|
|
|
|
memcpy(bytes,b,8) ;
|
|
|
|
}
|
|
|
|
|
2012-03-26 17:17:04 -04:00
|
|
|
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)
|
2012-03-27 16:48:21 -04:00
|
|
|
: pgphandlerMtx(std::string("PGPHandler")), _pubring_path(pubring),_secring_path(secring)
|
2012-03-26 17:17:04 -04:00
|
|
|
{
|
|
|
|
// 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) ;
|
|
|
|
}
|
2012-03-27 16:48:21 -04:00
|
|
|
|
|
|
|
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 ;
|
2012-03-27 17:45:43 -04:00
|
|
|
const char *s = strdup((name + " " + email + " (Generated by RetroShare)").c_str()) ;
|
2012-03-27 16:48:21 -04:00
|
|
|
uid.user_id = (unsigned char *)s ;
|
2012-03-29 17:51:37 -04:00
|
|
|
unsigned long int e = 17 ; // some prime number
|
2012-03-27 16:48:21 -04:00
|
|
|
|
|
|
|
ops_keydata_t *key = ops_rsa_create_selfsigned_keypair(KEY_NUMBITS,e,&uid) ;
|
|
|
|
|
|
|
|
if(!key)
|
|
|
|
return false ;
|
|
|
|
|
|
|
|
pgpId = PGPIdType(key->key_id) ;
|
|
|
|
|
2012-03-27 17:45:43 -04:00
|
|
|
// Now output the pubkey to a string.
|
|
|
|
//
|
2012-03-29 17:51:37 -04:00
|
|
|
std::string akey = makeRadixEncodedPGPKey(key) ;
|
|
|
|
|
|
|
|
std::cerr << "key: " << std::endl;
|
|
|
|
std::cerr << akey << std::endl;
|
|
|
|
ops_keydata_free(key) ;
|
|
|
|
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PGPHandler::makeRadixEncodedPGPKey(const ops_keydata_t *key)
|
|
|
|
{
|
2012-03-27 17:45:43 -04:00
|
|
|
ops_boolean_t armoured=ops_true;
|
|
|
|
ops_boolean_t overwrite=ops_true;
|
|
|
|
ops_create_info_t* cinfo;
|
|
|
|
|
|
|
|
ops_memory_t *buf = NULL ;//(ops_memory_t*)ops_mallocz(1000) ;
|
|
|
|
ops_setup_memory_write(&cinfo, &buf, 0);
|
|
|
|
|
|
|
|
ops_write_transferable_public_key(key,armoured,cinfo);
|
2012-03-29 17:51:37 -04:00
|
|
|
ops_writer_close(cinfo) ;
|
2012-03-27 17:45:43 -04:00
|
|
|
|
2012-03-29 17:51:37 -04:00
|
|
|
std::string akey((char *)ops_memory_get_data(buf),ops_memory_get_length(buf)) ;
|
2012-03-27 17:45:43 -04:00
|
|
|
|
2012-03-29 17:51:37 -04:00
|
|
|
ops_teardown_memory_write(cinfo,buf);
|
2012-03-27 17:45:43 -04:00
|
|
|
|
2012-03-29 17:51:37 -04:00
|
|
|
return akey ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PGPHandler::SaveCertificateToString(const PGPIdType& id,bool include_signatures)
|
|
|
|
{
|
|
|
|
const ops_keydata_t *key = ops_keyring_find_key_by_id(_pubring,id.toByteArray());
|
|
|
|
|
|
|
|
if(key == NULL)
|
|
|
|
{
|
|
|
|
std::cerr << "Cannot output key " << id.toStdString() << ": not found in keyring." << std::endl;
|
|
|
|
return "" ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return makeRadixEncodedPGPKey(key) ;
|
2012-03-27 16:48:21 -04:00
|
|
|
}
|
|
|
|
|