diff --git a/lib/Crypto/PublicKey/ECC.py b/lib/Crypto/PublicKey/ECC.py index 7a32adffc..f7872e585 100644 --- a/lib/Crypto/PublicKey/ECC.py +++ b/lib/Crypto/PublicKey/ECC.py @@ -999,7 +999,7 @@ def _import_openssh_public(encoded): return ecc_key -def _import_openssh_private_ecc(data, password): +def _import_openssh_private_ecc(data, password, include_comment=False): from ._openssh import (import_openssh_private_generic, read_bytes, read_string, check_padding) @@ -1048,9 +1048,12 @@ def _import_openssh_private_ecc(data, password): else: raise ValueError("Unsupport SSH agent key type:" + key_type) - _, padded = read_string(decrypted) # Comment + comment, padded = read_string(decrypted) # Comment check_padding(padded) + if include_comment: #return a tuple instead + return (construct(point_x=point_x, point_y=point_y, **params), comment) + return construct(point_x=point_x, point_y=point_y, **params) @@ -1196,7 +1199,7 @@ def _import_ed448_public_key(encoded): return point_x, point_y -def import_key(encoded, passphrase=None, curve_name=None): +def import_key(encoded, passphrase=None, curve_name=None, include_comment=False): """Import an ECC key (public or private). Args: @@ -1235,6 +1238,9 @@ def import_key(encoded, passphrase=None, curve_name=None): For a SEC1 encoding only. This is the name of the curve, as defined in the `ECC table`_. + include_comment (bool): + Include the comment associated + .. note:: To import EdDSA private and public keys, when encoded as raw ``bytes``, use: @@ -1279,7 +1285,7 @@ def import_key(encoded, passphrase=None, curve_name=None): if encoded.startswith(b'-----BEGIN OPENSSH PRIVATE KEY'): text_encoded = tostr(encoded) openssh_encoded, marker, enc_flag = PEM.decode(text_encoded, passphrase) - result = _import_openssh_private_ecc(openssh_encoded, passphrase) + result = _import_openssh_private_ecc(openssh_encoded, passphrase, include_comment) return result elif encoded.startswith(b'-----'): diff --git a/lib/Crypto/PublicKey/RSA.py b/lib/Crypto/PublicKey/RSA.py index bc8f9d2d5..0b458c698 100644 --- a/lib/Crypto/PublicKey/RSA.py +++ b/lib/Crypto/PublicKey/RSA.py @@ -754,7 +754,7 @@ def _import_keyDER(extern_key, passphrase): raise ValueError("RSA key format is not supported") -def _import_openssh_private_rsa(data, password): +def _import_openssh_private_rsa(data, password, include_comment=False): from ._openssh import (import_openssh_private_generic, read_bytes, read_string, check_padding) @@ -771,14 +771,18 @@ def _import_openssh_private_rsa(data, password): p, decrypted = read_bytes(decrypted) q, decrypted = read_bytes(decrypted) - _, padded = read_string(decrypted) # Comment + comment, padded = read_string(decrypted) # Comment check_padding(padded) build = [Integer.from_bytes(x) for x in (n, e, d, q, p, iqmp)] + + if include_comment: # return tuple instead + return(construct(build),comment) + return construct(build) -def import_key(extern_key, passphrase=None): +def import_key(extern_key, passphrase=None, include_comment=False): """Import an RSA key (public or private). Args: