def test_authentication_tag_not_128_bits(self): encoder = Encoder(*self.encoder_args) jwe = encoder.encrypt_token(VALID_SIGNED_JWT.encode(), tag=os.urandom(10)) self.assertInDecryptException( jwe.decode(), "'Authentication tag must be 16 bytes or longer")
def test_authentication_tag_corrupted(self): encoder = Encoder() jwe = encoder.encrypt(VALID_SIGNED_JWT.encode(), tag=b'adssadsadsadsadasdasdasads') decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode())
def test_missing_algorithm(self): jwe_protected_header = b'{"enc":"A256GCM"}' encoder = Encoder(*self.encoder_args) jwe = encoder.encrypt_token( VALID_SIGNED_JWT.encode(), jwe_protected_header=encoder._base_64_encode(jwe_protected_header)) # pylint: disable=protected-access self.assertInDecryptException(jwe.decode(), "Missing Algorithm")
def test_iv_not_96_bits(self): iv = os.urandom(45) encoder = Encoder(*self.encoder_args) encoder.iv = iv jwe = encoder.encrypt_token(VALID_SIGNED_JWT.encode()) self.assertInDecryptException(jwe.decode(), "IV incorrect length")
def test_cek_not_256_bits(self): cek = os.urandom(24) encoder = Encoder(*self.encoder_args) encoder.cek = cek jwe = encoder.encrypt_token(VALID_SIGNED_JWT.encode()) self.assertInDecryptException(jwe.decode(), "CEK incorrect length")
def test_jwe_header_only_contains_alg_and_enc(self): jwe_protected_header = b'{"alg":"RSA-OAEP","enc":"A256GCM", "test":"test"}' encoder = Encoder(*self.encoder_args) jwe = encoder.encrypt_token( VALID_SIGNED_JWT.encode(), jwe_protected_header=encoder._base_64_encode(jwe_protected_header)) # pylint: disable=protected-access self.assertInDecryptException(jwe.decode(), "InvalidTag")
def test_authentication_tag_corrupted(self): encoder = Encoder() jwe = encoder.encrypt_token(VALID_SIGNED_JWT.encode(), tag=b'adssadsadsadsadasdasdasads') decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException): decoder.decrypt_jwt_token(jwe.decode())
def test_authentication_tag_not_128_bits(self): encoder = Encoder() jwe = encoder.encrypt(VALID_SIGNED_JWT.encode(), tag=os.urandom(10)) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("'Authentication tag must be 16 bytes or longer", ite.exception.value)
def test_invalid_enc(self): jwe_protected_header = b'{"alg":"RSA-OAEP","enc":"A128GCM"}' encoder = Encoder(*self.encoder_args) jwe = encoder.encrypt_token( VALID_SIGNED_JWT.encode(), jwe_protected_header=encoder._base_64_encode(jwe_protected_header)) # pylint: disable=protected-access self.assertInDecryptException(jwe.decode(), "Invalid Encoding")
def test_invalid_algorithm(self): jwe_protected_header = b'{"alg":"PBES2_HS256_A128KW","enc":"A256GCM"}' encoder = Encoder(*self.encoder_args) jwe = encoder.encrypt_token( VALID_SIGNED_JWT.encode(), jwe_protected_header=encoder._base_64_encode(jwe_protected_header)) # pylint: disable=protected-access self.assertInDecryptException(jwe.decode(), "Invalid Algorithm")
def test_jwe_header_only_contains_alg_and_enc(self): jwe_protected_header = b'{"alg":"RSA-OAEP","enc":"A256GCM", "test":"test"}' encoder = Encoder() jwe = encoder.encrypt(VALID_SIGNED_JWT.encode(), jwe_protected_header=encoder._base_64_encode(jwe_protected_header)) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("InvalidTag", ite.exception.value)
def test_invalid_algorithm(self): jwe_protected_header = b'{"alg":"PBES2_HS256_A128KW","enc":"A256GCM"}' encoder = Encoder() jwe = encoder.encrypt(VALID_SIGNED_JWT.encode(), jwe_protected_header=encoder._base_64_encode(jwe_protected_header)) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("Invalid Algorithm", ite.exception.value)
def test_iv_not_96_bits(self): iv = os.urandom(45) encoder = Encoder() jwe = encoder.encrypt(VALID_SIGNED_JWT.encode(), iv=iv) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("IV incorrect length", ite.exception.value)
def test_enc_missing(self): jwe_protected_header = b'{"alg":"RSA-OAEP"}' encoder = Encoder() jwe = encoder.encrypt(VALID_SIGNED_JWT.encode(), jwe_protected_header=encoder._base_64_encode(jwe_protected_header)) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("Missing Encoding", ite.exception.value)
def test_cek_not_256_bits(self): cek = os.urandom(24) encoder = Encoder() jwe = encoder.encrypt(VALID_SIGNED_JWT.encode(), cek=cek) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("CEK incorrect length", ite.exception.value)
def test_authentication_tag_not_128_bits(self): encoder = Encoder() jwe = encoder.encrypt_token(VALID_SIGNED_JWT.encode(), tag=os.urandom(10)) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("'Authentication tag must be 16 bytes or longer", ite.exception.value)
def test_jwe_header_only_contains_alg_and_enc(self): jwe_protected_header = b'{"alg":"RSA-OAEP","enc":"A256GCM", "test":"test"}' encoder = Encoder() jwe = encoder.encrypt_token( VALID_SIGNED_JWT.encode(), jwe_protected_header=encoder._base_64_encode(jwe_protected_header)) # pylint: disable=protected-access decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("InvalidTag", ite.exception.value)
def test_invalid_algorithm(self): jwe_protected_header = b'{"alg":"PBES2_HS256_A128KW","enc":"A256GCM"}' encoder = Encoder() jwe = encoder.encrypt_token( VALID_SIGNED_JWT.encode(), jwe_protected_header=encoder._base_64_encode(jwe_protected_header)) # pylint: disable=protected-access decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("Invalid Algorithm", ite.exception.value)
def test_iv_not_96_bits(self): iv = os.urandom(45) encoder = Encoder() encoder.iv = iv jwe = encoder.encrypt_token(VALID_SIGNED_JWT.encode()) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("IV incorrect length", ite.exception.value)
def test_cek_not_256_bits(self): cek = os.urandom(24) encoder = Encoder() encoder.cek = cek jwe = encoder.encrypt_token(VALID_SIGNED_JWT.encode()) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("CEK incorrect length", ite.exception.value)
def test_jwe_key_not_2048_bits(self): cek = os.urandom(32) encoder = Encoder(*self.encoder_args) encoder.cek = cek encrypted_key = encoder._encrypted_key(cek) # pylint: disable=protected-access encrypted_key = encrypted_key[0:len(encrypted_key) - 2] jwe = encoder.encrypt_token(VALID_SIGNED_JWT.encode(), encrypted_key=encrypted_key) self.assertInDecryptException(jwe.decode(), "ValueError")
def test_enc_missing(self): jwe_protected_header = b'{"alg":"RSA-OAEP"}' encoder = Encoder() jwe = encoder.encrypt_token( VALID_SIGNED_JWT.encode(), jwe_protected_header=encoder._base_64_encode(jwe_protected_header)) # pylint: disable=protected-access decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("Missing Encoding", ite.exception.value)
def test_jwe_key_not_2048_bits(self): cek = os.urandom(32) encoder = Encoder() encrypted_key = encoder._encrypted_key(cek) encrypted_key = encrypted_key[0:len(encrypted_key) - 2] jwe = encoder.encrypt(VALID_SIGNED_JWT.encode(), cek=cek, encrypted_key=encrypted_key) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("ValueError", ite.exception.value)
def test_jwe_key_not_2048_bits(self): cek = os.urandom(32) encoder = Encoder() encoder.cek = cek encrypted_key = encoder._encrypted_key(cek) # pylint: disable=protected-access encrypted_key = encrypted_key[0:len(encrypted_key) - 2] jwe = encoder.encrypt_token(VALID_SIGNED_JWT.encode(), encrypted_key=encrypted_key) decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(jwe.decode()) self.assertIn("ValueError", ite.exception.value)
def test_cipher_text_corrupted(self): encoder = Encoder() jwe = encoder.encrypt_token(VALID_SIGNED_JWT.encode()) tokens = jwe.decode().split('.') jwe_protected_header = tokens[0] encrypted_key = tokens[1] encoded_iv = tokens[2] encoded_cipher_text = tokens[3] encoded_tag = tokens[4] corrupted_cipher = encoded_cipher_text[0:len(encoded_cipher_text) - 1] reassembled = jwe_protected_header + "." + encrypted_key + "." + encoded_iv + "." + corrupted_cipher + "." + encoded_tag decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException): decoder.decrypt_jwt_token(reassembled)
def test_cipher_text_corrupted(self): encoder = Encoder() jwe = encoder.encrypt(VALID_SIGNED_JWT.encode()) tokens = jwe.decode().split('.') jwe_protected_header = tokens[0] encrypted_key = tokens[1] encoded_iv = tokens[2] encoded_cipher_text = tokens[3] encoded_tag = tokens[4] corrupted_cipher = encoded_cipher_text[0:len(encoded_cipher_text) - 1] reassembled = jwe_protected_header + "." + encrypted_key + "." + encoded_iv + "." + corrupted_cipher + "." + encoded_tag decoder = JWTDecryptor() with self.assertRaises(InvalidTokenException) as ite: decoder.decrypt_jwt_token(reassembled)