Updated DH code, added code to tpqc to send DH info, removed extra gsscon messages.
[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
35 #include <string.h>
36 #include <openssl/dh.h>
37 #include <jansson.h>
38
39 #include <tr_msg.h>
40 #include <tpq.h>
41 static json_t *tr_msg_encode_dh(DH *dh)
42 {
43   json_t *jdh = NULL;
44   json_t *jbn = NULL;
45
46   if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
47     return NULL;
48
49   jdh = json_object();
50
51   jbn = json_string(BN_bn2hex(dh->p));
52   json_object_set_new(jdh, "dh_p", jbn);
53
54   jbn = json_string(BN_bn2hex(dh->g));
55   json_object_set_new(jdh, "dh_g", jbn);
56
57   jbn = json_string(BN_bn2hex(dh->pub_key));
58   json_object_set_new(jdh, "dh_pub_key", jbn);
59
60   return jdh;
61 }
62
63 static DH *tr_msg_decode_dh(json_t *jdh)
64 {
65   DH *dh = NULL;
66
67   return dh;
68 }
69
70 json_t *tr_msg_encode_tpqreq(TPQ_REQ *req)
71 {
72   json_t *jreq = NULL;
73   json_t *jstr = NULL;
74
75   if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->coi))
76     return NULL;
77
78   jreq = json_object();
79
80   jstr = json_string(req->rp_realm->buf);
81   json_object_set_new(jreq, "rp_realm", jstr);
82
83   jstr = json_string(req->realm->buf);
84   json_object_set_new(jreq, "target_realm", jstr);
85
86   jstr = json_string(req->coi->buf);
87   json_object_set_new(jreq, "community", jstr);
88
89   json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tpqc_dh));
90   
91   return jreq;
92 }
93
94 TPQ_REQ *tr_msg_decode_tpqreq(json_t *jreq)
95 {
96   TPQ_REQ *req = NULL;
97
98   return req;
99 }
100
101 json_t *tr_msg_encode_tpqresp(TPQ_RESP *resp)
102 {
103   json_t *jresp = NULL;
104
105   return jresp;
106 }
107
108
109 TPQ_RESP *tr_msg_decode_tpqresp(json_t *jresp)
110 {
111   TPQ_RESP *resp = NULL;
112
113   return resp;
114 }
115
116 json_t *tr_msg_encode_tidrreq(TIDR_REQ *req)
117 {
118   json_t *jreq = NULL;
119
120   return jreq;
121
122 }
123
124 TIDR_REQ *tr_msg_decode_tidrreq(json_t *jreq)
125 {
126   TIDR_REQ *req = NULL;
127
128   return req;
129 }
130
131 json_t *tr_msg_encode_tidrresp(TIDR_RESP *resp)
132 {
133   json_t *jresp = NULL;
134
135   return jresp;
136 }
137
138 TIDR_RESP *tr_msg_decode_tidrresp(json_t *jresp)
139 {
140   TIDR_RESP *resp = NULL;
141
142   return resp;
143 }
144
145 char *tr_msg_encode(TR_MSG *msg) 
146 {
147   json_t *jmsg;
148   json_t *jmsg_type;
149
150   /* TBD -- add error handling */
151   jmsg = json_object();
152
153   switch (msg->msg_type) 
154     {
155     case TPQ_REQUEST:
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));
159       break;
160
161     case TPQ_RESPONSE:
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));
165       break;
166
167     case TIDR_REQUEST:
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));
171       break;
172
173     case TIDR_RESPONSE:
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));
177       break;
178
179       /* TBD -- Add TR message types */
180
181     default:
182       json_decref(jmsg);
183       return NULL;
184     }
185   
186   return(json_dumps(jmsg, 0));
187 }
188
189 TR_MSG *tr_msg_decode(char *jmsg)
190 {
191   TR_MSG *msg;
192
193   if (!(msg = malloc(sizeof(TR_MSG *)))) {
194     fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
195     return NULL;
196   }
197
198   return msg;
199 }
200
201 void tr_msg_free_encoded(char *jmsg)
202 {
203   if (jmsg)
204     free (jmsg);
205 }
206
207 void tr_msg_free_decoded(TR_MSG *msg)
208 {
209   if (msg)
210     free (msg);
211 }
212
213