[转载]Exploring XML Encryption (第一部分)
文章作者:<a href="#author"><font color="#996699">Bilal Siddiqui</font></a> (<a href="mailto:wap_monster@yahoo.com?subject=Exploring XML Encryption, Part 1"><font color="#5c81a7">wap_monster@yahoo.com</font></a>), CEO, WAP Monster<br /><br /><p>01 Mar 2002</p><blockquote>XML Encryption provides end-to-end security for applications that require secure exchange of structured data. XML itself is the most popular technology for structuring data, and therefore XML-based encryption is the natural way to handle complex requirements for security in data interchange applications. Here in part 1 of this two-part series, Bilal explains how XML and security are proposed to be integrated into the W3C's Working Draft for XML Encryption.</blockquote><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><script language="java script" type="text/java script"></script><!--END RESERVED FOR FUTURE USE INCLUDE FILES--><p>Currently, Transport Layer Security (TLS) is the de facto standard for secure communication over the Internet. TLS is an end-to-end security protocol that follows the famous Secure Socket Layer (SSL). SSL was originally designed by Netscape, and its version 3.0 was later adapted by the Internet Engineering Task Force (IETF) while they were designing TLS. This is a very secure and reliable protocol that provides end-to-end security sessions between two parties. XML Encryption is not intended to replace or supersede SSL/TLS. Rather, it provides a mechanism for security requirements that are not covered by SSL. The following are a two important areas not addressed by SSL:</p><ul><li>Encrypting part of the data being exchanged </li><li>Secure sessions between more than two parties </li></ul><p>With XML Encryption, each party can maintain secure or insecure states with any of the communicating parties. Both secure and non-secure data can be exchanged in the same document. For example, think of a secure chat application containing a number of chat rooms with several people in each room. XML-encrypted files can be exchanged between chatting partners so that data intended for one room will not be visible to other rooms.</p><p>XML Encryption can handle both XML and non-XML (e.g. binary) data. We'll now demonstrate a simple exchange of data, making it secure through XML Encryption. We'll then slowly increase the complexity of the security requirements and explain the XML Encryption schema and the use of its different elements.</p><p><a name="simple"><span class="atitle"><font face="Arial" size="4">A simple example of secure exchange of XML data </font></span></a></p><p>Suppose you want to send the XML file in <a href="#code1"><font color="#996699">Listing 1</font></a> to a publishing company. This file contains details of a book that you want to purchase. In addition, it also contains your credit card information for payment. Naturally, you would like to use secure communication for this sensitive data. One option is to use SSL, which secures the whole communication. The alternative is to use XML Encryption. As already mentioned, XML Encryption is not an alternative to SSL/TLS. If the application requires that the whole communication be secure, you'll use SSL. On the other hand, XML Encryption is the best choice if the application requires a combination of secure and insecure communication (which means that some of the data will be securely exchanged and the rest will be exchanged as is).</p><br /><br /><a name="code1"><b>Listing 1. The sample XML file to be encrypted</b></a><br /><table cellspacing="0" cellpadding="5" width="100%" bgcolor="#eeeeee" border="1"><tbody><tr><td><code><pre class="section"><purchaseorder /><order />
<item />book</item />
<id />123-958-74598</id />
<quantity />12</quantity />
</order />
<payment />
<cardid />123654-8988889-9996874</cardid />
<cardname />visa</cardname />
<validdate />12-10-2004</validdate />
</payment />
</purchaseorder />
</pre></code></td></tr></tbody></table><br /><blockquote><b>Note:</b> We have intentionally kept the XML file in <a href="#code1"><font color="#996699">Listing 1</font></a> very simple. This helps in keeping our focus on encryption-related issues. Real-world XML files in collaborative commerce or Web services will be similar in structure but more verbose. WSDL (Web Services Definition Language) and SOAP (Simple Object Access Protocol) are XML-based grammars that are frequently used in B2B integration. Both WSDL and SOAP can use XML Encryption to provide secure communication across the enterprise. Visit the W3C for details about them (see <a href="#resources"><font color="#996699">Resources</font></a>).</blockquote><p><a name="N10074"><span class="smalltitle"><strong><font face="Arial" size="3">Encrypting complete documents with XML Encryption</font></strong></span></a></p><p>XML Encryption offers various options. <a href="listing2.html"><font color="#5c81a7">Listing 2</font></a>, <a href="listing3.html"><font color="#5c81a7">Listing 3</font></a>, and <a href="listing4.html"><font color="#5c81a7">Listing 4</font></a> show the different encrypted results. Let's look at them in detail, one by one.</p><p><a href="listing2.html"><font color="#5c81a7">Listing 2</font></a> shows the resulting XML-encrypted file, in case you decide to encrypt the entire XML document in <a href="#code1"><font color="#996699">Listing 1</font></a>. Notice the <code><font face="新宋体"><cipherdata /></font></code>and <code><font face="新宋体"><ciphervalue /></font></code>tags. The actual encrypted data appears as contents of the <code><font face="新宋体"><ciphervalue /></font></code>tag. The complete <code><font face="新宋体">CipherData</font></code> element appears within an <code><font face="新宋体">EncryptedData</font></code> element. The <code><font face="新宋体">EncryptedData</font></code> element contains the XML namespace used for encryption. For example, your original data before encryption was XML and the official type definition by the Internet Assigned Numbers Authority (IANA) for XML is [url]http://www.isi.edu/in-notes/iana/assignments/media-types/text/xml.[/url] This appears as the value of the <code><font face="新宋体">Type</font></code> attribute. XML Encryption uses the type definitions by IANA for various popular data formats such as RTF, PDF, and JPG. Refer to their Web site for complete details (see <a href="#resources"><font color="#996699">Resources</font></a>). If you have special application data types (perhaps your own DTDs or XSDs that belong to your company's content management system), you can specify them in the <code><font face="新宋体">Type</font></code> attribute of <code><font face="新宋体">EncryptedData</font></code> element. The other attribute, xmlns, specifies the XML Encryption namespace that we used to encrypt the XML data.</p><p><a name="N100B9"><span class="smalltitle"><strong><font face="Arial" size="3">Encrypting a single element with XML Encryption</font></strong></span></a></p><p>You may want to encrypt only one element in <a href="#code1"><font color="#996699">Listing 1</font></a> -- for example, the <code><font face="新宋体">Payment</font></code> element. In this case, the result is illustrated in <a href="listing3.html"><font color="#5c81a7">Listing 3</font></a>. Compare <a href="listing2.html"><font color="#5c81a7">Listing 2</font></a> and <a href="listing3.html"><font color="#5c81a7">Listing 3</font></a> and you'll find the following differences:</p><ol><li><a href="listing2.html"><font color="#5c81a7">Listing 2</font></a> contains only XML Encryption's schema, while <a href="listing3.html"><font color="#5c81a7">Listing 3</font></a> contains both XML Encryption as well as elements from the original data in <a href="#code1"><font color="#996699">Listing 1</font></a>. In <a href="listing3.html"><font color="#5c81a7">Listing 3</font></a>, the XML Encryption is embedded inside the user's XML. </li><li><a href="listing3.html"><font color="#5c81a7">Listing 3</font></a> also has a <code><font face="新宋体">Type</font></code> attribute in <code><font face="新宋体"><encrypteddata /></font></code>, but its value is [url]http://www.w3.org/2001/04/xmlenc#Element.[/url] We are no longer using the IANA type; instead, we are using the type that XML Encryption has specified. </li><li>Note particularly the fragment #Element at the end that means EncryptedData -- this represents one element. </li></ol><p><a name="N100FB"><span class="smalltitle"><strong><font face="Arial" size="3">Encrypting the content of an element</font></strong></span></a></p><p><a href="listing4.html"><font color="#5c81a7">Listing 4</font></a> will be the result if you want to encrypt only the content in <code><font face="新宋体">CardId</font></code>, an element in <a href="#code1"><font color="#996699">Listing 1</font></a>. This time, we have used [url]http://www.w3.org/2001/04/xmlenc#Content[/url] as the <code><font face="新宋体">Type</font></code> attribute value. We use this value whenever we have to encrypt only the content.</p><p><a name="N10112"><span class="smalltitle"><strong><font face="Arial" size="3">Encrypting non-XML data </font></strong></span></a></p><p>What if you want to send, say, a JPEG file through XML Encryption? <a href="listing5.html"><font color="#5c81a7">Listing 5</font></a> is a typical file that will result. The complete JPEG file in an encrypted sequence of bytes will appear as the content of the <code><font face="新宋体">CipherValue</font></code> element. Notice that there is only one difference between <a href="listing2.html"><font color="#5c81a7">Listing 2</font></a> and <a href="listing5.html"><font color="#5c81a7">Listing 5</font></a>: the <code><font face="新宋体">Type</font></code> attribute of the <code><font face="新宋体">EncryptedData</font></code> element. <a href="listing5.html"><font color="#5c81a7">Listing 5</font></a> includes the IANA type for the JPEG format. Similarly, you can encrypt any format by providing IANA values (refer to the IANA Web site, see <a href="#resources"><font color="#996699">Resources</font></a>).</p><br /><table cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><img height="1" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="539" /></td></tr></tbody></table><table class="no-print" cellspacing="0" cellpadding="0" align="right"><tbody><tr align="right"><td><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="middle"><img height="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /><br /></td><td valign="top" align="right"><a class="fbox" href="#main"><b><font color="#996699" size="2">Back to top</font></b></a></td></tr></tbody></table></td></tr></tbody></table><br /><br /><p><a name="keys"><span class="atitle"><font face="Arial" size="4">Keys for XML Encryption</font></span></a></p><p>In Listings 1 through 5, we have demonstrated encryption, which is not possible without keys (see the sidebar <a href="#sidebar"><font color="#996699">Public, private, and secret keys</font></a>). With XML Encryption, all key-related issues are divided into two parts:</p><ul><li>Exchange of keys (asymmetric encryption) </li><li>Using keys that were previously exchanged (symmetric encryption) </li></ul><p>This way, users can exchange keys and use them later.</p><p><a name="N1014E"><span class="smalltitle"><strong><font face="Arial" size="3">Asymmetric keys for exchange of secret keys</font></strong></span></a></p><p>In this scenario, one party sends its public key to a second party. The second party uses this public key to encrypt its secret key. This exchange of data is shown in <a href="listing6.html"><font color="#5c81a7">Listing 6</font></a> (request) and <a href="listing7.html"><font color="#5c81a7">Listing 7</font></a> (response). We will imagine Imran and Ali as the first party and second party, respectively, communicating with each other. Imran initializes the public key exchange request and sends his public key in the element named KeyValue . The attribute <code><font face="新宋体">CarriedKeyName</font></code> represents the name of the key that is being transported. Note that the root element of this structure is <code><font face="新宋体">EncryptedKey</font></code>, which contains the <code><font face="新宋体">ds:KeyInfo</font></code> and <code><font face="新宋体">ds:KeyValue</font></code> elements. The <code><font face="新宋体">ds: KeyInfo</font></code> and <code><font face="新宋体">ds:KeyValue</font></code> elements belong to the XML Digital Signature (ds:) namespace. XML Encryption relies entirely on the XML Digital Signature specification for key exchange. Therefore, both <code><font face="新宋体"><encryptedkey></encryptedkey></font></code>and <code><font face="新宋体"><keyvalue></keyvalue></font></code>belong to the XML Digital Signature specification namespace. <a href="listing7.html"><font color="#5c81a7">Listing 7</font></a> is what Ali sends in response. The <code><font face="新宋体">CipherValue</font></code> element in <a href="listing7.html"><font color="#5c81a7">Listing 7</font></a> contains a newly generated secret key, which is encrypted with the public key of the first party. Looking closely at Listing 6 and Listing 7, you'll notice that both request and response contain an <code><font face="新宋体">EncryptedKey</font></code> element. The <code><font face="新宋体">ds:KeyInfo</font></code> and <code><font face="新宋体">ds:KeyValue</font></code> elements within the <code><font face="新宋体">EncryptedKey</font></code> element carry the public key (<a href="listing6.html"><font color="#5c81a7">Listing 6</font></a>). On the other hand, the <code><font face="新宋体">CipherData</font></code> and <code><font face="新宋体">CipherValue</font></code> elements inside the <code><font face="新宋体">EncryptedKey</font></code> element (<a href="listing7.html"><font color="#5c81a7">Listing 7</font></a>) will transport the secret (encrypted) keys. Also notice that the <code><font face="新宋体">EncryptedKey</font></code> element always contains a <code><font face="新宋体">CarriedKeyName</font></code> attribute to specify the name of the key it is carrying.</p><p><a name="N101B5"><span class="smalltitle"><strong><font face="Arial" size="3">Using keys we have already exchanged in the past</font></strong></span></a></p><p>In the previous section, we exchanged a secret key. We'll now use that key to encrypt data. We will assume that Imran sends an XML message (<a href="listing8.html"><font color="#5c81a7">Listing 8</font></a>) in response to <a href="listing7.html"><font color="#5c81a7">Listing 7</font></a> (recall that <a href="listing7.html"><font color="#5c81a7">Listing 7</font></a> contains an encrypted secret key whose name is "Imran Ali"). Imran will decrypt this secret key with his (Imran's own) private key (as Ali encrypted this secret key with Imran's public key). Imran can encrypt the data he wants to send to Ali using this secret key and placing it inside the <code><font face="新宋体">CipherValue</font></code> element in <a href="listing8.html"><font color="#5c81a7">Listing 8</font></a>.</p><p>The <code><font face="新宋体">ds:KeyInfo</font></code> element in <a href="listing8.html"><font color="#5c81a7">Listing 8</font></a> contains a <code><font face="新宋体">KeyName</font></code> element. This combination refers to the name of the key that Imran uses for data encryption.</p><p>Figure 1 is a visual diagram showing this exchange of XML files for secure data exchange.</p><br /><br /><a name="figure1"><b>Figure 1. Sequence of key and data exchange with XML Encryption</b></a><br /> [attach]4479[/attach] <br /><p><a name="N101EB"><span class="smalltitle"><strong><font face="Arial" size="3">Referring external encrypted data from our XML Encryption file</font></strong></span></a></p><p>In <a href="listing5.html"><font color="#5c81a7">Listings 5</font></a> and <a href="listing7.html"><font color="#5c81a7">7</font></a>, the <code><font face="新宋体">CipherData</font></code> element can appear within an <code><font face="新宋体">EncryptedData</font></code> element or an <code><font face="新宋体">EncryptedKey</font></code> element. We use a <code><font face="新宋体">CipherData</font></code> element to refer to either the encrypted data (when it appears inside an <code><font face="新宋体">EncryptedData</font></code> element) or the encrypted key (when it appears inside an <code><font face="新宋体">EncryptedKey</font></code> element). In both Listings <a href="listing5.html"><font color="#5c81a7">5</font></a> and <a href="listing7.html"><font color="#5c81a7">7</font></a>, there is a <code><font face="新宋体">CipherValue</font></code> child element inside the <code><font face="新宋体">CipherData</font></code> element that contains the actual encrypted data.</p><p>We can also refer to external encrypted data or encrypted keys. This means that actual encrypted data or keys will be present somewhere else (perhaps somewhere on the Internet) and not inside our XML Encryption file. In this case we will use <code><font face="新宋体">CipherReference</font></code> instead of the <code><font face="新宋体">CipherValue</font></code> child element inside <code><font face="新宋体">CipherData</font></code>. We'll refer to the actual encrypted data through a URI. This is shown in <a href="listing9.html"><font color="#5c81a7">Listing 9</font></a>.</p><p><a name="N10234"><span class="smalltitle"><strong><font face="Arial" size="3">Referencing a particular element of an external XML file</font></strong></span></a></p><p><a href="listing10.html"><font color="#5c81a7">Listing 10</font></a> illustrates a variation of referring external XML files. Here we have referenced only a portion of the external file that the URI is pointing to. There is a <code><font face="新宋体">Transforms</font></code> child element inside the <code><font face="新宋体">CipherReference</font></code> element. This <code><font face="新宋体">Transforms</font></code> element may contain a number of <code><font face="新宋体">Transform</font></code> elements, each of which will contain a single XPath element. This XPath element specifies an XPath expression that refers to a particular node of the external XML document.</p><br /><table cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><img height="1" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="539" /></td></tr></tbody></table><table class="no-print" cellspacing="0" cellpadding="0" align="right"><tbody><tr align="right"><td><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="middle"><img height="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /><br /></td><td valign="top" align="right"><a class="fbox" href="#main"><b><font color="#996699" size="2">Back to top</font></b></a></td></tr></tbody></table></td></tr></tbody></table><br /><br /><p><a name="DOM"><span class="atitle"><font face="Arial" size="4">The DOM structure of our API</font></span></a></p><p>We have already demonstrated how to author XML Encryption files and exchange encrypted data. We will now propose a Java API for XML Encryption and provide a sample implementation. We will use DOM for this purpose.</p><p>Our DOM implementation consists of a set of classes (Listings 11 to 16). The <code><font face="新宋体">XmlEncryption </font></code>class (<a href="listing11.html"><font color="#5c81a7">Listing 11</font></a>) is a wrapper for the rest of the classes, which means users of our API will only need to interact with this class. It uses the functionality of other classes internally.</p><p><a href="listing11.html"><font color="#5c81a7">Listing 11</font></a> is a wrapper class that can generate a complete XML encrypted file.</p><p><a href="listing12.html"><font color="#5c81a7">Listing 12</font></a> authors the <code><font face="新宋体">EncryptedData</font></code> element.</p><p><a href="listing13.html"><font color="#5c81a7">Listing 13</font></a> authors the <code><font face="新宋体">EncryptionMethod</font></code> element.</p><p><a href="listing14.html"><font color="#5c81a7">Listing 14</font></a> authors the <code><font face="新宋体">KeyInfo</font></code> element.</p><p><a href="listing15.html"><font color="#5c81a7">Listing 15</font></a> authors the <code><font face="新宋体">CipherData </font></code>element.</p><p><a href="listing16.html"><font color="#5c81a7">Listing 16</font></a> contains names of Algorithms as static integers and their corresponding namespaces as strings.</p><p>The <code><font face="新宋体">XmlEncryption</font></code> class (<a href="listing11.html"><font color="#5c81a7">Listing 11</font></a>) contains various public Get/Set methods. The user will call Set methods to specify encryption parameters, which include the following:</p><ol><li>Name of the file to be encrypted </li><li>Name of the resulting XML Encryption file </li><li>Name of the algorithm for encryption </li><li>Name of the key that we will use for encryption </li><li>An ID for identification of <code><font face="新宋体"><encrypteddata /></font></code>structure </li></ol><p>We have demonstrated the use of the <code><font face="新宋体">XmlEncryption</font></code> class (<a href="listing11.html"><font color="#5c81a7">Listing 11</font></a>) through a <code><font face="新宋体">main ()</font></code> method. In the <code><font face="新宋体">main ()</font></code> method, we have created an instance of this class. The constructor instantiates DOM so that all underlying classes will use the same object.</p><table cellspacing="0" cellpadding="0" width="40%" align="right" border="0"><tbody><tr><td width="10"><img height="1" src="http://www.ibm.com/i/c.gif" width="10" /></td><td><table cellspacing="0" cellpadding="5" width="100%" border="1"><tbody><tr><td bgcolor="#eeeeee"><a name="sidebar"><b>Public, private, and secret keys</b></a><br /><p>We have used three technical terms related to keys (public, private, and secret keys). Although these terms are well known to developers working with end-to-end security, XML developers might not be familiar with them. Let's clarify these terms:</p><p><i>Public and private keys:</i> We use them as a pair. Some algorithms generate a pair of public and private keys. We send the public key to anyone who wants to exchange encrypted data with us. With a public key, we can only encrypt data of limited size. Our communicating partner encrypts the data with our public key and sends the encrypted data to us. We then decrypt the data with our private key. This is asymmetric encryption.</p><p><i>Secret key:</i> We use public and private keys to exchange a secret key. We normally generate secret keys randomly. Once we have exchanged a secret key with our communicating partner through asymmetric encryption, we then use this key for encrypting data at both ends. This is symmetric encryption.</p></td></tr></tbody></table></td></tr></tbody></table><p>This implementation only supports encryption of complete files, as illustrated in <a href="listing2.html"><font color="#5c81a7">Listing 2</font></a>. The <code><font face="新宋体">EncryptCompleteXmlFile ()</font></code> method will do this job by calling the following methods in a sequence:</p><ol><li><code><font face="新宋体">GetEncryptedDataDoc()</font></code> returns the object of the <code><font face="新宋体">EncryptedData</font></code> class (<a href="listing12.html"><font color="#5c81a7">Listing 12</font></a>). It contains the structure of the <code><font face="新宋体">EncryptedData</font></code> element. </li><li><code><font face="新宋体">GetEncryptionMethodDoc()</font></code> returns the Document object, which contains the XML structure corresponding to the <code><font face="新宋体">EncryptionMethod</font></code> element. <code><font face="新宋体">GetEncryptionMethodDoc()</font></code> uses <code><font face="新宋体">EncryptionMethod</font></code> class (<a href="listing13.html"><font color="#5c81a7">Listing 13</font></a>) to author XML. </li><li><code><font face="新宋体">GetKeyInfoDoc()</font></code> returns the <code><font face="新宋体">Document</font></code> object, which contains the XML structure corresponding to KeyInfo element. <code><font face="新宋体">GetKeyInfoDoc()</font></code> uses the object of <code><font face="新宋体">GenericKeyInfo</font></code> class (<a href="listing14.html"><font color="#5c81a7">Listing 14</font></a>) to author the XML. This class only provides the minimum necessary functionality (support for <code><font face="新宋体">KeyName</font></code> and <code><font face="新宋体">KeyValue</font></code> elements) you will inherit from <code><font face="新宋体">GenericKeyInfo</font></code> class to provide the complete functionality, which includes support for X509 Certificates, PGP Data, etc. </li><li><code><font face="新宋体">ReadFile()</font></code> fetches the data (complete XML file) that we want to encrypt. </li><li><code><font face="新宋体">GetEncryptedData()</font></code> for the time being is not doing anything. We'll implement this method in the next part of this article. It is supposed to create the encrypted form of XML data that we fetched in step 4. We have briefly discussed our encryption strategy in the last section (Java Cryptographic Architecture). </li><li><code><font face="新宋体">GetCipherDataDoc()</font></code> takes the encrypted data as an argument and returns the Document Object containing the <code><font face="新宋体">CipherData</font></code> element. <code><font face="新宋体">GetCipherDataDoc()</font></code> uses the Object of <code><font face="新宋体">CipherData</font></code> class (<a href="listing12.html"><font color="#5c81a7">Listing 12</font></a>) to author XML. </li><li>At the end, <code><font face="新宋体">addChild()</font></code> method of Object of <code><font face="新宋体">EncryptedData</font></code> (<a href="listing15.html"><font color="#5c81a7">Listing 15</font></a>) is called thrice, which will take the Document Objects of steps 2, 3, and 6 and adds them to the <code><font face="新宋体"><encrypteddata /></font></code>structure, which is the parent of all of them. </li><li><code><font face="新宋体">SaveEncryptedFile()</font></code> saves the completed XML Encryption file. </li></ol><p><code><font face="新宋体">AlgoNames</font></code> (<a href="listing16.html"><font color="#5c81a7">Listing 16</font></a>) is a helper class that only specifies namespace declarations required by XML Encryption.</p><p>The <code><font face="新宋体">XmlEncryption</font></code> class (<a href="listing11.html"><font color="#5c81a7">Listing 11</font></a>) can also be used as a server-side component. In the next part of this series, we'll demonstrate its use inside independent as well as server-side applications.</p><p>The set of classes that we have developed only performs DOM-based XML authoring. We need to implement cryptographic functionality as well. We will now try to form a strategy for cryptographic support. For this purpose, we need to study the Java Cryptographic Architecture (JCA).</p><br /><table cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><img height="1" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="539" /></td></tr></tbody></table><table class="no-print" cellspacing="0" cellpadding="0" align="right"><tbody><tr align="right"><td><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="middle"><img height="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /><br /></td><td valign="top" align="right"><a class="fbox" href="#main"><b><font color="#996699" size="2">Back to top</font></b></a></td></tr></tbody></table></td></tr></tbody></table><br /><br /><p><a name="JCA"><span class="atitle"><font face="Arial" size="4">The Java Cryptographic Architecture (JCA)</font></span></a></p><p>Java offers complete support for cryptography. For this purpose, there are several packages inside J2SE, covering all the main features of security architecture such as access controls, signatures, certificates, key pairs, key stores, and message digests.</p><p>The primary principle of JCA design is to separate cryptographic concepts from algorithmic implementations, so that different vendors can offer their tools within the JCA framework.</p><p><a name="N10393"><span class="smalltitle"><strong><font face="Arial" size="3">JCA Engine classes</font></strong></span></a></p><p>JCA defines a series of Engine classes, where each Engine provides a cryptographic function. For example, there are several different standards of MD (Message Digest) algorithm. All these standards differ in the implementation, but at the Engine API level they are all the same. Different vendors are free to provide implementations of specific algorithms.</p><p><a name="N1039A"><span class="smalltitle"><strong><font face="Arial" size="3">Java Cryptographic Extension (JCE)</font></strong></span></a></p><p>All independent (third party) vendor implementations of cryptographic algorithms are called Java Cryptographic Extensions (JCEs). Sun Microsystems has also provided an implementation of JCE. Whenever we use JCE, we need to configure it with JCA. For this, we need to do the following:</p><p>1. Add the address of the jar file to configure the provider (all JCE implementations are called providers) in the <code><font face="新宋体">CLASSPATH</font></code> environment variables.</p><p>2. Configure the provider in the list of your approved providers by editing the java.security file. This file is located in JavaHome/jre/lib/security folder. The following is the syntax to specify the priority: <code><font face="新宋体">security.provider.<n />=<masterclassname /></font></code>. Here, n is the priority number (1, 2, 3, etc.). <code><font face="新宋体">MasterClassName</font></code> is the name of master class to which the engine classes will call for a specific algorithm implementation. The provider's documentation will specify its master class name. For example, consider the following entries in a java.security file:</p><ul><li><code><font face="新宋体">security.provider.1=sun.security.provider.Sun</font></code> </li><li><code><font face="新宋体">security.provider.2=com.sun.rsajca.Provider</font></code> </li><li><code><font face="新宋体">security.provider.3=com.sun.net.ssl.internal.ssl.Provider</font></code> </li></ul><p>These entries mean that the engine class will search for any algorithm implementation in the above mentioned order. It will execute the implementation found first. After these simple steps, we are all set to use JCA/JCE in our XML Encryption application.</p><p><a name="N103CA"><span class="smalltitle"><strong><font face="Arial" size="3">Using JCA and JCE in our implementation of XML Encryption</font></strong></span></a></p><p>The <code><font face="新宋体">GetEncryptedData()</font></code> function in our wrapper class, <code><font face="新宋体">XmlEncryption</font></code> (<a href="listing11.html"><font color="#5c81a7">Listing 11</font></a>), is the place to handle all JCA/JCE-related issues. Currently this method only returns the string "This is Cipher Data". We have not yet written JCA/JCE-related classes. This method takes the unencrypted data and returns it as an encrypted string. We will handle all the algorithm- and key-related issues in this method after writing the wrapper classes for JCA/JCE.</p><p><b>Next time:</b> In our next installment of this series of articles, we will discuss and implement the details of cryptography. We'll demonstrate the working of encryption and decryption classes and their interaction with parsing logic, and present applications of XML Encryption in Web services.</p><br /><table cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><img height="1" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="539" /></td></tr></tbody></table><table class="no-print" cellspacing="0" cellpadding="0" align="right"><tbody><tr align="right"><td><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="middle"><img height="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /><br /></td><td valign="top" align="right"><a class="fbox" href="#main"><b><font color="#996699" size="2">Back to top</font></b></a></td></tr></tbody></table></td></tr></tbody></table><br /><br /><p><a name="resources"><span class="atitle"><font face="Arial" size="4">Resources</font></span></a></p><ul><li>Read "<a href="http://www.ibm.com/developerworks/library/x-encrypt2/"><font color="#5c81a7">Exploring XML Encryption, Part 2</font></a>" of this two-part series of articles on XML Encryption by Bilal Siddiqui (<i>developerworks</i>, August 2002).<br /><br /></li><li>Visit W3C to see official details of <a href="http://www.w3.org/TR/xml-encryption-req"><font color="#5c81a7">XML Encryption Requirements</font></a> and <a href="http://www.w3.org/TR/xmlenc-core/"><font color="#5c81a7">XML Encryption Processing and Syntax</font></a>.<br /><br /></li><li>Check out news about XML Encryption at <a href="http://www.oasis-open.org/cover/xmlAndEncryption.html"><font color="#5c81a7">OASIS/Robin Cover's XML Cover Pages</font></a>.<br /><br /></li><li><a href="http://www.alphaworks.ibm.com/tech/xmlsecuritysuite"><font color="#5c81a7">IBM's toolkits for XML Encryption</font></a> is available for download at alphaWorks.<br /><br /></li><li>Check what's happening at <a href="http://jcp.org/jsr/detail/106.jsp"><font color="#5c81a7">SUN's community process for XML Encryption</font></a>.<br /><br /></li><li>We mentioned XPath in this article. Here is a <a href="http://www.oreilly.com/catalog/xmlnut/chapter/ch09.html"><font color="#5c81a7">chapter on XPath</font></a> from an XML book <i>XML in a Nutshell</i> by O'Reilly.<br /><br /></li><li>We mentioned IANA in this article. Visit <a href="http://www.isi.edu/in-notes/iana/assignments/media-types/media-types"><font color="#5c81a7">IANA's Web site</font></a> to check type definitions for popular data formats.<br /><br /></li><li><a href="http://www-3.ibm.com/security/services/index.shtml"><font color="#5c81a7">IBM Security Services</font></a> can help you determine what your risks are, and then design a security program to address them. <br /><br /></li><li>IBM <a href="http://www-4.ibm.com/software/ad/studioappdev/"><font color="#5c81a7">WebSphere Studio Application Developer</font></a> is an easy-to-use, integrated development environment for building, testing, and deploying J2EE (TM) applications, including generating XML documents from DTDs and schemas. <br /></li></ul><br /><table cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><img height="1" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="539" /></td></tr></tbody></table><table class="no-print" cellspacing="0" cellpadding="0" align="right"><tbody><tr align="right"><td><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="middle"><img height="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /><br /></td><td valign="top" align="right"><a class="fbox" href="#main"><b><font color="#996699" size="2">Back to top</font></b></a></td></tr></tbody></table></td></tr></tbody></table><br /><br /><p><a name="author"><span class="atitle"><font face="Arial" size="4">About the author</font></span></a></p><table cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td colspan="3"><font face="Arial" size="4"><img height="5" src="http://www.ibm.com/i/c.gif" width="539" /></font></td></tr><tr valign="top" align="left"><td><p><font face="Arial" size="4"></font></p></td><td><font face="Arial" size="4"><img height="5" src="http://www.ibm.com/i/c.gif" width="4" /></font></td><td width="100%"><p>XML consultant Bilal Siddiqui received a degree in Electronics Engineering from the University of Engineering and Technology, Lahore, in 1995. He then began designing software solutions for industrial control systems. Later he turned to XML and used his experience programming in C++ to build Web- and WAP-based XML processing tools, server-side parsing solutions, and service applications. You can e-mail Bilal for working copies of the code files contained in this article at <a href="mailto:wap_monster@yahoo.com?cc="><font color="#5c81a7">wap_monster@yahoo.com</font></a>.</p></td></tr></tbody></table>
页:
[1]