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 {
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-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>
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>
92 <div class="INFORMALEXAMPLE">
94 <a name="AEN766"></a><pre class="PROGRAMLISTING">/**
95 * XML Security Library example: Encrypting XML file with a dynamicaly created template.
97 * Encrypts XML file using a dynamicaly created template file and a DES key
101 * ./encrypt2 <xml-doc> <des-key-file>
104 * ./encrypt2 encrypt2-doc.xml deskey.bin > encrypt2-res.xml
106 * The result could be decrypted with decrypt1 example:
107 * ./decrypt1 encrypt2-res.xml deskey.bin
109 * This is free software; see Copyright file in the source
110 * distribution for preciese wording.
112 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
114 #include <stdlib.h>
115 #include <string.h>
116 #include <assert.h>
118 #include <libxml/tree.h>
119 #include <libxml/xmlmemory.h>
120 #include <libxml/parser.h>
122 #ifndef XMLSEC_NO_XSLT
123 #include <libxslt/xslt.h>
124 #endif /* XMLSEC_NO_XSLT */
126 #include <xmlsec/xmlsec.h>
127 #include <xmlsec/xmltree.h>
128 #include <xmlsec/xmlenc.h>
129 #include <xmlsec/templates.h>
130 #include <xmlsec/crypto.h>
132 int encrypt_file(const char* xml_file, const char* key_file);
135 main(int argc, char **argv) {
139 fprintf(stderr, "Error: wrong number of arguments.\n");
140 fprintf(stderr, "Usage: %s <xml-file> <key-file>\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 if(encrypt_file(argv[1], argv[2]) < 0) {
195 /* Shutdown xmlsec-crypto library */
196 xmlSecCryptoShutdown();
198 /* Shutdown crypto library */
199 xmlSecCryptoAppShutdown();
201 /* Shutdown xmlsec library */
204 /* Shutdown libxslt/libxml */
205 #ifndef XMLSEC_NO_XSLT
206 xsltCleanupGlobals();
207 #endif /* XMLSEC_NO_XSLT */
215 * @xml_file: the encryption template file name.
216 * @key_file: the Triple DES key file.
218 * Encrypts #xml_file using a dynamicaly created template and DES key from
221 * Returns 0 on success or a negative value if an error occurs.
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;
235 doc = xmlParseFile(xml_file);
236 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
237 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
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");
250 /* we want to put encrypted data in the <enc:CipherValue/> node */
251 if(xmlSecTmplEncDataEnsureCipherValue(encDataNode) == NULL) {
252 fprintf(stderr, "Error: failed to add CipherValue node\n");
256 /* add <dsig:KeyInfo/> and <dsig:KeyName/> 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");
263 if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL) == NULL) {
264 fprintf(stderr, "Error: failed to add key name\n");
268 /* create encryption context, we don't need keys manager in this example */
269 encCtx = xmlSecEncCtxCreate(NULL);
271 fprintf(stderr,"Error: failed to create encryption context\n");
275 /* load DES key, assuming that there is not password */
276 encCtx->encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
277 if(encCtx->encKey == NULL) {
278 fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
282 /* set key name to the file name, this is just an example! */
283 if(xmlSecKeySetName(encCtx->encKey, key_file) < 0) {
284 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
288 /* encrypt the data */
289 if(xmlSecEncCtxXmlEncrypt(encCtx, encDataNode, xmlDocGetRootElement(doc)) < 0) {
290 fprintf(stderr,"Error: encryption failed\n");
294 /* we template is inserted in the doc */
297 /* print encrypted data with document to stdout */
298 xmlDocDump(stdout, doc);
307 xmlSecEncCtxDestroy(encCtx);
310 if(encDataNode != NULL) {
311 xmlFreeNode(encDataNode);
322 <br clear="all"><div class="SECT2">
323 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-ENCRYPT2-DOC">encrypt2-doc.xml</a></h2>
325 <div class="INFORMALEXAMPLE">
327 <a name="AEN771"></a><pre class="PROGRAMLISTING"><?xml version="1.0" encoding="UTF-8"?>
329 XML Security Library example: Original XML doc file before encryption (encrypt2 example).
331 <Envelope xmlns="urn:envelope">
335 </Envelope></pre>
339 <br clear="all"><div class="SECT2">
340 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-ENCRYPT2-RES">encrypt2-res.xml</a></h2>
342 <div class="INFORMALEXAMPLE">
344 <a name="AEN776"></a><pre class="PROGRAMLISTING"><?xml version="1.0" encoding="UTF-8"?>
346 XML Security Library example: Encrypted XML file (encrypt2 example).
348 <EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#" Type="http://www.w3.org/2001/04/xmlenc#Element">
349 <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
350 <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
351 <KeyName>deskey.bin</KeyName>
354 <CipherValue>WXlDyktaADlUe+PywKwS3KdKlahCteEKxi/hRlHcXNQlGwNGrYKy8aQ6dLtX1bKg
355 IgL/XoAQN3B27zD91b1ZLGh6QQ9CjnVD98+hYJ9TPp4piPnII4vGUA==</CipherValue>
357 </EncryptedData></pre>
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><<< 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. >>></b></a></td>
366 </td></tr></table></td>