Authenticate SOAP request with X509 certificate


Kiren Siva:

I have a SOAP server. The SOAP request received on the server has the ws security header . Following is the main node of the request XML.

  1. BinarySecurityToken (X509PKIPathv1 certificate)
  2. summary method
  3. summary value
  4. Signature value
  5. SecurityTokenReference

  6. data (data sent by the client in the SOAP body)

I have to use the certificate (.cer file) provided by the client (request sender ) to authenticate the request.

What are the steps to verify the request? Please explain this concept. There is no library available to do this. After long research, I was able to match BinarySecurityToken$ base64_encode($certFile)certFile to be the requester's certifiate. Now, I'm working out how to match DigestValueit .

Pasupathi Rajamanickam

WS-Security headers can be validated in the following ways. I have written a utility for this. Check it out.

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.security.KeyStore;
import java.security.Provider;
import java.security.PublicKey;
import java.security.cert.X509Certificate;

import javax.xml.crypto.dsig.XMLSignature;
import javax.xml.crypto.dsig.XMLSignatureFactory;
import javax.xml.crypto.dsig.dom.DOMValidateContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;


public class WSUtil {
    public static void main(String[] args) throws Exception {

        String req = "SOAPMESSAGE";
        Document p = createXMLDocument(req);
        InputStream inStream = new FileInputStream("certificate.p12"); //Provide your certificate file

        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(inStream, "pass".toCharArray()); //Certificate password - pass

        String alias = ks.aliases().nextElement();
        X509Certificate certificate = (X509Certificate) ks.getCertificate(alias);

        validateSignature(p.getElementsByTagName("ds:Signature").item(0),p.getElementsByTagName("soapenv:Body").item(0),certificate.getPublicKey());//True if the message is valid
    }

    public static Document createXMLDocument(String xmlString) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document document = null;
        try {
            builder = factory.newDocumentBuilder();
            document = builder.parse(new InputSource(
                    new StringReader(xmlString)));
        } catch (Exception e) {
            throw e;
        }
        return document;
    }

    private static boolean validateSignature(Node signatureNode, Node bodyTag, PublicKey publicKey) {
        boolean signatureIsValid = false;
        try {
            // Create a DOM XMLSignatureFactory that will be used to unmarshal the
            // document containing the XMLSignature
            String providerName = System.getProperty
                    ("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
            XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM",
                    (Provider) Class.forName(providerName).newInstance());

            // Create a DOMValidateContext and specify a KeyValue KeySelector
            // and document context
            DOMValidateContext valContext = new DOMValidateContext(new X509KeySelector(publicKey), signatureNode);
            valContext.setIdAttributeNS((Element) bodyTag, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id");

            // Unmarshal the XMLSignature.
            XMLSignature signature = fac.unmarshalXMLSignature(valContext);
            // Validate the XMLSignature.
            signatureIsValid = signature.validate(valContext); 

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return signatureIsValid;
    }
}

Note that you must provide the SOAP message as-is. You shouldn't be doing any XML formatting or any whitespace anywhere. SOAP messages with added security are very sensitive. Even trailing spaces can invalidate a SOAP message.

Related


Authenticate SOAP request with X509 certificate

Kiren Siva: I have a SOAP server. The SOAP request received on the server has the ws security header . Following is the main node of the request XML. BinarySecurityToken (X509PKIPathv1 certificate) summary method summary value Signature value SecurityTokenRefe

Authenticate SOAP request with X509 certificate

Kiren Siva: I have a SOAP server. The SOAP request received on the server has the ws security header . Following is the main node of the request XML. BinarySecurityToken (X509PKIPathv1 certificate) summary method summary value Signature value SecurityTokenRefe

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Unable to authenticate user in Kubernetes using x509 certificate

Ajov Crowe I am using the following versions: Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T11:52:23Z", GoVersion:"go1.9.2", Compiler:"g

Authenticate SOAP-Request with SOAPHandler

Ruben I am trying to validate a SOAP request against a schema defined in WSDL. I am using SOAPHandler with SOAP-Handler-Chain. The only problem with it working is that when I validate the request, I get the following error message: cvc-complex-type.2.4.a: Inva

Authenticate SOAP-Request with SOAPHandler

Ruben I am trying to validate a SOAP request against a schema defined in WSDL. I am using SOAPHandler with SOAP-Handler-Chain. The only problem with it working is that when I validate the request, I get the following error message: cvc-complex-type.2.4.a: Inva

Certificate chain X509

Omar Amalfi Hi I want to generate certificate chain using c#. Something like this: I created this code for generation: using System; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; namespace CC.CertificateCore { pu

Java X509 certificate parsing and verification

Driss Amri I'm trying to handle X509 certificates in several steps and have run into two issues. I'm new to JCE so I'm not fully up to date yet. We want to be able to parse several different X509 certificates based on different encodings (PEM, DER and PCKS7).

Detect root x509 certificate in Go

Adam Williams: I have X509 certificate obtained using: block, additionalData := pem.Decode([]byte(str)) cert, err := x509.ParseCertificate(block.Bytes) I want to check if the certificate is a root certificate. I've tried checking IsCA, but the same is true fo

Invalid X509 certificate for Kubernetes Master

user1208081: I am trying to contact my k8s master server from my workstation. I have normal access to the host over the LAN, but not from my workstation. The error message is: % kubectl --context=employee-context get pods Unable to connect to the server: x509: