Git init
[external/xmlsec1.git] / docs / api / xmlsec-verify-with-key.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Verifying a signature with a single key.</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 with X509 certificate." href="xmlsec-examples-sign-x509.html">
9 <link rel="NEXT" title="Verifying a signature with keys manager." href="xmlsec-verify-with-keys-mngr.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-sign-x509.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-keys-mngr.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-KEY">Verifying a signature with a single key.</a></h1>
89 <br clear="all"><div class="SECT2">
90 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-VERIFY1">verify1.c</a></h2>
91 <p></p>
92 <div class="INFORMALEXAMPLE">
93 <p></p>
94 <a name="AEN701"></a><pre class="PROGRAMLISTING">/** 
95  * XML Security Library example: Verifying a file using a single key.
96  *
97  * Verifies a file using a key from PEM file.
98  * 
99  * Usage: 
100  *      verify1 &lt;signed-file&gt; &lt;pem-key&gt; 
101  *
102  * Example:
103  *      ./verify1 sign1-res.xml rsapub.pem
104  *      ./verify1 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 int verify_file(const char* xml_file, const char* key_file);
129
130 int 
131 main(int argc, char **argv) {
132     assert(argv);
133
134     if(argc != 3) {
135         fprintf(stderr, "Error: wrong number of arguments.\n");
136         fprintf(stderr, "Usage: %s &lt;xml-file&gt; &lt;key-file&gt;\n", argv[0]);
137         return(1);
138     }
139
140     /* Init libxml and libxslt libraries */
141     xmlInitParser();
142     LIBXML_TEST_VERSION
143     xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
144     xmlSubstituteEntitiesDefault(1);
145 #ifndef XMLSEC_NO_XSLT
146     xmlIndentTreeOutput = 1; 
147 #endif /* XMLSEC_NO_XSLT */
148                 
149     /* Init xmlsec library */
150     if(xmlSecInit() &lt; 0) {
151         fprintf(stderr, "Error: xmlsec initialization failed.\n");
152         return(-1);
153     }
154
155     /* Check loaded library version */
156     if(xmlSecCheckVersion() != 1) {
157         fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
158         return(-1);
159     }
160
161     /* Load default crypto engine if we are supporting dynamic
162      * loading for xmlsec-crypto libraries. Use the crypto library
163      * name ("openssl", "nss", etc.) to load corresponding 
164      * xmlsec-crypto library.
165      */
166 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
167     if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) &lt; 0) {
168         fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
169                         "that you have it installed and check shared libraries path\n"
170                         "(LD_LIBRARY_PATH) envornment variable.\n");
171         return(-1);     
172     }
173 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
174
175     /* Init crypto library */
176     if(xmlSecCryptoAppInit(NULL) &lt; 0) {
177         fprintf(stderr, "Error: crypto initialization failed.\n");
178         return(-1);
179     }
180
181     /* Init xmlsec-crypto library */
182     if(xmlSecCryptoInit() &lt; 0) {
183         fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
184         return(-1);
185     }
186
187     if(verify_file(argv[1], argv[2]) &lt; 0) {
188         return(-1);
189     }    
190     
191     /* Shutdown xmlsec-crypto library */
192     xmlSecCryptoShutdown();
193     
194     /* Shutdown crypto library */
195     xmlSecCryptoAppShutdown();
196     
197     /* Shutdown xmlsec library */
198     xmlSecShutdown();
199
200     /* Shutdown libxslt/libxml */
201 #ifndef XMLSEC_NO_XSLT
202     xsltCleanupGlobals();            
203 #endif /* XMLSEC_NO_XSLT */
204     xmlCleanupParser();
205     
206     return(0);
207 }
208
209 /** 
210  * verify_file:
211  * @xml_file:           the signed XML file name.
212  * @key_file:           the PEM public key file name.
213  *
214  * Verifies XML signature in #xml_file using public key from #key_file.
215  *
216  * Returns 0 on success or a negative value if an error occurs.
217  */
218 int 
219 verify_file(const char* xml_file, const char* key_file) {
220     xmlDocPtr doc = NULL;
221     xmlNodePtr node = NULL;
222     xmlSecDSigCtxPtr dsigCtx = NULL;
223     int res = -1;
224     
225     assert(xml_file);
226     assert(key_file);
227
228     /* load file */
229     doc = xmlParseFile(xml_file);
230     if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
231         fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
232         goto done;      
233     }
234     
235     /* find start node */
236     node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
237     if(node == NULL) {
238         fprintf(stderr, "Error: start node not found in \"%s\"\n", xml_file);
239         goto done;      
240     }
241
242     /* create signature context, we don't need keys manager in this example */
243     dsigCtx = xmlSecDSigCtxCreate(NULL);
244     if(dsigCtx == NULL) {
245         fprintf(stderr,"Error: failed to create signature context\n");
246         goto done;
247     }
248
249     /* load public key */
250     dsigCtx-&gt;signKey = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem, NULL, NULL, NULL);
251     if(dsigCtx-&gt;signKey == NULL) {
252         fprintf(stderr,"Error: failed to load public pem key from \"%s\"\n", key_file);
253         goto done;
254     }
255
256     /* set key name to the file name, this is just an example! */
257     if(xmlSecKeySetName(dsigCtx-&gt;signKey, key_file) &lt; 0) {
258         fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
259         goto done;
260     }
261
262     /* Verify signature */
263     if(xmlSecDSigCtxVerify(dsigCtx, node) &lt; 0) {
264         fprintf(stderr,"Error: signature verify\n");
265         goto done;
266     }
267         
268     /* print verification result to stdout */
269     if(dsigCtx-&gt;status == xmlSecDSigStatusSucceeded) {
270         fprintf(stdout, "Signature is OK\n");
271     } else {
272         fprintf(stdout, "Signature is INVALID\n");
273     }    
274
275     /* success */
276     res = 0;
277
278 done:    
279     /* cleanup */
280     if(dsigCtx != NULL) {
281         xmlSecDSigCtxDestroy(dsigCtx);
282     }
283     
284     if(doc != NULL) {
285         xmlFreeDoc(doc); 
286     }
287     return(res);
288 }
289 \r</pre>
290 <p></p>
291 </div>
292 </div>
293 </div>
294 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
295 <td align="left"><a accesskey="p" href="xmlsec-examples-sign-x509.html"><b>&lt;&lt;&lt; Signing with X509 certificate.</b></a></td>
296 <td align="right"><a accesskey="n" href="xmlsec-verify-with-keys-mngr.html"><b>Verifying a signature with keys manager. &gt;&gt;&gt;</b></a></td>
297 </tr></table>
298 </td></tr></table></td>
299 </tr></table></body>
300 </html>