Git init
[external/xmlsec1.git] / docs / api / xmlsec-examples-sign-template-file.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Signing a template file.</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="Examples." href="xmlsec-examples.html">
9 <link rel="NEXT" title="Signing a dynamicaly created template." href="xmlsec-examples-sign-dynamimc-template.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-examples.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-examples-sign-dynamimc-template.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-EXAMPLES-SIGN-TEMPLATE-FILE">Signing a template file.</a></h1>
89 <br clear="all"><div class="SECT2">
90 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN1">sign1.c</a></h2>
91 <p></p>
92 <div class="INFORMALEXAMPLE">
93 <p></p>
94 <a name="AEN650"></a><pre class="PROGRAMLISTING">/** 
95  * XML Security Library example: Signing a template file.
96  * 
97  * Signs a template file using a key from PEM file
98  * 
99  * Usage: 
100  *      ./sign1 &lt;xml-tmpl&gt; &lt;pem-key&gt; 
101  *
102  * Example:
103  *      ./sign1 sign1-tmpl.xml rsakey.pem &gt; sign1-res.xml
104  *
105  * The result signature could be validated using verify1 example:
106  *      ./verify1 sign1-res.xml rsapub.pem
107  *
108  * This is free software; see Copyright file in the source
109  * distribution for preciese wording.
110  * 
111  * Copyright (C) 2002-2003 Aleksey Sanin &lt;aleksey@aleksey.com&gt;
112  */
113 #include &lt;stdlib.h&gt;
114 #include &lt;string.h&gt;
115 #include &lt;assert.h&gt;
116
117 #include &lt;libxml/tree.h&gt;
118 #include &lt;libxml/xmlmemory.h&gt;
119 #include &lt;libxml/parser.h&gt;
120
121 #ifndef XMLSEC_NO_XSLT
122 #include &lt;libxslt/xslt.h&gt;
123 #endif /* XMLSEC_NO_XSLT */
124
125 #include &lt;xmlsec/xmlsec.h&gt;
126 #include &lt;xmlsec/xmltree.h&gt;
127 #include &lt;xmlsec/xmldsig.h&gt;
128 #include &lt;xmlsec/crypto.h&gt;
129
130 int sign_file(const char* tmpl_file, const char* key_file);
131
132 int 
133 main(int argc, char **argv) {
134     assert(argv);
135
136     if(argc != 3) {
137         fprintf(stderr, "Error: wrong number of arguments.\n");
138         fprintf(stderr, "Usage: %s &lt;tmpl-file&gt; &lt;key-file&gt;\n", argv[0]);
139         return(1);
140     }
141
142     /* Init libxml and libxslt libraries */
143     xmlInitParser();
144     LIBXML_TEST_VERSION
145     xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
146     xmlSubstituteEntitiesDefault(1);
147 #ifndef XMLSEC_NO_XSLT
148     xmlIndentTreeOutput = 1; 
149 #endif /* XMLSEC_NO_XSLT */
150                 
151     /* Init xmlsec library */
152     if(xmlSecInit() &lt; 0) {
153         fprintf(stderr, "Error: xmlsec initialization failed.\n");
154         return(-1);
155     }
156
157     /* Check loaded library version */
158     if(xmlSecCheckVersion() != 1) {
159         fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
160         return(-1);
161     }
162
163     /* Load default crypto engine if we are supporting dynamic
164      * loading for xmlsec-crypto libraries. Use the crypto library
165      * name ("openssl", "nss", etc.) to load corresponding 
166      * xmlsec-crypto library.
167      */
168 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
169     if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) &lt; 0) {
170         fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
171                         "that you have it installed and check shared libraries path\n"
172                         "(LD_LIBRARY_PATH) envornment variable.\n");
173         return(-1);     
174     }
175 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
176
177     /* Init crypto library */
178     if(xmlSecCryptoAppInit(NULL) &lt; 0) {
179         fprintf(stderr, "Error: crypto initialization failed.\n");
180         return(-1);
181     }
182
183     /* Init xmlsec-crypto library */
184     if(xmlSecCryptoInit() &lt; 0) {
185         fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
186         return(-1);
187     }
188
189     if(sign_file(argv[1], argv[2]) &lt; 0) {
190         return(-1);
191     }    
192     
193     /* Shutdown xmlsec-crypto library */
194     xmlSecCryptoShutdown();
195     
196     /* Shutdown crypto library */
197     xmlSecCryptoAppShutdown();
198     
199     /* Shutdown xmlsec library */
200     xmlSecShutdown();
201
202     /* Shutdown libxslt/libxml */
203 #ifndef XMLSEC_NO_XSLT
204     xsltCleanupGlobals();            
205 #endif /* XMLSEC_NO_XSLT */
206     xmlCleanupParser();
207     
208     return(0);
209 }
210
211 /** 
212  * sign_file:
213  * @tmpl_file:          the signature template file name.
214  * @key_file:           the PEM private key file name.
215  *
216  * Signs the #tmpl_file using private key from #key_file.
217  *
218  * Returns 0 on success or a negative value if an error occurs.
219  */
220 int 
221 sign_file(const char* tmpl_file, const char* key_file) {
222     xmlDocPtr doc = NULL;
223     xmlNodePtr node = NULL;
224     xmlSecDSigCtxPtr dsigCtx = NULL;
225     int res = -1;
226     
227     assert(tmpl_file);
228     assert(key_file);
229
230     /* load template */
231     doc = xmlParseFile(tmpl_file);
232     if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
233         fprintf(stderr, "Error: unable to parse file \"%s\"\n", tmpl_file);
234         goto done;      
235     }
236     
237     /* find start node */
238     node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
239     if(node == NULL) {
240         fprintf(stderr, "Error: start node not found in \"%s\"\n", tmpl_file);
241         goto done;      
242     }
243
244     /* create signature context, we don't need keys manager in this example */
245     dsigCtx = xmlSecDSigCtxCreate(NULL);
246     if(dsigCtx == NULL) {
247         fprintf(stderr,"Error: failed to create signature context\n");
248         goto done;
249     }
250
251     /* load private key, assuming that there is not password */
252     dsigCtx-&gt;signKey = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem, NULL, NULL, NULL);
253     if(dsigCtx-&gt;signKey == NULL) {
254         fprintf(stderr,"Error: failed to load private pem key from \"%s\"\n", key_file);
255         goto done;
256     }
257
258     /* set key name to the file name, this is just an example! */
259     if(xmlSecKeySetName(dsigCtx-&gt;signKey, key_file) &lt; 0) {
260         fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
261         goto done;
262     }
263
264     /* sign the template */
265     if(xmlSecDSigCtxSign(dsigCtx, node) &lt; 0) {
266         fprintf(stderr,"Error: signature failed\n");
267         goto done;
268     }
269         
270     /* print signed document to stdout */
271     xmlDocDump(stdout, doc);
272     
273     /* success */
274     res = 0;
275
276 done:    
277     /* cleanup */
278     if(dsigCtx != NULL) {
279         xmlSecDSigCtxDestroy(dsigCtx);
280     }
281     
282     if(doc != NULL) {
283         xmlFreeDoc(doc); 
284     }
285     return(res);
286 }\r</pre>
287 <p></p>
288 </div>
289 </div>
290 <br clear="all"><div class="SECT2">
291 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN1-TMPL">sign1-tmpl.xml</a></h2>
292 <p></p>
293 <div class="INFORMALEXAMPLE">
294 <p></p>
295 <a name="AEN655"></a><pre class="PROGRAMLISTING">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
296 &lt;!-- 
297 XML Security Library example: Simple signature template file for sign1 example. 
298 --&gt;
299 &lt;Envelope xmlns="urn:envelope"&gt;
300   &lt;Data&gt;
301         Hello, World!
302   &lt;/Data&gt;
303   &lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
304     &lt;SignedInfo&gt;
305       &lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /&gt;
306       &lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /&gt;
307       &lt;Reference URI=""&gt;
308         &lt;Transforms&gt;
309           &lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /&gt;
310         &lt;/Transforms&gt;
311         &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /&gt;
312         &lt;DigestValue&gt;&lt;/DigestValue&gt;
313       &lt;/Reference&gt;
314     &lt;/SignedInfo&gt;
315     &lt;SignatureValue/&gt;
316     &lt;KeyInfo&gt;
317         &lt;KeyName/&gt;
318     &lt;/KeyInfo&gt;
319   &lt;/Signature&gt;
320 &lt;/Envelope&gt;\r</pre>
321 <p></p>
322 </div>
323 </div>
324 <br clear="all"><div class="SECT2">
325 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN1-RES">sign1-res.xml</a></h2>
326 <p></p>
327 <div class="INFORMALEXAMPLE">
328 <p></p>
329 <a name="AEN660"></a><pre class="PROGRAMLISTING">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
330 &lt;!-- 
331 XML Security Library example: Signed file (sign1 example). 
332 --&gt;
333 &lt;Envelope xmlns="urn:envelope"&gt;
334   &lt;Data&gt;
335         Hello, World!
336   &lt;/Data&gt;
337   &lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
338     &lt;SignedInfo&gt;
339       &lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/&gt;
340       &lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/&gt;
341       &lt;Reference URI=""&gt;
342         &lt;Transforms&gt;
343           &lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/&gt;
344         &lt;/Transforms&gt;
345         &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
346         &lt;DigestValue&gt;9H/rQr2Axe9hYTV2n/tCp+3UIQQ=&lt;/DigestValue&gt;
347       &lt;/Reference&gt;
348     &lt;/SignedInfo&gt;
349     &lt;SignatureValue&gt;Mx4psIy9/UY+u8QBJRDrwQWKRaCGz0WOVftyDzAe6WHAFSjMNr7qb2ojq9kdipT8
350 Oub5q2OQ7mzdSLiiejkrO1VeqM/90yEIGI4En6KEB6ArEzw+iq4N1wm6EptcyxXx
351 M9StAOOa9ilWYqR9Tfx3SW1urUIuKYgUitxsONiUHBVaW6HeX51bsXoTF++4ZI+D
352 jiPBjN4HHmr0cbJ6BXk91S27ffZIfp1Qj5nL9onFLUGbR6EFgu2luiRzQbPuM2tP
353 XxyI7GZ8AfHnRJK28ARvBC9oi+O1ej20S79CIV7gdBxbLbFprozBHAwOEC57YgJc
354 x+YEjSjcO7SBIR1FiUA7pw==&lt;/SignatureValue&gt;
355     &lt;KeyInfo&gt;
356         &lt;KeyName&gt;rsakey.pem&lt;/KeyName&gt;
357     &lt;/KeyInfo&gt;
358   &lt;/Signature&gt;
359 &lt;/Envelope&gt;</pre>
360 <p></p>
361 </div>
362 </div>
363 </div>
364 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
365 <td align="left"><a accesskey="p" href="xmlsec-examples.html"><b>&lt;&lt;&lt; Examples.</b></a></td>
366 <td align="right"><a accesskey="n" href="xmlsec-examples-sign-dynamimc-template.html"><b>Signing a dynamicaly created template. &gt;&gt;&gt;</b></a></td>
367 </tr></table>
368 </td></tr></table></td>
369 </tr></table></body>
370 </html>