2 * Copyright (c) 2012, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #include <openssl/dh.h>
40 #include <trust_router/tr_name.h>
41 #include <trust_router/tid.h>
43 static json_t *tr_msg_encode_dh(DH *dh)
48 if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
53 jbn = json_string(BN_bn2hex(dh->p));
54 json_object_set_new(jdh, "dh_p", jbn);
56 jbn = json_string(BN_bn2hex(dh->g));
57 json_object_set_new(jdh, "dh_g", jbn);
59 jbn = json_string(BN_bn2hex(dh->pub_key));
60 json_object_set_new(jdh, "dh_pub_key", jbn);
65 static DH *tr_msg_decode_dh(json_t *jdh)
71 json_t *jpub_key = NULL;
74 if (!(dh = malloc(sizeof(DH)))) {
75 fprintf (stderr, "tr_msg_decode_dh(): Error allocating DH structure.\n");
79 memset(dh, 0, sizeof(DH));
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");
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));
101 static json_t * tr_msg_encode_tidreq(TID_REQ *req)
106 if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
109 jreq = json_object();
111 jstr = json_string(req->rp_realm->buf);
112 json_object_set_new(jreq, "rp_realm", jstr);
114 jstr = json_string(req->realm->buf);
115 json_object_set_new(jreq, "target_realm", jstr);
117 jstr = json_string(req->comm->buf);
118 json_object_set_new(jreq, "community", jstr);
120 json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
125 static TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
127 TID_REQ *treq = NULL;
129 json_t *jrp_realm = NULL;
130 json_t *jrealm = NULL;
131 json_t *jcomm = NULL;
132 json_t *jorig_coi = NULL;
136 if (!(treq = malloc(sizeof(TID_REQ)))) {
137 fprintf (stderr, "tr_msg_decode_tidreq(): Error allocating TID_REQ structure.\n");
141 memset(treq, 0, sizeof(TID_REQ));
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");
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);
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));
172 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
174 json_t *jresp = NULL;
177 if ((!resp) || (!resp->result) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
180 jresp = json_object();
182 jstr = json_string(resp->result->buf);
183 json_object_set_new(jresp, "result", jstr);
185 jstr = json_string(resp->rp_realm->buf);
186 json_object_set_new(jresp, "rp_realm", jstr);
188 jstr = json_string(resp->realm->buf);
189 json_object_set_new(jresp, "target_realm", jstr);
191 jstr = json_string(resp->comm->buf);
192 json_object_set_new(jresp, "comm", jstr);
194 if (resp->orig_coi) {
195 jstr = json_string(resp->orig_coi->buf);
196 json_object_set_new(jresp, "orig_coi", jstr);
199 // TBD -- Encode server info.
204 static TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
206 TID_RESP *tresp = NULL;
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;
216 if (!(tresp = malloc(sizeof(TID_RESP)))) {
217 fprintf (stderr, "tr_msg_decode_tidresp(): Error allocating TID_RESP structure.\n");
221 memset(tresp, 0, sizeof(TID_RESP));
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");
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));
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));
251 // Decode server info
252 // tresp->servers = tr_msg_decode_servers(jservers);
257 char *tr_msg_encode(TR_MSG *msg)
262 /* TBD -- add error handling */
263 jmsg = json_object();
265 switch (msg->msg_type)
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));
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));
279 /* TBD -- Add TR message types */
286 return(json_dumps(jmsg, 0));
289 TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
297 const char *mtype = NULL;
299 if (NULL == (jmsg = json_loadb(jbuf, buflen, 0, &rc))) {
300 fprintf (stderr, "tr_msg_decode(): error loading object, rc = %d.\n", rc);
304 if (!(msg = malloc(sizeof(TR_MSG)))) {
305 fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
310 memset(msg, 0, sizeof(TR_MSG));
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");
319 tr_msg_free_decoded(msg);
323 mtype = json_string_value(jtype);
325 if (0 == strcmp(mtype, "TIDRequest")) {
326 msg->msg_type = TID_REQUEST;
327 msg->tid_req = tr_msg_decode_tidreq(jbody);
329 else if (0 == strcmp(mtype, "TIDResponse")) {
330 msg->msg_type = TID_RESPONSE;
331 msg->tid_resp = tr_msg_decode_tidresp(jbody);
334 msg->msg_type = TR_UNKNOWN;
340 void tr_msg_free_encoded(char *jmsg)
346 void tr_msg_free_decoded(TR_MSG *msg)