Skip to content

Commit 5fd2827

Browse files
committed
Fixed crash when loading a PKCS#7 bundle with no certificates (pyca#9926)
1 parent 5012bed commit 5fd2827

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,9 +1890,12 @@ def _load_pkcs7_certificates(self, p7) -> typing.List[x509.Certificate]:
18901890
_Reasons.UNSUPPORTED_SERIALIZATION,
18911891
)
18921892

1893+
certs: list[x509.Certificate] = []
1894+
if p7.d.sign == self._ffi.NULL:
1895+
return certs
1896+
18931897
sk_x509 = p7.d.sign.cert
18941898
num = self._lib.sk_X509_num(sk_x509)
1895-
certs = []
18961899
for i in range(num):
18971900
x509 = self._lib.sk_X509_value(sk_x509, i)
18981901
self.openssl_assert(x509 != self._ffi.NULL)

tests/hazmat/primitives/test_pkcs7.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ def test_load_pkcs7_unsupported_type(self, backend):
8989
mode="rb",
9090
)
9191

92+
def test_load_pkcs7_empty_certificates(self):
93+
der = b"\x30\x0B\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x07\x02"
94+
95+
certificates = pkcs7.load_der_pkcs7_certificates(der)
96+
assert certificates == []
97+
9298

9399
# We have no public verification API and won't be adding one until we get
94100
# some requirements from users so this function exists to give us basic

0 commit comments

Comments
 (0)