Merge branch 'logging_changes' of https://github.com/adam-bishop/trust_router
[trust_router.git] / common / tr_msg.c
1 /*
2  * Copyright (c) 2012-2014 , JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <string.h>
38 #include <openssl/dh.h>
39 #include <jansson.h>
40 #include <assert.h>
41 #include <talloc.h>
42
43
44 #include <tr_msg.h>
45 #include <trust_router/tr_name.h>
46 #include <tid_internal.h>
47 #include <trust_router/tr_constraint.h>
48 #include <tr_debug.h>
49
50 enum msg_type tr_msg_get_msg_type(TR_MSG *msg) 
51 {
52   return msg->msg_type;
53 }
54
55 void tr_msg_set_msg_type(TR_MSG *msg, enum msg_type type)
56 {
57   msg->msg_type = type;
58 }
59
60 TID_REQ *tr_msg_get_req(TR_MSG *msg)
61 {
62   if (msg->msg_type == TID_REQUEST)
63     return (TID_REQ *)msg->msg_rep;
64   return NULL;
65 }
66
67 void tr_msg_set_req(TR_MSG *msg, TID_REQ *req)
68 {
69   msg->msg_rep = req;
70   msg->msg_type = TID_REQUEST;
71 }
72
73 TID_RESP *tr_msg_get_resp(TR_MSG *msg)
74 {
75   if (msg->msg_type == TID_RESPONSE)
76     return (TID_RESP *)msg->msg_rep;
77   return NULL;
78 }
79
80 void tr_msg_set_resp(TR_MSG *msg, TID_RESP *resp)
81 {
82   msg->msg_rep = resp;
83   msg->msg_type = TID_RESPONSE;
84 }
85
86 static json_t *tr_msg_encode_dh(DH *dh)
87 {
88   json_t *jdh = NULL;
89   json_t *jbn = NULL;
90
91   if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
92     return NULL;
93
94   jdh = json_object();
95
96   jbn = json_string(BN_bn2hex(dh->p));
97   json_object_set_new(jdh, "dh_p", jbn);
98
99   jbn = json_string(BN_bn2hex(dh->g));
100   json_object_set_new(jdh, "dh_g", jbn);
101
102   jbn = json_string(BN_bn2hex(dh->pub_key));
103   json_object_set_new(jdh, "dh_pub_key", jbn);
104
105   return jdh;
106 }
107
108 static DH *tr_msg_decode_dh(json_t *jdh)
109 {
110   DH *dh = NULL;
111   json_t *jp = NULL;
112   json_t *jg = NULL;
113   json_t *jpub_key = NULL;
114
115   if (!(dh = malloc(sizeof(DH)))) {
116     tr_crit("tr_msg_decode_dh(): Error allocating DH structure.");
117     return NULL;
118   }
119  
120   memset(dh, 0, sizeof(DH));
121
122   /* store required fields from dh object */
123   if ((NULL == (jp = json_object_get(jdh, "dh_p"))) ||
124       (NULL == (jg = json_object_get(jdh, "dh_g"))) ||
125       (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key")))) {
126     tr_debug("tr_msg_decode_dh(): Error parsing dh_info.");
127     free(dh);
128     return NULL;
129   }
130
131   BN_hex2bn(&(dh->p), json_string_value(jp));
132   BN_hex2bn(&(dh->g), json_string_value(jg));
133   BN_hex2bn(&(dh->pub_key), json_string_value(jpub_key));
134
135   return dh;
136 }
137
138 static json_t * tr_msg_encode_tidreq(TID_REQ *req)
139 {
140   json_t *jreq = NULL;
141   json_t *jstr = NULL;
142
143   if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
144     return NULL;
145
146   assert(jreq = json_object());
147
148   jstr = json_string(req->rp_realm->buf);
149   json_object_set_new(jreq, "rp_realm", jstr);
150
151   jstr = json_string(req->realm->buf);
152   json_object_set_new(jreq, "target_realm", jstr);
153
154   jstr = json_string(req->comm->buf);
155   json_object_set_new(jreq, "community", jstr);
156   
157   if (req->orig_coi) {
158     jstr = json_string(req->orig_coi->buf);
159     json_object_set_new(jreq, "orig_coi", jstr);
160   }
161
162   json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
163
164   if (req->cons)
165     json_object_set(jreq, "constraints", (json_t *) req->cons);
166
167   if (req->path)
168     json_object_set(jreq, "path", req->path);
169   if (req->expiration_interval)
170     json_object_set_new(jreq, "expiration_interval",
171                         json_integer(req->expiration_interval));
172   
173   return jreq;
174 }
175
176 static TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
177 {
178   TID_REQ *treq = NULL;
179   json_t *jrp_realm = NULL;
180   json_t *jrealm = NULL;
181   json_t *jcomm = NULL;
182   json_t *jorig_coi = NULL;
183   json_t *jdh = NULL;
184   json_t *jpath = NULL;
185   json_t *jexpire_interval = NULL;
186
187   if (!(treq =tid_req_new())) {
188     tr_crit("tr_msg_decode_tidreq(): Error allocating TID_REQ structure.");
189     return NULL;
190   }
191  
192   /* store required fields from request */
193   if ((NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) ||
194       (NULL == (jrealm = json_object_get(jreq, "target_realm"))) ||
195       (NULL == (jcomm = json_object_get(jreq, "community")))) {
196     tr_notice("tr_msg_decode(): Error parsing required fields.");
197     tid_req_free(treq);
198     return NULL;
199   }
200
201   jpath = json_object_get(jreq, "path");
202   jexpire_interval = json_object_get(jreq, "expiration_interval");
203
204   treq->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
205   treq->realm = tr_new_name((char *)json_string_value(jrealm));
206   treq->comm = tr_new_name((char *)json_string_value(jcomm));
207
208   /* Get DH Info from the request */
209   if (NULL == (jdh = json_object_get(jreq, "dh_info"))) {
210     tr_debug("tr_msg_decode(): Error parsing dh_info.");
211     tid_req_free(treq);
212     return NULL;
213   }
214   treq->tidc_dh = tr_msg_decode_dh(jdh);
215
216   /* store optional "orig_coi" field */
217   if (NULL != (jorig_coi = json_object_get(jreq, "orig_coi"))) {
218     treq->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
219   }
220
221   treq->cons = (TR_CONSTRAINT_SET *) json_object_get(jreq, "constraints");
222   if (treq->cons) {
223     if (!tr_constraint_set_validate(treq->cons)) {
224       tr_debug("Constraint set validation failed");
225     tid_req_free(treq);
226     return NULL;
227     }
228     json_incref((json_t *) treq->cons);
229     tid_req_cleanup_json(treq, (json_t *) treq->cons);
230   }
231   if (jpath) {
232     json_incref(jpath);
233     treq->path = jpath;
234     tid_req_cleanup_json(treq, jpath);
235   }
236   if (jexpire_interval)
237     treq->expiration_interval = json_integer_value(jexpire_interval);
238   
239   return treq;
240 }
241
242 static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr)
243 {
244   json_t *jsrvr = NULL;
245   json_t *jstr = NULL;
246   gchar *time_str = g_time_val_to_iso8601(&srvr->key_expiration);
247
248   tr_debug("Encoding one server.");
249
250   jsrvr = json_object();
251
252   /* Server IP Address -- TBD handle IPv6 */
253   jstr = json_string(inet_ntoa(srvr->aaa_server_addr));
254   json_object_set_new(jsrvr, "server_addr", jstr);
255
256   json_object_set_new(jsrvr,
257                       "key_expiration", json_string(time_str));
258   g_free(time_str);
259   /* Server DH Block */
260   jstr = json_string(srvr->key_name->buf);
261   json_object_set_new(jsrvr, "key_name", jstr);
262   json_object_set_new(jsrvr, "server_dh", tr_msg_encode_dh(srvr->aaa_server_dh));
263   if (srvr->path)
264     /* The path is owned by the srvr, so grab an extra ref*/
265     json_object_set(jsrvr, "path", srvr->path);
266   return jsrvr;
267 }
268
269 static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr) 
270 {
271   json_t *jsrvr_addr = NULL;
272   json_t *jsrvr_kn = NULL;
273   json_t *jsrvr_dh = NULL;
274   json_t *jsrvr_expire = NULL;
275
276   if (jsrvr == NULL)
277     return -1;
278
279
280   if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) ||
281       (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) ||
282       (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) {
283     tr_notice("tr_msg_decode_one_server(): Error parsing required fields.");
284     return -1;
285   }
286   
287   /* TBD -- handle IPv6 Addresses */
288   inet_aton(json_string_value(jsrvr_addr), &(srvr->aaa_server_addr));
289   srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
290   srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
291   srvr->path = json_object_get(jsrvr, "path");
292   jsrvr_expire = json_object_get(jsrvr, "key_expiration");
293   if (jsrvr_expire && json_is_string(jsrvr_expire)) {
294     if (!g_time_val_from_iso8601(json_string_value(jsrvr_expire),
295                                  &srvr->key_expiration))
296       tr_notice("Key expiration %s cannot be parsed", json_string_value(jsrvr_expire));
297   }
298   
299   return 0;
300 }
301
302 static json_t *tr_msg_encode_servers(TID_RESP *resp)
303 {
304   json_t *jservers = NULL;
305   json_t *jsrvr = NULL;
306   TID_SRVR_BLK *srvr = NULL;
307   size_t index;
308
309   jservers = json_array();
310
311   tid_resp_servers_foreach(resp, srvr, index) {
312     if ((NULL == (jsrvr = tr_msg_encode_one_server(srvr))) ||
313         (-1 == json_array_append_new(jservers, jsrvr))) {
314       return NULL;
315     }
316   }
317
318   //  tr_debug("tr_msg_encode_servers(): servers contains:");
319   //  tr_debug("%s", json_dumps(jservers, 0));
320   return jservers;
321 }
322
323 static TID_SRVR_BLK *tr_msg_decode_servers(void * ctx, json_t *jservers, size_t *out_len)
324 {
325   TID_SRVR_BLK *servers = NULL;
326   json_t *jsrvr;
327   size_t i, num_servers;
328
329   num_servers = json_array_size(jservers);
330   tr_debug("tr_msg_decode_servers(): Number of servers = %u.", (unsigned) num_servers);
331   
332   if (0 == num_servers) {
333     tr_debug("tr_msg_decode_servers(): Server array is empty."); 
334     return NULL;
335   }
336   servers = talloc_zero_array(ctx, TID_SRVR_BLK, num_servers);
337
338   for (i = 0; i < num_servers; i++) {
339     jsrvr = json_array_get(jservers, i);
340     if (0 != tr_msg_decode_one_server(jsrvr, &servers[i])) {
341       talloc_free(servers);
342       return NULL;
343     }
344
345
346   }
347   *out_len = num_servers;
348   return servers;
349 }
350
351 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
352 {
353   json_t *jresp = NULL;
354   json_t *jstr = NULL;
355   json_t *jservers = NULL;
356
357   if ((!resp) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
358     return NULL;
359
360   jresp = json_object();
361
362   if (TID_ERROR == resp->result) {
363     jstr = json_string("error");
364     json_object_set_new(jresp, "result", jstr);
365     if (resp->err_msg) {
366       jstr = json_string(resp->err_msg->buf);
367       json_object_set_new(jresp, "err_msg", jstr);
368     }
369   }
370   else {
371     jstr = json_string("success");
372     json_object_set_new(jresp, "result", jstr);
373   }
374
375   jstr = json_string(resp->rp_realm->buf);
376   json_object_set_new(jresp, "rp_realm", jstr);
377
378   jstr = json_string(resp->realm->buf);
379   json_object_set_new(jresp, "target_realm", jstr);
380
381   jstr = json_string(resp->comm->buf);
382   json_object_set_new(jresp, "comm", jstr);
383
384   if (resp->orig_coi) {
385     jstr = json_string(resp->orig_coi->buf);
386     json_object_set_new(jresp, "orig_coi", jstr);
387   }
388
389   if (NULL == resp->servers) {
390     tr_debug("tr_msg_encode_tidresp(): No servers to encode.");
391   }
392   else {
393     jservers = tr_msg_encode_servers(resp);
394     json_object_set_new(jresp, "servers", jservers);
395   }
396   if (resp->error_path)
397     json_object_set(jresp, "error_path", resp->error_path);
398   
399   
400   return jresp;
401 }
402
403 static TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
404 {
405   TID_RESP *tresp = NULL;
406   json_t *jresult = NULL;
407   json_t *jrp_realm = NULL;
408   json_t *jrealm = NULL;
409   json_t *jcomm = NULL;
410   json_t *jorig_coi = NULL;
411   json_t *jservers = NULL;
412   json_t *jerr_msg = NULL;
413
414   if (!(tresp = talloc_zero(NULL, TID_RESP))) {
415     tr_crit("tr_msg_decode_tidresp(): Error allocating TID_RESP structure.");
416     return NULL;
417   }
418  
419
420   /* store required fields from response */
421   if ((NULL == (jresult = json_object_get(jresp, "result"))) ||
422       (!json_is_string(jresult)) ||
423       (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
424       (!json_is_string(jrp_realm)) ||
425       (NULL == (jrealm = json_object_get(jresp, "target_realm"))) ||
426       (!json_is_string(jrealm)) ||
427       (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
428       (!json_is_string(jcomm))) {
429     tr_debug("tr_msg_decode_tidresp(): Error parsing response.");
430     talloc_free(tresp);
431     return NULL;
432   }
433
434   if (0 == (strcmp(json_string_value(jresult), "success"))) {
435     tr_debug("tr_msg_decode_tidresp(): Success! result = %s.", json_string_value(jresult));
436     if ((NULL != (jservers = json_object_get(jresp, "servers"))) ||
437         (!json_is_array(jservers))) {
438       tresp->servers = tr_msg_decode_servers(tresp, jservers, &tresp->num_servers); 
439     } 
440     else {
441       talloc_free(tresp);
442       return NULL;
443     }
444     tresp->result = TID_SUCCESS;
445   }
446   else {
447     tresp->result = TID_ERROR;
448     tr_debug("tr_msg_decode_tidresp(): Error! result = %s.", json_string_value(jresult));
449     if ((NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) ||
450         (!json_is_string(jerr_msg))) {
451       tresp->err_msg = tr_new_name((char *)json_string_value(jerr_msg));
452     }
453   }
454
455   tresp->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
456   tresp->realm = tr_new_name((char *)json_string_value(jrealm));
457   tresp->comm = tr_new_name((char *)json_string_value(jcomm));
458
459   /* store optional "orig_coi" field */
460   if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
461       (!json_is_object(jorig_coi))) {
462     tresp->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
463   }
464      
465   return tresp;
466 }
467
468 char *tr_msg_encode(TR_MSG *msg) 
469 {
470   json_t *jmsg;
471   json_t *jmsg_type;
472
473   /* TBD -- add error handling */
474   jmsg = json_object();
475
476   switch (msg->msg_type) 
477     {
478     case TID_REQUEST:
479       jmsg_type = json_string("tid_request");
480       json_object_set_new(jmsg, "msg_type", jmsg_type);
481       json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(tr_msg_get_req(msg)));
482       break;
483
484     case TID_RESPONSE:
485       jmsg_type = json_string("tid_response");
486       json_object_set_new(jmsg, "msg_type", jmsg_type);
487       json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(tr_msg_get_resp(msg)));
488       break;
489
490       /* TBD -- Add TR message types */
491
492     default:
493       json_decref(jmsg);
494       return NULL;
495     }
496   
497   return(json_dumps(jmsg, 0));
498 }
499
500 TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
501 {
502   TR_MSG *msg;
503   json_t *jmsg = NULL;
504   json_error_t rc;
505   json_t *jtype;
506   json_t *jbody;
507   const char *mtype = NULL;
508
509   if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
510     tr_debug("tr_msg_decode(): error loading object");
511     return NULL;
512   }
513
514   if (!(msg = malloc(sizeof(TR_MSG)))) {
515     tr_debug("tr_msg_decode(): Error allocating TR_MSG structure.");
516     json_decref(jmsg);
517     return NULL;
518   }
519  
520   memset(msg, 0, sizeof(TR_MSG));
521
522   if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
523       (NULL == (jbody = json_object_get(jmsg, "msg_body")))) {
524     tr_debug("tr_msg_decode(): Error parsing message header.");
525     json_decref(jmsg);
526     tr_msg_free_decoded(msg);
527     return NULL;
528   }
529
530   mtype = json_string_value(jtype);
531
532   if (0 == strcmp(mtype, "tid_request")) {
533     msg->msg_type = TID_REQUEST;
534     tr_msg_set_req(msg, tr_msg_decode_tidreq(jbody));
535   }
536   else if (0 == strcmp(mtype, "tid_response")) {
537     msg->msg_type = TID_RESPONSE;
538     tr_msg_set_resp(msg, tr_msg_decode_tidresp(jbody));
539   }
540   else {
541     msg->msg_type = TR_UNKNOWN;
542     msg->msg_rep = NULL;
543   }
544   return msg;
545 }
546
547 void tr_msg_free_encoded(char *jmsg)
548 {
549   if (jmsg)
550     free (jmsg);
551 }
552
553 void tr_msg_free_decoded(TR_MSG *msg)
554 {
555   if (msg)
556     free (msg);
557 }
558
559