The HashDataInfo class contains a uri attribute referencing a data object and a ds:Transforms element indicating the transformations to make to this data object. The sequence of HashDataInfo elements will be used to produce the input of the hash computation process whose result will be included in the timestamp request to be sent to the TSA.
        /// <summary>
        /// Load state from an XML element
        /// </summary>
        /// <param name="xmlElement">XML element containing new state</param>
        public void LoadXml(System.Xml.XmlElement xmlElement)
        {
            XmlNamespaceManager xmlNamespaceManager;
            XmlNodeList         xmlNodeList;
            IEnumerator         enumerator;
            XmlElement          iterationXmlElement;
            HashDataInfo        newHashDataInfo;

            if (xmlElement == null)
            {
                throw new ArgumentNullException(nameof(xmlElement));
            }

            if (xmlElement.HasAttribute("Id"))
            {
                this.id = xmlElement.GetAttribute("Id");
            }
            else
            {
                this.id = "";
            }

            xmlNamespaceManager = new XmlNamespaceManager(xmlElement.OwnerDocument.NameTable);
            xmlNamespaceManager.AddNamespace("xades", XadesSignedXml.XadesNamespaceUri);

            this.hashDataInfoCollection.Clear();
            xmlNodeList = xmlElement.SelectNodes("xades:HashDataInfo", xmlNamespaceManager);
            enumerator  = xmlNodeList.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    iterationXmlElement = enumerator.Current as XmlElement;
                    if (iterationXmlElement != null)
                    {
                        newHashDataInfo = new HashDataInfo();
                        newHashDataInfo.LoadXml(iterationXmlElement);
                        this.hashDataInfoCollection.Add(newHashDataInfo);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable disposable)
                {
                    disposable.Dispose();
                }
            }

            xmlNodeList = xmlElement.SelectNodes("xades:EncapsulatedTimeStamp", xmlNamespaceManager);

            if (xmlNodeList.Count != 0)
            {
                this.encapsulatedTimeStamp = new EncapsulatedPKIData("EncapsulatedTimeStamp");
                this.encapsulatedTimeStamp.LoadXml((XmlElement)xmlNodeList.Item(0));
                this.xmlTimeStamp = null;
            }
            else
            {
                XmlNode nodeEncapsulatedTimeStamp = null;

                foreach (XmlNode node in xmlElement.ChildNodes)
                {
                    if (node.Name == "EncapsulatedTimeStamp")
                    {
                        nodeEncapsulatedTimeStamp = node;
                        break;
                    }
                }

                if (nodeEncapsulatedTimeStamp != null)
                {
                    this.encapsulatedTimeStamp = new EncapsulatedPKIData("EncapsulatedTimeStamp");
                    this.encapsulatedTimeStamp.LoadXml((XmlElement)nodeEncapsulatedTimeStamp);
                    this.xmlTimeStamp = null;
                }
                else
                {
                    xmlNodeList = xmlElement.SelectNodes("xades:XMLTimeStamp", xmlNamespaceManager);
                    if (xmlNodeList.Count != 0)
                    {
                        this.xmlTimeStamp = new XMLTimeStamp();
                        this.xmlTimeStamp.LoadXml((XmlElement)xmlNodeList.Item(0));
                        this.encapsulatedTimeStamp = null;
                    }
                    else
                    {
                        throw new CryptographicException("EncapsulatedTimeStamp or XMLTimeStamp element is missing");
                    }
                }
            }
        }
        /// <summary>
        /// Load state from an XML element
        /// </summary>
        /// <param name="xmlElement">XML element containing new state</param>
        public void LoadXml(System.Xml.XmlElement xmlElement)
        {
            XmlNamespaceManager xmlNamespaceManager;
            XmlNodeList         xmlNodeList;
            IEnumerator         enumerator;
            XmlElement          iterationXmlElement;
            HashDataInfo        newHashDataInfo;

            if (xmlElement == null)
            {
                throw new ArgumentNullException("xmlElement");
            }

            xmlNamespaceManager = new XmlNamespaceManager(xmlElement.OwnerDocument.NameTable);
            xmlNamespaceManager.AddNamespace("xsd", XadesSignedXml.XadesNamespaceUri);

            this.hashDataInfoCollection.Clear();
            xmlNodeList = xmlElement.SelectNodes("xsd:HashDataInfo", xmlNamespaceManager);
            enumerator  = xmlNodeList.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    iterationXmlElement = enumerator.Current as XmlElement;
                    if (iterationXmlElement != null)
                    {
                        newHashDataInfo = new HashDataInfo();
                        newHashDataInfo.LoadXml(iterationXmlElement);
                        this.hashDataInfoCollection.Add(newHashDataInfo);
                    }
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }

            xmlNodeList = xmlElement.SelectNodes("xsd:EncapsulatedTimeStamp", xmlNamespaceManager);
            if (xmlNodeList.Count != 0)
            {
                this.encapsulatedTimeStamp = new EncapsulatedPKIData("EncapsulatedTimeStamp");
                this.encapsulatedTimeStamp.LoadXml((XmlElement)xmlNodeList.Item(0));
                this.xmlTimeStamp = null;
            }
            else
            {
                xmlNodeList = xmlElement.SelectNodes("xsd:XMLTimeStamp", xmlNamespaceManager);
                if (xmlNodeList.Count != 0)
                {
                    this.xmlTimeStamp = new XMLTimeStamp();
                    this.xmlTimeStamp.LoadXml((XmlElement)xmlNodeList.Item(0));
                    this.encapsulatedTimeStamp = null;
                }
                else
                {
                    throw new CryptographicException("EncapsulatedTimeStamp or XMLTimeStamp missing");
                }
            }
        }
Exemple #3
0
        private void requestTimeStampButton_Click(object sender, System.EventArgs e)
        {
            TimeStamp signatureTimeStamp;
            HttpTsaClient httpTSAClient;
            KnownTsaResponsePkiStatus tsaResponsePkiStatus;
            ArrayList signatureValueElementXpaths;
            byte[] signatureValueHash;

            if (this.xadesSignedXml.SignatureStandard == KnownSignatureStandard.Xades)
            {
                try
                {
                    httpTSAClient = new HttpTsaClient();
                    httpTSAClient.RequestTsaCertificate = this.tsaCertificateInResponseCheckBox.Checked;
                    signatureValueElementXpaths = new ArrayList();
                    signatureValueElementXpaths.Add("ds:SignatureValue");
                    ArrayList elementIdValues = new ArrayList();
                    signatureValueHash = httpTSAClient.ComputeHashValueOfElementList(this.xadesSignedXml.GetXml(), signatureValueElementXpaths, ref elementIdValues);
                    httpTSAClient.SendTsaWebRequest(this.tsaUriTextBox.Text, signatureValueHash);
                    tsaResponsePkiStatus = httpTSAClient.ParseTsaResponse();
                    if (tsaResponsePkiStatus == KnownTsaResponsePkiStatus.Granted)
                    {
                        signatureTimeStamp = new TimeStamp("SignatureTimeStamp");
                        signatureTimeStamp.EncapsulatedTimeStamp.Id = this.signatureTimeStampIdTextBox.Text;
                        signatureTimeStamp.EncapsulatedTimeStamp.PkiData = httpTSAClient.TsaTimeStamp;
                        HashDataInfo hashDataInfo = new HashDataInfo();
                        hashDataInfo.UriAttribute = "#" + elementIdValues[0];
                        signatureTimeStamp.HashDataInfoCollection.Add(hashDataInfo);
                        UnsignedProperties unsignedProperties = this.xadesSignedXml.UnsignedProperties;
                        unsignedProperties.UnsignedSignatureProperties.SignatureTimeStampCollection.Add(signatureTimeStamp);
                        this.xadesSignedXml.UnsignedProperties = unsignedProperties;

                        XmlElement xml = this.xadesSignedXml.XadesObject.GetXml();
                        XmlElement xml1 = this.xadesSignedXml.GetXml();

                        this.ShowSignature();
                    }
                    else
                    {
                        MessageBox.Show("TSA timestamp request not granted: " + tsaResponsePkiStatus.ToString());
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show("Exception occurred during TSA timestamp request: " + exception.ToString());
                }
            }
            else
            {
                MessageBox.Show("Signature standard should be XAdES. (You need to add XAdES info before computing the signature to be able to inject a timestamp)");
            }
        }
Exemple #4
0
        private void injectXadesXInformationButton_Click(object sender, System.EventArgs e)
        {
            TimeStamp xadesXTimeStamp;
            HttpTsaClient httpTSAClient;
            KnownTsaResponsePkiStatus tsaResponsePkiStatus;
            ArrayList signatureValueElementXpaths;
            ArrayList elementIdValues;
            byte[] signatureValueHash;
            HashDataInfo hashDataInfo;

            httpTSAClient = new HttpTsaClient();
            httpTSAClient.RequestTsaCertificate = this.tsaCertificateInResponseCheckBox.Checked;
            signatureValueElementXpaths = new ArrayList();
            if (this.sigAndRefsTimeStampRadioButton.Checked)
            {
                signatureValueElementXpaths.Add("ds:SignatureValue");
                signatureValueElementXpaths.Add("ds:Object/xsd:QualifyingProperties/xsd:UnsignedProperties/xsd:UnsignedSignatureProperties/xsd:SignatureTimeStamp/xsd:EncapsulatedTimeStamp");
                signatureValueElementXpaths.Add("ds:Object/xsd:QualifyingProperties/xsd:UnsignedProperties/xsd:UnsignedSignatureProperties/xsd:CompleteCertificateRefs");
                signatureValueElementXpaths.Add("ds:Object/xsd:QualifyingProperties/xsd:UnsignedProperties/xsd:UnsignedSignatureProperties/xsd:CompleteRevocationRefs");
            }
            else
            {
                signatureValueElementXpaths.Add("ds:Object/xsd:QualifyingProperties/xsd:UnsignedProperties/xsd:UnsignedSignatureProperties/xsd:CompleteCertificateRefs");
                signatureValueElementXpaths.Add("ds:Object/xsd:QualifyingProperties/xsd:UnsignedProperties/xsd:UnsignedSignatureProperties/xsd:CompleteRevocationRefs");
            }
            elementIdValues = new ArrayList();
            signatureValueHash = httpTSAClient.ComputeHashValueOfElementList(this.xadesSignedXml.GetXml(), signatureValueElementXpaths, ref elementIdValues);
            httpTSAClient.SendTsaWebRequest(this.tsaUriTextBox.Text, signatureValueHash);
            tsaResponsePkiStatus = httpTSAClient.ParseTsaResponse();
            if (tsaResponsePkiStatus == KnownTsaResponsePkiStatus.Granted)
            {
                if (this.sigAndRefsTimeStampRadioButton.Checked)
                {
                    xadesXTimeStamp = new TimeStamp("SigAndRefsTimeStamp");
                }
                else
                {
                    xadesXTimeStamp = new TimeStamp("RefsOnlyTimeStamp");
                }
                xadesXTimeStamp.EncapsulatedTimeStamp.PkiData = httpTSAClient.TsaTimeStamp;
                xadesXTimeStamp.EncapsulatedTimeStamp.Id = this.xadesXTimeStampIdTextBox.Text;

                foreach (string elementIdValue in elementIdValues)
                {
                    hashDataInfo = new HashDataInfo();
                    hashDataInfo.UriAttribute = "#" + elementIdValue;
                    xadesXTimeStamp.HashDataInfoCollection.Add(hashDataInfo);
                }
                UnsignedProperties unsignedProperties = this.xadesSignedXml.UnsignedProperties;
                if (this.sigAndRefsTimeStampRadioButton.Checked)
                {
                    unsignedProperties.UnsignedSignatureProperties.RefsOnlyTimeStampFlag = false;
                    unsignedProperties.UnsignedSignatureProperties.SigAndRefsTimeStampCollection.Add(xadesXTimeStamp);
                }
                else
                {
                    unsignedProperties.UnsignedSignatureProperties.RefsOnlyTimeStampFlag = true;
                    unsignedProperties.UnsignedSignatureProperties.RefsOnlyTimeStampCollection.Add(xadesXTimeStamp);
                }
                this.xadesSignedXml.UnsignedProperties = unsignedProperties;
                this.ShowSignature();
            }
            else
            {
                MessageBox.Show("TSA timestamp request not granted: " + tsaResponsePkiStatus.ToString());
            }
        }
Exemple #5
0
		/// <summary>
		/// Load state from an XML element
		/// </summary>
		/// <param name="xmlElement">XML element containing new state</param>
		public void LoadXml(System.Xml.XmlElement xmlElement)
		{
			XmlNamespaceManager xmlNamespaceManager;
			XmlNodeList xmlNodeList;
			IEnumerator enumerator;
			XmlElement iterationXmlElement;
			HashDataInfo newHashDataInfo;
			
			if (xmlElement == null)
			{
				throw new ArgumentNullException("xmlElement");
			}

			xmlNamespaceManager = new XmlNamespaceManager(xmlElement.OwnerDocument.NameTable);
			xmlNamespaceManager.AddNamespace("xsd", XadesSignedXml.XadesNamespaceUri);

			this.hashDataInfoCollection.Clear();
			xmlNodeList = xmlElement.SelectNodes("xsd:HashDataInfo", xmlNamespaceManager);
			enumerator = xmlNodeList.GetEnumerator();
			try 
			{
				while (enumerator.MoveNext()) 
				{
					iterationXmlElement = enumerator.Current as XmlElement;
					if (iterationXmlElement != null)
					{
						newHashDataInfo = new HashDataInfo();
						newHashDataInfo.LoadXml(iterationXmlElement);
						this.hashDataInfoCollection.Add(newHashDataInfo);
					}
				}
			}
			finally 
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}

			xmlNodeList = xmlElement.SelectNodes("xsd:EncapsulatedTimeStamp", xmlNamespaceManager);
			if (xmlNodeList.Count != 0)
			{
				this.encapsulatedTimeStamp = new EncapsulatedPKIData("EncapsulatedTimeStamp");
				this.encapsulatedTimeStamp.LoadXml((XmlElement)xmlNodeList.Item(0));
				this.xmlTimeStamp = null;
			}
			else
			{
				xmlNodeList = xmlElement.SelectNodes("xsd:XMLTimeStamp", xmlNamespaceManager);
				if (xmlNodeList.Count != 0)
				{
					this.xmlTimeStamp = new XMLTimeStamp();
					this.xmlTimeStamp.LoadXml((XmlElement)xmlNodeList.Item(0));
					this.encapsulatedTimeStamp = null;

				}
				else
				{
					throw new CryptographicException("EncapsulatedTimeStamp or XMLTimeStamp missing");
				}
			}
		}
        /// <summary>
        /// Add typed object to the collection
        /// </summary>
        /// <param name="objectToAdd">Typed object to be added to collection</param>
        /// <returns>The object that has been added to collection</returns>
        public HashDataInfo Add(HashDataInfo objectToAdd)
        {
            base.Add(objectToAdd);

            return objectToAdd;
        }
Exemple #7
0
        /// <summary>
        /// Add typed object to the collection
        /// </summary>
        /// <param name="objectToAdd">Typed object to be added to collection</param>
        /// <returns>The object that has been added to collection</returns>
        public HashDataInfo Add(HashDataInfo objectToAdd)
        {
            base.Add(objectToAdd);

            return(objectToAdd);
        }