Git init
[external/xmlsec1.git] / docs / api / xmlsec-decrypt-with-keys-mngr.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Decrypting data with keys manager.</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="Decrypting data with a single key." href="xmlsec-decrypt-with-signle-key.html">
9 <link rel="NEXT" title="Writing a custom keys manager." href="xmlsec-custom-keys-manager.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-decrypt-with-signle-key.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-custom-keys-manager.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-DECRYPT-WITH-KEYS-MNGR">Decrypting data with keys manager.</a></h1>
89 <br clear="all"><div class="SECT2">
90 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-DECRYPT2">decrypt2.c</a></h2>
91 <p></p>
92 <div class="INFORMALEXAMPLE">
93 <p></p>
94 <a name="AEN807"></a><pre class="PROGRAMLISTING">/** 
95  * XML Security Library example: Decrypting an encrypted file using keys manager.
96  * 
97  * Decrypts encrypted XML file using keys manager and a list of 
98  * DES key from a binary file
99  * 
100  * Usage: 
101  *      ./decrypt2 &lt;xml-enc&gt; &lt;des-key-file1&gt; [&lt;des-key-file2&gt; [...]] 
102  *
103  * Example:
104  *      ./decrypt2 encrypt1-res.xml deskey.bin
105  *      ./decrypt2 encrypt2-res.xml deskey.bin
106  *
107  * This is free software; see Copyright file in the source
108  * distribution for preciese wording.
109  * 
110  * Copyright (C) 2002-2003 Aleksey Sanin &lt;aleksey@aleksey.com&gt;
111  */
112 #include &lt;stdlib.h&gt;
113 #include &lt;string.h&gt;
114 #include &lt;assert.h&gt;
115
116 #include &lt;libxml/tree.h&gt;
117 #include &lt;libxml/xmlmemory.h&gt;
118 #include &lt;libxml/parser.h&gt;
119
120 #ifndef XMLSEC_NO_XSLT
121 #include &lt;libxslt/xslt.h&gt;
122 #endif /* XMLSEC_NO_XSLT */
123
124 #include &lt;xmlsec/xmlsec.h&gt;
125 #include &lt;xmlsec/xmltree.h&gt;
126 #include &lt;xmlsec/xmlenc.h&gt;
127 #include &lt;xmlsec/crypto.h&gt;
128
129 xmlSecKeysMngrPtr load_des_keys(char** files, int files_size);
130 int decrypt_file(xmlSecKeysMngrPtr mngr, const char* enc_file);
131
132 int 
133 main(int argc, char **argv) {
134     xmlSecKeysMngrPtr mngr;
135
136     assert(argv);
137
138     if(argc != 3) {
139         fprintf(stderr, "Error: wrong number of arguments.\n");
140         fprintf(stderr, "Usage: %s &lt;enc-file&gt; &lt;key-file1&gt; [&lt;key-file2&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     /* create keys manager and load keys */
192     mngr = load_des_keys(&amp;(argv[2]), argc - 2);
193     if(mngr == NULL) {
194         return(-1);
195     }
196
197     if(decrypt_file(mngr, argv[1]) &lt; 0) {
198         xmlSecKeysMngrDestroy(mngr);    
199         return(-1);
200     }    
201
202     /* destroy keys manager */
203     xmlSecKeysMngrDestroy(mngr);
204     
205     /* Shutdown xmlsec-crypto library */
206     xmlSecCryptoShutdown();
207     
208     /* Shutdown crypto library */
209     xmlSecCryptoAppShutdown();
210     
211     /* Shutdown xmlsec library */
212     xmlSecShutdown();
213
214     /* Shutdown libxslt/libxml */
215 #ifndef XMLSEC_NO_XSLT
216     xsltCleanupGlobals();            
217 #endif /* XMLSEC_NO_XSLT */
218     xmlCleanupParser();
219     
220     return(0);
221 }
222
223 /**
224  * load_des_keys:
225  * @files:              the list of filenames.
226  * @files_size:         the number of filenames in #files.
227  *
228  * Creates simple keys manager and load DES keys from #files in it.
229  * The caller is responsible for destroing returned keys manager using
230  * @xmlSecKeysMngrDestroy.
231  *
232  * Returns the pointer to newly created keys manager or NULL if an error
233  * occurs.
234  */
235 xmlSecKeysMngrPtr 
236 load_des_keys(char** files, int files_size) {
237     xmlSecKeysMngrPtr mngr;
238     xmlSecKeyPtr key;
239     int i;
240     
241     assert(files);
242     assert(files_size &gt; 0);
243     
244     /* create and initialize keys manager, we use a simple list based
245      * keys manager, implement your own xmlSecKeysStore klass if you need
246      * something more sophisticated 
247      */
248     mngr = xmlSecKeysMngrCreate();
249     if(mngr == NULL) {
250         fprintf(stderr, "Error: failed to create keys manager.\n");
251         return(NULL);
252     }
253     if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) &lt; 0) {
254         fprintf(stderr, "Error: failed to initialize keys manager.\n");
255         xmlSecKeysMngrDestroy(mngr);
256         return(NULL);
257     }    
258     
259     for(i = 0; i &lt; files_size; ++i) {
260         assert(files[i]);
261
262         /* load DES key */
263         key = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, files[i]);
264         if(key == NULL) {
265             fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", files[i]);
266             xmlSecKeysMngrDestroy(mngr);
267             return(NULL);
268         }
269
270         /* set key name to the file name, this is just an example! */
271         if(xmlSecKeySetName(key, BAD_CAST files[i]) &lt; 0) {
272             fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", files[i]);
273             xmlSecKeyDestroy(key);
274             xmlSecKeysMngrDestroy(mngr);
275             return(NULL);
276         }
277         
278         /* add key to keys manager, from now on keys manager is responsible 
279          * for destroying key 
280          */
281         if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key) &lt; 0) {
282             fprintf(stderr,"Error: failed to add key from \"%s\" to keys manager\n", files[i]);
283             xmlSecKeyDestroy(key);
284             xmlSecKeysMngrDestroy(mngr);
285             return(NULL);
286         }
287     }
288
289     return(mngr);
290 }
291
292 /**
293  * decrypt_file:
294  * @mngr:               the pointer to keys manager.
295  * @enc_file:           the encrypted XML  file name.
296  *
297  * Decrypts the XML file #enc_file using DES key from #key_file and 
298  * prints results to stdout.
299  *
300  * Returns 0 on success or a negative value if an error occurs.
301  */
302 int 
303 decrypt_file(xmlSecKeysMngrPtr mngr, const char* enc_file) {
304     xmlDocPtr doc = NULL;
305     xmlNodePtr node = NULL;
306     xmlSecEncCtxPtr encCtx = NULL;
307     int res = -1;
308     
309     assert(mngr);
310     assert(enc_file);
311
312     /* load template */
313     doc = xmlParseFile(enc_file);
314     if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
315         fprintf(stderr, "Error: unable to parse file \"%s\"\n", enc_file);
316         goto done;      
317     }
318     
319     /* find start node */
320     node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs);
321     if(node == NULL) {
322         fprintf(stderr, "Error: start node not found in \"%s\"\n", enc_file);
323         goto done;      
324     }
325
326     /* create encryption context */
327     encCtx = xmlSecEncCtxCreate(mngr);
328     if(encCtx == NULL) {
329         fprintf(stderr,"Error: failed to create encryption context\n");
330         goto done;
331     }
332
333     /* decrypt the data */
334     if((xmlSecEncCtxDecrypt(encCtx, node) &lt; 0) || (encCtx-&gt;result == NULL)) {
335         fprintf(stderr,"Error: decryption failed\n");
336         goto done;
337     }
338         
339     /* print decrypted data to stdout */
340     if(encCtx-&gt;resultReplaced != 0) {
341         fprintf(stdout, "Decrypted XML data:\n");
342         xmlDocDump(stdout, doc);
343     } else {
344         fprintf(stdout, "Decrypted binary data (%d bytes):\n", xmlSecBufferGetSize(encCtx-&gt;result));
345         if(xmlSecBufferGetData(encCtx-&gt;result) != NULL) {
346             fwrite(xmlSecBufferGetData(encCtx-&gt;result), 
347                   1, 
348                   xmlSecBufferGetSize(encCtx-&gt;result),
349                   stdout);
350         }
351     }
352     fprintf(stdout, "\n");
353         
354     /* success */
355     res = 0;
356
357 done:    
358     /* cleanup */
359     if(encCtx != NULL) {
360         xmlSecEncCtxDestroy(encCtx);
361     }
362     
363     if(doc != NULL) {
364         xmlFreeDoc(doc); 
365     }
366     return(res);
367 }\r</pre>
368 <p></p>
369 </div>
370 </div>
371 </div>
372 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
373 <td align="left"><a accesskey="p" href="xmlsec-decrypt-with-signle-key.html"><b>&lt;&lt;&lt; Decrypting data with a single key.</b></a></td>
374 <td align="right"><a accesskey="n" href="xmlsec-custom-keys-manager.html"><b>Writing a custom keys manager. &gt;&gt;&gt;</b></a></td>
375 </tr></table>
376 </td></tr></table></td>
377 </tr></table></body>
378 </html>