Merge branch 'master' of moonshot.suchdamage.org:/srv/git/trust_router
[trust_router.git] / tid / tidc.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 <stdio.h>
36 #include <stdlib.h>
37 #include <jansson.h>
38
39 #include <gsscon.h>
40 #include <tr_dh.h>
41 #include <trust_router/tid.h>
42 #include <tr_msg.h>
43 #include <gsscon.h>
44
45 /* char tmp_key[32] = 
46   {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
47    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
48    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
49    0x19, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
50 */
51
52 int tmp_len = 32;
53
54 TIDC_INSTANCE *tidc_create ()
55 {
56   TIDC_INSTANCE *tidc = NULL;
57
58   if (tidc = malloc(sizeof(TIDC_INSTANCE))) 
59     memset(tidc, 0, sizeof(TIDC_INSTANCE));
60   else
61     return NULL;
62
63   if (NULL == (tidc->priv_dh = tr_create_dh_params(NULL, 0))) {
64     free (tidc);
65     return NULL;
66   }
67
68   fprintf(stderr, "TIDC DH Parameters:\n");
69   DHparams_print_fp(stdout, tidc->priv_dh);
70   fprintf(stderr, "\n");
71
72   return tidc;
73 }
74
75 void tidc_destroy (TIDC_INSTANCE *tidc)
76 {
77   if (tidc)
78     free(tidc);
79 }
80
81 int tidc_open_connection (TIDC_INSTANCE *tidc, 
82                           char *server,
83                           gss_ctx_id_t *gssctx)
84 {
85   int err = 0;
86   int conn = -1;
87
88   err = gsscon_connect(server, TID_PORT, &conn);
89
90   if (!err)
91     err = gsscon_active_authenticate(conn, NULL, "trustidentity", gssctx);
92
93   if (!err)
94     return conn;
95   else
96     return -1;
97 }
98
99 int tidc_send_request (TIDC_INSTANCE *tidc, 
100                        int conn, 
101                        gss_ctx_id_t gssctx,
102                        char *rp_realm,
103                        char *realm, 
104                        char *comm,
105                        TIDC_RESP_FUNC *resp_handler,
106                        void *cookie)
107
108 {
109   int err = 0;
110   char *req_buf = NULL;
111   char *resp_buf = NULL;
112   size_t resp_buflen = 0;
113   TR_MSG *msg = NULL;
114   TID_REQ *tid_req = NULL;
115   TR_MSG *resp_msg = NULL;
116   int c_keylen = 0;
117   unsigned char *c_keybuf = NULL;
118   int i;
119
120   /* Create and populate a TID msg structure */
121   if ((!(msg = malloc(sizeof(TR_MSG)))) ||
122       (!(tid_req = malloc(sizeof(TID_REQ)))))
123     return -1;
124
125   memset(tid_req, 0, sizeof(tid_req));
126
127   msg->msg_type = TID_REQUEST;
128
129   msg->tid_req = tid_req;
130
131   tid_req->conn = conn;
132   tid_req->gssctx = gssctx;
133
134   /* TBD -- error handling */
135   tid_req->rp_realm = tr_new_name(rp_realm);
136   tid_req->realm = tr_new_name(realm);
137   tid_req->comm = tr_new_name(comm);
138
139   tid_req->tidc_dh = tidc->priv_dh;
140   
141   tid_req->resp_func = resp_handler;
142   tid_req->cookie = cookie;
143
144   /* Encode the request into a json string */
145   if (!(req_buf = tr_msg_encode(msg))) {
146     printf("Error encoding TID request.\n");
147     return -1;
148   }
149
150   printf ("Sending TID request:\n");
151   printf ("%s\n", req_buf);
152
153   /* Send the request over the connection */
154   if (err = gsscon_write_encrypted_token (conn, gssctx, req_buf, 
155                                           strlen(req_buf))) {
156     fprintf(stderr, "Error sending request over connection.\n");
157     return -1;
158   }
159
160   /* TBD -- should queue request on instance, resps read in separate thread */
161   /* Read the response from the connection */
162
163   if (err = gsscon_read_encrypted_token(conn, gssctx, &resp_buf, &resp_buflen)) {
164     if (resp_buf)
165       free(resp_buf);
166     return -1;
167   }
168
169   fprintf(stdout, "Response Received (%d bytes).\n", resp_buflen);
170   fprintf(stdout, "%s\n", resp_buf);
171
172   if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
173     fprintf(stderr, "Error decoding response.\n");
174     return -1;
175   }
176
177   /* TBD -- Check if this is actually a valid response */
178   if (!resp_msg->tid_resp) {
179     fprintf(stderr, "Error: No response in the response!\n");
180     return -1;
181   }
182   
183   /* Call the caller's response function */
184   (*tid_req->resp_func)(tidc, tid_req, resp_msg->tid_resp, cookie);
185
186
187   /* Generate the client key -- TBD, handle more than one server */
188   if (TID_SUCCESS != resp_msg->tid_resp->result) {
189     fprintf(stderr, "Response is an error.\n");
190     return -1;
191   }
192
193   if (!resp_msg->tid_resp->servers) {
194     fprintf(stderr, "Response does not contain server info.\n");
195     return -1;
196   }
197   
198   if (NULL == (c_keybuf = malloc(DH_size(tid_req->tidc_dh)))) {
199     fprintf (stderr, "Error: Can't allocate client keybuf, exiting.\n");
200     return -1;
201   }
202   if (0 > (c_keylen = tr_compute_dh_key(c_keybuf, 
203                                       DH_size(tid_req->tidc_dh), 
204                                       resp_msg->tid_resp->servers->aaa_server_dh->pub_key, 
205                                       tid_req->tidc_dh))) {
206     
207     printf("Error computing client key.\n");
208     return -1;
209   }
210   
211   /* Print out the client key. */
212   printf("Client Key Generated (len = %d):\n", c_keylen);
213   for (i = 0; i < c_keylen; i++) {
214     printf("%x", c_keybuf[i]); 
215   }
216   printf("\n");
217
218   if (msg)
219     free(msg);
220   if (tid_req)
221     free(tid_req);
222   if (req_buf)
223     free(req_buf);
224   if (resp_buf)
225     free(resp_buf);
226
227   /* TBD -- free the decoded response */
228
229   return 0;
230 }
231
232
233
234
235