dll - PKCS11 Windows 无效引擎 YubiHSM2

标签 dll openssl pkcs#11 opensc yubico

又是我。 我正在使用 YubiHSM2 HSM 模块,并尝试将其设置为使用 pkcs11 引擎,这将允许我将 OpenSSL 与 HSM 一起使用。

我正在Windows上实现这个,这给我带来了很多麻烦。 我已经安装了 OpenSSL 32,64、OpenSC、YubiHSM2 驱动程序以及 libp11(使用 MSYS2 构建)。

我的 OpenSSL.cnf 中有趣的部分如下所示:

openssl_conf = openssl_init

[openssl_init]
engines = engine_section

[engine_section]
pkcs11 = pkcs11_section

[pkcs11_section]
engine_id = pkcs11
dynamic_path = "C:\Windows\System32\opensc-pkcs11.dll"
MODULE_path = "C:\Users\myUser\Desktop\SecureTemial\yubihsm2-sdk\bin\yubihsm_pkcs11.dll"
PIN = "0001password"
init = 0

当我尝试时:

 C:\OpenSSL-Win64\bin\openssl.exe req -new -x509 -days 365 -sha256 -config C:\Users\myUser\Desktop\SecureTemial\openssl.cnf -engine pkcs11 -keyform engine -key slot_0-label_my_key -out cert.pem

我收到以下信息:

C:\OpenSSL-Win64\bin\openssl.exe : invalid engine "pkcs11"
In Zeile:1 Zeichen:2
+  C:\OpenSSL-Win64\bin\openssl.exe req -new -x509 -days 365 -sha256 -c ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (invalid engine "pkcs11":String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

16056:error:25078067:DSO support routines:win32_load:could not load the shared 
library:crypto\dso\dso_win32.c:106:filename(C:\Program Files\OpenSSL\lib\engines-1_1\pkcs11.dll)
16056:error:25070067:DSO support routines:DSO_load:could not load the shared library:crypto\dso\dso_lib.c:161:
16056:error:260B6084:engine routines:dynamic_load:dso not found:crypto\engine\eng_dyn.c:414:
16056:error:2606A074:engine routines:ENGINE_by_id:no such engine:crypto\engine\eng_list.c:339:id=pkcs11
16056:error:25078067:DSO support routines:win32_load:could not load the shared 
library:crypto\dso\dso_win32.c:106:filename(pkcs11.dll)
16056:error:25070067:DSO support routines:DSO_load:could not load the shared library:crypto\dso\dso_lib.c:161:
16056:error:260B6084:engine routines:dynamic_load:dso not found:crypto\engine\eng_dyn.c:414:
Error configuring OpenSSL modules
16056:error:25078067:DSO support routines:win32_load:could not load the shared 
library:crypto\dso\dso_win32.c:106:filename(C:WindowsSystem32opensc-pkcs11.dll)
16056:error:25070067:DSO support routines:DSO_load:could not load the shared library:crypto\dso\dso_lib.c:161:
16056:error:260B6084:engine routines:dynamic_load:dso not found:crypto\engine\eng_dyn.c:414:
16056:error:260BC066:engine routines:int_engine_configure:engine configuration 
error:crypto\engine\eng_cnf.c:141:section=pkcs11_section, name=dynamic_path, value=C:WindowsSystem32opensc-pkcs11.dll
16056:error:0E07606D:configuration file routines:module_run:module initialization 
error:crypto\conf\conf_mod.c:173:module=engines, value=engine_section, retcode=-1   

我已经检查过 dll 是否被锁定并以管理员身份运行等。 如果您知道造成此问题的原因,请告诉我!

非常感谢!

最佳答案

这个问题是我在对类似主题进行一些研究时首先出现在搜索结果中的问题之一。由于还没有答案,我将概述我的解决方案的结果:

使用libp11的 PKCS#11 引擎与 OpenSSL,它必须编译为动态引擎,与您正在使用的 OpenSSL 版本静态链接。当您使用binaries from Shining Light Productions时(根据您在问题中提到的安装目录进行的一个很好的猜测),使用从第三方资源获取的 MSYS2 版本可能不起作用,使用 OpenSC 项目 Windows installers 附带的 PKCS#11 库也不起作用。 .

幸运的是,Shining Light Productions 的 OpenSSL 版本附带了所有必需的库,因此您可以轻松地自己编译 libp11,例如通过使用 NMAKE (点击链接查看如何获取它以及如何正确设置命令行以供其使用):

  1. 下载OpenSSL binaries满足您的要求(x86 或 x64)并将它们安装到建议的标准目标(例如,C:\OpenSSL-Win32C:\OpenSSL-Win64)。 - libp11 的 makefile 需要这些文件夹来进行绑定(bind)。
  2. 下载并解压或克隆 libp11项目的源代码。
  3. 打开 Windows 命令行并设置 NMAKE 环境变量,然后更改为之前下载的 libp11 文件的位置。
  4. 针对 64 位版本的 OpenSSL 进行构建时,您必须相应地设置 BUILD_FOR 环境变量。运行

    set BUILD_FOR=WIN64
    

    在命令行上。

  5. 现在通过运行来编译库

    NMAKE /F Makefile.mak
    
  6. 如果一切顺利,libp11 的 src 文件夹中就会有两个新库:libp11.dllpkcs11.dll 。后者是与 OpenSSL 一起使用的 PKCS#11 引擎。将其复制到 Windows 库文件夹(对于 32 位版本,System32;对于 x64 版本,SysWOW64)。

  7. 相应地调整您的 openssl.cnf 文件。复制

    openssl_conf = openssl_init
    

    到文件的开头,其余的到文件的结尾:

    [openssl_init]
    engines = engine_section
    
    [engine_section]
    pkcs11 = pkcs11_section
    
    [pkcs11_section]
    dynamic_path = "C:\\Windows\\SysWOW64\\pkcs11.dll"
    module_path = "C:\\Users\\myUser\\Desktop\\SecureTemial\\yubihsm2-sdk\\bin\\yubihsm_pkcs11.dll"
    PIN = "0001password"
    

最后一些注意事项:

  1. 确保 OpenSSL 确实选取了改编的 openssl.cnf 文件。 OpenSSL 安装附带了几个示例文件。默认情况下,上述二进制文件的配置文件位置为 C:\Program Files\Common Files\SSL\openssl.cnf(对于 x64 版本)和 C:\Program Files (x86) x86 版本的\Common Files\SSL\openssl.cnf。但系统上的其他 OpenSSL 安装(例如,来自 OpenVPN、MingW、MSYS2 等捆绑 OpenSSL 的设备)可能会干扰设置文件位置。您可以通过相应地设置 OPENSSL_CONF 环境变量来确保使用正确的设置文件。
  2. 在 Windows 路径中使用双引号时,请确保使用 \\ 而不是 \ 正确转义反斜杠。
  3. 您可以安全地省略 openssl.cnf[pkcs11_section]engine_idinit 部分.
  4. 虽然 libp11 的动态 PKCS#11 引擎需要针对与 OpenSSL 相同的架构(x86 或 x64)和库进行编译,但模块库可能需要 32 位版本(即使运行 OpenSSL 的 64 位版本时)。 - 至少这是我们系统场景中发生的情况(我们使用 Gemalto Safenet 电子 token ,因此使用 Safenet 身份验证客户端附带的 Aladdin 模块库)。

关于dll - PKCS11 Windows 无效引擎 YubiHSM2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49214347/

相关文章:

c++ - 使用 libp11 和 pkcs11-engine 读取私钥的区别

c++ - 如何在cryptoki中重命名容器名称

c++ - 当加载链接到使用空 DllMain 的 DLL 时,应用程序无法启动 (0xC0000142)

c++ - 未找到包含 <QtCore/qglobal.h>

java - 从 Java 使用 C# 编写的 DLL

openssl - 如何从命令行使用密码生成 openSSL key ?

java - IAIK PKCS11 Wrapper 中的私钥和证书对如何匹配?

visual-studio-2010 - 从 C :\Windows\SysWOW64 instead of PATH value 加载的 dll (libeay32)

c - PKCS5_PBKDF2_HMAC : binary password

SslStream<TcpStream> 读取不返回客户端的消息