Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/Crypto/PublicKey/ECC.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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'-----'):
Expand Down
10 changes: 7 additions & 3 deletions lib/Crypto/PublicKey/RSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down