Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Changelog
* Support for 32-bit Windows (including publishing wheels) is deprecated
and will be removed in the next release. Users should move to a 64-bit
Python installation.
* ``public_bytes`` methods on public keys now raise ``TypeError`` (instead of
``ValueError`` if an invalid encoding is provided for the given ``format``).

.. _v46-0-2:

Expand Down
35 changes: 11 additions & 24 deletions src/rust/src/backend/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,9 @@ pub(crate) fn pkey_public_bytes<'p>(

// SubjectPublicKeyInfo + PEM/DER
if format.is(&types::PUBLIC_FORMAT_SUBJECT_PUBLIC_KEY_INFO.get(py)?) {
if encoding.is(&types::ENCODING_PEM.get(py)?) {
let pem_bytes = pkey.public_key_to_pem()?;
return Ok(pyo3::types::PyBytes::new(py, &pem_bytes));
} else if encoding.is(&types::ENCODING_DER.get(py)?) {
let der_bytes = pkey.public_key_to_der()?;
return Ok(pyo3::types::PyBytes::new(py, &der_bytes));
}
return Err(CryptographyError::from(
pyo3::exceptions::PyValueError::new_err(
"SubjectPublicKeyInfo works only with PEM or DER encoding",
),
));
let der_bytes = pkey.public_key_to_der()?;

return crate::asn1::encode_der_data(py, "PUBLIC KEY".to_string(), der_bytes, encoding);
}

if let Ok(ec) = pkey.ec_key() {
Expand All @@ -325,18 +316,14 @@ pub(crate) fn pkey_public_bytes<'p>(

if let Ok(rsa) = pkey.rsa() {
if format.is(&types::PUBLIC_FORMAT_PKCS1.get(py)?) {
if encoding.is(&types::ENCODING_PEM.get(py)?) {
let pem_bytes = rsa.public_key_to_pem_pkcs1()?;
return Ok(pyo3::types::PyBytes::new(py, &pem_bytes));
} else if encoding.is(&types::ENCODING_DER.get(py)?) {
let der_bytes = rsa.public_key_to_der_pkcs1()?;
return Ok(pyo3::types::PyBytes::new(py, &der_bytes));
}
return Err(CryptographyError::from(
pyo3::exceptions::PyValueError::new_err(
"PKCS1 works only with PEM or DER encoding",
),
));
let der_bytes = rsa.public_key_to_der_pkcs1()?;

return crate::asn1::encode_der_data(
py,
"RSA PUBLIC KEY".to_string(),
der_bytes,
encoding,
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/hazmat/primitives/test_dh.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ def test_public_bytes_values(
def test_public_bytes_rejects_invalid(self, encoding, fmt, backend):
parameters = FFDH3072_P.parameters(backend)
key = parameters.generate_private_key().public_key()
with pytest.raises(ValueError):
with pytest.raises((ValueError, TypeError)):
key.public_bytes(encoding, fmt)

def test_parameter_bytes_invalid_encoding(self, backend):
Expand Down
2 changes: 1 addition & 1 deletion tests/hazmat/primitives/test_dsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,5 +1057,5 @@ def test_public_bytes_pkcs1_unsupported(self, backend):
)
def test_public_bytes_rejects_invalid(self, encoding, fmt, backend):
key = DSA_KEY_2048.private_key(backend).public_key()
with pytest.raises(ValueError):
with pytest.raises((ValueError, TypeError)):
key.public_bytes(encoding, fmt)
2 changes: 1 addition & 1 deletion tests/hazmat/primitives/test_ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ def test_public_bytes_invalid_encoding(self, backend):
def test_public_bytes_rejects_invalid(self, encoding, fmt, backend):
_skip_curve_unsupported(backend, ec.SECP256R1())
key = ec.generate_private_key(ec.SECP256R1(), backend).public_key()
with pytest.raises(ValueError):
with pytest.raises((TypeError, ValueError)):
key.public_bytes(encoding, fmt)

def test_public_bytes_invalid_format(self, backend):
Expand Down
6 changes: 3 additions & 3 deletions tests/hazmat/primitives/test_rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -2709,12 +2709,12 @@ def test_public_bytes_openssh(self, backend):
key.public_bytes(
serialization.Encoding.DER, serialization.PublicFormat.OpenSSH
)
with pytest.raises(ValueError):
with pytest.raises(TypeError):
key.public_bytes(
serialization.Encoding.OpenSSH,
serialization.PublicFormat.PKCS1,
)
with pytest.raises(ValueError):
with pytest.raises(TypeError):
key.public_bytes(
serialization.Encoding.OpenSSH,
serialization.PublicFormat.SubjectPublicKeyInfo,
Expand Down Expand Up @@ -2767,7 +2767,7 @@ def test_public_bytes_rejects_invalid(
self, rsa_key_2048: rsa.RSAPrivateKey, encoding, fmt, backend
):
key = rsa_key_2048.public_key()
with pytest.raises(ValueError):
with pytest.raises((ValueError, TypeError)):
key.public_bytes(encoding, fmt)

def test_public_key_equality(self, rsa_key_2048: rsa.RSAPrivateKey):
Expand Down
2 changes: 1 addition & 1 deletion tests/hazmat/primitives/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ def test_dh_public_key(self, backend):
):
# tested elsewhere
continue
with pytest.raises(ValueError):
with pytest.raises((TypeError, ValueError)):
public_key.public_bytes(enc, fmt)

@pytest.mark.skip_fips(reason="non-FIPS parameters")
Expand Down
2 changes: 1 addition & 1 deletion tests/hazmat/primitives/test_x25519.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_public_bytes_bad_args(self, backend):
serialization.Encoding.DER,
None, # type: ignore[arg-type]
)
with pytest.raises(ValueError):
with pytest.raises(TypeError):
key.public_bytes(
serialization.Encoding.SMIME,
serialization.PublicFormat.SubjectPublicKeyInfo,
Expand Down