/// <summary> /// Initializes a new instance of the <see cref="AsymmetricSignatureProvider"/> class used to create and verify signatures. /// </summary> /// <param name="key">The <see cref="SecurityKey"/> that will be used for signature operations.</param> /// <param name="algorithm">The signature algorithm to apply.</param> /// <param name="willCreateSignatures">If this <see cref="AsymmetricSignatureProvider"/> is required to create signatures then set this to true.</param> /// <para> /// Creating signatures requires that the <see cref="SecurityKey"/> has access to a private key. /// Verifying signatures (the default), does not require access to the private key. /// </para> /// <exception cref="ArgumentNullException"><paramref name="key"/>is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="algorithm"/>is null or empty.</exception> /// <exception cref="InvalidOperationException"><paramref name="willCreateSignatures"/>is true and there is no private key.</exception> /// <exception cref="NotSupportedException">If <see cref="SecurityKey"/> and algorithm pair are not supported.</exception> /// <exception cref="ArgumentOutOfRangeException"> /// willCreateSignatures is true and <see cref="SecurityKey"/>.KeySize is less than the size corresponding to the given algorithm in <see cref="AsymmetricSignatureProvider.MinimumAsymmetricKeySizeInBitsForSigningMap"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <see cref="SecurityKey"/>.KeySize is less than the size corresponding to the algorithm in <see cref="AsymmetricSignatureProvider.MinimumAsymmetricKeySizeInBitsForVerifyingMap"/>. Note: this is always checked. /// </exception> /// <exception cref="InvalidOperationException">If the runtime is unable to create a suitable cryptographic provider.</exception> public AsymmetricSignatureProvider(SecurityKey key, string algorithm, bool willCreateSignatures) : base(key, algorithm) { _cryptoProviderFactory = key.CryptoProviderFactory; _minimumAsymmetricKeySizeInBitsForSigningMap = new Dictionary <string, int>(DefaultMinimumAsymmetricKeySizeInBitsForSigningMap); _minimumAsymmetricKeySizeInBitsForVerifyingMap = new Dictionary <string, int>(DefaultMinimumAsymmetricKeySizeInBitsForVerifyingMap); if (willCreateSignatures && FoundPrivateKey(key) == PrivateKeyStatus.DoesNotExist) { throw LogHelper.LogExceptionMessage(new InvalidOperationException(LogHelper.FormatInvariant(LogMessages.IDX10638, key))); } if (!_cryptoProviderFactory.IsSupportedAlgorithm(algorithm, key)) { throw LogHelper.LogExceptionMessage(new NotSupportedException(LogHelper.FormatInvariant(LogMessages.IDX10634, (algorithm ?? "null"), key))); } ValidateAsymmetricSecurityKeySize(key, algorithm, willCreateSignatures); _asymmetricAdapter = ResolveAsymmetricAdapter(key, algorithm, willCreateSignatures); WillCreateSignatures = willCreateSignatures; }
/// <summary> /// Initializes a new instance of <see cref="RsaKeyWrapProvider"/> used for wrapping and un-wrappping keys. /// These keys are usually symmetric session keys that are wrapped using the recipients public key. /// <param name="key">The <see cref="SecurityKey"/> that will be used for cryptographic operations.</param> /// <param name="algorithm">The KeyWrap algorithm to apply.</param> /// <param name="willUnwrap">Whether this <see cref="RsaKeyWrapProvider"/> is required to un-wrap keys. If true, the private key is required.</param> /// <exception cref="ArgumentNullException">'key' is null.</exception> /// <exception cref="ArgumentNullException">'algorithm' is null.</exception> /// <exception cref="ArgumentException">The key size doesn't match the algorithm.</exception> /// <exception cref="ArgumentException">If <see cref="SecurityKey"/> and algorithm pair are not supported.</exception> /// <exception cref="NotSupportedException">Failed to create RSA algorithm with provided key and algorithm.</exception> /// </summary> public RsaKeyWrapProvider(SecurityKey key, string algorithm, bool willUnwrap) { if (key == null) { throw LogHelper.LogArgumentNullException(nameof(key)); } if (string.IsNullOrEmpty(algorithm)) { throw LogHelper.LogArgumentNullException(nameof(algorithm)); } if (!IsSupportedAlgorithm(key, algorithm)) { throw LogHelper.LogExceptionMessage(new NotSupportedException(LogHelper.FormatInvariant(LogMessages.IDX10661, algorithm, key))); } Algorithm = algorithm; Key = key; _asymmetricAdapter = new AsymmetricAdapter(key, algorithm, willUnwrap); }