/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk

« back to all changes in this revision

Viewing changes to security/libxml.c

  • Committer: Suren A. Chilingaryan
  • Date: 2009-02-16 09:27:17 UTC
  • Revision ID: csa@dside.dyndns.org-20090216092717-wipyvaaw2srxhgns
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdarg.h>
 
3
#include <sys/time.h>
 
4
 
 
5
#include <libxml/xmlmemory.h>
 
6
#include <libxml/tree.h>
 
7
#include <libxml/parser.h>
 
8
 
 
9
#include <openssl/err.h>
 
10
#include <openssl/rand.h>
 
11
 
 
12
#include <xmlsec/xmlsec.h>
 
13
#include <xmlsec/xmldsig.h> 
 
14
#include <xmlsec/xmlenc.h> 
 
15
#include <xmlsec/keys.h>
 
16
#include <xmlsec/keysmngr.h>
 
17
#include <xmlsec/transforms.h>
 
18
#include <xmlsec/xmltree.h>
 
19
 
 
20
#include "tools.h"
 
21
 
 
22
#define keyfile "../ssl/test.key"
 
23
#define crtfile "../ssl/test.crt"
 
24
 
 
25
xmlDocPtr               doc = 0;
 
26
xmlSecKeysMngrPtr       keysMngr = 0; 
 
27
xmlSecKeysMngrPtr       vkeysMngr = 0; 
 
28
xmlSecDSigCtxPtr        dsigCtx = 0;
 
29
xmlSecDSigCtxPtr        vdsigCtx = 0;
 
30
xmlSecEncCtxPtr         encCtx = 0;
 
31
xmlSecEncCtxPtr         decCtx = 0;
 
32
xmlNodePtr              signature = 0;
 
33
xmlNodePtr              enc = 0;
 
34
xmlSecKeyPtr            key,crt,skey;
 
35
 
 
36
void initXML(struct TestData *td) {
 
37
    xmlInitParser();
 
38
}
 
39
 
 
40
void releaseXML(struct TestData *td) {
 
41
    xmlFreeDoc(doc);    
 
42
 
 
43
    xmlSecSignatureDestroy(signature);
 
44
    
 
45
    xmlSecDSigCtxDestroy(dsigCtx);
 
46
    xmlSecSimpleKeysMngrDestroy(keysMngr);
 
47
    xmlSecDSigCtxDestroy(vdsigCtx);
 
48
    xmlSecSimpleKeysMngrDestroy(vkeysMngr);
 
49
    
 
50
    xmlSecEncCtxDestroy(encCtx);
 
51
    xmlSecEncCtxDestroy(decCtx);
 
52
 
 
53
    xmlSecShutdown();
 
54
 
 
55
    xmlCleanupParser();
 
56
 
 
57
    RAND_cleanup();
 
58
    ERR_clear_error();
 
59
}
 
60
 
 
61
 
 
62
xmlNodePtr      encKey;
 
63
 
 
64
void initXML_Security(struct TestData *td) {
 
65
    int rndseed = 0;
 
66
    xmlNodePtr  si_node,cm_node,sm_node,r_node,dm_node,t_node;
 
67
    xmlNodePtr  em_node,cv_node,ki_node,kn_node;
 
68
    xmlNodePtr  node_k_em,node_k_cv,node_k_ki,node_k_kn;
 
69
    
 
70
        /* Initialising OpenSSL */
 
71
    while (RAND_status() != 1) {
 
72
        RAND_seed(&rndseed, sizeof(rndseed));
 
73
    }
 
74
 
 
75
        /* Initialising XML Security */
 
76
    xmlSecInit();    
 
77
 
 
78
    keysMngr = xmlSecSimpleKeysMngrCreate();    
 
79
    if(keysMngr == NULL) {
 
80
        fprintf(stderr, "Error: failed to create keys manager!\n");
 
81
        exit(1);
 
82
    }
 
83
 
 
84
    key=xmlSecSimpleKeysMngrLoadPemKey(keysMngr, keyfile, 0/*password*/, 0/*password callback*/, 1/*private key*/);
 
85
    if(key == NULL) {
 
86
        fprintf(stderr, "Error: failed to load key from \"%s\"\n", keyfile);
 
87
        exit(1);
 
88
    }
 
89
    key->name = xmlStrdup(BAD_CAST "rsakey");
 
90
 
 
91
    vkeysMngr = xmlSecSimpleKeysMngrCreate();    
 
92
    if(vkeysMngr == NULL) {
 
93
        fprintf(stderr, "Error: failed to create keys manager!\n");
 
94
        exit(1);
 
95
    }
 
96
 
 
97
    crt=xmlSecSimpleKeysMngrLoadPemKey(vkeysMngr, crtfile, 0/*password*/, 0/*password callback*/, 0/*private key*/);
 
98
    if(crt == NULL) {
 
99
        fprintf(stderr, "Error: failed to load certificate from \"%s\"\n", crtfile);
 
100
        exit(1);
 
101
    }
 
102
    crt->name = xmlStrdup(BAD_CAST "rsapubkey");
 
103
 
 
104
//    keysMngr->allowedOrigins = xmlSecKeyOriginKeyManager | xmlSecKeyOriginKeyName;
 
105
//    vkeysMngr->allowedOrigins = xmlSecKeyOriginKeyManager | xmlSecKeyOriginKeyName;
 
106
 
 
107
    dsigCtx = xmlSecDSigCtxCreate(keysMngr);
 
108
    if(dsigCtx == NULL) {
 
109
        fprintf(stderr,"Error: failed to create dsig context\n");
 
110
        exit(1);
 
111
    }                
 
112
 
 
113
    vdsigCtx = xmlSecDSigCtxCreate(vkeysMngr);
 
114
    if(dsigCtx == NULL) {
 
115
        fprintf(stderr,"Error: failed to create dsig context\n");
 
116
        exit(1);
 
117
    }                
 
118
 
 
119
    encCtx = xmlSecEncCtxCreate(vkeysMngr);
 
120
    if(encCtx == NULL) {
 
121
        fprintf(stderr, "Error: failed to create encryption context\n");
 
122
        exit(1);
 
123
    }
 
124
 
 
125
    decCtx = xmlSecEncCtxCreate(keysMngr);
 
126
    if(encCtx == NULL) {
 
127
        fprintf(stderr, "Error: failed to create encryption context\n");
 
128
        exit(1);
 
129
    }
 
130
 
 
131
        /* Creating symmetric DES key */    
 
132
    skey = xmlSecKeyCreate(xmlSecDesKey, xmlSecKeyOriginDefault);
 
133
    if(skey == NULL) {
 
134
        fprintf(stderr, "Error: failed to create des3 key (init structure)\n"); 
 
135
        exit(1);
 
136
    }        
 
137
    
 
138
    if (xmlSecDesKeyGenerate(skey, NULL, 24)<0) {
 
139
        fprintf(stderr, "Error: failed to create des3 key (generate)\n"); 
 
140
        exit(1);
 
141
    }    
 
142
 
 
143
    skey->name = xmlStrdup(BAD_CAST "skey");
 
144
 
 
145
    if (xmlSecSimpleKeysMngrAddKey(vkeysMngr, skey)<0) {
 
146
        fprintf(stderr, "Error: failed to add des3 key into KeyManager\n"); 
 
147
        exit(1);
 
148
    }
 
149
 
 
150
        /* Creating Signature */    
 
151
    signature = xmlSecSignatureCreate(NULL);
 
152
    if(signature == NULL) {
 
153
        fprintf(stderr,"Error: failed to create signature node\n");
 
154
        exit(1);
 
155
    }
 
156
    
 
157
    si_node = xmlSecSignatureAddSignedInfo(signature, NULL);
 
158
    if(si_node == NULL) {
 
159
        fprintf(stderr,"Error: failed to create SignedInfo node\n");
 
160
        exit(1);
 
161
    }
 
162
 
 
163
    cm_node = xmlSecSignedInfoAddC14NMethod(si_node, xmlSecC14NInclusive /* algorithm */);
 
164
    if(cm_node == NULL) {
 
165
        fprintf(stderr,"Error: failed to create CanocalizationMethods node\n");
 
166
        exit(1);
 
167
    }
 
168
    
 
169
    sm_node = xmlSecSignedInfoAddSignMethod(si_node, xmlSecSignRsaSha1 /* key type */);
 
170
    if(sm_node == NULL) {
 
171
        fprintf(stderr,"Error: failed to create SignMethod node\n");
 
172
        exit(1);
 
173
    }
 
174
 
 
175
    r_node = xmlSecSignedInfoAddReference(si_node,0/*node_id*/,0/*node_uri*/,0/*node_type*/);
 
176
    if(r_node == NULL) {
 
177
        fprintf(stderr,"Error: failed to create Reference node\n");
 
178
        exit(1);
 
179
    }
 
180
 
 
181
    t_node = xmlSecReferenceAddTransform(r_node, xmlSecTransformEnveloped /* Signing enveloped element */);
 
182
    if(t_node == NULL) {
 
183
        fprintf(stderr,"Error: failed to add enveloped transform\n");
 
184
        exit(1);
 
185
    }
 
186
 
 
187
    dm_node = xmlSecReferenceAddDigestMethod(r_node, xmlSecDigestSha1);
 
188
    if(dm_node == NULL) {
 
189
        fprintf(stderr,"Error: failed to add DigestMethod node\n");
 
190
        exit(1);
 
191
    }
 
192
 
 
193
        /* Creating encryption Node */
 
194
 
 
195
    enc = xmlSecEncDataCreate(NULL, xmlSecEncTypeElement, NULL, NULL);
 
196
    if(enc == NULL) {
 
197
        fprintf(stderr, "Error: EncryptedData node creation failed\n");
 
198
        exit(1);
 
199
    }
 
200
 
 
201
    em_node=xmlSecEncDataAddEncMethod(enc, xmlSecEncDes3Cbc);
 
202
    if(em_node == NULL) {
 
203
        fprintf(stderr, "Error: failed to add EncryptionMethod node\n");
 
204
        exit(1);
 
205
    }
 
206
 
 
207
    cv_node = xmlSecEncDataAddCipherValue(enc);    
 
208
    if(cv_node == NULL) {
 
209
        fprintf(stderr, "Error: failed to add CipherValue node\n");
 
210
        exit(1);
 
211
    }
 
212
 
 
213
    ki_node = xmlSecEncDataAddKeyInfo(enc);
 
214
    if(ki_node == NULL) {
 
215
        fprintf(stderr, "Error: failed to add KeyInfo node\n");
 
216
        exit(1);
 
217
    }
 
218
 
 
219
/*    kn_node = xmlSecKeyInfoAddKeyName(ki_node);
 
220
    if(kn_node == NULL) {
 
221
        fprintf(stderr, "Error: failed to add KeyName node\n");
 
222
        exit(1);
 
223
    }*/
 
224
 
 
225
    encKey = xmlSecKeyInfoAddEncryptedKey(ki_node, NULL, NULL, NULL);
 
226
    if(encKey == NULL) {
 
227
        fprintf(stderr, "Error: failed to add EncryptedKey node\n");
 
228
        exit(1);
 
229
    }
 
230
    
 
231
    node_k_em = xmlSecEncDataAddEncMethod(encKey, xmlSecEncRsaPkcs1);
 
232
    if(node_k_em == NULL) {
 
233
        fprintf(stderr, "Error: failed to add EncryptedMethod node to EncryptedKey\n");
 
234
        exit(1);
 
235
    }
 
236
 
 
237
    node_k_cv = xmlSecEncDataAddCipherValue(encKey);    
 
238
    if(node_k_cv == NULL) {
 
239
        fprintf(stderr, "Error: failed to add CipherValue node to EncryptedKey \n");
 
240
        exit(1);
 
241
    }
 
242
 
 
243
    node_k_ki = xmlSecEncDataAddKeyInfo(encKey);
 
244
    if(node_k_ki == NULL) {
 
245
        fprintf(stderr, "Error: failed to add KeyInfo node to EncryptedKey\n");
 
246
        exit(1);
 
247
    }
 
248
 
 
249
    node_k_kn = xmlSecKeyInfoAddKeyName(node_k_ki);
 
250
    if(node_k_kn == NULL) {
 
251
        fprintf(stderr, "Error: failed to add KeyName node to EncryptedKey\n");
 
252
        exit(1);
 
253
    }
 
254
}
 
255
 
 
256
void parseXML(struct TestData *td, unsigned long iter) {
 
257
    if (doc) {
 
258
        xmlFreeDoc(doc);    
 
259
    }
 
260
 
 
261
    doc=xmlParseMemory(td->xml,td->xmllen);
 
262
    if (!doc) {
 
263
        fprintf(stderr,"Error parsing document!\n");
 
264
        exit(1);
 
265
    }
 
266
}
 
267
 
 
268
xmlChar *mem;
 
269
int memsize;
 
270
 
 
271
void signXML(struct TestData *td, unsigned long iter) { 
 
272
    xmlNodePtr node;
 
273
    xmlSecDSigResultPtr result;
 
274
    
 
275
    node=xmlCopyNode(signature,1);
 
276
 
 
277
    if (!node) {
 
278
        fprintf(stderr,"Error: failed copy Signature Node!\n");
 
279
        exit(1);
 
280
    }
 
281
    
 
282
    if(xmlAddChild(xmlDocGetRootElement(doc), node) == NULL) {
 
283
        fprintf(stderr,"Error: failed to add Signature into the Document!\n");
 
284
        exit(1);
 
285
    }
 
286
    
 
287
    if (xmlSecDSigGenerate(dsigCtx, NULL, key, node, &result)<0) {
 
288
        fprintf(stderr,"Error: signature failed\n");
 
289
        exit(1);
 
290
    }     
 
291
 
 
292
    xmlSecDSigResultDestroy(result);
 
293
 
 
294
/*    
 
295
    if (iter==td->iterations) {
 
296
        xmlDocDumpMemory(doc,&mem,&memsize);
 
297
        puts(mem);
 
298
        free(mem);
 
299
    }
 
300
*/
 
301
}
 
302
 
 
303
void verifyXML(struct TestData *td, unsigned long iter) { 
 
304
    xmlNodePtr node;
 
305
    xmlSecDSigResultPtr result;
 
306
 
 
307
    node = xmlSecFindNode(xmlDocGetRootElement(doc), BAD_CAST "Signature", xmlSecDSigNs);
 
308
    if(node == NULL) {
 
309
        fprintf(stderr,"Error: failed to find Signature node in Document\n");
 
310
        exit(1);
 
311
    }    
 
312
     
 
313
    if (xmlSecDSigValidate(vdsigCtx, NULL, crt, node, &result)<0) {
 
314
        fprintf(stderr,"Error: Verification failed (processing error)\n");
 
315
        exit(1);
 
316
    }     
 
317
    
 
318
    if (result->result!=xmlSecTransformStatusOk) {
 
319
        fprintf(stderr,"Error: Verification failed (verification error)\n");
 
320
        exit(1);
 
321
    }
 
322
    
 
323
//    xmlSecDSigResultDebugDump(result, stdout); 
 
324
    
 
325
    xmlSecDSigResultDestroy(result);
 
326
}
 
327
 
 
328
/* In case if EncryptedData "type" attribute isn't specified!
 
329
 
 
330
void encryptXML(struct TestData *td, unsigned long iter) { 
 
331
    xmlDocPtr doctmp;
 
332
    xmlNodePtr node,enode;
 
333
    xmlSecEncResultPtr result = 0;
 
334
    
 
335
    node=xmlCopyNode(enc,1);
 
336
    enode=xmlDocGetRootElement(doc);
 
337
 
 
338
        // If this 2 commands place in other direction, - not all subnodes
 
339
        // of node will attached to right doc. Why?
 
340
    xmlSetTreeDoc(node,doc);
 
341
    xmlDocSetRootElement(doc, node);
 
342
 
 
343
    if (xmlSecEncryptXmlNode(encCtx, NULL, skey, node, enode, &result)<0) {
 
344
        fprintf(stderr, "Error: Encryption Failed!\n");
 
345
        exit(1);
 
346
    }
 
347
    xmlSecEncResultDestroy(result);
 
348
    xmlFreeNode(enode);
 
349
}
 
350
*/
 
351
 
 
352
void encryptXML(struct TestData *td, unsigned long iter) { 
 
353
    xmlDocPtr doctmp;
 
354
    xmlNodePtr node,enode;
 
355
    xmlSecEncResultPtr result = 0;
 
356
    
 
357
    node=xmlCopyNode(enc,1);
 
358
    enode=xmlDocGetRootElement(doc);
 
359
    xmlAddChild(enode,node);
 
360
 
 
361
    if (xmlSecEncryptXmlNode(encCtx, NULL, skey, node, enode, &result)<0) {
 
362
        fprintf(stderr, "Error: Encryption Failed!\n");
 
363
        exit(1);
 
364
    }
 
365
    xmlSecEncResultDestroy(result);
 
366
 
 
367
/*    
 
368
    if (iter==td->iterations) {
 
369
        xmlDocDumpMemory(doc,&mem,&memsize);
 
370
        puts(mem);
 
371
        free(mem);
 
372
    }
 
373
*/
 
374
}
 
375
 
 
376
void decryptXML(struct TestData *td, unsigned long iter) { 
 
377
    xmlNodePtr node;
 
378
    xmlSecEncResultPtr result = 0;
 
379
 
 
380
    if (xmlSecDecrypt(decCtx, NULL, NULL, xmlDocGetRootElement(doc), &result)<0) {
 
381
        fprintf(stderr, "Error: Decryption Failed!\n");
 
382
        exit(1);
 
383
    }
 
384
    
 
385
    node = xmlSecFindNode(xmlDocGetRootElement(doc), BAD_CAST "EncryptedData", xmlSecEncNs);    
 
386
    if (node==NULL) {
 
387
        fprintf(stderr,"Error: Can't find EncryptedData node\n");
 
388
        exit(0);
 
389
    }
 
390
    xmlUnlinkNode(node);
 
391
    xmlFreeNode(node);
 
392
 
 
393
/*  
 
394
    if (iter==td->iterations) {
 
395
        xmlDocDumpMemory(doc,&mem,&memsize);
 
396
        puts(mem);
 
397
        free(mem);
 
398
    }
 
399
*/
 
400
 
 
401
}
 
402
 
 
403
int main(int argc, char *argv[]) {
 
404
    return Test(argc,argv);
 
405
}