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 {
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-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>
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>
92 <div class="INFORMALEXAMPLE">
94 <a name="AEN650"></a><pre class="PROGRAMLISTING">/**
95 * XML Security Library example: Signing a template file.
97 * Signs a template file using a key from PEM file
100 * ./sign1 <xml-tmpl> <pem-key>
103 * ./sign1 sign1-tmpl.xml rsakey.pem > sign1-res.xml
105 * The result signature could be validated using verify1 example:
106 * ./verify1 sign1-res.xml rsapub.pem
108 * This is free software; see Copyright file in the source
109 * distribution for preciese wording.
111 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
113 #include <stdlib.h>
114 #include <string.h>
115 #include <assert.h>
117 #include <libxml/tree.h>
118 #include <libxml/xmlmemory.h>
119 #include <libxml/parser.h>
121 #ifndef XMLSEC_NO_XSLT
122 #include <libxslt/xslt.h>
123 #endif /* XMLSEC_NO_XSLT */
125 #include <xmlsec/xmlsec.h>
126 #include <xmlsec/xmltree.h>
127 #include <xmlsec/xmldsig.h>
128 #include <xmlsec/crypto.h>
130 int sign_file(const char* tmpl_file, const char* key_file);
133 main(int argc, char **argv) {
137 fprintf(stderr, "Error: wrong number of arguments.\n");
138 fprintf(stderr, "Usage: %s <tmpl-file> <key-file>\n", argv[0]);
142 /* Init libxml and libxslt libraries */
145 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
146 xmlSubstituteEntitiesDefault(1);
147 #ifndef XMLSEC_NO_XSLT
148 xmlIndentTreeOutput = 1;
149 #endif /* XMLSEC_NO_XSLT */
151 /* Init xmlsec library */
152 if(xmlSecInit() < 0) {
153 fprintf(stderr, "Error: xmlsec initialization failed.\n");
157 /* Check loaded library version */
158 if(xmlSecCheckVersion() != 1) {
159 fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
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.
168 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
169 if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 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");
175 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
177 /* Init crypto library */
178 if(xmlSecCryptoAppInit(NULL) < 0) {
179 fprintf(stderr, "Error: crypto initialization failed.\n");
183 /* Init xmlsec-crypto library */
184 if(xmlSecCryptoInit() < 0) {
185 fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
189 if(sign_file(argv[1], argv[2]) < 0) {
193 /* Shutdown xmlsec-crypto library */
194 xmlSecCryptoShutdown();
196 /* Shutdown crypto library */
197 xmlSecCryptoAppShutdown();
199 /* Shutdown xmlsec library */
202 /* Shutdown libxslt/libxml */
203 #ifndef XMLSEC_NO_XSLT
204 xsltCleanupGlobals();
205 #endif /* XMLSEC_NO_XSLT */
213 * @tmpl_file: the signature template file name.
214 * @key_file: the PEM private key file name.
216 * Signs the #tmpl_file using private key from #key_file.
218 * Returns 0 on success or a negative value if an error occurs.
221 sign_file(const char* tmpl_file, const char* key_file) {
222 xmlDocPtr doc = NULL;
223 xmlNodePtr node = NULL;
224 xmlSecDSigCtxPtr dsigCtx = NULL;
231 doc = xmlParseFile(tmpl_file);
232 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
233 fprintf(stderr, "Error: unable to parse file \"%s\"\n", tmpl_file);
237 /* find start node */
238 node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
240 fprintf(stderr, "Error: start node not found in \"%s\"\n", tmpl_file);
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");
251 /* load private key, assuming that there is not password */
252 dsigCtx->signKey = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem, NULL, NULL, NULL);
253 if(dsigCtx->signKey == NULL) {
254 fprintf(stderr,"Error: failed to load private pem key from \"%s\"\n", key_file);
258 /* set key name to the file name, this is just an example! */
259 if(xmlSecKeySetName(dsigCtx->signKey, key_file) < 0) {
260 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
264 /* sign the template */
265 if(xmlSecDSigCtxSign(dsigCtx, node) < 0) {
266 fprintf(stderr,"Error: signature failed\n");
270 /* print signed document to stdout */
271 xmlDocDump(stdout, doc);
278 if(dsigCtx != NULL) {
279 xmlSecDSigCtxDestroy(dsigCtx);
290 <br clear="all"><div class="SECT2">
291 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN1-TMPL">sign1-tmpl.xml</a></h2>
293 <div class="INFORMALEXAMPLE">
295 <a name="AEN655"></a><pre class="PROGRAMLISTING"><?xml version="1.0" encoding="UTF-8"?>
297 XML Security Library example: Simple signature template file for sign1 example.
299 <Envelope xmlns="urn:envelope">
303 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
305 <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
306 <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
307 <Reference URI="">
309 <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
311 <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
312 <DigestValue></DigestValue>
315 <SignatureValue/>
320 </Envelope>
\r</pre>
324 <br clear="all"><div class="SECT2">
325 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN1-RES">sign1-res.xml</a></h2>
327 <div class="INFORMALEXAMPLE">
329 <a name="AEN660"></a><pre class="PROGRAMLISTING"><?xml version="1.0" encoding="UTF-8"?>
331 XML Security Library example: Signed file (sign1 example).
333 <Envelope xmlns="urn:envelope">
337 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
339 <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
340 <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
341 <Reference URI="">
343 <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
345 <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
346 <DigestValue>9H/rQr2Axe9hYTV2n/tCp+3UIQQ=</DigestValue>
349 <SignatureValue>Mx4psIy9/UY+u8QBJRDrwQWKRaCGz0WOVftyDzAe6WHAFSjMNr7qb2ojq9kdipT8
350 Oub5q2OQ7mzdSLiiejkrO1VeqM/90yEIGI4En6KEB6ArEzw+iq4N1wm6EptcyxXx
351 M9StAOOa9ilWYqR9Tfx3SW1urUIuKYgUitxsONiUHBVaW6HeX51bsXoTF++4ZI+D
352 jiPBjN4HHmr0cbJ6BXk91S27ffZIfp1Qj5nL9onFLUGbR6EFgu2luiRzQbPuM2tP
353 XxyI7GZ8AfHnRJK28ARvBC9oi+O1ej20S79CIV7gdBxbLbFprozBHAwOEC57YgJc
354 x+YEjSjcO7SBIR1FiUA7pw==</SignatureValue>
356 <KeyName>rsakey.pem</KeyName>
359 </Envelope></pre>
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><<< Examples.</b></a></td>
366 <td align="right"><a accesskey="n" href="xmlsec-examples-sign-dynamimc-template.html"><b>Signing a dynamicaly created template. >>></b></a></td>
368 </td></tr></table></td>