c - OpenSSL 1_1_0e BN_print_fp not working -
i using openssl 1_1_0e , cannot figure out what's wrong code:
#include <openssl/rsa.h> int main(){ bignum *bne = null; unsigned long e = rsa_f4; rsa *r = null; bne = bn_new(); bn_set_word(bne,e); r = rsa_new(); bignum *n = null; bignum *d = null; rsa_get0_key((const rsa *) r, (const bignum **) &n, null, (const bignum **) &d); bn_print_fp(stdout, n); rsa_free(r); bn_free(bne); return 0; }
valgrind says there invalid read of size 4:
==8066== invalid read of size 4 ==8066== @ 0x4ef603e: bn_print (in /home/roman/dropbox/uni/rsa/my_work/library/lib/libcrypto.so.1.1) ==8066== 0x4ef662d: bn_print_fp (in /home/roman/dropbox/uni/rsa/my_work/library/lib/libcrypto.so.1.1) ==8066== 0x40093b: main (in /home/roman/dropbox/uni/rsa/my_work/sharedlibrarytest) ==8066== address 0x10 not stack'd, malloc'd or (recently) free'd
what wrong code? looks fine me.
i cannot test piece of code directly because don't have rsa_get0_key
openssl documentation says that
the
n
,e
,d
parameters can obtained calling rsa_get0_key(). if have not been set yet,*n
,*e
,*d
set null. otherwise, set pointers respective values. these point directly internal representations of values , therefore should not freed caller.
you're calling rsa_new();
there nothing setting these bignum's in rsa object - far can read rsa_new()
doesn't - how it, because generating them takes loooong time. n
set null
pointer; , error comes when bn_print_fp
attempts read member of bignum
structure @ offset 16. (i.e. *(uint32_t*)((char*)null + 16)
)
minimal example:
#include <openssl/rsa.h> int main(void) { bn_print_fp(stdout, null); }
compile gcc test.c -lssl -lcrypto
, use valgrind
:
% valgrind ./a.out [...] ==16062== invalid read of size 4 ==16062== @ 0x4efa5f4: bn_print (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==16062== 0x4efa743: bn_print_fp (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==16062== 0x108745: main (in /home/user/tmp/a.out) ==16062== address 0x10 not stack'd, malloc'd or (recently) free'd [...]
Comments
Post a Comment