跳轉到內容

XML - 資料交換管理/XML 加密

來自華夏公益教科書,開放的書籍,開放的世界



上一章 下一章
將 MySQL 轉換為 XML XQL



作者: Shayla S. Lee 2005 年 11 月 15 日 02:38 (UTC)

XML 加密是為了解決傳輸層安全協議和安全套接字層協議 (TLS/SSL) 未解決的兩個常見問題而開發的。TLS/SSL 是一種非常安全可靠的協議,它在兩個參與者之間提供端到端安全會話。XML 透過加密部分或全部交換資料以及允許在多於兩個參與者之間進行安全會話,為 TLS/SSL 添加了一層額外的安全性。換句話說,每個參與者都可以與任何通訊參與者保持安全或不安全的會話,並且可以在同一文件中交換安全和非安全資料。此外,XML 加密可以處理 XML 和非 XML(例如二進位制)資料。

加密語法

[編輯 | 編輯原始碼]

所有 XML 加密檔案都必須以以下 XML 前言、宣告、內部實體和匯入開頭。

Schema Definition:
 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE schema  PUBLIC "-//W3C//DTD XMLSchema 200102//EN"
  "http://www.w3.org/2001/XMLSchema.dtd"
  [
    <!ATTLIST schema
      xmlns:xenc CDATA #FIXED 'http://www.w3.org/2001/04/xmlenc#'
      xmlns:ds CDATA #FIXED 'http://www.w3.org/2000/09/xmldsig#'>
    <!ENTITY xenc 'http://www.w3.org/2001/04/xmlenc#'>
    <!ENTITY % p ''>
    <!ENTITY % s ''>
   ]>
 
 <schema xmlns='http://www.w3.org/2001/XMLSchema' version='1.0'
         xmlns:ds='http://www.w3.org/2000/09/xmldsig#'
         xmlns:xenc='http://www.w3.org/2001/04/xmlenc#'
         targetNamespace='http://www.w3.org/2001/04/xmlenc#'
         elementFormDefault='qualified'>
   <import namespace='http://www.w3.org/2000/09/xmldsig#'
           schemaLocation='http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-   schema.xsd'/>


EncryptedType 元素

[編輯 | 編輯原始碼]

EncryptedType 是 EncryptedData 和 EncryptedKey 衍生的抽象型別。

 Schema Definition:
  <complexType name='EncryptedType' abstract='true'>
   <sequence>
     <element name='EncryptionMethod' type='xenc:EncryptionMethodType' 
              minOccurs='0'/>
     <element ref='ds:KeyInfo' minOccurs='0'/>
     <element ref='xenc:CipherData'/>
     <element ref='xenc:EncryptionProperties' minOccurs='0'/>
   </sequence>
   <attribute name='Id' type='ID' use='optional'/>
   <attribute name='Type' type='anyURI' use='optional'/>
   <attribute name='MimeType' type='string' use='optional'/>
   <attribute name='Encoding' type='anyURI' use='optional'/> 
  </complexType>

語法解釋

EncryptionMethod 是一個可選元素,它描述應用於密文資料的加密演算法。如果該元素不存在,則接收方必須知道加密演算法,否則解密將失敗。

<element name='EncryptionMethod' type='xenc:EncryptionMethodType' 
              minOccurs='0'/>

ds:KeyInfo 是一個可選元素,它包含有關用於加密資料的金鑰的資訊。本規範的後續部分定義了可能作為 ds:KeyInfo 子元素出現的新的元素。

<element ref='ds:KeyInfo' minOccurs='0'/>


CipherData 是一個必須元素,它包含包含加密資料的 CipherValue 或 CipherReference。

<element ref='xenc:CipherData'/>

EncryptionProperties 可以包含有關生成 EncryptedType 的附加資訊(例如,日期/時間戳)。

<element ref='xenc:EncryptionProperties' minOccurs='0'/>

Id 是一個可選屬性,它提供在文件上下文中為元素分配字串 ID 的標準方法。

<attribute name='Id' type='ID' use='optional'/>


Type 是一個可選屬性,它標識有關加密內容明文形式的型別資訊。雖然是可選的,但本規範利用它在解密中進行強制處理。如果 EncryptedData 元素包含型別為“元素”或元素“內容”的資料,並且在 XML 文件上下文中替換了該資料,則強烈建議提供 Type 屬性。沒有此資訊,解密器將無法自動將 XML 文件恢復到其原始明文形式。

<attribute name='Type' type='anyURI' use='optional'/>

MimeType 是一個可選的(建議性的)屬性,它描述已加密資料的媒體型別。此屬性的值是 [MIME] 中定義的字串。例如,如果加密的資料是 base64 編碼的 PNG,則傳輸編碼可以指定為 'http://www.w3.org/2000/09/xmldsig#base64' 並且 MimeType 為“image/png”。此屬性純粹是建議性的;不需要對 MimeType 資訊進行任何驗證,並且它不表示加密應用程式必須執行任何其他處理。請注意,如果此資訊已經繫結到 Type 屬性中的識別符號,則可能不需要此資訊。例如,本規範中定義的 Element 和 Content 型別始終是 UTF-8 編碼的文字。

<attribute name='MimeType' type='string' use='optional'/>

EncryptionMethod 元素

[編輯 | 編輯原始碼]

EncryptionMethod 是一個可選元素,它描述應用於密文資料的加密演算法。如果該元素不存在,則接收方必須知道加密演算法,否則解密將失敗。EncryptionMethod 的允許子元素由 Algorithm 屬性 URI 的特定值確定。

Schema Definition:
 <complexType name='EncryptionMethodType' mixed='true'>
   <sequence>
     <element name='KeySize' minOccurs='0' type='xenc:KeySizeType'/>
     <element name='OAEPparams' minOccurs='0' type='base64Binary'/>
     <any namespace='##other' minOccurs='0' maxOccurs='unbounded'/>
   </sequence>
   <attribute name='Algorithm' type='anyURI' use='required'/>
 </complexType>

CipherData 元素

[編輯 | 編輯原始碼]

CipherData 是一個必須元素,它提供加密資料。它必須包含作為 CipherValue 元素的 base64 編碼文字的加密八位位元組序列,或者透過 CipherReference 元素提供對包含加密八位位元組序列的外部位置的引用。

Schema Definition:
 <element name='CipherData' type='xenc:CipherDataType'/>
 <complexType name='CipherDataType'>
    <choice>
      <element name='CipherValue' type='base64Binary'/>
      <element ref='xenc:CipherReference'/>
    </choice>
  </complexType>

CipherReference 元素

[編輯 | 編輯原始碼]

CipherReference 標識一個源,當處理該源時,會產生加密的八位位元組序列。當未直接提供 CipherValue 時,使用 CipherReference。實際值是按如下方式獲得的。CipherReference URI 包含一個識別符號,該識別符號會被取消引用。如果 CipherReference 元素包含可選的 Transforms 序列,則對從取消引用 URI 生成的結果資料進行指定轉換,以便產生預期的密文值。例如,如果該值在 XML 文件中以 base64 編碼的形式存在;轉換可以指定一個 XPath 表示式,然後是 base64 解碼,以便提取八位位元組。

Schema Definition:
 <element name='CipherReference' type='xenc:CipherReferenceType'/>
  <complexType name='CipherReferenceType'>
      <sequence>
        <element name='Transforms' type='xenc:TransformsType' minOccurs='0'/>
      </sequence>
      <attribute name='URI' type='anyURI' use='required'/>
  </complexType>
   <complexType name='TransformsType'>
      <sequence>
        <element ref='ds:Transform' maxOccurs='unbounded'/> 
      </sequence>
    </complexType>

帶可選轉換功能的密文引用和轉換演算法

<CipherReference URI="http://www.example.com/CipherValues.xml">
   <Transforms>
     <ds:Transform 
      Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
         <ds:XPath xmlns:rep="http://www.example.org/repository">
           self::text()[parent::rep:CipherValue[@Id="example1"]]
         </ds:XPath>
     </ds:Transform>
     <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#base64"/>
   </Transforms>
 </CipherReference>

EncryptedData 元素

[編輯 | 編輯原始碼]

EncryptedData 是語法中的核心元素。它的 CipherData 子元素不僅包含加密資料,而且是替換加密元素或用作新文件根的元素。

Schema Definition:
 <element name='EncryptedData' type='xenc:EncryptedDataType'/>
 <complexType name='EncryptedDataType'>
   <complexContent>
    <extension base='xenc:EncryptedType'>
    </extension>
   </complexContent>
 </complexType>

以上資訊來自 W3C 和 IBM。有關更多資訊,請訪問以下連結

http://www.w3.org/TR/2002/CR-xmlenc-core-20020802/#sec-Encryption-Syntax
http://www-128.ibm.com/developerworks/xml/library/x-encrypt/
華夏公益教科書