Please I want to know how sign and encryption without template file.
Example code in C:\xmlsec-0.0.7\docs\examples\enc1 doesn't work well.
And that example code encrypt for memory data.
so I edit code and it sucessfully compile without error, but it does not work well.
Follow code is reference in C:\xmlsec-0.0.7\apps\xmlsec.c and C:\xmlsec-0.0.7\docs\examples\enc1.
//////////////////////Execute Result/////////////////////////////////
Call Init()
**********xmlSecEncCtxCreate()***********
Call readKeys()
startLoad : c:\test\Debug\des.key
return value:0
Call xmlSecParseFile()
Call encrypt()
********Start encrypt()*********
********xmlSecEncryptUri()*********
xmlSecEvpCipherFinal: evp cipher final failed
xmlSecCipherTransformFlush: cipher final failed
xmlSecCipherTransformFlush: next transform flush failed
xmlSecCipherValueNodeRead: failed to finalize encryption
xmlSecCipherDataNodeRead: failed to read CipherValue node
xmlSecDecrypt: failed to get CipherData node content
xmlSecEncryptedKeyNodeRead: node decrypt failed
********xmlDocDumpMemoryEnc() tmpl *********
xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.
Error: failed to dump document to memory
********xmlSecEncResultDestroy()*********
********End encrypt()*********
Call shutdown()
Press any key to continue
/////////////////////////////////////////////////////////////////////////
Please advice to me what is wrong.
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#ifndef XMLSEC_NO_XSLT
#include <libxslt/xslt.h>
#include <libxslt/extensions.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/xsltutils.h>
#include <libexslt/exslt.h>
#endif /* XMLSEC_NO_XSLT */
#include <xmlsec/xmlsec.h>
#include <xmlsec/xmltree.h>
#include <xmlsec/keys.h>
#include <xmlsec/keysmngr.h>
#include <xmlsec/transforms.h>
#include <xmlsec/xmldsig.h>
#include <xmlsec/xmlenc.h>
#include <xmlsec/debug.h>
#define KEY_FILE "c:\\test\\Debug\\des.key"
#define SRC_TYPE 1 //0:xml 1:binary
#define SRC_FILE "c:\\test\\Debug\\test.txt"
//#define TMPL_FILE "c:\\test\\Debug\\enc-des3-test.tmpl"
#define OUT_ENC "c:\\test\\Debug\\enc-test-des.xml"
#define OUT_DEC "c:\\test\\Debug\\after-dec.txt"
/**
* Init/Shutdown
*/
int init(void);
int createTmpl(void);
void shutdown(void);
int readKeys(char *file);
char *data = "NULL;
int type=1; //1:encrypt 2:decrypt
xmlSecEncCtxPtr encCtx = NULL;
int encrypt(void);
int decrypt(xmlDocPtr doc);
/**
* Global data
*/
xmlSecKeysMngrPtr keyMgr = NULL;
xmlSecKeyPtr sessionKey = NULL;
xmlNodePtr encData=NULL;
char *nodeId = NULL;
char *nodeName = NULL;
char *nodeNs = NULL;
int repeats = 1;
int printResult = 0;
clock_t total_time = 0;
char *global_pwd = NULL;
int main(int argc, char **argv)
{
xmlDocPtr doc = NULL;
int ret;
printf("Call Init()\n");
ret = init();
if(ret < 0)
{
fprintf(stdout, "Error: init failed\n");
goto done;
}
printf("Call readKeys()\n");
ret = readKeys(KEY_FILE);
data = "SRC_FILE;
if(ret<0)
{
fprintf(stdout, "Error: read keys \n");
goto done;
}
printf("Call xmlSecParseFile()\n");
//doc = xmlSecParseFile(TM);
ret=createTmpl();
if(ret<0)
{
fprintf(stdout, "Error: create Template Object\n");
goto done;
}
/* if(doc == NULL)
{
fprintf(stderr, "Error: failed to read XML file\n");
goto done;
}*/
switch(type)
{
case 1:
printf("Call encrypt()\n");
ret = encrypt();
break;
case 2:
printf("Call decrypt()\n");
ret = decrypt(doc);
break;
}
if(ret < 0)
{
fprintf(stderr, "Error: operation failed\n");
goto done;
}
done:
if(doc != NULL)
{
printf("Call xmlFreeDoc()\n");
xmlFreeDoc(doc);
}
printf("Call shutdown()\n");
shutdown();
return 1;
}
int createTmpl(void)
{
xmlNodePtr encKey = NULL;
xmlNodePtr cur;
xmlDocPtr doc = NULL;
encData = xmlSecEncDataCreate(NULL, NULL, NULL, NULL);
if(encData == NULL)
{
fprintf(stderr, "Error: template creation failed\n");
goto done;
}
cur = xmlSecEncDataAddEncMethod(encData, xmlSecEncDes3Cbc);
if(cur == NULL)
{
fprintf(stderr, "Error: failed to add Enc Method\n");
goto done;
}
cur = xmlSecEncDataAddCipherValue(encData);
if(cur == NULL)
{
fprintf(stderr, "Error: failed to add CipherValue\n");
goto done;
}
cur = xmlSecEncDataAddKeyInfo(encData);
if(cur == NULL)
{
fprintf(stderr, "Error: failed to add KeyInfo\n");
goto done;
}
encKey = xmlSecKeyInfoAddEncryptedKey(cur, NULL, NULL, NULL);
if(encKey == NULL)
{
fprintf(stderr, "Error: failed to add EncryptedKey\n");
goto done;
}
/**
* Set the encryption method for encrypting the key
*/
cur = xmlSecEncDataAddEncMethod(encKey, xmlSecEncDes3Cbc);
if(cur == NULL)
{
fprintf(stderr, "Error: failed to add EncryptedKey Enc Method\n");
goto done;
}
cur = xmlSecEncDataAddCipherValue(encKey);
if(cur == NULL)
{
fprintf(stderr, "Error: failed to add EncryptedKey CipherValue\n");
goto done;
}
cur = xmlSecEncDataAddKeyInfo(encKey);
if(cur == NULL)
{
fprintf(stderr, "Error: failed to add EncryptedKey KeyInfo\n");
goto done;
}
cur = xmlSecKeyInfoAddKeyName(cur);
if(cur == NULL)
{
fprintf(stderr, "Error: failed to add EncryptedKey KeyName\n");
goto done;
}
done:
return(0);
}
int init(void)
{
time_t t = 0;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
time(&t);
while (RAND_status() != 1)
{
RAND_seed(&t, sizeof(t));
}
xmlInitParser();
LIBXML_TEST_VERSION
xmlSecInit();
keyMgr = xmlSecSimpleKeysMngrCreate();
if(keyMgr == NULL)
{
fprintf(stderr, "Error: failed to create keys manager\n");
return(-1);
}
printf("**********xmlSecEncCtxCreate()***********\n");
encCtx = xmlSecEncCtxCreate(keyMgr);
if(encCtx == NULL)
{
fprintf(stderr,"Error: failed to create Enc context\n");
return(-1);
}
return(0);
}
void shutdown(void)
{
/* destroy xmlsec objects */
if(encCtx != NULL)
{
xmlSecEncCtxDestroy(encCtx);
}
if(keyMgr != NULL)
{
xmlSecSimpleKeysMngrDestroy(keyMgr);
}
if(encData !=NULL)
{
xmlSecEncDataDestroy(encData);
}
xmlSecShutdown();
xsltCleanupGlobals();
xmlCleanupParser();
RAND_cleanup();
ERR_clear_error();
}
int readKeys(char *file)
{
int ret=0;
printf("startLoad : %s\n",file);
ret = xmlSecSimpleKeysMngrLoad(keyMgr,file, 0);
printf("return value:%d\n",ret);
if(ret < 0)
{
fprintf(stderr, "Error: failed to load keys from \"%s\".\n", file);
return(-1);
}
return(0);
}
int encrypt(void)
{
xmlSecEncResultPtr encResult = NULL;
xmlChar *result = NULL;
xmlDocPtr doc = NULL;
FILE *fp;
int len;
int ret;
int res = -1;
printf("********Start encrypt()*********\n");
if(SRC_TYPE && (data != NULL) && encCtx !=NULL && encData !=NULL)
{
printf("********xmlSecEncryptUri()*********\n");
ret = xmlSecEncryptUri(encCtx, NULL, NULL, encData, data, &encResult);
if(ret < 0)
{
fprintf(stderr,"Error: xmlSecEncryptUri() failed \n");
goto done;
}
}
else if(!SRC_TYPE && (data != NULL))
{
xmlNodePtr cur;
printf("********xmlParseFile()*********\n");
doc = xmlParseFile(data);
if (doc == NULL)
{
fprintf(stderr, "Error: unable to parse file \"%s\"\n", data);
goto done;
}
if(nodeId != NULL)
{
printf("********xmlSecFindNodeById()*********\n");
cur = xmlSecFindNodeById(encData, BAD_CAST nodeId);
}
else if(nodeName != NULL)
{
printf("********xmlSecFindNode()*********\n");
cur = xmlSecFindNode(encData, BAD_CAST nodeName, BAD_CAST nodeNs);
}
else
{
printf("********xmlDocGetRootElement()*********\n");
cur = encData;
}
if(cur == NULL)
{
fprintf(stderr,"Error: empty document for file \"%s\" or unable to find node\n", data);
goto done;
}
printf("********xmlSecEncryptXmlNode()*********\n");
ret = xmlSecEncryptXmlNode(encCtx, NULL, sessionKey,encData, cur, &encResult);
if(ret < 0)
{
fprintf(stderr,"Error: xmlSecEncryptXmlNode() failed \n");
goto done;
}
}
else
printf("Error: haha\n");
if((encResult != NULL) && (encResult->replaced) && (doc != NULL))
{
printf("********xmlDocDumpMemoryEnc() doc *********\n");
xmlDocDumpMemoryEnc(doc, &result, &len, NULL);
}
else
{
printf("********xmlDocDumpMemoryEnc() tmpl *********\n");
xmlDocDumpMemoryEnc(encData->doc, &result, &len, NULL);
}
if(result == NULL) {
fprintf(stderr,"Error: failed to dump document to memory\n");
goto done;
}
if((fp=fopen(OUT_ENC,"w"))==NULL)
printf("Failure to file open\n");
else
fwrite(result, len, 1, fp);
done:
if(doc != NULL)
{
printf("********xmlFreeDoc()*********\n");
xmlFreeDoc(doc);
}
if(result != NULL)
{
printf("********xmlFree()*********\n");
xmlFree(result);
}
if(encResult != NULL)
{
printf("********xmlSecEncResultDestroy()*********\n");
xmlSecEncResultDestroy(encResult);
}
printf("********End encrypt()*********\n");
return 1;
}
int decrypt(xmlDocPtr doc)
{
xmlSecEncResultPtr encResult = NULL;
xmlNodePtr cur;
FILE *fp;
int ret;
printf("********Start decrypt()*********\n");
printf("********xmlSecFindNode()*********\n");
cur = xmlSecFindNode(encData, BAD_CAST "EncryptedData", xmlSecEncNs);
if(cur == NULL)
{
fprintf(stderr,"Error: unable to find EncryptedData node\n");
goto done;
}
printf("********xmlSecDecrypt()*********\n");
ret = xmlSecDecrypt(encCtx, NULL, NULL, cur, &encResult);
if(ret < 0)
{
fprintf(stderr,"Error: xmlSecDecrypt() failed \n");
goto done;
}
if((encResult != NULL) && encResult->replaced && (encResult->buffer != NULL))
{
printf("********xmlDocDump()*********\n");
ret = xmlDocDump(stdout, doc);
}
else if((encResult != NULL) && !encResult->replaced)
{
printf("****fwrite(xmlBufferContent())****\n");
if((fp=fopen(OUT_DEC,"w"))==NULL)
printf("Failure to file open\n");
else
ret = fwrite(xmlBufferContent(encResult->buffer), xmlBufferLength(encResult->buffer),1, fp);
}
else
{
fprintf(stderr,"Error: bad results \n");
goto done;
}
if(ret < 0)
{
fprintf(stderr,"Error: failed to print out the result \n");
goto done;
}
done:
if(encResult != NULL)
{
printf("********xmlSecEncResultDestroy()*********\n");
xmlSecEncResultDestroy(encResult);
}
printf("********End decrypt()*********\n");
return 1;
}
---------------------------------------
�������� 100% ����ç ä��
http://chat.freechal.com/
