Configuring the OpenSSL PKCS#11 Provider
To use the OpenSSL pkcs11-provider, you must configure OpenSSL to recognize and load the provider,
and you must configure the provider to recognize and load the Primus PKCS#11 Provider.
Both of these steps are done via an OpenSSL config file.
When OpenSSL is invoked, you have three options to provide a config file:
- Global default
openssl.cnffile. This works best. - CLI argument (
-config openssl.cnf). However, this is not supported by all OpenSSL subcommands. - Environment variable (
export OPENSSL_CONF=/path/to/openssl.cnf).
We recommend to write your settings to a separate primus.cnf file
and include/import this file in the global openssl.cnf.
This allows you to rely on all of the global defaults, while also
keeping your Primus-related settings separate, and avoids polluting the global config.
Find the OpenSSL Configuration Directory
Locate where OpenSSL and its default configuration file is installed:
openssl version -d
OPENSSLDIR: "/etc/pki/tls"
Common values for this directory are:
- Debian/Ubuntu:
/usr/lib/ssl- However,
/usr/lib/ssl/openssl.cnfis a symlink to/etc/ssl/openssl.cnf. Therefore, keep your config files in/etc/ssl.
- However,
- RHEL:
/etc/pki/tls - Windows:
C:\Program Files\Common Files\SSL
Check that this directory contains the global openssl.cnf:
- Linux
- Windows
ls -la /usr/lib/ssl
dir "C:\Program Files\Common Files\SSL"
Create the Configuration File
Create a new configuration file primus.cnf, either as a blank new file
or by copying the existing default config (/etc/ssl/openssl.cnf).
Add or append the following content to your config file:
HOME = .
openssl_conf = openssl_init
config_diagnostics = 1
[openssl_init]
providers = provider_sect
# random = random_sect
# Use the HSM as a source of randomness
# Warning: this can have unexpected side-effects. Only enable if required.
# [random_sect]
# random = PKCS11-RAND
[provider_sect]
default = default_sect
base = base_sect
pkcs11 = pkcs11_sect
[base_sect]
activate = 1
[default_sect]
activate = 1
[pkcs11_sect]
module = /usr/lib/x86_64-linux-gnu/ossl-modules/pkcs11.so
pkcs11-module-path = /usr/local/primus/lib/libprimusP11.so
# pkcs11-module-token-pin = PRIMUSDEV
pkcs11-module-encode-provider-uri-to-pem = true
pkcs11-module-load-behavior = early
pkcs11-module-quirks = no-deinit no-operation-state
activate = 1
The base-provider and default-provider need to be enabled explicitly. Otherwise the encoding function as well as the majority of algorithms implemented by OpenSSL will not be accessible.
We recommend to create a separate OpenSSL config file for the PKCS#11 integration. Enabling the pkcs11-provider in your global OpenSSL config file can have unintended side effects. In particular, if the PKCS#11 provider fails to load, it can block OpenSSL, which in turn can block applications that need OpenSSL but not PKCS#11. Since OpenSSL is fundamental to many Linux systems, this can cause stability issues.
pkcs11-provider options
The configuration of the OpenSSL pkcs11-provider is placed in [pkcs11_sect].
Additionally, the pkcs11_sect section needs to be listed in the [provider_sect].
-
moduleand thepkcs11-module-pathoptions are mandatory. They have to point to the installed pkcs11.so file and the libprimusP11.so file you installed when setting up the HSM's PKCS#11 interface. -
pkcs11-module-token-pincan be used to point to a file containing the PKCS#11 PIN. You can leave this empty, and instead specify it in the application calling OpenSSL in the PKCS#11 URI. -
pkcs11-module-encode-provider-uri-to-pem = trueis used to register a PEM encoder for private keys. It encodes private keys in PEM files that contain a PKCS#11 URI referencing the key. The PEM files use the non-standardPKCS#11 PROVIDER URIheader/trailer. This encoder is necessary for non-extractable keys, as otherwise OpenSSL will fail to export such keys (for example, duringopenssl genpkey). It is also necessary for legacy applications that require keys to be passed as PEM files instead of as PKCS#11 URIs. -
pkcs11-module-load-behaviorchanges the behavior at application startup. By setting it toearly, the pkcs11-provider immediately initializes the PKCS#11 module when the pkcs11-provider is loaded. Without this option, module loading (and the connection to the HSM) is deferred until a PKCS#11 object is explicitly requested. Theearlyload behavior is needed when working with OpenSSL on the command line. -
pkcs11-module-quirksare used to inform the pkcs11-provider about certain properties of the token that might be considered unusual.- The option
no-deinitprevents the de-initialization of the Primus pkcs11-provider. As part of the exit routine the OpenSSL command line interface tries to do a clean up. However, the deallocation on exit is done in reverse order of allocation, which means that the C++ objects in the Primus pkcs11 provider are already freed. Applications that callOPENSSL_cleanupbefore the exiting do not need that flag. Applications that re-load the OpenSSL library should not use this flag to avoid memory leaks. - The HSM does not support getting and setting the operation
state. Thus, the context cannot be duplicated during digest and sign
operations. By setting
no-operation-state, the duplication is not performed.
- The option
The full range of configuration options can be found in the provider's HOWTO.md and its man page.
Reference the Config File
Point your applications to use your custom config file. The easiest way is by setting the environment variable:
export OPENSSL_CONF=/etc/ssl/primus.cnf
Test the Provider Integration
The PKCS#11 provider should now be listed:
openssl list -providers
Providers:
default
name: OpenSSL Default Provider
version: 3.0.13
status: active
pkcs11
name: PKCS#11 Provider
version: 3.0.13
status: active
You can use the OpenSSL storeutl command to show a public key stored on your partition:
openssl storeutl "pkcs11:token=${PARTITION_NAME};type=public;object=${KEY_LABEL}"
For more details on how to debug configuration issues, see the Troubleshooting page.