2 * Copyright (c) 2012, 2014-2015, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
39 #include <trust_router/tr_dh.h>
40 #include <tid_internal.h>
46 TIDC_INSTANCE *tidc_create ()
48 TIDC_INSTANCE *tidc = NULL;
50 if (NULL == (tidc = talloc_zero(NULL, TIDC_INSTANCE)))
56 void tidc_destroy (TIDC_INSTANCE *tidc)
61 int tidc_open_connection (TIDC_INSTANCE *tidc,
68 unsigned int use_port = 0;
75 err = gsscon_connect(server, use_port, "trustidentity", &conn, gssctx);
83 int tidc_send_request (TIDC_INSTANCE *tidc,
89 TIDC_RESP_FUNC *resp_handler,
93 TID_REQ *tid_req = NULL;
95 /* Create and populate a TID req structure */
96 if (!(tid_req = tid_req_new()))
100 tid_req->gssctx = gssctx;
102 if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
103 (NULL == (tid_req->realm = tr_new_name(realm))) ||
104 (NULL == (tid_req->comm = tr_new_name(comm)))) {
105 fprintf (stderr, "tidc_send_request: Error duplicating names.\n");
109 tid_req->tidc_dh = tidc->client_dh;
111 return (tidc_fwd_request(tidc, tid_req, resp_handler, cookie));
114 int tidc_fwd_request (TIDC_INSTANCE *tidc,
116 TIDC_RESP_FUNC *resp_handler,
120 char *req_buf = NULL;
121 char *resp_buf = NULL;
122 size_t resp_buflen = 0;
124 TR_MSG *resp_msg = NULL;
127 /* Create and populate a TID msg structure */
128 if (!(msg = malloc(sizeof(TR_MSG))))
131 msg->msg_type = TID_REQUEST;
132 tr_msg_set_req(msg, tid_req);
134 /* store the response function and cookie */
135 // tid_req->resp_func = resp_handler;
136 // tid_req->cookie = cookie;
139 /* Encode the request into a json string */
140 if (!(req_buf = tr_msg_encode(msg))) {
141 fprintf(stderr, "tidc_fwd_request: Error encoding TID request.\n");
145 fprintf (stderr, "tidc_fwd_request: Sending TID request:\n");
146 fprintf (stderr, "%s\n", req_buf);
148 /* Send the request over the connection */
149 if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf,
151 fprintf(stderr, "tidc_fwd_request: Error sending request over connection.\n");
155 /* TBD -- queue request on instance, read resps in separate thread */
157 /* Read the response from the connection */
158 /* TBD -- timeout? */
159 if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
165 fprintf(stdout, "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
166 fprintf(stdout, "%s\n", resp_buf);
168 if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
169 fprintf(stderr, "tidc_fwd_request: Error decoding response.\n");
173 /* TBD -- Check if this is actually a valid response */
174 if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
175 fprintf(stderr, "tidc_fwd_request: Error, no response in the response!\n");
180 /* Call the caller's response function */
181 (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
183 fprintf(stderr, "tidc_fwd_request: NULL response function.\n");
188 tid_req_free(tid_req);
194 /* TBD -- free the decoded response */
200 DH * tidc_get_dh(TIDC_INSTANCE *inst)
202 return inst->client_dh;
205 DH *tidc_set_dh(TIDC_INSTANCE *inst, DH *dh)
207 inst->client_dh = dh;