| Jonathan Zanardi |
XML Signature
Ciao a tutti,
dovrei ottenere una firma digitale xml (XMLDISG) di questo tipo: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <root> <HEAHEA> <Identificativo><![CDATA[1234567890]]></Identificativo> <Data><![CDATA[20101201]]></Data> <Text><![CDATA[Primo test WS]]></Text> <CFTITOLARE><![CDATA[01629050897 ]]></CFTITOLARE> <CODICEUTENTEABILITATO><![CDATA[-001]]></CODICEUTENTEABILITATO> </HEAHEA> <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="Signature1"> <ds:SignedInfo> <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/> <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/> <ds:Reference Id="reference-document" URI=""> <ds:Transforms> <ds:Transform Algorithm="http://www.w3.org/2002/06/xmldsig-filter2"> <dsig-xpath:XPath xmlns:dsig-xpath="http://www.w3.org/2002/06/xmldsig-filter2" Filter="subtract">/descendant::ds:Signature</dsig-xpath:XPath> </ds:Transform> </ds:Transforms> <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/> <ds:DigestValue>y/M2eN74MHglkFeVrMPDqhGxGR6YWrWRtf+SIUu9nhI=</ds:DigestValue> </ds:Reference> <ds:Reference Id="reference-signedproperties" Type="http://uri.etsi.org/01903/v1.3.2#SignedProperties" URI="#SignedProperties_1"> <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/> <ds:DigestValue>H3e01SzyF0LU+daH33W0/bPMyIoeFTnAi4Ggw+qe9gk=</ds:DigestValue> </ds:Reference> </ds:SignedInfo> <ds:SignatureValue Id="SignatureValue1"> ..... </ds:SignatureValue> <ds:KeyInfo> <ds:X509Data> <ds:X509Certificate> ...... </ds:X509Certificate> </ds:X509Data> </ds:KeyInfo> <ds:Object> <xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#"> <xades:SignedProperties Id="SignedProperties_1"> <xades:SignedSignatureProperties> <xades:SigningTime>2010-12-07T11:18:54Z</xades:SigningTime> <xades:SigningCertificate> <xades:Cert> <xades:CertDigest> <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/> <ds:DigestValue>....</ds:DigestValue> </xades:CertDigest> <xades:IssuerSerial> <ds:X509IssuerName>CN=CA Agenzia delle Dogane Test,OU=Servizio Telematico,O=Agenzia delle Dogane,C=it</ds:X509IssuerName> <ds:X509SerialNumber>9319</ds:X509SerialNumber> </xades:IssuerSerial> </xades:Cert> </xades:SigningCertificate> </xades:SignedSignatureProperties> </xades:SignedProperties> </xades:QualifyingProperties> </ds:Object> </ds:Signature> </root> Come vedete si tratta di usare la firma RSA e l'algoritrmo di Hash SHA256. Per rendere il tutto un po' più complicato devo utilizzare anche XADES per aggiungere le SignedProperties riportate nell'esempio. Mi sembra di capire che quanto richiesto non sia completamente supportato dal framework 2.0 e sto provando ad installarmi VS2010 per testare le nuove funzionalità a disposizione. Qualcuno può indirizzarmi su dove trovare una documentazione valida e completa? Naturalmente gli esempi Microsoft disponibili sono scolastici e del tutto inutili per i miei scopi. La struttura di base del processo che seguo è questa Public Sub FirmaXml(ByVal xmlfilenameIn As String, ByVal filenameOut As String) Dim xmlDoc As XmlDocument Dim xWriter As XmlWriter = Nothing Try Dim x As New X509Certificate2("petrol.p12", .....) xmlDoc = New XmlDocument() ' Load an XML file into the XmlDocument object. xmlDoc.PreserveWhitespace = False xmlDoc.Load(Server.MapPath(xmlfilenameIn)) ' Create a SignedXml object. Dim signedXml As SignedXml = GetSignature(xmlfilenameIn, xmlDoc, x) ' Get the XML representation of the signature and save ' it to an XmlElement object. Dim xmlDigitalSignature As XmlElement = signedXml.GetXml() ' Append the element to the XML document. xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, True)) xWriter = XmlWriter.Create(Server.MapPath(filenameOut)) xmlDoc.WriteTo(xWriter) TextBox1.Text = "File creato!" Catch ex As Exception TextBox1.Text = "Errore: " & ex.Message Finally If Not IsNothing(xWriter) Then xWriter.Close() End If End Try End Sub entrando nei dettagli: Private Function GetSignature(ByVal xmlfilenameIn As String, ByVal document As XmlDocument, ByVal certificate As X509Certificate2) As SignedXml Dim sXml As New SignedXml(document) 'Qui dovrei implementare Xades ma non so come??????????? Dim dataObject As New DataObject("SignedProperties_1", "http://uri.etsi.org/01903/v1.3.2#SignedProperties", String.Empty, document.DocumentElement) Dim dobjRef As New Reference dobjRef.Uri = "#SignedProperties_1" dobjRef.Id = "reference-signedpropeties" sXml.AddObject(dataObject) sXml.AddReference(dobjRef) sXml.AddReference(GetSignatureReference()) sXml.SigningKey = certificate.PrivateKey sXml.KeyInfo = GetCertificateKeyInfo(certificate) sXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigC14NTransformUrl sXml.SignedInfo.Id = "Signature1" sXml.ComputeSignature() Return sXml End Function Private Function GetSignatureReference() As Reference Dim ref1 As New Reference() ref1.Uri = String.Empty ref1.Id = "reference-document" Dim t1 As XmlDsigXPathTransform = CreateXPathTransform("/descendant::ds:Signature") ref1.AddTransform(t1) Return ref1 End Function I problemi maggiori ce li ho nell'ottenere i dettagli Xades della firma e l'utilizzo dell'XPath nella creazione del <Reference id="reference-document">. Per quest'ultimo ho scoperto che si tratta di un'espressione XPath 2.0 e che pertanto devo provare con un framework più aggiornato. L'argomento è (per me) alquanto vasto e complesso e vorrei quanto meno trovare una buona documentazione su come implementare la firma in VB.Net. PS: l'RFC 3275 me lo sono già letto. Grazie Ciao John |