Internal changes to reflect merge of tpq and tid protocols.
[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 <tid.h>
41
42 static json_t *tr_msg_encode_dh(DH *dh)
43 {
44   json_t *jdh = NULL;
45   json_t *jbn = NULL;
46
47   if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
48     return NULL;
49
50   jdh = json_object();
51
52   jbn = json_string(BN_bn2hex(dh->p));
53   json_object_set_new(jdh, "dh_p", jbn);
54
55   jbn = json_string(BN_bn2hex(dh->g));
56   json_object_set_new(jdh, "dh_g", jbn);
57
58   jbn = json_string(BN_bn2hex(dh->pub_key));
59   json_object_set_new(jdh, "dh_pub_key", jbn);
60
61   return jdh;
62 }
63
64 static DH *tr_msg_decode_dh(json_t *jdh)
65 {
66   DH *dh = NULL;
67
68   return dh;
69 }
70
71 json_t *tr_msg_encode_tidreq(TID_REQ *req)
72 {
73   json_t *jreq = NULL;
74   json_t *jstr = NULL;
75
76   if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->coi))
77     return NULL;
78
79   jreq = json_object();
80
81   jstr = json_string(req->rp_realm->buf);
82   json_object_set_new(jreq, "rp_realm", jstr);
83
84   jstr = json_string(req->realm->buf);
85   json_object_set_new(jreq, "target_realm", jstr);
86
87   jstr = json_string(req->coi->buf);
88   json_object_set_new(jreq, "community", jstr);
89
90   json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
91   
92   return jreq;
93 }
94
95 TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
96 {
97   TID_REQ *req = NULL;
98
99   return req;
100 }
101
102 json_t *tr_msg_encode_tidresp(TID_RESP *resp)
103 {
104   json_t *jresp = NULL;
105
106   return jresp;
107 }
108
109
110 TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
111 {
112   TID_RESP *resp = NULL;
113
114   return resp;
115 }
116
117 char *tr_msg_encode(TR_MSG *msg) 
118 {
119   json_t *jmsg;
120   json_t *jmsg_type;
121
122   /* TBD -- add error handling */
123   jmsg = json_object();
124
125   switch (msg->msg_type) 
126     {
127     case TID_REQUEST:
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));
131       break;
132
133     case TID_RESPONSE:
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));
137       break;
138
139       /* TBD -- Add TR message types */
140
141     default:
142       json_decref(jmsg);
143       return NULL;
144     }
145   
146   return(json_dumps(jmsg, 0));
147 }
148
149 TR_MSG *tr_msg_decode(char *jmsg)
150 {
151   TR_MSG *msg;
152
153   if (!(msg = malloc(sizeof(TR_MSG *)))) {
154     fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
155     return NULL;
156   }
157
158   return msg;
159 }
160
161 void tr_msg_free_encoded(char *jmsg)
162 {
163   if (jmsg)
164     free (jmsg);
165 }
166
167 void tr_msg_free_decoded(TR_MSG *msg)
168 {
169   if (msg)
170     free (msg);
171 }
172
173