On Sat, Aug 01, 2009, Conor wrote:

> Greetings community,
> 
> Alright, I'm trying to serialise a RSA public key and then
> deserialise it again; Later the key will be serialised and
> then deserialised on the other end of a TCP connection.
> For now, I'm just serialising and then deserialising the
> key. I've read the other posts to the list on this, but I
> haven't had any luck.
> 
> Here's my problem - The deserialised key does not match
> the original public key:
> 
> // Code beginning
> 
> int serialisedPublicKeySize = i2d_RSAPublicKey(ourKeyPair->pub_key, NULL);
> unsigned char* serialisedPublicKey = new unsigned 
> char[serialisedPublicKeySize];
> unsigned char* ptrSerialisedPublicKey = serialisedPublicKey;
> serialisedPublicKeySize = i2d_RSAPublicKey(ourKeyPair->pub_key,
> &ptrSerialisedPublicKey);
> 
> FILE* pub_kfp = fopen("serialised_key.pem", "w");
> fwrite(ptrSerialisedPublicKey, sizeof(unsigned char),
> serialisedPublicKeySize, pub_kfp);
> fclose(pub_kfp);
> 
> // I'm correct to use serialisedPublicKey here, right?
> RSA* clientPublicKey = d2i_RSAPublicKey(NULL, (const unsigned
> char**)&serialisedPublicKey), serialisedPublicKeySize);
> // This doesn't work either.
> //RSA* clientPublicKey = d2i_RSAPublicKey(NULL, (const unsigned
> char**)&ptrSerialisedPublicKey), serialisedPublicKeySize);
> 
> int x;
> if((x = memcmp(ourKeyPair->pub_key, clientPublicKey,
> RSA_size(clientPublicKey))) == 0)
> {
>       printf("Keys match\n");
> }
> printf("%d\n", x);
> 
> // Code end
> 
> If I diff the dumped key file with the original public key
> file, it of course tells me they differ, although visually
> they are identical. I've been hacking away at this now
> for a while without any luck. I hope you guys can point
> me in the right direction, and that I'm just making some
> incredibly stupid mistake.
> 

Well memcmp certainly wont work with an RSA structure, it contains pointers
which will differ even for the same key.

You can check the values by calling BN_cmp() on rsa->n and rsa->e .

Steve.
--
Dr Stephen N. Henson. OpenSSL project core developer.
Commercial tech support now available see: http://www.openssl.org
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to