/// <summary> /// Añade una firma al documento /// </summary> /// <param name="certificate"></param> /// <param name="signMethod"></param> public void CoSign(X509Certificate2 certificate, SignMethod?signMethod = null) { if (_xadesSignedXml == null) { throw new Exception("No hay ninguna firma XADES creada previamente."); } if (certificate == null) { throw new Exception("Es necesario un certificado válido para la firma."); } Reference refContent = _xadesSignedXml.SignedInfo.References[0] as Reference; if (refContent == null) { throw new Exception("No se ha podido encontrar la referencia del contenido firmado."); } if (_xadesSignedXml.XadesObject.QualifyingProperties.SignedProperties.SignedDataObjectProperties.DataObjectFormatCollection.Count > 0) { foreach (DataObjectFormat dof in _xadesSignedXml.XadesObject.QualifyingProperties.SignedProperties.SignedDataObjectProperties.DataObjectFormatCollection) { if (dof.ObjectReferenceAttribute == ("#" + refContent.Id)) { _mimeType = dof.MimeType; break; } } } var destination = _xadesSignedXml.GetSignatureElement().ParentNode; _xadesSignedXml = new XadesSignedXml(_document); refContent.Id = "Reference-" + Guid.NewGuid().ToString(); _xadesSignedXml.AddReference(refContent); if (destination.NodeType != XmlNodeType.Document) { _xadesSignedXml.SignatureNodeDestination = (XmlElement)destination; } else { _xadesSignedXml.SignatureNodeDestination = ((XmlDocument)destination).DocumentElement; } _objectReference = refContent.Id; SetSignatureId(); Sign(certificate, signMethod); }
public static byte[] ComputeValueOfElementList(XadesSignedXml xadesSignedXml, ArrayList elementXpaths) { XmlElement signatureElement = xadesSignedXml.GetSignatureElement(); List <XmlAttribute> allNamespaces = xadesSignedXml.GetAllNamespaces(signatureElement); XmlDocument ownerDocument = signatureElement.OwnerDocument; XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(ownerDocument.NameTable); xmlNamespaceManager.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#"); xmlNamespaceManager.AddNamespace("xades", "http://uri.etsi.org/01903/v1.3.2#"); using (MemoryStream memoryStream = new MemoryStream()) { foreach (string elementXpath in elementXpaths) { XmlNodeList xmlNodeList = signatureElement.SelectNodes(elementXpath, xmlNamespaceManager); if (xmlNodeList.Count == 0) { throw new CryptographicException("Element " + elementXpath + " not found while calculating hash"); } foreach (XmlNode item in xmlNodeList) { XmlElement xmlElement = (XmlElement)item.Clone(); xmlElement.SetAttribute("xmlns:" + XadesSignedXml.XmlDSigPrefix, "http://www.w3.org/2000/09/xmldsig#"); foreach (XmlAttribute item2 in allNamespaces) { xmlElement.SetAttribute(item2.Name, item2.Value); } byte[] array = ApplyTransform(xmlElement, new XmlDsigC14NTransform()); memoryStream.Write(array, 0, array.Length); } } return(memoryStream.ToArray()); } }
/// <summary> /// Actualiza el documento resultante /// </summary> internal void UpdateDocument() { if (_document == null) { _document = new XmlDocument(); } if (_document.DocumentElement != null) { XmlNode xmlNode = _document.SelectSingleNode("//*[@Id='" + _xadesSignedXml.Signature.Id + "']"); if (xmlNode != null) { XmlNamespaceManager nm = new XmlNamespaceManager(_document.NameTable); nm.AddNamespace("xades", XadesSignedXml.XadesNamespaceUri); nm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl); XmlNode xmlQPNode = xmlNode.SelectSingleNode("ds:Object/xades:QualifyingProperties", nm); XmlNode xmlUnsingedPropertiesNode = xmlNode.SelectSingleNode("ds:Object/xades:QualifyingProperties/xades:UnsignedProperties", nm); if (xmlUnsingedPropertiesNode != null) { xmlUnsingedPropertiesNode.InnerXml = _xadesSignedXml.XadesObject.QualifyingProperties.UnsignedProperties.GetXml().InnerXml; } else { xmlUnsingedPropertiesNode = _document.ImportNode(_xadesSignedXml.XadesObject.QualifyingProperties.UnsignedProperties.GetXml(), true); xmlQPNode.AppendChild(xmlUnsingedPropertiesNode); } } else { XmlElement xmlSigned = _xadesSignedXml.GetXml(); byte[] canonicalizedElement = XMLUtil.ApplyTransform(xmlSigned, new XmlDsigC14NTransform()); XmlDocument doc = new XmlDocument { PreserveWhitespace = true }; doc.LoadXml(Encoding.UTF8.GetString(canonicalizedElement)); XmlNode canonSignature = _document.ImportNode(doc.DocumentElement, true); _xadesSignedXml.GetSignatureElement().AppendChild(canonSignature); } } else { _document.LoadXml(_xadesSignedXml.GetXml().OuterXml); } }
/// <summary> /// Obtiene el valor canonicalizado de los elementos especificados en elementXpaths /// </summary> /// <param name="xadesSignedXml"></param> /// <param name="elementXpaths"></param> /// <returns></returns> public static byte[] ComputeValueOfElementList(XadesSignedXml xadesSignedXml, ArrayList elementXpaths) { XmlDocument xmlDocument; XmlNamespaceManager xmlNamespaceManager; XmlNodeList searchXmlNodeList; var signatureXmlElement = xadesSignedXml.GetSignatureElement(); var namespaces = xadesSignedXml.GetAllNamespaces(signatureXmlElement); xmlDocument = signatureXmlElement.OwnerDocument; xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable); xmlNamespaceManager.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl); xmlNamespaceManager.AddNamespace("xades", XadesSignedXml.XadesNamespaceUri); using (MemoryStream msResult = new MemoryStream()) { foreach (string elementXpath in elementXpaths) { searchXmlNodeList = signatureXmlElement.SelectNodes(elementXpath, xmlNamespaceManager); if (searchXmlNodeList.Count == 0) { throw new CryptographicException("Element " + elementXpath + " not found while calculating hash"); } foreach (XmlNode xmlNode in searchXmlNodeList) { XmlAttribute dsNamespace = xmlDocument.CreateAttribute("xmlns:" + XadesSignedXml.XmlDSigPrefix); dsNamespace.Value = XadesSignedXml.XmlDsigNamespaceUrl; xmlNode.Attributes.Append(dsNamespace); foreach (var attr in namespaces) { XmlAttribute attrNamespace = xmlDocument.CreateAttribute(attr.Name); attrNamespace.Value = attr.Value; xmlNode.Attributes.Append(attrNamespace); } byte[] canonicalizedElement = ApplyTransform((XmlElement)xmlNode, new XmlDsigC14NTransform()); msResult.Write(canonicalizedElement, 0, canonicalizedElement.Length); } } return(msResult.ToArray()); } }
internal void UpdateDocument() { if (_document == null) { _document = new XmlDocument(); } if (_document.DocumentElement != null) { XmlNode xmlNode = _document.SelectSingleNode("//*[@Id='" + _xadesSignedXml.Signature.Id + "']"); if (xmlNode != null) { XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(_document.NameTable); xmlNamespaceManager.AddNamespace("xades", "http://uri.etsi.org/01903/v1.3.2#"); xmlNamespaceManager.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#"); XmlNode xmlNode2 = xmlNode.SelectSingleNode("ds:Object/xades:QualifyingProperties", xmlNamespaceManager); XmlNode xmlNode3 = xmlNode.SelectSingleNode("ds:Object/xades:QualifyingProperties/xades:UnsignedProperties", xmlNamespaceManager); if (xmlNode3 != null) { xmlNode3.InnerXml = _xadesSignedXml.XadesObject.QualifyingProperties.UnsignedProperties.GetXml().InnerXml; } else { xmlNode3 = _document.ImportNode(_xadesSignedXml.XadesObject.QualifyingProperties.UnsignedProperties.GetXml(), true); xmlNode2.AppendChild(xmlNode3); } } else { XmlElement xml = _xadesSignedXml.GetXml(); byte[] bytes = XMLUtil.ApplyTransform(xml, new XmlDsigC14NTransform()); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.PreserveWhitespace = true; xmlDocument.LoadXml(Encoding.UTF8.GetString(bytes)); XmlNode newChild = _document.ImportNode(xmlDocument.DocumentElement, true); _xadesSignedXml.GetSignatureElement().AppendChild(newChild); } } else { _document.LoadXml(_xadesSignedXml.GetXml().OuterXml); } }
/// <summary> /// Actualiza el documento resultante /// </summary> internal void UpdateDocument() { if (_document == null) { _document = new XmlDocument(); } if (_document.DocumentElement != null) { XmlNode xmlNode = _document.SelectSingleNode("//*[@Id='" + _xadesSignedXml.Signature.Id + "']"); if (xmlNode != null) { XmlNamespaceManager nm = new XmlNamespaceManager(_document.NameTable); nm.AddNamespace("xades", XadesSignedXml.XadesNamespaceUri); nm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl); XmlNode xmlQPNode = xmlNode.SelectSingleNode("ds:Object/xades:QualifyingProperties", nm); XmlNode xmlUnsingedPropertiesNode = xmlNode.SelectSingleNode("ds:Object/xades:QualifyingProperties/xades:UnsignedProperties", nm); if (xmlUnsingedPropertiesNode != null) { XmlNode xmlUnsingedSignaturePropertiesNode = xmlNode.SelectSingleNode("ds:Object/xades:QualifyingProperties/xades:UnsignedProperties/xades:UnsignedSignatureProperties", nm); XmlElement xmlUnsignedPropertiesNew = _xadesSignedXml.XadesObject.QualifyingProperties.UnsignedProperties.UnsignedSignatureProperties.GetXml(); foreach (XmlNode childNode in xmlUnsignedPropertiesNew.ChildNodes) { if (childNode.Attributes["Id"] != null && xmlUnsingedSignaturePropertiesNode.SelectSingleNode("//*[@Id='" + childNode.Attributes["Id"].Value + "']") == null) { var newNode = _document.ImportNode(childNode, true); xmlUnsingedSignaturePropertiesNode.AppendChild(newNode); } } // Se comprueban las ContraFirmas if (_xadesSignedXml.XadesObject.QualifyingProperties.UnsignedProperties.UnsignedSignatureProperties.CounterSignatureCollection.Count > 0) { foreach (XadesSignedXml counterSign in _xadesSignedXml.XadesObject.QualifyingProperties.UnsignedProperties.UnsignedSignatureProperties.CounterSignatureCollection) { if (xmlNode.SelectSingleNode("//*[@Id='" + counterSign.Signature.Id + "']") == null) { XmlNode xmlCounterSignatureNode = _document.CreateElement(XadesSignedXml.XmlXadesPrefix, "CounterSignature", XadesSignedXml.XadesNamespaceUri); xmlUnsingedSignaturePropertiesNode.AppendChild(xmlCounterSignatureNode); xmlCounterSignatureNode.AppendChild(_document.ImportNode(counterSign.GetXml(), true)); } } } } else { xmlUnsingedPropertiesNode = _document.ImportNode(_xadesSignedXml.XadesObject.QualifyingProperties.UnsignedProperties.GetXml(), true); xmlQPNode.AppendChild(xmlUnsingedPropertiesNode); } } else { XmlElement xmlSigned = _xadesSignedXml.GetXml(); byte[] canonicalizedElement = XMLUtil.ApplyTransform(xmlSigned, new XmlDsigC14NTransform()); XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.LoadXml(Encoding.UTF8.GetString(canonicalizedElement)); XmlNode canonSignature = _document.ImportNode(doc.DocumentElement, true); _xadesSignedXml.GetSignatureElement().AppendChild(canonSignature); } } else { _document.LoadXml(_xadesSignedXml.GetXml().OuterXml); } }