3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Signing 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="Signing a template file." href="xmlsec-examples-sign-template-file.html">
9 <link rel="NEXT" title="Signing with X509 certificate." href="xmlsec-examples-sign-x509.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-sign-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-examples-sign-x509.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-DYNAMIMC-TEMPLATE">Signing a dynamicaly created template.</a></h1>
89 <br clear="all"><div class="SECT2">
90 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN2">sign2.c</a></h2>
92 <div class="INFORMALEXAMPLE">
94 <a name="AEN667"></a><pre class="PROGRAMLISTING">/**
95 * XML Security Library example: Signing a file with a dynamicaly created template.
97 * Signs a file using a dynamicaly created template and key from PEM file.
98 * The signature has one reference with one enveloped transform to sign
99 * the whole document except the <dsig:Signature/> node itself.
102 * sign2 <xml-doc> <pem-key>
105 * ./sign2 sign2-doc.xml rsakey.pem > sign2-res.xml
107 * The result signature could be validated using verify1 example:
108 * ./verify1 sign2-res.xml rsapub.pem
110 * This is free software; see Copyright file in the source
111 * distribution for preciese wording.
113 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
115 #include <stdlib.h>
116 #include <string.h>
117 #include <assert.h>
119 #include <libxml/tree.h>
120 #include <libxml/xmlmemory.h>
121 #include <libxml/parser.h>
123 #ifndef XMLSEC_NO_XSLT
124 #include <libxslt/xslt.h>
125 #endif /* XMLSEC_NO_XSLT */
127 #include <xmlsec/xmlsec.h>
128 #include <xmlsec/xmltree.h>
129 #include <xmlsec/xmldsig.h>
130 #include <xmlsec/templates.h>
131 #include <xmlsec/crypto.h>
133 int sign_file(const char* xml_file, const char* key_file);
136 main(int argc, char **argv) {
140 fprintf(stderr, "Error: wrong number of arguments.\n");
141 fprintf(stderr, "Usage: %s <xml-file> <key-file>\n", argv[0]);
145 /* Init libxml and libxslt libraries */
148 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
149 xmlSubstituteEntitiesDefault(1);
150 #ifndef XMLSEC_NO_XSLT
151 xmlIndentTreeOutput = 1;
152 #endif /* XMLSEC_NO_XSLT */
154 /* Init xmlsec library */
155 if(xmlSecInit() < 0) {
156 fprintf(stderr, "Error: xmlsec initialization failed.\n");
160 /* Check loaded library version */
161 if(xmlSecCheckVersion() != 1) {
162 fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
166 /* Load default crypto engine if we are supporting dynamic
167 * loading for xmlsec-crypto libraries. Use the crypto library
168 * name ("openssl", "nss", etc.) to load corresponding
169 * xmlsec-crypto library.
171 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
172 if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
173 fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
174 "that you have it installed and check shared libraries path\n"
175 "(LD_LIBRARY_PATH) envornment variable.\n");
178 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
180 /* Init crypto library */
181 if(xmlSecCryptoAppInit(NULL) < 0) {
182 fprintf(stderr, "Error: crypto initialization failed.\n");
186 /* Init xmlsec-crypto library */
187 if(xmlSecCryptoInit() < 0) {
188 fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
192 if(sign_file(argv[1], argv[2]) < 0) {
196 /* Shutdown xmlsec-crypto library */
197 xmlSecCryptoShutdown();
199 /* Shutdown crypto library */
200 xmlSecCryptoAppShutdown();
202 /* Shutdown xmlsec library */
205 /* Shutdown libxslt/libxml */
206 #ifndef XMLSEC_NO_XSLT
207 xsltCleanupGlobals();
208 #endif /* XMLSEC_NO_XSLT */
216 * @xml_file: the XML file name.
217 * @key_file: the PEM private key file name.
219 * Signs the #xml_file using private key from #key_file and dynamicaly
220 * created enveloped signature template.
222 * Returns 0 on success or a negative value if an error occurs.
225 sign_file(const char* xml_file, const char* key_file) {
226 xmlDocPtr doc = NULL;
227 xmlNodePtr signNode = NULL;
228 xmlNodePtr refNode = NULL;
229 xmlNodePtr keyInfoNode = NULL;
230 xmlSecDSigCtxPtr dsigCtx = NULL;
237 doc = xmlParseFile(xml_file);
238 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
239 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
243 /* create signature template for RSA-SHA1 enveloped signature */
244 signNode = xmlSecTmplSignatureCreate(doc, xmlSecTransformExclC14NId,
245 xmlSecTransformRsaSha1Id, NULL);
246 if(signNode == NULL) {
247 fprintf(stderr, "Error: failed to create signature template\n");
251 /* add <dsig:Signature/> node to the doc */
252 xmlAddChild(xmlDocGetRootElement(doc), signNode);
255 refNode = xmlSecTmplSignatureAddReference(signNode, xmlSecTransformSha1Id,
257 if(refNode == NULL) {
258 fprintf(stderr, "Error: failed to add reference to signature template\n");
262 /* add enveloped transform */
263 if(xmlSecTmplReferenceAddTransform(refNode, xmlSecTransformEnvelopedId) == NULL) {
264 fprintf(stderr, "Error: failed to add enveloped transform to reference\n");
268 /* add <dsig:KeyInfo/> and <dsig:KeyName/> nodes to put key name in the signed document */
269 keyInfoNode = xmlSecTmplSignatureEnsureKeyInfo(signNode, NULL);
270 if(keyInfoNode == NULL) {
271 fprintf(stderr, "Error: failed to add key info\n");
275 if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL) == NULL) {
276 fprintf(stderr, "Error: failed to add key name\n");
280 /* create signature context, we don't need keys manager in this example */
281 dsigCtx = xmlSecDSigCtxCreate(NULL);
282 if(dsigCtx == NULL) {
283 fprintf(stderr,"Error: failed to create signature context\n");
287 /* load private key, assuming that there is not password */
288 dsigCtx->signKey = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem, NULL, NULL, NULL);
289 if(dsigCtx->signKey == NULL) {
290 fprintf(stderr,"Error: failed to load private pem key from \"%s\"\n", key_file);
294 /* set key name to the file name, this is just an example! */
295 if(xmlSecKeySetName(dsigCtx->signKey, key_file) < 0) {
296 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
300 /* sign the template */
301 if(xmlSecDSigCtxSign(dsigCtx, signNode) < 0) {
302 fprintf(stderr,"Error: signature failed\n");
306 /* print signed document to stdout */
307 xmlDocDump(stdout, doc);
314 if(dsigCtx != NULL) {
315 xmlSecDSigCtxDestroy(dsigCtx);
326 <br clear="all"><div class="SECT2">
327 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN2-DOC">sign2-doc.xml</a></h2>
329 <div class="INFORMALEXAMPLE">
331 <a name="AEN672"></a><pre class="PROGRAMLISTING"><?xml version="1.0" encoding="UTF-8"?>
333 XML Security Library example: Original XML doc file for sign2 example.
335 <Envelope xmlns="urn:envelope">
339 </Envelope></pre>
343 <br clear="all"><div class="SECT2">
344 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN2-RES">sign2-res.xml</a></h2>
346 <div class="INFORMALEXAMPLE">
348 <a name="AEN677"></a><pre class="PROGRAMLISTING"><?xml version="1.0" encoding="UTF-8"?>
350 XML Security Library example: Signed XML doc file (sign2 example).
352 <Envelope xmlns="urn:envelope">
356 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
358 <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
359 <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
362 <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
364 <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
365 <DigestValue>HjY8ilZAIEM2tBbPn5mYO1ieIX4=</DigestValue>
368 <SignatureValue>SIaj/6KY3C1SmDXU2++Gm31U1xTadFp04WhBgfsJFbxrL+q7GKSKN9kfQ+UpN9+i
369 D5fWmuavXEHe4Gw6RMaMEkq2URQo7F68+d5J/ajq8/l4n+xE6/reGScVwT6L4dEP
370 XXVJcAi2ZnQ3O7GTNvNGCPibL9mUcyCWBFZ92Uemtc/vJFCQ7ZyKMdMfACgxOwyN
371 T/9971oog241/2doudhonc0I/3mgPYWkZdX6yvr62mEjnG+oUZkhWYJ4ewZJ4hM4
372 JjbFqZO+OEzDRSbw3DkmuBA/mtlx+3t13SESfEub5hqoMdVmtth/eTb64dsPdl9r
373 3k1ACVX9f8aHfQQdJOmLFQ==</SignatureValue>
375 <KeyName>rsakey.pem</KeyName>
377 </Signature></Envelope></pre>
382 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
383 <td align="left"><a accesskey="p" href="xmlsec-examples-sign-template-file.html"><b><<< Signing a template file.</b></a></td>
384 <td align="right"><a accesskey="n" href="xmlsec-examples-sign-x509.html"><b>Signing with X509 certificate. >>></b></a></td>
386 </td></tr></table></td>