Add GPLv3, since automake installs GPLv2.
[shishi.git] / examples / client-cksum.c
blob048e268311e6b4758d27c82f4238fdb626056707
1 /* client-cksum.c --- Sample Shishi authenticated client, with checksum data.
2 * Copyright (C) 2003, 2004, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include <shishi.h>
29 #define SERVICE "sample"
31 /* XXX remove this */
32 const char *program_name = "client";
34 static int
35 doit (Shishi * handle, Shishi_ap * ap, int verbose)
37 char line[BUFSIZ];
39 #if 0
40 /* XXX Unfinished application-level security */
41 res = shishi_safe (handle, &safe);
42 if (res != SHISHI_OK)
44 printf ("Could not build SAFE: %s\n", shishi_strerror (res));
45 return res;
48 res = shishi_safe_set_user_data (handle, shishi_safe_safe (safe), "foo", 0);
49 if (res != SHISHI_OK)
51 printf ("Could not set application data in SAFE: %s\n",
52 shishi_strerror (res));
53 return res;
56 res = shishi_safe_build (safe, key);
57 if (res != SHISHI_OK)
59 printf ("Could not build SAFE: %s\n", shishi_strerror (res));
60 return res;
63 res = shishi_safe_print (handle, stdout, shishi_safe_safe (safe));
64 if (res != SHISHI_OK)
66 printf ("Could not print SAFE: %s\n", shishi_strerror (res));
67 return res;
69 #endif
71 printf ("Application exchange start. Press ^D to finish.\n");
73 while (fgets (line, sizeof (line), stdin))
75 printf ("read: %s", line);
78 if (ferror (stdin))
80 printf ("error reading stdin\n");
81 return 1;
84 return 0;
87 static Shishi_ap *
88 auth (Shishi * h, int verbose, const char *cname, const char *sname)
90 Shishi_ap *ap;
91 Shishi_tkt *tkt;
92 Shishi_tkts_hint hint;
93 int rc;
95 printf ("Client: %s\n", cname);
96 printf ("Server: %s\n", sname);
98 /* Get a ticket for the server. */
100 memset (&hint, 0, sizeof (hint));
101 hint.client = (char *) cname;
102 hint.server = (char *) sname;
103 tkt = shishi_tkts_get (shishi_tkts_default (h), &hint);
104 if (!tkt)
106 printf ("cannot find ticket for \"%s\"\n", sname);
107 return NULL;
110 if (verbose)
111 shishi_tkt_pretty_print (tkt, stderr);
113 /* Create Authentication context */
115 rc = shishi_ap_tktoptions (h, &ap, tkt, SHISHI_APOPTIONS_MUTUAL_REQUIRED);
116 if (rc != SHISHI_OK)
118 printf ("cannot create authentication context\n");
119 return NULL;
122 /* Add checksum of some application data to the AP.
124 * Note that only a pointer to this memory area is stored in the AP,
125 * so you MUST keep it allocated, at least until
126 * shishi_ap_req_build(ap) is finished. This might be changed in
127 * the future, probably by copying the data into the AP.
130 shishi_ap_authenticator_cksumdata_set (ap, "attack at dawn",
131 strlen ("attack at dawn"));
133 /* Build Authentication request */
135 rc = shishi_ap_req_build (ap);
136 if (rc != SHISHI_OK)
138 printf ("cannot build authentication request: %s\n",
139 shishi_strerror (rc));
140 return NULL;
143 if (verbose)
144 shishi_authenticator_print (h, stderr, shishi_ap_authenticator (ap));
146 /* Authentication ourself to server */
148 shishi_apreq_print (h, stdout, shishi_ap_req (ap));
149 /* Note: to get the binary blob to send, use:
151 * char *out; int outlen;
152 * ...
153 * rc = shishi_ap_req_der (ap, &out, &outlen);
154 * ...
155 * write(fd, out, outlen);
158 /* For mutual authentication, wait for server reply. */
160 if (shishi_apreq_mutual_required_p (h, shishi_ap_req (ap)))
162 Shishi_asn1 aprep;
164 printf ("Waiting for server to authenticate itself...\n");
166 rc = shishi_aprep_parse (h, stdin, &aprep);
167 if (rc != SHISHI_OK)
169 printf ("Cannot parse AP-REP from server: %s\n",
170 shishi_strerror (rc));
171 return NULL;
174 rc = shishi_ap_rep_verify_asn1 (ap, aprep);
175 if (rc == SHISHI_OK)
176 printf ("AP-REP verification OK...\n");
177 else
179 if (rc == SHISHI_APREP_VERIFY_FAILED)
180 printf ("AP-REP verification failed...\n");
181 else
182 printf ("AP-REP verification error: %s\n", shishi_strerror (rc));
183 return NULL;
186 /* The server is authenticated. */
187 printf ("Server authenticated.\n");
190 /* We are now authenticated. */
191 printf ("User authenticated.\n");
193 return ap;
197 main (int argc, char *argv[])
199 Shishi *h;
200 Shishi_ap *ap;
201 char *sname;
202 int rc;
204 printf ("sample-client (shishi " SHISHI_VERSION ")\n");
206 if (!shishi_check_version (SHISHI_VERSION))
208 printf ("shishi_check_version() failed:\n"
209 "Header file incompatible with shared library.\n");
210 return 1;
213 rc = shishi_init (&h);
214 if (rc != SHISHI_OK)
216 printf ("error initializing shishi: %s\n", shishi_strerror (rc));
217 return 1;
220 if (argc > 1)
221 sname = argv[1];
222 else
223 sname = shishi_server_for_local_service (h, SERVICE);
225 ap = auth (h, 1, shishi_principal_default (h), sname);
227 if (ap)
228 rc = doit (h, ap, 1);
229 else
230 rc = 1;
232 shishi_done (h);
234 return rc;