Skip to main content

Python async library for signing x509 using keys in a pkcs11 device such as an HSM.

Project description

workflow ubuntu workflow centos workflow debian

python_x509_pkcs11

Seamless async signing x509 using PKCS11 device for key storage

Currently supports

  • Creating root CAs and generating their keys in the PKCS11 device.
  • Using the keys in the PKCS11 device to sign certificates or Intermediate CAs.
  • Creating certificates, CSRs, CRLs, OCSPs with the PKCS11 device keys enabling a full PKI infrastructure.
  • 'Advanced' handling of fragile persistent PKCS11 sessions, including recreating the session if PKCS11 operation timeout.
  • This package is heavily uses python-pkcs11 and asn1crypto.
  • Package is async but python-pkcs11 is unfortunately still sync, probably due to the fragile nature of PKCS11.
  • Tested with SoftHSM and LUNAHSM.

Setup

# Install libs and add your user to the softhsm group
# You should probably replace softhsm when using this in production, any PKCS11 device should work

if awk -F= '/^NAME/{print $2}' /etc/os-release | grep -i "debian\|ubuntu"
then
    # Ubuntu / Debian
    sudo apt-get install python3-dev python3-pip softhsm2
    sudo usermod -a -G softhsm $USER
else
    # Redhat / Centos / Fedora
    sudo dnf install python3-devel python3-pip softhsm gcc 
    sudo usermod -a -G ods $USER
fi

# Update your softhsm group membership
exec sudo su -l $USER

# Install this package
pip3 install python_x509_pkcs11

# export env values the code will use
if awk -F= '/^NAME/{print $2}' /etc/os-release | grep -i "debian\|ubuntu"
then
    export PKCS11_MODULE="/usr/lib/softhsm/libsofthsm2.so"
else
    export PKCS11_MODULE="/usr/lib64/softhsm/libsofthsm.so"
fi
export PKCS11_PIN="1234"
export PKCS11_TOKEN="my_test_token_1"

# Initialize the token
softhsm2-util --init-token --slot 0 --label $PKCS11_TOKEN --pin $PKCS11_PIN --so-pin $PKCS11_PIN

Usage

Look at the documentation for quick examples to begin.

The tests are also a good starting point

Here is the basic, create a root CA and then use its key in the PKCS11 device to sign a csr:

# export env values the code will use
if awk -F= '/^NAME/{print $2}' /etc/os-release | grep -i "debian\|ubuntu"
then
    export PKCS11_MODULE="/usr/lib/softhsm/libsofthsm2.so"
else
    export PKCS11_MODULE="/usr/lib64/softhsm/libsofthsm.so"
fi
export PKCS11_PIN="1234"
export PKCS11_TOKEN="my_test_token_1"


# Delete the previous token if exists
softhsm2-util --delete-token --token $PKCS11_TOKEN

# Initialize a new fresh PKCS11 token
softhsm2-util --init-token --slot 0 --label $PKCS11_TOKEN --pin $PKCS11_PIN --so-pin $PKCS11_PIN
import asyncio
from python_x509_pkcs11.ca import create


async def my_func() -> None:
    root_ca_name_dict = {
        "country_name": "SE",
        "state_or_province_name": "Stockholm",
        "locality_name": "Stockholm",
        "organization_name": "SUNET",
        "organizational_unit_name": "SUNET Infrastructure",
        "common_name": "ca-test.sunet.se",
        "email_address": "soc@sunet.se",
    }

    # key_type must be:
    # [ed25519](https://en.wikipedia.org/wiki/EdDSA). This is default.
    # ed448
    # secp256r1
    # secp384r1
    # secp521r1
    # rsa_2048
    # rsa_4096

    csr_pem, root_cert_pem = await create("my_ed25519_key", root_ca_name_dict, key_type="ed25519")

    print("CSR which was self-signed into root CA")
    print(csr_pem)

    print("root CA")
    print(root_cert_pem)


asyncio.run(my_func())

Contributing / Tests

# install
if awk -F= '/^NAME/{print $2}' /etc/os-release | grep -i "debian\|ubuntu"
then
    # Ubuntu / Debian
    sudo apt-get install flit python3-mypy black pylint
else
    # Redhat / Centos / Fedora
    sudo dnf install epel-release
    sudo dnf install python3-flit python3-mypy python3-black pylint
fi


# Make your code changes
# Then in the root folder, where this README is
bash deploy.sh

# Build the package with flit
flit build

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_x509_pkcs11-0.8.1.tar.gz (57.3 kB view details)

Uploaded Source

Built Distribution

python_x509_pkcs11-0.8.1-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file python_x509_pkcs11-0.8.1.tar.gz.

File metadata

  • Download URL: python_x509_pkcs11-0.8.1.tar.gz
  • Upload date:
  • Size: 57.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for python_x509_pkcs11-0.8.1.tar.gz
Algorithm Hash digest
SHA256 541580d200da885be276f2b3a1b0d3140651ed86d058d5751a6417652477dceb
MD5 6a624abe828111e9521d1d1e0174d7cd
BLAKE2b-256 aeb15e85c077b2b334182fc204f9c7c00c5b6e55b6f3ff206b52b18668db840f

See more details on using hashes here.

File details

Details for the file python_x509_pkcs11-0.8.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_x509_pkcs11-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc8a688a76c88d7b47abb413dc0213426754505d723222458daad5ba4c05c585
MD5 75c22f922c29c9b4a5c8c3c507423d7e
BLAKE2b-256 ecb6eeb23aa111e167fc7554e02f6c380016be8aaf9b486672759244c3e5f20f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page