certbot / josepy

JOSE protocol implementation in Python using cryptography
https://josepy.readthedocs.io/en/latest/
Other
37 stars 27 forks source link

JWK.load does not work with a str as input type for data #11

Open canihavesomecoffee opened 6 years ago

canihavesomecoffee commented 6 years ago

Hi,

I'm trying to load a previously saved private key into a JWK instance using the JWK.load method.

The documentation (https://josepy.readthedocs.io/en/latest/api/jwk/) indicates that the load method expects a str to be passed to the data object, but whenever I do this, I'm getting an exception, resulting in the next stacktrace:

File "C:/Users/xxx/Desktop/xxx/certificate_requester.py", line 13, in __main__
    key = jose.JWK.load(data=private_key)
  File "C:\Users\xxx\Desktop\xxx\venv\lib\site-packages\josepy\jwk.py", line 108, in load
    key = cls._load_cryptography_key(data, password, backend)
  File "C:\Users\xxx\Desktop\xxx\venv\lib\site-packages\josepy\jwk.py", line 83, in _load_cryptography_key
    return loader(data, backend)
  File "C:\Users\xxx/\Desktop\xxx\venv\lib\site-packages\cryptography\hazmat\primitives\serialization.py", line 24, in load_pem_public_key
    return backend.load_pem_public_key(data)
  File "C:\Users\xxx/\Desktop\xxx\venv\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 1019, in load_pem_public_key
    mem_bio = self._bytes_to_bio(data)
  File "C:\Users\xxx\Desktop\xxx\venv\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 437, in _bytes_to_bio
    data_char_p = self._ffi.new("char[]", data)
TypeError: initializer for ctype 'char[]' must be a bytes or list or tuple, not str

However, when I pass in a bytes object (obtained by performing an encode('ascii') on the str instance, it's working out all fine.

Could this be due to a difference between python2 and python 3? I'm using the latter.

Environment:

ohemorange commented 6 years ago

Thanks for reporting! Looks like this might be an encoding issue; we'll look into it shortly.