1 /* as.c High level client AS functions
2 * Copyright (C) 2002, 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 * @handle: shishi handle as allocated by shishi_init().
36 * @as: holds pointer to newly allocate Shishi_as structure.
38 * Allocate a new AS exchange variable.
40 * Return value: Returns SHISHI_OK iff successful.
43 shishi_as (Shishi
* handle
, Shishi_as
** as
)
48 *as
= xmalloc (sizeof (**as
));
50 memset (las
, 0, sizeof (*las
));
54 las
->asreq
= shishi_asreq (handle
);
55 if (las
->asreq
== NULL
)
57 shishi_error_printf (handle
, "Could not create AS-REQ: %s\n",
58 shishi_error (handle
));
59 return SHISHI_ASN1_ERROR
;
62 /* XXX there are reasons for having padata in AS-REQ */
63 res
= shishi_kdcreq_clear_padata (las
->handle
, las
->asreq
);
67 las
->asrep
= shishi_asrep (handle
);
68 if (las
->asrep
== NULL
)
70 shishi_error_printf (handle
, "Could not create AS-REP: %s\n",
71 shishi_error (handle
));
72 return SHISHI_ASN1_ERROR
;
75 las
->krberror
= shishi_krberror (handle
);
76 if (las
->krberror
== NULL
)
78 shishi_error_printf (handle
, "Could not create KRB-ERROR: %s\n",
79 shishi_error (handle
));
80 return SHISHI_ASN1_ERROR
;
83 res
= shishi_tkt (handle
, &las
->tkt
);
87 res
= shishi_tkt_flags_set (las
->tkt
, SHISHI_TICKETFLAGS_INITIAL
);
96 * @as: structure that holds information about AS exchange
98 * Deallocate resources associated with AS exchange. This should be
99 * called by the application when it no longer need to utilize the AS
103 shishi_as_done (Shishi_as
* as
)
105 shishi_asn1_done (as
->handle
, as
->asreq
);
106 shishi_asn1_done (as
->handle
, as
->asrep
);
107 shishi_asn1_done (as
->handle
, as
->krberror
);
108 shishi_tkt_done (as
->tkt
);
112 /* TODO: add shishi_as_clientserver(h,p,a,client,server) and make the
113 shishi_as_cnamerealmsname function take real cname/sname pointer
118 * @as: structure that holds information about AS exchange
120 * Return value: Returns the generated AS-REQ packet from the AS
121 * exchange, or NULL if not yet set or an error occured.
124 shishi_as_req (Shishi_as
* as
)
130 * shishi_as_req_build:
131 * @as: structure that holds information about AS exchange
133 * Possibly remove unset fields (e.g., rtime).
135 * Return value: Returns SHISHI_OK iff successful.
138 shishi_as_req_build (Shishi_as
* as
)
142 res
= shishi_kdcreq_build (as
->handle
, as
->asreq
);
143 if (res
!= SHISHI_OK
)
151 * @as: structure that holds information about AS exchange
152 * @asreq: asreq to store in AS.
154 * Set the AS-REQ in the AS exchange.
157 shishi_as_req_set (Shishi_as
* as
, Shishi_asn1 asreq
)
160 shishi_asn1_done (as
->handle
, as
->asreq
);
166 * @as: structure that holds information about AS exchange
167 * @out: output array with newly allocated DER encoding of AS-REQ.
168 * @outlen: length of output array with DER encoding of AS-REQ.
170 * DER encode AS-REQ. @out is allocated by this function, and it is
171 * the responsibility of caller to deallocate it.
173 * Return value: Returns SHISHI_OK iff successful.
176 shishi_as_req_der (Shishi_as
* as
, char **out
, size_t * outlen
)
180 rc
= shishi_new_a2d (as
->handle
, as
->asreq
, out
, outlen
);
188 * shishi_as_req_der_set:
189 * @as: structure that holds information about AS exchange
190 * @der: input array with DER encoded AP-REQ.
191 * @derlen: length of input array with DER encoded AP-REQ.
193 * DER decode AS-REQ and set it AS exchange. If decoding fails, the
194 * AS-REQ in the AS exchange remains.
196 * Return value: Returns SHISHI_OK.
199 shishi_as_req_der_set (Shishi_as
* as
, char *der
, size_t derlen
)
203 asreq
= shishi_der2asn1_asreq (as
->handle
, der
, derlen
);
206 return SHISHI_ASN1_ERROR
;
215 * @as: structure that holds information about AS exchange
217 * Return value: Returns the received AS-REP packet from the AS
218 * exchange, or NULL if not yet set or an error occured.
221 shishi_as_rep (Shishi_as
* as
)
227 * shishi_as_rep_process:
228 * @as: structure that holds information about AS exchange
229 * @key: user's key, used to encrypt the encrypted part of the AS-REP.
230 * @password: user's password, used if key is NULL.
232 * Process new AS-REP and set ticket. The key is used to decrypt the
233 * AP-REP. If both key and password is NULL, the user is queried for
236 * Return value: Returns SHISHI_OK iff successful.
239 shishi_as_rep_process (Shishi_as
* as
, Shishi_key
* key
, const char *password
)
241 Shishi_asn1 ticket
, kdcreppart
;
246 if (VERBOSE (as
->handle
))
247 printf ("Processing AS-REQ and AS-REP...\n");
249 if (VERBOSEASN1 (as
->handle
))
250 shishi_kdcreq_print (as
->handle
, stdout
, as
->asreq
);
252 if (VERBOSEASN1 (as
->handle
))
253 shishi_kdcrep_print (as
->handle
, stdout
, as
->asrep
);
255 userlen
= sizeof (user
);
256 res
= shishi_asreq_cnamerealm_get (as
->handle
, as
->asreq
, user
, &userlen
);
257 if (res
!= SHISHI_OK
)
259 shishi_error_printf (as
->handle
, "Could not extract cname and "
260 "realm from AS-REQ: %s\n", shishi_strerror (res
),
261 shishi_error (as
->handle
));
264 user
[userlen
] = '\0';
266 if (key
== NULL
&& password
== NULL
)
270 res
= shishi_prompt_password (as
->handle
, &password
,
271 "Enter password for `%s': ", user
);
272 if (res
!= SHISHI_OK
)
274 shishi_error_printf (as
->handle
, "Reading password failed: %s\n",
275 shishi_strerror (res
));
279 res
= shishi_as_process (as
->handle
, as
->asreq
, as
->asrep
,
280 password
, &kdcreppart
);
283 else if (key
== NULL
)
284 res
= shishi_as_process (as
->handle
, as
->asreq
, as
->asrep
,
285 password
, &kdcreppart
);
287 res
= shishi_kdc_process (as
->handle
, as
->asreq
, as
->asrep
, key
,
288 SHISHI_KEYUSAGE_ENCASREPPART
, &kdcreppart
);
289 if (res
!= SHISHI_OK
)
292 if (VERBOSE (as
->handle
))
293 printf ("Got EncKDCRepPart...\n");
295 if (VERBOSEASN1 (as
->handle
))
296 shishi_enckdcreppart_print (as
->handle
, stdout
, kdcreppart
);
298 res
= shishi_kdcrep_get_ticket (as
->handle
, as
->asrep
, &ticket
);
299 if (res
!= SHISHI_OK
)
301 shishi_error_printf (as
->handle
,
302 "Could not extract ticket from AS-REP: %s",
303 shishi_error (as
->handle
));
307 if (VERBOSE (as
->handle
))
308 printf ("Got Ticket...\n");
310 if (VERBOSEASN1 (as
->handle
))
311 shishi_ticket_print (as
->handle
, stdout
, ticket
);
314 as
->tkt
= shishi_tkt2 (as
->handle
, ticket
, kdcreppart
, as
->asrep
);
320 * shishi_as_rep_build:
321 * @as: structure that holds information about AS exchange
322 * @key: user's key, used to encrypt the encrypted part of the AS-REP.
326 * Return value: Returns SHISHI_OK iff successful.
329 shishi_as_rep_build (Shishi_as
* as
, Shishi_key
* key
)
333 /* XXX there are reasons for having padata in AS-REP */
334 rc
= shishi_kdcrep_clear_padata (as
->handle
, as
->asrep
);
338 rc
= shishi_enckdcreppart_populate_encticketpart
339 (as
->handle
, shishi_tkt_enckdcreppart (as
->tkt
),
340 shishi_tkt_encticketpart (as
->tkt
));
344 rc
= shishi_kdc_copy_nonce (as
->handle
, as
->asreq
,
345 shishi_tkt_enckdcreppart (as
->tkt
));
349 rc
= shishi_kdcrep_add_enc_part (as
->handle
,
352 SHISHI_KEYUSAGE_ENCASREPPART
,
353 shishi_tkt_enckdcreppart (as
->tkt
));
357 rc
= shishi_kdcrep_set_ticket (as
->handle
, as
->asrep
,
358 shishi_tkt_ticket (as
->tkt
));
362 rc
= shishi_kdc_copy_crealm (as
->handle
, as
->asrep
,
363 shishi_tkt_encticketpart (as
->tkt
));
367 rc
= shishi_kdc_copy_cname (as
->handle
, as
->asrep
,
368 shishi_tkt_encticketpart (as
->tkt
));
377 * @as: structure that holds information about AS exchange
378 * @out: output array with newly allocated DER encoding of AS-REP.
379 * @outlen: length of output array with DER encoding of AS-REP.
381 * DER encode AS-REP. @out is allocated by this function, and it is
382 * the responsibility of caller to deallocate it.
384 * Return value: Returns SHISHI_OK iff successful.
387 shishi_as_rep_der (Shishi_as
* as
, char **out
, size_t * outlen
)
391 rc
= shishi_new_a2d (as
->handle
, as
->asrep
, out
, outlen
);
400 * @as: structure that holds information about AS exchange
401 * @asrep: asrep to store in AS.
403 * Set the AS-REP in the AS exchange.
406 shishi_as_rep_set (Shishi_as
* as
, Shishi_asn1 asrep
)
409 shishi_asn1_done (as
->handle
, as
->asrep
);
414 * shishi_as_rep_der_set:
415 * @as: structure that holds information about AS exchange
416 * @der: input array with DER encoded AP-REP.
417 * @derlen: length of input array with DER encoded AP-REP.
419 * DER decode AS-REP and set it AS exchange. If decoding fails, the
420 * AS-REP in the AS exchange remains.
422 * Return value: Returns SHISHI_OK.
425 shishi_as_rep_der_set (Shishi_as
* as
, char *der
, size_t derlen
)
429 asrep
= shishi_der2asn1_asrep (as
->handle
, der
, derlen
);
432 return SHISHI_ASN1_ERROR
;
440 * shishi_as_get_krberror:
441 * @as: structure that holds information about AS exchange
443 * Return value: Returns the received KRB-ERROR packet from the AS
444 * exchange, or NULL if not yet set or an error occured.
447 shishi_as_krberror (Shishi_as
* as
)
453 * shishi_as_krberror_der:
454 * @as: structure that holds information about AS exchange
455 * @out: output array with newly allocated DER encoding of KRB-ERROR.
456 * @outlen: length of output array with DER encoding of KRB-ERROR.
458 * DER encode KRB-ERROR. @out is allocated by this function, and it is
459 * the responsibility of caller to deallocate it.
461 * Return value: Returns SHISHI_OK iff successful.
464 shishi_as_krberror_der (Shishi_as
* as
, char **out
, size_t * outlen
)
468 rc
= shishi_new_a2d (as
->handle
, as
->krberror
, out
, outlen
);
476 * shishi_as_krberror_set:
477 * @as: structure that holds information about AS exchange
478 * @krberror: krberror to store in AS.
480 * Set the KRB-ERROR in the AS exchange.
483 shishi_as_krberror_set (Shishi_as
* as
, Shishi_asn1 krberror
)
486 shishi_asn1_done (as
->handle
, as
->krberror
);
487 as
->krberror
= krberror
;
492 * @as: structure that holds information about AS exchange
494 * Return value: Returns the newly aquired tkt from the AS
495 * exchange, or NULL if not yet set or an error occured.
498 shishi_as_tkt (Shishi_as
* as
)
505 * @as: structure that holds information about AS exchange
506 * @tkt: tkt to store in AS.
508 * Set the Tkt in the AS exchange.
511 shishi_as_tkt_set (Shishi_as
* as
, Shishi_tkt
* tkt
)
517 * shishi_as_sendrecv:
518 * @as: structure that holds information about AS exchange
520 * Send AS-REQ and receive AS-REP or KRB-ERROR. This is the initial
521 * authentication, usually used to acquire a Ticket Granting Ticket.
523 * Return value: Returns SHISHI_OK iff successful.
526 shishi_as_sendrecv (Shishi_as
* as
)
530 if (VERBOSE (as
->handle
))
531 printf ("Sending AS-REQ...\n");
533 if (VERBOSEASN1 (as
->handle
))
534 shishi_kdcreq_print (as
->handle
, stdout
, as
->asreq
);
536 res
= shishi_kdcreq_sendrecv (as
->handle
, as
->asreq
, &as
->asrep
);
537 if (res
== SHISHI_GOT_KRBERROR
)
539 as
->krberror
= as
->asrep
;
542 if (VERBOSE (as
->handle
))
543 printf ("Received KRB-ERROR...\n");
544 if (VERBOSEASN1 (as
->handle
))
545 shishi_krberror_print (as
->handle
, stdout
, as
->krberror
);
547 if (res
!= SHISHI_OK
)
550 if (VERBOSE (as
->handle
))
551 printf ("Received AS-REP...\n");
553 if (VERBOSEASN1 (as
->handle
))
554 shishi_kdcrep_print (as
->handle
, stdout
, as
->asrep
);