Skip to content

Commit 546a3a8

Browse files
committed
raise an exception instead of returning an empty list
as davidben points out in pyca#9926 we are calling a specific load certificates function and an empty value doesn't necessarily mean empty because PKCS7 contains multitudes. erroring is more correct.
1 parent f7db900 commit 546a3a8

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/cryptography/hazmat/backends/openssl/backend.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1111,12 +1111,15 @@ def _load_pkcs7_certificates(self, p7) -> list[x509.Certificate]:
11111111
_Reasons.UNSUPPORTED_SERIALIZATION,
11121112
)
11131113

1114-
certs: list[x509.Certificate] = []
11151114
if p7.d.sign == self._ffi.NULL:
1116-
return certs
1115+
raise ValueError(
1116+
"The provided PKCS7 has no certificate data, but a cert "
1117+
"loading method was called."
1118+
)
11171119

11181120
sk_x509 = p7.d.sign.cert
11191121
num = self._lib.sk_X509_num(sk_x509)
1122+
certs: list[x509.Certificate] = []
11201123
for i in range(num):
11211124
x509 = self._lib.sk_X509_value(sk_x509, i)
11221125
self.openssl_assert(x509 != self._ffi.NULL)

tests/hazmat/primitives/test_pkcs7.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def test_load_pkcs7_unsupported_type(self, backend):
9292
def test_load_pkcs7_empty_certificates(self):
9393
der = b"\x30\x0B\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x07\x02"
9494

95-
certificates = pkcs7.load_der_pkcs7_certificates(der)
96-
assert certificates == []
95+
with pytest.raises(ValueError):
96+
pkcs7.load_der_pkcs7_certificates(der)
9797

9898

9999
# We have no public verification API and won't be adding one until we get

0 commit comments

Comments
 (0)