/// <summary> /// Default constructor /// </summary> public UnsignedSignatureProperties() { this.counterSignatureCollection = new CounterSignatureCollection(); this.signatureTimeStampCollection = new SignatureTimeStampCollection(); this.completeCertificateRefs = new CompleteCertificateRefs(); this.completeRevocationRefs = new CompleteRevocationRefs(); this.refsOnlyTimeStampFlag = false; this.sigAndRefsTimeStampCollection = new SignatureTimeStampCollection(); this.refsOnlyTimeStampCollection = new SignatureTimeStampCollection(); this.certificateValues = new CertificateValues(); this.revocationValues = new RevocationValues(); this.archiveTimeStampCollection = new SignatureTimeStampCollection(); }
/// <summary> /// Default constructor /// </summary> public UnsignedSignatureProperties() { this.counterSignatureCollection = new CounterSignatureCollection(); this.signatureTimeStampCollection = new SignatureTimeStampCollection(); this.completeCertificateRefs = new CompleteCertificateRefs(); this.completeRevocationRefs = new CompleteRevocationRefs(); this.refsOnlyTimeStampFlag = false; this.sigAndRefsTimeStampCollection = new SignatureTimeStampCollection(); this.refsOnlyTimeStampCollection = new SignatureTimeStampCollection(); this.certificateValues = new CertificateValues(); this.revocationValues = new RevocationValues(); this.archiveTimeStampCollection = new SignatureTimeStampCollection(); }
/// <summary> /// Load state from an XML element /// </summary> /// <param name="xmlElement">XML element containing new state</param> /// <param name="counterSignedXmlElement">Element containing parent signature (needed if there are counter signatures)</param> public void LoadXml(System.Xml.XmlElement xmlElement, XmlElement counterSignedXmlElement) { XmlNamespaceManager xmlNamespaceManager; XmlNodeList xmlNodeList; IEnumerator enumerator; XmlElement iterationXmlElement; XadesSignedXml newXadesSignedXml; TimeStamp newTimeStamp; XmlElement counterSignatureElement; if (xmlElement == null) { throw new ArgumentNullException("xmlElement"); } xmlNamespaceManager = new XmlNamespaceManager(xmlElement.OwnerDocument.NameTable); xmlNamespaceManager.AddNamespace("xades", XadesSignedXml.XadesNamespaceUri); xmlNamespaceManager.AddNamespace("xadesv141", XadesSignedXml.XadesNamespace141Uri); this.counterSignatureCollection.Clear(); xmlNodeList = xmlElement.SelectNodes("xades:CounterSignature", xmlNamespaceManager); enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { if (counterSignedXmlElement != null) { newXadesSignedXml = new XadesSignedXml(counterSignedXmlElement); } else { newXadesSignedXml = new XadesSignedXml(); } //Skip any whitespace at start counterSignatureElement = null; for (int childNodeCounter = 0; (childNodeCounter < iterationXmlElement.ChildNodes.Count) && (counterSignatureElement == null); childNodeCounter++) { if (iterationXmlElement.ChildNodes[childNodeCounter] is XmlElement) { counterSignatureElement = (XmlElement)iterationXmlElement.ChildNodes[childNodeCounter]; } } if (counterSignatureElement != null) { newXadesSignedXml.LoadXml(counterSignatureElement); this.counterSignatureCollection.Add(newXadesSignedXml); } else { throw new CryptographicException("CounterSignature element does not contain signature"); } } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } this.signatureTimeStampCollection.Clear(); xmlNodeList = xmlElement.SelectNodes("xades:SignatureTimeStamp", xmlNamespaceManager); enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { newTimeStamp = new TimeStamp("SignatureTimeStamp"); newTimeStamp.LoadXml(iterationXmlElement); this.signatureTimeStampCollection.Add(newTimeStamp); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } xmlNodeList = xmlElement.SelectNodes("xades:CompleteCertificateRefs", xmlNamespaceManager); if (xmlNodeList.Count != 0) { this.completeCertificateRefs = new CompleteCertificateRefs(); this.completeCertificateRefs.LoadXml((XmlElement)xmlNodeList.Item(0)); } else { this.completeCertificateRefs = null; } xmlNodeList = xmlElement.SelectNodes("xades:CompleteRevocationRefs", xmlNamespaceManager); if (xmlNodeList.Count != 0) { this.CompleteRevocationRefs = new CompleteRevocationRefs(); this.CompleteRevocationRefs.LoadXml((XmlElement)xmlNodeList.Item(0)); } else { this.completeRevocationRefs = null; } this.sigAndRefsTimeStampCollection.Clear(); this.refsOnlyTimeStampCollection.Clear(); xmlNodeList = xmlElement.SelectNodes("xades:SigAndRefsTimeStamp", xmlNamespaceManager); if (xmlNodeList.Count > 0) { this.refsOnlyTimeStampFlag = false; enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { newTimeStamp = new TimeStamp("SigAndRefsTimeStamp"); newTimeStamp.LoadXml(iterationXmlElement); this.sigAndRefsTimeStampCollection.Add(newTimeStamp); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } else { xmlNodeList = xmlElement.SelectNodes("xades:RefsOnlyTimeStamp", xmlNamespaceManager); if (xmlNodeList.Count > 0) { this.refsOnlyTimeStampFlag = true; enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { newTimeStamp = new TimeStamp("RefsOnlyTimeStamp"); newTimeStamp.LoadXml(iterationXmlElement); this.refsOnlyTimeStampCollection.Add(newTimeStamp); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } else { this.refsOnlyTimeStampFlag = false; } } xmlNodeList = xmlElement.SelectNodes("xades:CertificateValues", xmlNamespaceManager); if (xmlNodeList.Count != 0) { this.certificateValues = new CertificateValues(); this.certificateValues.LoadXml((XmlElement)xmlNodeList.Item(0)); } else { this.certificateValues = null; } xmlNodeList = xmlElement.SelectNodes("xades:RevocationValues", xmlNamespaceManager); if (xmlNodeList.Count != 0) { this.revocationValues = new RevocationValues(); this.revocationValues.LoadXml((XmlElement)xmlNodeList.Item(0)); } else { this.revocationValues = null; } this.archiveTimeStampCollection.Clear(); xmlNodeList = xmlElement.SelectNodes("xades:ArchiveTimeStamp", xmlNamespaceManager); xmlNodeList = xmlElement.SelectNodes("xadesv141:ArchiveTimeStamp", xmlNamespaceManager); enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { newTimeStamp = new TimeStamp("ArchiveTimeStamp"); newTimeStamp.LoadXml(iterationXmlElement); this.archiveTimeStampCollection.Add(newTimeStamp); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } xmlNodeList = xmlElement.SelectNodes("xadesv141:ArchiveTimeStamp", xmlNamespaceManager); enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { newTimeStamp = new TimeStamp("ArchiveTimeStamp", "xadesv141", XadesSignedXml.XadesNamespace141Uri); newTimeStamp.LoadXml(iterationXmlElement); this.archiveTimeStampCollection.Add(newTimeStamp); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } }
/// <summary> /// Load state from an XML element /// </summary> /// <param name="xmlElement">XML element containing new state</param> /// <param name="counterSignedXmlElement">Element containing parent signature (needed if there are counter signatures)</param> public void LoadXml(System.Xml.XmlElement xmlElement, XmlElement counterSignedXmlElement) { XmlNamespaceManager xmlNamespaceManager; XmlNodeList xmlNodeList; IEnumerator enumerator; XmlElement iterationXmlElement; XadesSignedXml newXadesSignedXml; TimeStamp newTimeStamp; XmlElement counterSignatureElement; if (xmlElement == null) { throw new ArgumentNullException("xmlElement"); } xmlNamespaceManager = new XmlNamespaceManager(xmlElement.OwnerDocument.NameTable); xmlNamespaceManager.AddNamespace("xsd", XadesSignedXml.XadesNamespaceUri); this.counterSignatureCollection.Clear(); xmlNodeList = xmlElement.SelectNodes("xsd:CounterSignature", xmlNamespaceManager); enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { if (counterSignedXmlElement != null) { newXadesSignedXml = new XadesSignedXml(counterSignedXmlElement); } else { newXadesSignedXml = new XadesSignedXml(); } //Skip any whitespace at start counterSignatureElement = null; for (int childNodeCounter = 0; (childNodeCounter < iterationXmlElement.ChildNodes.Count) && (counterSignatureElement == null); childNodeCounter++) { if (iterationXmlElement.ChildNodes[childNodeCounter] is XmlElement) { counterSignatureElement = (XmlElement)iterationXmlElement.ChildNodes[childNodeCounter]; } } if (counterSignatureElement != null) { newXadesSignedXml.LoadXml(counterSignatureElement); this.counterSignatureCollection.Add(newXadesSignedXml); } else { throw new CryptographicException("CounterSignature element does not contain signature"); } } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } this.signatureTimeStampCollection.Clear(); xmlNodeList = xmlElement.SelectNodes("xsd:SignatureTimeStamp", xmlNamespaceManager); enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { newTimeStamp = new TimeStamp("SignatureTimeStamp"); newTimeStamp.LoadXml(iterationXmlElement); this.signatureTimeStampCollection.Add(newTimeStamp); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } xmlNodeList = xmlElement.SelectNodes("xsd:CompleteCertificateRefs", xmlNamespaceManager); if (xmlNodeList.Count != 0) { this.completeCertificateRefs = new CompleteCertificateRefs(); this.completeCertificateRefs.LoadXml((XmlElement)xmlNodeList.Item(0)); } else { this.completeCertificateRefs = null; } xmlNodeList = xmlElement.SelectNodes("xsd:CompleteRevocationRefs", xmlNamespaceManager); if (xmlNodeList.Count != 0) { this.CompleteRevocationRefs = new CompleteRevocationRefs(); this.CompleteRevocationRefs.LoadXml((XmlElement)xmlNodeList.Item(0)); } else { this.completeRevocationRefs = null; } this.sigAndRefsTimeStampCollection.Clear(); this.refsOnlyTimeStampCollection.Clear(); xmlNodeList = xmlElement.SelectNodes("xsd:SigAndRefsTimeStamp", xmlNamespaceManager); if (xmlNodeList.Count > 0) { this.refsOnlyTimeStampFlag = false; enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { newTimeStamp = new TimeStamp("SigAndRefsTimeStamp"); newTimeStamp.LoadXml(iterationXmlElement); this.sigAndRefsTimeStampCollection.Add(newTimeStamp); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } else { xmlNodeList = xmlElement.SelectNodes("xsd:RefsOnlyTimeStamp", xmlNamespaceManager); if (xmlNodeList.Count > 0) { this.refsOnlyTimeStampFlag = true; enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { newTimeStamp = new TimeStamp("RefsOnlyTimeStamp"); newTimeStamp.LoadXml(iterationXmlElement); this.refsOnlyTimeStampCollection.Add(newTimeStamp); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } else { this.refsOnlyTimeStampFlag = false; } } xmlNodeList = xmlElement.SelectNodes("xsd:CertificateValues", xmlNamespaceManager); if (xmlNodeList.Count != 0) { this.certificateValues = new CertificateValues(); this.certificateValues.LoadXml((XmlElement)xmlNodeList.Item(0)); } else { this.certificateValues = null; } xmlNodeList = xmlElement.SelectNodes("xsd:RevocationValues", xmlNamespaceManager); if (xmlNodeList.Count != 0) { this.revocationValues = new RevocationValues(); this.revocationValues.LoadXml((XmlElement)xmlNodeList.Item(0)); } else { this.revocationValues = null; } this.archiveTimeStampCollection.Clear(); xmlNodeList = xmlElement.SelectNodes("xsd:ArchiveTimeStamp", xmlNamespaceManager); enumerator = xmlNodeList.GetEnumerator(); try { while (enumerator.MoveNext()) { iterationXmlElement = enumerator.Current as XmlElement; if (iterationXmlElement != null) { newTimeStamp = new TimeStamp("ArchiveTimeStamp"); newTimeStamp.LoadXml(iterationXmlElement); this.archiveTimeStampCollection.Add(newTimeStamp); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } }
private void IncorporateCRLRefs(CompleteRevocationRefs completeRevocationRefs , ValidationContext ctx) { if (!ctx.GetNeededCRL().IsEmpty()) { var crl = ctx.GetNeededCRL()[0]; //TODO jbonilla Digest parameter? byte[] crlDigest = DigestUtilities.CalculateDigest("SHA-1", crl.GetEncoded()); MSXades.CRLRef incCRLRef = new MSXades.CRLRef(); incCRLRef.CertDigest.DigestMethod.Algorithm = SignedXml.XmlDsigSHA1Url; incCRLRef.CertDigest.DigestValue = crlDigest; //incCRLRef.CRLIdentifier.UriAttribute = ""; incCRLRef.CRLIdentifier.Issuer = crl.IssuerDN.ToString(); incCRLRef.CRLIdentifier.IssueTime = crl.ThisUpdate; completeRevocationRefs.CRLRefs.CRLRefCollection.Add(incCRLRef); } }
protected internal override void ExtendSignatureTag(XadesSignedXml xadesSignedXml) { base.ExtendSignatureTag(xadesSignedXml); X509Certificate signingCertificate = DotNetUtilities.FromX509Certificate( xadesSignedXml.GetSigningCertificate()); DateTime signingTime = xadesSignedXml.XadesObject.QualifyingProperties .SignedProperties.SignedSignatureProperties.SigningTime; ValidationContext ctx = certificateVerifier.ValidateCertificate(signingCertificate , signingTime, new XAdESCertificateSource(xadesSignedXml.GetXml(), false), null, null); UnsignedProperties unsignedProperties = xadesSignedXml.UnsignedProperties; var completeCertificateRefs = new CompleteCertificateRefs(); IncorporateCertificateRefs(completeCertificateRefs, ctx); unsignedProperties.UnsignedSignatureProperties.CompleteCertificateRefs = completeCertificateRefs; var completeRevocationRefs = new CompleteRevocationRefs(); IncorporateOCSPRefs(completeRevocationRefs, ctx); IncorporateCRLRefs(completeRevocationRefs, ctx); unsignedProperties.UnsignedSignatureProperties.CompleteRevocationRefs = completeRevocationRefs; xadesSignedXml.UnsignedProperties = unsignedProperties; }
private void IncorporateOCSPRefs(CompleteRevocationRefs completeRevocationRefs , ValidationContext ctx) { if (!ctx.GetNeededOCSPResp().IsEmpty()) { var ocsp = ctx.GetNeededOCSPResp()[0]; //TODO jbonill Digest parameter? byte[] ocspDigest = DigestUtilities.CalculateDigest("SHA-1", ocsp.GetEncoded()); MSXades.OCSPRef incOCSPRef = new MSXades.OCSPRef(); //TODO jbonilla Digest parameter? incOCSPRef.CertDigest.DigestMethod.Algorithm = SignedXml.XmlDsigSHA1Url; incOCSPRef.CertDigest.DigestValue = ocspDigest; //TODO jbonilla //incOCSPRef.OCSPIdentifier.UriAttribute = ""; incOCSPRef.OCSPIdentifier.ProducedAt = ocsp.ProducedAt; string responderIdText = ""; RespID respId = ocsp.ResponderId; ResponderID ocspResponderId = respId.ToAsn1Object(); DerTaggedObject derTaggedObject = (DerTaggedObject)ocspResponderId.ToAsn1Object(); if (2 == derTaggedObject.TagNo) { responderIdText = Convert.ToBase64String(ocspResponderId.GetKeyHash()); } else { responderIdText = ocspResponderId.Name.ToString(); } incOCSPRef.OCSPIdentifier.ResponderID = responderIdText; completeRevocationRefs.OCSPRefs.OCSPRefCollection.Add(incOCSPRef); } }