Oscar
Oscar

Reputation: 2345

Why would RSA_size crash?

I'm trying to use the OpenSSL crypto lib and it's crashing in a call that's a staple in every example I've seen. Here's how it's set up:

BIO* bp = BIO_new_mem_buf(_publicKey, -1); // Create a new memory buffer BIO.
RSA* pubKey = PEM_read_bio_RSA_PUBKEY(bp, 0, 0, 0); // And read the RSA key from it.

unsigned char encryptedRandKey[RSA_size(pubKey)];
BIO_free(bp);

_publicKey is a null-terminated character string containing a PEM-formatted RSA key (with the -----BEGIN XXX----- and so forth). It crashes with bad access in RSA_size.

It doesn't matter if I remove the BIO_free.

Any ideas? Thanks!

Upvotes: 2

Views: 1762

Answers (2)

Satachito
Satachito

Reputation: 5888

Try PEM_read_bio_RSAPublicKey instead of PEM_read_bio_RSA_PUBKEY.

Upvotes: 1

President James K. Polk
President James K. Polk

Reputation: 41974

You need to check the return value of PEM_read_bio_RSA_PUBKEY() to make sure it is non-null. Most likely the contents of _publicKey are not quite a correctly formatted key and as a consequence pubKey is NULL.

Upvotes: 3

Related Questions