Git init
[external/xmlsec1.git] / docs / api / xmlsec-encrypt-dynamic-template.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Encrypting data with a dynamicaly created template.</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="Examples." href="xmlsec-examples.html">
8 <link rel="PREVIOUS" title="Encrypting data with a template file." href="xmlsec-encrypt-template-file.html">
9 <link rel="NEXT" title="Encrypting data with a session key." href="xmlsec-encrypt-with-session-key.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-encrypt-template-file.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
82 <td><a accesskey="u" href="xmlsec-examples.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-encrypt-with-session-key.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-ENCRYPT-DYNAMIC-TEMPLATE">Encrypting data with a dynamicaly created template.</a></h1>
89 <br clear="all"><div class="SECT2">
90 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-ENCRYPT2">encrypt2.c</a></h2>
91 <p></p>
92 <div class="INFORMALEXAMPLE">
93 <p></p>
94 <a name="AEN766"></a><pre class="PROGRAMLISTING">/** 
95  * XML Security Library example: Encrypting XML file with a dynamicaly created template.
96  * 
97  * Encrypts XML file using a dynamicaly created template file and a DES key 
98  * from a binary file
99  * 
100  * Usage: 
101  *      ./encrypt2 &lt;xml-doc&gt; &lt;des-key-file&gt; 
102  *
103  * Example:
104  *      ./encrypt2 encrypt2-doc.xml deskey.bin &gt; encrypt2-res.xml
105  *
106  * The result could be decrypted with decrypt1 example:
107  *      ./decrypt1 encrypt2-res.xml deskey.bin
108  *
109  * This is free software; see Copyright file in the source
110  * distribution for preciese wording.
111  * 
112  * Copyright (C) 2002-2003 Aleksey Sanin &lt;aleksey@aleksey.com&gt;
113  */
114 #include &lt;stdlib.h&gt;
115 #include &lt;string.h&gt;
116 #include &lt;assert.h&gt;
117
118 #include &lt;libxml/tree.h&gt;
119 #include &lt;libxml/xmlmemory.h&gt;
120 #include &lt;libxml/parser.h&gt;
121
122 #ifndef XMLSEC_NO_XSLT
123 #include &lt;libxslt/xslt.h&gt;
124 #endif /* XMLSEC_NO_XSLT */
125
126 #include &lt;xmlsec/xmlsec.h&gt;
127 #include &lt;xmlsec/xmltree.h&gt;
128 #include &lt;xmlsec/xmlenc.h&gt;
129 #include &lt;xmlsec/templates.h&gt;
130 #include &lt;xmlsec/crypto.h&gt;
131
132 int encrypt_file(const char* xml_file, const char* key_file);
133
134 int 
135 main(int argc, char **argv) {
136     assert(argv);
137
138     if(argc != 3) {
139         fprintf(stderr, "Error: wrong number of arguments.\n");
140         fprintf(stderr, "Usage: %s &lt;xml-file&gt; &lt;key-file&gt;\n", argv[0]);
141         return(1);
142     }
143
144     /* Init libxml and libxslt libraries */
145     xmlInitParser();
146     LIBXML_TEST_VERSION
147     xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
148     xmlSubstituteEntitiesDefault(1);
149 #ifndef XMLSEC_NO_XSLT
150     xmlIndentTreeOutput = 1; 
151 #endif /* XMLSEC_NO_XSLT */
152                 
153     /* Init xmlsec library */
154     if(xmlSecInit() &lt; 0) {
155         fprintf(stderr, "Error: xmlsec initialization failed.\n");
156         return(-1);
157     }
158
159     /* Check loaded library version */
160     if(xmlSecCheckVersion() != 1) {
161         fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
162         return(-1);
163     }
164
165     /* Load default crypto engine if we are supporting dynamic
166      * loading for xmlsec-crypto libraries. Use the crypto library
167      * name ("openssl", "nss", etc.) to load corresponding 
168      * xmlsec-crypto library.
169      */
170 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
171     if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) &lt; 0) {
172         fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
173                         "that you have it installed and check shared libraries path\n"
174                         "(LD_LIBRARY_PATH) envornment variable.\n");
175         return(-1);     
176     }
177 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
178
179     /* Init crypto library */
180     if(xmlSecCryptoAppInit(NULL) &lt; 0) {
181         fprintf(stderr, "Error: crypto initialization failed.\n");
182         return(-1);
183     }
184
185     /* Init xmlsec-crypto library */
186     if(xmlSecCryptoInit() &lt; 0) {
187         fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
188         return(-1);
189     }
190
191     if(encrypt_file(argv[1], argv[2]) &lt; 0) {
192         return(-1);
193     }    
194     
195     /* Shutdown xmlsec-crypto library */
196     xmlSecCryptoShutdown();
197     
198     /* Shutdown crypto library */
199     xmlSecCryptoAppShutdown();
200     
201     /* Shutdown xmlsec library */
202     xmlSecShutdown();
203
204     /* Shutdown libxslt/libxml */
205 #ifndef XMLSEC_NO_XSLT
206     xsltCleanupGlobals();            
207 #endif /* XMLSEC_NO_XSLT */
208     xmlCleanupParser();
209     
210     return(0);
211 }
212
213 /**
214  * encrypt_file:
215  * @xml_file:           the encryption template file name.
216  * @key_file:           the Triple DES key file.
217  *
218  * Encrypts #xml_file using a dynamicaly created template and DES key from
219  * #key_file.
220  *
221  * Returns 0 on success or a negative value if an error occurs.
222  */
223 int 
224 encrypt_file(const char* xml_file, const char* key_file) {
225     xmlDocPtr doc = NULL;
226     xmlNodePtr encDataNode = NULL;
227     xmlNodePtr keyInfoNode = NULL;
228     xmlSecEncCtxPtr encCtx = NULL;
229     int res = -1;
230     
231     assert(xml_file);
232     assert(key_file);
233
234     /* load template */
235     doc = xmlParseFile(xml_file);
236     if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
237         fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
238         goto done;      
239     }
240     
241     /* create encryption template to encrypt XML file and replace 
242      * its content with encryption result */
243     encDataNode = xmlSecTmplEncDataCreate(doc, xmlSecTransformDes3CbcId,
244                                 NULL, xmlSecTypeEncElement, NULL, NULL);
245     if(encDataNode == NULL) {
246         fprintf(stderr, "Error: failed to create encryption template\n");
247         goto done;   
248     }
249
250     /* we want to put encrypted data in the &lt;enc:CipherValue/&gt; node */
251     if(xmlSecTmplEncDataEnsureCipherValue(encDataNode) == NULL) {
252         fprintf(stderr, "Error: failed to add CipherValue node\n");
253         goto done;   
254     }
255
256     /* add &lt;dsig:KeyInfo/&gt; and &lt;dsig:KeyName/&gt; nodes to put key name in the signed document */
257     keyInfoNode = xmlSecTmplEncDataEnsureKeyInfo(encDataNode, NULL);
258     if(keyInfoNode == NULL) {
259         fprintf(stderr, "Error: failed to add key info\n");
260         goto done;              
261     }
262
263     if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL) == NULL) {
264         fprintf(stderr, "Error: failed to add key name\n");
265         goto done;              
266     }
267
268     /* create encryption context, we don't need keys manager in this example */
269     encCtx = xmlSecEncCtxCreate(NULL);
270     if(encCtx == NULL) {
271         fprintf(stderr,"Error: failed to create encryption context\n");
272         goto done;
273     }
274
275     /* load DES key, assuming that there is not password */
276     encCtx-&gt;encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
277     if(encCtx-&gt;encKey == NULL) {
278         fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
279         goto done;
280     }
281
282     /* set key name to the file name, this is just an example! */
283     if(xmlSecKeySetName(encCtx-&gt;encKey, key_file) &lt; 0) {
284         fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
285         goto done;
286     }
287
288     /* encrypt the data */
289     if(xmlSecEncCtxXmlEncrypt(encCtx, encDataNode, xmlDocGetRootElement(doc)) &lt; 0) {
290         fprintf(stderr,"Error: encryption failed\n");
291         goto done;
292     }
293     
294     /* we template is inserted in the doc */
295     encDataNode = NULL;
296         
297     /* print encrypted data with document to stdout */
298     xmlDocDump(stdout, doc);
299     
300     /* success */
301     res = 0;
302
303 done:    
304
305     /* cleanup */
306     if(encCtx != NULL) {
307         xmlSecEncCtxDestroy(encCtx);
308     }
309
310     if(encDataNode != NULL) {
311         xmlFreeNode(encDataNode);
312     }
313         
314     if(doc != NULL) {
315         xmlFreeDoc(doc); 
316     }
317     return(res);
318 }\r</pre>
319 <p></p>
320 </div>
321 </div>
322 <br clear="all"><div class="SECT2">
323 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-ENCRYPT2-DOC">encrypt2-doc.xml</a></h2>
324 <p></p>
325 <div class="INFORMALEXAMPLE">
326 <p></p>
327 <a name="AEN771"></a><pre class="PROGRAMLISTING">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
328 &lt;!-- 
329 XML Security Library example: Original XML doc file before encryption (encrypt2 example). 
330 --&gt;
331 &lt;Envelope xmlns="urn:envelope"&gt;
332   &lt;Data&gt;
333         Hello, World!
334   &lt;/Data&gt;
335 &lt;/Envelope&gt;</pre>
336 <p></p>
337 </div>
338 </div>
339 <br clear="all"><div class="SECT2">
340 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-ENCRYPT2-RES">encrypt2-res.xml</a></h2>
341 <p></p>
342 <div class="INFORMALEXAMPLE">
343 <p></p>
344 <a name="AEN776"></a><pre class="PROGRAMLISTING">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
345 &lt;!-- 
346 XML Security Library example: Encrypted XML file (encrypt2 example). 
347 --&gt;
348 &lt;EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#" Type="http://www.w3.org/2001/04/xmlenc#Element"&gt;
349 &lt;EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/&gt;
350 &lt;KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
351 &lt;KeyName&gt;deskey.bin&lt;/KeyName&gt;
352 &lt;/KeyInfo&gt;
353 &lt;CipherData&gt;
354 &lt;CipherValue&gt;WXlDyktaADlUe+PywKwS3KdKlahCteEKxi/hRlHcXNQlGwNGrYKy8aQ6dLtX1bKg
355 IgL/XoAQN3B27zD91b1ZLGh6QQ9CjnVD98+hYJ9TPp4piPnII4vGUA==&lt;/CipherValue&gt;
356 &lt;/CipherData&gt;
357 &lt;/EncryptedData&gt;</pre>
358 <p></p>
359 </div>
360 </div>
361 </div>
362 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
363 <td align="left"><a accesskey="p" href="xmlsec-encrypt-template-file.html"><b>&lt;&lt;&lt; Encrypting data with a template file.</b></a></td>
364 <td align="right"><a accesskey="n" href="xmlsec-encrypt-with-session-key.html"><b>Encrypting data with a session key. &gt;&gt;&gt;</b></a></td>
365 </tr></table>
366 </td></tr></table></td>
367 </tr></table></body>
368 </html>