[xmlsec] Cannot get encryption to work on Win32
Jamshed Nawaz
jnawaz at usc.edu
Wed Mar 9 07:30:32 PST 2005
Can someone please provide a clue as to why I can't get the following code
to work on Windows (XP/2003server), but it works fine on Solaris. The
error I get:
func=xmlSecEncCtxXmlEncrypt:file=..\src\xmlenc.c:line=417:obj=unknown:subj=unknown:error=14:invalid type:type=ÿ%È'G
I used the win32 binaries:
xmlsec 1.2.6
libxml2 2.6.17
openssl 0.9.7e
iconv 1.9.1
I use the latest mingw compiler.
I tried using earlier versions but get the same error.
Here's my encrypt funtion (mostly based on the example):
------------------------------START CODE------------------------------
std::string encrypt()
{
int code = -1;
std::string msg("");
xmlChar *xml_secure = NULL;
int sz;
xmlDocPtr doc = NULL;
xmlNodePtr encDataNode = NULL;
xmlNodePtr keyInfoNode = NULL;
xmlSecEncCtxPtr encCtx = NULL;
/*assert(keys_mngr);
assert(xml_plain);*/
std::string priv_key = "symkey_test";
/* load template */
doc = xmlParseMemory("<?xml version=\"1.0\" encoding=\"UTF-8\"?><SomeXML/>",
strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?><SomeXML/>"));
if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)) {
std::cerr << "Error: unable to parse string [xmlParseMemory|xmlDocGetRootElement]\n";
goto CLEANUP_AFTER_ENCRYPT_;
}
/* create encryption template to encrypt XML file and replace
* its content with encryption result */
encDataNode = xmlSecTmplEncDataCreate(doc,
xmlSecTransformAes128CbcId,
NULL,
xmlSecTypeEncElement,
NULL,
NULL);
if(encDataNode == NULL) {
std::cerr << "Error: failed to create encryption template [xmlSecTmplEncDataCreate]\n";
goto CLEANUP_AFTER_ENCRYPT_;
}
/* we want to put encrypted data in the <enc:CipherValue/> node */
if(xmlSecTmplEncDataEnsureCipherValue(encDataNode) == NULL) {
std::cerr << "Error: failed to add CipherValue node [xmlSecTmplEncDataEnsureCipherValue]\n";
goto CLEANUP_AFTER_ENCRYPT_;
}
/* add <dsig:KeyInfo/> and <dsig:KeyName/> nodes to put key name
* in the signed document */
keyInfoNode = xmlSecTmplEncDataEnsureKeyInfo(encDataNode, NULL);
if(keyInfoNode == NULL) {
std::cerr << "Error: failed to add key info [xmlSecTmplEncDataEnsureKeyInfo]\n";
goto CLEANUP_AFTER_ENCRYPT_;
}
//"""""""""""""""""""""""//
// Provide name of key //
//"""""""""""""""""""""""//
if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode,
(xmlChar*)priv_key.c_str()) == NULL) {
std::cerr << "Error: failed to add symmetric key name [xmlSecTmplKeyInfoAddKeyName]\n";
goto CLEANUP_AFTER_ENCRYPT_;
}
/* create encryption context */
encCtx = xmlSecEncCtxCreate(keys_mngr);
if(encCtx == NULL) {
std::cerr << "Error: failed to create encryption context [xmlSecEncCtxCreate]\n";
goto CLEANUP_AFTER_ENCRYPT_;
}
/* encrypt the data */
if(xmlSecEncCtxXmlEncrypt(encCtx,
encDataNode,
xmlDocGetRootElement(doc)) < 0) {
std::cerr << "Error: encryption failed [xmlSecEncCtxXmlEncrypt]\n";
goto CLEANUP_AFTER_ENCRYPT_;
}
/* we template is inserted in the doc */
encDataNode = NULL;
xmlDocDumpMemory(doc, &xml_secure, &sz);
/* success */
code = 0;
CLEANUP_AFTER_ENCRYPT_:
if(encCtx != NULL)
xmlSecEncCtxDestroy(encCtx);
if(encDataNode != NULL)
xmlFreeNode(encDataNode);
if(doc != NULL)
xmlFreeDoc(doc);
std::string xml_secure_;
if (code<0 || xml_secure==NULL) {
throw XmlSecurityError("Error encrypting request data.");
}
else {
xml_secure_.assign( (char*)xml_secure );
free_(xml_secure);
}
return xml_secure_;
}
------------------------------END CODE------------------------------
--
Thanks,
Jamshed
More information about the xmlsec
mailing list