In with the scabs, out with the tr_msg union!
[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     fprintf (stderr, "tr_msg_decode_dh(): Error allocating DH structure.\n");
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     fprintf (stderr, "tr_msg_decode_dh(): Error parsing dh_info.\n");
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   return jreq;
168 }
169
170 static TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
171 {
172   TID_REQ *treq = NULL;
173   json_t *jrp_realm = NULL;
174   json_t *jrealm = NULL;
175   json_t *jcomm = NULL;
176   json_t *jorig_coi = NULL;
177   json_t *jdh = NULL;
178
179   if (!(treq =tid_req_new())) {
180     fprintf (stderr, "tr_msg_decode_tidreq(): Error allocating TID_REQ structure.\n");
181     return NULL;
182   }
183  
184   /* store required fields from request */
185   if ((NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) ||
186       (NULL == (jrealm = json_object_get(jreq, "target_realm"))) ||
187       (NULL == (jcomm = json_object_get(jreq, "community")))) {
188     fprintf (stderr, "tr_msg_decode(): Error parsing required fields.\n");
189     tid_req_free(treq);
190     return NULL;
191   }
192
193   treq->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
194   treq->realm = tr_new_name((char *)json_string_value(jrealm));
195   treq->comm = tr_new_name((char *)json_string_value(jcomm));
196
197   /* Get DH Info from the request */
198   if (NULL == (jdh = json_object_get(jreq, "dh_info"))) {
199     fprintf (stderr, "tr_msg_decode(): Error parsing dh_info.\n");
200     tid_req_free(treq);
201     return NULL;
202   }
203   treq->tidc_dh = tr_msg_decode_dh(jdh);
204
205   /* store optional "orig_coi" field */
206   if (NULL != (jorig_coi = json_object_get(jreq, "orig_coi"))) {
207     treq->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
208   }
209
210   treq->cons = (TR_CONSTRAINT_SET *) json_object_get(jreq, "constraints");
211   if (treq->cons) {
212     if (!tr_constraint_set_validate(treq->cons)) {
213       tr_debug("Constraint set validation failed\n");
214     tid_req_free(treq);
215     return NULL;
216     }
217     json_incref((json_t *) treq->cons);
218     tid_req_cleanup_json(treq, (json_t *) treq->cons);
219   }
220   return treq;
221 }
222
223 static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr)
224 {
225   json_t *jsrvr = NULL;
226   json_t *jstr = NULL;
227
228   fprintf(stderr, "Encoding one server.\n");
229
230   jsrvr = json_object();
231
232   /* Server IP Address -- TBD handle IPv6 */
233   jstr = json_string(inet_ntoa(srvr->aaa_server_addr));
234   json_object_set_new(jsrvr, "server_addr", jstr);
235
236   /* Server DH Block */
237   jstr = json_string(srvr->key_name->buf);
238   json_object_set_new(jsrvr, "key_name", jstr);
239   json_object_set_new(jsrvr, "server_dh", tr_msg_encode_dh(srvr->aaa_server_dh));
240   
241   //  fprintf(stderr,"tr_msg_encode_one_server(): jsrvr contains:\n");
242   //  fprintf(stderr,"%s\n", json_dumps(jsrvr, 0));
243   return jsrvr;
244 }
245
246 static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr) 
247 {
248   json_t *jsrvr_addr = NULL;
249   json_t *jsrvr_kn = NULL;
250   json_t *jsrvr_dh = NULL;
251
252   if (jsrvr == NULL)
253     return -1;
254
255
256   if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) ||
257       (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) ||
258       (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) {
259     tr_debug("tr_msg_decode_one_server(): Error parsing required fields.\n");
260     return -1;
261   }
262   
263   /* TBD -- handle IPv6 Addresses */
264   inet_aton(json_string_value(jsrvr_addr), &(srvr->aaa_server_addr));
265   srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
266   srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
267   return 0;
268 }
269
270 static json_t *tr_msg_encode_servers(TID_RESP *resp)
271 {
272   json_t *jservers = NULL;
273   json_t *jsrvr = NULL;
274   TID_SRVR_BLK *srvr = NULL;
275   size_t index;
276
277   jservers = json_array();
278
279   tid_resp_servers_foreach(resp, srvr, index) {
280     if ((NULL == (jsrvr = tr_msg_encode_one_server(srvr))) ||
281         (-1 == json_array_append_new(jservers, jsrvr))) {
282       return NULL;
283     }
284   }
285
286   //  fprintf(stderr,"tr_msg_encode_servers(): servers contains:\n");
287   //  fprintf(stderr,"%s\n", json_dumps(jservers, 0));
288   return jservers;
289 }
290
291 static TID_SRVR_BLK *tr_msg_decode_servers(void * ctx, json_t *jservers, size_t *out_len)
292 {
293   TID_SRVR_BLK *servers = NULL;
294   json_t *jsrvr;
295   size_t i, num_servers;
296
297   num_servers = json_array_size(jservers);
298   fprintf(stderr, "tr_msg_decode_servers(): Number of servers = %u.\n", (unsigned) num_servers);
299   
300   if (0 == num_servers) {
301     fprintf(stderr, "tr_msg_decode_servers(): Server array is empty.\n"); 
302     return NULL;
303   }
304     servers = talloc_zero_array(ctx, TID_SRVR_BLK, num_servers);
305
306   for (i = 0; i < num_servers; i++) {
307     jsrvr = json_array_get(jservers, i);
308     if (0 != tr_msg_decode_one_server(jsrvr, &servers[i])) {
309       talloc_free(servers);
310       return NULL;
311     }
312
313
314   }
315   *out_len = num_servers;
316   return servers;
317 }
318
319 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
320 {
321   json_t *jresp = NULL;
322   json_t *jstr = NULL;
323   json_t *jservers = NULL;
324
325   if ((!resp) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
326     return NULL;
327
328   jresp = json_object();
329
330   if (TID_ERROR == resp->result) {
331     jstr = json_string("error");
332     json_object_set_new(jresp, "result", jstr);
333     if (resp->err_msg) {
334       jstr = json_string(resp->err_msg->buf);
335       json_object_set_new(jresp, "err_msg", jstr);
336     }
337   }
338   else {
339     jstr = json_string("success");
340     json_object_set_new(jresp, "result", jstr);
341   }
342
343   jstr = json_string(resp->rp_realm->buf);
344   json_object_set_new(jresp, "rp_realm", jstr);
345
346   jstr = json_string(resp->realm->buf);
347   json_object_set_new(jresp, "target_realm", jstr);
348
349   jstr = json_string(resp->comm->buf);
350   json_object_set_new(jresp, "comm", jstr);
351
352   if (resp->orig_coi) {
353     jstr = json_string(resp->orig_coi->buf);
354     json_object_set_new(jresp, "orig_coi", jstr);
355   }
356
357   if (NULL == resp->servers) {
358     fprintf(stderr, "tr_msg_encode_tidresp(): No servers to encode.\n");
359     return jresp;
360   }
361   jservers = tr_msg_encode_servers(resp);
362   json_object_set_new(jresp, "servers", jservers);
363   
364   return jresp;
365 }
366
367 static TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
368 {
369   TID_RESP *tresp = NULL;
370   json_t *jresult = NULL;
371   json_t *jrp_realm = NULL;
372   json_t *jrealm = NULL;
373   json_t *jcomm = NULL;
374   json_t *jorig_coi = NULL;
375   json_t *jservers = NULL;
376   json_t *jerr_msg = NULL;
377
378   if (!(tresp = talloc_zero(NULL, TID_RESP))) {
379     fprintf (stderr, "tr_msg_decode_tidresp(): Error allocating TID_RESP structure.\n");
380     return NULL;
381   }
382  
383
384   /* store required fields from response */
385   if ((NULL == (jresult = json_object_get(jresp, "result"))) ||
386       (!json_is_string(jresult)) ||
387       (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
388       (!json_is_string(jrp_realm)) ||
389       (NULL == (jrealm = json_object_get(jresp, "target_realm"))) ||
390       (!json_is_string(jrealm)) ||
391       (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
392       (!json_is_string(jcomm))) {
393     fprintf (stderr, "tr_msg_decode_tidresp(): Error parsing response.\n");
394     talloc_free(tresp);
395     return NULL;
396   }
397
398   if (0 == (strcmp(json_string_value(jresult), "success"))) {
399     fprintf(stderr, "tr_msg_decode_tidresp(): Success! result = %s.\n", json_string_value(jresult));
400     if ((NULL != (jservers = json_object_get(jresp, "servers"))) ||
401         (!json_is_array(jservers))) {
402       tresp->servers = tr_msg_decode_servers(tresp, jservers, &tresp->num_servers); 
403     } 
404     else {
405       talloc_free(tresp);
406       return NULL;
407     }
408     tresp->result = TID_SUCCESS;
409   }
410   else {
411     tresp->result = TID_ERROR;
412     fprintf(stderr, "tr_msg_decode_tidresp(): Error! result = %s.\n", json_string_value(jresult));
413     if ((NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) ||
414         (!json_is_string(jerr_msg))) {
415       tresp->err_msg = tr_new_name((char *)json_string_value(jerr_msg));
416     }
417   }
418
419   tresp->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
420   tresp->realm = tr_new_name((char *)json_string_value(jrealm));
421   tresp->comm = tr_new_name((char *)json_string_value(jcomm));
422
423   /* store optional "orig_coi" field */
424   if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
425       (!json_is_object(jorig_coi))) {
426     tresp->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
427   }
428      
429   return tresp;
430 }
431
432 char *tr_msg_encode(TR_MSG *msg) 
433 {
434   json_t *jmsg;
435   json_t *jmsg_type;
436
437   /* TBD -- add error handling */
438   jmsg = json_object();
439
440   switch (msg->msg_type) 
441     {
442     case TID_REQUEST:
443       jmsg_type = json_string("tid_request");
444       json_object_set_new(jmsg, "msg_type", jmsg_type);
445       json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(tr_msg_get_req(msg)));
446       break;
447
448     case TID_RESPONSE:
449       jmsg_type = json_string("tid_response");
450       json_object_set_new(jmsg, "msg_type", jmsg_type);
451       json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(tr_msg_get_resp(msg)));
452       break;
453
454       /* TBD -- Add TR message types */
455
456     default:
457       json_decref(jmsg);
458       return NULL;
459     }
460   
461   return(json_dumps(jmsg, 0));
462 }
463
464 TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
465 {
466   TR_MSG *msg;
467   json_t *jmsg = NULL;
468   json_error_t rc;
469   json_t *jtype;
470   json_t *jbody;
471   const char *mtype = NULL;
472
473   if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
474     fprintf (stderr, "tr_msg_decode(): error loading object\n");
475     return NULL;
476   }
477
478   if (!(msg = malloc(sizeof(TR_MSG)))) {
479     fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
480     json_decref(jmsg);
481     return NULL;
482   }
483  
484   memset(msg, 0, sizeof(TR_MSG));
485
486   if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
487       (NULL == (jbody = json_object_get(jmsg, "msg_body")))) {
488     fprintf (stderr, "tr_msg_decode(): Error parsing message header.\n");
489     json_decref(jmsg);
490     tr_msg_free_decoded(msg);
491     return NULL;
492   }
493
494   mtype = json_string_value(jtype);
495
496   if (0 == strcmp(mtype, "tid_request")) {
497     msg->msg_type = TID_REQUEST;
498     tr_msg_set_req(msg, tr_msg_decode_tidreq(jbody));
499   }
500   else if (0 == strcmp(mtype, "tid_response")) {
501     msg->msg_type = TID_RESPONSE;
502     tr_msg_set_resp(msg, tr_msg_decode_tidresp(jbody));
503   }
504   else {
505     msg->msg_type = TR_UNKNOWN;
506     msg->msg_rep = NULL;
507   }
508   return msg;
509 }
510
511 void tr_msg_free_encoded(char *jmsg)
512 {
513   if (jmsg)
514     free (jmsg);
515 }
516
517 void tr_msg_free_decoded(TR_MSG *msg)
518 {
519   if (msg)
520     free (msg);
521 }
522
523