Esempio n. 1
0
        private Func <UpdateFileInfo, string?>?CreateFileSignatureFactory(AsymmetricKey key, string?keyPassword)
        {
            if (key.PrivateKey == null)
            {
                return(file =>
                {
                    if (string.IsNullOrEmpty(file.Signature))
                    {
                        SetError(new FieldValidationError("Files", "As the private key is not stored on the server, a signature has to be provided."));
                        return null;
                    }

                    if (!_asymmetricCryptoHandler.VerifyHash(file.Hash, file.Signature, key.PublicKey))
                    {
                        SetError(new FieldValidationError("Files", $"Invalid signature. The signature of file {file.Path} ({file.Hash}) could not be verified."));
                        return null;
                    }

                    return file.Signature !;
                });
            }
            else if (key.IsPrivateKeyEncrypted)
            {
                if (keyPassword == null)
                {
                    SetError(new FieldValidationError("KeyPassword", "The private key is encrypted and no key password was submitted."));
                    return(null);
                }

                string privateKey;
                try
                {
                    privateKey = _symmetricEncryption.DecryptString(key.PrivateKey, keyPassword);
                }
                catch (Exception)
                {
                    SetError(new FieldValidationError("KeyPassword", "An error occurred on decrypting private key.", ErrorCode.InvalidKeyPassword));
                    return(null);
                }

                return(file => _asymmetricCryptoHandler.SignHash(file.Hash, privateKey));
            }
            else
            {
                return(file => _asymmetricCryptoHandler.SignHash(file.Hash, key.PrivateKey));
            }
        }
Esempio n. 2
0
        private static object DecryptPropertyValue(ORMappingItem item, object originalData)
        {
            object result = originalData;

            if (originalData is string)
            {
                string stringValue = (string)originalData;

                if (stringValue.IsNotEmpty())
                {
                    try
                    {
                        ISymmetricEncryption encryptor = ORMappingItemEncryptionHelper.GetEncryptor(item.EncryptorName);
                        result = encryptor.DecryptString(stringValue.ToBase16Bytes());
                    }
                    catch (System.FormatException)
                    {
                    }
                }
            }

            return(result);
        }