Enable warnings; fix many of them
[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
35 #include <string.h>
36 #include <openssl/dh.h>
37 #include <jansson.h>
38
39 #include <tr_msg.h>
40 #include <trust_router/tr_name.h>
41 #include <trust_router/tid.h>
42
43 static json_t *tr_msg_encode_dh(DH *dh)
44 {
45   json_t *jdh = NULL;
46   json_t *jbn = NULL;
47
48   if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
49     return NULL;
50
51   jdh = json_object();
52
53   jbn = json_string(BN_bn2hex(dh->p));
54   json_object_set_new(jdh, "dh_p", jbn);
55
56   jbn = json_string(BN_bn2hex(dh->g));
57   json_object_set_new(jdh, "dh_g", jbn);
58
59   jbn = json_string(BN_bn2hex(dh->pub_key));
60   json_object_set_new(jdh, "dh_pub_key", jbn);
61
62   return jdh;
63 }
64
65 static DH *tr_msg_decode_dh(json_t *jdh)
66 {
67   DH *dh = NULL;
68   json_error_t rc;
69   json_t *jp = NULL;
70   json_t *jg = NULL;
71   json_t *jpub_key = NULL;
72   int msize;
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 (((msize = json_object_size(jdh)) < 3) ||
83       (NULL == (jp = json_object_get(jdh, "dh_p"))) ||
84       (!json_is_string(jp)) ||
85       (NULL == (jg = json_object_get(jdh, "dh_g"))) ||
86       (!json_is_string(jg)) ||
87       (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key"))) ||
88       (!json_is_string(jdh))) {
89     fprintf (stderr, "tr_msg_decode(): Error parsing message.\n");
90     free(dh);
91     return NULL;
92   }
93
94   BN_hex2bn(&(dh->p), json_string_value(jp));
95   BN_hex2bn(&(dh->g), json_string_value(jg));
96   BN_hex2bn(&(dh->pub_key), json_string_value(jpub_key));
97
98   return dh;
99 }
100
101 static json_t * tr_msg_encode_tidreq(TID_REQ *req)
102 {
103   json_t *jreq = NULL;
104   json_t *jstr = NULL;
105
106   if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
107     return NULL;
108
109   jreq = json_object();
110
111   jstr = json_string(req->rp_realm->buf);
112   json_object_set_new(jreq, "rp_realm", jstr);
113
114   jstr = json_string(req->realm->buf);
115   json_object_set_new(jreq, "target_realm", jstr);
116
117   jstr = json_string(req->comm->buf);
118   json_object_set_new(jreq, "community", jstr);
119
120   json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
121   
122   return jreq;
123 }
124
125 static TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
126 {
127   TID_REQ *treq = NULL;
128   json_error_t rc;
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   int msize;
135
136   if (!(treq = malloc(sizeof(TID_REQ)))) {
137     fprintf (stderr, "tr_msg_decode_tidreq(): Error allocating TID_REQ structure.\n");
138     return NULL;
139   }
140  
141   memset(treq, 0, sizeof(TID_REQ));
142
143   /* store required fields from request */
144   if (((msize = json_object_size(jreq)) < 4) ||
145       (NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) ||
146       (!json_is_string(jrp_realm)) ||
147       (NULL == (jrealm = json_object_get(jreq, "realm"))) ||
148       (!json_is_string(jrealm)) ||
149       (NULL == (jcomm = json_object_get(jreq, "comm"))) ||
150       (!json_is_string(jcomm)) ||
151       (NULL == (jdh = json_object_get(jreq, "dh_info"))) ||
152       (!json_is_object(jdh))) {
153     fprintf (stderr, "tr_msg_decode(): Error parsing message.\n");
154     free(treq);
155     return NULL;
156   }
157
158   treq->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
159   treq->realm = tr_new_name((char *)json_string_value(jrealm));
160   treq->comm = tr_new_name((char *)json_string_value(jcomm));
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       (!json_is_object(jorig_coi))) {
166     treq->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
167   }
168
169   return treq;
170 }
171
172 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
173 {
174   json_t *jresp = NULL;
175   json_t *jstr = NULL;
176
177   if ((!resp) || (!resp->result) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
178     return NULL;
179
180   jresp = json_object();
181
182   jstr = json_string(resp->result->buf);
183   json_object_set_new(jresp, "result", jstr);
184
185   jstr = json_string(resp->rp_realm->buf);
186   json_object_set_new(jresp, "rp_realm", jstr);
187
188   jstr = json_string(resp->realm->buf);
189   json_object_set_new(jresp, "target_realm", jstr);
190
191   jstr = json_string(resp->comm->buf);
192   json_object_set_new(jresp, "comm", jstr);
193
194   if (resp->orig_coi) {
195     jstr = json_string(resp->orig_coi->buf);
196     json_object_set_new(jresp, "orig_coi", jstr);
197   }
198
199   // TBD -- Encode server info.
200   
201   return jresp;
202 }
203
204 static TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
205 {
206   TID_RESP *tresp = NULL;
207   json_error_t rc;
208   json_t *jresult = NULL;
209   json_t *jrp_realm = NULL;
210   json_t *jrealm = NULL;
211   json_t *jcomm = NULL;
212   json_t *jorig_coi = NULL;
213   json_t *jservers = NULL;
214   int msize;
215
216   if (!(tresp = malloc(sizeof(TID_RESP)))) {
217     fprintf (stderr, "tr_msg_decode_tidresp(): Error allocating TID_RESP structure.\n");
218     return NULL;
219   }
220  
221   memset(tresp, 0, sizeof(TID_RESP));
222
223   /* store required fields from request */
224   if (((msize = json_object_size(jresp)) < 5) ||
225       (NULL == (jresult = json_object_get(jresp, "result"))) ||
226       (!json_is_string(jresult)) ||
227       (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
228       (!json_is_string(jrp_realm)) ||
229       (NULL == (jrealm = json_object_get(jresp, "realm"))) ||
230       (!json_is_string(jrealm)) ||
231       (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
232       (!json_is_string(jcomm)) ||
233       (NULL == (jservers = json_object_get(jresp, "servers"))) ||
234       (!json_is_object(jservers))) {
235     fprintf (stderr, "tr_msg_decode(): Error parsing message.\n");
236     free(tresp);
237     return NULL;
238   }
239
240   tresp->result = tr_new_name((char *)json_string_value(jresult));
241   tresp->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
242   tresp->realm = tr_new_name((char *)json_string_value(jrealm));
243   tresp->comm = tr_new_name((char *)json_string_value(jcomm));
244
245   /* store optional "orig_coi" field */
246   if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
247       (!json_is_object(jorig_coi))) {
248     tresp->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
249   }
250
251   //  Decode server info
252   //  tresp->servers = tr_msg_decode_servers(jservers); 
253   
254   return tresp;
255 }
256
257 char *tr_msg_encode(TR_MSG *msg) 
258 {
259   json_t *jmsg;
260   json_t *jmsg_type;
261
262   /* TBD -- add error handling */
263   jmsg = json_object();
264
265   switch (msg->msg_type) 
266     {
267     case TID_REQUEST:
268       jmsg_type = json_string("TIDRequest");
269       json_object_set_new(jmsg, "msg_type", jmsg_type);
270       json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(msg->tid_req));
271       break;
272
273     case TID_RESPONSE:
274       jmsg_type = json_string("TIDResponse");
275       json_object_set_new(jmsg, "msg_type", jmsg_type);
276       json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(msg->tid_resp));
277       break;
278
279       /* TBD -- Add TR message types */
280
281     default:
282       json_decref(jmsg);
283       return NULL;
284     }
285   
286   return(json_dumps(jmsg, 0));
287 }
288
289 TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
290 {
291   TR_MSG *msg;
292   json_t *jmsg = NULL;
293   json_error_t rc;
294   size_t msize;
295   json_t *jtype;
296   json_t *jbody;
297   const char *mtype = NULL;
298
299   if (NULL == (jmsg = json_loadb(jbuf, buflen, 0, &rc))) {
300     fprintf (stderr, "tr_msg_decode(): error loading object, rc = %d.\n", rc);
301     return NULL;
302   }
303
304   if (!(msg = malloc(sizeof(TR_MSG)))) {
305     fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
306     json_decref(jmsg);
307     return NULL;
308   }
309  
310   memset(msg, 0, sizeof(TR_MSG));
311
312   if ((2 != (msize = json_object_size(jmsg))) ||
313       (NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
314       (!json_is_string(jtype)) ||
315       (NULL == (jbody = json_object_get(jmsg, "msg_body"))) ||
316       (!json_is_object(jbody))) {
317     fprintf (stderr, "tr_msg_decode(): Error parsing message.\n");
318     json_decref(jmsg);
319     tr_msg_free_decoded(msg);
320     return NULL;
321   }
322
323   mtype = json_string_value(jtype);
324
325   if (0 == strcmp(mtype, "TIDRequest")) {
326     msg->msg_type = TID_REQUEST;
327     msg->tid_req = tr_msg_decode_tidreq(jbody);
328   }
329   else if (0 == strcmp(mtype, "TIDResponse")) {
330     msg->msg_type = TID_RESPONSE;
331     msg->tid_resp = tr_msg_decode_tidresp(jbody);
332   }
333   else {
334     msg->msg_type = TR_UNKNOWN;
335     msg->tid_req = NULL;
336   }
337   return msg;
338 }
339
340 void tr_msg_free_encoded(char *jmsg)
341 {
342   if (jmsg)
343     free (jmsg);
344 }
345
346 void tr_msg_free_decoded(TR_MSG *msg)
347 {
348   if (msg)
349     free (msg);
350 }
351
352