{"library":"pykcs11","title":"PyKCS11 - Python PKCS#11 Wrapper","description":"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.","language":"python","status":"active","last_verified":"Sun May 17","install":{"commands":["pip install pykcs11"],"cli":null},"imports":["import PyKCS11"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import PyKCS11\nimport os\n\n# Path to your PKCS#11 shared library (e.g., softhsm2.so, libeToken.so)\n# For testing, consider installing SoftHSMv2.\npkcs11_lib_path = os.environ.get('PKCS11_LIB_PATH', '/usr/local/lib/softhsm/libsofthsm2.so')\nuser_pin = os.environ.get('PKCS11_USER_PIN', '1234')\n\ntry:\n    # Initialize the PyKCS11 library wrapper\n    pkcs11 = PyKCS11.PyKCS11Lib()\n    pkcs11.load(pkcs11_lib_path)\n\n    # Get a list of available slots with a token present\n    slots = pkcs11.getSlotList(tokenPresent=True)\n    if not slots:\n        print(\"No PKCS#11 slots with tokens found. Ensure your HSM/smart card is connected and configured.\")\n        exit()\n\n    # Open a session with the first available slot\n    session = pkcs11.openSession(slots[0])\n\n    # Log in to the token (required for most operations)\n    session.login(user_pin)\n\n    print(f\"Successfully logged into slot {slots[0]} using PIN.\")\n\n    # Example: Find and list objects (e.g., keys, certificates)\n    objects = session.findObjects()\n    print(f\"\\nFound {len(objects)} objects in the token:\")\n    for obj in objects:\n        # Attempt to get CKA_CLASS and CKA_LABEL attributes gracefully\n        attributes = session.getAttributeValue(obj, [PyKCS11.CKA_CLASS, PyKCS11.CKA_LABEL])\n        obj_class = next((attr for attr in attributes if attr.type == PyKCS11.CKA_CLASS), None)\n        obj_label = next((attr for attr in attributes if attr.type == PyKCS11.CKA_LABEL), None)\n        \n        class_name = pkcs11.constantToString(obj_class.value, PyKCS11.CKF_CLASS_DEFINED) if obj_class else 'N/A'\n        label = obj_label.value if obj_label else 'N/A'\n        print(f\"  - Class: {class_name}, Label: {label}\")\n\nfinally:\n    # Ensure logout, close session, and unload library in a finally block\n    if 'session' in locals() and session:\n        try:\n            session.logout()\n            session.closeSession()\n            print(\"\\nLogged out and session closed.\")\n        except PyKCS11.PyKCS11Error as e:\n            print(f\"Error during cleanup: {e}\")\n    if 'pkcs11' in locals() and pkcs11:\n        try:\n            pkcs11.unload()\n            print(\"PKCS#11 library unloaded.\")\n        except PyKCS11.PyKCS11Error as e:\n            print(f\"Error during library unload: {e}\")\n","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-17","installed_version":null,"pypi_latest":"1.5.18","is_stale":null,"summary":{"python_range":"3.10–3.9","success_rate":0,"avg_install_s":null,"avg_import_s":null,"wheel_type":null},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"pykcs11","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"pykcs11","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":4,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"pykcs11","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"pykcs11","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":3.9,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"pykcs11","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"pykcs11","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":4,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"pykcs11","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"pykcs11","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":3.5,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"pykcs11","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"pykcs11","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":4.6,"import_time_s":null,"mem_mb":null,"disk_size":null}]}}