Exemple #1
0
        /// <summary>
        /// Convert a BouncyCastle AsymmetricCipherKeyPair into an AsymmetricAlgorithm.
        /// </summary>
        /// <remarks>
        /// <para>Converts a BouncyCastle AsymmetricCipherKeyPair into an AsymmetricAlgorithm.</para>
        /// <note type="note">Currently, only RSA and DSA keys are supported.</note>
        /// </remarks>
        /// <returns>The AsymmetricAlgorithm.</returns>
        /// <param name="key">The AsymmetricCipherKeyPair.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="key"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// <paramref name="key"/> is an unsupported asymmetric algorithm.
        /// </exception>
        public static AsymmetricAlgorithm AsAsymmetricAlgorithm(this AsymmetricCipherKeyPair key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (key.Private is RsaPrivateCrtKeyParameters rsaPrivateKey)
            {
                return(GetAsymmetricAlgorithm(rsaPrivateKey));
            }

            if (key.Private is DsaPrivateKeyParameters dsaPrivateKey)
            {
                return(GetAsymmetricAlgorithm(dsaPrivateKey, (DsaPublicKeyParameters)key.Public));
            }

            throw new NotSupportedException(string.Format("{0} is currently not supported.", key.GetType().Name));
        }
        /// <summary>
        /// Convert a BouncyCastle AsymmetricCipherKeyPair into an AsymmetricAlgorithm.
        /// </summary>
        /// <remarks>
        /// <para>Converts a BouncyCastle AsymmetricCipherKeyPair into an AsymmetricAlgorithm.</para>
        /// <note type="note">Currently, only RSA and DSA keys are supported.</note>
        /// </remarks>
        /// <returns>The AsymmetricAlgorithm.</returns>
        /// <param name="key">The AsymmetricCipherKeyPair.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="key"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// <paramref name="key"/> is an unsupported asymmetric algorithm.
        /// </exception>
        public static AsymmetricAlgorithm AsAsymmetricAlgorithm(this AsymmetricCipherKeyPair key)
        {
            // TODO: Drop this API - it's no longer needed. The WindowsSecureMimeContext now exports the certificate & key into a pkcs12 and then loads that into an X509Certificate2.
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (key.Private is RsaPrivateCrtKeyParameters rsaPrivateKey)
            {
                return(GetAsymmetricAlgorithm(rsaPrivateKey));
            }

            if (key.Private is DsaPrivateKeyParameters dsaPrivateKey)
            {
                return(GetAsymmetricAlgorithm(dsaPrivateKey, (DsaPublicKeyParameters)key.Public));
            }

            throw new NotSupportedException(string.Format("{0} is currently not supported.", key.GetType().Name));
        }