darkheir
darkheir

Reputation: 8950

OpenSSL, reading public RSA key from CString

I'm trying to read an RSA public key from a CString. Before I was doing it from a file and it was simple, I was using:

RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x, pem_password_cb *cb, void *u);

but now I can't use this method. This is why I thought about using:

RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x, pem_password_cb *cb, void *u);

But I don't understand the BIO *bp argument and how to pass a CString to it!

Upvotes: 2

Views: 2888

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74008

From bio(3) documentation

A BIO is an I/O abstraction, it hides many of the underlying I/O details from an application. If an application uses a BIO for its I/O it can transparently handle SSL connections, unencrypted network connections and file I/O.

And for your case, I would guess BIO_s_mem(3) is the proper type

A memory BIO is a source/sink BIO which uses memory for its I/O.

and

BIO *BIO_new_mem_buf(void *buf, int len);
...
BIO_new_mem_buf() creates a memory BIO using len bytes of data at buf, if len is -1 then the buf is assumed to be null terminated and its length is determined by strlen. The BIO is set to a read only state and as a result cannot be written to.

So with a C string, I would say this leads to

char rsa_key[1024];
...
BIO *bp = BIO_new_mem_buf(rsa_key, -1);
RSA *rsa = PEM_read_bio_RSA_PUBKEY(bp, ...);

Upvotes: 1

Gianluca Ghettini
Gianluca Ghettini

Reputation: 11618

A BIO in OpenSSL is similar to a File handle. You use a pair of them to communicate with each other securely like you would with two sockets.

here a detailed explanation with some example code

Upvotes: 0

Related Questions