def legacy_encrypt(claims, jwk, adata='', add_header=None, alg='RSA-OAEP', enc='A128CBC-HS256', rng=get_random_bytes, compression=None, version=None): # see https://github.com/Demonware/jose/pull/3/files header = dict((add_header or {}).items() + [('enc', enc), ('alg', alg)]) if version == 1: claims = deepcopy(claims) assert jose._TEMP_VER_KEY not in claims claims[jose._TEMP_VER_KEY] = version # promote the temp key to the header assert jose._TEMP_VER_KEY not in header header[jose._TEMP_VER_KEY] = version plaintext = jose.json_encode(claims) # compress (if required) if compression is not None: header['zip'] = compression try: (compress, _) = jose.COMPRESSION[compression] except KeyError: raise jose.Error( 'Unsupported compression algorithm: {}'.format(compression)) plaintext = compress(plaintext) # body encryption/hash ((cipher, _), key_size), ((hash_fn, _), hash_mod) = jose.JWA[enc] iv = rng(AES.block_size) if version == 1: encryption_key = rng(hash_mod.digest_size) cipher_key = encryption_key[-hash_mod.digest_size / 2:] mac_key = encryption_key[:-hash_mod.digest_size / 2] else: encryption_key = rng((key_size // 8) + hash_mod.digest_size) cipher_key = encryption_key[:-hash_mod.digest_size] mac_key = encryption_key[-hash_mod.digest_size:] ciphertext = cipher(plaintext, cipher_key, iv) hash = hash_fn(jose._jwe_hash_str(ciphertext, iv, adata, version), mac_key, hash_mod) # cek encryption (cipher, _), _ = jose.JWA[alg] encryption_key_ciphertext = cipher(encryption_key, jwk) return jose.JWE( *map(jose.b64encode_url, (jose.json_encode(header), encryption_key_ciphertext, iv, ciphertext, jose.auth_tag(hash))))
def test_decrypt_invalid_compression_error(self): jwe = jose.encrypt(claims, rsa_pub_key, compression='DEF') header = jose.b64encode_url('{"alg": "RSA-OAEP", ' '"enc": "A128CBC-HS256", "zip": "BAD"}') try: jose.decrypt(jose.JWE(*((header,) + (jwe[1:]))), rsa_priv_key) self.fail() except ValueError as e: self.assertEqual(e.message, 'Unsupported compression algorithm: BAD')