Add accessors for TR_MSG structure.
[trust_router.git] / common / tr_msg.c
1 /*
2  * Copyright (c) 2012, 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
41 #include <tr_msg.h>
42 #include <trust_router/tr_name.h>
43 #include <trust_router/tid.h>
44
45 enum msg_type tr_msg_get_msg_type(TR_MSG *msg) 
46 {
47   return msg->msg_type;
48 }
49
50 void tr_msg_set_msg_type(TR_MSG *msg, enum msg_type type)
51 {
52   msg->msg_type = type;
53 }
54
55 TID_REQ *tr_msg_get_req(TR_MSG *msg)
56 {
57   return msg->tid_req;
58 }
59
60 void tr_msg_set_req(TR_MSG *msg, TID_REQ *req)
61 {
62   msg->tid_req = req;
63 }
64
65 TID_RESP *tr_msg_get_resp(TR_MSG *msg)
66 {
67   return msg->tid_resp;
68 }
69
70 void tr_msg_set_resp(TR_MSG *msg, TID_RESP *resp)
71 {
72   msg->tid_resp = resp;
73 }
74
75 static json_t *tr_msg_encode_dh(DH *dh)
76 {
77   json_t *jdh = NULL;
78   json_t *jbn = NULL;
79
80   if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
81     return NULL;
82
83   jdh = json_object();
84
85   jbn = json_string(BN_bn2hex(dh->p));
86   json_object_set_new(jdh, "dh_p", jbn);
87
88   jbn = json_string(BN_bn2hex(dh->g));
89   json_object_set_new(jdh, "dh_g", jbn);
90
91   jbn = json_string(BN_bn2hex(dh->pub_key));
92   json_object_set_new(jdh, "dh_pub_key", jbn);
93
94   return jdh;
95 }
96
97 static DH *tr_msg_decode_dh(json_t *jdh)
98 {
99   DH *dh = NULL;
100   json_t *jp = NULL;
101   json_t *jg = NULL;
102   json_t *jpub_key = NULL;
103
104   if (!(dh = malloc(sizeof(DH)))) {
105     fprintf (stderr, "tr_msg_decode_dh(): Error allocating DH structure.\n");
106     return NULL;
107   }
108  
109   memset(dh, 0, sizeof(DH));
110
111   /* store required fields from dh object */
112   if ((NULL == (jp = json_object_get(jdh, "dh_p"))) ||
113       (NULL == (jg = json_object_get(jdh, "dh_g"))) ||
114       (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key")))) {
115     fprintf (stderr, "tr_msg_decode_dh(): Error parsing dh_info.\n");
116     free(dh);
117     return NULL;
118   }
119
120   BN_hex2bn(&(dh->p), json_string_value(jp));
121   BN_hex2bn(&(dh->g), json_string_value(jg));
122   BN_hex2bn(&(dh->pub_key), json_string_value(jpub_key));
123
124   return dh;
125 }
126
127 static json_t * tr_msg_encode_tidreq(TID_REQ *req)
128 {
129   json_t *jreq = NULL;
130   json_t *jstr = NULL;
131
132   if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
133     return NULL;
134
135   jreq = json_object();
136
137   jstr = json_string(req->rp_realm->buf);
138   json_object_set_new(jreq, "rp_realm", jstr);
139
140   jstr = json_string(req->realm->buf);
141   json_object_set_new(jreq, "target_realm", jstr);
142
143   jstr = json_string(req->comm->buf);
144   json_object_set_new(jreq, "community", jstr);
145   
146   if (req->orig_coi) {
147     jstr = json_string(req->orig_coi->buf);
148     json_object_set_new(jreq, "orig_coi", jstr);
149   }
150
151   json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
152   
153   return jreq;
154 }
155
156 static TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
157 {
158   TID_REQ *treq = NULL;
159   json_t *jrp_realm = NULL;
160   json_t *jrealm = NULL;
161   json_t *jcomm = NULL;
162   json_t *jorig_coi = NULL;
163   json_t *jdh = NULL;
164
165   if (!(treq = malloc(sizeof(TID_REQ)))) {
166     fprintf (stderr, "tr_msg_decode_tidreq(): Error allocating TID_REQ structure.\n");
167     return NULL;
168   }
169  
170   memset(treq, 0, sizeof(TID_REQ));
171
172   /* store required fields from request */
173   if ((NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) ||
174       (NULL == (jrealm = json_object_get(jreq, "target_realm"))) ||
175       (NULL == (jcomm = json_object_get(jreq, "community")))) {
176     fprintf (stderr, "tr_msg_decode(): Error parsing required fields.\n");
177     free(treq);
178     return NULL;
179   }
180
181   treq->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
182   treq->realm = tr_new_name((char *)json_string_value(jrealm));
183   treq->comm = tr_new_name((char *)json_string_value(jcomm));
184
185   /* Get DH Info from the request */
186   if (NULL == (jdh = json_object_get(jreq, "dh_info"))) {
187     fprintf (stderr, "tr_msg_decode(): Error parsing dh_info.\n");
188     free(treq);
189     return NULL;
190   }
191   treq->tidc_dh = tr_msg_decode_dh(jdh);
192
193   /* store optional "orig_coi" field */
194   if (NULL != (jorig_coi = json_object_get(jreq, "orig_coi"))) {
195     treq->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
196   }
197
198   return treq;
199 }
200
201 static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr)
202 {
203   json_t *jsrvr = NULL;
204   json_t *jstr = NULL;
205
206   fprintf(stderr, "Encoding one server.\n");
207
208   jsrvr = json_object();
209
210   /* Server IP Address -- TBD handle IPv6 */
211   jstr = json_string(inet_ntoa(srvr->aaa_server_addr));
212   json_object_set_new(jsrvr, "server_addr", jstr);
213
214   /* Server DH Block */
215   jstr = json_string(srvr->key_name->buf);
216   json_object_set_new(jsrvr, "key_name", jstr);
217   json_object_set_new(jsrvr, "server_dh", tr_msg_encode_dh(srvr->aaa_server_dh));
218   
219   //  fprintf(stderr,"tr_msg_encode_one_server(): jsrvr contains:\n");
220   //  fprintf(stderr,"%s\n", json_dumps(jsrvr, 0));
221   return jsrvr;
222 }
223
224 static TID_SRVR_BLK *tr_msg_decode_one_server(json_t *jsrvr) 
225 {
226   TID_SRVR_BLK *srvr;
227   json_t *jsrvr_addr = NULL;
228   json_t *jsrvr_kn = NULL;
229   json_t *jsrvr_dh = NULL;
230
231   if (jsrvr == NULL)
232     return NULL;
233
234   if (NULL == (srvr = malloc(sizeof(TID_SRVR_BLK)))) 
235     return NULL;
236   memset(srvr, 0, sizeof(TID_SRVR_BLK));
237
238   if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) ||
239       (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) ||
240       (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) {
241     fprintf (stderr, "tr_msg_decode_one_server(): Error parsing required fields.\n");
242     free(srvr);
243     return NULL;
244   }
245   
246   /* TBD -- handle IPv6 Addresses */
247   inet_aton(json_string_value(jsrvr_addr), &(srvr->aaa_server_addr));
248   srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
249   srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
250
251   return srvr;
252 }
253
254 static json_t *tr_msg_encode_servers(TID_SRVR_BLK *servers)
255 {
256   json_t *jservers = NULL;
257   json_t *jsrvr = NULL;
258   TID_SRVR_BLK *srvr = NULL;
259
260   jservers = json_array();
261
262   for (srvr = servers; srvr != NULL; srvr = srvr->next) {
263     if ((NULL == (jsrvr = tr_msg_encode_one_server(srvr))) ||
264         (-1 == json_array_append_new(jservers, jsrvr))) {
265       return NULL;
266     }
267   }
268
269   //  fprintf(stderr,"tr_msg_encode_servers(): servers contains:\n");
270   //  fprintf(stderr,"%s\n", json_dumps(jservers, 0));
271   return jservers;
272 }
273
274 static TID_SRVR_BLK *tr_msg_decode_servers(json_t *jservers) 
275 {
276   TID_SRVR_BLK *servers = NULL;
277   TID_SRVR_BLK *next = NULL;
278   TID_SRVR_BLK *srvr = NULL;
279   json_t *jsrvr;
280   size_t i, num_servers;
281
282   num_servers = json_array_size(jservers);
283   fprintf(stderr, "tr_msg_decode_servers(): Number of servers = %u.\n", (unsigned) num_servers);
284   
285   if (0 == num_servers) {
286     fprintf(stderr, "tr_msg_decode_servers(): Server array is empty.\n"); 
287     return NULL;
288   }
289
290   for (i = 0; i < num_servers; i++) {
291     jsrvr = json_array_get(jservers, i);
292     srvr = tr_msg_decode_one_server(jsrvr);
293
294     /* skip to the end of the list, and add srvr to list of servers */
295     if (NULL == servers) {
296       servers = srvr;
297     }
298     else {
299       for (next = servers; next->next != NULL; next = next->next);
300       next->next = srvr;
301     }
302   }
303
304   return servers;
305 }
306
307 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
308 {
309   json_t *jresp = NULL;
310   json_t *jstr = NULL;
311   json_t *jservers = NULL;
312
313   if ((!resp) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
314     return NULL;
315
316   jresp = json_object();
317
318   if (TID_ERROR == resp->result) {
319     jstr = json_string("error");
320     json_object_set_new(jresp, "result", jstr);
321     if (resp->err_msg) {
322       jstr = json_string(resp->err_msg->buf);
323       json_object_set_new(jresp, "err_msg", jstr);
324     }
325   }
326   else {
327     jstr = json_string("success");
328     json_object_set_new(jresp, "result", jstr);
329   }
330
331   jstr = json_string(resp->rp_realm->buf);
332   json_object_set_new(jresp, "rp_realm", jstr);
333
334   jstr = json_string(resp->realm->buf);
335   json_object_set_new(jresp, "target_realm", jstr);
336
337   jstr = json_string(resp->comm->buf);
338   json_object_set_new(jresp, "comm", jstr);
339
340   if (resp->orig_coi) {
341     jstr = json_string(resp->orig_coi->buf);
342     json_object_set_new(jresp, "orig_coi", jstr);
343   }
344
345   if (NULL == resp->servers) {
346     fprintf(stderr, "tr_msg_encode_tidresp(): No servers to encode.\n");
347     return jresp;
348   }
349   jservers = tr_msg_encode_servers(resp->servers);
350   json_object_set_new(jresp, "servers", jservers);
351   
352   return jresp;
353 }
354
355 static TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
356 {
357   TID_RESP *tresp = NULL;
358   json_t *jresult = NULL;
359   json_t *jrp_realm = NULL;
360   json_t *jrealm = NULL;
361   json_t *jcomm = NULL;
362   json_t *jorig_coi = NULL;
363   json_t *jservers = NULL;
364   json_t *jerr_msg = NULL;
365
366   if (!(tresp = malloc(sizeof(TID_RESP)))) {
367     fprintf (stderr, "tr_msg_decode_tidresp(): Error allocating TID_RESP structure.\n");
368     return NULL;
369   }
370  
371   memset(tresp, 0, sizeof(TID_RESP));
372
373   /* store required fields from response */
374   if ((NULL == (jresult = json_object_get(jresp, "result"))) ||
375       (!json_is_string(jresult)) ||
376       (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
377       (!json_is_string(jrp_realm)) ||
378       (NULL == (jrealm = json_object_get(jresp, "target_realm"))) ||
379       (!json_is_string(jrealm)) ||
380       (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
381       (!json_is_string(jcomm))) {
382     fprintf (stderr, "tr_msg_decode_tidresp(): Error parsing response.\n");
383     free(tresp);
384     return NULL;
385   }
386
387   if (0 == (strcmp(json_string_value(jresult), "success"))) {
388     fprintf(stderr, "tr_msg_decode_tidresp(): Success! result = %s.\n", json_string_value(jresult));
389     if ((NULL != (jservers = json_object_get(jresp, "servers"))) ||
390         (!json_is_array(jservers))) {
391       tresp->servers = tr_msg_decode_servers(jservers); 
392     } 
393     else {
394       return NULL;
395     }
396     tresp->result = TID_SUCCESS;
397   }
398   else {
399     tresp->result = TID_ERROR;
400     fprintf(stderr, "tr_msg_decode_tidresp(): Error! result = %s.\n", json_string_value(jresult));
401     if ((NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) ||
402         (!json_is_string(jerr_msg))) {
403       tresp->err_msg = tr_new_name((char *)json_string_value(jerr_msg));
404     }
405   }
406
407   tresp->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
408   tresp->realm = tr_new_name((char *)json_string_value(jrealm));
409   tresp->comm = tr_new_name((char *)json_string_value(jcomm));
410
411   /* store optional "orig_coi" field */
412   if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
413       (!json_is_object(jorig_coi))) {
414     tresp->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
415   }
416      
417   return tresp;
418 }
419
420 char *tr_msg_encode(TR_MSG *msg) 
421 {
422   json_t *jmsg;
423   json_t *jmsg_type;
424
425   /* TBD -- add error handling */
426   jmsg = json_object();
427
428   switch (msg->msg_type) 
429     {
430     case TID_REQUEST:
431       jmsg_type = json_string("tid_request");
432       json_object_set_new(jmsg, "msg_type", jmsg_type);
433       json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(msg->tid_req));
434       break;
435
436     case TID_RESPONSE:
437       jmsg_type = json_string("tid_response");
438       json_object_set_new(jmsg, "msg_type", jmsg_type);
439       json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(msg->tid_resp));
440       break;
441
442       /* TBD -- Add TR message types */
443
444     default:
445       json_decref(jmsg);
446       return NULL;
447     }
448   
449   return(json_dumps(jmsg, 0));
450 }
451
452 TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
453 {
454   TR_MSG *msg;
455   json_t *jmsg = NULL;
456   json_error_t rc;
457   json_t *jtype;
458   json_t *jbody;
459   const char *mtype = NULL;
460
461   if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
462     fprintf (stderr, "tr_msg_decode(): error loading object\n");
463     return NULL;
464   }
465
466   if (!(msg = malloc(sizeof(TR_MSG)))) {
467     fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
468     json_decref(jmsg);
469     return NULL;
470   }
471  
472   memset(msg, 0, sizeof(TR_MSG));
473
474   if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
475       (NULL == (jbody = json_object_get(jmsg, "msg_body")))) {
476     fprintf (stderr, "tr_msg_decode(): Error parsing message header.\n");
477     json_decref(jmsg);
478     tr_msg_free_decoded(msg);
479     return NULL;
480   }
481
482   mtype = json_string_value(jtype);
483
484   if (0 == strcmp(mtype, "tid_request")) {
485     msg->msg_type = TID_REQUEST;
486     msg->tid_req = tr_msg_decode_tidreq(jbody);
487   }
488   else if (0 == strcmp(mtype, "tid_response")) {
489     msg->msg_type = TID_RESPONSE;
490     msg->tid_resp = tr_msg_decode_tidresp(jbody);
491   }
492   else {
493     msg->msg_type = TR_UNKNOWN;
494     msg->tid_req = NULL;
495   }
496   return msg;
497 }
498
499 void tr_msg_free_encoded(char *jmsg)
500 {
501   if (jmsg)
502     free (jmsg);
503 }
504
505 void tr_msg_free_decoded(TR_MSG *msg)
506 {
507   if (msg)
508     free (msg);
509 }
510
511