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
29 #define SERVICE "sample"
32 const char *program_name
= "client";
35 doit (Shishi
* handle
, Shishi_ap
* ap
, int verbose
)
40 /* XXX Unfinished application-level security */
41 res
= shishi_safe (handle
, &safe
);
44 printf ("Could not build SAFE: %s\n", shishi_strerror (res
));
48 res
= shishi_safe_set_user_data (handle
, shishi_safe_safe (safe
), "foo", 0);
51 printf ("Could not set application data in SAFE: %s\n",
52 shishi_strerror (res
));
56 res
= shishi_safe_build (safe
, key
);
59 printf ("Could not build SAFE: %s\n", shishi_strerror (res
));
63 res
= shishi_safe_print (handle
, stdout
, shishi_safe_safe (safe
));
66 printf ("Could not print SAFE: %s\n", shishi_strerror (res
));
71 printf ("Application exchange start. Press ^D to finish.\n");
73 while (fgets (line
, sizeof (line
), stdin
))
75 printf ("read: %s", line
);
80 printf ("error reading stdin\n");
88 auth (Shishi
* h
, int verbose
, const char *cname
, const char *sname
)
92 Shishi_tkts_hint hint
;
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
);
106 printf ("cannot find ticket for \"%s\"\n", sname
);
111 shishi_tkt_pretty_print (tkt
, stderr
);
113 /* Create Authentication context */
115 rc
= shishi_ap_tktoptions (h
, &ap
, tkt
, SHISHI_APOPTIONS_MUTUAL_REQUIRED
);
118 printf ("cannot create authentication context\n");
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
);
138 printf ("cannot build authentication request: %s\n",
139 shishi_strerror (rc
));
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;
153 * rc = shishi_ap_req_der (ap, &out, &outlen);
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
)))
164 printf ("Waiting for server to authenticate itself...\n");
166 rc
= shishi_aprep_parse (h
, stdin
, &aprep
);
169 printf ("Cannot parse AP-REP from server: %s\n",
170 shishi_strerror (rc
));
174 rc
= shishi_ap_rep_verify_asn1 (ap
, aprep
);
176 printf ("AP-REP verification OK...\n");
179 if (rc
== SHISHI_APREP_VERIFY_FAILED
)
180 printf ("AP-REP verification failed...\n");
182 printf ("AP-REP verification error: %s\n", shishi_strerror (rc
));
186 /* The server is authenticated. */
187 printf ("Server authenticated.\n");
190 /* We are now authenticated. */
191 printf ("User authenticated.\n");
197 main (int argc
, char *argv
[])
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");
213 rc
= shishi_init (&h
);
216 printf ("error initializing shishi: %s\n", shishi_strerror (rc
));
223 sname
= shishi_server_for_local_service (h
, SERVICE
);
225 ap
= auth (h
, 1, shishi_principal_default (h
), sname
);
228 rc
= doit (h
, ap
, 1);