123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- '''Created Oct 13, 2021 Levi'''
- import binascii
- import keyring
- from PyKCS11 import *
- from PyKCS11.LowLevel import (CKA_CLASS, CKO_CERTIFICATE,
- CKA_VALUE, CKA_ID, CKM_SHA1_RSA_PKCS,
- CKO_PRIVATE_KEY, CKM_SHA256_RSA_PKCS,
- CKF_SERIAL_SESSION, CKF_RW_SESSION,
- CKA_LABEL, CKA_VALUE)
- # the key_id has to be the same for both objects
- key_id = (0x38, 0x7b, 0x4b, 0x49, 0xe2, 0xe7, 0x10, 0x4f, 0x60, 0x15, 0xc1, 0x42, 0x38, 0x6c, 0x3d, 0x41, 0x43, 0x5e, 0x91, 0x9b,)
- lib = PyKCS11.PyKCS11Lib()
- lib.load()
- # get 1st slot
- slot = lib.getSlotList(tokenPresent=True)[0]
- session = lib.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION)
- session.login(keyring.get_password('sig', 'token'))
- pubTemplate = [
- (CKA_CLASS, CKO_PUBLIC_KEY),
- (CKA_TOKEN, CK_TRUE),
- (CKA_PRIVATE, CK_FALSE),
- (CKA_MODULUS_BITS, 0x0400),
- (CKA_PUBLIC_EXPONENT, (0x01, 0x00, 0x01)),
- (CKA_ENCRYPT, CK_TRUE),
- (CKA_VERIFY, CK_TRUE),
- (CKA_VERIFY_RECOVER, CK_TRUE),
- (CKA_WRAP, CK_TRUE),
- (CKA_LABEL, "Levente Marton"),
- (CKA_ID, key_id)
- ]
- privTemplate = [
- (CKA_CLASS, CKO_PRIVATE_KEY),
- (CKA_TOKEN, CK_TRUE),
- (CKA_PRIVATE, CK_TRUE),
- (CKA_DECRYPT, CK_TRUE),
- (CKA_SIGN, CK_TRUE),
- (CKA_SIGN_RECOVER, CK_TRUE),
- (CKA_UNWRAP, CK_TRUE),
- (CKA_ID, key_id)
- ]
- pubKey = session.findObjects([(CKA_CLASS, CKO_PUBLIC_KEY), (CKA_ID, key_id)])[0]
- privKey = session.findObjects([(CKA_CLASS, CKO_PRIVATE_KEY), (CKA_ID, key_id)])[0]
- modulus = session.getAttributeValue(privKey, [CKA_MODULUS])[0]
- # print("\nmodulus: {}".format(bytes(modulus)))
- (pubKey, privKey) = session.generateKeyPair(pubTemplate, privTemplate)
- #
- print(bytes(privKey))
- # logout
- session.logout()
- session.closeSession()
|