Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 15, 2025
Merged
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
11 changes: 9 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
8 changes: 7 additions & 1 deletion src/cryptography/hazmat/primitives/serialization/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion tests/hazmat/primitives/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def test_bcrypt_encryption(self, backend):
)
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)
Expand Down Expand Up @@ -611,6 +611,15 @@ def test_ssh_errors_bad_secrets(self, backend):
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()
Expand Down
Loading