internal byte[] GetIvBytes()
 {
     try
     {
         if (IvBytes != null)
         {
             return(IvBytes);
         }
         // Decode the IV
         IvBytes = EncodingUtils.DecodeValue(IvValue, Config.ValueEncoding);
         return(IvBytes);
     }
     catch (Exception e)
     {
         throw new EncryptionException("Failed to decode the provided IV value!", e);
     }
 }
 internal byte[] GetSecretKeyBytes()
 {
     try
     {
         if (SecretKeyBytes != null)
         {
             return(SecretKeyBytes);
         }
         // Decrypt the AES secret key
         var encryptedSecretKeyBytes = EncodingUtils.DecodeValue(EncryptedKeyValue, Config.ValueEncoding);
         SecretKeyBytes = RsaEncryption.UnwrapSecretKey(Config, encryptedSecretKeyBytes, OaepPaddingDigestAlgorithmValue);
         return(SecretKeyBytes);
     }
     catch (Exception e)
     {
         throw new EncryptionException("Failed to decode and unwrap the provided secret key value!", e);
     }
 }
Example #3
0
        private static JToken DecryptPayloadPath(JToken payloadToken, string jsonPathIn, string jsonPathOut,
                                                 FieldLevelEncryptionConfig config, FieldLevelEncryptionParams parameters)
        {
            if (payloadToken == null)
            {
                throw new ArgumentNullException(nameof(payloadToken));
            }
            if (jsonPathIn == null)
            {
                throw new ArgumentNullException(nameof(jsonPathIn));
            }
            if (jsonPathOut == null)
            {
                throw new ArgumentNullException(nameof(jsonPathOut));
            }

            var inJsonToken = payloadToken.SelectToken(jsonPathIn);

            if (inJsonToken == null)
            {
                // Nothing to decrypt
                return(payloadToken);
            }

            // Read and remove encrypted data and encryption fields at the given JSON path
            JsonUtils.AssertIsObject(inJsonToken, jsonPathIn);
            var encryptedValueJsonToken = ReadAndDeleteJsonKey(inJsonToken, config.EncryptedValueFieldName);

            if (IsNullOrEmptyJson(encryptedValueJsonToken))
            {
                // Nothing to decrypt
                return(payloadToken);
            }

            if (!config.UseHttpPayloads() && parameters == null)
            {
                throw new InvalidOperationException("Encryption params have to be set when not stored in HTTP payloads!");
            }

            if (parameters == null)
            {
                // Read encryption params from the payload
                var oaepDigestAlgorithmJsonToken = ReadAndDeleteJsonKey(inJsonToken, config.OaepPaddingDigestAlgorithmFieldName);
                var oaepDigestAlgorithm          = IsNullOrEmptyJson(oaepDigestAlgorithmJsonToken) ? config.OaepPaddingDigestAlgorithm : oaepDigestAlgorithmJsonToken;
                var encryptedKeyJsonToken        = ReadAndDeleteJsonKey(inJsonToken, config.EncryptedKeyFieldName);
                var ivJsonToken = ReadAndDeleteJsonKey(inJsonToken, config.IvFieldName);
                ReadAndDeleteJsonKey(inJsonToken, config.EncryptionCertificateFingerprintFieldName);
                ReadAndDeleteJsonKey(inJsonToken, config.EncryptionKeyFingerprintFieldName);
                parameters = new FieldLevelEncryptionParams(config, ivJsonToken, encryptedKeyJsonToken, oaepDigestAlgorithm);
            }

            // Decrypt data
            var encryptedValueBytes = EncodingUtils.DecodeValue(encryptedValueJsonToken, config.ValueEncoding);
            var decryptedValueBytes = DecryptBytes(parameters.GetSecretKeyBytes(), parameters.GetIvBytes(), encryptedValueBytes);

            // Add decrypted data at the given JSON path
            var decryptedValue = JsonUtils.SanitizeJson(Encoding.UTF8.GetString(decryptedValueBytes));

            if ("$".Equals(jsonPathOut))
            {
                // The decrypted JSON is the new body
                return(JToken.Parse(decryptedValue));
            }
            else
            {
                JsonUtils.CheckOrCreateOutObject(payloadToken, jsonPathOut);
                JsonUtils.AddDecryptedDataToPayload(payloadToken, decryptedValue, jsonPathOut);

                // Remove the input if now empty
                inJsonToken = payloadToken.SelectToken(jsonPathIn);
                if (inJsonToken.Type == JTokenType.Object && !inJsonToken.HasValues)
                {
                    inJsonToken.Parent.Remove();
                }
            }

            return(payloadToken);
        }