gpt4 book ai didi

c - Perl XS 中的 OpenSSL 和线程

转载 作者:太空宇宙 更新时间:2023-11-04 03:28:42 25 4
gpt4 key购买 nike

我在 Perl-XS-Module 中使用 OpenSSL。我有一个调用 OpenSSL-API 函数的 C 函数。它对于测试目的非常简单(初始化 openssl,读取 key ,创建 RSA 对象并使用它,没有参数)。无需大检查,地址和内存都可以。

XS 是标准的 h2xs,在 Makefile 中带有 -lssl-lcrypto

void _foo (void)
{
unsigned char key [3000];
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 ("\nn=%d",keysize);
fclose (f);
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
printf ("\n\n");
}

如果我使用纯 C/C++,那是可行的。如果我将它放在 XS-Module 中并在线程外的 Perl 中使用它也可以。但如果我在 Perl 中和线程中使用它,它就会崩溃。

现在我会说那是因为我没有任何 thread-handling .但是如果我查看其他 Perl 模块(例如 Crypt::OpenSSL::RSA),我也没有找到特殊的线程处理。

我是 XS 的新手,也许我错过了什么。也许有人可以给我提示,谢谢!

最佳答案

printf ("\ninit=%d", SSL_library_init ());   // init

来自 SSL_library_init 的文档:

SSL_library_init() must be called before any other action takes place. SSL_library_init() is not reentrant.

这意味着您最好在线程启动之前调用一次 SSL_library_init 或至少确保您有适当的锁定并且该函数不会被多次调用,包括不会从其他模块调用你可能会用。类似 Net::SSLeay 的模块实际处理这个as can be seen in the source code .

关于c - Perl XS 中的 OpenSSL 和线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39134329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com