3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Signing with X509 certificate.</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 a dynamicaly created template." href="xmlsec-examples-sign-dynamimc-template.html">
9 <link rel="NEXT" title="Verifying a signature with a single key." href="xmlsec-verify-with-key.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-examples-sign-dynamimc-template.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-key.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-EXAMPLES-SIGN-X509">Signing with X509 certificate.</a></h1>
89 <br clear="all"><div class="SECT2">
90 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN3">sign3.c</a></h2>
92 <div class="INFORMALEXAMPLE">
94 <a name="AEN684"></a><pre class="PROGRAMLISTING">/**
95 * XML Security Library example: Signing a file with a dynamicaly created template and an X509 certificate.
97 * Signs a file using a dynamicaly created template, key from PEM file and
98 * an X509 certificate. The signature has one reference with one enveloped
99 * transform to sign the whole document except the <dsig:Signature/> node
100 * itself. The key certificate is written in the <dsig:X509Data/> node.
102 * This example was developed and tested with OpenSSL crypto library. The
103 * certificates management policies for another crypto library may break it.
106 * sign3 <xml-doc> <pem-key>
109 * ./sign3 sign3-doc.xml rsakey.pem rsacert.pem > sign3-res.xml
111 * The result signature could be validated using verify3 example:
112 * ./verify3 sign3-res.xml rootcert.pem
114 * This is free software; see Copyright file in the source
115 * distribution for preciese wording.
117 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
119 #include <stdlib.h>
120 #include <string.h>
121 #include <assert.h>
123 #include <libxml/tree.h>
124 #include <libxml/xmlmemory.h>
125 #include <libxml/parser.h>
127 #ifndef XMLSEC_NO_XSLT
128 #include <libxslt/xslt.h>
129 #endif /* XMLSEC_NO_XSLT */
131 #include <xmlsec/xmlsec.h>
132 #include <xmlsec/xmltree.h>
133 #include <xmlsec/xmldsig.h>
134 #include <xmlsec/templates.h>
135 #include <xmlsec/crypto.h>
137 int sign_file(const char* xml_file, const char* key_file, const char* cert_file);
140 main(int argc, char **argv) {
144 fprintf(stderr, "Error: wrong number of arguments.\n");
145 fprintf(stderr, "Usage: %s <xml-file> <key-file> <cert-file>\n", argv[0]);
149 /* Init libxml and libxslt libraries */
152 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
153 xmlSubstituteEntitiesDefault(1);
154 #ifndef XMLSEC_NO_XSLT
155 xmlIndentTreeOutput = 1;
156 #endif /* XMLSEC_NO_XSLT */
158 /* Init xmlsec library */
159 if(xmlSecInit() < 0) {
160 fprintf(stderr, "Error: xmlsec initialization failed.\n");
164 /* Check loaded library version */
165 if(xmlSecCheckVersion() != 1) {
166 fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
170 /* Load default crypto engine if we are supporting dynamic
171 * loading for xmlsec-crypto libraries. Use the crypto library
172 * name ("openssl", "nss", etc.) to load corresponding
173 * xmlsec-crypto library.
175 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
176 if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
177 fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
178 "that you have it installed and check shared libraries path\n"
179 "(LD_LIBRARY_PATH) envornment variable.\n");
182 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
184 /* Init crypto library */
185 if(xmlSecCryptoAppInit(NULL) < 0) {
186 fprintf(stderr, "Error: crypto initialization failed.\n");
190 /* Init xmlsec-crypto library */
191 if(xmlSecCryptoInit() < 0) {
192 fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
196 if(sign_file(argv[1], argv[2], argv[3]) < 0) {
200 /* Shutdown xmlsec-crypto library */
201 xmlSecCryptoShutdown();
203 /* Shutdown crypto library */
204 xmlSecCryptoAppShutdown();
206 /* Shutdown xmlsec library */
209 /* Shutdown libxslt/libxml */
210 #ifndef XMLSEC_NO_XSLT
211 xsltCleanupGlobals();
212 #endif /* XMLSEC_NO_XSLT */
220 * @xml_file: the XML file name.
221 * @key_file: the PEM private key file name.
222 * @cert_file: the x509 certificate PEM file.
224 * Signs the @xml_file using private key from @key_file and dynamicaly
225 * created enveloped signature template. The certificate from @cert_file
226 * is placed in the <dsig:X509Data/> node.
228 * Returns 0 on success or a negative value if an error occurs.
231 sign_file(const char* xml_file, const char* key_file, const char* cert_file) {
232 xmlDocPtr doc = NULL;
233 xmlNodePtr signNode = NULL;
234 xmlNodePtr refNode = NULL;
235 xmlNodePtr keyInfoNode = NULL;
236 xmlSecDSigCtxPtr dsigCtx = NULL;
244 doc = xmlParseFile(xml_file);
245 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
246 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
250 /* create signature template for RSA-SHA1 enveloped signature */
251 signNode = xmlSecTmplSignatureCreate(doc, xmlSecTransformExclC14NId,
252 xmlSecTransformRsaSha1Id, NULL);
253 if(signNode == NULL) {
254 fprintf(stderr, "Error: failed to create signature template\n");
258 /* add <dsig:Signature/> node to the doc */
259 xmlAddChild(xmlDocGetRootElement(doc), signNode);
262 refNode = xmlSecTmplSignatureAddReference(signNode, xmlSecTransformSha1Id,
264 if(refNode == NULL) {
265 fprintf(stderr, "Error: failed to add reference to signature template\n");
269 /* add enveloped transform */
270 if(xmlSecTmplReferenceAddTransform(refNode, xmlSecTransformEnvelopedId) == NULL) {
271 fprintf(stderr, "Error: failed to add enveloped transform to reference\n");
275 /* add <dsig:KeyInfo/> and <dsig:X509Data/> */
276 keyInfoNode = xmlSecTmplSignatureEnsureKeyInfo(signNode, NULL);
277 if(keyInfoNode == NULL) {
278 fprintf(stderr, "Error: failed to add key info\n");
282 if(xmlSecTmplKeyInfoAddX509Data(keyInfoNode) == NULL) {
283 fprintf(stderr, "Error: failed to add X509Data node\n");
287 /* create signature context, we don't need keys manager in this example */
288 dsigCtx = xmlSecDSigCtxCreate(NULL);
289 if(dsigCtx == NULL) {
290 fprintf(stderr,"Error: failed to create signature context\n");
294 /* load private key, assuming that there is not password */
295 dsigCtx->signKey = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem, NULL, NULL, NULL);
296 if(dsigCtx->signKey == NULL) {
297 fprintf(stderr,"Error: failed to load private pem key from \"%s\"\n", key_file);
301 /* load certificate and add to the key */
302 if(xmlSecCryptoAppKeyCertLoad(dsigCtx->signKey, cert_file, xmlSecKeyDataFormatPem) < 0) {
303 fprintf(stderr,"Error: failed to load pem certificate \"%s\"\n", cert_file);
307 /* set key name to the file name, this is just an example! */
308 if(xmlSecKeySetName(dsigCtx->signKey, key_file) < 0) {
309 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
313 /* sign the template */
314 if(xmlSecDSigCtxSign(dsigCtx, signNode) < 0) {
315 fprintf(stderr,"Error: signature failed\n");
319 /* print signed document to stdout */
320 xmlDocDump(stdout, doc);
327 if(dsigCtx != NULL) {
328 xmlSecDSigCtxDestroy(dsigCtx);
339 <br clear="all"><div class="SECT2">
340 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN3-DOC">sign3-doc.xml</a></h2>
342 <div class="INFORMALEXAMPLE">
344 <a name="AEN689"></a><pre class="PROGRAMLISTING"><?xml version="1.0" encoding="UTF-8"?>
346 XML Security Library example: Original XML doc file for sign3 example.
348 <Envelope xmlns="urn:envelope">
352 </Envelope></pre>
356 <br clear="all"><div class="SECT2">
357 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-SIGN3-RES">sign3-res.xml</a></h2>
359 <div class="INFORMALEXAMPLE">
361 <a name="AEN694"></a><pre class="PROGRAMLISTING"><?xml version="1.0" encoding="UTF-8"?>
363 XML Security Library example: Signed XML doc file (sign3 example).
365 <Envelope xmlns="urn:envelope">
369 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
371 <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
372 <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
375 <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
377 <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
378 <DigestValue>HjY8ilZAIEM2tBbPn5mYO1ieIX4=</DigestValue>
381 <SignatureValue>SIaj/6KY3C1SmDXU2++Gm31U1xTadFp04WhBgfsJFbxrL+q7GKSKN9kfQ+UpN9+i
382 D5fWmuavXEHe4Gw6RMaMEkq2URQo7F68+d5J/ajq8/l4n+xE6/reGScVwT6L4dEP
383 XXVJcAi2ZnQ3O7GTNvNGCPibL9mUcyCWBFZ92Uemtc/vJFCQ7ZyKMdMfACgxOwyN
384 T/9971oog241/2doudhonc0I/3mgPYWkZdX6yvr62mEjnG+oUZkhWYJ4ewZJ4hM4
385 JjbFqZO+OEzDRSbw3DkmuBA/mtlx+3t13SESfEub5hqoMdVmtth/eTb64dsPdl9r
386 3k1ACVX9f8aHfQQdJOmLFQ==</SignatureValue>
389 <X509Certificate>MIIE3zCCBEigAwIBAgIBBTANBgkqhkiG9w0BAQQFADCByzELMAkGA1UEBhMCVVMx
390 EzARBgNVBAgTCkNhbGlmb3JuaWExEjAQBgNVBAcTCVN1bm55dmFsZTE9MDsGA1UE
391 ChM0WE1MIFNlY3VyaXR5IExpYnJhcnkgKGh0dHA6Ly93d3cuYWxla3NleS5jb20v
392 eG1sc2VjKTEZMBcGA1UECxMQUm9vdCBDZXJ0aWZpY2F0ZTEWMBQGA1UEAxMNQWxl
393 a3NleSBTYW5pbjEhMB8GCSqGSIb3DQEJARYSeG1sc2VjQ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Gh0dHA6Ly93d3cuYWxla3NleS5jb20v
410 eG1sc2VjKTEZMBcGA1UECxMQUm9vdCBDZXJ0aWZpY2F0ZTEWMBQGA1UEAxMNQWxl
411 a3NleSBTYW5pbjEhMB8GCSqGSIb3DQEJARYSeG1sc2VjQGFsZWtzZXkuY29tggEA
412 MA0GCSqGSIb3DQEBBAUAA4GBALU/mzIxSv8vhDuomxFcplzwdlLZbvSQrfoNkMGY
413 1UoS3YJrN+jZLWKSyWE3mIaPpElqXiXQGGkwD5iPQ1iJMbI7BeLvx6ZxX/f+c8Wn
414 ss0uc1NxfahMaBoyG15IL4+beqO182fosaKJTrJNG3mc//ANGU9OsQM9mfBEt4oL
415 NJ2D</X509Certificate>
418 </Signature></Envelope></pre>
423 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
424 <td align="left"><a accesskey="p" href="xmlsec-examples-sign-dynamimc-template.html"><b><<< Signing a dynamicaly created template.</b></a></td>
425 <td align="right"><a accesskey="n" href="xmlsec-verify-with-key.html"><b>Verifying a signature with a single key. >>></b></a></td>
427 </td></tr></table></td>