Added keyname to server block in request/response.
[trust_router.git] / tid / example / tids_main.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 <string.h>
37
38 #include <trust_router/tid.h>
39 #include <tr_dh.h>
40
41 static int tids_req_handler (TIDS_INSTANCE * tids,
42                       TID_REQ *req, 
43                       TID_RESP **resp,
44                       void *cookie)
45 {
46   unsigned char *s_keybuf = NULL;
47   int s_keylen = 0;
48   int i = 0;
49
50   printf("Request received! target_realm = %s, community = %s\n", req->realm->buf, req->comm->buf);
51   if (tids)
52     tids->req_count++;
53
54   if (!(resp) || !(*resp)) {
55     printf("tids_req_handler: No response structure.\n");
56     return -1;
57   }
58
59   /* Allocate a new server block */
60   if (NULL == ((*resp)->servers = malloc(sizeof(TID_SRVR_BLK)))){
61     printf("tids_req_handler(): malloc failed.\n");
62     return -1;
63   }
64   memset((*resp)->servers, 0, sizeof(TID_SRVR_BLK));
65
66   /* TBD -- Set up the server IP Address */
67
68   if (!(req) || !(req->tidc_dh)) {
69     printf("tids_req_handler(): No client DH info.\n");
70     return -1;
71   }
72
73   if ((!req->tidc_dh->p) || (!req->tidc_dh->g)) {
74     printf("tids_req_handler(): NULL dh values.\n");
75     return -1;
76   }
77
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));
81
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");
84     return -1;
85   }
86
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. 
89    */
90   if (0 == inet_aton("127.0.0.1", &((*resp)->servers->aaa_server_addr))) {
91     printf("tids_req_handler(): inet_aton() failed.\n");
92     return -1;
93   }
94
95   /* Set the key name */
96   (*resp)->servers->key_name = tr_new_name("placeholder.key.name");
97
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");
102     return -1;
103   }
104
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.");
110     return -1;
111   }
112
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]); 
117   }
118   printf("\n");
119   return s_keylen;
120 }
121
122 int main (int argc, 
123           const char *argv[]) 
124 {
125   static TIDS_INSTANCE *tids;
126   int rc = 0;
127
128   /* Parse command-line arguments */ 
129   if (argc != 1)
130     printf("Unexpected arguments, ignored.\n");
131
132   /* Create a TID server instance */
133   if (NULL == (tids = tids_create())) {
134     printf("Unable to create TIDS instance,exiting.\n");
135     return 1;
136   }
137
138   /* Start-up the server, won't return unless there is an error. */
139   rc = tids_start(tids, &tids_req_handler , NULL);
140   
141   printf("Error in tids_start(), rc = %d. Exiting.\n", rc);
142
143   /* Clean-up the TID server instance */
144   tids_destroy(tids);
145
146   return 1;
147 }
148