使用 WSSVerification API 验证签名
可通过使用 Web Service 安全性 API (WSS API) 保护 SOAP 消息,而不必对配置使用策略集。要验证客户端请求的使用者绑定部分的签名信息,请使用 WSSVerification API。另外,还必须指定要进行验证的算法方法和要进行验证的 SOAP 消息的签名部件。WSSVerification API 是 com.ibm.websphere.wssecurity.wssapi.verification 包的一部分。
开始之前
使用 WSS API 来验证签名信息或通过管理控制台来配置策略集以验证签名信息。要保护 SOAP 消息,必须完成下列签名任务:
- 配置签名信息。
- 选择算法方法进行签名和签名验证。
- 验证签名信息。
关于此任务
WebSphere® Application Server 使用缺省生成器的签名信息来签署消息的部件,并将 XML 数字签名与诸如 RSA-SHA1 和 HMAC-SHA1 之类的现有算法配合使用。
XML 签名定义许多用于描述密钥信息的方法并启用新方法的定义。使用 XML 签名时,通常需要 XML 规范化 (C14N)。在已序列化的 XML 文档中可用各种方法表示信息。C14N 过程用于对 XML 信息进行规范化。由于规范化的信息取决于此算法,因此要选择适当的 C14N 算法。
下表显示了定义数字签名安全性约束(完整性)时的必需和可选绑定信息。
验证部件 | 描述 |
---|---|
关键字 | 通过使用关键字将必需签名部件添加为验证的目标。可以在生成器端上请求的消息保护中指定不同的消息部件。对必需签名验证部件使用下列关键字:
|
xpath | 通过使用 XPath 表达式添加验证部件。 |
part | 将 WSSVerifyPart 对象添加为验证部件。 |
header | 将由 QName 指定的头添加为验证部件。 |
为了获取签名验证信息,将执行特定的缺省行为。使用 WSSVerification API 的最简单方法是使用缺省行为。
WSS API 为摘要方法、变换方法、安全性令牌和必需的验证部件定义了缺省值。
签名验证决策 | 缺省行为 |
---|---|
要使用的签名方法(算法) | 设置签名算法方法。数据加密、签名和规范化均可以指定。缺省签名方法是 RSA SHA1。WebSphere Application Server 支持以下预配置的签名方法:
|
要使用的规范方法(算法) | 设置规范算法方法。数据加密、签名和规范化均可以指定。缺省签名方法是 EXC_C14N。WebSphere Application Server 支持以下预配置的规范方法:
|
签名确认是否是必需的 | 如果 WSSSignature API 指定签名确认是必需的,那么 WSSVerification API 将验证响应消息中的签名确认值(在接收到响应消息时,响应消息会连接签名确认值)。签名确认在 OASIS Web Services Security V1.1 规范中定义。 缺省签名确认是 false。 |
要指定的安全性令牌 (securityToken) | 将 securityToken 对象添加为签名部件。WebSphere Application Server 设置令牌信息以用于验证。 WebSphere Application Server 支持以下预配置的令牌进行签署:
|
过程
结果
示例
以下示例提供了样本代码,这段代码使用了在 WSSVerification API 中定义的方法:
// Get the message context
Object msgcontext = getMessageContext();
// Generate the WSSFactory instance (step: a)
WSSFactory factory = WSSFactory.getInstance();
// Generate the WSSConsumingContext instance (step: b)
WSSConsumingContext concont = factory.newWSSConsumingContext();
// Generate the certificate list
String certpath = "c:/WebSphere/AppServer/etc/ws-security/samples/intca2.cer";
// The location of the X509 certificate file
X509Certificate x509cert = null;
try {
InputStream is = new FileInputStream(certpath);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
x509cert = (X509Certificate)cf.generateCertificate(is);
} catch(FileNotFoundException e1){
throw new WSSException(e1);
} catch (CertificateException e2) {
throw new WSSException(e2);
}
Set<Object> eeCerts = new HashSet<Object>();
eeCerts.add(x509cert);
// Create the certificate store
java.util.List<CertStore> certList = new java.util.ArrayList<CertStore>();
CollectionCertStoreParameters certparam = new CollectionCertStoreParameters(eeCerts);
CertStore cert = null;
try {
cert = CertStore.getInstance("Collection", certparam, "IBMCertPath");
} catch (NoSuchProviderException e1) {
throw new WSSException(e1);
} catch (InvalidAlgorithmParameterException e2) {
throw new WSSException(e2);
} catch (NoSuchAlgorithmException e3) {
throw new WSSException (e3);
}
if(certList != null ){
certList.add(cert);
}
// Generate the callback handler
X509ConsumeCallbackHandler callbackHandler = new X509ConsumeCallbackHandler(
"dsig-receiver.ks",
"jks",
"server".toCharArray(),
certList,
java.security.Security.getProvider("IBMCertPath")
);
// Generate the WSSVerification instance (step: d)
WSSVerification ver = factory.newWSSVerification(X509Token.class, callbackHandler);
// Set the part to be verified (step: e)
// DEFAULT: WSSVerification.BODY, WSSSignature.ADDRESSING_HEADERS,
// and WSSSignature.TIMESTAMP.
// Set the part in the SOAP header to be specified by QName (step: e)
ver.addRequiredVerifyHeader(new QName("http://www.w3.org/2005/08/addressing", "MessageID"));
// Set the part to be specified by the keyword (step: e)
ver.addRequiredVerifyPart(WSSVerification.BODY);
// Set the part to be specified by WSSVerifyPart (step: e)
WSSVerifyPart verPart = factory.newWSSVerifyPart();
verPart.setRequiredVerifyPart(WSSVerification.BODY);
verPart.addAllowedDigestMethod(WSSVerifyPart.SHA256);
ver.addRequiredVerifyPart(verPart);
// Set the part specified by XPath expression (step: e)
StringBuffer sb = new StringBuffer();
sb.append("/*[namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/'
and local-name()='Envelope']");
sb.append("/*[namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/'
and local-name()='Body']");
sb.append("/*[namespace-uri()='http://xmlsoap.org/Ping'
and local-name()='Ping']");
sb.append("/*[namespace-uri()='http://xmlsoap.org/Ping'
and local-name()='Text']");
ver.addRequiredVerifyPartByXPath(sb.toString());
// Set one or more canonicalization method candidates for verification (step: f)
// DEFAULT : WSSVerification.EXC_C14N
ver.addAllowedCanonicalizationMethod(WSSVerification.C14N);
ver.addAllowedCanonicalizationMethod(WSSVerification.EXC_C14N);
// Set one or more signature method candidates for verification (step: g)
// DEFAULT : WSSVerification.RSA_SHA1
ver.addAllowedSignatureMethod(WSSVerification.HMAC_SHA1);
// Set the candidate security token to used for the verification (step: h)
X509ConsumeCallbackHandler callbackHandler2 = getCallbackHandler2();
ver.addToken(X509Token.class, callbackHandler2);
// Set the flag to require the signature confirmation (step: i)
ver.requireSignatureConfirmation();
// Add the WSSVerification to the WSSConsumingContext (step: j)
concont.add(ver);
//Validate the WS-Security header (step: k)
concont.process(msgcontext);
下一步做什么
验证签名并设置了 SOAP 消息的算法方法后,您可以设置摘要方法或变换方法。如果要设置这些方法,请使用 WSSVerifyPart API,或者通过管理控制台来配置策略集。