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)
71 json_t *tr_msg_encode_tidreq(TID_REQ *req)
76 if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->coi))
81 jstr = json_string(req->rp_realm->buf);
82 json_object_set_new(jreq, "rp_realm", jstr);
84 jstr = json_string(req->realm->buf);
85 json_object_set_new(jreq, "target_realm", jstr);
87 jstr = json_string(req->coi->buf);
88 json_object_set_new(jreq, "community", jstr);
90 json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
95 TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
102 json_t *tr_msg_encode_tidresp(TID_RESP *resp)
104 json_t *jresp = NULL;
110 TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
112 TID_RESP *resp = NULL;
117 char *tr_msg_encode(TR_MSG *msg)
122 /* TBD -- add error handling */
123 jmsg = json_object();
125 switch (msg->msg_type)
128 jmsg_type = json_string("TIDRequest");
129 json_object_set_new(jmsg, "msg_type", jmsg_type);
130 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(msg->tid_req));
134 jmsg_type = json_string("TIDResponse");
135 json_object_set_new(jmsg, "msg_type", jmsg_type);
136 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(msg->tid_resp));
139 /* TBD -- Add TR message types */
146 return(json_dumps(jmsg, 0));
149 TR_MSG *tr_msg_decode(char *jmsg)
153 if (!(msg = malloc(sizeof(TR_MSG *)))) {
154 fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
161 void tr_msg_free_encoded(char *jmsg)
167 void tr_msg_free_decoded(TR_MSG *msg)