-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
This module contains functions to import OpenSSH keys. | ||
""" | ||
|
||
import re | ||
import typing | ||
|
||
from cryptography.hazmat.primitives.serialization.ssh import ( | ||
load_ssh_private_key, | ||
load_ssh_public_key, | ||
) | ||
|
||
from .hazmat import import_hazmat_key | ||
|
||
openssh_text_format_marker_re = re.compile( | ||
b"^-{2,}BEGIN OPENSSH PRIVATE KEY-{2,}$" | ||
) | ||
|
||
|
||
def import_ssh_key( | ||
key: typing.Union[str, bytes], password: typing.Optional[bytes] = None | ||
): | ||
""" | ||
<Purpose> | ||
Imports either a public or a private key in OpenSSH format | ||
<Arguments> | ||
key: | ||
A string in OpenSSH format, usually Base64-encoded. | ||
<Exceptions> | ||
securesystemslib.exceptions.FormatError, if the arguments are improperly | ||
formatted. | ||
securesystemslib.exceptions.UnsupportedAlgorithmError, if 'pem' specifies | ||
an unsupported key type. | ||
<Side Effects> | ||
None. | ||
<Returns> | ||
A dictionary containing the keys, conforming to 'securesystemslib.formats.KEY_SCHEMA'. | ||
""" | ||
|
||
if isinstance(key, str): | ||
key = key.encode("utf-8") | ||
|
||
first_line = key.split(b"\n", 1)[0] | ||
if openssh_text_format_marker_re.match(first_line): | ||
return import_hazmat_key(load_ssh_private_key(key, password)) | ||
|
||
return import_hazmat_key(load_ssh_public_key(key)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAaAAAABNlY2RzYS | ||
1zaGEyLW5pc3RwMjU2AAAACG5pc3RwMjU2AAAAQQRIz8F5gDezWgzdlRn5Upj7B1NoK43R | ||
wP1d/oiCtNKSYDdql9ds3d+5zzEhnhWvdjGOn1sKwAFlbkz+aueExAkvAAAAqGkmJORpJi | ||
TkAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEjPwXmAN7NaDN2V | ||
GflSmPsHU2grjdHA/V3+iIK00pJgN2qX12zd37nPMSGeFa92MY6fWwrAAWVuTP5q54TECS | ||
8AAAAhAMfqco7t7Cdz3rXsPcUwOINw4CKTqyTpOCFTzjULEB6AAAAACWVjZHNhIGtleQEC | ||
AwQFBg== | ||
-----END OPENSSH PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEjPwXmAN7NaDN2VGflSmPsHU2grjdHA/V3+iIK00pJgN2qX12zd37nPMSGeFa92MY6fWwrAAWVuTP5q54TECS8= ecdsa key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW | ||
QyNTUxOQAAACAUMmZQY7F/6Pvhj+LQn6HLke1TI0L1qvVImeBCxKjFuAAAAJAh8wZwIfMG | ||
cAAAAAtzc2gtZWQyNTUxOQAAACAUMmZQY7F/6Pvhj+LQn6HLke1TI0L1qvVImeBCxKjFuA | ||
AAAED6uVRyKJZMvslMIFpm4nbqMcRlzza7VwJobiRcbLNwSBQyZlBjsX/o++GP4tCfocuR | ||
7VMjQvWq9UiZ4ELEqMW4AAAAC2VkMjU1MTkga2V5AQI= | ||
-----END OPENSSH PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBQyZlBjsX/o++GP4tCfocuR7VMjQvWq9UiZ4ELEqMW4 ed25519 key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env sh | ||
|
||
types="ecdsa ed25519 rsa" # dsa | ||
|
||
for t in $types; do | ||
yes | ssh-keygen -t $t -C "$t key" -N "" -f ./$t; | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn | ||
NhAAAAAwEAAQAAAYEAs845MyRmYxDVrhRaY4YSu8vmKTDEk9fJQitvHZRt0G6m6VSYIAvt | ||
w3KQ+BnxVR0xFKSVqZWrReJ5aR680E3P/EYSc1YPhWN4WV0XcV+Secyeb1tWk0HXbo9ABj | ||
ENEUWhUKN6OXxjEHa7Y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05lHskdsiIX+MJOAKhNegAF1373V2mXynUce53OhVo23GjtFrvYN8WhjbggqvgQ9UuQoda | ||
j2OoW0Es4ZN0kn6j8bmWV8fZhPxUhmJc62LAQ4TGPwa+iBD0Bw53KdbxOn3erU8Xzu8VYK | ||
MwELEQsnR7KLDE4N0y3+DBUWBHeZpoLC3Tw9/H+P2bn5cD7014+zNdRwOM0L2y44o+X/a+ | ||
fyozEzAWaCa90AAAAHcnNhIGtleQECAwQ= | ||
-----END OPENSSH PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCzzjkzJGZjENWuFFpjhhK7y+YpMMST18lCK28dlG3QbqbpVJggC+3DcpD4GfFVHTEUpJWplatF4nlpHrzQTc/8RhJzVg+FY3hZXRdxX5J5zJ5vW1aTQdduj0AGMQ0RRaFQo3o5fGMQdrthDUW1/9ybGF0sv4DlTH0rtR8yCvXpl0LmaCIn0MireG8SqsaxdYDb8KdbOHL8Ezo2UmqZg0+svwkphEFoFzqQYVNAhgG4+/5Yotl2PBxZrSUWEUApqvWSS5BeyT/cTnf/8XDGV64H2Qmw9nZhRTIan6GgTgzPOhaX3yUxXS4ZI5rnWRQJuOk/J6FVKsJFNVA+/BAsxjcbUpms+PUtrXe+mHMLcb9FvQAiR9oteNMWUM3X1H8hVRZ20ETTzZVXM44mbSJ+ZvBEcpsGo9RmaMnvsc+4QI9ak/wEojPGN1msNXh/R8RWnhG+sCkbM9Btf2wf1RUyxtpkpYksbcDVyQEl+Rr6x2M+sJH9KZCzS9OFL8hZHKsCAks= rsa key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python | ||
|
||
""" | ||
<Program Name> | ||
test_openssh.py | ||
<Author> | ||
KOLANICH | ||
<Started> | ||
Nov 15, 2017 | ||
<Copyright> | ||
See LICENSE for licensing information. | ||
<Purpose> | ||
Test OpenSSH-related functions. | ||
""" | ||
|
||
import unittest | ||
from pathlib import Path | ||
|
||
this_dir = Path(__file__).resolve().absolute().parent | ||
keys_dir = this_dir / "data" / "ssh" | ||
|
||
# pylint: disable=wrong-import-position | ||
from securesystemslib.convert.ssh import import_ssh_key | ||
from securesystemslib.keys import create_signature, verify_signature | ||
|
||
TEST_DATA = b"test" | ||
|
||
|
||
class TestOpenSSH(unittest.TestCase): | ||
def test_openssh_import_and_sign_and_verify(self): | ||
files = sorted(set(keys_dir.glob("*.pub"))) | ||
for pub_f in files: | ||
sec_f = pub_f.parent / pub_f.stem | ||
with self.subTest(pub_f=pub_f, sec_f=sec_f): | ||
pub = import_ssh_key(pub_f.read_text(), None) | ||
sec = import_ssh_key(sec_f.read_text(), None) | ||
signature = create_signature(sec, TEST_DATA) | ||
self.assertTrue(verify_signature(pub, signature, TEST_DATA)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |