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>
48 static int tidc_destructor(void *obj)
50 TIDC_INSTANCE *tidc=talloc_get_type_abort(obj, TIDC_INSTANCE);
52 if (NULL!=tidc->client_dh)
53 tr_destroy_dh_params(tidc->client_dh);
58 /* creates struct in talloc null context */
59 TIDC_INSTANCE *tidc_create(void)
61 TIDC_INSTANCE *tidc=talloc(NULL, TIDC_INSTANCE);
64 talloc_set_destructor((void *)tidc, tidc_destructor);
69 void tidc_destroy(TIDC_INSTANCE *tidc)
74 int tidc_open_connection (TIDC_INSTANCE *tidc,
81 unsigned int use_port = 0;
88 tr_debug("tidc_open_connection: opening tidc connection to %s:%d", server, port);
89 err = gsscon_connect(server, use_port, "trustidentity", &conn, gssctx);
97 int tidc_send_request (TIDC_INSTANCE *tidc,
100 const char *rp_realm,
103 TIDC_RESP_FUNC *resp_handler,
106 TID_REQ *tid_req = NULL;
109 /* Create and populate a TID req structure */
110 if (!(tid_req = tid_req_new()))
113 tid_req->conn = conn;
114 tid_req->gssctx = gssctx;
116 if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
117 (NULL == (tid_req->realm = tr_new_name(realm))) ||
118 (NULL == (tid_req->comm = tr_new_name(comm)))) {
119 tr_err ( "tidc_send_request: Error duplicating names.\n");
123 tid_req->tidc_dh = tr_dh_dup(tidc->client_dh);
125 rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
130 tid_req_free(tid_req);
134 int tidc_fwd_request(TIDC_INSTANCE *tidc,
136 TIDC_RESP_FUNC *resp_handler,
139 char *req_buf = NULL;
140 char *resp_buf = NULL;
141 size_t resp_buflen = 0;
143 TR_MSG *resp_msg = NULL;
147 /* Create and populate a TID msg structure */
148 if (!(msg = talloc_zero(tid_req, TR_MSG)))
151 msg->msg_type = TID_REQUEST;
152 tr_msg_set_req(msg, tid_req);
154 /* store the response function and cookie */
155 // tid_req->resp_func = resp_handler;
156 // tid_req->cookie = cookie;
159 /* Encode the request into a json string */
160 if (!(req_buf = tr_msg_encode(msg))) {
161 tr_err("tidc_fwd_request: Error encoding TID request.\n");
165 tr_debug( "tidc_fwd_request: Sending TID request:\n");
166 tr_debug( "%s\n", req_buf);
168 /* Send the request over the connection */
169 if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf,
171 tr_err( "tidc_fwd_request: Error sending request over connection.\n");
175 /* TBD -- queue request on instance, read resps in separate thread */
177 /* Read the response from the connection */
178 /* TBD -- timeout? */
179 if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
185 tr_debug( "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
186 tr_debug( "%s\n", resp_buf);
188 if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
189 tr_err( "tidc_fwd_request: Error decoding response.\n");
193 /* TBD -- Check if this is actually a valid response */
194 if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
195 tr_err( "tidc_fwd_request: Error, no response in the response!\n");
200 /* Call the caller's response function. It must copy any data it needs before returning. */
201 tr_debug("tidc_fwd_request: calling response callback function.");
202 (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
217 tr_msg_free_decoded(resp_msg);
222 DH * tidc_get_dh(TIDC_INSTANCE *inst)
224 return inst->client_dh;
227 DH *tidc_set_dh(TIDC_INSTANCE *inst, DH *dh)
229 inst->client_dh = dh;