Decode the error_path field in TID responses.
[trust_router.git] / common / tr_msg.c
index 0d18466..f8a462c 100644 (file)
@@ -36,6 +36,7 @@
 #include <arpa/inet.h>
 #include <string.h>
 #include <openssl/dh.h>
+#include <openssl/crypto.h>
 #include <jansson.h>
 #include <assert.h>
 #include <talloc.h>
@@ -47,6 +48,7 @@
 #include <trust_router/tr_name.h>
 #include <trp_internal.h>
 #include <trust_router/tr_constraint.h>
+#include <trust_router/tr_dh.h>
 #include <tr_debug.h>
 
 /* JSON helpers */
@@ -156,19 +158,23 @@ static json_t *tr_msg_encode_dh(DH *dh)
 {
   json_t *jdh = NULL;
   json_t *jbn = NULL;
+  char *s=NULL;
 
   if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
     return NULL;
 
   jdh = json_object();
 
-  jbn = json_string(BN_bn2hex(dh->p));
+  jbn = json_string(s=BN_bn2hex(dh->p));
+  OPENSSL_free(s);
   json_object_set_new(jdh, "dh_p", jbn);
 
-  jbn = json_string(BN_bn2hex(dh->g));
+  jbn = json_string(s=BN_bn2hex(dh->g));
+  OPENSSL_free(s);
   json_object_set_new(jdh, "dh_g", jbn);
 
-  jbn = json_string(BN_bn2hex(dh->pub_key));
+  jbn = json_string(s=BN_bn2hex(dh->pub_key));
+  OPENSSL_free(s);
   json_object_set_new(jdh, "dh_pub_key", jbn);
 
   return jdh;
@@ -181,19 +187,17 @@ static DH *tr_msg_decode_dh(json_t *jdh)
   json_t *jg = NULL;
   json_t *jpub_key = NULL;
 
-  if (!(dh = malloc(sizeof(DH)))) {
+  if (!(dh=tr_dh_new())) {
     tr_crit("tr_msg_decode_dh(): Error allocating DH structure.");
     return NULL;
   }
  
-  memset(dh, 0, sizeof(DH));
-
   /* store required fields from dh object */
   if ((NULL == (jp = json_object_get(jdh, "dh_p"))) ||
       (NULL == (jg = json_object_get(jdh, "dh_g"))) ||
       (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key")))) {
     tr_debug("tr_msg_decode_dh(): Error parsing dh_info.");
-    free(dh);
+    tr_dh_free(dh);
     return NULL;
   }
 
@@ -355,7 +359,7 @@ static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr)
   srvr->aaa_server_addr=talloc_strdup(srvr, json_string_value(jsrvr_addr));
   srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
   srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
-  srvr->path = json_object_get(jsrvr, "path");
+  tid_srvr_blk_set_path(srvr, json_object_get(jsrvr, "path"));
   jsrvr_expire = json_object_get(jsrvr, "key_expiration");
   if (jsrvr_expire && json_is_string(jsrvr_expire)) {
     if (!g_time_val_from_iso8601(json_string_value(jsrvr_expire),
@@ -406,7 +410,7 @@ static TID_SRVR_BLK *tr_msg_decode_servers(TALLOC_CTX *mem_ctx, json_t *jservers
   for (i = 0; i < num_servers; i++) {
     jsrvr = json_array_get(jservers, i);
 
-    new_srvr=tid_srvr_blk_new(NULL);
+    new_srvr=tid_srvr_blk_new(tmp_ctx);
     if (new_srvr==NULL) {
       servers=NULL; /* it's all in tmp_ctx, so we can just let go */
       goto cleanup;
@@ -527,18 +531,22 @@ static TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
     tr_debug("tr_msg_decode_tidresp(): Error! result = %s.", json_string_value(jresult));
     if ((NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) ||
        (!json_is_string(jerr_msg))) {
-      tresp->err_msg = tr_new_name((char *)json_string_value(jerr_msg));
-    }
+      tresp->err_msg = tr_new_name(json_string_value(jerr_msg));
+    } else
+      tresp->err_msg = tr_new_name("No error message set.");
+
+    if (NULL !=(tresp->error_path = json_object_get(jresp, "error_path")))
+      json_incref(tresp->error_path);
   }
 
-  tresp->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
-  tresp->realm = tr_new_name((char *)json_string_value(jrealm));
-  tresp->comm = tr_new_name((char *)json_string_value(jcomm));
+  tresp->rp_realm = tr_new_name(json_string_value(jrp_realm));
+  tresp->realm = tr_new_name(json_string_value(jrealm));
+  tresp->comm = tr_new_name(json_string_value(jcomm));
 
   /* store optional "orig_coi" field */
   if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
       (!json_is_object(jorig_coi))) {
-    tresp->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
+    tresp->orig_coi = tr_new_name(json_string_value(jorig_coi));
   }
      
   return tresp;
@@ -1242,6 +1250,9 @@ TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
     msg->msg_type = TR_UNKNOWN;
     msg->msg_rep = NULL;
   }
+
+  json_decref(jmsg);
+
   return msg;
 }