Como generar el DigestValue con PHP y xades4j (firmar xml)

Hola, estoy realizando un proyecto de facturación electronica para Colombia. he logrado enviar unas facturas correctamente con la ayuda de xades4j JAVA, pero quiero implementar todo el codigo en PHP.

He logrado realizar el DigestValue de un archivo XML de la siguiente manera.

  1. Canonizar el documento con C14N

  2. Convertir a SHA 1

  3. Pasar a base 64 el SHA 1 
    el resultado es el mismo que el arrojado por la librería xades4j de JAVA.

    <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=“xmldsig-6d5e64ce-bfd8-4088-9711-094bc1120f82-ref0” URI="">
    ds:Transforms
    <ds:Transform Algorithm=“http://www.w3.org/2000/09/xmldsig#enveloped-signature”/>
    </ds:Transforms>
    <ds:DigestMethod Algorithm=“http://www.w3.org/2000/09/xmldsig#sha1”/>
    ds:DigestValue
    W5gfW51GRaDmhJWf8IOBE9T5lv4=
    </ds:DigestValue>
    </ds:Reference>

Pero no logro obtener losvalores del Keyinfo y signedprops. 

Aquí muestro los valores que debería obtener.

<ds:Reference URI="#xmldsig-6d5e64ce-bfd8-4088-9711-094bc1120f82-keyinfo">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>8WcxuvLT09/tF0X7jYd3lSt3t4w=
</ds:DigestValue>
</ds:Reference>
<ds:Reference Type="http://uri.etsi.org/01903#SignedProperties" URI="#xmldsig-6d5e64ce-bfd8-4088-9711-094bc1120f82-signedprops">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>d0k/vJKGENxLq/Ejhd35JG+3xT4=
</ds:DigestValue>
</ds:Reference>

Según lo que entiendo, el keyinfo es aplicar el C14N + SHA1 + BASE64ENCODE a la siguiente parte.

<ds:KeyInfo Id="xmldsig-6d5e64ce-bfd8-4088-9711-094bc1120f82-keyinfo">
<ds:X509Data>
<ds:X509Certificate>MIIG7DCCBdSgAwIBAgIQaK/p8QLyeL1a/gh7cg43tDANBgkqhkiG9w0BAQsFADCBqDEcMBoGA1UECQwTd3d3LmNlcnRpY2FtYXJhLmNvbTEPMA0GA1UEBwwGQk9HT1RBMRkwFwYDVQQIDBBESVNUUklUTyBDQVBJVEFMMQswCQYDVQQGEwJDTzEYMBYGA1UECwwPTklUIDgzMDA4NDQzMy03MRgwFgYDVQQKDA9DRVJUSUNBTUFSQSBTLkExGzAZBgNVBAMMEkFDIFNVQiBDRVJUSUNBTUFSQTAgFw0xODA1MTcyMjU1NTVaGA8yMDE5MDUxNzIyNTU1MVowgfQxFDASBgNVBAgMC0JPR09UQSBELkMuMRwwGgYDVQQLDBNGQUNUVVJBIEVMRUNUUk9OSUNBMQ8wDQYDVQQFEwY4MzExMzAxGjAYBgorBgEEAYG1YwIDEwo5MDAxODAxMjY1MSEwHwYDVQQKDBhRVUFOVFVNIERBVEEgU1lTVEVNUyBTQVMxFDASBgNVBAcMC0JPR09UQSBELkMuMSgwJgYJKoZIhvcNAQkBFhlITU9OU0FMVkVAUVVBTlRVTUxUREEuQ09NMQswCQYDVQQGEwJDTzEhMB8GA1UEAwwYUVVBTlRVTSBEQVRBIFNZU1RFTVMgU0FTMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA59b75MrAtDCG76tRGcd8nWGNT9R8mzBmzzllIt6FMYtstvlF/g344DpaVV31rDFjrTOO68GOdIs9gUpCA3PlcwDe6hjezbEfB4SUe4TNR1j4kbZizdel1ZkFFLZ+jE21XHxoh5F63jLbRrTDUMLu6voK50KwlafgBfEDTDOYwtzB4xxqPiQ5VZFq26lzKOabaZPgTbVIhZO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0aWNhbWFyYV8yMDE0LmNybD9jcmw9Y3JsMA0GCSqGSIb3DQEBCwUAA4IBAQAal9v5r+OkjmpTIBm1UltFgfx3QUO+Hkvl6xqgF2VHM+M9useg063X3V1ekfw5gvhieVCBN3QzkA3lNAyFjefxs8035gbPMPanPAAQHx7kX0M65RzexSBRTdJZcL68nNdCaw32Z1Ou+z85M9ptK0lXnB140EFIVDyGDXSvTkbLzHuJQGXf1xuKRXWg7pWNbJ6x11mmnAtU7VRw1gnRbzatcFOLtinRdjiEZOUNQt7sHTVZIAZVYWg0M3FMNCBmapMVZlFhtAcXQ52c4HkWcMJszeeuVNESAJYyXzuiwVV4vU4t1qJuaEvmaYrMunR/VltDS6LFqhjCN1LU0uK5Uxfj
</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>

Pero los valores que obtengo para el DigestValue son erroneos. 
Agradecería su ayuda, saludos…

Hi Spartanoscar,

Thanks for participating!

We take our Code of Conduct very seriously and want to help ensure that everyone has a good experience free of antagonism and harassment. Unfortunately, we don’t currently have moderators for languages other than English. What this means is that, until that changes, we need to ask that everyone use English here in the GitHub Community Forum when posting. We’ll be locking any posts in languages other than English for now, including this one.

Thanks for meeting us halfway.

1 Like