[xmlsec] [xmlsec]Questions about template file

강정호 kjho80 at freechal.com
Tue Aug 13 03:23:47 PDT 2002


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;
}

---------------------------------------
신개념 종합 쇼핑몰, Buychal
http://www.buychal.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.aleksey.com/pipermail/xmlsec/attachments/20020813/2f96a77f/attachment.htm


More information about the xmlsec mailing list