2 * XML Security Library example: Signing a file with a dynamicaly created template and an X509 certificate.
4 * Signs a file using a dynamicaly created template, key from PEM file and
5 * an X509 certificate. The signature has one reference with one enveloped
6 * transform to sign the whole document except the <dsig:Signature/> node
7 * itself. The key certificate is written in the <dsig:X509Data/> node.
9 * This example was developed and tested with OpenSSL crypto library. The
10 * certificates management policies for another crypto library may break it.
13 * sign3 <xml-doc> <pem-key>
16 * ./sign3 sign3-doc.xml rsakey.pem rsacert.pem > sign3-res.xml
18 * The result signature could be validated using verify3 example:
19 * ./verify3 sign3-res.xml rootcert.pem
21 * This is free software; see Copyright file in the source
22 * distribution for preciese wording.
24 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
30 #include <libxml/tree.h>
31 #include <libxml/xmlmemory.h>
32 #include <libxml/parser.h>
34 #ifndef XMLSEC_NO_XSLT
35 #include <libxslt/xslt.h>
36 #endif /* XMLSEC_NO_XSLT */
38 #include <xmlsec/xmlsec.h>
39 #include <xmlsec/xmltree.h>
40 #include <xmlsec/xmldsig.h>
41 #include <xmlsec/templates.h>
42 #include <xmlsec/crypto.h>
44 int sign_file(const char* xml_file, const char* key_file, const char* cert_file);
47 main(int argc, char **argv) {
51 fprintf(stderr, "Error: wrong number of arguments.\n");
52 fprintf(stderr, "Usage: %s <xml-file> <key-file> <cert-file>\n", argv[0]);
56 /* Init libxml and libxslt libraries */
59 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
60 xmlSubstituteEntitiesDefault(1);
61 #ifndef XMLSEC_NO_XSLT
62 xmlIndentTreeOutput = 1;
63 #endif /* XMLSEC_NO_XSLT */
65 /* Init xmlsec library */
66 if(xmlSecInit() < 0) {
67 fprintf(stderr, "Error: xmlsec initialization failed.\n");
71 /* Check loaded library version */
72 if(xmlSecCheckVersion() != 1) {
73 fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
77 /* Load default crypto engine if we are supporting dynamic
78 * loading for xmlsec-crypto libraries. Use the crypto library
79 * name ("openssl", "nss", etc.) to load corresponding
80 * xmlsec-crypto library.
82 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
83 if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
84 fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
85 "that you have it installed and check shared libraries path\n"
86 "(LD_LIBRARY_PATH) envornment variable.\n");
89 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
91 /* Init crypto library */
92 if(xmlSecCryptoAppInit(NULL) < 0) {
93 fprintf(stderr, "Error: crypto initialization failed.\n");
97 /* Init xmlsec-crypto library */
98 if(xmlSecCryptoInit() < 0) {
99 fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
103 if(sign_file(argv[1], argv[2], argv[3]) < 0) {
107 /* Shutdown xmlsec-crypto library */
108 xmlSecCryptoShutdown();
110 /* Shutdown crypto library */
111 xmlSecCryptoAppShutdown();
113 /* Shutdown xmlsec library */
116 /* Shutdown libxslt/libxml */
117 #ifndef XMLSEC_NO_XSLT
118 xsltCleanupGlobals();
119 #endif /* XMLSEC_NO_XSLT */
127 * @xml_file: the XML file name.
128 * @key_file: the PEM private key file name.
129 * @cert_file: the x509 certificate PEM file.
131 * Signs the @xml_file using private key from @key_file and dynamicaly
132 * created enveloped signature template. The certificate from @cert_file
133 * is placed in the <dsig:X509Data/> node.
135 * Returns 0 on success or a negative value if an error occurs.
138 sign_file(const char* xml_file, const char* key_file, const char* cert_file) {
139 xmlDocPtr doc = NULL;
140 xmlNodePtr signNode = NULL;
141 xmlNodePtr refNode = NULL;
142 xmlNodePtr keyInfoNode = NULL;
143 xmlSecDSigCtxPtr dsigCtx = NULL;
151 doc = xmlParseFile(xml_file);
152 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
153 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
157 /* create signature template for RSA-SHA1 enveloped signature */
158 signNode = xmlSecTmplSignatureCreate(doc, xmlSecTransformExclC14NId,
159 xmlSecTransformRsaSha1Id, NULL);
160 if(signNode == NULL) {
161 fprintf(stderr, "Error: failed to create signature template\n");
165 /* add <dsig:Signature/> node to the doc */
166 xmlAddChild(xmlDocGetRootElement(doc), signNode);
169 refNode = xmlSecTmplSignatureAddReference(signNode, xmlSecTransformSha1Id,
171 if(refNode == NULL) {
172 fprintf(stderr, "Error: failed to add reference to signature template\n");
176 /* add enveloped transform */
177 if(xmlSecTmplReferenceAddTransform(refNode, xmlSecTransformEnvelopedId) == NULL) {
178 fprintf(stderr, "Error: failed to add enveloped transform to reference\n");
182 /* add <dsig:KeyInfo/> and <dsig:X509Data/> */
183 keyInfoNode = xmlSecTmplSignatureEnsureKeyInfo(signNode, NULL);
184 if(keyInfoNode == NULL) {
185 fprintf(stderr, "Error: failed to add key info\n");
189 if(xmlSecTmplKeyInfoAddX509Data(keyInfoNode) == NULL) {
190 fprintf(stderr, "Error: failed to add X509Data node\n");
194 /* create signature context, we don't need keys manager in this example */
195 dsigCtx = xmlSecDSigCtxCreate(NULL);
196 if(dsigCtx == NULL) {
197 fprintf(stderr,"Error: failed to create signature context\n");
201 /* load private key, assuming that there is not password */
202 dsigCtx->signKey = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem, NULL, NULL, NULL);
203 if(dsigCtx->signKey == NULL) {
204 fprintf(stderr,"Error: failed to load private pem key from \"%s\"\n", key_file);
208 /* load certificate and add to the key */
209 if(xmlSecCryptoAppKeyCertLoad(dsigCtx->signKey, cert_file, xmlSecKeyDataFormatPem) < 0) {
210 fprintf(stderr,"Error: failed to load pem certificate \"%s\"\n", cert_file);
214 /* set key name to the file name, this is just an example! */
215 if(xmlSecKeySetName(dsigCtx->signKey, key_file) < 0) {
216 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
220 /* sign the template */
221 if(xmlSecDSigCtxSign(dsigCtx, signNode) < 0) {
222 fprintf(stderr,"Error: signature failed\n");
226 /* print signed document to stdout */
227 xmlDocDump(stdout, doc);
234 if(dsigCtx != NULL) {
235 xmlSecDSigCtxDestroy(dsigCtx);