abhi abhi - 2 months ago 45
Python Question

load_pem_private_key fails with ecdsa key of size 521

I have the following two ECDSA private key for testing.

from cryptography.hazmat.primitives.serialization import load_pem_private_key
from cryptography.hazmat.backends import default_backend


privateECDSA_openssh521 = b"""-----BEGIN EC PRIVATE KEY-----
MIHcAgEBBEIAjn0lSVF6QweS4bjOGP9RHwqxUiTastSE0MVuLtFvkxygZqQ712oZ
ewMvqKkxthMQgxzSpGtRBcmkL7RqZ94+18qgBwYFK4EEACOhgYkDgYYABAFpX/6B
mxxglwD+VpEvw0hcyxVzLxNnMGzxZGF7xmNj8nlF7M+TQctdlR2Xv/J+AgIeVGmB
j2p84bkV9jBzrUNJEACsJjttZw8NbUrhxjkLT/3rMNtuwjE4vLja0P7DMTE0EV8X
f09ETdku/z/1tOSSrSvRwmUcM9nQUJtHHAZlr5Q0fw==
-----END EC PRIVATE KEY------"""


privateECDSA_openssh384 = b"""-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDAtAi7I8j73WCX20qUM5hhHwHuFzYWYYILs2Sh8UZ+awNkARZ/Fu2LU
LLl5RtOQpbWgBwYFK4EEACKhZANiAATU17sA9P5FRwSknKcFsjjsk0+E3CeXPYX0
Tk/M0HK3PpWQWgrO8JdRHP9eFE9O/23P8BumwFt7F/AvPlCzVd35VfraFT0o4cCW
G0RqpQ+np31aKmeJshkcYALEchnU+tQ=
-----END EC PRIVATE KEY-----"""


with this information if I do.
load_pem_private_key(privateECDSA_openssh384, None, default_backend())
it works fine but if I do
load_pem_private_key(privateECDSA_openssh521, None, default_backend())


I get the following error

load_pem_private_key(privateECDSA_openssh521, None, default_backend())
File "/usr/lib/python2.7/dist-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key
return backend.load_pem_private_key(data, password)
File "/usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/multibackend.py", line 282, in load_pem_private_key
return b.load_pem_private_key(data, password)
File "/usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/backend.py", line 1606, in load_pem_private_key
password,
File "/usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/backend.py", line 1802, in _load_key
self._handle_key_loading_error()
File "/usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/backend.py", line 1874, in _handle_key_loading_error
raise ValueError("Could not unserialize key data.")
ValueError: Could not unserialize key data.


I don't understand what is the problem.

atn atn
Answer

Have tried to load your data and got the following error string b'bad end line'

You have six dashes at the end line. Just fix it.

>>> privateECDSA_openssh521 = b"""-----BEGIN EC PRIVATE KEY-----
... MIHcAgEBBEIAjn0lSVF6QweS4bjOGP9RHwqxUiTastSE0MVuLtFvkxygZqQ712oZ
... ewMvqKkxthMQgxzSpGtRBcmkL7RqZ94+18qgBwYFK4EEACOhgYkDgYYABAFpX/6B
... mxxglwD+VpEvw0hcyxVzLxNnMGzxZGF7xmNj8nlF7M+TQctdlR2Xv/J+AgIeVGmB
... j2p84bkV9jBzrUNJEACsJjttZw8NbUrhxjkLT/3rMNtuwjE4vLja0P7DMTE0EV8X
... f09ETdku/z/1tOSSrSvRwmUcM9nQUJtHHAZlr5Q0fw==
... -----END EC PRIVATE KEY-----"""
>>> load_pem_private_key(privateECDSA_openssh521, None, default_backend())

returns:

<cryptography.hazmat.backends.openssl.ec._EllipticCurvePrivateKey object at 0x109cda128>
Comments