Skip to content

Commit 61c034c

Browse files
authored
feat: drop max password length of 72 characters from 'serialize_ssh_private_key' (#7439)
1 parent 4368331 commit 61c034c

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/cryptography/hazmat/primitives/serialization/ssh.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def _bcrypt_kdf(
5353
_NONE = b"none"
5454
_DEFAULT_CIPHER = b"aes256-ctr"
5555
_DEFAULT_ROUNDS = 16
56-
_MAX_PASSWORD = 72
5756

5857
# re is only way to work on bytes-like data
5958
_PEM_RC = re.compile(_SK_START + b"(.*?)" + _SK_END, re.DOTALL)
@@ -609,11 +608,6 @@ def serialize_ssh_private_key(
609608
"""Serialize private key with OpenSSH custom encoding."""
610609
if password is not None:
611610
utils._check_bytes("password", password)
612-
if password and len(password) > _MAX_PASSWORD:
613-
raise ValueError(
614-
"Passwords longer than 72 bytes are not supported by "
615-
"OpenSSH private key format"
616-
)
617611

618612
if isinstance(private_key, ec.EllipticCurvePrivateKey):
619613
key_type = _ecdsa_key_type(private_key.public_key())

tests/hazmat/primitives/test_serialization.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,14 +2342,6 @@ def test_serialize_ssh_private_key_errors(self, backend):
23422342

23432343
private_key = ec.generate_private_key(ec.SECP256R1(), backend)
23442344

2345-
# too long password
2346-
with pytest.raises(ValueError):
2347-
private_key.private_bytes(
2348-
Encoding.PEM,
2349-
PrivateFormat.OpenSSH,
2350-
BestAvailableEncryption(b"p" * 73),
2351-
)
2352-
23532345
# unknown encryption class
23542346
with pytest.raises(ValueError):
23552347
private_key.private_bytes(
@@ -2358,6 +2350,41 @@ def test_serialize_ssh_private_key_errors(self, backend):
23582350
DummyKeySerializationEncryption(),
23592351
)
23602352

2353+
@pytest.mark.supported(
2354+
only_if=lambda backend: ssh._bcrypt_supported,
2355+
skip_message="Requires that bcrypt exists",
2356+
)
2357+
@pytest.mark.parametrize(
2358+
"password",
2359+
(
2360+
b"1234",
2361+
b"p@ssw0rd",
2362+
b"x" * 100,
2363+
),
2364+
)
2365+
def test_serialize_ssh_private_key_with_password(self, password, backend):
2366+
original_key = ec.generate_private_key(ec.SECP256R1(), backend)
2367+
encoded_key_data = ssh.serialize_ssh_private_key(
2368+
private_key=original_key,
2369+
password=password,
2370+
)
2371+
2372+
decoded_key = load_ssh_private_key(
2373+
data=encoded_key_data,
2374+
password=password,
2375+
backend=backend,
2376+
)
2377+
2378+
original_public_key = original_key.public_key().public_bytes(
2379+
Encoding.OpenSSH, PublicFormat.OpenSSH
2380+
)
2381+
2382+
decoded_public_key = decoded_key.public_key().public_bytes(
2383+
Encoding.OpenSSH, PublicFormat.OpenSSH
2384+
)
2385+
2386+
assert original_public_key == decoded_public_key
2387+
23612388
@pytest.mark.supported(
23622389
only_if=lambda backend: backend.dsa_supported(),
23632390
skip_message="Does not support DSA.",

0 commit comments

Comments
 (0)