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