5
#include <libxml/xmlmemory.h>
6
#include <libxml/tree.h>
7
#include <libxml/parser.h>
9
#include <openssl/err.h>
10
#include <openssl/rand.h>
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>
27
#define keyfile "../ssl/test.key"
28
#define crtfile "../ssl/test.crt"
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;
39
xmlSecKeyPtr key,crt,skey;
41
void initXML(struct TestData *td) {
45
void releaseXML(struct TestData *td) {
48
xmlFreeNode(signature);
50
xmlSecDSigCtxDestroy(dsigCtx);
51
xmlSecKeysMngrDestroy(keysMngr);
52
xmlSecDSigCtxDestroy(vdsigCtx);
53
xmlSecKeysMngrDestroy(vkeysMngr);
55
xmlSecEncCtxDestroy(encCtx);
56
xmlSecEncCtxDestroy(decCtx);
58
xmlSecCryptoShutdown();
59
xmlSecCryptoAppShutdown();
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;
74
/* Init xmlsec library, could complain on I/O error, but works */
75
if(xmlSecInit() < 0) {
76
fprintf(stderr, "Error: xmlsec initialization failed.\n");
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");
86
#endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
88
/* Init crypto library */
89
if(xmlSecCryptoAppInit(NULL) < 0) {
90
fprintf(stderr, "Error: crypto initialization failed.\n");
94
/* Init xmlsec-crypto library */
95
if(xmlSecCryptoInit() < 0) {
96
fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
100
/* Initialising XML Security */
102
keysMngr = load_rsa_keys(keyfile,crtfile,"rsakey");
103
if(keysMngr == NULL) {
104
fprintf(stderr, "Error: loading private key!\n");
108
vkeysMngr = load_rsa_keys(crtfile,NULL,"rsapubkey");
109
if(vkeysMngr == NULL) {
110
fprintf(stderr, "Error: loading public key!\n");
114
// keysMngr->allowedOrigins = xmlSecKeyOriginKeyManager | xmlSecKeyOriginKeyName;
115
// vkeysMngr->allowedOrigins = xmlSecKeyOriginKeyManager | xmlSecKeyOriginKeyName;
118
dsigCtx = xmlSecDSigCtxCreate(keysMngr);
119
if(dsigCtx == NULL) {
120
fprintf(stderr,"Error: failed to create dsig context\n");
124
vdsigCtx = xmlSecDSigCtxCreate(vkeysMngr);
125
if(dsigCtx == NULL) {
126
fprintf(stderr,"Error: failed to create dsig context\n");
130
encCtx = xmlSecEncCtxCreate(vkeysMngr);
132
fprintf(stderr, "Error: failed to create encryption context\n");
136
decCtx = xmlSecEncCtxCreate(keysMngr);
138
fprintf(stderr, "Error: failed to create encryption context\n");
142
/* Creating symmetric Triple DES key (24 * 8 = 192) */
143
skey = xmlSecKeyGenerate(xmlSecKeyDataDesId, 192, xmlSecKeyDataTypeSession);
145
fprintf(stderr, "Error: failed to create des3 key (init structure)\n");
149
// encCtx->encKey,name = xmlStrdup(BAD_CAST "skey");
151
/* Creating Signature */
153
#ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
154
signature = xmlSecTmplSignatureCreate(NULL, xmlSecTransformExclC14NId, xmlSecTransformRsaSha1Id, NULL);
156
signature = xmlSecTmplSignatureCreate(NULL, xmlSecTransformExclC14NId, xmlSecOpenSSLTransformRsaSha1Id, NULL);
159
fprintf(stderr, "Error: Signature node creation failed\n");
163
r_node = xmlSecTmplSignatureAddReference(signature, xmlSecTransformSha1Id, NULL, NULL, NULL);
165
fprintf(stderr, "Error: Reference node creation failed\n");
169
if(xmlSecTmplReferenceAddTransform(r_node, xmlSecTransformEnvelopedId) == NULL) {
170
fprintf(stderr, "Error: failed to add enveloped transform to reference\n");
175
/* Creating encryption Node */
176
enc = xmlSecTmplEncDataCreate(NULL, xmlSecTransformDes3CbcId, NULL, xmlSecTypeEncElement, NULL, NULL);
178
fprintf(stderr, "Error: EncryptedData node creation failed\n");
182
if(xmlSecTmplEncDataEnsureCipherValue(enc) == NULL) {
183
fprintf(stderr, "Error: failed to add CipherValue node\n");
187
ki_node = xmlSecTmplEncDataEnsureKeyInfo(enc, NULL);
188
if(ki_node == NULL) {
189
fprintf(stderr, "Error: failed to add key info\n");
193
#ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
194
encKey = xmlSecTmplKeyInfoAddEncryptedKey(ki_node, xmlSecTransformRsaPkcs1Id, NULL, NULL, NULL);
196
encKey = xmlSecTmplKeyInfoAddEncryptedKey(ki_node, xmlSecOpenSSLTransformRsaPkcs1Id, NULL, NULL, NULL);
199
fprintf(stderr, "Error: failed to add key info\n");
203
if(xmlSecTmplEncDataEnsureCipherValue(encKey) == NULL) {
204
fprintf(stderr, "Error: failed to add CipherValue node\n");
208
node_k_ki = xmlSecTmplEncDataEnsureKeyInfo(encKey, NULL);
209
if(node_k_ki == NULL) {
210
fprintf(stderr, "Error: failed to add key info\n");
214
if(xmlSecTmplKeyInfoAddKeyName(node_k_ki, BAD_CAST "rsapubkey") == NULL) {
215
fprintf(stderr, "Error: failed to add key name\n");
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");
227
void parseXML(struct TestData *td, unsigned long iter) {
232
doc=xmlParseMemory(td->xml,td->xmllen);
234
fprintf(stderr,"Error parsing document!\n");
242
void signXML(struct TestData *td, unsigned long iter) {
245
node=xmlCopyNode(signature,1);
248
fprintf(stderr,"Error: failed copy Signature Node!\n");
252
if(xmlAddChild(xmlDocGetRootElement(doc), node) == NULL) {
253
fprintf(stderr,"Error: failed to add Signature into the Document!\n");
257
if(xmlSecDSigCtxSign(dsigCtx, node) < 0) {
258
fprintf(stderr,"Error: signature failed\n");
262
xmlSecDSigCtxFinalize(dsigCtx);
263
if (xmlSecDSigCtxInitialize(dsigCtx,keysMngr)<0) {
264
fprintf(stderr,"Error: failed to reinitialize Sign Signature Context\n");
269
if (iter==td->iterations) {
270
xmlDocDumpMemory(doc,&mem,&memsize);
277
void verifyXML(struct TestData *td, unsigned long iter) {
280
node = xmlSecFindNode(xmlDocGetRootElement(doc), BAD_CAST "Signature", xmlSecDSigNs);
282
fprintf(stderr,"Error: failed to find Signature node in Document\n");
286
if(xmlSecDSigCtxVerify(vdsigCtx, node) < 0) {
287
fprintf(stderr,"Error: signature verify\n");
291
if(vdsigCtx->status != xmlSecDSigStatusSucceeded) {
292
fprintf(stdout, "Signature is INVALID\n");
296
xmlSecDSigCtxFinalize(vdsigCtx);
297
if (xmlSecDSigCtxInitialize(vdsigCtx,vkeysMngr)<0) {
298
fprintf(stderr,"Error: failed to reinitialize Verify Signature Context\n");
304
void encryptXML(struct TestData *td, unsigned long iter) {
306
xmlNodePtr node,enode;
308
node=xmlCopyNode(enc,1);
309
enode=xmlDocGetRootElement(doc);
310
xmlAddChild(enode,node);
312
if(xmlSecEncCtxXmlEncrypt(encCtx, node, enode) < 0) {
313
fprintf(stderr,"Error: encryption failed\n");
318
xmlSecEncCtxFinalize(encCtx);
319
if (xmlSecEncCtxInitialize(encCtx,vkeysMngr)<0) {
320
fprintf(stderr,"Error: failed to reinitialize Decrypt Context\n");
326
if (iter==td->iterations) {
327
xmlDocDumpMemory(doc,&mem,&memsize);
334
void decryptXML(struct TestData *td, unsigned long iter) {
337
if((xmlSecEncCtxDecrypt(decCtx, xmlDocGetRootElement(doc)) < 0) || (decCtx->result == NULL)) {
338
fprintf(stderr,"Error: decryption failed\n");
342
node = xmlSecFindNode(xmlDocGetRootElement(doc), BAD_CAST "EncryptedData", xmlSecEncNs);
344
fprintf(stderr,"Error: Can't find EncryptedData node\n");
350
xmlSecEncCtxFinalize(decCtx);
351
if (xmlSecEncCtxInitialize(decCtx,keysMngr)<0) {
352
fprintf(stderr,"Error: failed to reinitialize Decrypt Context\n");
356
if (iter==td->iterations) {
357
xmlDocDumpMemory(doc,&mem,&memsize);
364
int main(int argc, char *argv[]) {
365
return Test(argc,argv);