debugging of pgpkey parser and radix output form openpgpsdk

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5061 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2012-03-29 21:51:37 +00:00
parent 51fa97ac59
commit 648555711c
5 changed files with 67 additions and 32 deletions

View File

@ -160,7 +160,7 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri
ops_user_id_t uid ;
const char *s = strdup((name + " " + email + " (Generated by RetroShare)").c_str()) ;
uid.user_id = (unsigned char *)s ;
unsigned long int e = 44497 ; // some prime number
unsigned long int e = 17 ; // some prime number
ops_keydata_t *key = ops_rsa_create_selfsigned_keypair(KEY_NUMBITS,e,&uid) ;
@ -171,25 +171,44 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri
// Now output the pubkey to a string.
//
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)
{
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_writer_push_armoured(cinfo,OPS_PGP_PUBLIC_KEY_BLOCK) ;
//ops_writer_push_armoured(cinfo,OPS_PGP_SIGNATURE) ;
ops_write_transferable_public_key(key,armoured,cinfo);
//ops_writer_close(cinfo) ;
ops_writer_close(cinfo) ;
std::cerr << "Memory written: size = " << ops_memory_get_length(buf) << std::endl;
std::cerr << "String of key: " << std::endl;
std::cerr << std::string((char *)ops_memory_get_data(buf),ops_memory_get_length(buf)) << std::endl;
std::string akey((char *)ops_memory_get_data(buf),ops_memory_get_length(buf)) ;
//ops_teardown_memory_write(cinfo,buf);
ops_teardown_memory_write(cinfo,buf);
ops_keydata_free(key) ;
return true ;
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) ;
}

View File

@ -22,6 +22,7 @@ class PGPIdType
std::string toStdString() const ;
uint64_t toUInt64() const ;
const unsigned char *toByteArray() const { return &bytes[0] ; }
private:
unsigned char bytes[KEY_ID_SIZE] ;
@ -53,6 +54,8 @@ class PGPHandler
virtual void printKeys() const ;
private:
static std::string makeRadixEncodedPGPKey(const ops_keydata_t *key) ;
RsMutex pgphandlerMtx ;
ops_keyring_t *_pubring ;

View File

@ -50,6 +50,11 @@ int main(int argc,char *argv[])
else
std::cerr << "Certificate generation success. New id = " << newid.toStdString() << std::endl;
PGPIdType id2(std::string("EFD19E9DC737CA98")) ;
std::cerr << "Now extracting key " << id2.toStdString() << " from keyring:" << std::endl ;
std::string cert = pgph.SaveCertificateToString(id2,false) ;
std::cerr << cert << std::endl;
return 0 ;
}

View File

@ -99,28 +99,7 @@ bool PGPKeyManagement::createMinimalKey(const std::string& pgp_certificate,std::
break ;
}
std::string outstring ;
Radix64::encode(keydata,(uint64_t)data - (uint64_t)keydata,outstring) ;
uint32_t crc = compute24bitsCRC((unsigned char *)keydata,(uint64_t)data - (uint64_t)keydata) ;
unsigned char tmp[3] = { (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff } ;
std::string crc_string ;
Radix64::encode((const char *)tmp,3,crc_string) ;
#ifdef DEBUG_PGPUTIL
std::cerr << "After signature pruning: " << std::endl;
std::cerr << outstring << std::endl;
#endif
cleaned_certificate = std::string(PGP_CERTIFICATE_START_STRING) + "\n" + version_string + "\n\n" ;
for(uint32_t i=0;i<outstring.length();i+=64)
cleaned_certificate += outstring.substr(i,64) + "\n" ;
cleaned_certificate += "=" + crc_string + "\n" ;
cleaned_certificate += std::string(PGP_CERTIFICATE_END_STRING) + "\n" ;
cleaned_certificate = makeArmouredKey((unsigned char*)keydata,(uint64_t)data - (uint64_t)keydata,version_string) ;
return true ;
}
catch(std::exception& e)
@ -131,6 +110,33 @@ bool PGPKeyManagement::createMinimalKey(const std::string& pgp_certificate,std::
}
}
std::string PGPKeyManagement::makeArmouredKey(const unsigned char *keydata,size_t key_size,const std::string& version_string)
{
std::string outstring ;
Radix64::encode((const char *)keydata,key_size,outstring) ;
uint32_t crc = compute24bitsCRC((unsigned char *)keydata,key_size) ;
unsigned char tmp[3] = { (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff } ;
std::string crc_string ;
Radix64::encode((const char *)tmp,3,crc_string) ;
#ifdef DEBUG_PGPUTIL
std::cerr << "After signature pruning: " << std::endl;
std::cerr << outstring << std::endl;
#endif
std::string certificate = std::string(PGP_CERTIFICATE_START_STRING) + "\n" + version_string + "\n\n" ;
for(uint32_t i=0;i<outstring.length();i+=64)
certificate += outstring.substr(i,64) + "\n" ;
certificate += "=" + crc_string + "\n" ;
certificate += std::string(PGP_CERTIFICATE_END_STRING) + "\n" ;
return certificate ;
}
uint32_t PGPKeyManagement::compute24bitsCRC(unsigned char *octets, size_t len)
{
long crc = PGP_CRC24_INIT;
@ -171,6 +177,7 @@ uint32_t PGPKeyParser::read_125Size(unsigned char *& data)
return b1 ;
uint8_t b2 = *data ;
++data ;
if(b1 < 224)
return ((b1-192) << 8) + b2 + 192 ;

View File

@ -59,6 +59,7 @@ class PGPKeyManagement
//
static bool createMinimalKey(const std::string& pgp_certificate,std::string& cleaned_certificate) ;
static std::string makeArmouredKey(const unsigned char *keydata,size_t key_size,const std::string& version_string) ;
private:
// Computes the 24 bits CRC checksum necessary to all PGP data.
//