0

Estoy hace unos días intentando enviar una request de SOAP al servidor de DGI de Uruguay ("https://serviciosdp.dgi.gub.uy:6491/RUTWSPGetEntidad/servlet/arutpersonagetentidad"). El problema es que el XML a enviar es muy específico y ha sido complicado. Desde SOAP UI puedo enviar bien la solicitud, y traer información pero desde código no he podido. Probé desde Python recrear la estructura del XML y pude hacerlo, pero no pude crear la firma, así que intenté hacerlo en Java, logré firmar pero me está dando este error el servidor "Unsupported or unrecognized Signature signer format in the message"

Mi código Java es este:

        dbFactory.setNamespaceAware(true);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.newDocument();

        Element envelope = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soapenv:Envelope");
        envelope.setAttribute("xmlns:dgi", "DGI_Modernizacion_Consolidado");
        doc.appendChild(envelope);

        Element header = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soapenv:Header");
        envelope.appendChild(header);

        Element security = doc.createElementNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse:Security");
        security.setAttribute("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
        security.setAttribute("xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        header.appendChild(security);

        Element binarySecurityToken = doc.createElementNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse:BinarySecurityToken");
        binarySecurityToken.setAttribute("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
        binarySecurityToken.setAttribute("ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
        binarySecurityToken.setAttributeNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "wsu:Id", "X509-98AA1C4C993D13165517193243203236");
        binarySecurityToken.setTextContent(Base64.getEncoder().encodeToString(cert.getEncoded()));
        security.appendChild(binarySecurityToken);

        Element body = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soapenv:Body");
        body.setAttributeNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "wsu:Id", "id-13C5A47EBE15A189E717193383508079");
        body.setAttribute("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

        envelope.appendChild(body);

        Element execute = doc.createElementNS("DGI_Modernizacion_Consolidado", "dgi:RUTPersonaGetEntidad.Execute");
        body.appendChild(execute);

        Element ruc = doc.createElementNS("DGI_Modernizacion_Consolidado", "dgi:Ruc");
        ruc.setTextContent("5");
        execute.appendChild(ruc);

        Element canonicalizationMethod = doc.createElementNS("http://www.w3.org/2000/09/xmldsig#", "ds:CanonicalizationMethod");
        canonicalizationMethod.setAttribute("Algorithm", CanonicalizationMethod.EXCLUSIVE);

        Element inclusiveNamespacesCanonicalization = doc.createElementNS("http://www.w3.org/2001/10/xml-exc-c14n#", "ec:InclusiveNamespaces");
        inclusiveNamespacesCanonicalization.setAttribute("PrefixList", "dgi soapenv");
        inclusiveNamespacesCanonicalization.setAttribute("xmlns:ec", CanonicalizationMethod.EXCLUSIVE);
        canonicalizationMethod.appendChild(inclusiveNamespacesCanonicalization);

        Element digestMethod = doc.createElementNS("http://www.w3.org/2000/09/xmldsig#", "ds:DigestMethod");
        digestMethod.setAttribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");

        String contentToDigest = getContentAsString(doc, execute);
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] digestBytes = md.digest(contentToDigest.getBytes("UTF-8"));
        String digestValueBase64 = Base64.getEncoder().encodeToString(digestBytes);

        Element digestValue = doc.createElementNS("http://www.w3.org/2000/09/xmldsig#", "ds:DigestValue");
        digestValue.setTextContent(digestValueBase64);

        XMLSignature sig = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256, CanonicalizationMethod.EXCLUSIVE);
        security.appendChild(sig.getElement());

        Element signedInfo = sig.getSignedInfo().getElement();
        Element canonicalizationMethodSignedInfo = (Element) signedInfo.getElementsByTagNameNS(Constants.SignatureSpecNS, Constants._TAG_CANONICALIZATIONMETHOD).item(0);
        Element inclusiveNamespacesSignedInfo = doc.createElementNS("http://www.w3.org/2001/10/xml-exc-c14n#", "ec:InclusiveNamespaces");
        inclusiveNamespacesSignedInfo.setAttribute("PrefixList", "dgi soapenv");
        inclusiveNamespacesSignedInfo.setAttribute("xmlns:ec", CanonicalizationMethod.EXCLUSIVE);
        canonicalizationMethodSignedInfo.appendChild(inclusiveNamespacesSignedInfo);

        Element reference = doc.createElementNS("http://www.w3.org/2000/09/xmldsig#", "ds:Reference");
        reference.setAttribute("URI", "#id-13C5A47EBE15A189E717193383508079");

        Transforms transforms = new Transforms(doc);
        transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
        transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);

        Element transformsElement = doc.createElementNS("http://www.w3.org/2000/09/xmldsig#", "ds:Transforms");
        Element transformElement = doc.createElementNS("http://www.w3.org/2000/09/xmldsig#", "ds:Transform");

        Element inclusiveNamespacesTransform = doc.createElementNS("http://www.w3.org/2001/10/xml-exc-c14n#", "ec:InclusiveNamespaces");
        inclusiveNamespacesTransform.setAttribute("PrefixList", "dgi");
        inclusiveNamespacesTransform.setAttribute("xmlns:ec", CanonicalizationMethod.EXCLUSIVE);
        transformElement.appendChild(inclusiveNamespacesTransform);

        transformsElement.appendChild(transformElement);
        transformElement.setAttribute("Algorithm", CanonicalizationMethod.EXCLUSIVE);

        reference.appendChild(transformsElement);
        reference.appendChild(digestMethod);
        reference.appendChild(digestValue);

        Element keyInfo = doc.createElementNS("http://www.w3.org/2000/09/xmldsig#", "ds:KeyInfo");
        keyInfo.setAttribute("Id", "KI-13C5A47EBE15A189E717193383508057");

        Element securityTokenReference = doc.createElementNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse:SecurityTokenReference");
        securityTokenReference.setAttributeNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "wsu:Id", "STR-13C5A47EBE15A189E717193383508058");
        keyInfo.appendChild(securityTokenReference);

        Element referenceKeyInfo = doc.createElementNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse:Reference");
        referenceKeyInfo.setAttribute("URI", "#X509-13C5A47EBE15A189E717193383508026");
        referenceKeyInfo.setAttribute("ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
        securityTokenReference.appendChild(referenceKeyInfo);

        signedInfo.appendChild(reference);
        Element signature = sig.getElement();
        signature.setAttribute("Id", "SIG-13C5A47EBE15A189E7171933835081310");

        signature.appendChild(keyInfo);

        sig.sign(privateKey);

        byte[] xmlBytes = transformDocumentToBytes(doc);

        String response = sendSoapRequest(xmlBytes);
        System.out.println("Response: " + response);

Este es el XML que estoy obteniendo desde el Java (formateado ya que lo crea con ciertos saltos de línea):

    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
            xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="X509-98AA1C4C993D13165517193243203236">MIIGdDCCBF...</wsse:BinarySecurityToken>
            <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="SIG-13C5A47EBE15A189E7171933835081310">
                <ds:SignedInfo>
                    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                        <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="dgi soapenv" />
                    </ds:CanonicalizationMethod>
                    <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
                    <ds:Reference URI="#id-13C5A47EBE15A189E717193383508079">
                        <ds:Transforms>
                            <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                                <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="dgi" />
                            </ds:Transform>
                        </ds:Transforms>
                        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                        <ds:DigestValue>dCUcL+zaVPT/Fwd+/CTwM7hiQocsfcF6yNzvNmsA/c4=</ds:DigestValue>
                    </ds:Reference>
                </ds:SignedInfo>
                <ds:SignatureValue>uvy...</ds:SignatureValue>
                <ds:KeyInfo Id="KI-13C5A47EBE15A189E717193383508057">
                    <wsse:SecurityTokenReference wsu:Id="STR-13C5A47EBE15A189E717193383508058">
                        <wsse:Reference URI="#X509-13C5A47EBE15A189E717193383508026" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" />
                    </wsse:SecurityTokenReference>
                </ds:KeyInfo>
            </ds:Signature>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-13C5A47EBE15A189E717193383508079">
        <dgi:RUTPersonaGetEntidad.Execute>
            <dgi:Ruc>5</dgi:Ruc>
        </dgi:RUTPersonaGetEntidad.Execute>
    </soapenv:Body>
</soapenv:Envelope> 

Y por último el XML que envía SOAP UI y que sí me devuelve información es este:

<soapenv:Envelope xmlns:dgi="DGI_Modernizacion_Consolidado" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="X509-AE7AA35874249F9BCC171940795173114">MIIGdDCCBF...</wsse:BinarySecurityToken><ds:Signature Id="SIG-AE7AA35874249F9BCC171940795173518" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"><ec:InclusiveNamespaces PrefixList="dgi soapenv" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:CanonicalizationMethod><ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/><ds:Reference URI="#id-AE7AA35874249F9BCC171940795173217"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"><ec:InclusiveNamespaces PrefixList="dgi" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transform></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>edCqAbYrwOKYrCTlFg++vuivMgmrvCPYaZmt3N5Z+pQ=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>P1ipeBGPD...</ds:SignatureValue><ds:KeyInfo Id="KI-AE7AA35874249F9BCC171940795173115"><wsse:SecurityTokenReference wsu:Id="STR-AE7AA35874249F9BCC171940795173116"><wsse:Reference URI="#X509-AE7AA35874249F9BCC171940795173114" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/></wsse:SecurityTokenReference></ds:KeyInfo></ds:Signature></wsse:Security></soapenv:Header>
   <soapenv:Body wsu:Id="id-AE7AA35874249F9BCC171940795173217" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
      <dgi:RUTPersonaGetEntidad.Execute>
         <dgi:Ruc>5</dgi:Ruc>
      </dgi:RUTPersonaGetEntidad.Execute>
   </soapenv:Body>
</soapenv:Envelope>

Estoy atento a cualquier respuesta, muchas gracias.

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.