gdiazs / MITyCLib

Componentes de firma MITyC
14 stars 28 forks source link

Firma se inserta al final del documento. #3

Closed juanp88 closed 7 years ago

juanp88 commented 7 years ago

Cordial saludo, estoy tratando de firmar un xml empleando la especificación Xades-EPES. Encontré esta librería y traté usando la clase XAdESEPESSignature pero me genera la firma al final del documento duplicandome las etiquetas </fe:invoice> y . Hay alguna forma que la firma se inserte entre las etiquetas </ext:ExtensionContent>?

Este es el resultado de mi firma:

<?xml version="1.0" encoding="UTF-8"?>
<fe:Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:clm54217="urn:un:unece:uncefact:codelist:specification:54217:2001" xmlns:clm66411="urn:un:unece:uncefact:codelist:specification:66411:2001" xmlns:clmIANAMIMEMediaType="urn:un:unece:uncefact:codelist:specification:IANAMIMEMediaType:2003" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:fe="http://www.dian.gov.co/contratos/facturaelectronica/v1" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:sts="http://www.dian.gov.co/contratos/facturaelectronica/v1/Structures" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dian.gov.co/contratos/facturaelectronica/v1 ../xsd/DIAN_UBL.xsd urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2 ../../ubl2/common/UnqualifiedDataTypeSchemaModule-2.0.xsd urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2 ../../ubl2/common/UBL-QualifiedDatatypes-2.0.xsd">
<ext:UBLExtensions>
    <ext:UBLExtension>
        <ext:ExtensionContent>
            <sts:DianExtensions>
                <sts:InvoiceControl>
                    <sts:InvoiceAuthorization>9000000500017960</sts:InvoiceAuthorization>
                    <sts:AuthorizationPeriod><cbc:StartDate>2016-07-11</cbc:StartDate>
                    <cbc:EndDate>2018-07-11</cbc:EndDate>
                    </sts:AuthorizationPeriod>
                    <sts:AuthorizedInvoices>
                        <sts:Prefix>PRUE</sts:Prefix>
                        <sts:From>980000000</sts:From>
                        <sts:To>985000000</sts:To>
                        </sts:AuthorizedInvoices>
                        </sts:InvoiceControl>
                        <sts:InvoiceSource>
                            <cbc:IdentificationCode listAgencyID="6" listAgencyName="United Nations Economic Commission for Europe" listSchemeURI="urn:oasis:names:specification:ubl:codelist:gc:CountryIdentificationCode-2.0">CO</cbc:IdentificationCode>
                            </sts:InvoiceSource><sts:SoftwareProvider><sts:ProviderID schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)">900373115</sts:ProviderID>
                            <sts:SoftwareID schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)">0d2e2883-eb8d-4237-87fe-28aeb71e961e</sts:SoftwareID>
                            </sts:SoftwareProvider>
                            <sts:SoftwareSecurityCode schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)">bdaa51c9953e08dcc8f398961f7cd0717cd5fbea356e937660aa1a8abbe31f4c9b4eb5cf8682eaca4c8523953253dcce</sts:SoftwareSecurityCode>
                            </sts:DianExtensions>
                            </ext:ExtensionContent>
                            </ext:UBLExtension>
                            <ext:UBLExtension>
                                <ext:ExtensionContent>

</ext:ExtensionContent>
</ext:UBLExtension>
</ext:UBLExtensions>

<cbc:UBLVersionID>UBL 2.0</cbc:UBLVersionID>
<cbc:ProfileID>DIAN 1.0</cbc:ProfileID>
<cbc:ID>ORIGINAL</cbc:ID>
<cbc:UUID schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)">c1db672511b492915a6f88a2aeed9f2f7eae0f38</cbc:UUID>
<cbc:IssueDate>2017-08-16</cbc:IssueDate>
<cbc:IssueTime>17:41:34</cbc:IssueTime>
<cbc:InvoiceTypeCode listAgencyID="195" listAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)" listSchemeURI="http://www.dian.gov.co/contratos/facturaelectronica/v1/InvoiceType">1</cbc:InvoiceTypeCode>
<cbc:Note>Informacion libre sobre el documento</cbc:Note>
<cbc:DocumentCurrencyCode>COP</cbc:DocumentCurrencyCode>

<fe:AccountingSupplierParty>
    <cbc:AdditionalAccountID>1</cbc:AdditionalAccountID>
    <fe:Party>
        <cac:PartyIdentification>
            <cbc:ID schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)" schemeID="31">$nit</cbc:ID>
        </cac:PartyIdentification>
        <cac:PartyName>
            <cbc:Name>$NombreEmpresa</cbc:Name>
        </cac:PartyName>
        <fe:PhysicalLocation>
            <fe:Address>
                <cbc:Department>Distrito Capital</cbc:Department>           
                <cbc:CityName>Bogotá</cbc:CityName>
                <cac:AddressLine>
                    <cbc:Line>  carrera 8 Nº 6C - 78</cbc:Line>
                </cac:AddressLine>
                <cac:Country>
                    <cbc:IdentificationCode>CO</cbc:IdentificationCode>
                </cac:Country>
            </fe:Address>
        </fe:PhysicalLocation>
        <fe:PartyTaxScheme>
            <cbc:TaxLevelCode>0</cbc:TaxLevelCode>
            <cac:TaxScheme/>
        </fe:PartyTaxScheme>
        <fe:PartyLegalEntity>
            <cbc:RegistrationName>PJ - 900373115</cbc:RegistrationName>
        </fe:PartyLegalEntity></fe:Party>
</fe:AccountingSupplierParty>

<fe:AccountingCustomerParty>
    <cbc:AdditionalAccountID>2</cbc:AdditionalAccountID>
    <fe:Party>
        <cac:PartyIdentification>
            <cbc:ID schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)" schemeID="22">NitCliente</cbc:ID>
        </cac:PartyIdentification>
        <cac:PartyName>
            <cbc:Name>NOTARIA ONCE DE MEDELLIN</cbc:Name>
        </cac:PartyName>
        <fe:PhysicalLocation>
            <fe:Address>
                <cbc:Department>ANTIOQUIA</cbc:Department>          
                <cbc:CityName>MEDELLIN</cbc:CityName>
                <cac:AddressLine>
                    <cbc:Line>CARRERA 43A # 1 - 77, LOCAL 2, EDIFICIO BANCO CAJA SOCIAL</cbc:Line>
                </cac:AddressLine>
                <cac:Country>
                    <cbc:IdentificationCode>$CodigoPaisCliente</cbc:IdentificationCode>
                </cac:Country>
            </fe:Address>
        </fe:PhysicalLocation>
        <fe:PartyTaxScheme>
            <cbc:TaxLevelCode>0</cbc:TaxLevelCode>
            <cac:TaxScheme/>
        </fe:PartyTaxScheme>        
    </fe:Party>
    </fe:AccountingCustomerParty>
            <fe:TaxTotal>
            <cbc:TaxAmount currencyID="COP">256569.00</cbc:TaxAmount>
            <cbc:TaxEvidenceIndicator>false</cbc:TaxEvidenceIndicator>
            <fe:TaxSubtotal>
                <cbc:TaxableAmount currencyID="COP">1350366.00</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="COP">256569.00</cbc:TaxAmount>
                <cbc:Percent>19</cbc:Percent>
                <cac:TaxCategory>
                    <cac:TaxScheme>
                        <cbc:ID>01</cbc:ID>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </fe:TaxSubtotal>
        </fe:TaxTotal>  
            <fe:TaxTotal>
            <cbc:TaxAmount currencyID="COP">0.00</cbc:TaxAmount>
            <cbc:TaxEvidenceIndicator>false</cbc:TaxEvidenceIndicator>
            <fe:TaxSubtotal>
                <cbc:TaxableAmount currencyID="COP">1350366.00</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="COP">0.00</cbc:TaxAmount>
                <cbc:Percent>0</cbc:Percent>
                <cac:TaxCategory>
                    <cac:TaxScheme>
                        <cbc:ID>06</cbc:ID>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </fe:TaxSubtotal>
        </fe:TaxTotal>
            <fe:TaxTotal>
            <cbc:TaxAmount currencyID="COP">0.00</cbc:TaxAmount>
            <cbc:TaxEvidenceIndicator>false</cbc:TaxEvidenceIndicator>
            <fe:TaxSubtotal>
                <cbc:TaxableAmount currencyID="COP">1350366.00</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="COP">0.00</cbc:TaxAmount>
                <cbc:Percent>0</cbc:Percent>
                <cac:TaxCategory>
                    <cac:TaxScheme>
                        <cbc:ID>07</cbc:ID>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </fe:TaxSubtotal>
        </fe:TaxTotal>
            <fe:TaxTotal>
            <cbc:TaxAmount currencyID="COP">6015.00</cbc:TaxAmount>
            <cbc:TaxEvidenceIndicator>false</cbc:TaxEvidenceIndicator>
            <fe:TaxSubtotal>
                <cbc:TaxableAmount currencyID="COP">1350366.00</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="COP">6015.00</cbc:TaxAmount>
                <cbc:Percent>0</cbc:Percent>
                <cac:TaxCategory>
                    <cac:TaxScheme>
                        <cbc:ID>05</cbc:ID>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </fe:TaxSubtotal>
        </fe:TaxTotal>  

        <fe:LegalMonetaryTotal>
            <cbc:LineExtensionAmount currencyID="COP">1134840.69</cbc:LineExtensionAmount>
            <cbc:TaxExclusiveAmount currencyID="COP">156608.01</cbc:TaxExclusiveAmount>
            <cbc:PayableAmount currencyID="COP">1291448.7</cbc:PayableAmount>
        </fe:LegalMonetaryTotal>

                <fe:InvoiceLine>
            <cbc:ID>1</cbc:ID>
            <cbc:InvoicedQuantity>1</cbc:InvoicedQuantity>
            <cbc:LineExtensionAmount currencyID="COP">1200000.00</cbc:LineExtensionAmount>
            <fe:Item>
                <cbc:Description>RENTA EQUIPO IMPRESION Y MULTIFUNCION (4)</cbc:Description>
            </fe:Item>
            <fe:Price>
                <cbc:PriceAmount currencyID="COP">1200000.00</cbc:PriceAmount>
            </fe:Price> 
        </fe:InvoiceLine>
            <fe:InvoiceLine>
            <cbc:ID>2</cbc:ID>
            <cbc:InvoicedQuantity>3944</cbc:InvoicedQuantity>
            <cbc:LineExtensionAmount currencyID="COP">134096.00</cbc:LineExtensionAmount>
            <fe:Item>
                <cbc:Description>EXCEDENTE IMPRESION</cbc:Description>
            </fe:Item>
            <fe:Price>
                <cbc:PriceAmount currencyID="COP">34.00</cbc:PriceAmount>
            </fe:Price> 
        </fe:InvoiceLine>
            <fe:InvoiceLine>
            <cbc:ID>3</cbc:ID>
            <cbc:InvoicedQuantity>1627</cbc:InvoicedQuantity>
            <cbc:LineExtensionAmount currencyID="COP">16270.00</cbc:LineExtensionAmount>
            <fe:Item>
                <cbc:Description>PAGINAS ESCANEADAS</cbc:Description>
            </fe:Item>
            <fe:Price>
                <cbc:PriceAmount currencyID="COP">10.00</cbc:PriceAmount>
            </fe:Price> 
        </fe:InvoiceLine>
    <ds:Signature Id="Signature846249" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:etsi="http://uri.etsi.org/01903/v1.3.2#">
<ds:SignedInfo Id="Signature-SignedInfo1042806">
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference Id="SignedPropertiesID773447" Type="http://uri.etsi.org/01903#SignedProperties" URI="#Signature846249-SignedProperties674307">
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>KJRYZ1PhiMSQBWkWgDAvJxLbD8E=</ds:DigestValue>
</ds:Reference>
<ds:Reference URI="#Certificate1982236">
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>BvCdlP1ukYgCCskdXKToQtDUphI=</ds:DigestValue>
</ds:Reference>
<ds:Reference Id="Reference-ID-919367" 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>2qwDkQnWai8fOsDahLEej/WKIeA=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue Id="SignatureValue533454">
GMwKn3TmR2ctt0979u5WFkEeTP3x8Gd/05yW1CkYlT/DnTjKmNnYswVJ+XtjSuRLfzg/Wxi4tDFc
cwC42QokbXlSEuyuKhxGW4n1apdTktXeMML+7b/QFhRbeLUd6jZ3EHA+AEkRDjnycToSobLhssZb
w4cc4mJsyq1cUclmCXs=
</ds:SignatureValue>
<ds:KeyInfo Id="Certificate1982236">
<ds:X509Data>
<ds:X509Certificate>
MIIDvzCCAyigAwIBAgICAKEwDQYJKoZIhvcNAQEFBQAwcjELMAkGA1UEBhMCRVMxDzANBgNVBAgT
Bk1hZHJpZDEPMA0GA1UEBxMGTWFkcmlkMQ4wDAYDVQQKEwVNSVR5QzEbMBkGA1UECxMSTUlUeUMg
RE5JZSBQcnVlYmFzMRQwEgYDVQQDEwtDQSB1c3VhcmlvczAeFw0xMDA0MDgxMjM4MjJaFw0yMDA0
MDUxMjM4MjJaMHwxCzAJBgNVBAYTAkVTMQ8wDQYDVQQIEwZNYWRyaWQxDzANBgNVBAcTBk1hZHJp
ZDEOMAwGA1UEChMFTUlUeUMxGzAZBgNVBAsTEk1JVHlDIEROSWUgUHJ1ZWJhczEeMBwGA1UEAxMV
VXN1YXJpbyBkZSBwcnVlYmFzIDYxMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2ehoLcO6l
XWmKzJfdz2m+vRZmGeDo5OF+Q8MNdVtL8AKbMykP6G9JOzBT3WLhzQKszMg43DQjViN6mTQsPLYC
fe/n6LmTWZkRvsIzrffMaL7goy47VCX1CeUQ80cuUAHJpRq7UMObNvgV/8rn+zPfYmErqZVhAckl
eP4/RgeC9QIDAQABo4IBWDCCAVQwCQYDVR0TBAIwADALBgNVHQ8EBAMCBeAwHQYDVR0OBBYEFNhw
w+tqmd7gvdedMv0Gk1mEolYdMIGYBgNVHSMEgZAwgY2AFPWhaqh3T1uxBIynfkjxDp/Fdo8boXKk
cDBuMQ8wDQYDVQQIEwZNYWRyaWQxDzANBgNVBAcTBk1hZHJpZDEOMAwGA1UEChMFTUlUeUMxGzAZ
BgNVBAsTEk1JVHlDIEROSWUgUHJ1ZWJhczEQMA4GA1UEAxMHUm9vdCBDQTELMAkGA1UEBhMCRVOC
AQMwCQYDVR0RBAIwADA2BgNVHRIELzAthitodHRwOi8vbWluaXN0ZXItOGpneHk5Lm1pdHljLmFn
ZS9QS0kvQ0EuY3J0MD0GA1UdHwQ2MDQwMqAwoC6GLGh0dHA6Ly9taW5pc3Rlci04amd4eTkubWl0
eWMuYWdlL1BLSS9jcmwuY3JsMA0GCSqGSIb3DQEBBQUAA4GBADn3vfsgfhyreHhn4VCG4WuG+g5q
TRKpu72ZScMbyY+e3d2m6fOSMkoEC+NwaXgl4Y/vKlXGKgdhYjLDWjobRDVQOQRQ4Q/Wv/aPPno1
CjBkYdY5rnCM/oiy8QXNjdboXRiE40kCyqj7jiopB20uO2a0yacC5ooWQz1pqZEhSWRD
</ds:X509Certificate>
</ds:X509Data>
<ds:KeyValue>
<ds:RSAKeyValue>
<ds:Modulus>
tnoaC3DupV1pisyX3c9pvr0WZhng6OThfkPDDXVbS/ACmzMpD+hvSTswU91i4c0CrMzIONw0I1Yj
epk0LDy2An3v5+i5k1mZEb7CM633zGi+4KMuO1Ql9QnlEPNHLlAByaUau1DDmzb4Ff/K5/sz32Jh
K6mVYQHJJXj+P0YHgvU=
</ds:Modulus>
<ds:Exponent>AQAB</ds:Exponent>
</ds:RSAKeyValue>
</ds:KeyValue>
</ds:KeyInfo>
<ds:Object Id="Signature846249-Object782076"><etsi:QualifyingProperties Target="#Signature846249"><etsi:SignedProperties Id="Signature846249-SignedProperties674307"><etsi:SignedSignatureProperties><etsi:SigningTime>2017-09-04T11:12:49-05:00</etsi:SigningTime><etsi:SigningCertificate><etsi:Cert><etsi:CertDigest><ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><ds:DigestValue>3+JZSMCKF3YCSOhGFNiEpBEKvPY=</ds:DigestValue></etsi:CertDigest><etsi:IssuerSerial><ds:X509IssuerName>CN=CA usuarios,OU=MITyC DNIe Pruebas,O=MITyC,L=Madrid,ST=Madrid,C=ES</ds:X509IssuerName><ds:X509SerialNumber>161</ds:X509SerialNumber></etsi:IssuerSerial></etsi:Cert></etsi:SigningCertificate><etsi:SignaturePolicyIdentifier><etsi:SignaturePolicyImplied/></etsi:SignaturePolicyIdentifier><etsi:SignerRole><etsi:ClaimedRoles><etsi:ClaimedRole>Rol de firma</etsi:ClaimedRole></etsi:ClaimedRoles></etsi:SignerRole></etsi:SignedSignatureProperties><etsi:SignedDataObjectProperties><etsi:DataObjectFormat ObjectReference="#Reference-ID-919367"><etsi:Description>Documento de ejemplo</etsi:Description><etsi:MimeType>text/xml</etsi:MimeType></etsi:DataObjectFormat></etsi:SignedDataObjectProperties></etsi:SignedProperties></etsi:QualifyingProperties></ds:Object></ds:Signature></fe:Invoice>
gdiazs commented 7 years ago

Hola @juanp88 este proyecto viene originalmente de aquí es de un snapshot de webarchive.org, porque ya no existe la pagina. Hay un poco de documentación.

El proyecto me pareció un buen esfuerzo y me lo traje para github y después de eso, vi que cerraron la página (no sé si se debió a eso jaja).

En cuanto a tu duda, hay varios tipso de firma qué puedes consultar aquí, primero descartar que no sea un tema de formato o tipo de firma para así validar si realmente es un error o bug de la librería.

Recuerdo que hay una propiedad del DataToSign, donde se configura el id, del nodo a tener la firma.

dataToSign.setParentSignNode(this.parentId);

Saludos.

juanp88 commented 7 years ago

Muchas gracias @gdiazs por la respuesta, pude econtrar lo que dices y la forma de ingresar nodo deseado es usando el setter de dicha clase.

dataToSign.setParentSignNode(node);

Se puede usar el tagname o el ID, aunque en mi caso solo me sirve el tagname. Te agradezco que hayas subido este proyecto a github porque llevo mucho tiempo buscando la forma de firmat electronicamente archivos xml con la especificación Xades-EPES y este es el unico que he podido hacer funcionar. Tambien me tope con la librería Xades4j pero su documentación es algo general y no me funcionó. Si sabes algo de esa te agradecería me orientaras ya que tambien me interesa.

gdiazs commented 7 years ago

Genial, cualquier aporte que quieras hacer al proyecto bienvenido sea, está muy falto de documentación por si quieres empezar por allí :), como ejemplos básicos de uso en el Readme.

Sobre xades4j, intercambié correos con el iniciador del proyecto y le hice algunas consultas sobre Xades-XL y me dijo que no estaba soportada (eso en aquel tiempo), asi que no seguí investigando sobre esas librerías, asi que no creo poder ayudarte.

maparamo commented 6 years ago

Buen dia, Juan. Podrias ayudarme en la firma de documentos XML, me he acabado el celebro en PHP y no me funciona,

Mil gracias.

lvillamar commented 6 years ago

@juanp88 , Hola que tal a la final como te fue con tu caso, de agregar la firma en el nodo ext:ExtensionContent</ext:ExtensionContent>?, podrias contactarme. Saludos Luis

juanp88 commented 6 years ago

Hola Luis, si pude agregar la firma pero tuve que cambiarme a la librería Xades4J, a esta le quitaron algo entonces la ultima vez que cloné el proyecto ya no me funcionaba nada. Te sugiero le des una mirada a ese proyecto: https://github.com/luisgoncalves/xades4j.