1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- '''Created Oct 13, 2021 Levi'''
- import os
- import ssl
- import tempfile
- import urllib.request
- import asn1crypto.pem
- import keyring
- import PyKCS11
- 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, CKA_MODULUS)
- import requests
- lib = PyKCS11.PyKCS11Lib()
- lib.load()
- keyID = (0x38, 0x7b, 0x4b, 0x49, 0xe2, 0xe7, 0x10, 0x4f, 0x60, 0x15, 0xc1, 0x42, 0x38, 0x6c, 0x3d, 0x41, 0x43, 0x5e, 0x91, 0x9b,)
- token_present = True
- slot = lib.getSlotList(tokenPresent=True)[0]
- session = lib.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION)
- session.login(keyring.get_password('sig', 'token'))
- pkcs11_certificates = session.findObjects([(CKA_CLASS, CKO_CERTIFICATE), (CKA_LABEL, 'Levente Marton')])
- # assert len(pkcs11_certificates) == 1
- pkcs11_cert = pkcs11_certificates[0]
- privKey = session.findObjects([(CKA_CLASS, CKO_PRIVATE_KEY), (CKA_ID, keyID)])[0]
- modulus = session.getAttributeValue(privKey, [CKA_MODULUS])[0]
- priv_key_dct = privKey.to_dict()
- # print(pkcs11_cert)
- print(privKey)
- pkcs11_cert_dct = pkcs11_cert.to_dict()
- der_encoded_certificate = bytes(pkcs11_cert_dct['CKA_VALUE'])
- # print(der_encoded_certificate)
- pem_armored_certificate = asn1crypto.pem.armor("CERTIFICATE",
- der_encoded_certificate)
- pem_armored_priv_key = asn1crypto.pem.armor("PRIVATE KEY",
- bytes(modulus))
- certfile = 'cert.pem'
- # privkey = 'privkey.key'
- # with open(certfile, 'wb') as certfile_handle:
- # certfile_handle.write(pem_armored_certificate)
- # with open(privkey, 'wb') as certfile_handle:
- # certfile_handle.write(pem_armored_priv_key)
- # try:
- session.logout()
- session.closeSession()
- cookie = {'LastMRH_Session': '7309fec2; F5_ST=1z1z1z1637062323z-1',
- 'MRHSession': '12f446c0f7d6dacc00657e8d7309fec2'}
- sslctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
- sslctx.load_cert_chain('cert.pem')
- # except Exception as exc_:
- # print(exc_)
- # finally:
- # r = requests.get('https://decl.anaf.mfinante.gov.ro/WAS6DUS/', cookies=cookie)
- # print(r.status_code)
|