Beispiel #1
0
        private void ResolveAsymmetricAlgorithm(SecurityKey key, string algorithm, bool willCreateSignatures)
        {
            if (key == null)
            {
                throw LogHelper.LogArgumentNullException("key");
            }

            if (string.IsNullOrWhiteSpace(algorithm))
            {
                throw LogHelper.LogArgumentNullException("algorithm");
            }

            _hashAlgorithm = GetHashAlgorithmString(algorithm);
            RsaAlgorithm rsaAlgorithm = Utility.ResolveRsaAlgorithm(key, algorithm, willCreateSignatures);

            if (rsaAlgorithm != null)
            {
                if (rsaAlgorithm.rsaCryptoServiceProvider != null)
                {
                    _rsaCryptoServiceProvider = rsaAlgorithm.rsaCryptoServiceProvider;
                    _disposeRsa = rsaAlgorithm.dispose;
                    return;
                }
                else if (rsaAlgorithm.rsaCryptoServiceProviderProxy != null)
                {
                    _rsaCryptoServiceProviderProxy = rsaAlgorithm.rsaCryptoServiceProviderProxy;
                    _disposeRsa = rsaAlgorithm.dispose;
                    return;
                }
                else
                {
                    throw LogHelper.LogExceptionMessage(new NotSupportedException(LogHelper.FormatInvariant(LogMessages.IDX10641, key)));
                }
            }

            ECDsaAlgorithm ecdsaAlgorithm = Utility.ResolveECDsaAlgorithm(key, algorithm, willCreateSignatures);

            if (ecdsaAlgorithm != null && ecdsaAlgorithm.ecdsaCng != null)
            {
                _ecdsa = ecdsaAlgorithm.ecdsaCng;
                _ecdsa.HashAlgorithm = new CngAlgorithm(_hashAlgorithm);
                _disposeEcdsa        = ecdsaAlgorithm.dispose;
                return;
            }

            throw LogHelper.LogExceptionMessage(new NotSupportedException(LogHelper.FormatInvariant(LogMessages.IDX10641, key)));
        }
Beispiel #2
0
        internal static RsaAlgorithm ResolveRsaAlgorithm(SecurityKey key, string algorithm, bool requirePrivateKey)
        {
            if (key == null)
            {
                return(null);
            }

            var rsaAlgorithm = new RsaAlgorithm();
            var rsaKey       = key as RsaSecurityKey;

            if (rsaKey != null)
            {
                if (rsaKey.Rsa != null)
                {
#if NETSTANDARD1_4
                    rsaAlgorithm.rsa = rsaKey.Rsa;
#else
                    rsaAlgorithm.rsaCryptoServiceProvider = rsaKey.Rsa as RSACryptoServiceProvider;
#endif
                    return(rsaAlgorithm);
                }
                else
                {
#if NETSTANDARD1_4
                    rsaAlgorithm.rsa = RSA.Create();
                    rsaAlgorithm.rsa.ImportParameters(rsaKey.Parameters);
                    rsaAlgorithm.dispose = true;
#else
                    rsaAlgorithm.rsaCryptoServiceProvider = new RSACryptoServiceProvider();
                    (rsaAlgorithm.rsaCryptoServiceProvider as RSA).ImportParameters(rsaKey.Parameters);
                    rsaAlgorithm.dispose = true;
#endif
                }

                return(rsaAlgorithm);
            }

            X509SecurityKey x509Key = key as X509SecurityKey;
            if (x509Key != null)
            {
#if NETSTANDARD1_4
                if (requirePrivateKey)
                {
                    rsaAlgorithm.rsa = x509Key.PrivateKey as RSA;
                }
                else
                {
                    rsaAlgorithm.rsa = x509Key.PublicKey as RSA;
                }
#else
                if (requirePrivateKey)
                {
                    rsaAlgorithm.rsaCryptoServiceProviderProxy = new RSACryptoServiceProviderProxy(x509Key.PrivateKey as RSACryptoServiceProvider);
                }
                else
                {
                    rsaAlgorithm.rsaCryptoServiceProviderProxy = new RSACryptoServiceProviderProxy(x509Key.PublicKey as RSACryptoServiceProvider);
                }
#endif
                return(rsaAlgorithm);
            }

            JsonWebKey webKey = key as JsonWebKey;
            if (webKey != null && webKey.Kty == JsonWebAlgorithmsKeyTypes.RSA)
            {
#if NETSTANDARD1_4
                RSAParameters parameters = webKey.CreateRsaParameters();
                rsaAlgorithm.rsa     = RSA.Create();
                rsaAlgorithm.dispose = true;
                if (rsaAlgorithm.rsa != null)
                {
                    rsaAlgorithm.rsa.ImportParameters(parameters);
                }
#else
                RSAParameters parameters = webKey.CreateRsaParameters();
                rsaAlgorithm.rsaCryptoServiceProvider = new RSACryptoServiceProvider();
                (rsaAlgorithm.rsaCryptoServiceProvider as RSA).ImportParameters(parameters);
#endif
                return(rsaAlgorithm);
            }

            return(null);
        }
Beispiel #3
0
        internal static RsaAlgorithm ResolveRsaAlgorithm(SecurityKey key, string algorithm, bool requirePrivateKey)
        {
            if (key == null)
            {
                return(null);
            }

            if (key is RsaSecurityKey rsaKey)
            {
                if (rsaKey.Rsa != null)
                {
#if NETSTANDARD1_4
                    return(new RsaAlgorithm(rsaKey.Rsa));
#else
                    if (rsaKey.Rsa is RSACryptoServiceProvider rsa)
                    {
                        return(new RsaAlgorithm(new RSACryptoServiceProviderProxy(rsa)));
                    }
                    else if (RsaCngAdapter.IsRsaCngSupported())
                    {
                        return(new RsaAlgorithm(new RsaCngAdapter(rsaKey.Rsa)));
                    }
#endif
                }
                else
                {
#if NETSTANDARD1_4
                    var rsaAlgorithm = new RsaAlgorithm(RSA.Create());
                    rsaAlgorithm.Rsa.ImportParameters(rsaKey.Parameters);
#else
                    var rsaCapiProvider = new RSACryptoServiceProvider();
                    rsaCapiProvider.ImportParameters(rsaKey.Parameters);
                    var rsaAlgorithm = new RsaAlgorithm(new RSACryptoServiceProviderProxy(rsaCapiProvider));
#endif
                    rsaAlgorithm.ShouldDispose = true;
                    return(rsaAlgorithm);
                }
            }

            if (key is X509SecurityKey x509Key)
            {
#if NETSTANDARD1_4
                if (requirePrivateKey)
                {
                    return(new RsaAlgorithm(x509Key.PrivateKey as RSA));
                }
                else
                {
                    return(new RsaAlgorithm(x509Key.PublicKey as RSA));
                }
#else
                if (requirePrivateKey)
                {
                    return(new RsaAlgorithm(new RSACryptoServiceProviderProxy(x509Key.PrivateKey as RSACryptoServiceProvider)));
                }
                else
                {
                    return(new RsaAlgorithm(new RSACryptoServiceProviderProxy(x509Key.PublicKey as RSACryptoServiceProvider)));
                }
#endif
            }

            if (key is JsonWebKey webKey && webKey.Kty == JsonWebAlgorithmsKeyTypes.RSA)
            {
#if NETSTANDARD1_4
                var rsaAlgorithm = new RsaAlgorithm(RSA.Create());
                rsaAlgorithm.Rsa.ImportParameters(webKey.CreateRsaParameters());
#else
                var rsaCapiProvider = new RSACryptoServiceProvider();
                rsaCapiProvider.ImportParameters(webKey.CreateRsaParameters());
                var rsaAlgorithm = new RsaAlgorithm(new RSACryptoServiceProviderProxy(rsaCapiProvider));
#endif
                rsaAlgorithm.ShouldDispose = true;
                return(rsaAlgorithm);
            }

            return(null);
        }