Reorganize code to have single TID protocol (no separate TPQ protocol).
[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 <tpq.h>
42 #include <tr_msg.h>
43
44 /* char tmp_key[32] = 
45   {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
46    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
47    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
48    0x19, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
49 */
50
51 int tmp_len = 32;
52
53 TPQC_INSTANCE *tpqc_create ()
54 {
55   TPQC_INSTANCE *tpqc = NULL;
56
57   if (tpqc = malloc(sizeof(TPQC_INSTANCE))) 
58     memset(tpqc, 0, sizeof(TPQC_INSTANCE));
59   else
60     return NULL;
61
62   if (NULL == (tpqc->priv_dh = tr_create_dh_params(NULL, 0))) {
63     free (tpqc);
64     return NULL;
65   }
66
67   fprintf(stderr, "TPQC DH Parameters:\n");
68   DHparams_print_fp(stdout, tpqc->priv_dh);
69   fprintf(stderr, "\n");
70
71   return tpqc;
72 }
73
74 void tpqc_destroy (TPQC_INSTANCE *tpqc)
75 {
76   if (tpqc)
77     free(tpqc);
78 }
79
80 int tpqc_open_connection (TPQC_INSTANCE *tpqc, 
81                           char *server,
82                           gss_ctx_id_t *gssctx)
83 {
84   int err = 0;
85   int conn = -1;
86
87   err = gsscon_connect(server, TPQ_PORT, &conn);
88
89   if (!err)
90     err = gsscon_active_authenticate(conn, NULL, "trustquery", gssctx);
91
92   if (!err)
93     return conn;
94   else
95     return -1;
96 }
97
98 int tpqc_send_request (TPQC_INSTANCE *tpqc, 
99                        int conn, 
100                        gss_ctx_id_t gssctx,
101                        char *rp_realm,
102                        char *realm, 
103                        char *coi,
104                        TPQC_RESP_FUNC *resp_handler,
105                        void *cookie)
106
107 {
108   json_t *jreq;
109   int err;
110   char *req_buf;
111   char *resp_buf;
112   size_t resp_buflen = 0;
113   TR_MSG *msg;
114   TPQ_REQ *tpq_req;
115
116   /* Create and populate a TPQ msg structure */
117   if ((!(msg = malloc(sizeof(TR_MSG)))) ||
118       (!(tpq_req = malloc(sizeof(TPQ_REQ)))))
119     return -1;
120
121   memset(tpq_req, 0, sizeof(tpq_req));
122
123   msg->msg_type = TPQ_REQUEST;
124
125   msg->tpq_req = tpq_req;
126
127   tpq_req->conn = conn;
128
129   /* TBD -- error handling */
130   tpq_req->rp_realm = tr_new_name(rp_realm);
131   tpq_req->realm = tr_new_name(realm);
132   tpq_req->coi = tr_new_name(coi);
133
134   tpq_req->tpqc_dh = tpqc->priv_dh;
135   
136   tpq_req->resp_func = resp_handler;
137   tpq_req->cookie = cookie;
138
139   /* Encode the request into a json string */
140   if (!(req_buf = tr_msg_encode(msg))) {
141     printf("Error encoding TPQ request.\n");
142     return -1;
143   }
144
145   printf ("Sending TPQ request:\n");
146   printf ("%s\n", req_buf);
147
148   /* Send the request over the connection */
149   if (err = gsscon_write_encrypted_token (conn, gssctx, req_buf, 
150                                           strlen(req_buf))) {
151     fprintf(stderr, "Error sending request over connection.\n");
152     return -1;
153   }
154
155   /* TBD -- should queue request on instance, resps read in separate thread */
156   /* Read the response from the connection */
157
158   if (err = gsscon_read_encrypted_token(conn, gssctx, &resp_buf, &resp_buflen)) {
159     if (resp_buf)
160       free(resp_buf);
161     return -1;
162   }
163
164   fprintf(stdout, "Response Received, %d bytes.\n", resp_buflen);
165
166   /* Parse response -- TBD */
167
168   /* Call the caller's response function */
169   (*resp_handler)(tpqc, NULL, cookie);
170
171   if (msg)
172     free(msg);
173   if (tpq_req)
174     free(tpq_req);
175   if (req_buf)
176     free(req_buf);
177   if (resp_buf)
178     free(resp_buf);
179
180   return 0;
181 }
182
183
184
185
186