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>
42 static json_t *tr_msg_encode_dh(DH *dh)
47 if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
52 jbn = json_string(BN_bn2hex(dh->p));
53 json_object_set_new(jdh, "dh_p", jbn);
55 jbn = json_string(BN_bn2hex(dh->g));
56 json_object_set_new(jdh, "dh_g", jbn);
58 jbn = json_string(BN_bn2hex(dh->pub_key));
59 json_object_set_new(jdh, "dh_pub_key", jbn);
64 static DH *tr_msg_decode_dh(json_t *jdh)
70 json_t *jpub_key = NULL;
73 if (!(dh = malloc(sizeof(DH)))) {
74 fprintf (stderr, "tr_msg_decode_dh(): Error allocating DH structure.\n");
78 memset(dh, 0, sizeof(DH));
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");
93 dh->p = json_string_value(jp);
94 dh->g = json_string_value(jg);
95 dh->pub_key = json_string_value(jpub_key);
100 json_t *tr_msg_encode_tidreq(TID_REQ *req)
105 if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
108 jreq = json_object();
110 jstr = json_string(req->rp_realm->buf);
111 json_object_set_new(jreq, "rp_realm", jstr);
113 jstr = json_string(req->realm->buf);
114 json_object_set_new(jreq, "target_realm", jstr);
116 jstr = json_string(req->comm->buf);
117 json_object_set_new(jreq, "community", jstr);
119 json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
124 TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
126 TID_REQ *treq = NULL;
128 json_t *jrp_realm = NULL;
129 json_t *jrealm = NULL;
130 json_t *jcomm = NULL;
131 json_t *jorig_coi = NULL;
135 if (!(treq = malloc(sizeof(TID_REQ)))) {
136 fprintf (stderr, "tr_msg_decode_tidreq(): Error allocating TID_REQ structure.\n");
140 memset(treq, 0, sizeof(TID_REQ));
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");
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);
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);
175 json_t *tr_msg_encode_tidresp(TID_RESP *resp)
177 json_t *jresp = NULL;
180 if ((!resp) || (!resp->result) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
183 jresp = json_object();
185 jstr = json_string(resp->result->buf);
186 json_object_set_new(jresp, "result", jstr);
188 jstr = json_string(resp->rp_realm->buf);
189 json_object_set_new(jresp, "rp_realm", jstr);
191 jstr = json_string(resp->realm->buf);
192 json_object_set_new(jresp, "target_realm", jstr);
194 jstr = json_string(resp->comm->buf);
195 json_object_set_new(jresp, "comm", jstr);
197 if (resp->orig_coi) {
198 jstr = json_string(resp->orig_coi->buf);
199 json_object_set_new(jresp, "orig_coi", jstr);
202 // TBD -- Encode server info.
207 TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
209 TID_RESP *tresp = NULL;
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;
219 if (!(tresp = malloc(sizeof(TID_RESP)))) {
220 fprintf (stderr, "tr_msg_decode_tidresp(): Error allocating TID_RESP structure.\n");
224 memset(tresp, 0, sizeof(TID_RESP));
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");
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);
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);
259 // Decode server info
260 // tresp->servers = tr_msg_decode_servers(jservers);
265 char *tr_msg_encode(TR_MSG *msg)
270 /* TBD -- add error handling */
271 jmsg = json_object();
273 switch (msg->msg_type)
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));
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));
287 /* TBD -- Add TR message types */
294 return(json_dumps(jmsg, 0));
297 TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
305 const char *mtype = NULL;
307 if (NULL == (jmsg = json_loadb(jbuf, buflen, 0, &rc))) {
308 fprintf (stderr, "tr_msg_decode(): error loading object, rc = %d.\n", rc);
312 if (!(msg = malloc(sizeof(TR_MSG)))) {
313 fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
318 memset(msg, 0, sizeof(TR_MSG));
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");
327 tr_msg_free_decoded(msg);
331 mtype = json_string_value(jtype);
333 if (0 == strcmp(mtype, "TIDRequest")) {
334 msg->msg_type = TID_REQUEST;
335 msg->tid_req = tr_msg_decode_tidreq(jbody);
337 else if (0 == strcmp(mtype, "TIDResponse")) {
338 msg->msg_type = TID_RESPONSE;
339 msg->tid_resp = tr_msg_decode_tidresp(jbody);
342 msg->msg_type = TR_UNKNOWN;
348 void tr_msg_free_encoded(char *jmsg)
354 void tr_msg_free_decoded(TR_MSG *msg)