1 /* client-priv.c --- Sample Shishi authenticated client, with privacy
2 * protected application data exchange.
3 * Copyright (C) 2003, 2004 Simon Josefsson
5 * This file is part of Shishi.
7 * Shishi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Shishi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Shishi; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth 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 printf ("Application exchange start. Press ^D to finish.\n");
42 while (fgets (line
, sizeof (line
), stdin
))
46 line
[strlen(line
)-1] = '\0';
47 printf ("read: %s\n", line
);
49 res
= shishi_priv (handle
, &priv
);
52 printf ("Could not build PRIV: %s\n", shishi_strerror (res
));
56 res
= shishi_encprivpart_set_user_data (handle
,
57 shishi_priv_encprivpart (priv
),
61 printf ("Could not set application data in PRIV: %s\n",
62 shishi_strerror (res
));
66 res
= shishi_priv_build (priv
, shishi_ap_key (ap
));
69 printf ("Could not build PRIV: %s\n", shishi_strerror (res
));
73 res
= shishi_priv_print (handle
, stdout
, shishi_priv_priv (priv
));
76 printf ("Could not print PRIV: %s\n", shishi_strerror (res
));
83 printf ("error reading stdin\n");
91 auth (Shishi
* h
, int verbose
, const char *cname
, const char *sname
)
95 Shishi_tkts_hint hint
;
98 printf ("Client: %s\n", cname
);
99 printf ("Server: %s\n", sname
);
101 /* Get a ticket for the server. */
103 memset (&hint
, 0, sizeof (hint
));
104 hint
.client
= (char *) cname
;
105 hint
.server
= (char *) sname
;
106 tkt
= shishi_tkts_get (shishi_tkts_default (h
), &hint
);
109 printf ("cannot find ticket for \"%s\"\n", sname
);
114 shishi_tkt_pretty_print (tkt
, stderr
);
116 /* Create Authentication context */
118 rc
= shishi_ap_tktoptions (h
, &ap
, tkt
, SHISHI_APOPTIONS_MUTUAL_REQUIRED
);
121 printf ("cannot create authentication context\n");
125 /* Build Authentication request */
127 rc
= shishi_ap_req_build (ap
);
130 printf ("cannot build authentication request: %s\n",
131 shishi_strerror (rc
));
136 shishi_authenticator_print (h
, stderr
, shishi_ap_authenticator (ap
));
138 /* Authentication ourself to server */
140 shishi_apreq_print (h
, stdout
, shishi_ap_req (ap
));
141 /* Note: to get the binary blob to send, use:
143 * char *out; int outlen;
145 * rc = shishi_ap_req_der (ap, &out, &outlen);
147 * write(fd, out, outlen);
150 /* For mutual authentication, wait for server reply. */
152 if (shishi_apreq_mutual_required_p (h
, shishi_ap_req (ap
)))
156 printf ("Waiting for server to authenticate itself...\n");
158 rc
= shishi_aprep_parse (h
, stdin
, &aprep
);
161 printf ("Cannot parse AP-REP from server: %s\n",
162 shishi_strerror (rc
));
166 rc
= shishi_ap_rep_verify_asn1 (ap
, aprep
);
168 printf ("AP-REP verification OK...\n");
171 if (rc
== SHISHI_APREP_VERIFY_FAILED
)
172 printf ("AP-REP verification failed...\n");
174 printf ("AP-REP verification error: %s\n", shishi_strerror (rc
));
178 /* The server is authenticated. */
179 printf ("Server authenticated.\n");
182 /* We are now authenticated. */
183 printf ("User authenticated.\n");
189 main (int argc
, char *argv
[])
196 printf ("sample-client (shishi " SHISHI_VERSION
")\n");
198 if (!shishi_check_version (SHISHI_VERSION
))
200 printf ("shishi_check_version() failed:\n"
201 "Header file incompatible with shared library.\n");
205 rc
= shishi_init (&h
);
208 printf ("error initializing shishi: %s\n", shishi_strerror (rc
));
215 sname
= shishi_server_for_local_service (h
, SERVICE
);
217 ap
= auth (h
, 1, shishi_principal_default (h
), sname
);
220 rc
= doit (h
, ap
, 1);