Skip to content

Commit a7023b1

Browse files
committed
More cover
1 parent a9aecb6 commit a7023b1

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.3.1
4+
5+
* is_issued_by now raises MissingCertProperty if no hash algorithm found
6+
37
## 1.3.0
48

59
New feature and sane defaults. for the CLI the root is now by default excluded, at first it would include it if it found one, but not all certificate authorities provide a link to their root in their certs. This resulted in sometimes a root to be included and othertimes not.

cert_chain_resolver/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,5 @@ def main():
133133
cli(**cli_args)
134134

135135

136-
if __name__ == "__main__":
136+
if __name__ == "__main__": # pragma: no cover
137137
main()

cert_chain_resolver/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey
88
from cryptography.hazmat.primitives.asymmetric.ec import ECDSA, EllipticCurvePublicKey
99
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
10+
from cryptography.exceptions import InvalidSignature
1011

1112

1213
import binascii
@@ -204,7 +205,7 @@ def is_issued_by(self, other):
204205
ECDSA(hash_algorithm),
205206
)
206207
return True
207-
except Exception:
208+
except InvalidSignature as e:
208209
pass
209210

210211
return False

tests/test_models.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
from cert_chain_resolver.models import Cert, CertificateChain
55
from cryptography.x509.oid import ExtensionOID, AuthorityInformationAccessOID, NameOID
66
import pytest
7+
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey
8+
from cryptography.hazmat.primitives.asymmetric.ec import ECDSA, EllipticCurvePublicKey
9+
from cryptography.hazmat.primitives import hashes
10+
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
11+
from cryptography.exceptions import InvalidSignature
712

813

914
try:
1015
from contextlib import nullcontext as does_not_raise
1116
except ImportError:
1217
from contextlib import contextmanager
1318

14-
@contextmanager
19+
@contextmanager # type: ignore[no-redef]
1520
def does_not_raise():
1621
yield
1722

@@ -22,6 +27,18 @@ def does_not_raise():
2227
unicode = str
2328

2429

30+
@pytest.fixture
31+
def mock_x509(mocker):
32+
return mocker.Mock(spec=x509.Certificate)
33+
34+
35+
@pytest.fixture
36+
def mock_cert(mocker, mock_x509):
37+
cert = mocker.Mock(spec=Cert)
38+
cert._x509 = mock_x509
39+
return cert
40+
41+
2542
@pytest.mark.parametrize("cert", CERT_FIXTURES, ids=certfixture_to_id)
2643
def test_certcontainer_x509_helper_props(cert):
2744
c = Cert(cert["cert_x509"])
@@ -190,3 +207,35 @@ class CertOverride(Cert):
190207
c = CertOverride()
191208

192209
assert repr(c) == '<Cert common_name="CN" subject="Subject" issuer="Issuer">'
210+
211+
212+
@pytest.mark.parametrize(
213+
"key_type,expected",
214+
[
215+
(RSAPublicKey, True),
216+
(RSAPublicKey, False),
217+
(EllipticCurvePublicKey, True),
218+
(EllipticCurvePublicKey, False),
219+
(object, False), # Unexpected key type FIXME: Maybe this should raise??
220+
],
221+
)
222+
def test_is_issued_by_handles_different_keys(
223+
mocker, mock_x509, mock_cert, key_type, expected
224+
):
225+
mock_public_key = mocker.Mock(spec=key_type)
226+
mock_x509.public_key.return_value = mock_public_key
227+
mock_x509.signature_hash_algorithm = mocker.Mock(spec=hashes.SHA256)
228+
229+
subject = Cert(mock_x509)
230+
231+
if not expected and hasattr(key_type, "verify"):
232+
mock_public_key.verify.side_effect = InvalidSignature()
233+
234+
assert subject.is_issued_by(mock_cert) is expected
235+
236+
237+
def test_is_issued_raises_when_no_signature_hash_algo(mock_x509, mock_cert):
238+
mock_x509.signature_hash_algorithm = None
239+
mock_x509.public_key = lambda: None
240+
with pytest.raises(MissingCertProperty):
241+
Cert(mock_x509).is_issued_by(mock_cert)

0 commit comments

Comments
 (0)