Git init
[external/xmlsec1.git] / docs / api / xmlsec-notes-dynamic-encryption-templates.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Creating dynamic encryption templates.</title>
5 <meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.79">
6 <link rel="HOME" title="XML Security Library Reference Manual" href="index.html">
7 <link rel="UP" title="Creating dynamic templates." href="xmlsec-notes-templates.html">
8 <link rel="PREVIOUS" title="Creating dynamic signature templates." href="xmlsec-notes-dynamic-signature-templates.html">
9 <link rel="NEXT" title="Verifing and decrypting documents." href="xmlsec-notes-verify-decrypt.html">
10 <style type="text/css">.synopsis, .classsynopsis {
11     background: #eeeeee;
12     border: solid 1px #aaaaaa;
13     padding: 0.5em;
14 }
15 .programlisting {
16     background: #eeeeff;
17     border: solid 1px #aaaaff;
18     padding: 0.5em;
19 }
20 .variablelist {
21     padding: 4px;
22     margin-left: 3em;
23 }
24 .navigation {
25     background: #ffeeee;
26     border: solid 1px #ffaaaa;
27     margin-top: 0.5em;
28     margin-bottom: 0.5em;
29 }
30 .navigation a {
31     color: #770000;
32 }
33 .navigation a:visited {
34     color: #550000;
35 }
36 .navigation .title {
37     font-size: 200%;
38 }</style>
39 </head>
40 <body><table witdh="100%" valign="top"><tr valign="top">
41 <td valign="top" align="left" width="210">
42 <img src="../images/logo.gif" alt="XML Security Library" border="0"><p></p>
43 <ul>
44 <li><a href="../index.html">Home</a></li>
45 <li><a href="../download.html">Download</a></li>
46 <li><a href="../news.html">News</a></li>
47 <li><a href="../documentation.html">Documentation</a></li>
48 <ul>
49 <li><a href="../faq.html">FAQ</a></li>
50 <li><a href="../api/xmlsec-notes.html">Tutorial</a></li>
51 <li><a href="../api/xmlsec-reference.html">API reference</a></li>
52 <li><a href="../api/xmlsec-examples.html">Examples</a></li>
53 </ul>
54 <li><a href="../xmldsig.html">XML Digital Signature</a></li>
55 <ul><li><a href="http://www.aleksey.com/xmlsec/xmldsig-verifier.html">Online Verifier</a></li></ul>
56 <li><a href="../xmlenc.html">XML Encryption</a></li>
57 <li><a href="../c14n.html">XML Canonicalization</a></li>
58 <li><a href="../bugs.html">Reporting Bugs</a></li>
59 <li><a href="http://www.aleksey.com/pipermail/xmlsec">Mailing list</a></li>
60 <li><a href="../related.html">Related</a></li>
61 <li><a href="../authors.html">Authors</a></li>
62 </ul>
63 <table width="100%">
64 <tr>
65 <td width="15"></td>
66 <td><a href="http://xmlsoft.org/"><img src="../images/libxml2-logo.png" alt="LibXML2" border="0"></a></td>
67 </tr>
68 <tr>
69 <td width="15"></td>
70 <td><a href="http://xmlsoft.org/XSLT"><img src="../images/libxslt-logo.png" alt="LibXSLT" border="0"></a></td>
71 </tr>
72 <tr>
73 <td width="15"></td>
74 <td><a href="http://www.openssl.org/"><img src="../images/openssl-logo.png" alt="OpenSSL" border="0"></a></td>
75 </tr>
76 <!--Links - start--><!--Links - end-->
77 </table>
78 </td>
79 <td valign="top"><table width="100%" valign="top"><tr><td valign="top" align="left" id="xmlsecContent">
80 <table width="100%" class="navigation" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
81 <td><a accesskey="p" href="xmlsec-notes-dynamic-signature-templates.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
82 <td><a accesskey="u" href="xmlsec-notes-templates.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
83 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
84 <th width="100%" align="center">XML Security Library Reference Manual</th>
85 <td><a accesskey="n" href="xmlsec-notes-verify-decrypt.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
86 </tr></table>
87 <br clear="all"><div class="SECT1">
88 <h1 class="SECT1"><a name="XMLSEC-NOTES-DYNAMIC-ENCRYPTION-TEMPLATES">Creating dynamic encryption templates.</a></h1>
89 <p>The encryption template has structure similar 
90         to the XML Encryption structure as it is described in 
91         <a href="http://www.w3.org/TR/xmlenc-core" target="_top">specification</a>.
92         The only difference is that some nodes (for example,
93         &lt;enc:CipherValue/&gt;)
94         are empty. The XML Security Library sets the content of these
95         nodes after doing necessary calculations.
96         </p>
97 <div class="FIGURE">
98 <a name="AEN256"></a><p><b>Figure 2. XML Encryption structure</b></p>
99 <pre class="PROGRAMLISTING">&lt;enc:EncryptedData Id? Type? MimeType? Encoding?&gt;
100     &lt;enc:EncryptionMethod Algorithm /&gt;?
101     (&lt;dsig:KeyInfo&gt;
102         &lt;dsig:KeyName&gt;?
103         &lt;dsig:KeyValue&gt;?
104         &lt;dsig:RetrievalMethod&gt;?
105         &lt;dsig:X509Data&gt;?
106         &lt;dsig:PGPData&gt;?
107         &lt;enc:EncryptedKey&gt;?
108         &lt;enc:AgreementMethod&gt;?
109         &lt;dsig:KeyName&gt;?
110         &lt;dsig:RetrievalMethod&gt;?
111         &lt;*&gt;?
112     &lt;/dsig:KeyInfo&gt;)?
113     &lt;enc:CipherData&gt;
114         &lt;enc:CipherValue&gt;?
115         &lt;enc:CipherReference URI?&gt;?
116     &lt;/enc:CipherData&gt;
117     &lt;enc:EncryptionProperties&gt;?
118 &lt;/enc:EncryptedData&gt;      
119             </pre>
120 </div>
121 <p>          </p>
122 <div class="EXAMPLE">
123 <a name="AEN260"></a><p><b>Example 2. Creating dynamic encrytion template.</b></p>
124 <pre class="PROGRAMLISTING">/**
125  * encrypt_file:
126  * @xml_file:           the encryption template file name.
127  * @key_file:           the Triple DES key file.
128  *
129  * Encrypts #xml_file using a dynamicaly created template and DES key from
130  * #key_file.
131  *
132  * Returns 0 on success or a negative value if an error occurs.
133  */
134 int 
135 encrypt_file(const char* xml_file, const char* key_file) {
136     xmlDocPtr doc = NULL;
137     xmlNodePtr encDataNode = NULL;
138     xmlNodePtr keyInfoNode = NULL;
139     xmlSecEncCtxPtr encCtx = NULL;
140     int res = -1;
141     
142     assert(xml_file);
143     assert(key_file);
144
145     /* load template */
146     doc = xmlParseFile(xml_file);
147     if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
148         fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
149         goto done;      
150     }
151     
152     /* create encryption template to encrypt XML file and replace 
153      * its content with encryption result */
154     encDataNode = xmlSecTmplEncDataCreate(doc, xmlSecTransformDes3CbcId,
155                                 NULL, xmlSecTypeEncElement, NULL, NULL);
156     if(encDataNode == NULL) {
157         fprintf(stderr, "Error: failed to create encryption template\n");
158         goto done;   
159     }
160
161     /* we want to put encrypted data in the &lt;enc:CipherValue/&gt; node */
162     if(xmlSecTmplEncDataEnsureCipherValue(encDataNode) == NULL) {
163         fprintf(stderr, "Error: failed to add CipherValue node\n");
164         goto done;   
165     }
166
167     /* add &lt;dsig:KeyInfo/&gt; and &lt;dsig:KeyName/&gt; nodes to put key name in the signed document */
168     keyInfoNode = xmlSecTmplEncDataEnsureKeyInfo(encDataNode, NULL);
169     if(keyInfoNode == NULL) {
170         fprintf(stderr, "Error: failed to add key info\n");
171         goto done;              
172     }
173
174     if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL) == NULL) {
175         fprintf(stderr, "Error: failed to add key name\n");
176         goto done;              
177     }
178
179     /* create encryption context, we don't need keys manager in this example */
180     encCtx = xmlSecEncCtxCreate(NULL);
181     if(encCtx == NULL) {
182         fprintf(stderr,"Error: failed to create encryption context\n");
183         goto done;
184     }
185
186     /* load DES key, assuming that there is not password */
187     encCtx-&gt;encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
188     if(encCtx-&gt;encKey == NULL) {
189         fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
190         goto done;
191     }
192
193     /* set key name to the file name, this is just an example! */
194     if(xmlSecKeySetName(encCtx-&gt;encKey, key_file) &lt; 0) {
195         fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
196         goto done;
197     }
198
199     /* encrypt the data */
200     if(xmlSecEncCtxXmlEncrypt(encCtx, encDataNode, xmlDocGetRootElement(doc)) &lt; 0) {
201         fprintf(stderr,"Error: encryption failed\n");
202         goto done;
203     }
204     
205     /* we template is inserted in the doc */
206     encDataNode = NULL;
207         
208     /* print encrypted data with document to stdout */
209     xmlDocDump(stdout, doc);
210     
211     /* success */
212     res = 0;
213
214 done:    
215
216     /* cleanup */
217     if(encCtx != NULL) {
218         xmlSecEncCtxDestroy(encCtx);
219     }
220
221     if(encDataNode != NULL) {
222         xmlFreeNode(encDataNode);
223     }
224         
225     if(doc != NULL) {
226         xmlFreeDoc(doc); 
227     }
228     return(res);
229 }
230                 </pre>
231 <p><a href="xmlsec-encrypt-dynamic-template.html#XMLSEC-EXAMPLE-ENCRYPT2">Full program listing</a></p>
232 </div>
233         </div>
234 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
235 <td align="left"><a accesskey="p" href="xmlsec-notes-dynamic-signature-templates.html"><b>&lt;&lt;&lt; Creating dynamic signature templates.</b></a></td>
236 <td align="right"><a accesskey="n" href="xmlsec-notes-verify-decrypt.html"><b>Verifing and decrypting documents. &gt;&gt;&gt;</b></a></td>
237 </tr></table>
238 </td></tr></table></td>
239 </tr></table></body>
240 </html>