Example #1
0
        public void ComputeSignature()
        {
            SignedXmlDebugLog.LogBeginSignatureComputation(this, _context);

            BuildDigestedReferences();

            // Load the key
            AsymmetricAlgorithm key = SigningKey;

            if (key == null)
            {
                throw new CryptographicException(SR.Cryptography_Xml_LoadKeyFailed);
            }

            // Check the signature algorithm associated with the key so that we can accordingly set the signature method
            if (SignedInfo.SignatureMethod == null)
            {
                if (key is DSA)
                {
                    SignedInfo.SignatureMethod = XmlDsigDSAUrl;
                }
                else if (key is RSA)
                {
                    // Default to RSA-SHA1
                    if (SignedInfo.SignatureMethod == null)
                    {
                        SignedInfo.SignatureMethod = XmlDsigRSASHA256Url;
                    }
                }
                else
                {
                    throw new CryptographicException(SR.Cryptography_Xml_CreatedKeyFailed);
                }
            }

            // See if there is a signature description class defined in the Config file
            SignatureDescription signatureDescription = CryptoHelpers.CreateFromName <SignatureDescription>(SignedInfo.SignatureMethod);

            if (signatureDescription == null)
            {
                throw new CryptographicException(SR.Cryptography_Xml_SignatureDescriptionNotCreated);
            }
            HashAlgorithm hashAlg = signatureDescription.CreateDigest();

            if (hashAlg == null)
            {
                throw new CryptographicException(SR.Cryptography_Xml_CreateHashAlgorithmFailed);
            }
            byte[] hashvalue = GetC14NDigest(hashAlg);
            AsymmetricSignatureFormatter asymmetricSignatureFormatter = signatureDescription.CreateFormatter(key);

            SignedXmlDebugLog.LogSigning(this, key, signatureDescription, hashAlg, asymmetricSignatureFormatter);
            m_signature.SignatureValue = asymmetricSignatureFormatter.CreateSignature(hashAlg);
        }
Example #2
0
        public void ComputeSignature(KeyedHashAlgorithm macAlg)
        {
            if (macAlg == null)
            {
                throw new ArgumentNullException(nameof(macAlg));
            }

            if (!(macAlg is HMAC hash))
            {
                throw new CryptographicException(SR.Cryptography_Xml_SignatureMethodKeyMismatch);
            }

            int signatureLength;

            if (m_signature.SignedInfo.SignatureLength == null)
            {
                signatureLength = hash.HashSize;
            }
            else
            {
                signatureLength = Convert.ToInt32(m_signature.SignedInfo.SignatureLength, null);
            }
            // signatureLength should be less than hash size
            if (signatureLength < 0 || signatureLength > hash.HashSize)
            {
                throw new CryptographicException(SR.Cryptography_Xml_InvalidSignatureLength);
            }
            if (signatureLength % 8 != 0)
            {
                throw new CryptographicException(SR.Cryptography_Xml_InvalidSignatureLength2);
            }

            BuildDigestedReferences();
            SignedInfo.SignatureMethod = hash.HashName switch
            {
                "SHA1" => SignedXml.XmlDsigHMACSHA1Url,
                "SHA256" => SignedXml.XmlDsigMoreHMACSHA256Url,
                "SHA384" => SignedXml.XmlDsigMoreHMACSHA384Url,
                "SHA512" => SignedXml.XmlDsigMoreHMACSHA512Url,
                "MD5" => SignedXml.XmlDsigMoreHMACMD5Url,
                "RIPEMD160" => SignedXml.XmlDsigMoreHMACRIPEMD160Url,
                _ => throw new CryptographicException(SR.Cryptography_Xml_SignatureMethodKeyMismatch),
            };
            byte[] hashValue = GetC14NDigest(hash);

            SignedXmlDebugLog.LogSigning(this, hash);
            m_signature.SignatureValue = new byte[signatureLength / 8];
            Buffer.BlockCopy(hashValue, 0, m_signature.SignatureValue, 0, signatureLength / 8);
        }