简体   繁体   中英

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!

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, ...);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM