751a504d590638b1dbbf0bc27871ba1bb799041f
[trust_router.git] / tid / example / tidc_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 <stdlib.h>
36 #include <stdio.h>
37
38 #include <gsscon.h>
39 #include <trust_router/tid.h>
40 #include <trust_router/tr_dh.h>
41
42 void static tidc_print_usage (const char *name)
43 {
44   printf("Usage: %s <server> <RP-realm> <target-realm> <community>\n", name);
45 }
46
47 static void tidc_resp_handler (TIDC_INSTANCE * tidc, 
48                         TID_REQ *req,
49                         TID_RESP *resp, 
50                         void *cookie) 
51 {
52   int c_keylen = 0;
53   unsigned char *c_keybuf = NULL;
54   int i;
55
56   printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
57
58   /* Generate the client key -- TBD, handle more than one server */
59   if (TID_SUCCESS != resp->result) {
60     fprintf(stderr, "tidc_resp_handler: Response is an error.\n");
61     return;
62   }
63
64   if (!resp->servers) {
65     fprintf(stderr, "tidc_resp_handler: Response does not contain server info.\n");
66     return;
67   }
68   
69   if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf, 
70                                       resp->servers->aaa_server_dh->pub_key, 
71                                       req->tidc_dh))) {
72     
73     printf("tidc_resp_handler: Error computing client key.\n");
74     return;
75   }
76   
77   /* Print out the client key. */
78   printf("Client Key Generated (len = %d):\n", c_keylen);
79   for (i = 0; i < c_keylen; i++) {
80     printf("%.2x", c_keybuf[i]); 
81   }
82   printf("\n");
83
84   return;
85 }
86
87 int main (int argc, 
88           const char *argv[]) 
89 {
90   TIDC_INSTANCE *tidc;
91   char *server = NULL;
92   char *rp_realm = NULL;
93   char *realm = NULL;
94   char *coi = NULL;
95   int conn = 0;
96   int rc;
97   gss_ctx_id_t gssctx;
98
99   /* Parse command-line arguments */ 
100   if (argc != 5) {
101     tidc_print_usage(argv[0]);
102     exit(1);
103   }
104
105   /* TBD -- validity checking, dealing with quotes, etc. */
106   server = (char *)argv[1];
107   rp_realm = (char *) argv[2];
108   realm = (char *)argv[3];
109   coi = (char *)argv[4];
110
111   printf("TIDC Client:\nServer = %s, rp_realm = %s, target_realm = %s, community = %s\n", server, rp_realm, realm, coi);
112  
113   /* Create a TID client instance & the client DH */
114   tidc = tidc_create();
115   if (NULL == (tidc->client_dh = tr_create_dh_params(NULL, 0))) {
116     printf("Error creating client DH params.\n");
117     return 1;
118   }
119
120   /* Set-up TID connection */
121   if (-1 == (conn = tidc_open_connection(tidc, server, TID_PORT, &gssctx))) {
122     /* Handle error */
123     printf("Error in tidc_open_connection.\n");
124     return 1;
125   };
126
127   /* Send a TID request */
128   if (0 > (rc = tidc_send_request(tidc, conn, gssctx, rp_realm, realm, coi, 
129                                   &tidc_resp_handler, NULL))) {
130     /* Handle error */
131     printf("Error in tidc_send_request, rc = %d.\n", rc);
132     return 1;
133   }
134     
135   /* Clean-up the TID client instance, and exit */
136   tidc_destroy(tidc);
137
138   return 0;
139 }
140