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