[xmlsec] xmlSecAddAndGenerateId

nitin singh xml_prgrm at yahoo.com
Fri Nov 5 16:12:44 PST 2004


Hello,
I copied the code from examples given in xmlsec1-1.2.6
nothing has been changed except one additional API
call.
following is the encrypt_file function.
Below is the xml file and the output.
I am using linux.
-----------------------------------
int 
encrypt_file(const char* xml_file, const char*
key_file) {
    xmlDocPtr doc = NULL;
    xmlNodePtr encDataNode = NULL;
    xmlNodePtr keyInfoNode = NULL;
    xmlSecEncCtxPtr encCtx = NULL;
    int res = -1;
    
    assert(xml_file);
    assert(key_file);

    /* load template */
    doc = xmlParseFile(xml_file);
    if ((doc == NULL) || (xmlDocGetRootElement(doc) ==
NULL)){
	fprintf(stderr, "Error: unable to parse file
\"%s\"\n", xml_file);
	goto done;	
    }
    
    /* create encryption template to encrypt XML file
and replace 
     * its content with encryption result */
    encDataNode = xmlSecTmplEncDataCreate(doc,
xmlSecTransformDes3CbcId,
				NULL, xmlSecTypeEncElement, NULL, NULL);
    if(encDataNode == NULL) {
	fprintf(stderr, "Error: failed to create encryption
template\n");
	goto done;   
    }

    /* we want to put encrypted data in the
<enc:CipherValue/> node */
    if(xmlSecTmplEncDataEnsureCipherValue(encDataNode)
== NULL) {
	fprintf(stderr, "Error: failed to add CipherValue
node\n");
	goto done;   
    }

    /* add <dsig:KeyInfo/> and <dsig:KeyName/> nodes
to put key name in the signed document */
    keyInfoNode =
xmlSecTmplEncDataEnsureKeyInfo(encDataNode, NULL);
    if(keyInfoNode == NULL) {
	fprintf(stderr, "Error: failed to add key info\n");
	goto done;		
    }

    if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL)
== NULL) {
	fprintf(stderr, "Error: failed to add key name\n");
	goto done;		
    }

    /* create encryption context, we don't need keys
manager in this example */
    encCtx = xmlSecEncCtxCreate(NULL);
    if(encCtx == NULL) {
        fprintf(stderr,"Error: failed to create
encryption context\n");
	goto done;
    }

    /* load DES key, assuming that there is not
password */
    encCtx->encKey =
xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
    if(encCtx->encKey == NULL) {
        fprintf(stderr,"Error: failed to load des key
from binary file \"%s\"\n", key_file);
	goto done;
    }

    /* set key name to the file name, this is just an
example! */
    if(xmlSecKeySetName(encCtx->encKey, BAD_CAST
key_file) < 0) {
    	fprintf(stderr,"Error: failed to set key name for
key from \"%s\"\n", key_file);
	goto done;
    }

   
if(xmlSecGenerateAndAddID(encDataNode,BAD_CAST"Id",BAD_CAST"enc",4)<0)
{
      fprintf(stderr,"Unable to add id \n");
      goto done;
    }

    /* encrypt the data */
    if(xmlSecEncCtxXmlEncrypt(encCtx, encDataNode,
xmlDocGetRootElement(doc)) < 0) {
        fprintf(stderr,"Error: encryption failed\n");
	goto done;
    }
    
    /* we template is inserted in the doc */
    encDataNode = NULL;
        
    /* print encrypted data with document to stdout */
    xmlDocDump(stdout, doc);
    
    /* success */
    res = 0;

done:    

    /* cleanup */
    if(encCtx != NULL) {
	xmlSecEncCtxDestroy(encCtx);
    }

    if(encDataNode != NULL) {
	xmlFreeNode(encDataNode);
    }
        
    if(doc != NULL) {
	xmlFreeDoc(doc); 
    }
    return(res);
}
----------------------------
Xml file used 
------------------------
 <?xml version="1.0" encoding="UTF-8"?>
<!-- 
XML Security Library example: Original XML doc file
before encryption (encrypt2 example). 
-->
<Envelope xmlns="urn:envelope">
  <Data>
	Hello, World!
  </Data>
</Envelope>

-------------------------
output
--------------------
./test mysample.xml deskeyvar1.bin
<?xml version="1.0" encoding="UTF-8"?>
<!--
XML Security Library example: Original XML doc file
before encryption (encrypt2 example).
-->
<EncryptedData
xmlns="http://www.w3.org/2001/04/xmlenc#"
Type="http://www.w3.org/2001/04/xmlenc#Element"
Id="?B?B">
<EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>deskeyvar1.bin</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>P64J/YL8I+r30mwR47XPkT62WFYA42qaBKQI4SfUyHSxJYSPT/pw3EvBNTa6qD0V
VEwnNL3tjd+VdGSfhlRuR8VDfOiE5mEh+jR93hMC04NwCHYU6Li2Eg==</CipherValue>
</CipherData>
</EncryptedData>


-----------------------
regards
Nitin
--- Aleksey Sanin <aleksey at aleksey.com> wrote:

> Hm... Seems to be working fine for me. Which OS do
> you use?
> Are you sure you have initialized everything
> correctly?
> Can you create a small reproducible test case?
> 
> Aleksey
> 



		
__________________________________ 
Do you Yahoo!? 
Check out the new Yahoo! Front Page. 
www.yahoo.com 
 




More information about the xmlsec mailing list