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 ECDsaAlgorithm ResolveECDsaAlgorithm(SecurityKey key, string algorithm, bool usePrivateKey)
        {
            if (key == null)
            {
                return(null);
            }

            var ecdsaAlgorithm = new ECDsaAlgorithm();
            var ecdsaKey       = key as ECDsaSecurityKey;

            if (ecdsaKey != null)
            {
#if NETSTANDARD1_4
                if (ecdsaKey.ECDsa != null && ValidateECDSAKeySize(ecdsaKey.ECDsa.KeySize, algorithm))
                {
                    ecdsaAlgorithm.ecdsa = ecdsaKey.ECDsa;
                    return(ecdsaAlgorithm);
                }
#else // net451 windows
                if (ecdsaKey.ECDsa != null && ValidateECDSAKeySize(ecdsaKey.ECDsa.KeySize, algorithm))
                {
                    ecdsaAlgorithm.ecdsaCng = ecdsaKey.ECDsa as ECDsaCng;
                    return(ecdsaAlgorithm);
                }
#endif
            }

            var webKey = key as JsonWebKey;
            if (webKey != null && webKey.Kty == JsonWebAlgorithmsKeyTypes.EllipticCurve)
            {
                ecdsaAlgorithm.dispose = true;
#if NETSTANDARD1_4
                ecdsaAlgorithm.ecdsa = webKey.CreateECDsa(algorithm, usePrivateKey);
#else // net451 windows
                ecdsaAlgorithm.ecdsaCng = webKey.CreateECDsa(algorithm, usePrivateKey);
#endif
                return(ecdsaAlgorithm);
            }

            return(null);
        }