2 * Copyright (c) 2012, 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.
38 #include <trust_router/tid.h>
41 static int tids_req_handler (TIDS_INSTANCE * tids,
46 unsigned char *s_keybuf = NULL;
50 printf("Request received! target_realm = %s, community = %s\n", req->realm->buf, req->comm->buf);
54 if (!(resp) || !(*resp)) {
55 printf("tids_req_handler: No response structure.\n");
59 /* Allocate a new server block */
60 if (NULL == ((*resp)->servers = malloc(sizeof(TID_SRVR_BLK)))){
61 printf("tids_req_handler(): malloc failed.\n");
64 memset((*resp)->servers, 0, sizeof(TID_SRVR_BLK));
66 /* TBD -- Set up the server IP Address */
68 if (!(req) || !(req->tidc_dh)) {
69 printf("tids_req_handler(): No client DH info.\n");
73 if ((!req->tidc_dh->p) || (!req->tidc_dh->g)) {
74 printf("tids_req_handler(): NULL dh values.\n");
78 /* Generate the server DH block based on the client DH block */
79 printf("Generating the server DH block.\n");
80 printf("...from client DH block, dh_g = %s, dh_p = %s.\n", BN_bn2hex(req->tidc_dh->g), BN_bn2hex(req->tidc_dh->p));
82 if (NULL == ((*resp)->servers->aaa_server_dh = tr_create_matching_dh(NULL, 0, req->tidc_dh))) {
83 printf("tids_req_handler(): Can't create server DH params.\n");
87 /* Hard-code the IP Address in the response. If this were a AAA server, we'd expect
88 * this to be set by the Trust Router before calling us.
90 if (0 == inet_aton("127.0.0.1", &((*resp)->servers->aaa_server_addr))) {
91 printf("tids_req_handler(): inet_aton() failed.\n");
95 /* Set the key name */
96 (*resp)->servers->key_name = tr_new_name("placeholder.key.name");
98 /* Generate the server key */
99 printf("Generating the server key.\n");
100 if (NULL == (s_keybuf = malloc(DH_size((*resp)->servers->aaa_server_dh)))) {
101 printf ("tids_req_handler(): Can't allocate server keybuf.\n");
105 if (0 > (s_keylen = tr_compute_dh_key(s_keybuf,
106 DH_size((*resp)->servers->aaa_server_dh),
107 req->tidc_dh->pub_key,
108 (*resp)->servers->aaa_server_dh))) {
109 printf("tids_req_handler(): Key computation failed.");
113 /* Print out the key. If this were a AAA server, we'd store the key. */
114 printf("tids_req_handler(): Server Key Generated (len = %d):\n", s_keylen);
115 for (i = 0; i < s_keylen; i++) {
116 printf("%x", s_keybuf[i]);
125 static TIDS_INSTANCE *tids;
128 /* Parse command-line arguments */
130 printf("Unexpected arguments, ignored.\n");
132 /* Create a TID server instance */
133 if (NULL == (tids = tids_create())) {
134 printf("Unable to create TIDS instance,exiting.\n");
138 /* Start-up the server, won't return unless there is an error. */
139 rc = tids_start(tids, &tids_req_handler , NULL);
141 printf("Error in tids_start(), rc = %d. Exiting.\n", rc);
143 /* Clean-up the TID server instance */