public override DecryptResult Decrypt(DecryptOptions options, CancellationToken cancellationToken)
        {
            Argument.AssertNotNull(options, nameof(options));

            if (MustRemote)
            {
                // A private key is required to decrypt. Send to the server.
                KeysEventSource.Singleton.PrivateKeyRequired(nameof(Decrypt));
                return(null);
            }

            EncryptionAlgorithm  algorithm = options.Algorithm;
            RSAEncryptionPadding padding   = algorithm.GetRsaEncryptionPadding();

            if (padding is null)
            {
                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Decrypt), algorithm);
                return(null);
            }

            byte[]        plaintext = Decrypt(options.Ciphertext, padding);
            DecryptResult result    = null;

            if (plaintext != null)
            {
                result = new DecryptResult
                {
                    Algorithm = algorithm,
                    KeyId     = KeyMaterial.Id,
                    Plaintext = plaintext,
                };
            }

            return(result);
        }
Esempio n. 2
0
        public override DecryptResult Decrypt(DecryptOptions options, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(options, nameof(options));

            ThrowIfTimeInvalid();

            EncryptionAlgorithm algorithm = options.Algorithm;

            if (algorithm.GetAesCbcEncryptionAlgorithm() is AesCbc aesCbc)
            {
                using ICryptoTransform decryptor = aesCbc.CreateDecryptor(KeyMaterial.K, options.Iv);

                byte[] ciphertext = options.Ciphertext;
                byte[] plaintext  = decryptor.TransformFinalBlock(ciphertext, 0, ciphertext.Length);

                return(new DecryptResult
                {
                    Algorithm = algorithm,
                    KeyId = KeyMaterial.Id,
                    Plaintext = plaintext,
                });
            }
            else
            {
                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Decrypt), algorithm);
                return(null);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Decrypts the specified ciphertext.
        /// </summary>
        /// <param name="options">A <see cref="DecryptOptions"/> containing the data to decrypt and other options for algorithm-dependent decryption.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param>
        /// <returns>
        /// The result of the decrypt operation. The returned <see cref="DecryptResult"/> contains the encrypted data
        /// along with information regarding the algorithm and key used to decrypt it.
        /// </returns>
        /// <exception cref="ArgumentException">The specified algorithm does not match the key corresponding to the key identifier.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="options"/> is null.</exception>
        /// <exception cref="CryptographicException">The local cryptographic provider threw an exception.</exception>
        /// <exception cref="InvalidOperationException">The key is invalid for the current operation.</exception>
        /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception>
        public virtual DecryptResult Decrypt(DecryptOptions options, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(options, nameof(options));

            DecryptResult result = null;

            if (_provider.SupportsOperation(KeyOperation.Decrypt))
            {
                result = _provider.Decrypt(options, cancellationToken);
            }

            return(result ?? throw LocalCryptographyProvider.CreateOperationNotSupported(nameof(KeyOperation.Decrypt)));
        }
Esempio n. 4
0
        /// <summary>
        /// Decrypts ciphertext.
        /// </summary>
        /// <param name="options">A <see cref="DecryptOptions"/> containing the data to decrypt and other options for algorithm-dependent decryption.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param>
        /// <returns>
        /// The result of the decrypt operation. The returned <see cref="DecryptResult"/> contains the encrypted data
        /// along with information regarding the algorithm and key used to decrypt it.
        /// </returns>
        /// <exception cref="ArgumentException">The specified algorithm does not match the key corresponding to the key identifier.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="options"/> is null.</exception>
        /// <exception cref="CryptographicException">The local cryptographic provider threw an exception.</exception>
        /// <exception cref="InvalidOperationException">The key is invalid for the current operation.</exception>
        /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception>
        public virtual async Task <DecryptResult> DecryptAsync(DecryptOptions options, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(options, nameof(options));

            DecryptResult result = null;

            if (_provider.SupportsOperation(KeyOperation.Decrypt))
            {
                result = await _provider.DecryptAsync(options, cancellationToken).ConfigureAwait(false);
            }

            return(result ?? throw LocalCryptographyProvider.CreateOperationNotSupported(nameof(KeyOperation.Decrypt)));
        }
Esempio n. 5
0
        public virtual async Task <Response <DecryptResult> > DecryptAsync(DecryptOptions options, CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(Decrypt)}");
            scope.AddAttribute("key", _keyId);
            scope.Start();

            try
            {
                return(await Pipeline.SendRequestAsync(RequestMethod.Post, options, () => new DecryptResult { Algorithm = options.Algorithm }, cancellationToken, "/decrypt").ConfigureAwait(false));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
        public virtual Task <DecryptResult> DecryptAsync(DecryptOptions options, CancellationToken cancellationToken = default)
        {
            DecryptResult result = Decrypt(options, cancellationToken);

            return(Task.FromResult(result));
        }
 public virtual DecryptResult Decrypt(DecryptOptions options, CancellationToken cancellationToken = default)
 {
     throw CreateOperationNotSupported(nameof(Decrypt));
 }
Esempio n. 8
0
 DecryptResult ICryptographyProvider.Decrypt(DecryptOptions options, CancellationToken cancellationToken)
 {
     return(Decrypt(options, cancellationToken));
 }
Esempio n. 9
0
 async Task <DecryptResult> ICryptographyProvider.DecryptAsync(DecryptOptions options, CancellationToken cancellationToken)
 {
     return(await DecryptAsync(options, cancellationToken).ConfigureAwait(false));
 }