pysmime.pkcs11

Package pysmime :: Module pkcs11 |
[hide private]
[frames] | no frames]

# Source Code for Module pysmime.pkcs11

     1  # pysmime/pkcs11.py
     2  # Lorenzo Gaggini <lg@libersoft.it>
     3  # Libersoft <tech@libersoft.it>
     4  # http://www.libersoft.it
     5  # License: http://www.gnu.org/licenses/gpl.txt
     6
     7  """
     8  Interact with OpenSSL by M2Crypto library wrapper and OpenSC pkcs11 engine to
     9  access smart cards and retrieve private keys and certificates reference.
    10  """
    11
    12  from M2Crypto import Engine
    13
    14
    15 -def pkcs11_init(pkcs11_engine, pkcs11_driver):
    16      """
    17      Initializes Openssl pkcs11 engine with pkcs11 driver module and returns
    18      initialized engine for operations.
    19      """
    20      # loading Dynamic engine to load the PKCS#11 engine
    21      dynamic = Engine.load_dynamic_engine("pkcs11", pkcs11_engine)
    22      # loading pkcs#11 module
    23      pkcs11 = Engine.Engine("pkcs11")
    24      pkcs11.ctrl_cmd_string("MODULE_PATH", pkcs11_driver)
    25      pkcs11.init()
    26      return pkcs11
    27
    28
    29 -def pkcs11_login(pkcs11, pin):
    30      """
    31      Performs authentication by PIN on the smart card.
    32      """
    33      # logging in"
    34      pkcs11.ctrl_cmd_string("PIN", pin)
    35
    36
    37 -def pkcs11_get_data(pkcs11, slot_id):
    38      """
    39      Returns references to private key and certificate stored on the slot_id of
    40      the smart card.
    41      """
    42      # grab private key and certificate reference from smart card
    43      private_key = pkcs11.load_private_key(slot_id)
    44      cert = pkcs11.load_certificate(slot_id)
    45      return private_key, cert
    46