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