阿帕奇

您可以配置`Apache httpd<https://httpd.apache.org/>`__,通过 OpenSSL 引擎使用 NetHSM,然后使用 NetHSM 的 PKCS#11 模块。

证书文件必须在磁盘上,但私钥可通过 NetHSM 使用。

完整的示例见`以下<#example>`__。

OpenSSL 配置

按照`OpenSSL 引擎设置指南<openssl.html#engine>`__ 设置 OpenSSL 引擎(Apache httpd 尚不支持 OpenSSL Providers)。

Httpd 配置

在``httpd.conf`` 中添加以下行:

Listen 443
#...
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so
#...

<VirtualHost *:443>
    DocumentRoot /usr/local/apache2/htdocs
    SSLEngine on
    SSLCertificateFile /certs/certificate.pem
    SSLCertificateKeyFile "pkcs11:object=webserver"
    ErrorLog /tmp/a-error.log
    CustomLog /tmp/a-access.log combined
</VirtualHost>

The SSLCertificateFile must point to a certificate file on the disk. The SSLCertificateKeyFile should be a PKCS#11 URI pointing to the private key in the NetHSM.

备注

你必须单独生成证书,然后上传到 NetHSM。如果磁盘上的证书与 NetHSM 中的密钥不匹配,httpd 将无法启动。

libnethsm_pkcs11 配置

slots:
  - label: LocalHSM
    description: Local HSM (docker)
    url: "https://192.168.3.161:8443/api/v1"
    operator:
      username: "operator"
      password: "opPassphrase"

To secure the password you can provide it via an environment variable (see Passwords) or provide it in the httpd configuration:

SSLCertificateKeyFile "pkcs11:object=webserver;type=private;pin=opPassphrase";

例子

如果您想使用`示例<https://github.com/Nitrokey/nethsm-pkcs11/tree/main/container/apache>`__进行实验,请使用 git 克隆`nethsm-pkcs11 仓库<https://github.com/Nitrokey/nethsm-pkcs11>`__,并运行以下命令:

警告

运行生成脚本会删除``webserver`` 密钥并将其替换。

  1. Configure a NetHSM, either a real one or a container. See the getting-started guide for more information. Besides an administrator, you are going to need an operator account.

  2. 下载并安装最新版本的 nethsm-pkcs11 驱动程序`,可从此处获取<https://github.com/Nitrokey/nethsm-pkcs11/releases>`__。

  3. 按照`OpenSSL Manual<openssl.html>`__ 中的说明安装 OpenSSL PKCS11 引擎。无需创建配置文件。

  4. 调整``container/apache/generate.sh`` 中的变量``HOST``、ADMIN_ACCOUNT 和``ADMIN_ACCOUNT_PWD``,使``HOST`` 中包含 NetHSM 的 URL 和端口,ADMIN_ACCOUNT 中包含管理员账户的用户名,ADMIN_ACCOUNT_PWD 中包含相应的密码。进一步在``OPENSSL_PKCS11_ENGINE_PATH`` 中配置 OpenSSL PKCS11 引擎的绝对路径,在``NETHSM_PKCS11_LIBRARY_PATH`` 中配置 NetHSM PKCS11 库的绝对路径。

  5. Create a NetHSM PKCS11 configuration file in one of the known locations, e.g., /etc/nitrokey/p11nethsm.conf. It must have configured an operator account and use the same NetHSM instance specified in the generate script before.

  6. 在``container/apache/p11nethsm.conf`` 中用 NetHSMs URL 和有效的操作员凭证更新 PKCS11 配置。

  7. 生成证书和密钥

    ./container/apache/generate.sh
    
  8. 建造集装箱

    docker build -f container/apache/Dockerfile . -t pkcs-httpd
    
  9. 运行容器。

    docker run -p 9443:443 -p 9080:80 pkcs-httpd
    

Apache 测试页面可在`https://localhost:9443/<https://localhost:9443/>`___中找到。请注意,希望您的浏览器会警告您网站证书是自签名的。