11import logging
22
3- from cryptojwt .jwk .jwk import key_from_jwk_dict
43from ..jwk .asym import AsymmetricKey
4+ from ..jwk .ec import ECKey
5+ from ..jwk .hmac import SYMKey
6+ from ..jwk .jwk import key_from_jwk_dict
7+ from ..jwk .rsa import RSAKey
58from ..jwx import JWx
69
710from .exception import DecryptionFailed
@@ -82,14 +85,16 @@ def encrypt(self, keys=None, cek="", iv="", **kwargs):
8285
8386 # Determine Encryption Class by Algorithm
8487 if _alg in ["RSA-OAEP" , "RSA-OAEP-256" , "RSA1_5" ]:
88+ keys = [k for k in keys if isinstance (k , RSAKey )]
8589 encrypter = JWE_RSA (self .msg , ** self ._dict )
8690 elif _alg .startswith ("A" ) and _alg .endswith ("KW" ):
91+ keys = [k for k in keys if isinstance (k , SYMKey )]
8792 encrypter = JWE_SYM (self .msg , ** self ._dict )
8893 elif _alg .startswith ("ECDH-ES" ):
89-
90- # ECDH-ES Requires the Server ECDH-ES Key to be set
94+ keys = [k for k in keys if isinstance (k , ECKey )]
9195 if not keys :
92- raise NoSuitableECDHKey (_alg )
96+ logger .error (KEY_ERR .format (_alg ))
97+ raise NoSuitableEncryptionKey (_alg )
9398
9499 encrypter = JWE_EC (** self ._dict )
95100 cek , encrypted_key , iv , params , eprivk = encrypter .enc_setup (
@@ -100,17 +105,25 @@ def encrypt(self, keys=None, cek="", iv="", **kwargs):
100105 logger .error ("'{}' is not a supported algorithm" .format (_alg ))
101106 raise NotSupportedAlgorithm
102107
108+ if not keys :
109+ logger .error (KEY_ERR .format (_alg ))
110+ raise NoSuitableEncryptionKey (_alg )
111+
103112 if cek :
104113 kwargs ["cek" ] = cek
105114
106115 if iv :
107116 kwargs ["iv" ] = iv
108117
109118 for key in keys :
110- if isinstance (key , AsymmetricKey ):
119+ if isinstance (key , SYMKey ):
120+ _key = key .key
121+ elif isinstance (key , ECKey ):
111122 _key = key .public_key ()
123+ elif isinstance (key , RSAKey ):
124+ _key = key .public_key ()
112125 else :
113- _key = key . key
126+ raise ValueError ( 'Unknown key type' )
114127
115128 if key .kid :
116129 encrypter ["kid" ] = key .kid
0 commit comments