[xmlsec] How doese xmlSecEncCtxXmlEncrypt() work?

Aleksey Sanin aleksey at aleksey.com
Fri Aug 10 12:50:17 PDT 2007


The node you've created

 > 	oNode = xmlNewNode(0, "AboutBoxText");

was not assigned to any document. Check LibXML2 tutorial
for more details on constructing XML DOM tree using
LibXML2 functions.

Aleksey

Martin Salo wrote:
> Hello Mailinglist,
> 
> I have a question about xmlSecEncCtxXmlEncrypt().
> I have created the EncryptionContext and the Template nodes like in encyrpt2.c example.
> Then I have created the nodes (subtree) I want to decrypt. The return value of the
> encryption is -1. And the node is not encrypted. Why?
> 
> Error Message:
> func=xmlSecEncCtxXmlEncrypt:file=..\src\xmlenc.c:line=366:obj=unknown:subj=node->doc != NULL:
> error=100:assertion: ;last error=0 (0x00000000);last error msg=The action was successfully.
> 
> I want to create this xml file, with the differnce that the <Server> node should be
> replaced with an encryption section:
> <?xml version="1.0"?>
> <ApplicationSettings>
> 	<AboutBoxText>MyApp 1.0</AboutBoxText>
> 	<Server>
> 		<Url>www.abc.com</Url>
> 		<Password>TopSecret</Password>
> 	</Server>
> </ApplicationSettings>
> 
> 
> // Replace the encrypt_file() function in encrypt2.c with this fucntion:
> int encrypt_file(const char* xml_file, const char* key_file) {
> 	xmlNode *oNode=0, *oNodeFather=0, *oNodeToEncrypt=0, *oNodeTmpl=0;
> 	xmlDoc *oNewDoc=0;
> 	xmlNs *oNsSig=0, *oNsEnc=0, *oNsXsi=0;
> 	xmlSecEncCtx *oEncCtx=0;
> 	int retval=0;
> 
> 	// Create new xml file
> 	oNewDoc = xmlNewDoc(BAD_CAST "1.0");
> 	oNodeFather = xmlNewDocNode(oNewDoc, 0, BAD_CAST "ApplicationSettings", 0);
> 	xmlDocSetRootElement(oNewDoc, oNodeFather);
> 
> 	// Add a plain node:
> 	oNode = xmlNewNode(0, "AboutBoxText");
> 	xmlNodeSetContent(oNode, "MyApp");
> 	xmlAddChild(oNodeFather, oNode);
> 	
> 	// Create nodes that should be encrypted:
> 	oNodeToEncrypt = xmlNewNode(0, "ToEncrypt");
> 	oNode = xmlSecAddChild(oNodeToEncrypt, "Server", 0);
> 	xmlNodeSetContent(oNode, "www.abc.com");
> 	oNode = xmlSecAddChild(oNodeToEncrypt, "Password", 0);
> 	xmlNodeSetContent(oNode, "TopSecret");
> 
> 	// Create encryption template nodes:
> 	oNodeTmpl = xmlSecTmplEncDataCreate(oNewDoc, xmlSecTransformDes3CbcId, 0, 0, 0, 0);
> 	xmlSecTmplEncDataEnsureCipherValue(oNodeTmpl);
> 	oNode = xmlSecTmplEncDataEnsureKeyInfo(oNodeTmpl, NULL);
> 	xmlSecTmplKeyInfoAddKeyName(oNode, NULL);
> 
> 	// Create encryption context:
> 	oEncCtx = xmlSecEncCtxCreate(NULL);
> 	oEncCtx->encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
> 	xmlSecKeySetName(oEncCtx->encKey, (const unsigned char *)key_file);
> 
> 	// Do encryption:
> 	retval = xmlSecEncCtxXmlEncrypt(oEncCtx, oNodeTmpl, oNodeToEncrypt);
> 	xmlAddChild(oNodeFather, oNodeToEncrypt);
> 
> 
> 
> 	// Print result:
> 	xmlDocDump(stdout, oNewDoc);
> 
> 
> 
> 	// Cleanup:
> 	if(oEncCtx != NULL)
> 		xmlSecEncCtxDestroy(oEncCtx);
> 	if(oNodeTmpl != NULL)
> 		xmlFreeNode(oNodeTmpl);
> 	if(oNewDoc != NULL)
> 		xmlFreeDoc(oNewDoc);
> 	
> 	return 0;
> }



More information about the xmlsec mailing list