/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/libxml1.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
#include <xmlsec/crypto.h>
 
20
#include <xmlsec/templates.h>
 
21
 
 
22
 
 
23
#include "libxml1.h"
 
24
 
 
25
#include "tools.h"
 
26
 
 
27
#define keyfile "../ssl/test.key"
 
28
#define crtfile "../ssl/test.crt"
 
29
 
 
30
xmlDocPtr               doc = 0;
 
31
xmlSecKeysMngrPtr       keysMngr = 0; 
 
32
xmlSecKeysMngrPtr       vkeysMngr = 0; 
 
33
xmlSecDSigCtxPtr        dsigCtx = 0;
 
34
xmlSecDSigCtxPtr        vdsigCtx = 0;
 
35
xmlSecEncCtxPtr         encCtx = 0;
 
36
xmlSecEncCtxPtr         decCtx = 0;
 
37
xmlNodePtr              signature = 0;
 
38
xmlNodePtr              enc = 0;
 
39
xmlSecKeyPtr            key,crt,skey;
 
40
 
 
41
void initXML(struct TestData *td) {
 
42
    xmlInitParser();
 
43
}
 
44
 
 
45
void releaseXML(struct TestData *td) {
 
46
    xmlFreeDoc(doc);    
 
47
 
 
48
    xmlFreeNode(signature);
 
49
    
 
50
    xmlSecDSigCtxDestroy(dsigCtx);
 
51
    xmlSecKeysMngrDestroy(keysMngr);
 
52
    xmlSecDSigCtxDestroy(vdsigCtx);
 
53
    xmlSecKeysMngrDestroy(vkeysMngr);
 
54
    
 
55
    xmlSecEncCtxDestroy(encCtx);
 
56
    xmlSecEncCtxDestroy(decCtx);
 
57
 
 
58
    xmlSecCryptoShutdown();
 
59
    xmlSecCryptoAppShutdown();
 
60
    xmlSecShutdown();
 
61
 
 
62
    xmlCleanupParser();
 
63
}
 
64
 
 
65
 
 
66
xmlNodePtr      encKey;
 
67
 
 
68
void initXML_Security(struct TestData *td) {
 
69
    xmlNodePtr  si_node,cm_node,sm_node,r_node,dm_node,t_node;
 
70
    xmlNodePtr  em_node,cv_node,ki_node,kn_node;
 
71
    xmlNodePtr  node_k_em,node_k_cv,node_k_ki,node_k_kn;
 
72
    
 
73
 
 
74
    /* Init xmlsec library, could complain on I/O error, but works */
 
75
    if(xmlSecInit() < 0) {
 
76
        fprintf(stderr, "Error: xmlsec initialization failed.\n");
 
77
        exit(1);
 
78
    }
 
79
 
 
80
 
 
81
#ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
 
82
    if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
 
83
        fprintf(stderr, "Error: unable to load default xmlsec-crypto library.\n");
 
84
        exit(1);        
 
85
    }
 
86
#endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
 
87
 
 
88
    /* Init crypto library */
 
89
    if(xmlSecCryptoAppInit(NULL) < 0) {
 
90
        fprintf(stderr, "Error: crypto initialization failed.\n");
 
91
        exit(1);
 
92
    }
 
93
 
 
94
    /* Init xmlsec-crypto library */
 
95
    if(xmlSecCryptoInit() < 0) {
 
96
        fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
 
97
        exit(1);
 
98
    }
 
99
 
 
100
        /* Initialising XML Security */
 
101
 
 
102
    keysMngr = load_rsa_keys(keyfile,crtfile,"rsakey");
 
103
    if(keysMngr == NULL) {
 
104
        fprintf(stderr, "Error: loading private key!\n");
 
105
        exit(1);
 
106
    }
 
107
 
 
108
    vkeysMngr = load_rsa_keys(crtfile,NULL,"rsapubkey");
 
109
    if(vkeysMngr == NULL) {
 
110
        fprintf(stderr, "Error: loading public key!\n");
 
111
        exit(1);
 
112
    }
 
113
 
 
114
//    keysMngr->allowedOrigins = xmlSecKeyOriginKeyManager | xmlSecKeyOriginKeyName;
 
115
//    vkeysMngr->allowedOrigins = xmlSecKeyOriginKeyManager | xmlSecKeyOriginKeyName;
 
116
 
 
117
 
 
118
    dsigCtx = xmlSecDSigCtxCreate(keysMngr);
 
119
    if(dsigCtx == NULL) {
 
120
        fprintf(stderr,"Error: failed to create dsig context\n");
 
121
        exit(1);
 
122
    }                
 
123
 
 
124
    vdsigCtx = xmlSecDSigCtxCreate(vkeysMngr);
 
125
    if(dsigCtx == NULL) {
 
126
        fprintf(stderr,"Error: failed to create dsig context\n");
 
127
        exit(1);
 
128
    }                
 
129
 
 
130
    encCtx = xmlSecEncCtxCreate(vkeysMngr);
 
131
    if(encCtx == NULL) {
 
132
        fprintf(stderr, "Error: failed to create encryption context\n");
 
133
        exit(1);
 
134
    }
 
135
 
 
136
    decCtx = xmlSecEncCtxCreate(keysMngr);
 
137
    if(encCtx == NULL) {
 
138
        fprintf(stderr, "Error: failed to create encryption context\n");
 
139
        exit(1);
 
140
    }
 
141
 
 
142
        /* Creating symmetric Triple DES key (24 * 8 = 192) */    
 
143
    skey = xmlSecKeyGenerate(xmlSecKeyDataDesId, 192, xmlSecKeyDataTypeSession);
 
144
    if(skey == NULL) {
 
145
        fprintf(stderr, "Error: failed to create des3 key (init structure)\n"); 
 
146
        exit(1);
 
147
    }        
 
148
    encCtx->encKey=skey;
 
149
//    encCtx->encKey,name = xmlStrdup(BAD_CAST "skey");
 
150
 
 
151
        /* Creating Signature */    
 
152
 
 
153
#ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
 
154
    signature = xmlSecTmplSignatureCreate(NULL, xmlSecTransformExclC14NId, xmlSecTransformRsaSha1Id, NULL);
 
155
#else
 
156
    signature = xmlSecTmplSignatureCreate(NULL, xmlSecTransformExclC14NId, xmlSecOpenSSLTransformRsaSha1Id, NULL);
 
157
#endif
 
158
    if (!signature) {
 
159
        fprintf(stderr, "Error: Signature node creation failed\n");
 
160
        exit(1);
 
161
    }
 
162
 
 
163
    r_node = xmlSecTmplSignatureAddReference(signature, xmlSecTransformSha1Id, NULL, NULL, NULL);
 
164
    if (!r_node) {
 
165
        fprintf(stderr, "Error: Reference node creation failed\n");
 
166
        exit(1);
 
167
    }
 
168
 
 
169
    if(xmlSecTmplReferenceAddTransform(r_node, xmlSecTransformEnvelopedId) == NULL) {
 
170
        fprintf(stderr, "Error: failed to add enveloped transform to reference\n");
 
171
        exit(1);
 
172
    }
 
173
 
 
174
 
 
175
        /* Creating encryption Node */
 
176
    enc = xmlSecTmplEncDataCreate(NULL, xmlSecTransformDes3CbcId, NULL, xmlSecTypeEncElement, NULL, NULL);
 
177
    if(enc == NULL) {
 
178
        fprintf(stderr, "Error: EncryptedData node creation failed\n");
 
179
        exit(1);
 
180
    }
 
181
 
 
182
    if(xmlSecTmplEncDataEnsureCipherValue(enc) == NULL) {
 
183
        fprintf(stderr, "Error: failed to add CipherValue node\n");
 
184
        exit(1);
 
185
    }
 
186
 
 
187
    ki_node = xmlSecTmplEncDataEnsureKeyInfo(enc, NULL);
 
188
    if(ki_node == NULL) {
 
189
        fprintf(stderr, "Error: failed to add key info\n");
 
190
        exit(1);
 
191
    }
 
192
 
 
193
#ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
 
194
    encKey = xmlSecTmplKeyInfoAddEncryptedKey(ki_node, xmlSecTransformRsaPkcs1Id, NULL, NULL, NULL);
 
195
#else
 
196
    encKey = xmlSecTmplKeyInfoAddEncryptedKey(ki_node, xmlSecOpenSSLTransformRsaPkcs1Id, NULL, NULL, NULL);
 
197
#endif
 
198
    if(encKey == NULL) {
 
199
        fprintf(stderr, "Error: failed to add key info\n");
 
200
        exit(1);
 
201
    }
 
202
    
 
203
    if(xmlSecTmplEncDataEnsureCipherValue(encKey) == NULL) {
 
204
        fprintf(stderr, "Error: failed to add CipherValue node\n");
 
205
        exit(1);
 
206
    }
 
207
 
 
208
    node_k_ki = xmlSecTmplEncDataEnsureKeyInfo(encKey, NULL);
 
209
    if(node_k_ki == NULL) {
 
210
        fprintf(stderr, "Error: failed to add key info\n");
 
211
        exit(1);
 
212
    }
 
213
 
 
214
    if(xmlSecTmplKeyInfoAddKeyName(node_k_ki, BAD_CAST "rsapubkey") == NULL) {
 
215
        fprintf(stderr, "Error: failed to add key name\n");
 
216
        exit(1);
 
217
    }
 
218
 
 
219
/*    node_k_em = xmlSecEncDataAddEncMethod(encKey, xmlSecEncRsaPkcs1);
 
220
    if(node_k_em == NULL) {
 
221
        fprintf(stderr, "Error: failed to add EncryptedMethod node to EncryptedKey\n");
 
222
        exit(1);
 
223
    }*/
 
224
 
 
225
}
 
226
 
 
227
void parseXML(struct TestData *td, unsigned long iter) {
 
228
    if (doc) {
 
229
        xmlFreeDoc(doc);    
 
230
    }
 
231
 
 
232
    doc=xmlParseMemory(td->xml,td->xmllen);
 
233
    if (!doc) {
 
234
        fprintf(stderr,"Error parsing document!\n");
 
235
        exit(1);
 
236
    }
 
237
}
 
238
 
 
239
xmlChar *mem;
 
240
int memsize;
 
241
 
 
242
void signXML(struct TestData *td, unsigned long iter) { 
 
243
    xmlNodePtr node;
 
244
 
 
245
    node=xmlCopyNode(signature,1);
 
246
 
 
247
    if (!node) {
 
248
        fprintf(stderr,"Error: failed copy Signature Node!\n");
 
249
        exit(1);
 
250
    }
 
251
    
 
252
    if(xmlAddChild(xmlDocGetRootElement(doc), node) == NULL) {
 
253
        fprintf(stderr,"Error: failed to add Signature into the Document!\n");
 
254
        exit(1);
 
255
    }
 
256
 
 
257
    if(xmlSecDSigCtxSign(dsigCtx, node) < 0) {
 
258
        fprintf(stderr,"Error: signature failed\n");
 
259
        exit(1);
 
260
    }     
 
261
 
 
262
    xmlSecDSigCtxFinalize(dsigCtx);
 
263
    if (xmlSecDSigCtxInitialize(dsigCtx,keysMngr)<0) {
 
264
        fprintf(stderr,"Error: failed to reinitialize Sign Signature Context\n");
 
265
        exit(1);
 
266
    }              
 
267
 
 
268
/*    
 
269
    if (iter==td->iterations) {
 
270
        xmlDocDumpMemory(doc,&mem,&memsize);
 
271
        puts(mem);
 
272
        free(mem);
 
273
    }
 
274
*/
 
275
}
 
276
 
 
277
void verifyXML(struct TestData *td, unsigned long iter) { 
 
278
    xmlNodePtr node;
 
279
 
 
280
    node = xmlSecFindNode(xmlDocGetRootElement(doc), BAD_CAST "Signature", xmlSecDSigNs);
 
281
    if(node == NULL) {
 
282
        fprintf(stderr,"Error: failed to find Signature node in Document\n");
 
283
        exit(1);
 
284
    }    
 
285
 
 
286
    if(xmlSecDSigCtxVerify(vdsigCtx, node) < 0) {
 
287
        fprintf(stderr,"Error: signature verify\n");
 
288
        exit(1);
 
289
    }
 
290
 
 
291
    if(vdsigCtx->status != xmlSecDSigStatusSucceeded) {
 
292
        fprintf(stdout, "Signature is INVALID\n");
 
293
        exit(1);
 
294
    }    
 
295
 
 
296
    xmlSecDSigCtxFinalize(vdsigCtx);
 
297
    if (xmlSecDSigCtxInitialize(vdsigCtx,vkeysMngr)<0) {
 
298
        fprintf(stderr,"Error: failed to reinitialize Verify Signature Context\n");
 
299
        exit(1);
 
300
    }              
 
301
}
 
302
 
 
303
 
 
304
void encryptXML(struct TestData *td, unsigned long iter) { 
 
305
    xmlDocPtr doctmp;
 
306
    xmlNodePtr node,enode;
 
307
    
 
308
    node=xmlCopyNode(enc,1);
 
309
    enode=xmlDocGetRootElement(doc);
 
310
    xmlAddChild(enode,node);
 
311
 
 
312
    if(xmlSecEncCtxXmlEncrypt(encCtx, node, enode) < 0) {
 
313
        fprintf(stderr,"Error: encryption failed\n");
 
314
        exit(1);
 
315
    }
 
316
 
 
317
    encCtx->encKey=NULL;
 
318
    xmlSecEncCtxFinalize(encCtx);
 
319
    if (xmlSecEncCtxInitialize(encCtx,vkeysMngr)<0) {
 
320
        fprintf(stderr,"Error: failed to reinitialize Decrypt Context\n");
 
321
        exit(1);
 
322
    }              
 
323
    encCtx->encKey=skey;
 
324
 
 
325
/*
 
326
    if (iter==td->iterations) {
 
327
        xmlDocDumpMemory(doc,&mem,&memsize);
 
328
        puts(mem);
 
329
        free(mem);
 
330
    }
 
331
*/
 
332
}
 
333
 
 
334
void decryptXML(struct TestData *td, unsigned long iter) { 
 
335
    xmlNodePtr node;
 
336
 
 
337
    if((xmlSecEncCtxDecrypt(decCtx, xmlDocGetRootElement(doc)) < 0) || (decCtx->result == NULL)) {
 
338
        fprintf(stderr,"Error: decryption failed\n");
 
339
        exit(1);
 
340
    }
 
341
 
 
342
    node = xmlSecFindNode(xmlDocGetRootElement(doc), BAD_CAST "EncryptedData", xmlSecEncNs);    
 
343
    if (node==NULL) {
 
344
        fprintf(stderr,"Error: Can't find EncryptedData node\n");
 
345
        exit(0);
 
346
    }
 
347
    xmlUnlinkNode(node);
 
348
    xmlFreeNode(node);
 
349
 
 
350
    xmlSecEncCtxFinalize(decCtx);
 
351
    if (xmlSecEncCtxInitialize(decCtx,keysMngr)<0) {
 
352
        fprintf(stderr,"Error: failed to reinitialize Decrypt Context\n");
 
353
        exit(1);
 
354
    }              
 
355
/*
 
356
    if (iter==td->iterations) {
 
357
        xmlDocDumpMemory(doc,&mem,&memsize);
 
358
        puts(mem);
 
359
        free(mem);
 
360
    }
 
361
*/
 
362
}
 
363
 
 
364
int main(int argc, char *argv[]) {
 
365
    return Test(argc,argv);
 
366
}