PyKCS11 is a comprehensive Python wrapper for the PKCS#11 standard, enabling interaction with Hardware Security Modules (HSMs) and smart cards. It provides Python bindings for PKCS#11 functions, constants, and structures. The library is actively maintained with periodic releases, typically several times a year, with the current stable version being 1.5.18.
pip install pykcs11Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to load a PKCS#11 library, open a session, log in with a user PIN, and list objects (keys, certificates) present on the token. Replace `PKCS11_LIB_PATH` and `PKCS11_USER_PIN` with your actual PKCS#11 library path and PIN. For testing, SoftHSMv2 is a common choice.
Upgrade your Python environment to Python 3.x. Ensure your code is compatible with Python 3.
Upgrade to PyKCS11 v1.5.18 or newer for improved thread-safety. If using an older version, ensure `load()` and `unload()` are called from a single thread or protected by explicit locks.
Upgrade to PyKCS11 v1.5.13 or newer, which includes a specific fix for this Python 3.12 garbage collector interaction.
Ensure `SWIG` is installed on your system (e.g., `sudo apt-get install swig` on Debian/Ubuntu, `brew install swig` on macOS). For a binary installation, ensure `pip` can find a compatible wheel.
Verify the exact path to your PKCS#11 provider's shared library. Common locations include `/usr/local/lib/softhsm/libsofthsm2.so` (SoftHSMv2 on Linux), `/usr/lib/libeToken.so` (eToken on Linux), or specific vendor directories. Consult your HSM/smart card documentation.
Run `pip install pykcs11` to install the package.
Double-check the `pkcs11_lib_path` provided to `pkcs11.load()`. Ensure the shared library exists, is readable, and its own dependencies (if any) are met. Also, ensure the library is compatible with your system's architecture (e.g., 64-bit Python with 64-bit PKCS#11 library).
Verify the `user_pin` value. Ensure the token is not locked due to too many incorrect attempts. Some tokens have separate 'User PIN' and 'SO PIN' (Security Officer PIN).
Ensure your HSM or smart card is correctly inserted and detected by the system. Use `pkcs11.getSlotList(tokenPresent=True)` to confirm active slots before attempting to open a session.
Always check if the attribute object is `None` before attempting to access its properties. Use a conditional check or `next((attr for attr in attributes if attr.type == ...), None)` as shown in the quickstart to provide a default or handle missing attributes gracefully.
No dependency data recorded yet.