<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>XML Encryption Syntax and Processing</title>
  <style type="text/css">

    u,ins        { background: white; color: red;}
    del,strike,.strike   { background: white; color: silver; text-decoration: line-through;}
    code     {font-weight: normal; }
    .link-def   { background: #FFFFFF; color: teal;  font-style: italic;}
    .comment    { background: #FFFFF5; color: black; padding: .7em; border: navy thin solid;}
    .discuss    { color: blue; background: yellow; }
    .xml-example,.xml-dtd { margin-left: -1em; padding: .5em; white-space: pre; border: none;}
    .xml-dtd    { background: #efeff8; color: black;}
    </style>
  <link rel="stylesheet" type="text/css"
  href="http://www.w3.org/StyleSheets/TR/W3C-WD.css" />
</head>

<body xml:lang="en" lang="en">
<h2><a name="sec-Processing" id="sec-Processing">4 Processing Rules</a></h2>

<p>This section describes the operations to be performed as part of
encryption and decryption processing.</p>

<h3><a name="sec-Processing-Encryption" id="sec-Processing-Encryption">4.1
Encryption</a></h3>

<p>For each data item to be encrypted:</p>
<ol>
  <li>Select the algorithm (and parameters) to be used in encrypting this
    data.</li>
  <li>Obtain and (optionally) represent the key. 
    <ol>
      <li>If the key is to be identified (via naming, URI, or included in a
        child element), construct the <code>ds:KeyInfo</code> as approriate
        (e.g., <code>ds:KeyName</code>, <code>ds:KeyValue</code>,
        <code>ds:KeyRetrievalMethod</code>, etc.)</li>
      <li>If the key itself is to be encrypted, construct an
        <code>EncryptedKey</code> element. This may be then be a child of
        <code>ds:KeyInfo</code>, or it may exist elsewhere and may be
        identified in the previous step.</li>
    </ol>
  </li>
  <li> Encrypt the data 
    <ol>
      <li>If the data is an [<a
        href="data/2web/WWW/Encryption/2001/Drafts/xmlenc-core/Overview.html#ref-XML">XML</a>]
        <a
        href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-element">Element</a>
        or [<a
        href="data/2web/WWW/Encryption/2001/Drafts/xmlenc-core/Overview.html#ref-XML">XML</a>]
        <a
        href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-content">Element
        Content</a>, obtain the octets resulting from their serialization in
        UTF-8 as specified in [<a
        href="data/2web/WWW/Encryption/2001/Drafts/xmlenc-core/Overview.html#ref-XML">XML</a>].</li>
      <li>If the data is of any other type, its is treated solely as an octet
        sequence. For example, the data might be a serialization of an XML
        document, textual document, binary image data, or a compressed XML
        element. Unless the Decryptor will implicitly know the type (or
        encoding) of the encrypted data, the encryptor SHOULD specify the
        type value with the <code>EncryptedData</code> <code>Type</code>
        attribute.</li>
      <li>Encrypt the octets using the algorithm and key from steps 1 and
      2.</li>
    </ol>
  </li>
  <li>Build the <code>EncryptedData</code> structure. An
    <code>EncryptedData</code> element represents all of the information
    previously discussed including the encryption algorithm, parameters, key,
    type of the encrypted data, etc. 
    <ol>
      <li>If the encrypted octet sequence obtained in Step 3 is to be stored
        in the <code>CipherData</code> element within the
        <code>EncryptedData</code>, then the encrypted octet sequence is
        base64 encoded and inserted as the text value of a
        <code>CipherValue</code> element.</li>
      <li>If the encrypted octet sequence is to be stored externally to the
        <code>EncryptedData</code> structure, then represent the URI, and
        optional transforms, required for the Decryptor to retrieve the
        encrypted octet sequence within a <code>CipherReference</code>
        element.</li>
    </ol>
  </li>
  <li>EncryptedData Processing 
    <ol>
      <li>If the <code>Type</code> of the encrypted data is <a
        href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-element">Element</a>
        or <a
        href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-content">Element
        Content</a>, implementations MUST be able to return the UTF-8
        encoding of the <code>EncryptedData</code> element to the
        application. The application may use this as the top-level element in
        a new XML Document or insert it into another XML document. 
        <p>Encryption applications MUST be able to replace the unencrypted
        Element or Content with the <code>EncryptedData</code> element.. The
        application supplies the XML Document context and identifies the
        Element or Content being replaced. The Encryptor must remove the
        identified XML and insert the <code>EncryptedData</code> element in
        its place.</p>
      </li>
      <li>If the Type of the encrypted data is not <a
        href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-element">Element</a>
        or <a
        href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-content">Element
        Content</a>, then the UTF-8 encoded <code>EncryptedData</code>
        element is always returned to the application. The application may
        use this as the top-level element in a new XML Document or insert it
        into another XML document (which may require a re-encoding).</li>
    </ol>
  </li>
</ol>

<h3><a name="sec-Processing-Decryption" id="sec-Processing-Decryption">4.2
Decryption</a></h3>

<p>For each <code>EncryptedType</code> item to be decrypted (either an
<code>EncryptedData</code> or <code>EncryptedKey</code> element):</p>
<ol>
  <li>Parse the application identified <code>EncryptedType</code> element to
    determine the algorithm, parameters and <code>ds:KeyInfo</code> element
    to be used. If some information is omitted, the application must supply
    it.</li>
  <li>Locate the data encryption key according to the <code>ds:KeyInfo</code>
    element, which may contain one or more key attributes.  These attributes
    have no implied processing order. If the data encryption key is
    encrypted, locate the corresponding key to decrypt it. (This may be a
    recursive step as the key-encryption key may itself be encrypted.) Or,
    one might retrieve the data encryption key from a local store using the
    provided attributes or implicit binding.</li>
  <li>Decrypt the data contained in the <code>CipherData</code> element. 
    <ol>
      <li>If a <code>CipherValue</code> child element is present, then the
        associated text value is retrieved and base64 decoded so as to obtain
        the encrypted octet sequence.</li>
      <li>If a <code>CipherReference</code> child element is present, the URI
        and optional transforms are used to retrieve the encrypted octet
        sequence. </li>
      <li>The encrypted octed sequence obtained is decrypted using the
        algorithm/parameters and key value already determined from Steps 1
        and 2.</li>
      <li>If the cleartext octet sequence represents a key value, it is saved
        for use in decrypting the associated <code>EncryptedData</code>
        element(s). The Decryptor should support passing this key value to
        the application for persistent storage. If it represents encrypted
        data then processing as described below is required.</li>
    </ol>
  </li>
  <li>Decrypt data of Type <a
    href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-element">Element</a>
    or <a
    href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-content">Element
    Content</a>.
    <ol>
      <li>The cleartext octet sequence obtained in step 3 is interpreted as
        UTF-8 encoded character data.</li>
      <li>The Decryptor MUST be able to return the type value and the UTF-8
        encode XML character data. </li>
      <li>The Decryptor MUST support the ability to replace the
        <code>EncryptedData</code> element with the decrypted <a
        href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-element">Element</a>
        or <a
        href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-content">Element
        Content</a> represented by the UTF-8 encoded characters. The
        Decryptor is not required to perform validation on the serialized XML
        nor are they required to ensure the result of this replacement
        operation results in a valid XML document.</li>
    </ol>
  </li>
  <li>Decrypt data when the <code>Type</code> is unspecified or is not <a
    href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-element">Element</a>
    or <a
    href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-content">Element
    Content</a> 
    <ol>
      <li>The cleartext octet sequence obtained in step 3 is always returned
        to the application for further processing along with the Type
        attribute value when specified.</li>
    </ol>
  </li>
</ol>

<h3>4.3 <a name="sec-Processing-XML" id="sec-Processing-XML">XML
Encryption</a></h3>

<p>Encryption and decryption operations are transforms on octets. As noted in
the preceding discussion, the encrypting application is responsible for the
serialization of XML into an octet sequence, and that octets' semantics, such
that the decrypted data is useful to the decrypting application.</p>

<p>For example, if the applications wishes to canonicalize its data or
encode/compress the data in an XML packaging format, the application needs to
marshal the XML accordingly and identify the resulting type, which will
encoded in the optional <code>EncryptedData</code> <code>Type</code>
attribute. The likelihood of successful decryption and subsequent processing
will be dependent on the recipient's support for the given type. Also, if the
data is intended to be processed both before and after decryption (e.g., XML
Signature [<a
href="http://lists.w3.org/Archives/Public/xml-encryption/2001Aug/att-0008/01-xenc.html#ref-XMLDSIG">XMLDSIG</a>]
validation or an XSLT transform) the encrypting application must be careful
to preserve information necessary for that process's success.</p>

<p>For interoperability purposes, the following types MUST be implemented.</p>
<dl>
  <dt><a id="Element" name="Element">Element</a> '<a
  href="http://www.w3.org/2001/04/xmlenc#Element">http://www.w3.org/2001/04/xmlenc#Element</a>'</dt>
    <dd>"<a name="NT-element" id="NT-element"></a>[39]  <a
      href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-element">element</a>
      ::= <a
      href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-EmptyElemTag"><code>EmptyElemTag</code></a>
      | <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-STag">STag</a>
      <a
      href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-content">content</a>
      <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-ETag">ETag</a>"
      [<a href="#ref-XML">XML</a>]</dd>
  <dt><a id="Content" name="Content">Content</a> '<a
  href="http://www.w3.org/2001/04/xmlenc#Content">http://www.w3.org/2001/04/xmlenc#Content</a>'</dt>
    <dd>"[43] <a
      href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-content">content</a>
      ::= <a
      href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-CharData"><code>CharData</code></a>?
      ((<a
      href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-element">element</a>
      | <a
      href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Reference">Reference</a>
      | <a
      href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-CDSect">CDSect</a>
      | <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-PI">PI</a> |
      <a
      href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Comment">Comment</a>)
      <a
      href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-CharData"><code>CharData</code></a>?)*"
      [<a href="#ref-XML">XML</a>]</dd>
  <dt>MediaType '<a
  href="http://www.isi.edu/in-notes/iana/assignments/media-types/">http://www.isi.edu/in-notes/iana/assignments/media-types/*/*</a>'</dt>
    <dd>A user specified media type (e.g., <a
      href="http://www.isi.edu/in-notes/iana/assignments/media-types/text/xml">text/xml</a>).
      All such types are implemented as simple octet encryption.</dd>
</dl>
</body>
</html>
