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>
41 static json_t *tr_msg_encode_dh(DH *dh)
46 if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
51 jbn = json_string(BN_bn2hex(dh->p));
52 json_object_set_new(jdh, "dh_p", jbn);
54 jbn = json_string(BN_bn2hex(dh->g));
55 json_object_set_new(jdh, "dh_g", jbn);
57 jbn = json_string(BN_bn2hex(dh->pub_key));
58 json_object_set_new(jdh, "dh_pub_key", jbn);
63 static DH *tr_msg_decode_dh(json_t *jdh)
70 json_t *tr_msg_encode_tpqreq(TPQ_REQ *req)
75 if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->coi))
80 jstr = json_string(req->rp_realm->buf);
81 json_object_set_new(jreq, "rp_realm", jstr);
83 jstr = json_string(req->realm->buf);
84 json_object_set_new(jreq, "target_realm", jstr);
86 jstr = json_string(req->coi->buf);
87 json_object_set_new(jreq, "community", jstr);
89 json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tpqc_dh));
94 TPQ_REQ *tr_msg_decode_tpqreq(json_t *jreq)
101 json_t *tr_msg_encode_tpqresp(TPQ_RESP *resp)
103 json_t *jresp = NULL;
109 TPQ_RESP *tr_msg_decode_tpqresp(json_t *jresp)
111 TPQ_RESP *resp = NULL;
116 json_t *tr_msg_encode_tidrreq(TIDR_REQ *req)
124 TIDR_REQ *tr_msg_decode_tidrreq(json_t *jreq)
126 TIDR_REQ *req = NULL;
131 json_t *tr_msg_encode_tidrresp(TIDR_RESP *resp)
133 json_t *jresp = NULL;
138 TIDR_RESP *tr_msg_decode_tidrresp(json_t *jresp)
140 TIDR_RESP *resp = NULL;
145 char *tr_msg_encode(TR_MSG *msg)
150 /* TBD -- add error handling */
151 jmsg = json_object();
153 switch (msg->msg_type)
156 jmsg_type = json_string("TPQRequest");
157 json_object_set_new(jmsg, "msg_type", jmsg_type);
158 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tpqreq(msg->tpq_req));
162 jmsg_type = json_string("TPQResponse");
163 json_object_set_new(jmsg, "msg_type", jmsg_type);
164 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tpqresp(msg->tpq_resp));
168 jmsg_type = json_string("TIDRequest");
169 json_object_set_new(jmsg, "msg_type", jmsg_type);
170 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidrreq(msg->tidr_req));
174 jmsg_type = json_string("TIDResponse");
175 json_object_set_new(jmsg, "msg_type", jmsg_type);
176 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidrresp(msg->tidr_resp));
179 /* TBD -- Add TR message types */
186 return(json_dumps(jmsg, 0));
189 TR_MSG *tr_msg_decode(char *jmsg)
193 if (!(msg = malloc(sizeof(TR_MSG *)))) {
194 fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
201 void tr_msg_free_encoded(char *jmsg)
207 void tr_msg_free_decoded(TR_MSG *msg)