mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
exposed generic ID type in util/, patched rest of the code to use it.
git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5197 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
ba56f5f611
commit
af1fe976e2
@ -19,141 +19,6 @@ extern "C" {
|
||||
|
||||
PassphraseCallback PGPHandler::_passphrase_callback = NULL ;
|
||||
|
||||
std::string PGPIdType::toStdString() const
|
||||
{
|
||||
static const char out[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' } ;
|
||||
|
||||
std::string res(KEY_ID_SIZE*2,' ') ;
|
||||
|
||||
for(int j = 0; j < KEY_ID_SIZE; j++)
|
||||
{
|
||||
res[2*j ] = out[ (bytes[j]>>4) ] ;
|
||||
res[2*j+1] = out[ bytes[j] & 0xf ] ;
|
||||
}
|
||||
|
||||
return res ;
|
||||
}
|
||||
std::string PGPFingerprintType::toStdString() const
|
||||
{
|
||||
static const char out[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' } ;
|
||||
|
||||
std::string res(KEY_FINGERPRINT_SIZE*2,' ') ;
|
||||
|
||||
for(int j = 0; j < KEY_FINGERPRINT_SIZE; j++)
|
||||
{
|
||||
res[2*j ] = out[ (bytes[j]>>4) ] ;
|
||||
res[2*j+1] = out[ bytes[j] & 0xf ] ;
|
||||
}
|
||||
|
||||
return res ;
|
||||
}
|
||||
|
||||
|
||||
PGPIdType PGPIdType::fromUserId_hex(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") ;
|
||||
|
||||
PGPIdType res ;
|
||||
|
||||
for(int i = 0; i < KEY_ID_SIZE; ++i)
|
||||
{
|
||||
res.bytes[i] = 0 ;
|
||||
|
||||
for(int k=0;k<2;++k)
|
||||
{
|
||||
char b = s[n++] ;
|
||||
|
||||
if(b >= 'A' && b <= 'F')
|
||||
res.bytes[i] += (b-'A'+10) << 4*(1-k) ;
|
||||
else if(b >= 'a' && b <= 'f')
|
||||
res.bytes[i] += (b-'a'+10) << 4*(1-k) ;
|
||||
else if(b >= '0' && b <= '9')
|
||||
res.bytes[i] += (b-'0') << 4*(1-k) ;
|
||||
else
|
||||
throw std::runtime_error("PGPIdType::Sha1CheckSum: can't init from non pure hexadecimal string") ;
|
||||
}
|
||||
}
|
||||
return res ;
|
||||
}
|
||||
PGPIdType PGPIdType::fromFingerprint_hex(const std::string& s)
|
||||
{
|
||||
if(s.length() != PGPFingerprintType::KEY_FINGERPRINT_SIZE*2)
|
||||
throw std::runtime_error("PGPIdType::PGPIdType: can only init from 40 chars hexadecimal string") ;
|
||||
|
||||
PGPIdType res ;
|
||||
|
||||
int n=2*PGPFingerprintType::KEY_FINGERPRINT_SIZE - 2*PGPIdType::KEY_ID_SIZE ;
|
||||
|
||||
for(int i = 0; i < PGPIdType::KEY_ID_SIZE; ++i)
|
||||
{
|
||||
res.bytes[i] = 0 ;
|
||||
|
||||
for(int k=0;k<2;++k)
|
||||
{
|
||||
char b = s[n++] ;
|
||||
|
||||
if(b >= 'A' && b <= 'F')
|
||||
res.bytes[i] += (b-'A'+10) << 4*(1-k) ;
|
||||
else if(b >= 'a' && b <= 'f')
|
||||
res.bytes[i] += (b-'a'+10) << 4*(1-k) ;
|
||||
else if(b >= '0' && b <= '9')
|
||||
res.bytes[i] += (b-'0') << 4*(1-k) ;
|
||||
else
|
||||
throw std::runtime_error("PGPIdType::Sha1CheckSum: can't init from non pure hexadecimal string") ;
|
||||
}
|
||||
}
|
||||
return res ;
|
||||
}
|
||||
PGPFingerprintType PGPFingerprintType::fromFingerprint_hex(const std::string& s)
|
||||
{
|
||||
int n=0;
|
||||
if(s.length() != PGPFingerprintType::KEY_FINGERPRINT_SIZE*2)
|
||||
throw std::runtime_error("PGPIdType::PGPIdType: can only init from 40 chars hexadecimal string") ;
|
||||
|
||||
PGPFingerprintType res ;
|
||||
|
||||
for(int i = 0; i < PGPFingerprintType::KEY_FINGERPRINT_SIZE; ++i)
|
||||
{
|
||||
res.bytes[i] = 0 ;
|
||||
|
||||
for(int k=0;k<2;++k)
|
||||
{
|
||||
char b = s[n++] ;
|
||||
|
||||
if(b >= 'A' && b <= 'F')
|
||||
res.bytes[i] += (b-'A'+10) << 4*(1-k) ;
|
||||
else if(b >= 'a' && b <= 'f')
|
||||
res.bytes[i] += (b-'a'+10) << 4*(1-k) ;
|
||||
else if(b >= '0' && b <= '9')
|
||||
res.bytes[i] += (b-'0') << 4*(1-k) ;
|
||||
else
|
||||
throw std::runtime_error("PGPIdType::Sha1CheckSum: can't init from non pure hexadecimal string") ;
|
||||
}
|
||||
}
|
||||
return res ;
|
||||
}
|
||||
PGPIdType::PGPIdType(const unsigned char b[])
|
||||
{
|
||||
memcpy(bytes,b,KEY_ID_SIZE) ;
|
||||
}
|
||||
PGPFingerprintType::PGPFingerprintType(const unsigned char b[])
|
||||
{
|
||||
memcpy(bytes,b,KEY_FINGERPRINT_SIZE) ;
|
||||
}
|
||||
|
||||
|
||||
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 ;
|
||||
}
|
||||
|
||||
ops_keyring_t *PGPHandler::allocateOPSKeyring()
|
||||
{
|
||||
ops_keyring_t *kr = (ops_keyring_t*)malloc(sizeof(ops_keyring_t)) ;
|
||||
@ -342,7 +207,7 @@ bool PGPHandler::printKeys() const
|
||||
for(sit = it->second.signers.begin(); sit != it->second.signers.end(); sit++)
|
||||
{
|
||||
std::cerr << "\t\tSigner ID:" << *sit << ", Name: " ;
|
||||
const PGPCertificateInfo *info = PGPHandler::getCertificateInfo(PGPIdType::fromUserId_hex(*sit)) ;
|
||||
const PGPCertificateInfo *info = PGPHandler::getCertificateInfo(PGPIdType(*sit)) ;
|
||||
|
||||
if(info != NULL)
|
||||
std::cerr << info->_name ;
|
||||
@ -668,7 +533,7 @@ bool PGPHandler::getKeyFingerprint(const PGPIdType& id,PGPFingerprintType& fp) c
|
||||
|
||||
bool PGPHandler::VerifySignBin(const void *literal_data, uint32_t literal_data_length, unsigned char *sign, unsigned int sign_len, const PGPFingerprintType& key_fingerprint)
|
||||
{
|
||||
PGPIdType id = PGPIdType::fromFingerprint_hex(key_fingerprint.toStdString()) ;
|
||||
PGPIdType id = PGPIdType(key_fingerprint.toByteArray() + PGPFingerprintType::SIZE_IN_BYTES - PGPIdType::SIZE_IN_BYTES) ;
|
||||
const ops_keydata_t *key = getPublicKey(id) ;
|
||||
|
||||
if(key == NULL)
|
||||
@ -712,7 +577,7 @@ bool PGPHandler::getGPGFilteredList(std::list<PGPIdType>& list,bool (*filter)(co
|
||||
|
||||
for(std::map<std::string,PGPCertificateInfo>::const_iterator it(_public_keyring_map.begin());it!=_public_keyring_map.end();++it)
|
||||
if( filter == NULL || (*filter)(it->second) )
|
||||
list.push_back(PGPIdType::fromUserId_hex(it->first)) ;
|
||||
list.push_back(PGPIdType(it->first)) ;
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <util/rsthreads.h>
|
||||
#include <util/rsid.h>
|
||||
|
||||
extern "C" {
|
||||
#include <openpgpsdk/types.h>
|
||||
@ -15,55 +16,13 @@ extern "C" {
|
||||
#include <openpgpsdk/keyring_local.h>
|
||||
}
|
||||
|
||||
static const int KEY_ID_SIZE = 8 ;
|
||||
static const int KEY_FINGERPRINT_SIZE = 20 ;
|
||||
|
||||
typedef std::string (*PassphraseCallback)(void *data, const char *uid_hint, const char *passphrase_info, int prev_was_bad) ;
|
||||
|
||||
class PGPIdType
|
||||
{
|
||||
public:
|
||||
static const int KEY_ID_SIZE = 8 ;
|
||||
PGPIdType() {}
|
||||
|
||||
static PGPIdType fromUserId_hex(const std::string& hex_string) ;
|
||||
static PGPIdType fromFingerprint_hex(const std::string& hex_string) ;
|
||||
|
||||
explicit PGPIdType(const unsigned char bytes[]) ;
|
||||
|
||||
std::string toStdString() const ;
|
||||
uint64_t toUInt64() const ;
|
||||
const unsigned char *toByteArray() const { return &bytes[0] ; }
|
||||
|
||||
private:
|
||||
unsigned char bytes[KEY_ID_SIZE] ;
|
||||
std::string _string_id ;
|
||||
};
|
||||
class PGPFingerprintType
|
||||
{
|
||||
public:
|
||||
static const int KEY_FINGERPRINT_SIZE = 20 ;
|
||||
|
||||
static PGPFingerprintType fromFingerprint_hex(const std::string& hex_string) ;
|
||||
explicit PGPFingerprintType(const unsigned char bytes[]) ;
|
||||
|
||||
std::string toStdString() const ;
|
||||
const unsigned char *toByteArray() const { return &bytes[0] ; }
|
||||
|
||||
bool operator==(const PGPFingerprintType& fp) const
|
||||
{
|
||||
for(int i=0;i<KEY_FINGERPRINT_SIZE;++i)
|
||||
if(fp.bytes[i] != bytes[i])
|
||||
return false ;
|
||||
return true ;
|
||||
}
|
||||
bool operator!=(const PGPFingerprintType& fp) const
|
||||
{
|
||||
return !operator==(fp) ;
|
||||
}
|
||||
|
||||
PGPFingerprintType() {}
|
||||
private:
|
||||
unsigned char bytes[KEY_FINGERPRINT_SIZE] ;
|
||||
std::string _string_id ;
|
||||
};
|
||||
typedef t_RsGenericIdType<KEY_ID_SIZE> PGPIdType;
|
||||
typedef t_RsGenericIdType<KEY_FINGERPRINT_SIZE> PGPFingerprintType ;
|
||||
|
||||
class PGPCertificateInfo
|
||||
{
|
||||
|
@ -28,9 +28,9 @@ int main(int argc,char *argv[])
|
||||
{
|
||||
// test pgp ids.
|
||||
//
|
||||
PGPIdType id = PGPIdType::fromUserId_hex("3e5b22140ef56abb") ;
|
||||
PGPIdType id = PGPIdType(std::string("3e5b22140ef56abb")) ;
|
||||
|
||||
std::cerr << "Id is : " << std::hex << id.toUInt64() << std::endl;
|
||||
//std::cerr << "Id is : " << std::hex << id.toUInt64() << std::endl;
|
||||
std::cerr << "Id st : " << id.toStdString() << std::endl;
|
||||
|
||||
// test PGPHandler
|
||||
@ -76,7 +76,7 @@ int main(int argc,char *argv[])
|
||||
else
|
||||
std::cerr << "Certificate generation success. New id = " << newid.toStdString() << std::endl;
|
||||
|
||||
PGPIdType id2 = PGPIdType::fromUserId_hex("618E54CF7670FF5E") ;
|
||||
PGPIdType id2 = PGPIdType(std::string("618E54CF7670FF5E")) ;
|
||||
std::cerr << "Now extracting key " << id2.toStdString() << " from keyring:" << std::endl ;
|
||||
std::string cert = pgph.SaveCertificateToString(id2,false) ;
|
||||
|
||||
|
112
libretroshare/src/util/rsid.h
Normal file
112
libretroshare/src/util/rsid.h
Normal file
@ -0,0 +1,112 @@
|
||||
// This class aims at defining a generic ID type that is a list of bytes. It
|
||||
// can be converted into a hexadecial string for printing, mainly) or for
|
||||
// compatibility with old methods.
|
||||
//
|
||||
// To use this class, derive your own ID type from it. Examples include:
|
||||
//
|
||||
// class PGPIdType: public t_RsGenericIdType<8>
|
||||
// {
|
||||
// [..]
|
||||
// };
|
||||
//
|
||||
// class PGPFingerprintType: public t_RsGenericIdType<20>
|
||||
// {
|
||||
// [..]
|
||||
// };
|
||||
//
|
||||
// With this, there is no implicit conversion between subtypes, and therefore ID mixup
|
||||
// is impossible.
|
||||
//
|
||||
// A simpler way to make ID types is to
|
||||
// typedef t_RsGenericIdType<MySize> MyType ;
|
||||
//
|
||||
// ID Types with different lengths will be incompatible on compilation.
|
||||
//
|
||||
// Warning: never store references to a t_RsGenericIdType accross threads, since the
|
||||
// cached string convertion is not thread safe.
|
||||
//
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
template<uint32_t ID_SIZE_IN_BYTES> class t_RsGenericIdType
|
||||
{
|
||||
public:
|
||||
t_RsGenericIdType() {}
|
||||
virtual ~t_RsGenericIdType() {}
|
||||
|
||||
// Explicit constructor from a hexadecimal string
|
||||
//
|
||||
explicit t_RsGenericIdType(const std::string& hex_string) ;
|
||||
|
||||
// Explicit constructor from a byte array. The array should have size at least ID_SIZE_IN_BYTES
|
||||
//
|
||||
explicit t_RsGenericIdType(const unsigned char bytes[]) ;
|
||||
|
||||
// Converts to a std::string using cached value.
|
||||
//
|
||||
std::string toStdString() const ;
|
||||
const unsigned char *toByteArray() const { return &bytes[0] ; }
|
||||
static const uint32_t SIZE_IN_BYTES = ID_SIZE_IN_BYTES ;
|
||||
|
||||
bool operator==(const t_RsGenericIdType<ID_SIZE_IN_BYTES>& fp) const
|
||||
{
|
||||
for(uint32_t i=0;i<ID_SIZE_IN_BYTES;++i)
|
||||
if(fp.bytes[i] != bytes[i])
|
||||
return false ;
|
||||
return true ;
|
||||
}
|
||||
bool operator!=(const t_RsGenericIdType<ID_SIZE_IN_BYTES>& fp) const
|
||||
{
|
||||
return !operator==(fp) ;
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned char bytes[ID_SIZE_IN_BYTES] ;
|
||||
};
|
||||
|
||||
template<uint32_t ID_SIZE_IN_BYTES> std::string t_RsGenericIdType<ID_SIZE_IN_BYTES>::toStdString() const
|
||||
{
|
||||
static const char out[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' } ;
|
||||
|
||||
std::string res(ID_SIZE_IN_BYTES*2,' ') ;
|
||||
|
||||
for(uint32_t j = 0; j < ID_SIZE_IN_BYTES; j++)
|
||||
{
|
||||
res[2*j ] = out[ (bytes[j]>>4) ] ;
|
||||
res[2*j+1] = out[ bytes[j] & 0xf ] ;
|
||||
}
|
||||
|
||||
return res ;
|
||||
}
|
||||
|
||||
template<uint32_t ID_SIZE_IN_BYTES> t_RsGenericIdType<ID_SIZE_IN_BYTES>::t_RsGenericIdType(const std::string& s)
|
||||
{
|
||||
int n=0;
|
||||
if(s.length() != ID_SIZE_IN_BYTES*2)
|
||||
throw std::runtime_error("t_RsGenericIdType<>::t_RsGenericIdType(std::string&): supplied string in constructor has wrong size.") ;
|
||||
|
||||
for(uint32_t i = 0; i < ID_SIZE_IN_BYTES; ++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("t_RsGenericIdType<>::t_RsGenericIdType(std::string&): supplied string is not purely hexadecimal") ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<uint32_t ID_SIZE_IN_BYTES> t_RsGenericIdType<ID_SIZE_IN_BYTES>::t_RsGenericIdType(const unsigned char *mem)
|
||||
{
|
||||
memcpy(bytes,mem,ID_SIZE_IN_BYTES) ;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user