1

Turns out that I have tested a webservice via soapUI configuring a keystore with a pem certificate I got.

When I apply the outgoing WSSE signature I get a wsse:Security element like this:

    <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">
<ds:Signature Id="SIG-1C747258C55C77E5C5154835835043446"
    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="hal ns ns1 ns2 soapenv"
                xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/>
            </ds:CanonicalizationMethod>
            <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
            <ds:Reference URI="#id-1C747258C55C77E5C515483521292064">
                <ds:Transforms>
                    <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                        <ec:InclusiveNamespaces PrefixList="hal ns ns1 ns2"
                            xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/>
                        </ds:Transform>
                    </ds:Transforms>
                    <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                    <ds:DigestValue>AfQsslNyqfZcR2GwBV+0vtAuO/c=</ds:DigestValue>
                </ds:Reference>
            </ds:SignedInfo>
            <ds:SignatureValue>
                [THE SIGNATURE]
            </ds:SignatureValue>
            <ds:KeyInfo Id="KI-1C747258C55C77E5C5154835835043344">
                <wsse:SecurityTokenReference wsu:Id="STR-1C747258C55C77E5C5154835835043345">
                    <wsse:KeyIdentifier 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#X509SubjectKeyIdentifier">VwDFSfuw3Zk0GeAG4PhV8YWZ2P4=</wsse:KeyIdentifier>
                </wsse:SecurityTokenReference>
            </ds:KeyInfo>
        </ds:Signature>
    </wsse:Security>

Im interested in generating this exact block with nodejs. I tried using the soap package, but it always returns an error "Unable To Parse Request" which if I replace the signature block with this works.

So basically I want to know the steps to manually sign the body and replace the hashes, to then replace them in this block and do a simple post :)

I hope you don't find this question naive (I'm very new to SOAP webservices).

Thanks!

RicardoE
  • 1,665
  • 6
  • 24
  • 42

1 Answers1

1

I gave up doing it with nodeJS and made it with java instead. Here is how I did it:

import org.apache.ws.security.WSConstants;
import org.apache.ws.security.components.crypto.Crypto;
import org.apache.ws.security.components.crypto.CryptoFactory;
import org.apache.ws.security.message.WSSecHeader;
import org.apache.ws.security.message.WSSecSignature;
import org.apache.xml.security.algorithms.MessageDigestAlgorithm;
import org.apache.xml.security.signature.XMLSignature;
import org.w3c.dom.Document;
import java.io.StringWriter;
import java.util.Properties;

import static java.nio.file.Files.readAllBytes;
import static java.nio.file.Paths.get;

public class WSSecuritySign {

    public static void signXml() throws Exception {

        final String req = new String(readAllBytes(get("target/classes/test.xml"))); //the soap envelope to be signed without the security header
        final Document soapDocument = XmlUtils.parseXml(req);

        final WSSecHeader secHeader = new WSSecHeader();
        final WSSecSignature wssSign = new WSSecSignature();

        final Crypto wssCrypto = getCrypto();

        secHeader.insertSecurityHeader(soapDocument);

        wssSign.setSignatureAlgorithm(XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
        wssSign.setSigCanonicalization(WSConstants.C14N_EXCL_OMIT_COMMENTS);
        wssSign.setUseSingleCertificate(false);
        wssSign.setDigestAlgo(MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA1);
        wssSign.setKeyIdentifierType(WSConstants.CUSTOM_KEY_IDENTIFIER);

        wssSign.setUserInfo("keystore_alias", "a_password");
        wssSign.setKeyIdentifierType(4); //Or Subject Key Identifier

        wssSign.build(soapDocument, wssCrypto, secHeader);

        final StringWriter writer = new StringWriter();
        XmlUtils.serialize(soapDocument.getDocumentElement(), writer);
        System.out.println(writer.toString());
    }
    public static void main(String... unused) throws Exception {
        signXml();
    }

    public static Crypto getCrypto() throws Exception {
        Properties props =  new Properties();
        props.setProperty("org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin");
        props.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "a_password");
        props.setProperty("org.apache.ws.security.crypto.merlin.keystore.alias", "keystore_alias");
        props.setProperty("org.apache.ws.security.crypto.merlin.keystore.file", "keystore.jks");

        return CryptoFactory.getInstance(props, ClassLoader.getSystemClassLoader());
    }
}

Don't forget to replace your keystore alias name, the location to it and the passphrase (if any), I think there is a default passphrase if your keystore doesn't have one, don't remember how it is named.

RicardoE
  • 1,665
  • 6
  • 24
  • 42
  • Where is XmlUtils coming from? I get a "XmlUtils cannot be resolved" error. – Keeleon Oct 19 '20 at 22:18
  • 1
    Very true, this is a class I came up with (don't remember exactly why), but here is the source: https://gist.github.com/RicardoGeek/f299fea401e6b170cf69dc2aedba483f Should be placed in the same package as the other class – RicardoE Oct 21 '20 at 08:38