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 ; ops_user_id_t uid ;
const char *s = strdup((name + " " + email + " (Generated by RetroShare)").c_str()) ; const char *s = strdup((name + " " + email + " (Generated by RetroShare)").c_str()) ;
uid.user_id = (unsigned char *)s ; 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) ; 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. // 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 armoured=ops_true;
ops_boolean_t overwrite=ops_true; ops_boolean_t overwrite=ops_true;
ops_create_info_t* cinfo; ops_create_info_t* cinfo;
ops_memory_t *buf = NULL ;//(ops_memory_t*)ops_mallocz(1000) ; ops_memory_t *buf = NULL ;//(ops_memory_t*)ops_mallocz(1000) ;
ops_setup_memory_write(&cinfo, &buf, 0); 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_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::string akey((char *)ops_memory_get_data(buf),ops_memory_get_length(buf)) ;
std::cerr << "String of key: " << std::endl;
std::cerr << std::string((char *)ops_memory_get_data(buf),ops_memory_get_length(buf)) << std::endl;
//ops_teardown_memory_write(cinfo,buf); ops_teardown_memory_write(cinfo,buf);
ops_keydata_free(key) ; return akey ;
return true ; }
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 ; std::string toStdString() const ;
uint64_t toUInt64() const ; uint64_t toUInt64() const ;
const unsigned char *toByteArray() const { return &bytes[0] ; }
private: private:
unsigned char bytes[KEY_ID_SIZE] ; unsigned char bytes[KEY_ID_SIZE] ;
@ -53,6 +54,8 @@ class PGPHandler
virtual void printKeys() const ; virtual void printKeys() const ;
private: private:
static std::string makeRadixEncodedPGPKey(const ops_keydata_t *key) ;
RsMutex pgphandlerMtx ; RsMutex pgphandlerMtx ;
ops_keyring_t *_pubring ; ops_keyring_t *_pubring ;

View file

@ -50,6 +50,11 @@ int main(int argc,char *argv[])
else else
std::cerr << "Certificate generation success. New id = " << newid.toStdString() << std::endl; 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 ; return 0 ;
} }

View file

@ -99,10 +99,23 @@ bool PGPKeyManagement::createMinimalKey(const std::string& pgp_certificate,std::
break ; break ;
} }
std::string outstring ; cleaned_certificate = makeArmouredKey((unsigned char*)keydata,(uint64_t)data - (uint64_t)keydata,version_string) ;
Radix64::encode(keydata,(uint64_t)data - (uint64_t)keydata,outstring) ; return true ;
}
catch(std::exception& e)
{
cleaned_certificate = "" ;
std::cerr << "Certificate cleaning failed: " << e.what() << std::endl;
return false ;
}
}
uint32_t crc = compute24bitsCRC((unsigned char *)keydata,(uint64_t)data - (uint64_t)keydata) ; 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 } ; unsigned char tmp[3] = { (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff } ;
std::string crc_string ; std::string crc_string ;
@ -113,22 +126,15 @@ bool PGPKeyManagement::createMinimalKey(const std::string& pgp_certificate,std::
std::cerr << outstring << std::endl; std::cerr << outstring << std::endl;
#endif #endif
cleaned_certificate = std::string(PGP_CERTIFICATE_START_STRING) + "\n" + version_string + "\n\n" ; std::string certificate = std::string(PGP_CERTIFICATE_START_STRING) + "\n" + version_string + "\n\n" ;
for(uint32_t i=0;i<outstring.length();i+=64) for(uint32_t i=0;i<outstring.length();i+=64)
cleaned_certificate += outstring.substr(i,64) + "\n" ; certificate += outstring.substr(i,64) + "\n" ;
cleaned_certificate += "=" + crc_string + "\n" ; certificate += "=" + crc_string + "\n" ;
cleaned_certificate += std::string(PGP_CERTIFICATE_END_STRING) + "\n" ; certificate += std::string(PGP_CERTIFICATE_END_STRING) + "\n" ;
return true ; return certificate ;
}
catch(std::exception& e)
{
cleaned_certificate = "" ;
std::cerr << "Certificate cleaning failed: " << e.what() << std::endl;
return false ;
}
} }
uint32_t PGPKeyManagement::compute24bitsCRC(unsigned char *octets, size_t len) uint32_t PGPKeyManagement::compute24bitsCRC(unsigned char *octets, size_t len)
@ -171,6 +177,7 @@ uint32_t PGPKeyParser::read_125Size(unsigned char *& data)
return b1 ; return b1 ;
uint8_t b2 = *data ; uint8_t b2 = *data ;
++data ;
if(b1 < 224) if(b1 < 224)
return ((b1-192) << 8) + b2 + 192 ; 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 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: private:
// Computes the 24 bits CRC checksum necessary to all PGP data. // Computes the 24 bits CRC checksum necessary to all PGP data.
// //