Android Question Signature and Digest for XML

Angelo Messina

Active Member
Licensed User
Longtime User
Hi, what do you suggest for signing XML files, having only the certificate and the CRS issued by the revenue agency to transmit the monthly sales amounts using only B4A.

What do you advise me to do? I'm in trouble I have to deliver a job and I only need this part.

Thank you
 

Angelo Messina

Active Member
Licensed User
Longtime User
thanks for your kind reply, a practical example to generate a DigestValue what to use as text to pass and what to do to generate the SignatureValue. I've been stuck on this for a month.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
thanks for your kind reply, a practical example to generate a DigestValue what to use as text to pass and what to do to generate the SignatureValue. I've been stuck on this for a month.

Did you do a Google search? Can you provide more infos about what they expect? (api, xml format, docs)?
 
Upvote 0

Angelo Messina

Active Member
Licensed User
Longtime User
B4X:
y = y.create("RichiestaCertificatoDispositivo").attribute("xmlns:ds","http://www.w3.org/2000/09/xmldsig#").attribute("xmlns:p","http://ivaservizi.agenziaentrate.gov.it/docs/xsd/corrispettivi/v1.0").attribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance") _
    .element("Csr").text(CSR).up _
    .element("Dispositivo") _
    .element("Tipo").text("DM").up.up.up _
    .element("TecnicoVerificatore") _
    .element("CodiceFiscale").text(CodiceFiscale).up _
    .element("PIvaSocieta") _
    .element("IdPaese").text("IT").up _
    .element("IdCodice").text(CodiceDitta).up.up.up _
    .element("ds:Signature").attribute("xmlns:ds","http://www.w3.org/2000/09/xmldsig#") _
    .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(DigestValue).up.up.up _
    .element("ds:SignatureValue").text(SignatureValue).up _
    .element("ds:KeyInfo") _
    .element("ds:X509Data") _
    .element("ds:X509Certificate").text(x509).up


<?xml version="1.0" encoding="ISO-8859-1"?>
<p:RichiestaCertificatoDispositivo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/corrispettivi/v1.0" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" versione="1.0">
<Csr>MII......==</Csr>
<Dispositivo>
<Tipo>DM</Tipo>
</Dispositivo>
<TecnicoVerificatore>
<CodiceFiscale>XXXXXXXXXXXXXXXY</CodiceFiscale>
<PIvaSocieta>
<IdPaese>IT</IdPaese>
<IdCodice>09999999999</IdCodice>
</PIvaSocieta>
</TecnicoVerificatore>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<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 URI="">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue> How to do this </ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue> how to calculate this</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>MIIE.....</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
</p:RichiestaCertificatoDispositivo>
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Angelo Messina

Active Member
Licensed User
Longtime User
B4X:
Dim strCanonData As String
    Dim abMessage() As Byte
    Dim BC As ByteConverter
    Dim DigestValue As String
    
    strCanonData = Testo.Replace( CRLF, Chr(10))
    strCanonData = "<Object xmlns=""http://www.w3.org/2000/09/xmldsig#"" Id=""object"">" & strCanonData & "</Object>"
    Log( "CANON DATA='" & strCanonData & "'")
    abMessage = BC.StringToBytes(strCanonData, "UTF8")
    Log( "HEX(DATA)=" & BC.HexFromBytes(abMessage))
    'Digest = BC.HexFromBytes(abMessage)
    nativeMe.InitializeContext
    DigestValue = Encode64(nativeMe.RunMethod("getSHA256",Array(strCanonData)))
    Return DigestValue
 
Upvote 0

Angelo Messina

Active Member
Licensed User
Longtime User
Please can you show me a concrete exampleto use MessageDigest from the Encryption library.

B4X:
    Dim strCanonData As String
    Dim abMessage() As Byte
    Dim BC As ByteConverter
    Dim DigestValue As String
    Dim Enc As MessageDigest
    Dim Testo As String =  "Corrispettivielettronici/Out/Richiesta.xml"
    strCanonData = Testo.Replace( CRLF, Chr(10))
   
strCanonData = "<Object xmlns=""http://www.w3.org/2000/09/xmldsig#"" Id=""object"">" & strCanonData & "</Object>"
       
    abMessage = BC.StringToBytes(strCanonData, "UTF8")
   
    DigestValue = BC.StringFromBytes( Enc.GetMessageDigest(abMessage, "SHA-256"),"UTF8")
    Log("Digest " & DigestValue)
 
Upvote 0

Angelo Messina

Active Member
Licensed User
Longtime User
B4X:
    Dim strCanonData As String
    Dim abMessage() As Byte
    Dim BC As ByteConverter
    Dim DigestValue As String
    Dim Enc As MessageDigest
    Dim su As StringUtils
    Dim Testo As String =  "Corrispettivielettronici/Out/Richiesta.xml"
    
    strCanonData = Testo.Replace( CRLF, Chr(10))
    strCanonData = "<Object xmlns=""http://www.w3.org/2000/09/xmldsig#"" Id=""object"">" & strCanonData & "</Object>"
    
    Log( "CANON DATA='" & strCanonData & "'")
    
    abMessage = BC.StringToBytes(strCanonData, "UTF8")
    DigestValue = su.EncodeBase64(Enc.GetMessageDigest(abMessage, "SHA-256"))

    Log("Digest " & DigestValue )

right so?
 
Upvote 0

Angelo Messina

Active Member
Licensed User
Longtime User
can you show me the way for the Signature Value?
B4X:
 Dim Appoggio As String
 Dim strSignature64 As String
 Dim strSignedInfoCanonic As String
 Dim BC As ByteConverter
 Dim su As StringUtils
 Dim En As Signature
strSignedInfoCanonic = "<SignedInfo xmlns=""http://www.w3.org/2000/09/xmldsig#"">" & CRLF & _
            "  <CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315""></CanonicalizationMethod>" & CRLF & _
            "  <SignatureMethod Algorithm=""http://www.w3.org/2001/04/xmldsig-more#rsa-sha256""></SignatureMethod>" & CRLF & _
            "  <Reference URI=""#object"">" & CRLF & _
            "    <DigestMethod Algorithm=""http://www.w3.org/2001/04/xmlenc#sha256""></DigestMethod>" & CRLF & _
            "    <DigestValue>" & strDigestBase64 & "</DigestValue>" & CRLF & _
            "  </Reference>" & CRLF & _
            "</SignedInfo>"

    abMessage = BC.StringToBytes(strSignedInfoCanonic, "UTF8")
   
   
    En.Update(abMessage)
 
Upvote 0
Top