Skip to content

Commit 5bc498a

Browse files
authored
Merge pull request jpadilla#244 from jpadilla/fix-ec-ssh-pub-key
Add support for ECDSA public keys in OpenSSH (RFC 4253) format
2 parents 0a4b8dd + 1710c15 commit 5bc498a

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
[Unreleased][unreleased]
88
-------------------------------------------------------------------------
99
### Changed
10+
- Add support for ECDSA public keys in RFC 4253 (OpenSSH) format [#244][244]
1011
- Renamed commandline script `jwt` to `jwt-cli` to avoid issues with the script clobbering the `jwt` module in some circumstances.
1112
- Better error messages when using an algorithm that requires the cryptography package, but it isn't available [#230][230]
1213

@@ -129,3 +130,4 @@ rarely used. Users affected by this should upgrade to 3.3+.
129130
[182]: https://github.com/jpadilla/pyjwt/pull/182
130131
[183]: https://github.com/jpadilla/pyjwt/pull/183
131132
[213]: https://github.com/jpadilla/pyjwt/pull/214
133+
[244]: https://github.com/jpadilla/pyjwt/pull/244

jwt/algorithms.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,10 @@ def prepare_key(self, key):
356356
# a Signing Key or a Verifying Key, so we try
357357
# the Verifying Key first.
358358
try:
359-
key = load_pem_public_key(key, backend=default_backend())
359+
if key.startswith(b'ecdsa-sha2-'):
360+
key = load_ssh_public_key(key, backend=default_backend())
361+
else:
362+
key = load_pem_public_key(key, backend=default_backend())
360363
except ValueError:
361364
key = load_pem_private_key(key, password=None, backend=default_backend())
362365

tests/keys/testkey_ec_ssh.pub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBAFZwnA8QCdL+TiQWBSHE0XsnRJBCFkb6c2DL7+ZfCFDk9khSYh3VrVOOQ1eIrO/oOm20Gp24dvP9XQS0f5B9bLQHgGFnkydPIMaNzPUNCop17F5uHOhtuFIhmOlh3lpTjyj2ten86cCetqN12kawnRs1/iu0wsGoVgk3os6yUAHvFMFGA==

tests/test_algorithms.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,13 @@ def test_ec_should_accept_pem_private_key_bytes(self):
375375
with open(key_path('testkey_ec'), 'rb') as ec_key:
376376
algo.prepare_key(ec_key.read())
377377

378+
@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
379+
def test_ec_should_accept_ssh_public_key_bytes(self):
380+
algo = ECAlgorithm(ECAlgorithm.SHA256)
381+
382+
with open(key_path('testkey_ec_ssh.pub'), 'r') as ec_key:
383+
algo.prepare_key(ec_key.read())
384+
378385
@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
379386
def test_ec_verify_should_return_false_if_signature_invalid(self):
380387
algo = ECAlgorithm(ECAlgorithm.SHA256)

0 commit comments

Comments
 (0)