0

I'm using openssl library and I want to read a public key from a .pem file with BIO. I tried this, but my rsa variable remains uninitialized :

 RSA *rsa = RSA_new();

 BIO *keybio = NULL;

 keybio = BIO_new(BIO_s_file());
 BIO_read_filename(keybio, "public.pem");

 // and also tried this instead of last two lines:
 // keybio = BIO_new_file("public.rem", "r");

 rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);

When I debug my application it shows me something like this:

rsa { padding = ???, n = ??? , ...} 
rsa->n <unable to read from memory> and so on for all rsa fields.

My file is valid and the key is generated respecting PKCS#1 format. I parsed it with an asn1 parser.

Radu Mardari
  • 131
  • 3
  • 6
  • Possible duplicate of [Reading and writing rsa keys to a pem file in C](http://stackoverflow.com/questions/12647220/reading-and-writing-rsa-keys-to-a-pem-file-in-c) – jww May 13 '15 at 22:25
  • I think the obvious thing is to ensure you have symbol paths set correctly so the debugger can locate symbols for your program and the library. I also *think* a `RSA*` is really a `rsa_st*`. You might try casting it first. – jww May 13 '15 at 22:28
  • @jww That's about reading a private key. Please *thoroughly* check the other question/answers before marking something as a dupe. – Maarten Bodewes May 14 '15 at 08:50
  • Are you sure that your public key is in PEM form, including the header files and the base64 encoding? – Maarten Bodewes May 14 '15 at 08:51
  • @Maarten - Thanks for the advice. If you look, you will see there are no close votes cast on the question by me. The possible duplicate was cited to provide cross referencing because there's little difference between reading a public key and a private key. See, for example, [Linking Duplicate Questions](http://blog.stackoverflow.com/2009/page/20/) on the Stack Exchange blog. There's a even better reference in one of the blogs. It has to do with providing a "flag in the sand". But I can't find it at the moment. Thanks again for your advice. – jww May 14 '15 at 08:58
  • @jww I agree that the methods are quite the same but the underlying format - which is probably the issue - could differ significantly. I'm just hesitant to go for a dupe if the underlying issue could lead to a different solution. But as you said, you didn't chose to close. Dear user2991856, please copy the public key into the question. – Maarten Bodewes May 14 '15 at 15:56

1 Answers1

0

Your code looks fine. Try this input:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1IHYYTavz9PQRxpcyO7J
m0dtiIjNUvW3coLQQKhq+wySTTN1cwm2zYTw0FSfLDPOtOBNXFwKF9wykiyHS2uU
D8vIU+T/fvlcADYTtZqdC5AoBWlSuhp0xqqtHmNUEjGa4FpRmKusL8s5/cuAfNRV
NVSxA3JCN3kYrT9Q1qBN+XbOQn+h7gPQU3ICmG7L1R/CwIsq/wwUbq+NeY0TMvz5
LM6AIS+GCV0UeJVm6UN6GDBCOHk02XuplyhkbCsNhq+HTfhHVeE1s7NcIavmgvqm
EtlIcTGemW9tXs5/REZUv+SDpR6RLUKhwuij/Ft5Pe9b7cH3wXqNmOBhJ3F/ht2C
swIDAQAB
-----END PUBLIC KEY-----

Compare this to what you see with your asn1 parser:

$ openssl asn1parse -in public.pem
    0:d=0  hl=4 l= 290 cons: SEQUENCE          
    4:d=1  hl=2 l=  13 cons: SEQUENCE          
    6:d=2  hl=2 l=   9 prim: OBJECT            :rsaEncryption
   17:d=2  hl=2 l=   0 prim: NULL              
   19:d=1  hl=4 l= 271 prim: BIT STRING        
Jim Flood
  • 8,144
  • 3
  • 36
  • 48
  • His code is reading an RSA public key out of a public key PEM file. It should work as written, as long as the PEM file is valid. For example, this code won't work if the PEM file is an X509 certificate file, even though it contains a public key. – Jim Flood May 14 '15 at 00:24