Git init
[external/xmlsec1.git] / examples / encrypt1.c
1 /** 
2  * XML Security Library example: Encrypting data using a template file.
3  * 
4  * Encrypts binary data using a template file and a DES key from a binary file
5  * 
6  * Usage: 
7  *      ./encrypt1 <xml-tmpl> <des-key-file> 
8  *
9  * Example:
10  *      ./encrypt1 encrypt1-tmpl.xml deskey.bin > encrypt1-res.xml
11  *
12  * The result could be decrypted with decrypt1 example:
13  *      ./decrypt1 encrypt1-res.xml deskey.bin
14  *
15  * This is free software; see Copyright file in the source
16  * distribution for preciese wording.
17  * 
18  * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
19  */
20 #include <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
23
24 #include <libxml/tree.h>
25 #include <libxml/xmlmemory.h>
26 #include <libxml/parser.h>
27
28 #ifndef XMLSEC_NO_XSLT
29 #include <libxslt/xslt.h>
30 #endif /* XMLSEC_NO_XSLT */
31
32 #include <xmlsec/xmlsec.h>
33 #include <xmlsec/xmltree.h>
34 #include <xmlsec/xmlenc.h>
35 #include <xmlsec/crypto.h>
36
37 int encrypt_file(const char* tmpl_file, const char* key_file, 
38                  const unsigned char* data, size_t dataSize);
39 int 
40 main(int argc, char **argv) {
41     static const char secret_data[] = "Big secret";
42     
43     assert(argv);
44
45     if(argc != 3) {
46         fprintf(stderr, "Error: wrong number of arguments.\n");
47         fprintf(stderr, "Usage: %s <tmpl-file> <key-file>\n", argv[0]);
48         return(1);
49     }
50
51     /* Init libxml and libxslt libraries */
52     xmlInitParser();
53     LIBXML_TEST_VERSION
54     xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
55     xmlSubstituteEntitiesDefault(1);
56 #ifndef XMLSEC_NO_XSLT
57     xmlIndentTreeOutput = 1; 
58 #endif /* XMLSEC_NO_XSLT */
59                 
60     /* Init xmlsec library */
61     if(xmlSecInit() < 0) {
62         fprintf(stderr, "Error: xmlsec initialization failed.\n");
63         return(-1);
64     }
65
66     /* Check loaded library version */
67     if(xmlSecCheckVersion() != 1) {
68         fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
69         return(-1);
70     }
71
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.
76      */
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");
82         return(-1);     
83     }
84 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
85
86     /* Init crypto library */
87     if(xmlSecCryptoAppInit(NULL) < 0) {
88         fprintf(stderr, "Error: crypto initialization failed.\n");
89         return(-1);
90     }
91
92     /* Init xmlsec-crypto library */
93     if(xmlSecCryptoInit() < 0) {
94         fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
95         return(-1);
96     }
97
98     if(encrypt_file(argv[1], argv[2], secret_data, strlen(secret_data)) < 0) {
99         return(-1);
100     }    
101     
102     /* Shutdown xmlsec-crypto library */
103     xmlSecCryptoShutdown();
104     
105     /* Shutdown crypto library */
106     xmlSecCryptoAppShutdown();
107     
108     /* Shutdown xmlsec library */
109     xmlSecShutdown();
110
111     /* Shutdown libxslt/libxml */
112 #ifndef XMLSEC_NO_XSLT
113     xsltCleanupGlobals();            
114 #endif /* XMLSEC_NO_XSLT */
115     xmlCleanupParser();
116     
117     return(0);
118 }
119
120 /**
121  * encrypt_file:
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.
126  *
127  * Encrypts binary #data using template from #tmpl_file and DES key from
128  * #key_file.
129  *
130  * Returns 0 on success or a negative value if an error occurs.
131  */
132 int 
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;
138     int res = -1;
139     
140     assert(tmpl_file);
141     assert(key_file);
142     assert(data);
143
144     /* load template */
145     doc = xmlParseFile(tmpl_file);
146     if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
147         fprintf(stderr, "Error: unable to parse file \"%s\"\n", tmpl_file);
148         goto done;      
149     }
150     
151     /* find start node */
152     node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs);
153     if(node == NULL) {
154         fprintf(stderr, "Error: start node not found in \"%s\"\n", tmpl_file);
155         goto done;      
156     }
157
158     /* create encryption context, we don't need keys manager in this example */
159     encCtx = xmlSecEncCtxCreate(NULL);
160     if(encCtx == NULL) {
161         fprintf(stderr,"Error: failed to create encryption context\n");
162         goto done;
163     }
164
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);
169         goto done;
170     }
171
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);
175         goto done;
176     }
177
178     /* encrypt the data */
179     if(xmlSecEncCtxBinaryEncrypt(encCtx, node, data, dataSize) < 0) {
180         fprintf(stderr,"Error: encryption failed\n");
181         goto done;
182     }
183         
184     /* print encrypted data with document to stdout */
185     xmlDocDump(stdout, doc);
186     
187     /* success */
188     res = 0;
189
190 done:    
191
192     /* cleanup */
193     if(encCtx != NULL) {
194         xmlSecEncCtxDestroy(encCtx);
195     }
196     
197     if(doc != NULL) {
198         xmlFreeDoc(doc); 
199     }
200     return(res);
201 }
202