Git init
[external/xmlsec1.git] / docs / api / xmlsec-verify-with-keys-mngr.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Verifying a signature with keys manager.</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="Verifying a signature with a single key." href="xmlsec-verify-with-key.html">
9 <link rel="NEXT" title="Verifying a signature with X509 certificates." href="xmlsec-verify-with-x509.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-verify-with-key.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-verify-with-x509.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-VERIFY-WITH-KEYS-MNGR">Verifying a signature with keys manager.</a></h1>
89 <br clear="all"><div class="SECT2">
90 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-VERIFY2">verify2.c</a></h2>
91 <p></p>
92 <div class="INFORMALEXAMPLE">
93 <p></p>
94 <a name="AEN708"></a><pre class="PROGRAMLISTING">/** 
95  * XML Security Library example: Verifying a file using keys manager.
96  *
97  * Verifies a file using keys manager
98  * 
99  * Usage: 
100  *      verify2 &lt;signed-file&gt; &lt;public-pem-key1&gt; [&lt;public-pem-key2&gt; [...]]
101  *
102  * Example:
103  *      ./verify2 sign1-res.xml rsapub.pem
104  *      ./verify2 sign2-res.xml rsapub.pem
105  * 
106  * This is free software; see Copyright file in the source
107  * distribution for preciese wording.
108  * 
109  * Copyright (C) 2002-2003 Aleksey Sanin &lt;aleksey@aleksey.com&gt;
110  */
111 #include &lt;stdlib.h&gt;
112 #include &lt;string.h&gt;
113 #include &lt;assert.h&gt;
114
115 #include &lt;libxml/tree.h&gt;
116 #include &lt;libxml/xmlmemory.h&gt;
117 #include &lt;libxml/parser.h&gt;
118
119 #ifndef XMLSEC_NO_XSLT
120 #include &lt;libxslt/xslt.h&gt;
121 #endif /* XMLSEC_NO_XSLT */
122
123 #include &lt;xmlsec/xmlsec.h&gt;
124 #include &lt;xmlsec/xmltree.h&gt;
125 #include &lt;xmlsec/xmldsig.h&gt;
126 #include &lt;xmlsec/crypto.h&gt;
127
128 xmlSecKeysMngrPtr load_keys(char** files, int files_size);
129 int verify_file(xmlSecKeysMngrPtr mngr, const char* xml_file);
130
131 int 
132 main(int argc, char **argv) {
133     xmlSecKeysMngrPtr mngr;
134     
135     assert(argv);
136
137     if(argc &lt; 3) {
138         fprintf(stderr, "Error: wrong number of arguments.\n");
139         fprintf(stderr, "Usage: %s &lt;xml-file&gt; &lt;key-file1&gt; [&lt;key-file2&gt; [...]]\n", argv[0]);
140         return(1);
141     }
142
143     /* Init libxml and libxslt libraries */
144     xmlInitParser();
145     LIBXML_TEST_VERSION
146     xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
147     xmlSubstituteEntitiesDefault(1);
148 #ifndef XMLSEC_NO_XSLT
149     xmlIndentTreeOutput = 1; 
150 #endif /* XMLSEC_NO_XSLT */
151                 
152     /* Init xmlsec library */
153     if(xmlSecInit() &lt; 0) {
154         fprintf(stderr, "Error: xmlsec initialization failed.\n");
155         return(-1);
156     }
157
158     /* Check loaded library version */
159     if(xmlSecCheckVersion() != 1) {
160         fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
161         return(-1);
162     }
163
164     /* Load default crypto engine if we are supporting dynamic
165      * loading for xmlsec-crypto libraries. Use the crypto library
166      * name ("openssl", "nss", etc.) to load corresponding 
167      * xmlsec-crypto library.
168      */
169 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
170     if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) &lt; 0) {
171         fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
172                         "that you have it installed and check shared libraries path\n"
173                         "(LD_LIBRARY_PATH) envornment variable.\n");
174         return(-1);     
175     }
176 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
177
178     /* Init crypto library */
179     if(xmlSecCryptoAppInit(NULL) &lt; 0) {
180         fprintf(stderr, "Error: crypto initialization failed.\n");
181         return(-1);
182     }
183
184     /* Init xmlsec-crypto library */
185     if(xmlSecCryptoInit() &lt; 0) {
186         fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
187         return(-1);
188     }
189
190     /* create keys manager and load keys */
191     mngr = load_keys(&amp;(argv[2]), argc - 2);
192     if(mngr == NULL) {
193         return(-1);
194     }
195     
196     /* verify file */
197     if(verify_file(mngr, argv[1]) &lt; 0) {
198         xmlSecKeysMngrDestroy(mngr);    
199         return(-1);
200     }    
201     
202     /* destroy keys manager */
203     xmlSecKeysMngrDestroy(mngr);
204     
205     /* Shutdown xmlsec-crypto library */
206     xmlSecCryptoShutdown();
207     
208     /* Shutdown crypto library */
209     xmlSecCryptoAppShutdown();
210     
211     /* Shutdown xmlsec library */
212     xmlSecShutdown();
213
214     /* Shutdown libxslt/libxml */
215 #ifndef XMLSEC_NO_XSLT
216     xsltCleanupGlobals();            
217 #endif /* XMLSEC_NO_XSLT */
218     xmlCleanupParser();
219     
220     return(0);
221 }
222
223 /**
224  * load_keys:
225  * @files:              the list of filenames.
226  * @files_size:         the number of filenames in #files.
227  *
228  * Creates simple keys manager and load PEM keys from #files in it.
229  * The caller is responsible for destroing returned keys manager using
230  * @xmlSecKeysMngrDestroy.
231  *
232  * Returns the pointer to newly created keys manager or NULL if an error
233  * occurs.
234  */
235 xmlSecKeysMngrPtr 
236 load_keys(char** files, int files_size) {
237     xmlSecKeysMngrPtr mngr;
238     xmlSecKeyPtr key;
239     int i;
240     
241     assert(files);
242     assert(files_size &gt; 0);
243     
244     /* create and initialize keys manager, we use a simple list based
245      * keys manager, implement your own xmlSecKeysStore klass if you need
246      * something more sophisticated 
247      */
248     mngr = xmlSecKeysMngrCreate();
249     if(mngr == NULL) {
250         fprintf(stderr, "Error: failed to create keys manager.\n");
251         return(NULL);
252     }
253     if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) &lt; 0) {
254         fprintf(stderr, "Error: failed to initialize keys manager.\n");
255         xmlSecKeysMngrDestroy(mngr);
256         return(NULL);
257     }    
258     
259     for(i = 0; i &lt; files_size; ++i) {
260         assert(files[i]);
261
262         /* load key */
263         key = xmlSecCryptoAppKeyLoad(files[i], xmlSecKeyDataFormatPem, NULL, NULL, NULL);
264         if(key == NULL) {
265             fprintf(stderr,"Error: failed to load pem key from \"%s\"\n", files[i]);
266             xmlSecKeysMngrDestroy(mngr);
267             return(NULL);
268         }
269
270         /* set key name to the file name, this is just an example! */
271         if(xmlSecKeySetName(key, BAD_CAST files[i]) &lt; 0) {
272             fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", files[i]);
273             xmlSecKeyDestroy(key);
274             xmlSecKeysMngrDestroy(mngr);
275             return(NULL);
276         }
277         
278         /* add key to keys manager, from now on keys manager is responsible 
279          * for destroying key 
280          */
281         if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key) &lt; 0) {
282             fprintf(stderr,"Error: failed to add key from \"%s\" to keys manager\n", files[i]);
283             xmlSecKeyDestroy(key);
284             xmlSecKeysMngrDestroy(mngr);
285             return(NULL);
286         }
287     }
288
289     return(mngr);
290 }
291
292 /** 
293  * verify_file:
294  * @mngr:               the pointer to keys manager.
295  * @xml_file:           the signed XML file name.
296  *
297  * Verifies XML signature in #xml_file.
298  *
299  * Returns 0 on success or a negative value if an error occurs.
300  */
301 int 
302 verify_file(xmlSecKeysMngrPtr mngr, const char* xml_file) {
303     xmlDocPtr doc = NULL;
304     xmlNodePtr node = NULL;
305     xmlSecDSigCtxPtr dsigCtx = NULL;
306     int res = -1;
307     
308     assert(mngr);
309     assert(xml_file);
310
311     /* load file */
312     doc = xmlParseFile(xml_file);
313     if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
314         fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
315         goto done;      
316     }
317     
318     /* find start node */
319     node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
320     if(node == NULL) {
321         fprintf(stderr, "Error: start node not found in \"%s\"\n", xml_file);
322         goto done;      
323     }
324
325     /* create signature context */
326     dsigCtx = xmlSecDSigCtxCreate(mngr);
327     if(dsigCtx == NULL) {
328         fprintf(stderr,"Error: failed to create signature context\n");
329         goto done;
330     }
331
332     /* Verify signature */
333     if(xmlSecDSigCtxVerify(dsigCtx, node) &lt; 0) {
334         fprintf(stderr,"Error: signature verify\n");
335         goto done;
336     }
337         
338     /* print verification result to stdout */
339     if(dsigCtx-&gt;status == xmlSecDSigStatusSucceeded) {
340         fprintf(stdout, "Signature is OK\n");
341     } else {
342         fprintf(stdout, "Signature is INVALID\n");
343     }    
344
345     /* success */
346     res = 0;
347
348 done:    
349     /* cleanup */
350     if(dsigCtx != NULL) {
351         xmlSecDSigCtxDestroy(dsigCtx);
352     }
353     
354     if(doc != NULL) {
355         xmlFreeDoc(doc); 
356     }
357     return(res);
358 }
359 \r</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-verify-with-key.html"><b>&lt;&lt;&lt; Verifying a signature with a single key.</b></a></td>
366 <td align="right"><a accesskey="n" href="xmlsec-verify-with-x509.html"><b>Verifying a signature with X509 certificates. &gt;&gt;&gt;</b></a></td>
367 </tr></table>
368 </td></tr></table></td>
369 </tr></table></body>
370 </html>