summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorValery Febvre <vfebvre at easter-eggs.com>2004-04-15 02:42:29 +0000
committerValery Febvre <vfebvre at easter-eggs.com>2004-04-15 02:42:29 +0000
commit83b36c2a0532661699345a49ed4ba15ebef2adad (patch)
tree4415e3e7c505fcf01350bc51b48927b5bdebbb3b
parent45bbe4077279f352938adcb0810c3c2e34fa8030 (diff)
downloadlasso-83b36c2a0532661699345a49ed4ba15ebef2adad.tar.gz
lasso-83b36c2a0532661699345a49ed4ba15ebef2adad.tar.xz
lasso-83b36c2a0532661699345a49ed4ba15ebef2adad.zip
Clean-ups, cosmetics and memory leaks fixed
-rw-r--r--lasso/xml/tools.c64
-rw-r--r--lasso/xml/tools.h28
-rw-r--r--lasso/xml/xml.c33
-rw-r--r--lasso/xml/xml.h4
4 files changed, 72 insertions, 57 deletions
diff --git a/lasso/xml/tools.c b/lasso/xml/tools.c
index 6f16b072..aedbf92a 100644
--- a/lasso/xml/tools.c
+++ b/lasso/xml/tools.c
@@ -60,6 +60,7 @@ lasso_doc_get_node_content(xmlDocPtr doc, const xmlChar *name)
{
xmlNodePtr node;
+ /* FIXME : bad namespace used */
node = xmlSecFindNode(xmlDocGetRootElement(doc), name, xmlSecDSigNs);
if (node != NULL)
/* val returned must be xmlFree() */
@@ -107,7 +108,7 @@ lasso_get_current_time()
GData *
lasso_query_to_dict(const xmlChar *query)
{
- GData *gd;
+ GData *gd = NULL;
gchar **sa1, **sa2, **sa3;
GPtrArray *gpa;
@@ -153,14 +154,13 @@ lasso_str_sign(xmlChar *str,
xmlSecTransformId signMethodId,
const char* key_file)
{
- xmlDocPtr doc = xmlNewDoc("1.0");
- xmlNodePtr cur;
+ xmlDocPtr doc = xmlNewDoc("1.0");
xmlNodePtr envelope = xmlNewNode(NULL, "Envelope");
xmlNodePtr cdata, data = xmlNewNode(NULL, "Data");
- xmlNodePtr signNode = NULL;
- xmlNodePtr refNode = NULL;
- xmlNodePtr keyInfoNode = NULL;
- xmlSecDSigCtxPtr dsigCtx = NULL;
+ xmlNodePtr signNode;
+ xmlNodePtr refNode;
+ xmlNodePtr keyInfoNode;
+ xmlSecDSigCtxPtr dsigCtx;
/* create doc */
xmlNewNs(envelope, "urn:envelope", NULL);
@@ -172,7 +172,7 @@ lasso_str_sign(xmlChar *str,
/* create signature template for enveloped signature */
signNode = xmlSecTmplSignatureCreate(doc, xmlSecTransformExclC14NId,
signMethodId, NULL);
- if(signNode == NULL) {
+ if (signNode == NULL) {
fprintf(stderr, "Error: failed to create signature template\n");
goto done;
}
@@ -183,13 +183,13 @@ lasso_str_sign(xmlChar *str,
/* add reference */
refNode = xmlSecTmplSignatureAddReference(signNode, xmlSecTransformSha1Id,
NULL, NULL, NULL);
- if(refNode == NULL) {
+ if (refNode == NULL) {
fprintf(stderr, "Error: failed to add reference to signature template\n");
goto done;
}
/* add enveloped transform */
- if(xmlSecTmplReferenceAddTransform(refNode, xmlSecTransformEnvelopedId) == NULL) {
+ if (xmlSecTmplReferenceAddTransform(refNode, xmlSecTransformEnvelopedId) == NULL) {
fprintf(stderr, "Error: failed to add enveloped transform to reference\n");
goto done;
}
@@ -197,50 +197,52 @@ lasso_str_sign(xmlChar *str,
/* add <dsig:KeyInfo/> and <dsig:KeyName/> nodes to put key name in the
signed document */
keyInfoNode = xmlSecTmplSignatureEnsureKeyInfo(signNode, NULL);
- if(keyInfoNode == NULL) {
+ if (keyInfoNode == NULL) {
fprintf(stderr, "Error: failed to add key info\n");
goto done;
}
- if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL) == NULL) {
+ if (xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL) == NULL) {
fprintf(stderr, "Error: failed to add key name\n");
goto done;
}
/* create signature context */
dsigCtx = xmlSecDSigCtxCreate(NULL);
- if(dsigCtx == NULL) {
+ if (dsigCtx == NULL) {
fprintf(stderr,"Error: failed to create signature context\n");
goto done;
}
- /* load private key, assuming that there is not password */
+ /* load private key */
dsigCtx->signKey = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem,
NULL, NULL, NULL);
- if(dsigCtx->signKey == NULL) {
+ if (dsigCtx->signKey == NULL) {
fprintf(stderr,"Error: failed to load private pem key from \"%s\"\n", key_file);
goto done;
}
/* sign the template */
- if(xmlSecDSigCtxSign(dsigCtx, signNode) < 0) {
+ if (xmlSecDSigCtxSign(dsigCtx, signNode) < 0) {
fprintf(stderr,"Error: signature failed\n");
goto done;
}
//xmlDocDump(stdout, doc);
+ xmlSecDSigCtxDestroy(dsigCtx);
+ /* doc must be freed be caller */
return (doc);
done:
/* cleanup */
- if(dsigCtx != NULL) {
+ if (dsigCtx != NULL) {
xmlSecDSigCtxDestroy(dsigCtx);
}
- if(doc != NULL) {
+ if (doc != NULL) {
xmlFreeDoc(doc);
}
- return(NULL);
+ return (NULL);
}
xmlChar *
@@ -258,11 +260,12 @@ lasso_str_verify(xmlChar *str,
const xmlChar *sender_public_key_file,
const xmlChar *recipient_private_key_file)
{
- xmlDocPtr doc = NULL;
- xmlNodePtr node = NULL, sigValNode = NULL;
- xmlSecDSigCtxPtr dsigCtx = NULL;
+ xmlDocPtr doc;
+ xmlNodePtr sigNode, sigValNode;
+ xmlSecDSigCtxPtr dsigCtx;
gchar **str_split;
-
+ gint ret = -1;
+
/* split query, signatureValue */
str_split = g_strsplit((const gchar *)str, "&Signature=", 0);
/* re-create doc to verify (signed + enrypted) */
@@ -272,14 +275,14 @@ lasso_str_verify(xmlChar *str,
sigValNode = xmlSecFindNode(xmlDocGetRootElement(doc),
xmlSecNodeSignatureValue,
xmlSecDSigNs);
- /* SignatureValue content */
+ /* set SignatureValue content */
xmlNodeSetContent(sigValNode, lasso_str_unescape(str_split[1]));
g_strfreev(str_split);
//xmlDocDump(stdout, doc);
/* find start node */
- node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
+ sigNode = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
/* create signature context */
dsigCtx = xmlSecDSigCtxCreate(NULL);
@@ -296,7 +299,7 @@ lasso_str_verify(xmlChar *str,
}
/* Verify signature */
- if(xmlSecDSigCtxVerify(dsigCtx, node) < 0) {
+ if(xmlSecDSigCtxVerify(dsigCtx, sigNode) < 0) {
fprintf(stderr,"Error: signature verify\n");
goto done;
}
@@ -304,10 +307,11 @@ lasso_str_verify(xmlChar *str,
/* print verification result to stdout and return */
if(dsigCtx->status == xmlSecDSigStatusSucceeded) {
fprintf(stdout, "Signature is OK\n");
- return (1);
- } else {
+ ret = 1;
+ }
+ else {
fprintf(stdout, "Signature is INVALID\n");
- return (0);
+ ret = 0;
}
done:
@@ -319,5 +323,5 @@ lasso_str_verify(xmlChar *str,
if(doc != NULL) {
xmlFreeDoc(doc);
}
- return (-1);
+ return (ret);
}
diff --git a/lasso/xml/tools.h b/lasso/xml/tools.h
index 188aa09e..05adf2c9 100644
--- a/lasso/xml/tools.h
+++ b/lasso/xml/tools.h
@@ -30,24 +30,26 @@
#include <xmlsec/templates.h>
#include <xmlsec/crypto.h>
-xmlChar * lasso_build_unique_id(guint8 size);
+xmlChar* lasso_build_unique_id (guint8 size);
-xmlChar * lasso_doc_get_node_content(xmlDocPtr doc, const xmlChar *name);
+xmlChar* lasso_doc_get_node_content (xmlDocPtr doc,
+ const xmlChar *name);
-xmlChar * lasso_g_ptr_array_index(GPtrArray *a, guint i);
+xmlChar* lasso_g_ptr_array_index (GPtrArray *a,
+ guint i);
-xmlChar * lasso_get_current_time(void);
+xmlChar* lasso_get_current_time (void);
-GData * lasso_query_to_dict(const xmlChar *query);
+GData* lasso_query_to_dict (const xmlChar *query);
-xmlChar * lasso_str_escape(xmlChar *str);
+xmlChar* lasso_str_escape (xmlChar *str);
-xmlDocPtr lasso_str_sign(xmlChar *str,
- xmlSecTransformId signMethodId,
- const char* key_file);
+xmlDocPtr lasso_str_sign (xmlChar *str,
+ xmlSecTransformId signMethodId,
+ const char *key_file);
-xmlChar * lasso_str_unescape(xmlChar *str);
+xmlChar* lasso_str_unescape (xmlChar *str);
-int lasso_str_verify(xmlChar *str,
- const xmlChar *sender_public_key_file,
- const xmlChar *recipient_private_key_file);
+int lasso_str_verify (xmlChar *str,
+ const xmlChar *sender_public_key_file,
+ const xmlChar *recipient_private_key_file);
diff --git a/lasso/xml/xml.c b/lasso/xml/xml.c
index f2108184..5a4a1728 100644
--- a/lasso/xml/xml.c
+++ b/lasso/xml/xml.c
@@ -145,10 +145,10 @@ lasso_node_serialize(LassoNode *node, GData *gd)
gchar *
lasso_node_url_encode(LassoNode *node,
guint sign_method,
- const gchar *key_file)
+ const gchar *private_key_file)
{
LassoNodeClass *class = LASSO_NODE_GET_CLASS(node);
- return (class->url_encode(node, sign_method, key_file));
+ return (class->url_encode(node, sign_method, private_key_file));
}
gint
@@ -394,7 +394,7 @@ lasso_node_impl_get_child(LassoNode *node,
static GPtrArray *
lasso_node_impl_get_children(LassoNode *node)
{
- GPtrArray *children = NULL;
+ GPtrArray *children;
xmlNodePtr cur;
cur = node->private->node->children;
@@ -421,17 +421,26 @@ lasso_node_impl_get_name(LassoNode *node)
return ((xmlChar *)(node->private->node->name));
}
+/**
+ * lasso_node_impl_parse_memory:
+ * @node: a LassoNode instance
+ * @buffer: a string containing xml
+ *
+ *
+ **/
void
-lasso_node_impl_parse_memory(LassoNode *node,
+lasso_node_impl_parse_memory(LassoNode *node,
const char *buffer)
{
xmlDocPtr doc;
xmlNodePtr root;
doc = xmlParseMemory(buffer, strlen(buffer));
- root = xmlDocGetRootElement(doc);
- xmlFreeNode(node->private->node);
- node->private->node = root;
+ /* get root element of doc and duplicate it */
+ root = xmlCopyNode(xmlDocGetRootElement(doc), 1);
+ lasso_node_set_node(node, root);
+ /* free doc */
+ xmlFreeDoc(doc);
}
static void
@@ -440,7 +449,6 @@ lasso_node_impl_rename_prop(LassoNode *node,
const xmlChar *new_name)
{
xmlChar *value;
- LassoAttr *prop;
value = xmlGetProp(node->private->node, old_name);
if (value != NULL) {
@@ -502,7 +510,7 @@ lasso_node_impl_serialize(LassoNode *node, GData *gd)
static gchar *
lasso_node_impl_url_encode(LassoNode *node,
guint sign_method,
- const gchar *key_file)
+ const gchar *private_key_file)
{
GString *msg;
xmlDocPtr doc;
@@ -511,12 +519,12 @@ lasso_node_impl_url_encode(LassoNode *node,
msg = lasso_node_build_query(node);
- if (sign_method > 0 && key_file != NULL) {
+ if (sign_method > 0 && private_key_file != NULL) {
switch (sign_method) {
case lassoUrlEncodeRsaSha1:
msg = g_string_append(msg, "&SigAlg=");
msg = g_string_append(msg, lasso_str_escape("http://www.w3.org/2000/09/xmldsig#rsa-sha1"));
- doc = lasso_str_sign(msg->str, xmlSecTransformRsaSha1Id, key_file);
+ doc = lasso_str_sign(msg->str, xmlSecTransformRsaSha1Id, private_key_file);
msg = g_string_append(msg, "&Signature=");
str1 = lasso_doc_get_node_content(doc, xmlSecNodeSignatureValue);
str2 = lasso_str_escape(str1);
@@ -527,7 +535,7 @@ lasso_node_impl_url_encode(LassoNode *node,
case lassoUrlEncodeDsaSha1:
msg = g_string_append(msg, "&SigAlg=");
msg = g_string_append(msg, lasso_str_escape("http://www.w3.org/2000/09/xmldsig#dsa-sha1"));
- doc = lasso_str_sign(msg->str, xmlSecTransformDsaSha1Id, key_file);
+ doc = lasso_str_sign(msg->str, xmlSecTransformDsaSha1Id, private_key_file);
msg = g_string_append(msg, "&Signature=");
str1 = lasso_doc_get_node_content(doc, xmlSecNodeSignatureValue);
str2 = lasso_str_escape(str1);
@@ -540,6 +548,7 @@ lasso_node_impl_url_encode(LassoNode *node,
ret = g_strdup(msg->str);
g_string_free(msg, TRUE);
+ xmlFreeDoc(doc);
return (ret);
}
diff --git a/lasso/xml/xml.h b/lasso/xml/xml.h
index c949c510..93dcae27 100644
--- a/lasso/xml/xml.h
+++ b/lasso/xml/xml.h
@@ -83,7 +83,7 @@ struct _LassoNodeClass {
GData *);
gchar * (* url_encode) (LassoNode *node,
guint sign_method,
- const gchar *key_file);
+ const gchar *private_key_file);
gint (* verify_signature) (LassoNode *node,
const gchar *certificate_file);
/*< private >*/
@@ -150,7 +150,7 @@ LASSO_EXPORT GData* lasso_node_serialize (LassoNode *node,
LASSO_EXPORT gchar* lasso_node_url_encode (LassoNode *node,
guint sign_method,
- const gchar *key_file);
+ const gchar *private_key_file);
LASSO_EXPORT gint lasso_node_verify_signature (LassoNode *node,
const gchar *certificate_file);