74cdcff0d95851da648dcf97ca1b9160627f995a
[trust_router.git] / tpq / tpqc.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
43 char tmp_key[32] = 
44   {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
45    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
46    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
47    0x19, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
48 int tmp_len = 32;
49
50 TPQC_INSTANCE *tpqc_create ()
51 {
52   TPQC_INSTANCE *tpqc = NULL;
53
54   if (tpqc = malloc(sizeof(TPQC_INSTANCE))) 
55     memset(tpqc, 0, sizeof(TPQC_INSTANCE));
56   else
57     return NULL;
58
59   /* TBD -- Generate random private key */
60   tpqc->priv_key = tmp_key;
61   tpqc->priv_len = tmp_len;
62
63   if (NULL == (tpqc->priv_dh = tr_create_dh_params(tpqc->priv_key, tpqc->priv_len))) {
64     free (tpqc);
65     return NULL;
66   }
67
68   fprintf(stderr, "TPQC DH Parameters:\n");
69   DHparams_print_fp(stdout, tpqc->priv_dh);
70   fprintf(stderr, "\n");
71
72   return tpqc;
73 }
74
75 void tpqc_destroy (TPQC_INSTANCE *tpqc)
76 {
77   if (tpqc)
78     free(tpqc);
79 }
80
81 int tpqc_open_connection (TPQC_INSTANCE *tpqc, 
82                           char *server,
83                           gss_ctx_id_t *gssctx)
84 {
85   int err = 0;
86   int conn = -1;
87
88   err = gsscon_connect(server, TPQ_PORT, &conn);
89
90   if (!err)
91     err = gsscon_active_authenticate(conn, NULL, "trustquery", gssctx);
92
93   if (!err)
94     return conn;
95   else
96     return -1;
97 }
98
99 int tpqc_send_request (TPQC_INSTANCE *tpqc, 
100                        int conn, 
101                        gss_ctx_id_t gssctx,
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
114   /* Create a json TPQ request */
115   if (NULL == (jreq = json_object())) {
116     fprintf(stderr,"Error creating json object.\n");
117     return -1;
118   }
119
120   if (0 > (err = json_object_set_new(jreq, "type", json_string("tpq_request")))) {
121     fprintf(stderr, "Error adding type to request.\n");
122     return -1;
123   }
124
125   /* Insert realm and coi into the json request */
126   if (0 > (err = json_object_set_new(jreq, "realm", json_string(realm)))) {
127     fprintf(stderr, "Error adding realm to request.\n");
128     return -1;
129   }
130   if (0 > (err = json_object_set_new(jreq, "coi", json_string(coi)))) {
131     fprintf(stderr, "Error adding coi to request.\n");
132     return -1;
133   }
134
135   /* Generate half of a D-H exchange -- TBD */
136   /* Insert D-H information into the request -- TBD */
137
138   /* Encode the json request */
139   if (NULL == (req_buf = json_dumps(jreq, 0))) {
140     fprintf(stderr, "Error encoding json request.\n");
141     return -1;
142   }
143   
144   printf("Encoded request:\n%s\n", req_buf);
145   
146   /* Send the request over the connection */
147   if (err = gsscon_write_encrypted_token (conn, gssctx, req_buf, 
148                                           strlen(req_buf) + 1)) {
149     fprintf(stderr, "Error sending request over connection.\n");
150     return -1;
151   }
152
153   free(req_buf);
154
155   /* read the response from the connection */
156
157   if (err = gsscon_read_encrypted_token(conn, gssctx, &resp_buf, &resp_buflen)) {
158     if (resp_buf)
159       free(resp_buf);
160     return -1;
161   }
162
163   fprintf(stdout, "Response Received, %d bytes.\n", resp_buflen);
164
165   /* Parse response -- TBD */
166
167   /* Call the caller's response function */
168   (*resp_handler)(tpqc, NULL, cookie);
169
170   if (resp_buf)
171     free(resp_buf);
172
173   return 0;
174 }
175
176
177
178
179