享受代码,享受人生

SOA is an integration solution. SOA is message oriented first.
The Key character of SOA is loosely coupled. SOA is enriched
by creating composite apps.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WS-Security Core

Posted on 2006-04-20 19:06  idior  阅读(4916)  评论(1编辑  收藏  举报

在分别介绍了XML Signature和XML Encryption后,我们了解了如何用XML的形式来保证消息的完整性(Integrity)和机密性(Confidentiality)。如何将其运用到Web Service中从而保证Web Service的安全性,这就是WS-Security规范所描述的内容。我们知道Web Service的采用SOAP作为消息封装协议, 因此WS-Security规范主要描述了如何将XML Security(XML Signature和XML Encryption)与已有的安全技术(Kerberos,X.509,SAML)结合, 并把它们绑定到SOAP中. 

  
NoteA good way to think about WS-Security is that it is a specification that takes XML security (XML Encryption and XML Signature); links that with pre-existing security technologies that it calls tokens, such as X.509, Kerberos, and SAML; and binds it all to SOAP so that it can become part of a secure Web service interaction.

                    

             
NoteSOAP描述了Web Service消息的打包形式,而非消息的传输协议,Web Service的传输协议可以是http,tcp甚至smtp。

                   
<S:Envelope>
    <S:Header>
       <wsse:Security>
         <!-- Security Token -->
         <wsse:UsernameToken>
                 ...
         </wsse:UsernameToken>

         <!-- XML Signature -->
         <ds:Signature>
                 ...
           <ds:Reference URI="#body">
                 ...
         </ds:Signature>

         <!-- XML Encryption Reference List -->
         <xenc:ReferenceList>
             <xenc:DataReference URI="#body"/>
         </xenc:ReferenceList>
       </wsse:Security>
    </S:Header>
    <S:Body>
       <!-- XML Encrypted Body -->
       <xenc:EncryptedData Id="body" Type="content">
                 ...
       </xenc:EncryptedData>
    </S:Body>
</S:Envelope>
              
以上是一个典型的使用WS-Security协议来保证SOAP消息安全的例子。从中可以看出WS-Security协议主要对SOAP消息的Head部分做了扩展---加入了wsse:Security元素。其中针对安全的三个方面Authentication, Integrity, Confidentiality分别定义了Security Token, XML Signature以及XML Encryption Reference List三个子元素。而在SOAP包中的需要加密的业务内容(通常会加密整个Soap Body)被经过XML Encryption处理过的元素(EncryptedDate)所替代。
 
下面这张图大致描述了WS-Security所包含的元素以及它们之间的关系。 

 


              

(注意这幅图只是一张示意图,比如ds:Signature也可以对Soap Head中的元素做签名)

从之前对XML Signature和XML Encryption介绍中,我们已经了解了签名和加密的具体过程,以及在整个过程中产生的各个元素的含义。下面就从一个具体的运用了WS-Security规范的Soap Envelop中来看看如何在WS-Security中使用XML Signature和XML Encryption。

<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope"
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
    xmlns:xenc="http://www.w3.org/2001/04/xmlenc"
    xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
    <SOAP-ENV:Header>
        <wsse:Security
            xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/secext">
            <wsse:UsernameToken wsu:Id="UserToken">
                   <wsse:Username>HotelService</wsse:Username>
                   <wsse11:Salt>sdfer..</wsse11:Salt>     
                   <
wsse11:Iteration>1000</wsse11:Iteration>                         
               </wsse:UsernameToken>
            <ds:Signature>
                <ds:SignedInfo>
                    <ds:CanonicalizationMethod
                        Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
                    <ds:SignatureMethod
                        Algorithm="http://www.w3.org/2000/09/xmldsig#hmac-sha1"/>
                    <ds:Reference URI="#DiscountedBookingForPartnersResponse">
                        <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                        <ds:DigestValue>JwFsd3eQc0iXlJm5PkLh7...</ds:DigestValue>
                    </ds:Reference>
                </ds:SignedInfo>
                <ds:SignatureValue>BSxlJbSiFdm5Plhk...</ds:SignatureValue>
                <ds:KeyInfo>
                       <wsse:SecurityTokenReference>
                            <wsse:Reference URI="#UserToken"/>
                       </wsse:SecurityTokenReference>
                   </ds:KeyInfo>
            </ds:Signature>
            <xenc:ReferenceList>
                <xenc:DataReference URI="#DiscountResponse"/>
            </xenc:ReferenceList>
         </wsse:Security>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body wsu:Id="DiscountedBookingForPartnersResponse">
        <s:GetSpecialDiscountedBookingForPartnersResponse
            xmlns:s=?http://www.MyHotel.com/partnerservice?>
            <xenc:EncryptedData
                wsu:Id="DiscountResponse"
                type="http://www.w3.org/2001/04/xmlenc#Element">
                <xenc:EncryptionMethod
                    Algorithm="http://www.w3.org/2001/04/xmlenc#aes256_cbc "/>
                <ds:KeyInfo xmlns:ds='http://www.w3.org/2000/09/xmldsig#'>
                         <wsse:SecurityTokenReference>
                              <wsse:Reference URI="#UserToken"/>
                         </wsse:SecurityTokenReference>
                     </ds:KeyInfo>
                <CipherData>
                <CipherValue>XDsFaDWsHdhrHdhcW0x...</CipherValue>
                </CipherData>
            </xenc:EncryptedData>
        </s:GetSpecialDiscountedBookingForPartnersResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

          
       
Noteds:Signature和xenc:EncryptedData在wsse:Security 中出现的顺序决定了加密和签名的顺序。ds:Signature先出现表示先加密后签名,反之先签名后加密。通常采用先加密后签名的做法。


对于其中的大部分元素我们已经比较熟悉,而wsse:UsernameToken以及在ds:Signature和xenc:EncryptedData 中出现的wsse:SecurityTokenReference子元素则显得比较陌生。
 
我们知道为了保证消息的完整性和机密性需要用到密钥(对称或非对称),而在它们各自的ds:KeyInfo子元素中都使用wsse:SecurityTokenReference引用到了在Soap Head最开始定义的Security Token(这里是UsernameToken)。由此可以看出Security Token是保证消息的完整性和机密性的基础,同时密钥也是用户证明身份的标志,因此包含密钥信息的Security Token也是实现身份鉴别的基础。在已经介绍过XML Signature和XML Encryption的基础上,下面对WS-Security的介绍将主要集中在Security Token上。
  

系列文章