Skip to content

Commit

Permalink
feat(espsecure): Allow prompting for HSM PIN in read_hsm_config
Browse files Browse the repository at this point in the history
If hsm_config does not contain "credentials" the user will be
prompted for the HSM PIN.

This avoids the need to have HSM PINs typed in config files
which is not a good security practice.

ADJUNCT: Updated documentation to reflect new usage

Closes #900
  • Loading branch information
rretanubun authored and radimkarnis committed Jul 25, 2023
1 parent 2bea6f4 commit ab25fc1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/en/espsecure/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ HSM config file
An HSM config file is required with the fields (``pkcs11_lib``, ``credentials``, ``slot``, ``label``, ``label_pubkey``)
populated corresponding to the HSM used.

To access an HSM token of a selected slot, you will also need to pass in the token User PIN and thus you will be prompted to type in the User PIN.
Alternatively, you could also add a ``credentials`` field in the HSM config file to store the (plaintext) User PIN to automate the signing workflow.

Below is a sample HSM config file (``hsm_config.ini``) for using `SoftHSMv2 <https://github.com/opendnssec/SoftHSMv2>`_ as an external HSM: ::

# hsm_config.ini
Expand Down
9 changes: 8 additions & 1 deletion espsecure/esp_hsm_sign/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import configparser
import os
import sys
from getpass import getpass

try:
import pkcs11
Expand All @@ -31,11 +32,17 @@ def read_hsm_config(configfile):
if not config.has_section(section):
raise configparser.NoSectionError(section)

section_options = ["pkcs11_lib", "credentials", "slot", "label"]
section_options = ["pkcs11_lib", "slot", "label"]
for option in section_options:
if not config.has_option(section, option):
raise configparser.NoOptionError(option, section)

# If the config file does not contain the "credentials" option,
# prompt the user for the HSM PIN
if not config.has_option(section, "credentials"):
hsm_pin = getpass("Please enter the PIN of your HSM:\n")
config.set(section, "credentials", hsm_pin)

return config[section]


Expand Down

0 comments on commit ab25fc1

Please sign in to comment.