7
7
import pytest
8
8
from cryptography .hazmat .backends import default_backend
9
9
from cryptography .hazmat .primitives .asymmetric import ec
10
- from cryptojwt .exception import MissingKey
10
+ from oic import rndstr
11
+
12
+ from cryptojwt .exception import MissingKey , BadSyntax , HeaderError
11
13
from cryptojwt .exception import Unsupported
12
14
from cryptojwt .exception import VerificationError
13
15
from cryptojwt .jwe .exception import UnsupportedBitLength , \
14
- NoSuitableEncryptionKey
16
+ NoSuitableEncryptionKey , WrongEncryptionAlgorithm , NoSuitableDecryptionKey
15
17
16
- from cryptojwt .utils import b64e
18
+ from cryptojwt .utils import b64e , as_bytes
17
19
18
20
from cryptojwt .jwe .aes import AES_CBCEncrypter
19
21
from cryptojwt .jwe .aes import AES_GCMEncrypter
@@ -349,6 +351,50 @@ def test_ecdh_encrypt_decrypt_keywrapped_key():
349
351
assert msg == plain
350
352
351
353
354
+ def test_ecdh_enc_setup_wrong_key ():
355
+ jwenc = JWE_EC (plain , alg = "ECDH-ES+A128KW" , enc = "A128GCM" )
356
+ with pytest .raises (ValueError ):
357
+ jwenc .enc_setup (plain , key = priv_key )
358
+
359
+
360
+ def test_ecdh_enc_setup_enk ():
361
+ jwenc = JWE_EC (plain , alg = "ECDH-ES+A128KW" , enc = "A128GCM" )
362
+ assert jwenc .enc_setup (plain , key = eck_bob , epk = alice )
363
+
364
+
365
+ def test_ecdh_enc_setup_enk_eckey ():
366
+ jwenc = JWE_EC (plain , alg = "ECDH-ES+A128KW" , enc = "A128GCM" )
367
+ assert jwenc .enc_setup (plain , key = eck_bob , epk = eck_alice )
368
+
369
+
370
+ def test_ecdh_setup_iv ():
371
+ jwenc = JWE_EC (plain , alg = "ECDH-ES+A128KW" , enc = "A128GCM" )
372
+ iv0 = rndstr (16 )
373
+ cek , encrypted_key , iv , params , ret_epk = jwenc .enc_setup (plain , iv = iv0 ,
374
+ key = eck_bob )
375
+ assert iv == iv0
376
+
377
+
378
+ def test_ecdh_setup_cek ():
379
+ jwenc = JWE_EC (plain , alg = "ECDH-ES+A128KW" , enc = "A128GCM" )
380
+ cek0 = as_bytes (rndstr (16 ))
381
+ cek , encrypted_key , iv , params , ret_epk = jwenc .enc_setup (plain , cek = cek0 ,
382
+ key = eck_bob )
383
+ assert cek == cek0
384
+
385
+
386
+ def test_ecdh_setup_unknown_alg ():
387
+ jwenc = JWE_EC (plain , alg = "ECDH-ES+A128KW" , enc = "A384GCM" )
388
+ with pytest .raises (ValueError ):
389
+ jwenc .enc_setup (plain , key = eck_bob )
390
+
391
+
392
+ def test_ecdh_setup_unknown_alg_2 ():
393
+ jwenc = JWE_EC (plain , alg = "ECDH-ES" , enc = "A384GCM" )
394
+ with pytest .raises (ValueError ):
395
+ jwenc .enc_setup (plain , key = eck_bob )
396
+
397
+
352
398
def test_sym_encrypt_decrypt ():
353
399
encryption_key = SYMKey (use = "enc" , key = 'DukeofHazardpass' ,
354
400
kid = "some-key-id" )
@@ -373,10 +419,10 @@ def test_verify_headers():
373
419
jwenc = JWE (plain , alg = "ECDH-ES" , enc = "A128GCM" )
374
420
jwt = jwenc .encrypt ([eck_bob ])
375
421
assert jwt
376
- decryptor = factory (jwt , alg = "ECDH-ES" , enc = "A128GCM" )
377
- assert decryptor .jwt .verify_headers (alg = 'ECDH-ES' , enc = 'A128GCM' )
378
- assert decryptor .jwt .verify_headers (alg = 'RS256' ) is False
379
- assert decryptor .jwt .verify_headers (kid = 'RS256' ) is False
422
+ decrypter = factory (jwt , alg = "ECDH-ES" , enc = "A128GCM" )
423
+ assert decrypter .jwt .verify_headers (alg = 'ECDH-ES' , enc = 'A128GCM' )
424
+ assert decrypter .jwt .verify_headers (alg = 'RS256' ) is False
425
+ assert decrypter .jwt .verify_headers (kid = 'RS256' ) is False
380
426
381
427
382
428
def test_encrypt_no_keys ():
@@ -391,17 +437,101 @@ def test_encrypt_jwk_key():
391
437
jwenc = JWE (plain , alg = "ECDH-ES" , enc = "A128GCM" , jwk = eck_bob )
392
438
_enc = jwenc .encrypt ()
393
439
assert _enc
394
- decryptor = factory (_enc , alg = "ECDH-ES" , enc = "A128GCM" )
395
- res = decryptor .decrypt ()
440
+ decrypter = factory (_enc , alg = "ECDH-ES" , enc = "A128GCM" )
441
+ res = decrypter .decrypt ()
396
442
assert res == plain
397
443
398
444
399
- def test_sym_encrypt_decrypt_JWE ():
445
+ def test_sym_encrypt_decrypt_jwe ():
400
446
encryption_key = SYMKey (use = "enc" , key = 'DukeofHazardpass' ,
401
447
kid = "some-key-id" )
402
448
jwe = JWE (plain , alg = "A128KW" , enc = "A128CBC-HS256" )
403
449
_jwe = jwe .encrypt (keys = [encryption_key ], kid = "some-key-id" )
404
- decryptor = factory (_jwe , alg = "A128KW" , enc = "A128CBC-HS256" )
450
+ decrypter = factory (_jwe , alg = "A128KW" , enc = "A128CBC-HS256" )
405
451
406
- resp = decryptor .decrypt (_jwe , [encryption_key ])
452
+ resp = decrypter .decrypt (_jwe , [encryption_key ])
407
453
assert resp == plain
454
+
455
+
456
+ def test_sym_jwenc ():
457
+ encryption_key = SYMKey (use = "enc" , key = 'DukeofHazardpass' ,
458
+ kid = "some-key-id" )
459
+ jwe = JWE (plain , alg = "A128KW" , enc = "A128CBC-HS256" )
460
+ _jwe = jwe .encrypt (keys = [encryption_key ], kid = "some-key-id" )
461
+ decrypter = factory (_jwe , alg = "A128KW" , enc = "A128CBC-HS256" )
462
+
463
+ _jwenc = decrypter .jwt
464
+ assert _jwenc .b64_protected_header () == _jwenc .b64part [0 ]
465
+ assert _jwenc .b64_encrypted_key () == _jwenc .b64part [1 ]
466
+ assert _jwenc .b64_initialization_vector () == _jwenc .b64part [2 ]
467
+ assert _jwenc .b64_ciphertext () == _jwenc .b64part [3 ]
468
+ assert _jwenc .b64_authentication_tag () == _jwenc .b64part [4 ]
469
+
470
+ assert _jwenc .protected_header () == _jwenc .part [0 ]
471
+ assert _jwenc .encrypted_key () == _jwenc .part [1 ]
472
+ assert _jwenc .initialization_vector () == _jwenc .part [2 ]
473
+ assert _jwenc .ciphertext () == _jwenc .part [3 ]
474
+ assert _jwenc .authentication_tag () == _jwenc .part [4 ]
475
+
476
+
477
+ def test_wrong_key_type ():
478
+ encryption_key = SYMKey (use = "enc" , key = 'DukeofHazardpass' ,
479
+ kid = "some-key-id" )
480
+ jwenc = JWE (plain , alg = "ECDH-ES" , enc = "A128GCM" )
481
+ with pytest .raises (NoSuitableEncryptionKey ):
482
+ jwenc .encrypt ([encryption_key ])
483
+
484
+
485
+ def test_wrong_alg ():
486
+ encryption_key = SYMKey (use = "enc" , key = 'DukeofHazardpass' ,
487
+ kid = "some-key-id" )
488
+ jwe = JWE (plain , alg = "A128KW" , enc = "A128CBC-HS256" )
489
+ _jwe = jwe .encrypt (keys = [encryption_key ], kid = "some-key-id" )
490
+ with pytest .raises (HeaderError ):
491
+ decrypter = factory (_jwe , alg = "A192KW" , enc = "A128CBC-HS256" )
492
+
493
+
494
+ def test_wrong_alg_2 ():
495
+ encryption_key = SYMKey (use = "enc" , key = 'DukeofHazardpass' ,
496
+ kid = "some-key-id" )
497
+ jwe = JWE (plain , alg = "A128KW" , enc = "A128CBC-HS256" )
498
+ _jwe = jwe .encrypt (keys = [encryption_key ], kid = "some-key-id" )
499
+ decrypter = factory (_jwe , alg = "A128KW" , enc = "A128CBC-HS256" )
500
+ with pytest .raises (WrongEncryptionAlgorithm ):
501
+ decrypter .decrypt (_jwe , [encryption_key ], alg = 'A192KW' )
502
+
503
+
504
+ def test_no_key ():
505
+ encryption_key = SYMKey (use = "enc" , key = 'DukeofHazardpass' ,
506
+ kid = "some-key-id" )
507
+ jwe = JWE (plain , alg = "A128KW" , enc = "A128CBC-HS256" )
508
+ _jwe = jwe .encrypt (keys = [encryption_key ], kid = "some-key-id" )
509
+ decrypter = factory (_jwe , alg = "A128KW" , enc = "A128CBC-HS256" )
510
+ with pytest .raises (NoSuitableDecryptionKey ):
511
+ decrypter .decrypt (_jwe , [])
512
+
513
+
514
+ def test_unknown_alg ():
515
+ encryption_key = SYMKey (use = "enc" , key = 'DukeofHazardpass' ,
516
+ kid = "some-key-id" )
517
+ jwenc = JWE (plain , alg = "BCD" , enc = "A128GCM" )
518
+ with pytest .raises (ValueError ):
519
+ jwenc .encrypt ([encryption_key ])
520
+
521
+
522
+ def test_nothing ():
523
+ encryption_key = SYMKey (use = "enc" , key = 'DukeofHazardpass' ,
524
+ kid = "some-key-id" )
525
+
526
+ decrypter = JWE (plain , alg = "A128KW" , enc = "A128CBC-HS256" )
527
+ with pytest .raises (ValueError ):
528
+ decrypter .decrypt (keys = [encryption_key ])
529
+
530
+
531
+ def test_invalid ():
532
+ encryption_key = SYMKey (use = "enc" , key = 'DukeofHazardpass' ,
533
+ kid = "some-key-id" )
534
+
535
+ decrypter = JWE (plain , alg = "A128KW" , enc = "A128CBC-HS256" )
536
+ with pytest .raises (BadSyntax ):
537
+ decrypter .decrypt ('a.b.c.d.e' , keys = [encryption_key ])
0 commit comments