pkcs_lib.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. '''Created Feb 24, 2021 Levi'''
  2. import os
  3. import pkcs11
  4. import asn1crypto.pem
  5. import urllib.request
  6. import tempfile
  7. import ssl
  8. import requests
  9. lib = pkcs11.lib(os.getenv('PKCS11_MODULE'))
  10. LABEL = str(list(lib.get_tokens())[0])
  11. token = lib.get_token(token_label=LABEL)
  12. # print(list(lib.get_tokens())[0])
  13. pem = None
  14. with token.open(user_pin=os.getenv('PKCS11PIN')) as sess:
  15. pkcs11_certificates = sess.get_objects(
  16. {
  17. # pkcs11.Attribute.CLASS: pkcs11.constants.ObjectClass.PUBLIC_KEY,
  18. pkcs11.Attribute.CLASS: pkcs11.ObjectClass.CERTIFICATE,
  19. pkcs11.Attribute.LABEL: "Levente Marton"
  20. })
  21. # hopefully the selector above is sufficient
  22. pkcs11_certificates = list(pkcs11_certificates)
  23. assert len(pkcs11_certificates) == 1
  24. # for cert in pkcs11_certificates:
  25. # pkcs11_cert = cert
  26. pkcs11_cert = pkcs11_certificates[0]
  27. der_encoded_certificate = pkcs11_cert.__getitem__(pkcs11.Attribute.VALUE)
  28. print(der_encoded_certificate)
  29. # the ssl library expects to be given PEM armored certificates
  30. pem_armored_certificate = asn1crypto.pem.armor("CERTIFICATE",
  31. der_encoded_certificate)
  32. # this is the ugly part: persisting the certificate on disk
  33. # i deliberately did not go with a sophisticated solution here since it's
  34. # such a big caveat to have to do this...
  35. # certfile = tempfile.mkstemp()
  36. # with open(certfile[1], 'w') as certfile_handle:
  37. # certfile_handle.write(pem_armored_certificate.decode("utf-8"))
  38. # this will instruct the ssl library to provide the certificate
  39. # if asked by the server.
  40. # sslctx = ssl.create_default_context()
  41. # sslctx.load_cert_chain(certfile=certfile[1])
  42. # if your certificate does not contain the private key, find it elsewhere
  43. # sslctx.load_cert_chain(certfile=certfile[1],
  44. # keyfile="/path/to/privatekey.pem",
  45. # password="<private_key_password_if_applicable>")
  46. # response = urllib.request.urlopen("https://webserviced.anaf.ro/SPVWS2/rest/listaMesaje?zile=5", context=sslctx)
  47. s = requests.Session()
  48. s.cert = 'cert.pem'
  49. r = s.get("https://webserviced.anaf.ro/SPVWS2/rest/listaMesaje?zile=5", cert='cert.pem')
  50. # Cleanup and delete the "temporary" certificate from disk
  51. # os.remove(certfile[1])
  52. # data = b'INPUT DATA'
  53. #...............................................................................
  54. # data = 'plaintext'
  55. # priv_key = b'INPUT DATA'
  56. # with token.open(user_pin='111555') as session:
  57. # pubs = list(session.get_objects(
  58. # {pkcs11.Attribute.CLASS: pkcs11.constants.ObjectClass.PUBLIC_KEY}
  59. # ))
  60. # privs = list(session.get_objects(
  61. # {pkcs11.Attribute.CLASS: pkcs11.constants.ObjectClass.PRIVATE_KEY}
  62. # ))
  63. # #...........................................................................
  64. # # for obj in session.get_objects(
  65. # # {pkcs11.Attribute.CLASS: pkcs11.constants.ObjectClass.PUBLIC_KEY}
  66. # # ):
  67. # # print(obj)
  68. # #...........................................................................
  69. # #...........................................................................
  70. # # for key in session.get_key(object_class=pkcs11.constants.ObjectClass.PUBLIC_KEY):
  71. # # print(key)
  72. # #...........................................................................
  73. # pub, priv = pubs[0], privs[0]
  74. # # print(priv.key_length)
  75. # signature = priv.sign(data)
  76. # #...........................................................................
  77. # # with open('test.txt', 'wb') as test:
  78. # # test.write(data)
  79. # #...........................................................................
  80. # assert pub.verify(data, signature)
  81. # # print(pub)
  82. # # print(priv)
  83. # # priv = session.get_key(id=b'9c0be6eee41e1bbfebf3c36c58064e04a5a29688', object_class=pkcs11.constants.ObjectClass.PRIVATE_KEY)
  84. # # print(pub)
  85. # # session.get_objects(label='certSIGN')
  86. # # Generate an RSA keypair in this session
  87. # # pub, priv = session.generate_keypair(pkcs11.KeyType.RSA, 2048)
  88. # # Encrypt as one block
  89. # # crypttext = pub.encrypt(data)
  90. #...............................................................................