Git init
[external/xmlsec1.git] / docs / api / xmlsec-notes-verify.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Verifying a signed document</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="Verifing and decrypting documents." href="xmlsec-notes-verify-decrypt.html">
8 <link rel="PREVIOUS" title="Verifing and decrypting documents." href="xmlsec-notes-verify-decrypt.html">
9 <link rel="NEXT" title="Decrypting an encrypted document" href="xmlsec-notes-decrypt.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-notes-verify-decrypt.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
82 <td><a accesskey="u" href="xmlsec-notes-verify-decrypt.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-notes-decrypt.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-NOTES-VERIFY">Verifying a signed document</a></h1>
89 <p>The typical siganture verification process includes following steps:
90           </p>
91 <p></p>
92 <ul>
93 <li><p>         Load keys, X509 certificates, etc. in the <a href="xmlsec-keysmngr.html#XMLSECKEYSMNGR">keys manager</a> .
94             </p></li>
95 <li><p>         Create signature context <a href="xmlsec-xmldsig.html#XMLSECDSIGCTX">xmlSecDSigCtx</a>
96                 using <a href="xmlsec-xmldsig.html#XMLSECDSIGCTXCREATE">xmlSecDSigCtxCreate</a> or
97                 <a href="xmlsec-xmldsig.html#XMLSECDSIGCTXINITIALIZE">xmlSecDSigCtxInitialize</a>
98                 functions.
99             </p></li>
100 <li><p>         Select start verification 
101                 <a href="http://www.w3.org/TR/xmldsig-core/#sec-Signature" target="_top">&lt;dsig:Signature/&gt;</a>
102                 node in the signed XML document.
103             </p></li>
104 <li><p>         Verify signature by calling <a href="xmlsec-xmldsig.html#XMLSECDSIGCTXVERIFY">xmlSecDSigCtxVerify</a> 
105                 function.
106             </p></li>
107 <li><p>         Check returned value and verification status (<code class="STRUCTFIELD">status</code>
108                 member of <a href="xmlsec-xmldsig.html#XMLSECDSIGCTX">xmlSecDSigCtx</a> structure).
109                 If necessary, consume returned data from the <a href="xmlsec-xmldsig.html#XMLSECDSIGCTX">context</a>.
110             </p></li>
111 <li><p>         Destroy signature context <a href="xmlsec-xmldsig.html#XMLSECDSIGCTX">xmlSecDSigCtx</a>
112                 using <a href="xmlsec-xmldsig.html#XMLSECDSIGCTXDESTROY">xmlSecDSigCtxDestroy</a> or
113                 <a href="xmlsec-xmldsig.html#XMLSECDSIGCTXFINALIZE">xmlSecDSigCtxFinalize</a>
114                 functions.
115             </p></li>
116 </ul>
117 <p>          </p>
118 <div class="EXAMPLE">
119 <a name="AEN303"></a><p><b>Example 1. Verifying a document.</b></p>
120 <pre class="PROGRAMLISTING">/** 
121  * verify_file:
122  * @xml_file:           the signed XML file name.
123  * @key_file:           the PEM public key file name.
124  *
125  * Verifies XML signature in #xml_file using public key from #key_file.
126  *
127  * Returns 0 on success or a negative value if an error occurs.
128  */
129 int 
130 verify_file(const char* xml_file, const char* key_file) {
131     xmlDocPtr doc = NULL;
132     xmlNodePtr node = NULL;
133     xmlSecDSigCtxPtr dsigCtx = NULL;
134     int res = -1;
135     
136     assert(xml_file);
137     assert(key_file);
138
139     /* load file */
140     doc = xmlParseFile(xml_file);
141     if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
142         fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
143         goto done;      
144     }
145     
146     /* find start node */
147     node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
148     if(node == NULL) {
149         fprintf(stderr, "Error: start node not found in \"%s\"\n", xml_file);
150         goto done;      
151     }
152
153     /* create signature context, we don't need keys manager in this example */
154     dsigCtx = xmlSecDSigCtxCreate(NULL);
155     if(dsigCtx == NULL) {
156         fprintf(stderr,"Error: failed to create signature context\n");
157         goto done;
158     }
159
160     /* load public key */
161     dsigCtx-&gt;signKey = xmlSecCryptoAppKeyLoad(key_file,xmlSecKeyDataFormatPem, NULL, NULL, NULL);
162     if(dsigCtx-&gt;signKey == NULL) {
163         fprintf(stderr,"Error: failed to load public pem key from \"%s\"\n", key_file);
164         goto done;
165     }
166
167     /* set key name to the file name, this is just an example! */
168     if(xmlSecKeySetName(dsigCtx-&gt;signKey, key_file) &lt; 0) {
169         fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
170         goto done;
171     }
172
173     /* Verify signature */
174     if(xmlSecDSigCtxVerify(dsigCtx, node) &lt; 0) {
175         fprintf(stderr,"Error: signature verify\n");
176         goto done;
177     }
178         
179     /* print verification result to stdout */
180     if(dsigCtx-&gt;status == xmlSecDSigStatusSucceeded) {
181         fprintf(stdout, "Signature is OK\n");
182     } else {
183         fprintf(stdout, "Signature is INVALID\n");
184     }    
185
186     /* success */
187     res = 0;
188
189 done:    
190     /* cleanup */
191     if(dsigCtx != NULL) {
192         xmlSecDSigCtxDestroy(dsigCtx);
193     }
194     
195     if(doc != NULL) {
196         xmlFreeDoc(doc); 
197     }
198     return(res);
199 }
200                 </pre>
201 <p><a href="xmlsec-verify-with-key.html#XMLSEC-EXAMPLE-VERIFY1">Full Program Listing</a></p>
202 </div>
203         </div>
204 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
205 <td align="left"><a accesskey="p" href="xmlsec-notes-verify-decrypt.html"><b>&lt;&lt;&lt; Verifing and decrypting documents.</b></a></td>
206 <td align="right"><a accesskey="n" href="xmlsec-notes-decrypt.html"><b>Decrypting an encrypted document &gt;&gt;&gt;</b></a></td>
207 </tr></table>
208 </td></tr></table></td>
209 </tr></table></body>
210 </html>