implemented more tests and fixed a few bugs in GxsSecurity

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7683 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2014-11-15 20:24:56 +00:00
parent c9881d92a6
commit e007151197
2 changed files with 44 additions and 23 deletions

View File

@ -64,7 +64,7 @@ static RSA *extractPublicKey(const RsTlvSecurityKey& key)
return rsakey; return rsakey;
} }
static void setRSAPublicKey(RsTlvSecurityKey & key, RSA *rsa_pub) static void setRSAPublicKeyData(RsTlvSecurityKey & key, RSA *rsa_pub)
{ {
unsigned char *data = NULL ; // this works for OpenSSL > 0.9.7 unsigned char *data = NULL ; // this works for OpenSSL > 0.9.7
int reqspace = i2d_RSAPublicKey(rsa_pub, &data); int reqspace = i2d_RSAPublicKey(rsa_pub, &data);
@ -75,7 +75,7 @@ static void setRSAPublicKey(RsTlvSecurityKey & key, RSA *rsa_pub)
free(data) ; free(data) ;
} }
static void setRSAPrivateKey(RsTlvSecurityKey & key, RSA *rsa_priv) static void setRSAPrivateKeyData(RsTlvSecurityKey & key, RSA *rsa_priv)
{ {
unsigned char *data = NULL ; unsigned char *data = NULL ;
int reqspace = i2d_RSAPrivateKey(rsa_priv, &data); int reqspace = i2d_RSAPrivateKey(rsa_priv, &data);
@ -88,6 +88,8 @@ static void setRSAPrivateKey(RsTlvSecurityKey & key, RSA *rsa_priv)
static RSA *extractPrivateKey(const RsTlvSecurityKey & key) static RSA *extractPrivateKey(const RsTlvSecurityKey & key)
{ {
assert(key.keyFlags & RSTLV_KEY_TYPE_FULL) ;
const unsigned char *keyptr = (const unsigned char *) key.keyData.bin_data; const unsigned char *keyptr = (const unsigned char *) key.keyData.bin_data;
long keylen = key.keyData.bin_len; long keylen = key.keyData.bin_len;
@ -103,14 +105,16 @@ bool GxsSecurity::generateKeyPair(RsTlvSecurityKey& public_key,RsTlvSecurityKey&
RSA *rsa = RSA_generate_key(2048, 65537, NULL, NULL); RSA *rsa = RSA_generate_key(2048, 65537, NULL, NULL);
RSA *rsa_pub = RSAPublicKey_dup(rsa); RSA *rsa_pub = RSAPublicKey_dup(rsa);
setRSAPublicKey(public_key, rsa_pub); setRSAPublicKeyData(public_key, rsa_pub);
setRSAPrivateKey(private_key, rsa); setRSAPrivateKeyData(private_key, rsa);
public_key.startTS = time(NULL); public_key.startTS = time(NULL);
public_key.endTS = public_key.startTS + 60 * 60 * 24 * 365 * 5; /* approx 5 years */ public_key.endTS = public_key.startTS + 60 * 60 * 24 * 365 * 5; /* approx 5 years */
public_key.keyFlags = RSTLV_KEY_TYPE_PUBLIC_ONLY ;
private_key.startTS = public_key.startTS; private_key.startTS = public_key.startTS;
private_key.endTS = 0; /* no end */ private_key.endTS = 0; /* no end */
private_key.keyFlags = RSTLV_KEY_TYPE_FULL ;
// clean up // clean up
RSA_free(rsa); RSA_free(rsa);
@ -121,28 +125,31 @@ bool GxsSecurity::generateKeyPair(RsTlvSecurityKey& public_key,RsTlvSecurityKey&
bool GxsSecurity::extractPublicKey(const RsTlvSecurityKey& private_key,RsTlvSecurityKey& public_key) bool GxsSecurity::extractPublicKey(const RsTlvSecurityKey& private_key,RsTlvSecurityKey& public_key)
{ {
if(!(private_key.keyFlags & RSTLV_KEY_TYPE_FULL)) public_key.TlvClear() ;
return false ;
RSA *rsaPrivKey = extractPrivateKey(private_key); if(!(private_key.keyFlags & RSTLV_KEY_TYPE_FULL))
return false ;
if(!rsaPrivKey) RSA *rsaPrivKey = extractPrivateKey(private_key);
return false ;
RSA *rsaPubKey = RSAPublicKey_dup(rsaPrivKey); if(!rsaPrivKey)
RSA_free(rsaPrivKey); return false ;
if(!rsaPubKey) RSA *rsaPubKey = RSAPublicKey_dup(rsaPrivKey);
return false ; RSA_free(rsaPrivKey);
setRSAPublicKey(public_key, rsaPubKey); if(!rsaPubKey)
RSA_free(rsaPubKey); return false ;
public_key.keyFlags = private_key.keyFlags & (RSTLV_KEY_DISTRIB_MASK) ; // keep the distrib flags setRSAPublicKeyData(public_key, rsaPubKey);
public_key.keyFlags |= RSTLV_KEY_TYPE_PUBLIC_ONLY; RSA_free(rsaPubKey);
public_key.endTS = public_key.startTS + 60 * 60 * 24 * 365 * 5; /* approx 5 years */
return true ; public_key.keyFlags = private_key.keyFlags & (RSTLV_KEY_DISTRIB_MASK) ; // keep the distrib flags
public_key.keyFlags |= RSTLV_KEY_TYPE_PUBLIC_ONLY;
public_key.startTS = private_key.startTS ;
public_key.endTS = public_key.startTS + 60 * 60 * 24 * 365 * 5; /* approx 5 years */
return true ;
} }
bool GxsSecurity::getSignature(const char *data, uint32_t data_len, const RsTlvSecurityKey& privKey, RsTlvKeySignature& sign) bool GxsSecurity::getSignature(const char *data, uint32_t data_len, const RsTlvSecurityKey& privKey, RsTlvKeySignature& sign)

View File

@ -42,6 +42,20 @@ TEST(libretroshare_gxs, GxsSecurity)
srand48(getpid()) ; srand48(getpid()) ;
EXPECT_TRUE( pub_key.keyId == priv_key.keyId );
EXPECT_TRUE( pub_key.startTS == priv_key.startTS );
RsTlvSecurityKey pub_key2 ;
EXPECT_TRUE(GxsSecurity::extractPublicKey(priv_key,pub_key2)) ;
EXPECT_TRUE( pub_key.keyId == pub_key2.keyId );
EXPECT_TRUE( pub_key.keyFlags == pub_key2.keyFlags );
EXPECT_TRUE( pub_key.startTS == pub_key2.startTS );
EXPECT_TRUE( pub_key.endTS == pub_key2.endTS );
EXPECT_TRUE(pub_key.keyData.bin_len == pub_key2.keyData.bin_len) ;
EXPECT_TRUE(!memcmp(pub_key.keyData.bin_data,pub_key2.keyData.bin_data,pub_key.keyData.bin_len));
// create some random data and sign it / verify the signature. // create some random data and sign it / verify the signature.
uint32_t data_len = 1000 + RSRandom::random_u32()%100 ; uint32_t data_len = 1000 + RSRandom::random_u32()%100 ;