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