2 * XML Security Library example: Encrypting XML file with a session key and dynamicaly created template.
4 * Encrypts XML file using a dynamicaly created template file and a session
5 * DES key (encrypted with an RSA key).
8 * ./encrypt3 <xml-doc> <rsa-pem-key-file>
11 * ./encrypt3 encrypt3-doc.xml rsakey.pem > encrypt3-res.xml
13 * The result could be decrypted with decrypt3 example:
14 * ./decrypt3 encrypt3-res.xml
16 * This is free software; see Copyright file in the source
17 * distribution for preciese wording.
19 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
25 #include <libxml/tree.h>
26 #include <libxml/xmlmemory.h>
27 #include <libxml/parser.h>
29 #ifndef XMLSEC_NO_XSLT
30 #include <libxslt/xslt.h>
31 #endif /* XMLSEC_NO_XSLT */
33 #include <xmlsec/xmlsec.h>
34 #include <xmlsec/xmltree.h>
35 #include <xmlsec/xmlenc.h>
36 #include <xmlsec/templates.h>
37 #include <xmlsec/crypto.h>
39 xmlSecKeysMngrPtr load_rsa_keys(char* key_file);
40 int encrypt_file(xmlSecKeysMngrPtr mngr, const char* xml_file, const char* key_name);
43 main(int argc, char **argv) {
44 xmlSecKeysMngrPtr mngr;
49 fprintf(stderr, "Error: wrong number of arguments.\n");
50 fprintf(stderr, "Usage: %s <xml-file> <key-file>\n", argv[0]);
54 /* Init libxml and libxslt libraries */
57 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
58 xmlSubstituteEntitiesDefault(1);
59 #ifndef XMLSEC_NO_XSLT
60 xmlIndentTreeOutput = 1;
61 #endif /* XMLSEC_NO_XSLT */
63 /* Init xmlsec library */
64 if(xmlSecInit() < 0) {
65 fprintf(stderr, "Error: xmlsec initialization failed.\n");
69 /* Check loaded library version */
70 if(xmlSecCheckVersion() != 1) {
71 fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
75 /* Load default crypto engine if we are supporting dynamic
76 * loading for xmlsec-crypto libraries. Use the crypto library
77 * name ("openssl", "nss", etc.) to load corresponding
78 * xmlsec-crypto library.
80 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
81 if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
82 fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
83 "that you have it installed and check shared libraries path\n"
84 "(LD_LIBRARY_PATH) envornment variable.\n");
87 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
89 /* Init crypto library */
90 if(xmlSecCryptoAppInit(NULL) < 0) {
91 fprintf(stderr, "Error: crypto initialization failed.\n");
95 /* Init xmlsec-crypto library */
96 if(xmlSecCryptoInit() < 0) {
97 fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
101 /* create keys manager and load keys */
102 mngr = load_rsa_keys(argv[2]);
107 /* we use key filename as key name here */
108 if(encrypt_file(mngr, argv[1], argv[2]) < 0) {
109 xmlSecKeysMngrDestroy(mngr);
113 /* destroy keys manager */
114 xmlSecKeysMngrDestroy(mngr);
116 /* Shutdown xmlsec-crypto library */
117 xmlSecCryptoShutdown();
119 /* Shutdown crypto library */
120 xmlSecCryptoAppShutdown();
122 /* Shutdown xmlsec library */
125 /* Shutdown libxslt/libxml */
126 #ifndef XMLSEC_NO_XSLT
127 xsltCleanupGlobals();
128 #endif /* XMLSEC_NO_XSLT */
136 * @key_file: the key filename.
138 * Creates simple keys manager and load RSA key from #key_file in it.
139 * The caller is responsible for destroing returned keys manager using
140 * @xmlSecKeysMngrDestroy.
142 * Returns the pointer to newly created keys manager or NULL if an error
146 load_rsa_keys(char* key_file) {
147 xmlSecKeysMngrPtr mngr;
152 /* create and initialize keys manager, we use a simple list based
153 * keys manager, implement your own xmlSecKeysStore klass if you need
154 * something more sophisticated
156 mngr = xmlSecKeysMngrCreate();
158 fprintf(stderr, "Error: failed to create keys manager.\n");
161 if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) {
162 fprintf(stderr, "Error: failed to initialize keys manager.\n");
163 xmlSecKeysMngrDestroy(mngr);
167 /* load private RSA key */
168 key = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem, NULL, NULL, NULL);
170 fprintf(stderr,"Error: failed to load rsa key from file \"%s\"\n", key_file);
171 xmlSecKeysMngrDestroy(mngr);
175 /* set key name to the file name, this is just an example! */
176 if(xmlSecKeySetName(key, BAD_CAST key_file) < 0) {
177 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
178 xmlSecKeyDestroy(key);
179 xmlSecKeysMngrDestroy(mngr);
183 /* add key to keys manager, from now on keys manager is responsible
186 if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key) < 0) {
187 fprintf(stderr,"Error: failed to add key from \"%s\" to keys manager\n", key_file);
188 xmlSecKeyDestroy(key);
189 xmlSecKeysMngrDestroy(mngr);
198 * @mngr: the pointer to keys manager.
199 * @xml_file: the encryption template file name.
200 * @key_name: the RSA key name.
202 * Encrypts #xml_file using a dynamicaly created template, a session DES key
203 * and an RSA key from keys manager.
205 * Returns 0 on success or a negative value if an error occurs.
208 encrypt_file(xmlSecKeysMngrPtr mngr, const char* xml_file, const char* key_name) {
209 xmlDocPtr doc = NULL;
210 xmlNodePtr encDataNode = NULL;
211 xmlNodePtr keyInfoNode = NULL;
212 xmlNodePtr encKeyNode = NULL;
213 xmlNodePtr keyInfoNode2 = NULL;
214 xmlSecEncCtxPtr encCtx = NULL;
222 doc = xmlParseFile(xml_file);
223 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
224 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
228 /* create encryption template to encrypt XML file and replace
229 * its content with encryption result */
230 encDataNode = xmlSecTmplEncDataCreate(doc, xmlSecTransformDes3CbcId,
231 NULL, xmlSecTypeEncElement, NULL, NULL);
232 if(encDataNode == NULL) {
233 fprintf(stderr, "Error: failed to create encryption template\n");
237 /* we want to put encrypted data in the <enc:CipherValue/> node */
238 if(xmlSecTmplEncDataEnsureCipherValue(encDataNode) == NULL) {
239 fprintf(stderr, "Error: failed to add CipherValue node\n");
243 /* add <dsig:KeyInfo/> */
244 keyInfoNode = xmlSecTmplEncDataEnsureKeyInfo(encDataNode, NULL);
245 if(keyInfoNode == NULL) {
246 fprintf(stderr, "Error: failed to add key info\n");
250 /* add <enc:EncryptedKey/> to store the encrypted session key */
251 encKeyNode = xmlSecTmplKeyInfoAddEncryptedKey(keyInfoNode,
252 xmlSecTransformRsaPkcs1Id,
254 if(encKeyNode == NULL) {
255 fprintf(stderr, "Error: failed to add key info\n");
259 /* we want to put encrypted key in the <enc:CipherValue/> node */
260 if(xmlSecTmplEncDataEnsureCipherValue(encKeyNode) == NULL) {
261 fprintf(stderr, "Error: failed to add CipherValue node\n");
265 /* add <dsig:KeyInfo/> and <dsig:KeyName/> nodes to <enc:EncryptedKey/> */
266 keyInfoNode2 = xmlSecTmplEncDataEnsureKeyInfo(encKeyNode, NULL);
267 if(keyInfoNode2 == NULL) {
268 fprintf(stderr, "Error: failed to add key info\n");
272 /* set key name so we can lookup key when needed */
273 if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode2, key_name) == NULL) {
274 fprintf(stderr, "Error: failed to add key name\n");
278 /* create encryption context */
279 encCtx = xmlSecEncCtxCreate(mngr);
281 fprintf(stderr,"Error: failed to create encryption context\n");
285 /* generate a Triple DES key */
286 encCtx->encKey = xmlSecKeyGenerate(xmlSecKeyDataDesId, 192, xmlSecKeyDataTypeSession);
287 if(encCtx->encKey == NULL) {
288 fprintf(stderr,"Error: failed to generate session des key\n");
292 /* encrypt the data */
293 if(xmlSecEncCtxXmlEncrypt(encCtx, encDataNode, xmlDocGetRootElement(doc)) < 0) {
294 fprintf(stderr,"Error: encryption failed\n");
298 /* we template is inserted in the doc */
301 /* print encrypted data with document to stdout */
302 xmlDocDump(stdout, doc);
311 xmlSecEncCtxDestroy(encCtx);
314 if(encDataNode != NULL) {
315 xmlFreeNode(encDataNode);