4
4
from cert_chain_resolver .models import Cert , CertificateChain
5
5
from cryptography .x509 .oid import ExtensionOID , AuthorityInformationAccessOID , NameOID
6
6
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
7
12
8
13
9
14
try :
@@ -22,6 +27,18 @@ def does_not_raise():
22
27
unicode = str
23
28
24
29
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
+
25
42
@pytest .mark .parametrize ("cert" , CERT_FIXTURES , ids = certfixture_to_id )
26
43
def test_certcontainer_x509_helper_props (cert ):
27
44
c = Cert (cert ["cert_x509" ])
@@ -190,3 +207,28 @@ class CertOverride(Cert):
190
207
c = CertOverride ()
191
208
192
209
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
0 commit comments