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 {
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-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>
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 <enc:CipherValue/>)
94 are empty. The XML Security Library sets the content of these
95 nodes after doing necessary calculations.
98 <a name="AEN256"></a><p><b>Figure 2. XML Encryption structure</b></p>
99 <pre class="PROGRAMLISTING"><enc:EncryptedData Id? Type? MimeType? Encoding?>
100 <enc:EncryptionMethod Algorithm />?
101 (<dsig:KeyInfo>
102 <dsig:KeyName>?
103 <dsig:KeyValue>?
104 <dsig:RetrievalMethod>?
105 <dsig:X509Data>?
106 <dsig:PGPData>?
107 <enc:EncryptedKey>?
108 <enc:AgreementMethod>?
109 <dsig:KeyName>?
110 <dsig:RetrievalMethod>?
112 </dsig:KeyInfo>)?
113 <enc:CipherData>
114 <enc:CipherValue>?
115 <enc:CipherReference URI?>?
116 </enc:CipherData>
117 <enc:EncryptionProperties>?
118 </enc:EncryptedData>
122 <div class="EXAMPLE">
123 <a name="AEN260"></a><p><b>Example 2. Creating dynamic encrytion template.</b></p>
124 <pre class="PROGRAMLISTING">/**
126 * @xml_file: the encryption template file name.
127 * @key_file: the Triple DES key file.
129 * Encrypts #xml_file using a dynamicaly created template and DES key from
132 * Returns 0 on success or a negative value if an error occurs.
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;
146 doc = xmlParseFile(xml_file);
147 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
148 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
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");
161 /* we want to put encrypted data in the <enc:CipherValue/> node */
162 if(xmlSecTmplEncDataEnsureCipherValue(encDataNode) == NULL) {
163 fprintf(stderr, "Error: failed to add CipherValue node\n");
167 /* add <dsig:KeyInfo/> and <dsig:KeyName/> 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");
174 if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL) == NULL) {
175 fprintf(stderr, "Error: failed to add key name\n");
179 /* create encryption context, we don't need keys manager in this example */
180 encCtx = xmlSecEncCtxCreate(NULL);
182 fprintf(stderr,"Error: failed to create encryption context\n");
186 /* load DES key, assuming that there is not password */
187 encCtx->encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
188 if(encCtx->encKey == NULL) {
189 fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
193 /* set key name to the file name, this is just an example! */
194 if(xmlSecKeySetName(encCtx->encKey, key_file) < 0) {
195 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
199 /* encrypt the data */
200 if(xmlSecEncCtxXmlEncrypt(encCtx, encDataNode, xmlDocGetRootElement(doc)) < 0) {
201 fprintf(stderr,"Error: encryption failed\n");
205 /* we template is inserted in the doc */
208 /* print encrypted data with document to stdout */
209 xmlDocDump(stdout, doc);
218 xmlSecEncCtxDestroy(encCtx);
221 if(encDataNode != NULL) {
222 xmlFreeNode(encDataNode);
231 <p><a href="xmlsec-encrypt-dynamic-template.html#XMLSEC-EXAMPLE-ENCRYPT2">Full program listing</a></p>
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><<< 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. >>></b></a></td>
238 </td></tr></table></td>