服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用xml-encryption实现安全soap消息

  自1977年以来,使用最为广泛的加密算法是数据加密标准(data encryption standard,des)。但是事实表明,由于近几年计算能力的极大提高,des可以在一天之内被攻破。所以2001年,联邦政府引入了一个新的标准:高级加密标准(advanced encryption standard,aes)。des和aes使用的都是对称密钥加密算法。顾名思义,其加密和解密是使用同一个密码块进行的。在一个客户-服务器环境中,如何创建、分发用于加密和解密消息的密钥并对其达成协议是首要的问题。

  xml-encryption指定encryptedkey机制,使用rsa――一种公钥加密体制――来加密密钥。我们需要记住,在非对称加密中,我们使用公钥来加密,而使用私钥来解密。所以,密钥由消息发送方创建,并使用接收方的公钥进行加密。然后用于加密的密钥会包含在消息中。接收方根据keyinfo元素得出解密密钥(私钥)。

  让我们来详细看一下由weblogic server 9.0所生成的示例消息:

<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:header>
    <wsse:security
      xmlns:wsse="…/oasis-200401-wss-wssecurity-secext-1.0.xsd"
      soapenv:mustunderstand="1">
       <ns1:encryptedkey id="encrypt"
           xmlns:ns1="http://www.w3.org/2001/04/xmlenc#">
       <ns1:encryptionmethod algorithm="…#rsa-1_5"/>
       <ns2:keyinfo xmlns:ns2="…/xmldsig#">
         <wsse:securitytokenreference wsu:id="dk8xm"
             xmlns:wsu="…/oasis-200401-wss-wssecurity-utility-1.0.xsd"
             xmlns:wsse="…/oasis-200401-wss-wssecurity-secext1.0.xsd">
             <ns2:x509data>
                <ns2:x509issuerserial>
                   <ns2:x509issuername>cn=certgencab…c=us</ns2:x509issuername>
                   <ns2:x509serialnumber>-</ns2:x509serialnumber>
                </ns2:x509issuerserial>
             </ns2:x509data>
          </wsse:securitytokenreference>
       </ns2:keyinfo>
       <ns1:cipherdata>
            <ns1:ciphervalue>asmjpehitl/2doflgwdq==</ns1:ciphervalue>
       </ns1:cipherdata>
       <ns1:referencelist>
           <ns1:datareference uri="#dk8zw31"/>
       </ns1:referencelist>
     </ns1:encryptedkey>
    </wsse:security>
   </soapenv:header>
   <soapenv:body>
      <ns1:encrypteddata id=" dk8zw31" type="…#content"
         mimetype="text/xml" encoding="utf-8" xmlns:ns1="…/xmlenc#">
         <ns1:encryptionmethod algorithm="…#tripledescbc"/>
         <ns1:cipherdata>
            <ns1:ciphervalue>2hikjvudsl9qpqhp</ns1:ciphervalue>
         </ns1:cipherdata>
       </ns1:encrypteddata>
   </soapenv:body>
</soapenv:envelope>

  securitytokenreference元素指定接收方的x509证书的x509issuerserial。接收方应该能够从密钥库查找相应的私钥来执行解密操作。除了x509issuerserial,x509证书的subject key identifier (skid)也可以用来描述用于加密的密钥。

<wsse:securitytokenreference>
  <wsse:keyidentifier
    valuetype="...#x509subjectkeyidentifier">
      xeg55vryk3zhaehef+yt0z986l0=
  </wsse:keyidentifier>
</wsse:securitytokenreference>

  如果x509证书中包含skid,weblogic server 9.0就总会在securitytokenreference中生成keyidentifier。而如果x509证书中没有skid,那么就会在securitytokenreference中使用x509issuerserial。

  只要响应消息需要加密,客户端就会将它的公钥作为请求消息的一部分发送,以便服务器使用其中的密钥来加密响应消息。

<soapenv:envelope
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:header>
     <wsse:security
       xmlns:wsse=".../oasis-200401 wss-wssecurity-secext-1.0.xsd"
       soapenv:mustunderstand="1">
       <wsse:binarysecuritytoken wsu:id="encrypt-token"
          xmlns:wsu="…/oasis-200401-wss-wssecurity-utility-1.0.xsd"
          valuetype="…#x509v3"
          encodingtype="…#base64binary">bzewx…
       </wsse:binarysecuritytoken>
     </wsse:security>
   </soapenv:header>
   <soapenv:body>
       <m:encryptresponse xmlns:m="http://dev2dev.bea.com">
          <m:s>only response message isencrypted!</m:s>
       </m:encryptresponse>
   </soapenv:body>
</soapenv:envelope>

扫描关注微信公众号