@@ -2457,7 +2457,9 @@ def test_load_locations_parameters(
2457
2457
monkeypatch ,
2458
2458
) -> None :
2459
2459
class LibMock :
2460
- def load_locations (self , store , cafile , capath ):
2460
+ def load_locations (
2461
+ self , store : object , cafile : object , capath : object
2462
+ ) -> int :
2461
2463
self .cafile = cafile
2462
2464
self .capath = capath
2463
2465
return 1
@@ -2489,12 +2491,14 @@ def test_load_locations_raises_error_on_failure(
2489
2491
store .load_locations (cafile = str (invalid_ca_file ))
2490
2492
2491
2493
2492
- def _runopenssl (pem , * args ) :
2494
+ def _runopenssl (pem : bytes , * args : bytes ) -> bytes :
2493
2495
"""
2494
2496
Run the command line openssl tool with the given arguments and write
2495
2497
the given PEM to its stdin. Not safe for quotes.
2496
2498
"""
2497
2499
proc = Popen ([b"openssl" , * list (args )], stdin = PIPE , stdout = PIPE )
2500
+ assert proc .stdin is not None
2501
+ assert proc .stdout is not None
2498
2502
proc .stdin .write (pem )
2499
2503
proc .stdin .close ()
2500
2504
output = proc .stdout .read ()
@@ -2562,7 +2566,9 @@ def test_load_privatekey_invalid_passphrase_type(self) -> None:
2562
2566
"""
2563
2567
with pytest .raises (TypeError ):
2564
2568
load_privatekey (
2565
- FILETYPE_PEM , encryptedPrivateKeyPEMPassphrase , object ()
2569
+ FILETYPE_PEM ,
2570
+ encryptedPrivateKeyPEMPassphrase ,
2571
+ object (), # type: ignore[arg-type]
2566
2572
)
2567
2573
2568
2574
def test_load_privatekey_wrongPassphrase (self ) -> None :
@@ -2576,14 +2582,14 @@ def test_load_privatekey_wrongPassphrase(self) -> None:
2576
2582
2577
2583
def test_load_privatekey_passphraseWrongType (self ) -> None :
2578
2584
"""
2579
- `load_privatekey` raises `ValueError` when it is passeda passphrase
2585
+ `load_privatekey` raises `ValueError` when it is passed a passphrase
2580
2586
with a private key encoded in a format, that doesn't support
2581
2587
encryption.
2582
2588
"""
2583
2589
key = load_privatekey (FILETYPE_PEM , root_key_pem )
2584
2590
blob = dump_privatekey (FILETYPE_ASN1 , key )
2585
2591
with pytest .raises (ValueError ):
2586
- load_privatekey (FILETYPE_ASN1 , blob , "secret" )
2592
+ load_privatekey (FILETYPE_ASN1 , blob , b "secret" )
2587
2593
2588
2594
def test_load_privatekey_passphrase (self ) -> None :
2589
2595
"""
@@ -2603,7 +2609,7 @@ def test_load_privatekey_passphrase_exception(self) -> None:
2603
2609
raised by `load_privatekey`.
2604
2610
"""
2605
2611
2606
- def cb (ignored ) :
2612
+ def cb (ignored : object ) -> bytes :
2607
2613
raise ArithmeticError
2608
2614
2609
2615
with pytest .raises (ArithmeticError ):
@@ -2615,10 +2621,11 @@ def test_load_privatekey_wrongPassphraseCallback(self) -> None:
2615
2621
is passed an encrypted PEM and a passphrase callback which returns an
2616
2622
incorrect passphrase.
2617
2623
"""
2618
- called = []
2624
+ called = False
2619
2625
2620
- def cb (* a ):
2621
- called .append (None )
2626
+ def cb (* a : object ) -> bytes :
2627
+ nonlocal called
2628
+ called = True
2622
2629
return b"quack"
2623
2630
2624
2631
with pytest .raises (Error ) as err :
@@ -2634,7 +2641,7 @@ def test_load_privatekey_passphraseCallback(self) -> None:
2634
2641
"""
2635
2642
called = []
2636
2643
2637
- def cb (writing ) :
2644
+ def cb (writing : bool ) -> bytes :
2638
2645
called .append (writing )
2639
2646
return encryptedPrivateKeyPEMPassphrase
2640
2647
@@ -2649,7 +2656,9 @@ def test_load_privatekey_passphrase_wrong_return_type(self) -> None:
2649
2656
"""
2650
2657
with pytest .raises (ValueError ):
2651
2658
load_privatekey (
2652
- FILETYPE_PEM , encryptedPrivateKeyPEM , lambda * args : 3
2659
+ FILETYPE_PEM ,
2660
+ encryptedPrivateKeyPEM ,
2661
+ lambda * args : 3 , # type: ignore[arg-type]
2653
2662
)
2654
2663
2655
2664
def test_dump_privatekey_wrong_args (self ) -> None :
@@ -2674,7 +2683,7 @@ def test_dump_privatekey_not_rsa_key(self) -> None:
2674
2683
2675
2684
def test_dump_privatekey_invalid_pkey (self ) -> None :
2676
2685
with pytest .raises (TypeError ):
2677
- dump_privatekey (FILETYPE_TEXT , object ())
2686
+ dump_privatekey (FILETYPE_TEXT , object ()) # type: ignore[arg-type]
2678
2687
2679
2688
def test_dump_privatekey_unknown_cipher (self ) -> None :
2680
2689
"""
@@ -2684,7 +2693,7 @@ def test_dump_privatekey_unknown_cipher(self) -> None:
2684
2693
key = PKey ()
2685
2694
key .generate_key (TYPE_RSA , 2048 )
2686
2695
with pytest .raises (ValueError ):
2687
- dump_privatekey (FILETYPE_PEM , key , BAD_CIPHER , "passphrase" )
2696
+ dump_privatekey (FILETYPE_PEM , key , BAD_CIPHER , b "passphrase" )
2688
2697
2689
2698
def test_dump_privatekey_invalid_passphrase_type (self ) -> None :
2690
2699
"""
@@ -2694,7 +2703,7 @@ def test_dump_privatekey_invalid_passphrase_type(self) -> None:
2694
2703
key = PKey ()
2695
2704
key .generate_key (TYPE_RSA , 2048 )
2696
2705
with pytest .raises (TypeError ):
2697
- dump_privatekey (FILETYPE_PEM , key , GOOD_CIPHER , object ())
2706
+ dump_privatekey (FILETYPE_PEM , key , GOOD_CIPHER , object ()) # type: ignore[arg-type]
2698
2707
2699
2708
def test_dump_privatekey_invalid_filetype (self ) -> None :
2700
2709
"""
@@ -2712,8 +2721,8 @@ def test_load_privatekey_passphrase_callback_length(self) -> None:
2712
2721
provided by the callback is too long, not silently truncate it.
2713
2722
"""
2714
2723
2715
- def cb (ignored ) :
2716
- return "a" * 1025
2724
+ def cb (ignored : object ) -> bytes :
2725
+ return b "a" * 1025
2717
2726
2718
2727
with pytest .raises (ValueError ):
2719
2728
load_privatekey (FILETYPE_PEM , encryptedPrivateKeyPEM , cb )
@@ -2739,7 +2748,7 @@ def test_dump_privatekey_passphrase_wrong_type(self) -> None:
2739
2748
"""
2740
2749
key = load_privatekey (FILETYPE_PEM , root_key_pem )
2741
2750
with pytest .raises (ValueError ):
2742
- dump_privatekey (FILETYPE_ASN1 , key , GOOD_CIPHER , "secret" )
2751
+ dump_privatekey (FILETYPE_ASN1 , key , GOOD_CIPHER , b "secret" )
2743
2752
2744
2753
def test_dump_certificate (self ) -> None :
2745
2754
"""
@@ -2765,7 +2774,7 @@ def test_dump_certificate_bad_type(self) -> None:
2765
2774
"""
2766
2775
cert = load_certificate (FILETYPE_PEM , root_cert_pem )
2767
2776
with pytest .raises (ValueError ):
2768
- dump_certificate (object (), cert )
2777
+ dump_certificate (object (), cert ) # type: ignore[arg-type]
2769
2778
2770
2779
def test_dump_privatekey_pem (self ) -> None :
2771
2780
"""
@@ -2856,7 +2865,7 @@ def test_dump_privatekey_passphrase_callback(self) -> None:
2856
2865
passphrase = b"foo"
2857
2866
called = []
2858
2867
2859
- def cb (writing ) :
2868
+ def cb (writing : bool ) -> bytes :
2860
2869
called .append (writing )
2861
2870
return passphrase
2862
2871
@@ -2875,7 +2884,7 @@ def test_dump_privatekey_passphrase_exception(self) -> None:
2875
2884
by the passphrase callback.
2876
2885
"""
2877
2886
2878
- def cb (ignored ) :
2887
+ def cb (ignored : object ) -> bytes :
2879
2888
raise ArithmeticError
2880
2889
2881
2890
key = load_privatekey (FILETYPE_PEM , root_key_pem )
@@ -2888,8 +2897,8 @@ def test_dump_privatekey_passphraseCallbackLength(self) -> None:
2888
2897
provided by the callback is too long, not silently truncate it.
2889
2898
"""
2890
2899
2891
- def cb (ignored ) :
2892
- return "a" * 1025
2900
+ def cb (ignored : object ) -> bytes :
2901
+ return b "a" * 1025
2893
2902
2894
2903
key = load_privatekey (FILETYPE_PEM , root_key_pem )
2895
2904
with pytest .raises (ValueError ):
@@ -2945,9 +2954,9 @@ def test_bad_file_type(self) -> None:
2945
2954
`FILETYPE_PEM` nor `FILETYPE_ASN1` then `ValueError` is raised.
2946
2955
"""
2947
2956
with pytest .raises (ValueError ):
2948
- load_certificate_request (object (), b"" )
2957
+ load_certificate_request (object (), b"" ) # type: ignore[arg-type]
2949
2958
with pytest .raises (ValueError ):
2950
- load_certificate (object (), b"" )
2959
+ load_certificate (object (), b"" ) # type: ignore[arg-type]
2951
2960
2952
2961
def test_bad_certificate (self ) -> None :
2953
2962
"""
@@ -2978,7 +2987,9 @@ class TestCRL:
2978
2987
)
2979
2988
2980
2989
@staticmethod
2981
- def _make_test_crl_cryptography (issuer_cert , issuer_key , certs = ()):
2990
+ def _make_test_crl_cryptography (
2991
+ issuer_cert : X509 , issuer_key : PKey , certs : list [X509 ] = []
2992
+ ) -> x509 .CertificateRevocationList :
2982
2993
"""
2983
2994
Create a CRL using cryptography's API.
2984
2995
@@ -2988,9 +2999,7 @@ def _make_test_crl_cryptography(issuer_cert, issuer_key, certs=()):
2988
2999
from cryptography .x509 .extensions import CRLReason , ReasonFlags
2989
3000
2990
3001
builder = x509 .CertificateRevocationListBuilder ()
2991
- builder = builder .issuer_name (
2992
- X509 .to_cryptography (issuer_cert ).subject
2993
- )
3002
+ builder = builder .issuer_name (issuer_cert .to_cryptography ().subject )
2994
3003
for cert in certs :
2995
3004
revoked = (
2996
3005
x509 .RevokedCertificateBuilder ()
@@ -3007,7 +3016,7 @@ def _make_test_crl_cryptography(issuer_cert, issuer_key, certs=()):
3007
3016
builder = builder .next_update (datetime (5000 , 6 , 1 , 0 , 0 , 0 ))
3008
3017
3009
3018
crl = builder .sign (
3010
- private_key = PKey .to_cryptography_key (issuer_key ),
3019
+ private_key = issuer_key .to_cryptography_key (),
3011
3020
algorithm = hashes .SHA512 (),
3012
3021
)
3013
3022
return crl
@@ -3078,7 +3087,7 @@ def test_valid(self) -> None:
3078
3087
store .add_cert (self .root_cert )
3079
3088
store .add_cert (self .intermediate_cert )
3080
3089
store_ctx = X509StoreContext (store , self .intermediate_server_cert )
3081
- assert store_ctx .verify_certificate () is None
3090
+ assert store_ctx .verify_certificate () is None # type: ignore[func-returns-value]
3082
3091
3083
3092
def test_reuse (self ) -> None :
3084
3093
"""
@@ -3089,8 +3098,8 @@ def test_reuse(self) -> None:
3089
3098
store .add_cert (self .root_cert )
3090
3099
store .add_cert (self .intermediate_cert )
3091
3100
store_ctx = X509StoreContext (store , self .intermediate_server_cert )
3092
- assert store_ctx .verify_certificate () is None
3093
- assert store_ctx .verify_certificate () is None
3101
+ assert store_ctx .verify_certificate () is None # type: ignore[func-returns-value]
3102
+ assert store_ctx .verify_certificate () is None # type: ignore[func-returns-value]
3094
3103
3095
3104
@pytest .mark .parametrize (
3096
3105
"root_cert, chain, verified_cert" ,
@@ -3116,12 +3125,12 @@ def test_reuse(self) -> None:
3116
3125
],
3117
3126
)
3118
3127
def test_verify_success_with_chain (
3119
- self , root_cert , chain , verified_cert
3128
+ self , root_cert : X509 , chain : list [ X509 ] , verified_cert : X509
3120
3129
) -> None :
3121
3130
store = X509Store ()
3122
3131
store .add_cert (root_cert )
3123
3132
store_ctx = X509StoreContext (store , verified_cert , chain = chain )
3124
- assert store_ctx .verify_certificate () is None
3133
+ assert store_ctx .verify_certificate () is None # type: ignore[func-returns-value]
3125
3134
3126
3135
def test_valid_untrusted_chain_reuse (self ) -> None :
3127
3136
"""
@@ -3136,8 +3145,8 @@ def test_valid_untrusted_chain_reuse(self) -> None:
3136
3145
store_ctx = X509StoreContext (
3137
3146
store , self .intermediate_server_cert , chain = chain
3138
3147
)
3139
- assert store_ctx .verify_certificate () is None
3140
- assert store_ctx .verify_certificate () is None
3148
+ assert store_ctx .verify_certificate () is None # type: ignore[func-returns-value]
3149
+ assert store_ctx .verify_certificate () is None # type: ignore[func-returns-value]
3141
3150
3142
3151
def test_chain_reference (self ) -> None :
3143
3152
"""
@@ -3153,7 +3162,7 @@ def test_chain_reference(self) -> None:
3153
3162
)
3154
3163
3155
3164
del chain
3156
- assert store_ctx .verify_certificate () is None
3165
+ assert store_ctx .verify_certificate () is None # type: ignore[func-returns-value]
3157
3166
3158
3167
@pytest .mark .parametrize (
3159
3168
"root_cert, chain, verified_cert" ,
@@ -3185,7 +3194,7 @@ def test_chain_reference(self) -> None:
3185
3194
],
3186
3195
)
3187
3196
def test_verify_fail_with_chain (
3188
- self , root_cert , chain , verified_cert
3197
+ self , root_cert : X509 , chain : list [ X509 ] , verified_cert : X509
3189
3198
) -> None :
3190
3199
store = X509Store ()
3191
3200
if root_cert :
@@ -3211,7 +3220,9 @@ def test_verify_fail_with_chain(
3211
3220
),
3212
3221
],
3213
3222
)
3214
- def test_untrusted_chain_wrong_args (self , chain , expected_error ) -> None :
3223
+ def test_untrusted_chain_wrong_args (
3224
+ self , chain : list [X509 ], expected_error : type [Exception ]
3225
+ ) -> None :
3215
3226
"""
3216
3227
Creating ``X509StoreContext`` with wrong chain raises an exception.
3217
3228
"""
@@ -3245,7 +3256,7 @@ def test_trusted_self_signed(self) -> None:
3245
3256
store = X509Store ()
3246
3257
store .add_cert (self .root_cert )
3247
3258
store_ctx = X509StoreContext (store , self .root_cert )
3248
- assert store_ctx .verify_certificate () is None
3259
+ assert store_ctx .verify_certificate () is None # type: ignore[func-returns-value]
3249
3260
3250
3261
def test_untrusted_self_signed (self ) -> None :
3251
3262
"""
@@ -3313,7 +3324,7 @@ def test_modification_pre_verify(self) -> None:
3313
3324
assert exc .value .certificate .get_subject ().CN == "intermediate"
3314
3325
3315
3326
store_ctx .set_store (store_good )
3316
- assert store_ctx .verify_certificate () is None
3327
+ assert store_ctx .verify_certificate () is None # type: ignore[func-returns-value]
3317
3328
3318
3329
def test_verify_with_time (self ) -> None :
3319
3330
"""
@@ -3325,6 +3336,7 @@ def test_verify_with_time(self) -> None:
3325
3336
store .add_cert (self .intermediate_cert )
3326
3337
3327
3338
expire_time = self .intermediate_server_cert .get_notAfter ()
3339
+ assert expire_time is not None
3328
3340
expire_datetime = datetime .strptime (
3329
3341
expire_time .decode ("utf-8" ), "%Y%m%d%H%M%SZ"
3330
3342
)
@@ -3393,23 +3405,29 @@ def _create_ca_file(
3393
3405
cafile .write_bytes (dump_certificate (FILETYPE_PEM , cacert ))
3394
3406
return cafile
3395
3407
3396
- def test_verify_with_ca_file_location (self , root_ca_file ) -> None :
3408
+ def test_verify_with_ca_file_location (
3409
+ self , root_ca_file : pathlib .Path
3410
+ ) -> None :
3397
3411
store = X509Store ()
3398
3412
store .load_locations (str (root_ca_file ))
3399
3413
3400
3414
store_ctx = X509StoreContext (store , self .intermediate_cert )
3401
3415
store_ctx .verify_certificate ()
3402
3416
3403
- def test_verify_with_ca_path_location (self , root_ca_file ) -> None :
3417
+ def test_verify_with_ca_path_location (
3418
+ self , root_ca_file : pathlib .Path
3419
+ ) -> None :
3404
3420
store = X509Store ()
3405
3421
store .load_locations (None , str (root_ca_file .parent ))
3406
3422
3407
3423
store_ctx = X509StoreContext (store , self .intermediate_cert )
3408
3424
store_ctx .verify_certificate ()
3409
3425
3410
3426
def test_verify_with_cafile_and_capath (
3411
- self , root_ca_file , intermediate_ca_file
3412
- ):
3427
+ self ,
3428
+ root_ca_file : pathlib .Path ,
3429
+ intermediate_ca_file : pathlib .Path ,
3430
+ ) -> None :
3413
3431
store = X509Store ()
3414
3432
store .load_locations (
3415
3433
cafile = str (root_ca_file ), capath = str (intermediate_ca_file .parent )
@@ -3419,8 +3437,8 @@ def test_verify_with_cafile_and_capath(
3419
3437
store_ctx .verify_certificate ()
3420
3438
3421
3439
def test_verify_with_multiple_ca_files (
3422
- self , root_ca_file , intermediate_ca_file
3423
- ):
3440
+ self , root_ca_file : pathlib . Path , intermediate_ca_file : pathlib . Path
3441
+ ) -> None :
3424
3442
store = X509Store ()
3425
3443
store .load_locations (str (root_ca_file ))
3426
3444
store .load_locations (str (intermediate_ca_file ))
@@ -3451,7 +3469,7 @@ def test_verify_with_partial_chain(self) -> None:
3451
3469
# Now set the partial verification flag for verification.
3452
3470
store .set_flags (X509StoreFlags .PARTIAL_CHAIN )
3453
3471
store_ctx = X509StoreContext (store , self .intermediate_server_cert )
3454
- assert store_ctx .verify_certificate () is None
3472
+ assert store_ctx .verify_certificate () is None # type: ignore[func-returns-value]
3455
3473
3456
3474
3457
3475
class TestEllipticCurve :
0 commit comments