使用XML-Encryption实现安全SOAP消息

来源:互联网 发布:机房电脑控制软件 编辑:程序博客网 时间:2024/06/05 18:36

自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>

 

 

 

signatures.xml文件是出版物的电子签名
<signatures>

  <Signature Id="MyFirstSignature" xmlns="http://www.w3.org/2000/09/xmldsig#">

    <SignedInfo>

      <CanonicalizationMethod
       Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>

      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#dsa-sha1"/>

      <Reference URI="#Manifest1">

        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>

        <DigestValue>j6lwx3rvEPO0vKtMup4NbeVu8nk=</DigestValue>

      </Reference>

    </SignedInfo>

    <SignatureValue>MC0CFFrVLtRlk=...</SignatureValue>

    <KeyInfo>

      <KeyValue>

        <DSAKeyValue>

          <P>...</P><Q>...</Q><G>...</G><Y>...</Y>

        </DSAKeyValue>

      </KeyValue>

    </KeyInfo>

    <Object>

      <Manifest Id="Manifest1">

        <Reference URI="OEBFPS/book.xml">

          <Transforms>

            <Transform
             Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>

          </Transforms>

        </Reference>

        <Reference URI="OEBFPS/images/cover.jpeg"/>

      </Manifest>

    </Object>

  </Signature>

</signatures>

encryption.xml文件中是出版物的加密信息

<encryption

 xmlns ="urn:oasis:names:tc:opendocument:xmlns:container"
 xmlns:enc="http://www.w3.org/2001/04/xmlenc#"
 xmlns:ds="http://www.w3.org/2000/09/xmldsig#">

  <enc:EncryptedKey Id="EK">

    <enc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>

    <ds:KeyInfo>

      <ds:KeyName>John Smith</ds:KeyName>

    </ds:KeyInfo>

    <enc:CipherData>

      <enc:CipherValue>xyzabc</enc:CipherValue>

    </enc:CipherData>

  </enc:EncryptedKey>

  <enc:EncryptedData Id="ED1">

    <enc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#kw-aes128"/>

    <ds:KeyInfo>

      <ds:RetrievalMethod URI="#EK"

       Type="http://www.w3.org/2001/04/xmlenc#EncryptedKey"/>

    </ds:KeyInfo>

    <enc:CipherData>

      <enc:CipherReference URI="image.jpeg"/>

    </enc:CipherData>

  </enc:EncryptedData>

</encryption>

    有关XML的加密参见 “XML Encryption Syntax and Processing” (http://www.w3.org/TR/2002/REC-xmlenc-core-20021210).我们介绍一下 <EncryptedKey> 和 <EncryptedData>两个元素:

每一个EncryptedKey元素说明了一个或者几个文件是怎样加密的(密钥)。如果包中的任何资源是加密的,则包内必须有encryption.xml文件。

    EncryptedData元素是一加密的XML文件名。它和EncryptedKey对应。OCF中每个文件是分别加密的,阅读时再分别解密。这样会暴露压缩包(容器)内的结构和文件名。

OCF提供了XML文件加密的框架,允许使用各种加密算法。XML加密仅对XML文档,尽管这样OCF也允许对非XML数据加密。XML加密是对所有的文件的,而且只能是整个文件的加密。但encryption.xml文件不能是加密的。

加密的文件取代了包内原来非加密的文件。例如photo.ipg被告加密了,那么加密的photo.jpg将取代包内原来没有加密的photo.jpg

    当存储在一个压缩包中时,数据是在加密前就已经压缩了的,而不是用Flate compression。

 下面这些文件是从来不被加密的:

·         mimetype

·         META-INF/container.xml

·         META-INF/manifest.xml

·         META-INF/metadata.xml

·         META-INF/signatures.xml

·         META-INF/encryption.xml

·         META-INF/rights.xml

·         OEBPS rootfile (the OEBPS Package file)

rights.xml文件是出版物的版权信息,是为数字版权管理(DRM)用的,目录还没有标准格式。

原创粉丝点击