/// <summary> /// Encrypt RestSharp request payloads. /// </summary> /// <param name="request">A RestSharp request object</param> public void InterceptRequest(IRestRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } try { // Check request actually has a payload var bodyParam = request.Parameters.FirstOrDefault(param => param.Type == ParameterType.RequestBody); if (bodyParam == null) { // Nothing to encrypt return; } var payload = bodyParam.Value.ToString(); if (string.IsNullOrEmpty(payload)) { // Nothing to encrypt return; } // Encrypt fields & update headers string encryptedPayload; if (_config.UseHttpHeaders()) { // Generate encryption params and add them as HTTP headers var parameters = FieldLevelEncryptionParams.Generate(_config); UpdateRequestHeader(request, _config.IvHeaderName, parameters.IvValue); UpdateRequestHeader(request, _config.EncryptedKeyHeaderName, parameters.EncryptedKeyValue); UpdateRequestHeader(request, _config.EncryptionCertificateFingerprintHeaderName, _config.EncryptionCertificateFingerprint); UpdateRequestHeader(request, _config.EncryptionKeyFingerprintHeaderName, _config.EncryptionKeyFingerprint); UpdateRequestHeader(request, _config.OaepPaddingDigestAlgorithmHeaderName, parameters.OaepPaddingDigestAlgorithmValue); encryptedPayload = FieldLevelEncryption.EncryptPayload(payload, _config, parameters); } else { // Encryption params will be stored in the payload encryptedPayload = FieldLevelEncryption.EncryptPayload(payload, _config); } // Update body and content length bodyParam.Value = encryptedPayload; UpdateRequestHeader(request, "Content-Length", encryptedPayload.Length); } catch (EncryptionException) { throw; } catch (Exception e) { throw new EncryptionException("Failed to intercept and encrypt request!", e); } }
public void TestGenerate_Nominal() { // GIVEN var config = TestUtils.GetTestFieldLevelEncryptionConfigBuilder().Build(); // WHEN var parameters = FieldLevelEncryptionParams.Generate(config); // THEN Assert.IsNotNull(parameters.IvValue); Assert.IsNotNull(parameters.GetIvBytes()); Assert.IsNotNull(parameters.EncryptedKeyValue); Assert.IsNotNull(parameters.GetSecretKeyBytes()); Assert.AreEqual("SHA256", parameters.OaepPaddingDigestAlgorithmValue); }
/// <summary> /// Encrypt a RestSharp request payload. /// </summary> /// <param name="request">A RestSharp request object</param> /// <param name="payload">The payload to be encrypted</param> /// <returns>The encrypted payload</returns> internal override string EncryptPayload(IRestRequest request, string payload) { // Encrypt fields & update headers string encryptedPayload; if (_config.UseHttpHeaders()) { // Generate encryption params and add them as HTTP headers var parameters = FieldLevelEncryptionParams.Generate(_config); UpdateRequestHeader(request, _config.IvHeaderName, parameters.IvValue); UpdateRequestHeader(request, _config.EncryptedKeyHeaderName, parameters.EncryptedKeyValue); UpdateRequestHeader(request, _config.EncryptionCertificateFingerprintHeaderName, _config.EncryptionCertificateFingerprint); UpdateRequestHeader(request, _config.EncryptionKeyFingerprintHeaderName, _config.EncryptionKeyFingerprint); UpdateRequestHeader(request, _config.OaepPaddingDigestAlgorithmHeaderName, parameters.OaepPaddingDigestAlgorithmValue); encryptedPayload = FieldLevelEncryption.EncryptPayload(payload.ToString(), _config, parameters); } else { // Encryption params will be stored in the payload encryptedPayload = FieldLevelEncryption.EncryptPayload(payload.ToString(), _config); } return(encryptedPayload); }