首页 文章

OpenSSL RSA库抛出异常

提问于
浏览
0

我想将RSA加密包含在使用 Visual Studio 2017 用C编写的 DLL 项目中,我认为OpenSSL是完美的 . 我在 Additional Include DirectoriesAdditional Library Directiories 中添加了正确的路径到我的OpenSSL文件夹,我已将 libcrypto.lib/libeay32.lib 添加到 Additional Dependencies . 该项目编译,但当我想使用 rsa.h 中的一些函数,如 RSA_size ,我收到异常,如:

Exception thrown at 0x0F17EB64 (libcrypto-1_1.dll) in Win32Project1.exe: 0xC0000005: Access violation reading location 0x00000004.

我找不到原因了 . This告诉我用常规的exe应用程序尝试这个库,但即使用最简单的代码也会出现同样的错误

#include <openssl/rsa.h>
int main()
{
    RSA *m_rsa = RSA_new();
    RSA_size(m_rsa);
    return 0;
}

我试图在版本 1.1.01.0.2 中实现OpenSSL,两者都有相同的结果 .

RSA_new正确分配新的RSA实例,ERR_get_error()返回“0” .

1 回答

  • 1

    看看图书馆文档:

    如果分配失败,RSA_new()将返回NULL并设置可由ERR_get_error(3)获取的错误代码 . 否则,它返回指向新分配结构的指针 .

    RSA_new可能无法返回空指针 . 使用 RSA_size() 将使用此空指针(地址0)来访问RSA结构的字段 . 如果此字段位于偏移量4处,您将尝试访问地址4(基址0偏移4),您将获得"Access violation reading location 0x00000004" .

    所以,检查返回的指针 . 如果为null,请检查错误代码以查看发生的情况...

    图书馆链接:

    编辑:

    另一件需要知道的是 RSA->n 在调用 RSA_size()see documentation)时不应为null . n 是模数,因此在生成或设置密钥之前,不能调用 RSA_size() .

    看这个例子(permalink here):

    #include <openssl/rsa.h>
    int main ()
    {
        RSA* rsa = RSA_new ();
        std::cout << "rsa pointer = " << rsa << std::endl;
        std::cout << "rsa->n = " << rsa->n << std::endl;
        RSA_size ( rsa ); // Seg fault.
    
        return EXIT_SUCCESS;
    }
    

    输出:

    rsa pointer = 0x12d9c20
    rsa->n = 0
    bash: line 7: 10149 Segmentation fault      (core dumped) ./a.out
    

相关问题