add function for decrypting and parsing crypto-prop elements

Mon, 22 Apr 2019 14:04:37 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 22 Apr 2019 14:04:37 +0200
changeset 589
2514559a6367
parent 588
0cfe006fcad2
child 590
cda8093116f2

add function for decrypting and parsing crypto-prop elements

libidav/resource.c file | annotate | diff | comparison | revisions
--- a/libidav/resource.c	Mon Apr 22 14:02:34 2019 +0200
+++ b/libidav/resource.c	Mon Apr 22 14:04:37 2019 +0200
@@ -1341,5 +1341,62 @@
 }
 
 UcxMap* parse_crypto_prop(DavSession *sn, DavXmlNode *node) {
+    if(!node || node->type != DAV_XML_TEXT || node->contentlength == 0) {
+        return NULL;
+    }
     
+    size_t len = 0;
+    char *dec_str = aes_decrypt(node->content, &len, sn->key);
+    
+    xmlDoc *doc = xmlReadMemory(dec_str, len, NULL, NULL, 0);
+    free(dec_str);
+    if(!doc) {
+        return NULL;
+    }
+    
+    int err = 0;
+    xmlNode *xml_root = xmlDocGetRootElement(doc);
+    if(xml_root) {
+        if(
+                !xml_root->ns ||
+                !xstreq(xml_root->name, "prop") ||
+                !xstreq(xml_root->ns->href, "DAV:"))
+        {
+            err = 1;
+        }
+    } else {
+        err = 1;
+    }
+    
+    if(err) {
+        xmlFreeDoc(doc);
+        return NULL;
+    }
+    
+    // ready to get the properties
+    UcxMap *map = ucx_map_new(32);
+    xmlNode *n = xml_root->children;
+    while(n) {
+        if(n->type == XML_ELEMENT_NODE && n->ns && n->ns->href) {
+            DavProperty *property = dav_session_malloc(sn, sizeof(DavProperty));
+            property->name = dav_session_strdup(sn, (const char*)n->name);
+            property->ns = dav_session_malloc(sn, sizeof(DavNamespace));
+            property->ns->name = dav_session_strdup(sn, (const char*)n->ns->href);
+            property->ns->prefix = n->ns->prefix ?
+                    dav_session_strdup(sn, (const char*)n->ns->prefix) : NULL;
+            property->value = n->children ? dav_convert_xml(sn, n->children) : NULL;
+            
+            sstr_t key = dav_property_key(property->ns->name, property->name);
+            ucx_map_sstr_put(map, key, property);
+            free(key.ptr);
+        }
+        n = n->next;
+    }
+    
+    xmlFreeDoc(doc);
+    if(map->count == 0) {
+        ucx_map_free(map);
+        return NULL;
+    }
+    return map;
 }

mercurial