2 * XML Security Library example: Encrypting data using a template file.
4 * Encrypts binary data using a template file and a DES key from a binary file
7 * ./encrypt1 <xml-tmpl> <des-key-file>
10 * ./encrypt1 encrypt1-tmpl.xml deskey.bin > encrypt1-res.xml
12 * The result could be decrypted with decrypt1 example:
13 * ./decrypt1 encrypt1-res.xml deskey.bin
15 * This is free software; see Copyright file in the source
16 * distribution for preciese wording.
18 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
24 #include <libxml/tree.h>
25 #include <libxml/xmlmemory.h>
26 #include <libxml/parser.h>
28 #ifndef XMLSEC_NO_XSLT
29 #include <libxslt/xslt.h>
30 #endif /* XMLSEC_NO_XSLT */
32 #include <xmlsec/xmlsec.h>
33 #include <xmlsec/xmltree.h>
34 #include <xmlsec/xmlenc.h>
35 #include <xmlsec/crypto.h>
37 int encrypt_file(const char* tmpl_file, const char* key_file,
38 const unsigned char* data, size_t dataSize);
40 main(int argc, char **argv) {
41 static const char secret_data[] = "Big secret";
46 fprintf(stderr, "Error: wrong number of arguments.\n");
47 fprintf(stderr, "Usage: %s <tmpl-file> <key-file>\n", argv[0]);
51 /* Init libxml and libxslt libraries */
54 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
55 xmlSubstituteEntitiesDefault(1);
56 #ifndef XMLSEC_NO_XSLT
57 xmlIndentTreeOutput = 1;
58 #endif /* XMLSEC_NO_XSLT */
60 /* Init xmlsec library */
61 if(xmlSecInit() < 0) {
62 fprintf(stderr, "Error: xmlsec initialization failed.\n");
66 /* Check loaded library version */
67 if(xmlSecCheckVersion() != 1) {
68 fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
72 /* Load default crypto engine if we are supporting dynamic
73 * loading for xmlsec-crypto libraries. Use the crypto library
74 * name ("openssl", "nss", etc.) to load corresponding
75 * xmlsec-crypto library.
77 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
78 if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
79 fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
80 "that you have it installed and check shared libraries path\n"
81 "(LD_LIBRARY_PATH) envornment variable.\n");
84 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
86 /* Init crypto library */
87 if(xmlSecCryptoAppInit(NULL) < 0) {
88 fprintf(stderr, "Error: crypto initialization failed.\n");
92 /* Init xmlsec-crypto library */
93 if(xmlSecCryptoInit() < 0) {
94 fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
98 if(encrypt_file(argv[1], argv[2], secret_data, strlen(secret_data)) < 0) {
102 /* Shutdown xmlsec-crypto library */
103 xmlSecCryptoShutdown();
105 /* Shutdown crypto library */
106 xmlSecCryptoAppShutdown();
108 /* Shutdown xmlsec library */
111 /* Shutdown libxslt/libxml */
112 #ifndef XMLSEC_NO_XSLT
113 xsltCleanupGlobals();
114 #endif /* XMLSEC_NO_XSLT */
122 * @tmpl_file: the encryption template file name.
123 * @key_file: the Triple DES key file.
124 * @data: the binary data to encrypt.
125 * @dataSize: the binary data size.
127 * Encrypts binary #data using template from #tmpl_file and DES key from
130 * Returns 0 on success or a negative value if an error occurs.
133 encrypt_file(const char* tmpl_file, const char* key_file,
134 const unsigned char* data, size_t dataSize) {
135 xmlDocPtr doc = NULL;
136 xmlNodePtr node = NULL;
137 xmlSecEncCtxPtr encCtx = NULL;
145 doc = xmlParseFile(tmpl_file);
146 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
147 fprintf(stderr, "Error: unable to parse file \"%s\"\n", tmpl_file);
151 /* find start node */
152 node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs);
154 fprintf(stderr, "Error: start node not found in \"%s\"\n", tmpl_file);
158 /* create encryption context, we don't need keys manager in this example */
159 encCtx = xmlSecEncCtxCreate(NULL);
161 fprintf(stderr,"Error: failed to create encryption context\n");
165 /* load DES key, assuming that there is not password */
166 encCtx->encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
167 if(encCtx->encKey == NULL) {
168 fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
172 /* set key name to the file name, this is just an example! */
173 if(xmlSecKeySetName(encCtx->encKey, key_file) < 0) {
174 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
178 /* encrypt the data */
179 if(xmlSecEncCtxBinaryEncrypt(encCtx, node, data, dataSize) < 0) {
180 fprintf(stderr,"Error: encryption failed\n");
184 /* print encrypted data with document to stdout */
185 xmlDocDump(stdout, doc);
194 xmlSecEncCtxDestroy(encCtx);