I am working with OpenSSL inside a Perl-XS-Module. I have a C-function that calls OpenSSL-API-functions. It is very simple for test-purposes (init openssl, read a key, make the RSA-object and use it, no arguments). No big checks needed and addresses and memory are ok.
XS is standard h2xs with
void _foo (void)
unsigned char key ;
memset (key, 0, 3000);
printf ("\ninit=%d", SSL_library_init ()); // init
FILE *f = fopen ("key.key","r");
printf ("\nf=%d", f);
int keysize = fread (key, 1, 3000, f); // readin
printf ("\nkey=%s", key);
BIO *bio = BIO_new_mem_buf (key, keysize);
printf ("\nbio=%ld", bio);
RSA *pk = (RSA *) PEM_read_bio_RSAPrivateKey (bio, NULL, NULL, NULL);
printf ("\npk=%ld", pk);
printf ("\nsz=%d" ,RSA_size(pk)); // ***** crash here if in a perl-thread
I found out the answer. I am not sure about it and I do not like it but it seems to be legit.
I have a lot of openssl-includes in my xs. But I did not include ssl.h.
No compile-error, no linker-error. Only a segmentation-fault if I use it in threads. Works even outside threads.
If I include the h-file it works all fine. Even without the added openssl-mutex-thread-handling.
Found out after I tried to make my sample "pretty" to post it here. That is a bit weird.... I would prefer an compiler-error...
But it explains why the other module is working.
Thanks for help!!