[xmlsec] How to encrypt only some Subnodes? (2)

Martin Salo Martin.Salo at gmx.net
Thu Aug 2 08:57:44 PDT 2007


Hello Mailinglist,

I have looked through the mailing archive and found some hints. One was to look at xmlsec1. At this moment I dont get xmlsec1 running. But I found the function xmlSecAppEncryptTmpl(). I have created an example (see below). I use it in encrypt2.c instead of function encrypt_file().


// The Idea of this func is to create dynamicaly a complete new encryption template
// and to use this template to encrypt ONLY SOME sub nodes in the file of param xml_file.
int EncryptFile_New(const char* xml_file, const char* key_file) {
	xmlDocPtr doc=0, docTmpl=0;
	xmlNodePtr encDataNode=0, keyInfoNode=0, NodeToEncrypt=0;
	xmlSecEncCtxPtr encCtx=0;

	// Create dynamical a new template:
	docTmpl = xmlNewDoc(BAD_CAST "1.0");
	encDataNode = xmlSecTmplEncDataCreate(docTmpl, xmlSecTransformDes3CbcId, 0, 0, 0, 0);
	xmlDocSetRootElement(docTmpl, encDataNode);
	xmlSecTmplEncDataEnsureCipherValue(encDataNode);
	keyInfoNode = xmlSecTmplEncDataEnsureKeyInfo(encDataNode, NULL);
	xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL);

	// Load the key:
	encCtx = xmlSecEncCtxCreate(NULL);
	encCtx->encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
	xmlSecKeySetName(encCtx->encKey, (const unsigned char *)key_file);

	// Encrypt:
	doc = xmlParseFile(xml_file);
	// If I get this code running, I will replace the following code with a function
	// that searches for the sub nodes I want to encrypt.
	NodeToEncrypt = xmlDocGetRootElement(doc);
	xmlSecEncCtxXmlEncrypt(encCtx, encDataNode, NodeToEncrypt);
	xmlDocDump(stdout, doc);

	// Cleanup:
	encDataNode = NULL;
	if(encCtx != NULL)
		xmlSecEncCtxDestroy(encCtx);
	if(encDataNode != NULL)
		xmlFreeNode(encDataNode);
	if(doc != NULL)
		xmlFreeDoc(doc);

	return 0;
}

If I start it, I get this error:
#####################################
func=xmlSecEncCtxXmlEncrypt:file=..\src\xmlenc.c:line=417:obj=unknown:subj=unknown:error=14:invalid type:type=NULL;last error=1008 (0x000003f0);last error msg=Es wurde versucht, auf ein Token zuzugreifen, das nicht vorhanden ist.
#####################################
To translate the last message: There was a try to access a token that was not there.


Will this way work at all?
Can someone give me a hint what I'm doing wrong?


Regards

Martin

-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger



More information about the xmlsec mailing list