2 * XML Security Library example: Decrypting an encrypted file using a single key.
4 * Decrypts encrypted XML file using a single DES key from a binary file
7 * ./decrypt1 <xml-enc> <des-key-file>
10 * ./decrypt1 encrypt1-res.xml deskey.bin
11 * ./decrypt1 encrypt2-res.xml deskey.bin
13 * This is free software; see Copyright file in the source
14 * distribution for preciese wording.
16 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
22 #include <libxml/tree.h>
23 #include <libxml/xmlmemory.h>
24 #include <libxml/parser.h>
26 #ifndef XMLSEC_NO_XSLT
27 #include <libxslt/xslt.h>
28 #endif /* XMLSEC_NO_XSLT */
30 #include <xmlsec/xmlsec.h>
31 #include <xmlsec/xmltree.h>
32 #include <xmlsec/xmlenc.h>
33 #include <xmlsec/crypto.h>
35 int decrypt_file(const char* enc_file, const char* key_file);
38 main(int argc, char **argv) {
42 fprintf(stderr, "Error: wrong number of arguments.\n");
43 fprintf(stderr, "Usage: %s <enc-file> <key-file>\n", argv[0]);
47 /* Init libxml and libxslt libraries */
50 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
51 xmlSubstituteEntitiesDefault(1);
52 #ifndef XMLSEC_NO_XSLT
53 xmlIndentTreeOutput = 1;
54 #endif /* XMLSEC_NO_XSLT */
56 /* Init xmlsec library */
57 if(xmlSecInit() < 0) {
58 fprintf(stderr, "Error: xmlsec initialization failed.\n");
62 /* Check loaded library version */
63 if(xmlSecCheckVersion() != 1) {
64 fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
68 /* Load default crypto engine if we are supporting dynamic
69 * loading for xmlsec-crypto libraries. Use the crypto library
70 * name ("openssl", "nss", etc.) to load corresponding
71 * xmlsec-crypto library.
73 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
74 if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
75 fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
76 "that you have it installed and check shared libraries path\n"
77 "(LD_LIBRARY_PATH) envornment variable.\n");
80 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
82 /* Init crypto library */
83 if(xmlSecCryptoAppInit(NULL) < 0) {
84 fprintf(stderr, "Error: crypto initialization failed.\n");
88 /* Init xmlsec-crypto library */
89 if(xmlSecCryptoInit() < 0) {
90 fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
94 if(decrypt_file(argv[1], argv[2]) < 0) {
98 /* Shutdown xmlsec-crypto library */
99 xmlSecCryptoShutdown();
101 /* Shutdown crypto library */
102 xmlSecCryptoAppShutdown();
104 /* Shutdown xmlsec library */
107 /* Shutdown libxslt/libxml */
108 #ifndef XMLSEC_NO_XSLT
109 xsltCleanupGlobals();
110 #endif /* XMLSEC_NO_XSLT */
118 * @enc_file: the encrypted XML file name.
119 * @key_file: the Triple DES key file.
121 * Decrypts the XML file #enc_file using DES key from #key_file and
122 * prints results to stdout.
124 * Returns 0 on success or a negative value if an error occurs.
127 decrypt_file(const char* enc_file, const char* key_file) {
128 xmlDocPtr doc = NULL;
129 xmlNodePtr node = NULL;
130 xmlSecEncCtxPtr encCtx = NULL;
137 doc = xmlParseFile(enc_file);
138 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
139 fprintf(stderr, "Error: unable to parse file \"%s\"\n", enc_file);
143 /* find start node */
144 node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs);
146 fprintf(stderr, "Error: start node not found in \"%s\"\n", enc_file);
150 /* create encryption context, we don't need keys manager in this example */
151 encCtx = xmlSecEncCtxCreate(NULL);
153 fprintf(stderr,"Error: failed to create encryption context\n");
158 encCtx->encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
159 if(encCtx->encKey == NULL) {
160 fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
164 /* set key name to the file name, this is just an example! */
165 if(xmlSecKeySetName(encCtx->encKey, key_file) < 0) {
166 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
170 /* decrypt the data */
171 if((xmlSecEncCtxDecrypt(encCtx, node) < 0) || (encCtx->result == NULL)) {
172 fprintf(stderr,"Error: decryption failed\n");
176 /* print decrypted data to stdout */
177 if(encCtx->resultReplaced != 0) {
178 fprintf(stdout, "Decrypted XML data:\n");
179 xmlDocDump(stdout, doc);
181 fprintf(stdout, "Decrypted binary data (%d bytes):\n", xmlSecBufferGetSize(encCtx->result));
182 if(xmlSecBufferGetData(encCtx->result) != NULL) {
183 fwrite(xmlSecBufferGetData(encCtx->result),
185 xmlSecBufferGetSize(encCtx->result),
189 fprintf(stdout, "\n");
197 xmlSecEncCtxDestroy(encCtx);