/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <stdio.h>
#include <stdarg.h>
#include <sys/time.h>

#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/parser.h>

#include <openssl/err.h>
#include <openssl/rand.h>

#include <xmlsec/xmlsec.h>
#include <xmlsec/xmldsig.h> 
#include <xmlsec/xmlenc.h> 
#include <xmlsec/keys.h>
#include <xmlsec/keysmngr.h>
#include <xmlsec/transforms.h>
#include <xmlsec/xmltree.h>

#include "tools.h"

#define keyfile "../ssl/test.key"
#define crtfile "../ssl/test.crt"

xmlDocPtr 		doc = 0;
xmlSecKeysMngrPtr	keysMngr = 0; 
xmlSecKeysMngrPtr	vkeysMngr = 0; 
xmlSecDSigCtxPtr 	dsigCtx = 0;
xmlSecDSigCtxPtr 	vdsigCtx = 0;
xmlSecEncCtxPtr 	encCtx = 0;
xmlSecEncCtxPtr 	decCtx = 0;
xmlNodePtr		signature = 0;
xmlNodePtr		enc = 0;
xmlSecKeyPtr		key,crt,skey;

void initXML(struct TestData *td) {
    xmlInitParser();
}

void releaseXML(struct TestData *td) {
    if (doc) {
	xmlFreeDoc(doc);    
	doc = NULL;
    }

    xmlSecSignatureDestroy(signature);
    
    xmlSecDSigCtxDestroy(dsigCtx);
    xmlSecSimpleKeysMngrDestroy(keysMngr);
    xmlSecDSigCtxDestroy(vdsigCtx);
    xmlSecSimpleKeysMngrDestroy(vkeysMngr);
    
    xmlSecEncCtxDestroy(encCtx);
    xmlSecEncCtxDestroy(decCtx);

    xmlSecShutdown();

    xmlCleanupParser();

    RAND_cleanup();
    ERR_clear_error();
}


xmlNodePtr	encKey;

void initXML_Security(struct TestData *td) {
    int rndseed = 0;
    xmlNodePtr	si_node,cm_node,sm_node,r_node,dm_node,t_node;
    xmlNodePtr	em_node,cv_node,ki_node,kn_node;
    xmlNodePtr	node_k_em,node_k_cv,node_k_ki,node_k_kn;
    
	/* Initialising OpenSSL */
    while (RAND_status() != 1) {
	RAND_seed(&rndseed, sizeof(rndseed));
    }

	/* Initialising XML Security */
    xmlSecInit();    

    keysMngr = xmlSecSimpleKeysMngrCreate();    
    if(keysMngr == NULL) {
	fprintf(stderr, "Error: failed to create keys manager!\n");
	exit(1);
    }

    key=xmlSecSimpleKeysMngrLoadPemKey(keysMngr, keyfile, 0/*password*/, 0/*password callback*/, 1/*private key*/);
    if(key == NULL) {
	fprintf(stderr, "Error: failed to load key from \"%s\"\n", keyfile);
	exit(1);
    }
    key->name = xmlStrdup(BAD_CAST "rsakey");

    vkeysMngr = xmlSecSimpleKeysMngrCreate();    
    if(vkeysMngr == NULL) {
	fprintf(stderr, "Error: failed to create keys manager!\n");
	exit(1);
    }

    crt=xmlSecSimpleKeysMngrLoadPemKey(vkeysMngr, crtfile, 0/*password*/, 0/*password callback*/, 0/*private key*/);
    if(crt == NULL) {
	fprintf(stderr, "Error: failed to load certificate from \"%s\"\n", crtfile);
	exit(1);
    }
    crt->name = xmlStrdup(BAD_CAST "rsapubkey");

//    keysMngr->allowedOrigins = xmlSecKeyOriginKeyManager | xmlSecKeyOriginKeyName;
//    vkeysMngr->allowedOrigins = xmlSecKeyOriginKeyManager | xmlSecKeyOriginKeyName;

    dsigCtx = xmlSecDSigCtxCreate(keysMngr);
    if(dsigCtx == NULL) {
    	fprintf(stderr,"Error: failed to create dsig context\n");
	exit(1);
    }                

    vdsigCtx = xmlSecDSigCtxCreate(vkeysMngr);
    if(dsigCtx == NULL) {
    	fprintf(stderr,"Error: failed to create dsig context\n");
	exit(1);
    }                

    encCtx = xmlSecEncCtxCreate(vkeysMngr);
    if(encCtx == NULL) {
	fprintf(stderr, "Error: failed to create encryption context\n");
	exit(1);
    }

    decCtx = xmlSecEncCtxCreate(keysMngr);
    if(encCtx == NULL) {
	fprintf(stderr, "Error: failed to create encryption context\n");
	exit(1);
    }

	/* Creating symmetric DES key */    
    skey = xmlSecKeyCreate(xmlSecDesKey, xmlSecKeyOriginDefault);
    if(skey == NULL) {
	fprintf(stderr, "Error: failed to create des3 key (init structure)\n"); 
	exit(1);
    }        
    
    if (xmlSecDesKeyGenerate(skey, NULL, 24)<0) {
	fprintf(stderr, "Error: failed to create des3 key (generate)\n"); 
	exit(1);
    }    

    skey->name = xmlStrdup(BAD_CAST "skey");

    if (xmlSecSimpleKeysMngrAddKey(vkeysMngr, skey)<0) {
	fprintf(stderr, "Error: failed to add des3 key into KeyManager\n"); 
	exit(1);
    }

	/* Creating Signature */    
    signature = xmlSecSignatureCreate(NULL);
    if(signature == NULL) {
    	fprintf(stderr,"Error: failed to create signature node\n");
	exit(1);
    }
    
    si_node = xmlSecSignatureAddSignedInfo(signature, NULL);
    if(si_node == NULL) {
    	fprintf(stderr,"Error: failed to create SignedInfo node\n");
	exit(1);
    }

    cm_node = xmlSecSignedInfoAddC14NMethod(si_node, xmlSecC14NInclusive /* algorithm */);
    if(cm_node == NULL) {
    	fprintf(stderr,"Error: failed to create CanocalizationMethods node\n");
	exit(1);
    }
    
    sm_node = xmlSecSignedInfoAddSignMethod(si_node, xmlSecSignRsaSha1 /* key type */);
    if(sm_node == NULL) {
    	fprintf(stderr,"Error: failed to create SignMethod node\n");
	exit(1);
    }

    r_node = xmlSecSignedInfoAddReference(si_node,0/*node_id*/,0/*node_uri*/,0/*node_type*/);
    if(r_node == NULL) {
    	fprintf(stderr,"Error: failed to create Reference node\n");
	exit(1);
    }

    t_node = xmlSecReferenceAddTransform(r_node, xmlSecTransformEnveloped /* Signing enveloped element */);
    if(t_node == NULL) {
    	fprintf(stderr,"Error: failed to add enveloped transform\n");
	exit(1);
    }

    dm_node = xmlSecReferenceAddDigestMethod(r_node, xmlSecDigestSha1);
    if(dm_node == NULL) {
    	fprintf(stderr,"Error: failed to add DigestMethod node\n");
	exit(1);
    }

	/* Creating encryption Node */

    enc = xmlSecEncDataCreate(NULL, xmlSecEncTypeElement, NULL, NULL);
    if(enc == NULL) {
	fprintf(stderr, "Error: EncryptedData node creation failed\n");
	exit(1);
    }

    em_node=xmlSecEncDataAddEncMethod(enc, xmlSecEncDes3Cbc);
    if(em_node == NULL) {
	fprintf(stderr, "Error: failed to add EncryptionMethod node\n");
	exit(1);
    }

    cv_node = xmlSecEncDataAddCipherValue(enc);    
    if(cv_node == NULL) {
	fprintf(stderr, "Error: failed to add CipherValue node\n");
	exit(1);
    }

    ki_node = xmlSecEncDataAddKeyInfo(enc);
    if(ki_node == NULL) {
	fprintf(stderr, "Error: failed to add KeyInfo node\n");
	exit(1);
    }

/*    kn_node = xmlSecKeyInfoAddKeyName(ki_node);
    if(kn_node == NULL) {
	fprintf(stderr, "Error: failed to add KeyName node\n");
	exit(1);
    }*/

    encKey = xmlSecKeyInfoAddEncryptedKey(ki_node, NULL, NULL, NULL);
    if(encKey == NULL) {
	fprintf(stderr, "Error: failed to add EncryptedKey node\n");
	exit(1);
    }
    
    node_k_em = xmlSecEncDataAddEncMethod(encKey, xmlSecEncRsaPkcs1);
    if(node_k_em == NULL) {
	fprintf(stderr, "Error: failed to add EncryptedMethod node to EncryptedKey\n");
	exit(1);
    }

    node_k_cv = xmlSecEncDataAddCipherValue(encKey);    
    if(node_k_cv == NULL) {
	fprintf(stderr, "Error: failed to add CipherValue node to EncryptedKey \n");
	exit(1);
    }

    node_k_ki = xmlSecEncDataAddKeyInfo(encKey);
    if(node_k_ki == NULL) {
	fprintf(stderr, "Error: failed to add KeyInfo node to EncryptedKey\n");
	exit(1);
    }

    node_k_kn = xmlSecKeyInfoAddKeyName(node_k_ki);
    if(node_k_kn == NULL) {
	fprintf(stderr, "Error: failed to add KeyName node to EncryptedKey\n");
	exit(1);
    }
}

void parseXML(struct TestData *td, unsigned long iter) {
    if (doc) {
	xmlFreeDoc(doc);    
    }

    doc=xmlParseMemory(td->xml,td->xmllen);
    if (!doc) {
        fprintf(stderr,"Error parsing document!\n");
        exit(1);
    }
}

xmlChar *mem;
int memsize;

void signXML(struct TestData *td, unsigned long iter) { 
    xmlNodePtr node;
    xmlSecDSigResultPtr result;
    
    node=xmlCopyNode(signature,1);

    if (!node) {
	fprintf(stderr,"Error: failed copy Signature Node!\n");
	exit(1);
    }
    
    if(xmlAddChild(xmlDocGetRootElement(doc), node) == NULL) {
    	fprintf(stderr,"Error: failed to add Signature into the Document!\n");
	exit(1);
    }
    
    if (xmlSecDSigGenerate(dsigCtx, NULL, key, node, &result)<0) {
    	fprintf(stderr,"Error: signature failed\n");
	exit(1);
    }     

    xmlSecDSigResultDestroy(result);

/*    
    if (iter==td->iterations) {
        xmlDocDumpMemory(doc,&mem,&memsize);
	puts(mem);
	free(mem);
    }
*/
}

void verifyXML(struct TestData *td, unsigned long iter) { 
    xmlNodePtr node;
    xmlSecDSigResultPtr result;

    node = xmlSecFindNode(xmlDocGetRootElement(doc), BAD_CAST "Signature", xmlSecDSigNs);
    if(node == NULL) {
        fprintf(stderr,"Error: failed to find Signature node in Document\n");
	exit(1);
    }    
     
    if (xmlSecDSigValidate(vdsigCtx, NULL, crt, node, &result)<0) {
    	fprintf(stderr,"Error: Verification failed (processing error)\n");
	exit(1);
    }     
    
    if (result->result!=xmlSecTransformStatusOk) {
    	fprintf(stderr,"Error: Verification failed (verification error)\n");
	exit(1);
    }
    
//    xmlSecDSigResultDebugDump(result, stdout); 
    
    xmlSecDSigResultDestroy(result);
}

/* In case if EncryptedData "type" attribute isn't specified!

void encryptXML(struct TestData *td, unsigned long iter) { 
    xmlDocPtr doctmp;
    xmlNodePtr node,enode;
    xmlSecEncResultPtr result = 0;
    
    node=xmlCopyNode(enc,1);
    enode=xmlDocGetRootElement(doc);

	// If this 2 commands place in other direction, - not all subnodes
	// of node will attached to right doc. Why?
    xmlSetTreeDoc(node,doc);
    xmlDocSetRootElement(doc, node);

    if (xmlSecEncryptXmlNode(encCtx, NULL, skey, node, enode, &result)<0) {
	fprintf(stderr, "Error: Encryption Failed!\n");
	exit(1);
    }
    xmlSecEncResultDestroy(result);
    xmlFreeNode(enode);
}
*/

void encryptXML(struct TestData *td, unsigned long iter) { 
    xmlDocPtr doctmp;
    xmlNodePtr node,enode;
    xmlSecEncResultPtr result = 0;
    
    node=xmlCopyNode(enc,1);
    enode=xmlDocGetRootElement(doc);
    xmlAddChild(enode,node);

    if (xmlSecEncryptXmlNode(encCtx, NULL, skey, node, enode, &result)<0) {
	fprintf(stderr, "Error: Encryption Failed!\n");
	exit(1);
    }
    xmlSecEncResultDestroy(result);

/*    
    if (iter==td->iterations) {
	xmlDocDumpMemory(doc,&mem,&memsize);
	puts(mem);
	free(mem);
    }
*/
}

void decryptXML(struct TestData *td, unsigned long iter) { 
    xmlNodePtr node;
    xmlSecEncResultPtr result = 0;

    if (xmlSecDecrypt(decCtx, NULL, NULL, xmlDocGetRootElement(doc), &result)<0) {
	fprintf(stderr, "Error: Decryption Failed!\n");
	exit(1);
    }
    
    node = xmlSecFindNode(xmlDocGetRootElement(doc), BAD_CAST "EncryptedData", xmlSecEncNs);    
    if (node==NULL) {
	fprintf(stderr,"Error: Can't find EncryptedData node\n");
	exit(0);
    }
    xmlUnlinkNode(node);
    xmlFreeNode(node);

/*  
    if (iter==td->iterations) {
	xmlDocDumpMemory(doc,&mem,&memsize);
	puts(mem);
	free(mem);
    }
*/

}

int main(int argc, char *argv[]) {
    return Test(argc,argv);
}