使用 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 算法。

下表显示了定义数字签名安全性约束(完整性)时的必需和可选绑定信息。

表 1. 签名验证部件. 使用签名验证部分来保护消息。
验证部件 描述
关键字
通过使用关键字将必需签名部件添加为验证的目标。可以在生成器端上请求的消息保护中指定不同的消息部件。对必需签名验证部件使用下列关键字:
  • ADDRESSING_HEADERS
  • BODY
  • TIMESTAMP
WS-Addressing 头未进行加密,但可以签署。
xpath 通过使用 XPath 表达式添加验证部件。
part 将 WSSVerifyPart 对象添加为验证部件。
header 将由 QName 指定的头添加为验证部件。

为了获取签名验证信息,将执行特定的缺省行为。使用 WSSVerification API 的最简单方法是使用缺省行为。

WSS API 为摘要方法、变换方法、安全性令牌和必需的验证部件定义了缺省值。

表 2. 签名验证缺省行为. 缺省情况下,已配置签名验证部分的若干特征。
签名验证决策 缺省行为
要使用的签名方法(算法) 设置签名算法方法。数据加密、签名和规范化均可以指定。缺省签名方法是 RSA SHA1。WebSphere Application Server 支持以下预配置的签名方法:
  • WSSVerification.RSA_SHA1:http://www.w3.org/2000/09/xmldsig#rsa-sha1
  • WSSVerification.HMAC_SHA1:http://www.w3.org/2000/09/xmldsig#hmac-sha1
不支持 DSA-SHA1 数字签名方法 (http://www.w3.org/2000/09/xmldsig#dsa-sha1)。
要使用的规范方法(算法) 设置规范算法方法。数据加密、签名和规范化均可以指定。缺省签名方法是 EXC_C14N。WebSphere Application Server 支持以下预配置的规范方法:
  • WSSVerification.EXC_C14N:http://www.w3.org/2001/10/xml-exc-c14n#
  • WSSVerification.C14N:http://www.w3.org/2001/10/xml-c14n#
签名确认是否是必需的 如果 WSSSignature API 指定签名确认是必需的,那么 WSSVerification API 将验证响应消息中的签名确认值(在接收到响应消息时,响应消息会连接签名确认值)。签名确认在 OASIS Web Services Security V1.1 规范中定义。

缺省签名确认是 false。

要指定的安全性令牌 (securityToken)

将 securityToken 对象添加为签名部件。WebSphere Application Server 设置令牌信息以用于验证。

WebSphere Application Server 支持以下预配置的令牌进行签署:

  • X.509 令牌
  • 派生密钥令牌
令牌所必需的信息包括令牌的类、回调处理程序信息以及 JAAS 登录模块的名称。

过程

  1. 要通过使用 WSSVerification API 来验证 SOAP 消息中的签名,请首先确保安装了应用程序服务器。
  2. 使用 WSSVerification API 设置要验证的消息部件以及在 SOAP 消息中指定算法。 用于签名验证的 WSS API 过程遵循以下过程步骤:
    1. 使用 WSSFactory.getInstance() 获取 WSS API 实现实例。
    2. 根据 WSSFactory 实例创建 WSSConsumingContext 实例。
    3. 确保在 JAX-WS 提供程序实现类中调用了 WSSConsumingContext。 由于 JAX-WS 编程模型的特征,需要实现 JAX-WS 提供程序并且该提供程序必须调用 WSSConsumingContext 以验证 SOAP 消息签名。
    4. 根据 WSSFactory 实例创建 WSSVerification。
    5. 添加要验证的部件。 如果更改了摘要方法或变换方法,请创建 WSSVerifyPart 并将其设置到 WSSVerification。
    6. 设置规范方法的候选值(如果缺省值不合适)。
    7. 设置签名方法的候选值(如果缺省值不合适)。
    8. 设置候选安全性令牌(如果缺省值不合适)。
    9. 调用 requireSignatureConfirmation()(如果应用了签名确认)。
    10. 将 WSSVerification 添加至 WSSConsumingContext。
    11. 使用 SOAP 消息上下文调用 WSSConsumingContext.process()。

结果

您完成了相应的步骤来验证绑定的使用者部分的签名。如果存在错误情况,那么将提供 WSSException。如果成功,那么将调用 WSSConsumingContext.process() 并将 Web Service 安全性应用于 SOAP 消息。

示例

以下示例提供了样本代码,这段代码使用了在 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,或者通过管理控制台来配置策略集。


指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_confsignaturecon
文件名:twbs_confsignaturecon.html