/// <summary>Gets a value that indicates whether the specified algorithm is supported by this class. </summary>
        /// <param name="algorithm">The cryptographic algorithm.</param>
        /// <returns>
        /// <see langword="true" /> when the specified algorithm is <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigDSAUrl" />, <see cref="F:System.Security.Cryptography.Xml.EncryptedXml.XmlEncRSA15Url" />, <see cref="F:System.Security.Cryptography.Xml.EncryptedXml.XmlEncRSAOAEPUrl" />, <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigRSASHA1Url" />, or <see cref="F:System.IdentityModel.Tokens.SecurityAlgorithms.RsaSha256Signature" /> and the public key is of the right type; otherwise, <see langword="false" />. See the remarks for details.</returns>
        public override bool IsSupportedAlgorithm(string algorithm)
        {
            if (string.IsNullOrEmpty(algorithm))
            {
                throw new ArgumentNullException(nameof(algorithm));
            }

            object obj = (object)null;

            try
            {
                obj = CryptoHelper.GetAlgorithmFromConfig(algorithm);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex);
                algorithm = (string)null;
            }
            if (obj != null)
            {
                return(obj is SignatureDescription || obj is AsymmetricAlgorithm);
            }
            if (algorithm == "http://www.w3.org/2000/09/xmldsig#dsa-sha1")
            {
                return(this.PublicKey is DSA);
            }
            if (algorithm == "http://www.w3.org/2000/09/xmldsig#rsa-sha1" || algorithm == "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" || (algorithm == "http://www.w3.org/2001/04/xmlenc#rsa-1_5" || algorithm == "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"))
            {
                return(this.PublicKey is RSA);
            }
            return(false);
        }
        /// <summary>Gets a cryptographic algorithm that generates a hash for a digital signature.</summary>
        /// <param name="algorithm">The hash algorithm.</param>
        /// <returns>A <see cref="T:System.Security.Cryptography.HashAlgorithm" /> that generates hashes for digital signatures.</returns>
        /// <exception cref="T:System.NotSupportedException">
        /// <paramref name="algorithm" /> is not supported. The supported algorithms are <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigDSAUrl" />, <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigRSASHA1Url" />, and <see cref="F:System.IdentityModel.Tokens.SecurityAlgorithms.RsaSha256Signature" />.</exception>
        public override HashAlgorithm GetHashAlgorithmForSignature(string algorithm)
        {
            if (string.IsNullOrEmpty(algorithm))
            {
                throw new ArgumentNullException(nameof(algorithm));
            }

            object algorithmFromConfig = CryptoHelper.GetAlgorithmFromConfig(algorithm);

            if (algorithmFromConfig != null)
            {
                SignatureDescription signatureDescription = algorithmFromConfig as SignatureDescription;
                if (signatureDescription != null)
                {
                    return(signatureDescription.CreateDigest());
                }
                HashAlgorithm hashAlgorithm = algorithmFromConfig as HashAlgorithm;
                if (hashAlgorithm != null)
                {
                    return(hashAlgorithm);
                }
                throw new CryptographicException("UnsupportedAlgorithmForCryptoOperation");
            }
            if (algorithm == "http://www.w3.org/2000/09/xmldsig#dsa-sha1" || algorithm == "http://www.w3.org/2000/09/xmldsig#rsa-sha1")
            {
                return(CryptoHelper.NewSha1HashAlgorithm());
            }
            if (algorithm == "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256")
            {
                return(CryptoHelper.NewSha256HashAlgorithm());
            }
            throw new NotSupportedException("UnsupportedCryptoAlgorithm");
        }
        /// <summary>Gets the de-formatter algorithm for the digital signature.</summary>
        /// <param name="algorithm">The de-formatter algorithm for the digital signature to get an instance of.</param>
        /// <returns>An <see cref="T:System.Security.Cryptography.AsymmetricSignatureDeformatter" /> that represents the de-formatter algorithm for the digital signature.</returns>
        /// <exception cref="T:System.NotSupportedException">
        /// <paramref name="algorithm" /> is <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigDSAUrl" /> and the public key for the X.509 certificate specified in the constructor is not of type <see cref="T:System.Security.Cryptography.DSA" />.-or-
        /// <paramref name="algorithm" /> is <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigRSASHA1Url" /> or <see cref="F:System.IdentityModel.Tokens.SecurityAlgorithms.RsaSha256Signature" /> and the public key for the X.509 certificate specified in the constructor is not of type <see cref="T:System.Security.Cryptography.RSA" />.-or-
        /// <paramref name="algorithm" /> is not supported. The supported algorithms are <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigDSAUrl" />,
        /// <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigRSASHA1Url" />, and <see cref="F:System.IdentityModel.Tokens.SecurityAlgorithms.RsaSha256Signature" />.</exception>
        public override AsymmetricSignatureDeformatter GetSignatureDeformatter(
            string algorithm)
        {
            if (string.IsNullOrEmpty(algorithm))
            {
                throw new ArgumentNullException(nameof(algorithm));
            }

            object algorithmFromConfig = CryptoHelper.GetAlgorithmFromConfig(algorithm);

            if (algorithmFromConfig != null)
            {
                SignatureDescription signatureDescription = algorithmFromConfig as SignatureDescription;
                if (signatureDescription != null)
                {
                    return(signatureDescription.CreateDeformatter(this.PublicKey));
                }
                try
                {
                    AsymmetricSignatureDeformatter signatureDeformatter = algorithmFromConfig as AsymmetricSignatureDeformatter;
                    if (signatureDeformatter != null)
                    {
                        signatureDeformatter.SetKey(this.PublicKey);
                        return(signatureDeformatter);
                    }
                }
                catch (InvalidCastException ex) { throw new NotSupportedException("AlgorithmAndPublicKeyMisMatch", (Exception)ex); }

                throw new CryptographicException("UnsupportedAlgorithmForCryptoOperation");
            }
            if (algorithm != "http://www.w3.org/2000/09/xmldsig#dsa-sha1")
            {
                if (algorithm == "http://www.w3.org/2000/09/xmldsig#rsa-sha1" || algorithm == "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256")
                {
                    RSA publicKey = this.PublicKey as RSA;
                    if (publicKey == null)
                    {
                        throw new NotSupportedException("PublicKeyNotRSA");
                    }

                    return((AsymmetricSignatureDeformatter) new ADSD.Crypto.RSAPKCS1SignatureDeformatter((AsymmetricAlgorithm)publicKey));
                }
                throw new NotSupportedException("UnsupportedCryptoAlgorithm");
            }
            DSA publicKey1 = this.PublicKey as DSA;

            if (publicKey1 == null)
            {
                throw new NotSupportedException("PublicKeyNotDSA");
            }

            return((AsymmetricSignatureDeformatter) new DSASignatureDeformatter((AsymmetricAlgorithm)publicKey1));
        }
Exemple #4
0
        /// <summary>Gets the formatter algorithm for the digital signature.</summary>
        /// <param name="algorithm">The formatter algorithm for the digital signature to get an instance of.</param>
        /// <returns>An <see cref="T:System.Security.Cryptography.AsymmetricSignatureDeformatter" /> that represents the formatter algorithm for the digital signature.</returns>
        /// <exception cref="T:System.NotSupportedException">The X.509 certificate specified in the constructor does not have a private key.-or-
        /// <paramref name="algorithm" /> is <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigDSAUrl" /> and the private key for the X.509 certificate specified in the constructor is not of type <see cref="T:System.Security.Cryptography.DSA" />.-or-
        /// <paramref name="algorithm" /> is <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigRSASHA1Url" /> or <see cref="F:System.IdentityModel.Tokens.SecurityAlgorithms.RsaSha256Signature" /> and the private key for the X.509 certificate specified in the constructor is not of type <see cref="T:System.Security.Cryptography.RSA" />.-or-
        /// <paramref name="algorithm" /> is not supported. The supported algorithms are <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigDSAUrl" />,
        /// <see cref="F:System.Security.Cryptography.Xml.SignedXml.XmlDsigRSASHA1Url" />, and <see cref="F:System.IdentityModel.Tokens.SecurityAlgorithms.RsaSha256Signature" />.</exception>
        public AsymmetricSignatureFormatter GetSignatureFormatter(
            string algorithm)
        {
            if (this.PrivateKey == null)
            {
                throw new NotSupportedException("MissingPrivateKey");
            }
            if (string.IsNullOrEmpty(algorithm))
            {
                throw new ArgumentNullException(nameof(algorithm));
            }

            AsymmetricAlgorithm key    = X509AsymmetricSecurityKey.LevelUpRsa(this.PrivateKey, algorithm);
            object algorithmFromConfig = CryptoHelper.GetAlgorithmFromConfig(algorithm);

            if (algorithmFromConfig != null)
            {
                SignatureDescription signatureDescription = algorithmFromConfig as SignatureDescription;
                if (signatureDescription != null)
                {
                    return(signatureDescription.CreateFormatter(key));
                }
                try
                {
                    AsymmetricSignatureFormatter signatureFormatter = algorithmFromConfig as AsymmetricSignatureFormatter;
                    if (signatureFormatter != null)
                    {
                        signatureFormatter.SetKey(key);
                        return(signatureFormatter);
                    }
                }
                catch (InvalidCastException ex)
                {
                    throw new NotSupportedException("AlgorithmAndPrivateKeyMisMatch", (Exception)ex);
                }
                throw new CryptographicException("UnsupportedAlgorithmForCryptoOperation");
            }
            if (algorithm != "http://www.w3.org/2000/09/xmldsig#dsa-sha1")
            {
                if (algorithm != "http://www.w3.org/2000/09/xmldsig#rsa-sha1")
                {
                    if (algorithm == "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256")
                    {
                        RSA rsa = key as RSA;
                        if (rsa == null)
                        {
                            throw new NotSupportedException("PrivateKeyNotRSA");
                        }

                        return((AsymmetricSignatureFormatter) new RSAPKCS1SignatureFormatter((AsymmetricAlgorithm)rsa));
                    }
                    throw new NotSupportedException("UnsupportedCryptoAlgorithm");
                }
                RSA privateKey = this.PrivateKey as RSA;
                if (privateKey == null)
                {
                    throw new NotSupportedException("PrivateKeyNotRSA");
                }

                return((AsymmetricSignatureFormatter) new RSAPKCS1SignatureFormatter((AsymmetricAlgorithm)privateKey));
            }
            DSA privateKey1 = this.PrivateKey as DSA;

            if (privateKey1 == null)
            {
                throw new NotSupportedException("PrivateKeyNotDSA");
            }

            return((AsymmetricSignatureFormatter) new DSASignatureFormatter((AsymmetricAlgorithm)privateKey1));
        }