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 {
12 border: solid 1px #aaaaaa;
17 border: solid 1px #aaaaff;
26 border: solid 1px #ffaaaa;
33 .navigation a:visited {
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>
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>
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>
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>
66 <td><a href="http://xmlsoft.org/"><img src="../images/libxml2-logo.png" alt="LibXML2" border="0"></a></td>
70 <td><a href="http://xmlsoft.org/XSLT"><img src="../images/libxslt-logo.png" alt="LibXSLT" border="0"></a></td>
74 <td><a href="http://www.openssl.org/"><img src="../images/openssl-logo.png" alt="OpenSSL" border="0"></a></td>
76 <!--Links - start--><!--Links - end-->
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>
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>
92 <div class="INFORMALEXAMPLE">
94 <a name="AEN807"></a><pre class="PROGRAMLISTING">/**
95 * XML Security Library example: Decrypting an encrypted file using keys manager.
97 * Decrypts encrypted XML file using keys manager and a list of
98 * DES key from a binary file
101 * ./decrypt2 <xml-enc> <des-key-file1> [<des-key-file2> [...]]
104 * ./decrypt2 encrypt1-res.xml deskey.bin
105 * ./decrypt2 encrypt2-res.xml deskey.bin
107 * This is free software; see Copyright file in the source
108 * distribution for preciese wording.
110 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
112 #include <stdlib.h>
113 #include <string.h>
114 #include <assert.h>
116 #include <libxml/tree.h>
117 #include <libxml/xmlmemory.h>
118 #include <libxml/parser.h>
120 #ifndef XMLSEC_NO_XSLT
121 #include <libxslt/xslt.h>
122 #endif /* XMLSEC_NO_XSLT */
124 #include <xmlsec/xmlsec.h>
125 #include <xmlsec/xmltree.h>
126 #include <xmlsec/xmlenc.h>
127 #include <xmlsec/crypto.h>
129 xmlSecKeysMngrPtr load_des_keys(char** files, int files_size);
130 int decrypt_file(xmlSecKeysMngrPtr mngr, const char* enc_file);
133 main(int argc, char **argv) {
134 xmlSecKeysMngrPtr mngr;
139 fprintf(stderr, "Error: wrong number of arguments.\n");
140 fprintf(stderr, "Usage: %s <enc-file> <key-file1> [<key-file2> [...]]\n", argv[0]);
144 /* Init libxml and libxslt libraries */
147 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
148 xmlSubstituteEntitiesDefault(1);
149 #ifndef XMLSEC_NO_XSLT
150 xmlIndentTreeOutput = 1;
151 #endif /* XMLSEC_NO_XSLT */
153 /* Init xmlsec library */
154 if(xmlSecInit() < 0) {
155 fprintf(stderr, "Error: xmlsec initialization failed.\n");
159 /* Check loaded library version */
160 if(xmlSecCheckVersion() != 1) {
161 fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
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.
170 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
171 if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 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");
177 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
179 /* Init crypto library */
180 if(xmlSecCryptoAppInit(NULL) < 0) {
181 fprintf(stderr, "Error: crypto initialization failed.\n");
185 /* Init xmlsec-crypto library */
186 if(xmlSecCryptoInit() < 0) {
187 fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
191 /* create keys manager and load keys */
192 mngr = load_des_keys(&(argv[2]), argc - 2);
197 if(decrypt_file(mngr, argv[1]) < 0) {
198 xmlSecKeysMngrDestroy(mngr);
202 /* destroy keys manager */
203 xmlSecKeysMngrDestroy(mngr);
205 /* Shutdown xmlsec-crypto library */
206 xmlSecCryptoShutdown();
208 /* Shutdown crypto library */
209 xmlSecCryptoAppShutdown();
211 /* Shutdown xmlsec library */
214 /* Shutdown libxslt/libxml */
215 #ifndef XMLSEC_NO_XSLT
216 xsltCleanupGlobals();
217 #endif /* XMLSEC_NO_XSLT */
225 * @files: the list of filenames.
226 * @files_size: the number of filenames in #files.
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.
232 * Returns the pointer to newly created keys manager or NULL if an error
236 load_des_keys(char** files, int files_size) {
237 xmlSecKeysMngrPtr mngr;
242 assert(files_size > 0);
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
248 mngr = xmlSecKeysMngrCreate();
250 fprintf(stderr, "Error: failed to create keys manager.\n");
253 if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) {
254 fprintf(stderr, "Error: failed to initialize keys manager.\n");
255 xmlSecKeysMngrDestroy(mngr);
259 for(i = 0; i < files_size; ++i) {
263 key = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, files[i]);
265 fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", files[i]);
266 xmlSecKeysMngrDestroy(mngr);
270 /* set key name to the file name, this is just an example! */
271 if(xmlSecKeySetName(key, BAD_CAST files[i]) < 0) {
272 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", files[i]);
273 xmlSecKeyDestroy(key);
274 xmlSecKeysMngrDestroy(mngr);
278 /* add key to keys manager, from now on keys manager is responsible
281 if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key) < 0) {
282 fprintf(stderr,"Error: failed to add key from \"%s\" to keys manager\n", files[i]);
283 xmlSecKeyDestroy(key);
284 xmlSecKeysMngrDestroy(mngr);
294 * @mngr: the pointer to keys manager.
295 * @enc_file: the encrypted XML file name.
297 * Decrypts the XML file #enc_file using DES key from #key_file and
298 * prints results to stdout.
300 * Returns 0 on success or a negative value if an error occurs.
303 decrypt_file(xmlSecKeysMngrPtr mngr, const char* enc_file) {
304 xmlDocPtr doc = NULL;
305 xmlNodePtr node = NULL;
306 xmlSecEncCtxPtr encCtx = NULL;
313 doc = xmlParseFile(enc_file);
314 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
315 fprintf(stderr, "Error: unable to parse file \"%s\"\n", enc_file);
319 /* find start node */
320 node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs);
322 fprintf(stderr, "Error: start node not found in \"%s\"\n", enc_file);
326 /* create encryption context */
327 encCtx = xmlSecEncCtxCreate(mngr);
329 fprintf(stderr,"Error: failed to create encryption context\n");
333 /* decrypt the data */
334 if((xmlSecEncCtxDecrypt(encCtx, node) < 0) || (encCtx->result == NULL)) {
335 fprintf(stderr,"Error: decryption failed\n");
339 /* print decrypted data to stdout */
340 if(encCtx->resultReplaced != 0) {
341 fprintf(stdout, "Decrypted XML data:\n");
342 xmlDocDump(stdout, doc);
344 fprintf(stdout, "Decrypted binary data (%d bytes):\n", xmlSecBufferGetSize(encCtx->result));
345 if(xmlSecBufferGetData(encCtx->result) != NULL) {
346 fwrite(xmlSecBufferGetData(encCtx->result),
348 xmlSecBufferGetSize(encCtx->result),
352 fprintf(stdout, "\n");
360 xmlSecEncCtxDestroy(encCtx);
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><<< 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. >>></b></a></td>
376 </td></tr></table></td>