fixes #12070 -- made SSH private key loading more consistent with other key loading (#12286)

This commit is contained in:
Alex Gaynor 2025-01-15 11:25:43 -05:00 committed by GitHub
parent b154fefdfd
commit da62c2fce7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 4 deletions

View file

@ -10,8 +10,15 @@ Changelog
* Support for Python 3.7 is deprecated and will be removed in the next
``cryptography`` release.
* Added support for PKCS7 decryption & encryption using AES-256 as content algorithm,
in addition to AES-128.
* Added support for PKCS7 decryption & encryption using AES-256 as content algorithm,
in addition to AES-128.
* **BACKWARDS INCOMPATIBLE:** Made SSH private key loading more consistent with
other private key loading:
:func:`~cryptography.hazmat.primitives.serialization.load_ssh_private_key`
now raises a ``TypeError`` if the key is unencrypted but a password is
provided (previously no exception was raised), and raises a ``TypeError`` if
the key is encrypted but no password is provided (previously a ``ValueError``
was raised).
.. _v44-0-0:

View file

@ -196,7 +196,9 @@ def _init_cipher(
) -> Cipher[modes.CBC | modes.CTR | modes.GCM]:
"""Generate key + iv and return cipher."""
if not password:
raise ValueError("Key is password-protected.")
raise TypeError(
"Key is password-protected, but password was not provided."
)
ciph = _SSH_CIPHERS[ciphername]
seed = _bcrypt_kdf(
@ -745,6 +747,10 @@ def load_ssh_private_key(
# should be no output from finalize
_check_empty(dec.finalize())
else:
if password:
raise TypeError(
"Password was given but private key is not encrypted."
)
# load secret data
edata, data = _get_sshstr(data)
_check_empty(data)

View file

@ -375,7 +375,7 @@ class TestOpenSSHSerialization:
)
assert pub1 == pub2
with pytest.raises(ValueError):
with pytest.raises(TypeError):
decoded_key = load_ssh_private_key(encdata, None, backend)
with pytest.raises(ValueError):
decoded_key = load_ssh_private_key(encdata, b"wrong", backend)
@ -611,6 +611,15 @@ class TestOpenSSHSerialization:
with pytest.raises(ValueError):
load_ssh_private_key(data, None, backend)
def test_ssh_errors_unencrypted_with_password(self):
data = load_vectors_from_file(
os.path.join("asymmetric", "OpenSSH", "rsa-nopsw.key"),
lambda f: f.read(),
mode="rb",
)
with pytest.raises(TypeError):
load_ssh_private_key(data, password=b"password")
@pytest.mark.supported(
only_if=lambda backend: backend.elliptic_curve_supported(
ec.SECP192R1()