CorryWestSide
Member
hi, the question is very easy, i make a function for build a xml structure so i put it in a file and save it like xml. Easy, the problem is that i have to sign this file for send it to Italian Tax Agency and i have with me only a x509Certificate. On Internet i found that with only that i can sign the document but actually i dont know how.
Here my Complete Test Function:
How you can see there is actually a part of this XML that actually i have to Add with a directly Sign and is this:
So my question is actually, i wanna create a function that can Sign an XML and i still dont know how i can, maybe can give me some tips
or i dont know...i can share here a source of wiki about how should be work: https://en.wikipedia.org/wiki/XML_Signature
Here my Complete Test Function:
Generate XML:
Sub GetCertificatoX509 As String
If File.Exists(Comodo.PATHCORRISPETTIVI, "deviceCertificate.x509") Then
' Legge il contenuto del file
Dim fileContent As String = File.ReadString(Comodo.PATHCORRISPETTIVI, "deviceCertificate.x509")
' Divide il contenuto del file in righe
Dim lines() As String = Regex.Split(CRLF, fileContent)
' Verifica che ci siano almeno 3 righe (minimo per avere una seconda riga e una penultima riga)
If lines.Length > 2 Then
Dim result As StringBuilder
result.Initialize
' Aggiungi le righe dalla seconda alla penultima
For i = 1 To lines.Length - 2
result.Append(lines(i)).Append(CRLF)
Next
' Rimuove l'ultimo CRLF aggiunto
If result.Length > 0 Then
result.Remove(result.Length - CRLF.Length, result.Length)
End If
' Ritorna il risultato come stringa
Return result.ToString
Else
' Se ci sono meno di 3 righe, ritorna una stringa vuota
Return ""
End If
Else
' Se il file non esiste, ritorna una stringa vuota
Return ""
End If
End Sub
Sub GeneraXMLPeriodico()
Dim xmlBuilder As XMLBuilder ' Inizializza un nuovo oggetto XMLBuilder
Dim props As Map ' Inizializza una nuova mappa per le proprietà
xmlBuilder = xmlBuilder.create("p:DatiCorrispettivi") _
.attribute("xmlns:p", "http://ivaservizi.agenziaentrate.gov.it/docs/xsd/corrispettivi/dati/v1.0") _
.attribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#") _
.attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") _
.attribute("versione", "COR10") _
.element("Trasmissione") _
.element("Progressivo").text("").up _
.element("Formato").text("").up _
.element("Dispositivo") _
.element("Tipo").text("").up _
.element("IdDispositivo").text("").up _
.element("GeoLocalizzazione") _
.element("Lat").text("").up _
.element("Long").text("").up.up.up.up _
.element("DataOraRilevazione").text("").up _
.element("DatiDA") _
.element("Periodo") _ 'AGGIUNGERE QUI I VALORI PRESI DIRETTAMENTE DALL'AUDIT
.element("Venduto").text("").up _
.element("VendutoContante").text("").up _
.element("VendutoNoContante").text("").up _
.element("Incassato").text("").up _
.element("IncassatoRicarica").text("").up _
.element("IncassatoVendita").text("").up _
.element("TotaleResoTubiResto").text("").up _
.element("TotaleCaricatoTubiResto").text("").up _
.element("TotaleResoManualeTubiResto").text("").up _
.element("TotaleCaricatoManualeTubiResto").text("").up _
.element("DataOraPrelievoPrec").text("").up _
.element("ProgressivoPrelievo").text("").up.up.up.up _
.element("ds:Signature") _
.element("ds:SignedInfo") _
.element("ds:CanonicalizationMethod").attribute("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315").up _
.element("ds:SignatureMethod").attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256").up _
.element("ds:Reference").attribute("URI", "") _
.element("ds:Transforms") _
.element("ds:Transform").attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature").up.up _
.element("ds:DigestMethod").attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256").up _
.element("ds:DigestValue").text("field to fill with sign numer 1").up.up.up _
.element("ds:SignatureValue").text("field to fill with sign numer 2").up _
.element("ds:KeyInfo") _
.element("ds:X509Data") _
.element("ds:X509Certificate").text(GetCertificatoX509).up.up.up.up
props.Initialize
If (Main.phoneInstance.SdkVersion < 29) Then
props.Put("{http://xml.apache.org/xslt}indent-amount", "4")
Else
props.Put("{http://xml.apache.org/xslt}indent-amount", "14")
End If
props.Put("indent", "yes")
File.WriteString(Comodo.DBDir, "testnewPeriodicoOUT.xml", xmlBuilder.asString2(props))
Log("File testnewPeriodicoOUT created")
End Sub
How you can see there is actually a part of this XML that actually i have to Add with a directly Sign and is this:
Part to Add with Signature:
.element("ds:Signature") _
.element("ds:SignedInfo") _
.element("ds:CanonicalizationMethod").attribute("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315").up _
.element("ds:SignatureMethod").attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256").up _
.element("ds:Reference").attribute("URI", "") _
.element("ds:Transforms") _
.element("ds:Transform").attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature").up.up _
.element("ds:DigestMethod").attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256").up _
.element("ds:DigestValue").text("field to fill with sign numer 1").up.up.up _
.element("ds:SignatureValue").text("field to fill with sign numer 2").up _
.element("ds:KeyInfo") _
.element("ds:X509Data") _
.element("ds:X509Certificate").text(GetCertificatoX509).up.up.up.up
So my question is actually, i wanna create a function that can Sign an XML and i still dont know how i can, maybe can give me some tips
or i dont know...i can share here a source of wiki about how should be work: https://en.wikipedia.org/wiki/XML_Signature