예제 #1
0
 public XmlElement GetXml()
 {
     if (ContainingDocument != null)
     {
         return(MSignature.GetXml(ContainingDocument));
     }
     else
     {
         return(MSignature.GetXml());
     }
 }
예제 #2
0
        public void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            MSignature.LoadXml(value);

            if (_context == null)
            {
                _context = value;
            }

            IsCacheValid = false;
        }
예제 #3
0
        public void ComputeSignature()
        {
            SignedXmlDebugLog.LogBeginSignatureComputation(this, _context);

            ReferenceManager.BuildDigestedReferences(this);

            AsymmetricKeyParameter key = SigningKey;

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

            if (SignedInfo.SignatureMethod == null)
            {
                if (key is DsaKeyParameters)
                {
                    SignedInfo.SignatureMethod = XmlNameSpace.Url[NS.XmlDsigDSAUrl];
                }
                else if (key is RsaKeyParameters)
                {
                    SignedInfo.SignatureMethod = XmlNameSpace.Url[NS.XmlDsigRSASHA256Url];
                }
                else
                {
                    throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_CreatedKeyFailed);
                }
            }

            ISigner signatureDescription = CryptoHelpers.CreateFromName <ISigner>(SignedInfo.SignatureMethod);

            if (signatureDescription == null)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_SignatureDescriptionNotCreated);
            }

            signatureDescription.Init(true, key);
            CheckSignatureManager.GetC14NDigest(new SignerHashWrapper(signatureDescription), this);

            SignedXmlDebugLog.LogSigning(this, key, signatureDescription);
            MSignature.SetSignatureValue(signatureDescription.GenerateSignature());
        }
예제 #4
0
        public void ComputeSignature(IMac macAlg)
        {
            if (macAlg == null)
            {
                throw new ArgumentNullException(nameof(macAlg));
            }

            if (!(macAlg is HMac))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_SignatureMethodKeyMismatch);
            }

            int signatureLength;

            if (MSignature.SignedInfo.SignatureLength == null)
            {
                signatureLength = macAlg.GetMacSize() * 8;
            }
            else
            {
                signatureLength = Convert.ToInt32(MSignature.SignedInfo.SignatureLength, null);
            }
            if (signatureLength < 0 || signatureLength > macAlg.GetMacSize() * 8)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidSignatureLength);
            }
            if (signatureLength % 8 != 0)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidSignatureLength2);
            }

            ReferenceManager.BuildDigestedReferences(this);

            var algorithmName       = macAlg.AlgorithmName.Substring(0, macAlg.AlgorithmName.IndexOf('/')).ToUpperInvariant();
            var signedXmlDictionary = new Dictionary <string, NS>()
            {
                { "SHA-1", NS.XmlDsigHMACSHA1Url },
                { "SHA-256", NS.XmlDsigMoreHMACSHA256Url },
                { "SHA-384", NS.XmlDsigMoreHMACSHA384Url },
                { "SHA-512", NS.XmlDsigMoreHMACSHA512Url },
                { "MD5", NS.XmlDsigMoreHMACMD5Url },
                { "RIPEMD160", NS.XmlDsigMoreHMACRIPEMD160Url }
            };

            try
            {
                SignedInfo.SignatureMethod = XmlNameSpace.Url[signedXmlDictionary[algorithmName]];
            }
            catch (Exception)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_SignatureMethodKeyMismatch);
            }

            CheckSignatureManager.GetC14NDigest(new MacHashWrapper(macAlg), this);
            byte[] hashValue = new byte[macAlg.GetMacSize()];
            macAlg.DoFinal(hashValue, 0);

            SignedXmlDebugLog.LogSigning(this, macAlg);
            MSignature.SetSignatureValue(new byte[signatureLength / 8]);
            Buffer.BlockCopy(hashValue, 0, MSignature.GetSignatureValue(), 0, signatureLength / 8);
        }
예제 #5
0
 public void AddObject(DataObject dataObject)
 {
     MSignature.AddObject(dataObject);
 }