1 /* server.c --- Sample server with authentication using Shishi.
2 * Copyright (C) 2003, 2004 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 #define SERVICE "sample"
30 const char *program_name
= "client";
33 doit (Shishi
* h
, Shishi_ap
* ap
, int verbose
)
41 printf ("Application exchange start. Press ^D to finish.\n");
43 while ((res
= shishi_safe_parse (h
, stdin
, &asn1safe
)) == SHISHI_OK
)
47 fprintf (stderr
, "Could not read SAFE:\n%s\n%s\n",
48 shishi_strerror (res
), shishi_error (h
));
52 res
= shishi_safe (h
, &safe
);
55 fprintf (stderr
, "Could not create SAFE:\n%s\n%s\n",
56 shishi_strerror (res
), shishi_error (h
));
60 shishi_safe_safe_set (safe
, asn1safe
);
62 res
= shishi_safe_verify (safe
, shishi_ap_key (ap
));
65 fprintf (stderr
, "Could not verify SAFE:\n%s\n%s\n",
66 shishi_strerror (res
), shishi_error (h
));
70 printf ("Verified SAFE successfully...\n");
72 res
= shishi_safe_user_data (h
, asn1safe
, &userdata
, &userdatalen
);
75 fprintf (stderr
, "Could not extract user data:\n%s\n%s\n",
76 shishi_strerror (res
), shishi_error (h
));
79 userdata
[userdatalen
] = '\0';
80 printf ("user data: `%s'\n", userdata
);
86 printf ("error reading stdin\n");
94 auth (Shishi
* h
, int verbose
, const char *cname
, const char *sname
)
103 printf ("Client: %s\n", cname
);
104 printf ("Server: %s\n", sname
);
106 /* Get key for the server. */
108 key
= shishi_hostkeys_for_server (h
, sname
);
111 printf ("could not find key: %s\n", shishi_error (h
));
116 shishi_key_print (h
, stderr
, key
);
118 /* Read Authentication request from client */
120 printf ("Waiting for client to authenticate itself...\n");
122 rc
= shishi_apreq_parse (h
, stdin
, &apreq
);
125 printf ("could not read AP-REQ: %s\n", shishi_strerror (rc
));
129 /* Create Authentication context */
131 rc
= shishi_ap (h
, &ap
);
134 printf ("Could not create AP: %s\n", shishi_strerror (rc
));
138 /* Store request in context */
140 shishi_ap_req_set (ap
, apreq
);
142 /* Process authentication request */
144 rc
= shishi_ap_req_process (ap
, key
);
147 printf ("Could not process AP-REQ: %s\n", shishi_strerror (rc
));
152 shishi_authenticator_print (h
, stderr
, shishi_ap_authenticator (ap
));
154 buflen
= sizeof (buf
);
155 rc
= shishi_authenticator_cnamerealm_get (h
, shishi_ap_authenticator (ap
),
158 printf ("Client name (from authenticator): %s\n", buf
);
160 buflen
= sizeof (buf
);
161 rc
= shishi_encticketpart_cnamerealm_get
162 (h
, shishi_tkt_encticketpart (shishi_ap_tkt (ap
)), buf
, &buflen
);
164 printf ("Client name (from encticketpart): %s\n", buf
);
166 buflen
= sizeof (buf
);
168 shishi_ticket_snamerealm_get (h
, shishi_tkt_ticket (shishi_ap_tkt (ap
)),
171 printf ("Server name (from ticket): %s\n", buf
);
173 /* User is authenticated. */
175 printf ("User authenticated.\n");
177 /* Authenticate ourself to client, if request */
179 if (shishi_apreq_mutual_required_p (h
, apreq
))
183 printf ("Mutual authentication required.\n");
185 rc
= shishi_ap_rep_asn1 (ap
, &aprep
);
188 printf ("Error creating AP-REP: %s\n", shishi_strerror (rc
));
193 shishi_encapreppart_print (h
, stderr
, shishi_ap_encapreppart (ap
));
195 shishi_aprep_print (h
, stdout
, aprep
);
197 /* We are authenticated to client */
204 main (int argc
, char *argv
[])
211 printf ("sample-server (shishi " SHISHI_VERSION
")\n");
213 if (!shishi_check_version (SHISHI_VERSION
))
215 printf ("shishi_check_version() failed:\n"
216 "Header file incompatible with shared library.\n");
220 rc
= shishi_init_server (&h
);
223 printf ("error initializing shishi: %s\n", shishi_strerror (rc
));
230 sname
= shishi_server_for_local_service (h
, SERVICE
);
232 ap
= auth (h
, 1, shishi_principal_default (h
), sname
);
235 rc
= doit (h
, ap
, 1);