|
| 1 | +import json |
| 2 | +import copy |
| 3 | +from Crypto.Cipher import AES |
| 4 | +from Crypto.Util.Padding import pad, unpad |
| 5 | +from client_encryption.session_key_params import SessionKeyParams |
| 6 | +from client_encryption.encoding_utils import encode_bytes, decode_value |
| 7 | +from client_encryption.json_path_utils import get_node, pop_node, update_node, cleanup_node |
| 8 | +from client_encryption.encryption_exception import EncryptionError |
| 9 | + |
| 10 | + |
| 11 | +def encrypt_payload(payload, config, _params=None): |
| 12 | + """Encrypt some fields of a JSON payload using the given configuration.""" |
| 13 | + |
| 14 | + try: |
| 15 | + if type(payload) is str: |
| 16 | + json_payload = json.loads(payload) |
| 17 | + else: |
| 18 | + json_payload = copy.deepcopy(payload) |
| 19 | + |
| 20 | + for elem, target in config.paths["$"].to_encrypt.items(): |
| 21 | + if not _params: |
| 22 | + params = SessionKeyParams.generate(config) |
| 23 | + else: |
| 24 | + params = _params |
| 25 | + |
| 26 | + try: |
| 27 | + value = pop_node(json_payload, elem) |
| 28 | + |
| 29 | + try: |
| 30 | + encrypted_value = _encrypt_value(params.key, params.iv_spec, value) |
| 31 | + crypto_node = get_node(json_payload, target, create=True) |
| 32 | + crypto_node[config.encrypted_value_field_name] = encode_bytes(encrypted_value, config.data_encoding) |
| 33 | + |
| 34 | + if not _params: |
| 35 | + _populate_node_with_key_params(crypto_node, config, params) |
| 36 | + |
| 37 | + except KeyError: |
| 38 | + raise EncryptionError("Field " + target + " not found!") |
| 39 | + |
| 40 | + except KeyError: |
| 41 | + pass # data-to-encrypt node not found, nothing to encrypt |
| 42 | + |
| 43 | + return json_payload |
| 44 | + |
| 45 | + except (IOError, ValueError, TypeError) as e: |
| 46 | + raise EncryptionError("Payload encryption failed!", e) |
| 47 | + |
| 48 | + |
| 49 | +def decrypt_payload(payload, config, _params=None): |
| 50 | + """Decrypt some fields of a JSON payload using the given configuration.""" |
| 51 | + |
| 52 | + try: |
| 53 | + if type(payload) is str: |
| 54 | + json_payload = json.loads(payload) |
| 55 | + else: |
| 56 | + json_payload = payload |
| 57 | + |
| 58 | + for elem, target in config.paths["$"].to_decrypt.items(): |
| 59 | + try: |
| 60 | + node = get_node(json_payload, elem) |
| 61 | + |
| 62 | + cipher_text = decode_value(node.pop(config.encrypted_value_field_name), config.data_encoding) |
| 63 | + |
| 64 | + if not _params: |
| 65 | + try: |
| 66 | + encrypted_key = node.pop(config.encrypted_key_field_name) |
| 67 | + iv = node.pop(config.iv_field_name) |
| 68 | + except KeyError: |
| 69 | + raise EncryptionError("Encryption field(s) missing in payload.") |
| 70 | + |
| 71 | + oaep_digest_algo = node.pop(config.oaep_padding_digest_algorithm_field_name, |
| 72 | + config.oaep_padding_digest_algorithm) |
| 73 | + |
| 74 | + _remove_fingerprint_from_node(node, config) |
| 75 | + |
| 76 | + params = SessionKeyParams(config, encrypted_key, iv, oaep_digest_algo) |
| 77 | + else: |
| 78 | + params = _params |
| 79 | + |
| 80 | + cleanup_node(json_payload, elem) |
| 81 | + |
| 82 | + try: |
| 83 | + update_node(json_payload, target, _decrypt_bytes(params.key, params.iv_spec, cipher_text)) |
| 84 | + except KeyError: |
| 85 | + raise EncryptionError("Field '" + target + "' not found!") |
| 86 | + |
| 87 | + except KeyError: |
| 88 | + pass # encrypted data node not found, nothing to decrypt |
| 89 | + |
| 90 | + return json_payload |
| 91 | + |
| 92 | + except (IOError, ValueError, TypeError) as e: |
| 93 | + raise EncryptionError("Payload encryption failed!", e) |
| 94 | + |
| 95 | + |
| 96 | +def _encrypt_value(_key, iv, node_str): |
| 97 | + padded_node = pad(node_str.encode('utf-8'), AES.block_size) |
| 98 | + |
| 99 | + aes = AES.new(_key, AES.MODE_CBC, iv) |
| 100 | + return aes.encrypt(padded_node) |
| 101 | + |
| 102 | + |
| 103 | +def _decrypt_bytes(_key, iv, _bytes): |
| 104 | + aes = AES.new(_key, AES.MODE_CBC, iv) |
| 105 | + plain_bytes = aes.decrypt(_bytes) |
| 106 | + |
| 107 | + return unpad(plain_bytes, AES.block_size).decode('utf-8') |
| 108 | + |
| 109 | + |
| 110 | +def _populate_node_with_key_params(node, config, params): |
| 111 | + node[config.encrypted_key_field_name] = params.encrypted_key_value |
| 112 | + node[config.iv_field_name] = params.iv_value |
| 113 | + if config.oaep_padding_digest_algorithm_field_name: |
| 114 | + node[config.oaep_padding_digest_algorithm_field_name] = params.oaep_padding_digest_algorithm_value |
| 115 | + if config.encryption_certificate_fingerprint_field_name: |
| 116 | + node[config.encryption_certificate_fingerprint_field_name] = config.encryption_certificate_fingerprint |
| 117 | + if config.encryption_key_fingerprint_field_name: |
| 118 | + node[config.encryption_key_fingerprint_field_name] = config.encryption_key_fingerprint |
| 119 | + |
| 120 | + |
| 121 | +def _remove_fingerprint_from_node(node, config): |
| 122 | + if config.encryption_certificate_fingerprint_field_name in node: |
| 123 | + del node[config.encryption_certificate_fingerprint_field_name] |
| 124 | + if config.encryption_key_fingerprint_field_name in node: |
| 125 | + del node[config.encryption_key_fingerprint_field_name] |
| 126 | + |
0 commit comments