fixes for building on Centos; mostly -Werror clean at this point
[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   memset(srvr, 0, sizeof(TID_SRVR_BLK));
202
203   if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) ||
204       (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) ||
205       (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) {
206     fprintf (stderr, "tr_msg_decode_one_server(): Error parsing required fields.\n");
207     free(srvr);
208     return NULL;
209   }
210   
211   /* TBD -- handle IPv6 Addresses */
212   inet_aton(json_string_value(jsrvr_addr), &(srvr->aaa_server_addr));
213   srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
214   srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
215
216   return srvr;
217 }
218
219 static json_t *tr_msg_encode_servers(TID_SRVR_BLK *servers)
220 {
221   json_t *jservers = NULL;
222   json_t *jsrvr = NULL;
223   TID_SRVR_BLK *srvr = NULL;
224
225   jservers = json_array();
226
227   for (srvr = servers; srvr != NULL; srvr = srvr->next) {
228     if ((NULL == (jsrvr = tr_msg_encode_one_server(srvr))) ||
229         (-1 == json_array_append_new(jservers, jsrvr))) {
230       return NULL;
231     }
232   }
233
234   //  fprintf(stderr,"tr_msg_encode_servers(): servers contains:\n");
235   //  fprintf(stderr,"%s\n", json_dumps(jservers, 0));
236   return jservers;
237 }
238
239 static TID_SRVR_BLK *tr_msg_decode_servers(json_t *jservers) 
240 {
241   TID_SRVR_BLK *servers = NULL;
242   TID_SRVR_BLK *next = NULL;
243   TID_SRVR_BLK *srvr = NULL;
244   json_t *jsrvr;
245   size_t i, num_servers;
246
247   num_servers = json_array_size(jservers);
248   fprintf(stderr, "tr_msg_decode_servers(): Number of servers = %u.\n", (unsigned) num_servers);
249   
250   if (0 == num_servers) {
251     fprintf(stderr, "tr_msg_decode_servers(): Server array is empty.\n"); 
252     return NULL;
253   }
254
255   for (i = 0; i < num_servers; i++) {
256     jsrvr = json_array_get(jservers, i);
257     srvr = tr_msg_decode_one_server(jsrvr);
258
259     /* skip to the end of the list, and add srvr to list of servers */
260     if (NULL == servers) {
261       servers = srvr;
262     }
263     else {
264       for (next = servers; next->next != NULL; next = next->next);
265       next->next = srvr;
266     }
267   }
268
269   return servers;
270 }
271
272 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
273 {
274   json_t *jresp = NULL;
275   json_t *jstr = NULL;
276   json_t *jservers = NULL;
277
278   if ((!resp) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
279     return NULL;
280
281   jresp = json_object();
282
283   if (TID_ERROR == resp->result) {
284     jstr = json_string("error");
285     json_object_set_new(jresp, "result", jstr);
286     if (resp->err_msg) {
287       jstr = json_string(resp->err_msg->buf);
288       json_object_set_new(jresp, "err_msg", jstr);
289     }
290   }
291   else {
292     jstr = json_string("success");
293     json_object_set_new(jresp, "result", jstr);
294   }
295
296   jstr = json_string(resp->rp_realm->buf);
297   json_object_set_new(jresp, "rp_realm", jstr);
298
299   jstr = json_string(resp->realm->buf);
300   json_object_set_new(jresp, "target_realm", jstr);
301
302   jstr = json_string(resp->comm->buf);
303   json_object_set_new(jresp, "comm", jstr);
304
305   if (resp->orig_coi) {
306     jstr = json_string(resp->orig_coi->buf);
307     json_object_set_new(jresp, "orig_coi", jstr);
308   }
309
310   if (NULL == resp->servers) {
311     fprintf(stderr, "tr_msg_encode_tidresp(): No servers to encode.\n");
312     return jresp;
313   }
314   jservers = tr_msg_encode_servers(resp->servers);
315   json_object_set_new(jresp, "servers", jservers);
316   
317   return jresp;
318 }
319
320 static TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
321 {
322   TID_RESP *tresp = NULL;
323   json_t *jresult = NULL;
324   json_t *jerr_msg = NULL;
325   json_t *jrp_realm = NULL;
326   json_t *jrealm = NULL;
327   json_t *jcomm = NULL;
328   json_t *jorig_coi = NULL;
329   json_t *jservers = NULL;
330
331   if (!(tresp = malloc(sizeof(TID_RESP)))) {
332     fprintf (stderr, "tr_msg_decode_tidresp(): Error allocating TID_RESP structure.\n");
333     return NULL;
334   }
335  
336   memset(tresp, 0, sizeof(TID_RESP));
337
338   /* store required fields from request */
339   if ((NULL == (jresult = json_object_get(jresp, "result"))) ||
340       (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
341       (NULL == (jrealm = json_object_get(jresp, "target_realm"))) ||
342       (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
343       (NULL == (jservers = json_object_get(jresp, "servers")))) {
344     fprintf (stderr, "tr_msg_decode_tidresp(): Error parsing response.\n");
345     free(tresp);
346     return NULL;
347   }
348
349   if (0 == (strcmp(json_string_value(jresult), "success"))) {
350     fprintf(stderr, "tr_msg_decode_tidresp(): Success! result = %s.\n", json_string_value(jresult));
351     tresp->result = TID_SUCCESS;
352   }
353   else {
354     tresp->result = TID_ERROR;
355     fprintf(stderr, "tr_msg_decode_tidresp(): Error! result = %s.\n", json_string_value(jresult));
356     if (NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) {
357       tresp->err_msg = tr_new_name((char *)json_string_value(jerr_msg));
358     }
359   }
360
361   tresp->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
362   tresp->realm = tr_new_name((char *)json_string_value(jrealm));
363   tresp->comm = tr_new_name((char *)json_string_value(jcomm));
364
365   /* store optional "orig_coi" field */
366   if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
367       (!json_is_object(jorig_coi))) {
368     tresp->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
369   }
370
371   tresp->servers = tr_msg_decode_servers(jservers); 
372   
373   return tresp;
374 }
375
376 char *tr_msg_encode(TR_MSG *msg) 
377 {
378   json_t *jmsg;
379   json_t *jmsg_type;
380
381   /* TBD -- add error handling */
382   jmsg = json_object();
383
384   switch (msg->msg_type) 
385     {
386     case TID_REQUEST:
387       jmsg_type = json_string("tid_request");
388       json_object_set_new(jmsg, "msg_type", jmsg_type);
389       json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(msg->tid_req));
390       break;
391
392     case TID_RESPONSE:
393       jmsg_type = json_string("tid_response");
394       json_object_set_new(jmsg, "msg_type", jmsg_type);
395       json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(msg->tid_resp));
396       break;
397
398       /* TBD -- Add TR message types */
399
400     default:
401       json_decref(jmsg);
402       return NULL;
403     }
404   
405   return(json_dumps(jmsg, 0));
406 }
407
408 TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
409 {
410   TR_MSG *msg;
411   json_t *jmsg = NULL;
412   json_error_t rc;
413   json_t *jtype;
414   json_t *jbody;
415   const char *mtype = NULL;
416
417   if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
418     fprintf (stderr, "tr_msg_decode(): error loading object\n");
419     return NULL;
420   }
421
422   if (!(msg = malloc(sizeof(TR_MSG)))) {
423     fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
424     json_decref(jmsg);
425     return NULL;
426   }
427  
428   memset(msg, 0, sizeof(TR_MSG));
429
430   if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
431       (NULL == (jbody = json_object_get(jmsg, "msg_body")))) {
432     fprintf (stderr, "tr_msg_decode(): Error parsing message header.\n");
433     json_decref(jmsg);
434     tr_msg_free_decoded(msg);
435     return NULL;
436   }
437
438   mtype = json_string_value(jtype);
439
440   if (0 == strcmp(mtype, "tid_request")) {
441     msg->msg_type = TID_REQUEST;
442     msg->tid_req = tr_msg_decode_tidreq(jbody);
443   }
444   else if (0 == strcmp(mtype, "tid_response")) {
445     msg->msg_type = TID_RESPONSE;
446     msg->tid_resp = tr_msg_decode_tidresp(jbody);
447   }
448   else {
449     msg->msg_type = TR_UNKNOWN;
450     msg->tid_req = NULL;
451   }
452   return msg;
453 }
454
455 void tr_msg_free_encoded(char *jmsg)
456 {
457   if (jmsg)
458     free (jmsg);
459 }
460
461 void tr_msg_free_decoded(TR_MSG *msg)
462 {
463   if (msg)
464     free (msg);
465 }
466
467