1

I'd like to print the content of an RSA key created using OpenSSL and accessed by means of the pointer

RSA * rsa;

Now, I've tried to use the RSA_print and BIO_read functions as follows:

BIO * keybio ;
RSA_print(keybio, rsa, 0);
char buffer [1024];
std::string res = "";
while (BIO_read (keybio, buffer, 1023) > 0)
{
    std::cout << buffer;
}

but I get a segmentation fault at the very first execution of the BIO_read (sixth line). Can someone spot the error?

jww
  • 97,681
  • 90
  • 411
  • 885
gosbi
  • 755
  • 1
  • 6
  • 13
  • Um does `BIO * keybio = malloc(sizeof(Bio));` help? I don't think `RSA_print()` allocates memory for `keybio`, and you are writing over an unallocated memory space. If it does I'll make it an answer. – luk32 May 05 '14 at 08:13
  • possible duplicate of [How to properly print RSA\* as string in C++?](http://stackoverflow.com/questions/10451936/how-to-properly-print-rsa-as-string-in-c) – jww May 05 '14 at 12:10
  • Use `RSA_print` or `RSA_print_fp`. See the docs at [`RSA_print(3)`](http://www.openssl.org/docs/crypto/RSA_print.html). Allow the key to be written to the `BIO`, and then read the data from the `BIO` to a `string`. – jww May 05 '14 at 12:13
  • 1
    Don't waste effort in trying to make that loop work. `std::cout << buffer` is broken because `buffer` is not `NULL` terminated. Use `BIO_get_mem_ptr` to access the raw memory from the `BIO`. – jww May 05 '14 at 13:02
  • 1
    "... I get a segmentation fault at the very first execution of the BIO_read" - the `BIO` does not appear to be initialized. You seem to be missing a `BIO_new(BIO_s_mem())`. – jww May 05 '14 at 13:59
  • @gosbi - Please provide an answer in an answer block. Readers know to look in answer blocks for answers. Its OK if your answer your own question. Also see [How does accepting an answer work?](http://meta.stackexchange.com/q/5234) on Meta. – jww Jan 06 '17 at 21:37

1 Answers1

3

I think you are writing over unallocated memory. I don't think RSA_print() allocates memory for BIO, so you are passing an uninitialized pointer to it.

EDIT: As @jww pointed out in the comment, the BIO object should be instantiated with a set of dedicated options. I edited the answer accordingly. I also changed the buffer size passed to 1024.

Allocate space:

BIO * keybio = BIO_new(BIO_s_mem());
RSA_print(keybio, rsa, 0);
char buffer [1024];
std::string res = "";
while (BIO_read (keybio, buffer, 1024) > 0)
{
    std::cout << buffer;
}
BIO_free(keybio); //appropriate free "method"

BIO_s_mem() creates a memory BIO method function. And BIO_new() creates and initializes the object itself, using the supplied method.

Also please note that this code is c-style. There is a nice way of wrapping pure pointer into c++ smart pointers, shown here: How to properly print RSA* as string in C++?.

luk32
  • 15,812
  • 38
  • 62
  • Thank you for your answer. I think you meant `sizeof(BIO)`. Anyway, both solutions compile but none of them solved the issue. I still have segmentation fault. On the other hand, I think you are right about the dimension of the buffer. – gosbi May 05 '14 at 09:18
  • 1
    You are right. It should be `BIO` of course. Does the `segfault` occur on the same line? I do not really see any other reason for it to fail like this. I would also suggest checking `RSA_print` result for error. However, even if it does fail, I don't think a subsequent call to `BIO_read` should segfault. It is worth of checking though. – luk32 May 05 '14 at 09:29
  • 1
    I believe you need `BIO* keybio = BIO_new(BIO_s_mem());`. Its not clear to me where any initialization is occurring. At minimum: `BIO keybio = *BIO_new(BIO_s_mem());` – jww May 05 '14 at 13:55
  • @jww I'll update my answer as soon as this is confirmed. Or you can write your own, and I will delete mine as incorrect then. I left it as a hint in case someone else would like to try it. Since there is no other answers I think it is not that offending as of yet. – luk32 May 05 '14 at 14:10
  • @jww Thanks for link. Care to write your own answer, or want me to edit and get credited for your research work =) ? – luk32 May 05 '14 at 14:19