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 {
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-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>
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:
93 <li><p> Load keys, X509 certificates, etc. in the <a href="xmlsec-keysmngr.html#XMLSECKEYSMNGR">keys manager</a> .
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>
100 <li><p> Select start verification
101 <a href="http://www.w3.org/TR/xmldsig-core/#sec-Signature" target="_top"><dsig:Signature/></a>
102 node in the signed XML document.
104 <li><p> Verify signature by calling <a href="xmlsec-xmldsig.html#XMLSECDSIGCTXVERIFY">xmlSecDSigCtxVerify</a>
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>.
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>
118 <div class="EXAMPLE">
119 <a name="AEN303"></a><p><b>Example 1. Verifying a document.</b></p>
120 <pre class="PROGRAMLISTING">/**
122 * @xml_file: the signed XML file name.
123 * @key_file: the PEM public key file name.
125 * Verifies XML signature in #xml_file using public key from #key_file.
127 * Returns 0 on success or a negative value if an error occurs.
130 verify_file(const char* xml_file, const char* key_file) {
131 xmlDocPtr doc = NULL;
132 xmlNodePtr node = NULL;
133 xmlSecDSigCtxPtr dsigCtx = NULL;
140 doc = xmlParseFile(xml_file);
141 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
142 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
146 /* find start node */
147 node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
149 fprintf(stderr, "Error: start node not found in \"%s\"\n", xml_file);
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");
160 /* load public key */
161 dsigCtx->signKey = xmlSecCryptoAppKeyLoad(key_file,xmlSecKeyDataFormatPem, NULL, NULL, NULL);
162 if(dsigCtx->signKey == NULL) {
163 fprintf(stderr,"Error: failed to load public pem key from \"%s\"\n", key_file);
167 /* set key name to the file name, this is just an example! */
168 if(xmlSecKeySetName(dsigCtx->signKey, key_file) < 0) {
169 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
173 /* Verify signature */
174 if(xmlSecDSigCtxVerify(dsigCtx, node) < 0) {
175 fprintf(stderr,"Error: signature verify\n");
179 /* print verification result to stdout */
180 if(dsigCtx->status == xmlSecDSigStatusSucceeded) {
181 fprintf(stdout, "Signature is OK\n");
183 fprintf(stdout, "Signature is INVALID\n");
191 if(dsigCtx != NULL) {
192 xmlSecDSigCtxDestroy(dsigCtx);
201 <p><a href="xmlsec-verify-with-key.html#XMLSEC-EXAMPLE-VERIFY1">Full Program Listing</a></p>
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><<< Verifing and decrypting documents.</b></a></td>
206 <td align="right"><a accesskey="n" href="xmlsec-notes-decrypt.html"><b>Decrypting an encrypted document >>></b></a></td>
208 </td></tr></table></td>