使用X509证书验证SOAP请求

8
我有一个SOAP服务器。在服务器接收的SOAP请求中,有 WS安全头部。以下是请求XML的主要节点:
  1. BinarySecurityToken(X509PKIPathv1证书)
  2. DigestMethod
  3. DigestValue
  4. SignatureValue
  5. SecurityTokenReference

  6. Data(客户端在SOAP正文中发送的数据)

我必须使用由客户端(请求者)提供的证书(.cer文件)验证请求
请解释验证请求的步骤和概念。没有可用于执行此操作的库。 经过长时间的研究,我能够将BinarySecurityTokenbase64_encode($certFile)匹配起来。其中$certFile是请求者的证书。现在我正在研究如何将DigestValue与什么匹配。
2个回答

5

以下是验证WS-Security头信息的方法。我已经为此编写了一个实用程序,请查看它。

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;
    }
}

注意 您必须按原样提供SOAP消息。您不应该进行任何XML格式或在某个地方添加空格。增加安全性的SOAP消息非常敏感。即使在末尾添加一个空格也会使SOAP消息无效。


X509KeySelector类的源代码是什么? - janv8000
@janv8000 导入 java.security.cert.X509Certificate。 - Pasupathi Rajamanickam

-1

X509KeySelector构造函数类型不正确


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接