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 {
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-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>
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>
92 <div class="INFORMALEXAMPLE">
94 <a name="AEN708"></a><pre class="PROGRAMLISTING">/**
95 * XML Security Library example: Verifying a file using keys manager.
97 * Verifies a file using keys manager
100 * verify2 <signed-file> <public-pem-key1> [<public-pem-key2> [...]]
103 * ./verify2 sign1-res.xml rsapub.pem
104 * ./verify2 sign2-res.xml rsapub.pem
106 * This is free software; see Copyright file in the source
107 * distribution for preciese wording.
109 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
111 #include <stdlib.h>
112 #include <string.h>
113 #include <assert.h>
115 #include <libxml/tree.h>
116 #include <libxml/xmlmemory.h>
117 #include <libxml/parser.h>
119 #ifndef XMLSEC_NO_XSLT
120 #include <libxslt/xslt.h>
121 #endif /* XMLSEC_NO_XSLT */
123 #include <xmlsec/xmlsec.h>
124 #include <xmlsec/xmltree.h>
125 #include <xmlsec/xmldsig.h>
126 #include <xmlsec/crypto.h>
128 xmlSecKeysMngrPtr load_keys(char** files, int files_size);
129 int verify_file(xmlSecKeysMngrPtr mngr, const char* xml_file);
132 main(int argc, char **argv) {
133 xmlSecKeysMngrPtr mngr;
138 fprintf(stderr, "Error: wrong number of arguments.\n");
139 fprintf(stderr, "Usage: %s <xml-file> <key-file1> [<key-file2> [...]]\n", argv[0]);
143 /* Init libxml and libxslt libraries */
146 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
147 xmlSubstituteEntitiesDefault(1);
148 #ifndef XMLSEC_NO_XSLT
149 xmlIndentTreeOutput = 1;
150 #endif /* XMLSEC_NO_XSLT */
152 /* Init xmlsec library */
153 if(xmlSecInit() < 0) {
154 fprintf(stderr, "Error: xmlsec initialization failed.\n");
158 /* Check loaded library version */
159 if(xmlSecCheckVersion() != 1) {
160 fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
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.
169 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
170 if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 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");
176 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
178 /* Init crypto library */
179 if(xmlSecCryptoAppInit(NULL) < 0) {
180 fprintf(stderr, "Error: crypto initialization failed.\n");
184 /* Init xmlsec-crypto library */
185 if(xmlSecCryptoInit() < 0) {
186 fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
190 /* create keys manager and load keys */
191 mngr = load_keys(&(argv[2]), argc - 2);
197 if(verify_file(mngr, argv[1]) < 0) {
198 xmlSecKeysMngrDestroy(mngr);
202 /* destroy keys manager */
203 xmlSecKeysMngrDestroy(mngr);
205 /* Shutdown xmlsec-crypto library */
206 xmlSecCryptoShutdown();
208 /* Shutdown crypto library */
209 xmlSecCryptoAppShutdown();
211 /* Shutdown xmlsec library */
214 /* Shutdown libxslt/libxml */
215 #ifndef XMLSEC_NO_XSLT
216 xsltCleanupGlobals();
217 #endif /* XMLSEC_NO_XSLT */
225 * @files: the list of filenames.
226 * @files_size: the number of filenames in #files.
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.
232 * Returns the pointer to newly created keys manager or NULL if an error
236 load_keys(char** files, int files_size) {
237 xmlSecKeysMngrPtr mngr;
242 assert(files_size > 0);
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
248 mngr = xmlSecKeysMngrCreate();
250 fprintf(stderr, "Error: failed to create keys manager.\n");
253 if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) {
254 fprintf(stderr, "Error: failed to initialize keys manager.\n");
255 xmlSecKeysMngrDestroy(mngr);
259 for(i = 0; i < files_size; ++i) {
263 key = xmlSecCryptoAppKeyLoad(files[i], xmlSecKeyDataFormatPem, NULL, NULL, NULL);
265 fprintf(stderr,"Error: failed to load pem key from \"%s\"\n", files[i]);
266 xmlSecKeysMngrDestroy(mngr);
270 /* set key name to the file name, this is just an example! */
271 if(xmlSecKeySetName(key, BAD_CAST files[i]) < 0) {
272 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", files[i]);
273 xmlSecKeyDestroy(key);
274 xmlSecKeysMngrDestroy(mngr);
278 /* add key to keys manager, from now on keys manager is responsible
281 if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key) < 0) {
282 fprintf(stderr,"Error: failed to add key from \"%s\" to keys manager\n", files[i]);
283 xmlSecKeyDestroy(key);
284 xmlSecKeysMngrDestroy(mngr);
294 * @mngr: the pointer to keys manager.
295 * @xml_file: the signed XML file name.
297 * Verifies XML signature in #xml_file.
299 * Returns 0 on success or a negative value if an error occurs.
302 verify_file(xmlSecKeysMngrPtr mngr, const char* xml_file) {
303 xmlDocPtr doc = NULL;
304 xmlNodePtr node = NULL;
305 xmlSecDSigCtxPtr dsigCtx = NULL;
312 doc = xmlParseFile(xml_file);
313 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
314 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
318 /* find start node */
319 node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
321 fprintf(stderr, "Error: start node not found in \"%s\"\n", xml_file);
325 /* create signature context */
326 dsigCtx = xmlSecDSigCtxCreate(mngr);
327 if(dsigCtx == NULL) {
328 fprintf(stderr,"Error: failed to create signature context\n");
332 /* Verify signature */
333 if(xmlSecDSigCtxVerify(dsigCtx, node) < 0) {
334 fprintf(stderr,"Error: signature verify\n");
338 /* print verification result to stdout */
339 if(dsigCtx->status == xmlSecDSigStatusSucceeded) {
340 fprintf(stdout, "Signature is OK\n");
342 fprintf(stdout, "Signature is INVALID\n");
350 if(dsigCtx != NULL) {
351 xmlSecDSigCtxDestroy(dsigCtx);
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><<< 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. >>></b></a></td>
368 </td></tr></table></td>