Esempio n. 1
0
        public virtual AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
        {
            AsymmetricSignatureDeformatter item = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(DeformatterAlgorithm);

            item.SetKey(key);
            return(item);
        }
        /// <summary>
        /// Creates signature deformatter
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
        {
            AsymmetricSignatureDeformatter asymmetricSignatureDeformatter = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(base.DeformatterAlgorithm);

            asymmetricSignatureDeformatter.SetKey(key);
            asymmetricSignatureDeformatter.SetHashAlgorithm("SHA256");
            return(asymmetricSignatureDeformatter);
        }
        public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
        {
            AsymmetricSignatureDeformatter item = (AsymmetricSignatureDeformatter)GostCryptoConfig.CreateFromName(DeformatterAlgorithm);

            item.SetKey(key);
            item.SetHashAlgorithm("SHA1");
            return(item);
        }
        public override AsymmetricSignatureDeformatter GetSignatureDeformatter(string algorithm)
        {
            if (string.IsNullOrEmpty(algorithm))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, System.IdentityModel.SR.GetString("EmptyOrNullArgumentString", new object[] { "algorithm" }));
            }
            object algorithmFromConfig = CryptoHelper.GetAlgorithmFromConfig(algorithm);

            if (algorithmFromConfig != null)
            {
                SignatureDescription description = algorithmFromConfig as SignatureDescription;
                if (description != null)
                {
                    return(description.CreateDeformatter(this.PublicKey.Key));
                }
                try
                {
                    AsymmetricSignatureDeformatter deformatter = algorithmFromConfig as AsymmetricSignatureDeformatter;
                    if (deformatter != null)
                    {
                        deformatter.SetKey(this.PublicKey.Key);
                        return(deformatter);
                    }
                }
                catch (InvalidCastException exception)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.IdentityModel.SR.GetString("AlgorithmAndPublicKeyMisMatch"), exception));
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(System.IdentityModel.SR.GetString("UnsupportedAlgorithmForCryptoOperation", new object[] { algorithm, "GetSignatureDeformatter" })));
            }
            switch (algorithm)
            {
            case "http://www.w3.org/2000/09/xmldsig#dsa-sha1":
            {
                DSA key = this.PublicKey.Key as DSA;
                if (key == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.IdentityModel.SR.GetString("PublicKeyNotDSA")));
                }
                return(new DSASignatureDeformatter(key));
            }

            case "http://www.w3.org/2000/09/xmldsig#rsa-sha1":
            case "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256":
            {
                RSA rsa = this.PublicKey.Key as RSA;
                if (rsa == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.IdentityModel.SR.GetString("PublicKeyNotRSA")));
                }
                return(new RSAPKCS1SignatureDeformatter(rsa));
            }
            }
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.IdentityModel.SR.GetString("UnsupportedCryptoAlgorithm", new object[] { algorithm })));
        }
        public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
        {
            AsymmetricSignatureDeformatter signatureDeformatter = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(this.DeformatterAlgorithm);
            AsymmetricAlgorithm            key1 = key;

            signatureDeformatter.SetKey(key1);
            string strName = "SHA1";

            signatureDeformatter.SetHashAlgorithm(strName);
            return(signatureDeformatter);
        }
Esempio n. 6
0
        /// <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));
        }
Esempio n. 7
0
        public override AsymmetricSignatureDeformatter GetSignatureDeformatter(string algorithm)
        {
            if (string.IsNullOrEmpty(algorithm))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format(SR.EmptyOrNullArgumentString, algorithm));
            }

            object algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm);

            if (algorithmObject != null)
            {
                SignatureDescription description = algorithmObject as SignatureDescription;
                if (description != null)
                {
                    return(description.CreateDeformatter(this.rsa));
                }

                try
                {
                    AsymmetricSignatureDeformatter asymmetricSignatureDeformatter = algorithmObject as AsymmetricSignatureDeformatter;
                    if (asymmetricSignatureDeformatter != null)
                    {
                        asymmetricSignatureDeformatter.SetKey(this.rsa);
                        return(asymmetricSignatureDeformatter);
                    }
                }
                catch (InvalidCastException e)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.AlgorithmAndKeyMisMatch, algorithm), e));
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.UnsupportedAlgorithmForCryptoOperation,
                                                                                                               algorithm, "GetSignatureDeformatter")));
            }

            switch (algorithm)
            {
            case SecurityAlgorithms.RsaSha1Signature:
            case SecurityAlgorithms.RsaSha256Signature:
                return(new RSAPKCS1SignatureDeformatter(rsa));

            default:
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.UnsupportedAlgorithmForCryptoOperation,
                                                                                                               algorithm, "GetSignatureDeformatter")));
            }
        }
        public virtual AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
        {
            if (_DeformatterAlgorithm == null)
            {
                throw new ArgumentNullException("DeformatterAlgorithm");
            }

            // this should throw the InvalidCastException if we have an invalid class
            // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
            AsymmetricSignatureDeformatter def = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(_DeformatterAlgorithm);

            if (_KeyAlgorithm == null)
            {
                throw new NullReferenceException("KeyAlgorithm");
            }

            def.SetKey(key);
            return(def);
        }
        // Is the signature (over SignedInfo) valid ?
        private bool CheckSignatureWithKey(AsymmetricAlgorithm key)
        {
            if (key == null)
            {
                return(false);
            }

            SignatureDescription sd = (SignatureDescription)CryptoConfig.CreateFromName(m_signature.SignedInfo.SignatureMethod);

            if (sd == null)
            {
                return(false);
            }

            AsymmetricSignatureDeformatter verifier = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(sd.DeformatterAlgorithm);

            if (verifier == null)
            {
                return(false);
            }

            try
            {
                verifier.SetKey(key);
                verifier.SetHashAlgorithm(sd.DigestAlgorithm);

                HashAlgorithm hash = GetHash(sd.DigestAlgorithm, true);
                // get the hash of the C14N SignedInfo element
                MemoryStream ms = (MemoryStream)SignedInfoTransformed();

                byte[] digest = hash.ComputeHash(ms);
                return(verifier.VerifySignature(digest, m_signature.SignatureValue));
            }
            catch
            {
                // e.g. SignatureMethod != AsymmetricAlgorithm type
                return(false);
            }
        }
        public override AsymmetricSignatureDeformatter GetSignatureDeformatter(string algorithm)
        {
            // We support one of the two algoritms, but not both.
            //     XmlDsigDSAUrl = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
            //     XmlDsigRSASHA1Url = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";

            if (string.IsNullOrEmpty(algorithm))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.GetString(SR.EmptyOrNullArgumentString, "algorithm"));
            }

            object algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm);

            if (algorithmObject != null)
            {
                SignatureDescription description = algorithmObject as SignatureDescription;
                if (description != null)
                {
                    return(description.CreateDeformatter(this.PublicKey));
                }

                try
                {
                    AsymmetricSignatureDeformatter asymmetricSignatureDeformatter = algorithmObject as AsymmetricSignatureDeformatter;
                    if (asymmetricSignatureDeformatter != null)
                    {
                        asymmetricSignatureDeformatter.SetKey(this.PublicKey);
                        return(asymmetricSignatureDeformatter);
                    }
                }
                catch (InvalidCastException e)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.AlgorithmAndPublicKeyMisMatch), e));
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.UnsupportedAlgorithmForCryptoOperation,
                                                                                                                  algorithm, "GetSignatureDeformatter")));
            }

            switch (algorithm)
            {
            case SignedXml.XmlDsigDSAUrl:

                // Ensure that we have a DSA algorithm object.
                DSA dsa = (this.PublicKey as DSA);
                if (dsa == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.PublicKeyNotDSA)));
                }
                return(new DSASignatureDeformatter(dsa));

            case SignedXml.XmlDsigRSASHA1Url:
            case SecurityAlgorithms.RsaSha256Signature:
                // Ensure that we have an RSA algorithm object.
                RSA rsa = (this.PublicKey as RSA);
                if (rsa == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.PublicKeyNotRSA)));
                }
                return(new RSAPKCS1SignatureDeformatter(rsa));

            default:
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedCryptoAlgorithm, algorithm)));
            }
        }
 protected static void InvalidDeformatterArguments(AsymmetricSignatureDeformatter deformatter)
 {
     Assert.Throws<ArgumentNullException>(() => deformatter.SetKey(null));
     Assert.Throws<ArgumentNullException>(() => deformatter.VerifySignature((byte[])null, new byte[] { 0, 1, 2 }));
     Assert.Throws<CryptographicUnexpectedOperationException>(() => deformatter.VerifySignature(new byte[] { 0, 1, 2 }, new byte[] { 0, 1, 2 }));
 }
 protected static void InvalidDeformatterArguments(AsymmetricSignatureDeformatter deformatter)
 {
     Assert.Throws <ArgumentNullException>(() => deformatter.SetKey(null));
     Assert.Throws <ArgumentNullException>(() => deformatter.VerifySignature((byte[])null, new byte[] { 0, 1, 2 }));
     Assert.Throws <CryptographicUnexpectedOperationException>(() => deformatter.VerifySignature(new byte[] { 0, 1, 2 }, new byte[] { 0, 1, 2 }));
 }