Dustin Sun
Dustin Sun

Reputation: 5532

Get cert & key for python signxml from a PKS file

I used following command to get cert & key from a pks file.

 openssl pkcs12 -in ../my.pfx  -nocerts -out my.key
openssl pkcs12 -in ~/my.pfx -clcerts -nokeys -out cert.pem

However I keep getting error. I suspect my.key is not correct. How to generate the correct key and cert to feed signxml? I use python3.5 on Ubuntu 16.04. Thank you in advance!

 File "/home/ubuntu/.local/lib/python3.5/site-packages/signxml/__init__.py", line 362, in sign
    key = load_pem_private_key(key, password=passphrase, backend=default_backend())
  File "/home/ubuntu/.local/lib/python3.5/site-packages/cryptography/hazmat/primitives/serialization/base.py", line 16, in load_pem_private_key
    return backend.load_pem_private_key(data, password)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1025, in load_pem_private_key
    password,
  File "/home/ubuntu/.local/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1218, in _load_key
    mem_bio = self._bytes_to_bio(data)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 454, in _bytes_to_bio
    data_ptr = self._ffi.from_buffer(data)
TypeError: from_buffer() cannot return the address of a unicode object

The sample code from xml page:

cert = open("example.pem").read()
key = open("example.key").read()
root = ElementTree.fromstring(data_to_sign)
signed_root = XMLSigner().sign(root, key=key, cert=cert)
verified_data = XMLVerifier().verify(signed_root).signed_xml

Upvotes: 4

Views: 2354

Answers (1)

Dustin Sun
Dustin Sun

Reputation: 5532

I guess it is Python2 vs Python3 thing. All I needed is

key = open("example.key").read().encode('ascii')

Upvotes: 4

Related Questions