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