Add.
[shishi.git] / lib / tkt.c
blob21116b5a547428c35f1c1211e11370969d2a00e3
1 /* tkt.c --- Ticket handling.
2 * Copyright (C) 2002, 2003, 2004, 2006 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
22 #include "internal.h"
24 struct Shishi_tkt
26 Shishi *handle;
27 Shishi_asn1 ticket;
28 Shishi_asn1 kdcrep;
29 Shishi_asn1 enckdcreppart;
30 Shishi_asn1 encticketpart;
31 Shishi_key *key;
34 /**
35 * shishi_tkt:
36 * @handle: shishi handle as allocated by shishi_init().
37 * @tkt: output variable with newly allocated ticket.
39 * Create a new ticket handle.
41 * Return value: Returns SHISHI_OK iff successful.
42 **/
43 int
44 shishi_tkt (Shishi * handle, Shishi_tkt ** tkt)
46 Shishi_tkt *t;
47 int res;
49 t = xcalloc (1, sizeof (*t));
51 t->handle = handle;
53 t->ticket = shishi_ticket (handle);
54 if (t->ticket == NULL)
56 shishi_error_printf (handle, "Could not create Ticket: %s\n",
57 shishi_error (handle));
58 return SHISHI_ASN1_ERROR;
61 /* XXX what about tgs's? */
62 t->enckdcreppart = shishi_encasreppart (handle);
63 if (t->enckdcreppart == NULL)
65 shishi_error_printf (handle, "Could not create EncKDCRepPart: %s\n",
66 shishi_error (handle));
67 return SHISHI_ASN1_ERROR;
70 t->encticketpart = shishi_encticketpart (handle);
71 if (t->encticketpart == NULL)
73 shishi_error_printf (handle, "Could not create EncTicketPart: %s\n",
74 shishi_error (handle));
75 return SHISHI_ASN1_ERROR;
78 res = shishi_encticketpart_transited_set (handle,
79 t->encticketpart,
80 SHISHI_TR_DOMAIN_X500_COMPRESS,
81 "", 0);
82 if (res != SHISHI_OK)
83 return res;
85 res = shishi_encticketpart_authtime_set
86 (handle, t->encticketpart, shishi_generalize_time (handle, time (NULL)));
87 if (res != SHISHI_OK)
88 return res;
90 res = shishi_encticketpart_endtime_set
91 (handle, t->encticketpart,
92 shishi_generalize_time (handle, time (NULL) + 1000));
93 if (res != SHISHI_OK)
94 return res;
96 t->kdcrep = shishi_asrep (handle);
97 if (t->kdcrep == NULL)
99 shishi_error_printf (handle, "Could not create AS-REP: %s\n",
100 shishi_error (handle));
101 return SHISHI_ASN1_ERROR;
104 /* XXX We don't allocate t->key here, because shishi_tkt_key()
105 relies on it being NULL. Possibly, we should allocate it here
106 instead, and simplify shishi_tkt_key(). */
108 *tkt = t;
110 return SHISHI_OK;
114 * shishi_tkt2:
115 * @handle: shishi handle as allocated by shishi_init().
116 * @ticket: input variable with ticket.
117 * @enckdcreppart: input variable with auxilliary ticket information.
118 * @kdcrep: input variable with KDC-REP ticket information.
120 * Create a new ticket handle.
122 * Return value: Returns new ticket handle, or %NULL on error.
124 Shishi_tkt *
125 shishi_tkt2 (Shishi * handle,
126 Shishi_asn1 ticket, Shishi_asn1 enckdcreppart,
127 Shishi_asn1 kdcrep)
129 Shishi_tkt *tkt;
131 tkt = xcalloc (1, sizeof (*tkt));
133 tkt->handle = handle;
134 tkt->ticket = ticket;
135 tkt->enckdcreppart = enckdcreppart;
136 tkt->kdcrep = kdcrep;
138 return tkt;
142 * shishi_tkt_done:
143 * @tkt: input variable with ticket info.
145 * Deallocate resources associated with ticket. The ticket must not
146 * be used again after this call.
148 void
149 shishi_tkt_done (Shishi_tkt * tkt)
151 if (tkt->key)
152 shishi_key_done (tkt->key);
153 free (tkt);
158 shishi_tkt_build (Shishi_tkt * tkt, Shishi_key * key)
160 int res;
162 res = shishi_ticket_add_enc_part (tkt->handle, tkt->ticket,
163 key, tkt->encticketpart);
164 if (res != SHISHI_OK)
165 return res;
167 return SHISHI_OK;
171 * shishi_tkt_ticket:
172 * @tkt: input variable with ticket info.
174 * Get ASN.1 Ticket structure from ticket.
176 * Return value: Returns actual ticket.
178 Shishi_asn1
179 shishi_tkt_ticket (Shishi_tkt * tkt)
181 return tkt->ticket;
185 * shishi_tkt_ticket_set:
186 * @tkt: input variable with ticket info.
187 * @ticket: ASN.1 Ticket to store in ticket.
189 * Set the ASN.1 Ticket in the Ticket.
191 void
192 shishi_tkt_ticket_set (Shishi_tkt * tkt, Shishi_asn1 ticket)
194 if (tkt->ticket)
195 shishi_asn1_done (tkt->handle, tkt->ticket);
196 tkt->ticket = ticket;
200 * shishi_tkt_enckdcreppart:
201 * @tkt: input variable with ticket info.
203 * Get ASN.1 EncKDCRepPart structure from ticket.
205 * Return value: Returns auxilliary ticket information.
207 Shishi_asn1
208 shishi_tkt_enckdcreppart (Shishi_tkt * tkt)
210 return tkt->enckdcreppart;
214 * shishi_tkt_enckdcreppart_set:
215 * @tkt: structure that holds information about Ticket exchange
216 * @enckdcreppart: EncKDCRepPart to store in Ticket.
218 * Set the EncKDCRepPart in the Ticket.
220 void
221 shishi_tkt_enckdcreppart_set (Shishi_tkt * tkt, Shishi_asn1 enckdcreppart)
223 if (tkt->enckdcreppart)
224 shishi_asn1_done (tkt->handle, tkt->enckdcreppart);
225 tkt->enckdcreppart = enckdcreppart;
229 * shishi_tkt_kdcrep:
230 * @tkt: input variable with ticket info.
232 * Get ASN.1 KDCRep structure from ticket.
234 * Return value: Returns KDC-REP information.
236 Shishi_asn1
237 shishi_tkt_kdcrep (Shishi_tkt * tkt)
239 return tkt->kdcrep;
243 * shishi_tkt_encticketpart:
244 * @tkt: input variable with ticket info.
246 * Get ASN.1 EncTicketPart structure from ticket.
248 * Return value: Returns EncTicketPart information.
250 Shishi_asn1
251 shishi_tkt_encticketpart (Shishi_tkt * tkt)
253 return tkt->encticketpart;
257 * shishi_tkt_encticketpart_set:
258 * @tkt: input variable with ticket info.
259 * @encticketpart: encticketpart to store in ticket.
261 * Set the EncTicketPart in the Ticket.
263 void
264 shishi_tkt_encticketpart_set (Shishi_tkt * tkt, Shishi_asn1 encticketpart)
266 if (tkt->encticketpart)
267 shishi_asn1_done (tkt->handle, tkt->encticketpart);
268 tkt->encticketpart = encticketpart;
272 * shishi_tkt_key:
273 * @tkt: input variable with ticket info.
275 * Get key used in ticket, by looking first in EncKDCRepPart and then
276 * in EncTicketPart. If key is already populated, it is not extracted
277 * again.
279 * Return value: Returns key extracted from EncKDCRepPart or
280 * EncTicketPart.
282 Shishi_key *
283 shishi_tkt_key (Shishi_tkt * tkt)
285 int rc;
287 /* XXX We probably shouldn't extract the keys here. Where is this
288 extraction actually needed? */
289 if (!tkt->key && tkt->enckdcreppart)
291 rc = shishi_enckdcreppart_get_key (tkt->handle,
292 tkt->enckdcreppart, &tkt->key);
293 if (rc != SHISHI_OK)
294 return NULL;
296 else if (!tkt->key && tkt->encticketpart)
298 rc = shishi_encticketpart_get_key (tkt->handle,
299 tkt->encticketpart, &tkt->key);
300 if (rc != SHISHI_OK)
301 return NULL;
304 return tkt->key;
308 * shishi_tkt_key_set:
309 * @tkt: input variable with ticket info.
310 * @key: key to store in ticket.
312 * Set the key in the EncTicketPart.
314 * Return value: Returns SHISHI_OK iff successful.
317 shishi_tkt_key_set (Shishi_tkt * tkt, Shishi_key * key)
319 int res;
321 res = shishi_encticketpart_key_set (tkt->handle, tkt->encticketpart, key);
322 if (res != SHISHI_OK)
323 return res;
325 res = shishi_enckdcreppart_key_set (tkt->handle, tkt->enckdcreppart, key);
326 if (res != SHISHI_OK)
327 return res;
329 if (!tkt->key)
331 res = shishi_key (tkt->handle, &tkt->key);
332 if (res != SHISHI_OK)
333 return res;
336 shishi_key_copy (tkt->key, key);
338 return SHISHI_OK;
342 shishi_tkt_clientrealm_set (Shishi_tkt * tkt,
343 const char *realm, const char *client)
345 int res;
347 res = shishi_encticketpart_crealm_set (tkt->handle,
348 tkt->encticketpart, realm);
349 if (res != SHISHI_OK)
350 return res;
352 res = shishi_encticketpart_cname_set (tkt->handle,
353 tkt->encticketpart,
354 SHISHI_NT_UNKNOWN, client);
355 if (res != SHISHI_OK)
356 return res;
358 return SHISHI_OK;
362 shishi_tkt_serverrealm_set (Shishi_tkt * tkt,
363 const char *realm, const char *server)
365 int res;
367 res = shishi_ticket_srealmserver_set (tkt->handle, tkt->ticket,
368 realm, server);
369 if (res != SHISHI_OK)
370 return res;
372 res = shishi_enckdcreppart_srealmserver_set
373 (tkt->handle, tkt->enckdcreppart, realm, server);
374 if (res != SHISHI_OK)
375 return res;
377 return SHISHI_OK;
381 * shishi_tkt_client:
382 * @tkt: input variable with ticket info.
383 * @client: pointer to newly allocated zero terminated string containing
384 * principal name. May be %NULL (to only populate @clientlen).
385 * @clientlen: pointer to length of @client on output, excluding terminating
386 * zero. May be %NULL (to only populate @client).
388 * Represent client principal name in Ticket KDC-REP as
389 * zero-terminated string. The string is allocate by this function,
390 * and it is the responsibility of the caller to deallocate it. Note
391 * that the output length @clientlen does not include the terminating
392 * zero.
394 * Return value: Returns SHISHI_OK iff successful.
397 shishi_tkt_client (Shishi_tkt * tkt, char **client, size_t * clientlen)
399 return shishi_principal_name (tkt->handle, tkt->kdcrep,
400 "cname", client, clientlen);
404 * shishi_tkt_client_p:
405 * @tkt: input variable with ticket info.
406 * @client: client name of ticket.
408 * Determine if ticket is for specified client.
410 * Return value: Returns non-0 iff ticket is for specified client.
413 shishi_tkt_client_p (Shishi_tkt * tkt, const char *client)
415 char *buf;
416 size_t buflen;
417 int res;
419 res = shishi_tkt_client (tkt, &buf, &buflen);
420 if (res != SHISHI_OK)
421 return 0;
423 res = strcmp (client, buf) == 0;
425 free (buf);
427 return res;
431 * shishi_tkt_clientrealm:
432 * @tkt: input variable with ticket info.
433 * @client: pointer to newly allocated zero terminated string containing
434 * principal name and realm. May be %NULL (to only populate @clientlen).
435 * @clientlen: pointer to length of @client on output, excluding terminating
436 * zero. May be %NULL (to only populate @client).
438 * Convert cname and realm fields from AS-REQ to printable principal
439 * name format. The string is allocate by this function, and it is
440 * the responsibility of the caller to deallocate it. Note that the
441 * output length @clientlen does not include the terminating zero.
443 * Return value: Returns SHISHI_OK iff successful.
446 shishi_tkt_clientrealm (Shishi_tkt * tkt, char **client, size_t * clientlen)
448 return shishi_principal_name_realm (tkt->handle,
449 tkt->kdcrep, "cname",
450 tkt->kdcrep, "crealm",
451 client, clientlen);
455 * shishi_tkt_clientrealm_p:
456 * @tkt: input variable with ticket info.
457 * @client: principal name (client name and realm) of ticket.
459 * Determine if ticket is for specified client principal.
461 * Return value: Returns non-0 iff ticket is for specified client principal.
464 shishi_tkt_clientrealm_p (Shishi_tkt * tkt, const char *client)
466 char *buf;
467 size_t buflen;
468 int res;
470 res = shishi_tkt_clientrealm (tkt, &buf, &buflen);
471 if (res != SHISHI_OK)
472 return 0;
474 res = strcmp (client, buf) == 0;
476 free (buf);
478 return res;
482 * shishi_tkt_realm:
483 * @tkt: input variable with ticket info.
484 * @realm: pointer to newly allocated character array with realm name.
485 * @realmlen: length of newly allocated character array with realm name.
487 * Extract realm of server in ticket.
489 * Return value: Returns SHISHI_OK iff successful.
492 shishi_tkt_realm (Shishi_tkt * tkt, char **realm, size_t * realmlen)
494 return shishi_ticket_realm_get (tkt->handle, tkt->ticket, realm, realmlen);
498 * shishi_tkt_server:
499 * @tkt: input variable with ticket info.
500 * @server: pointer to newly allocated zero terminated string containing
501 * principal name. May be %NULL (to only populate @serverlen).
502 * @serverlen: pointer to length of @server on output, excluding terminating
503 * zero. May be %NULL (to only populate @server).
505 * Represent server principal name in Ticket as zero-terminated
506 * string. The string is allocate by this function, and it is the
507 * responsibility of the caller to deallocate it. Note that the
508 * output length @serverlen does not include the terminating zero.
510 * Return value: Returns SHISHI_OK iff successful.
513 shishi_tkt_server (Shishi_tkt * tkt, char **server, size_t * serverlen)
515 return shishi_ticket_server (tkt->handle, tkt->ticket, server, serverlen);
519 * shishi_tkt_server_p:
520 * @tkt: input variable with ticket info.
521 * @server: server name of ticket.
523 * Determine if ticket is for specified server.
525 * Return value: Returns non-0 iff ticket is for specified server.
528 shishi_tkt_server_p (Shishi_tkt * tkt, const char *server)
530 char *buf;
531 int res;
533 res = shishi_tkt_server (tkt, &buf, NULL);
534 if (res != SHISHI_OK)
535 return 0;
537 res = strcmp (server, buf) == 0;
539 free (buf);
541 return res;
545 * shishi_tkt_flags:
546 * @tkt: input variable with ticket info.
547 * @flags: pointer to output integer with flags.
549 * Extract flags in ticket (i.e., EncKDCRepPart).
551 * Return value: Returns SHISHI_OK iff successful.
554 shishi_tkt_flags (Shishi_tkt * tkt, uint32_t * flags)
556 return shishi_asn1_read_bitstring (tkt->handle, tkt->enckdcreppart,
557 "flags", flags);
561 * shishi_tkt_flags_set:
562 * @tkt: input variable with ticket info.
563 * @flags: integer with flags to store in ticket.
565 * Set flags in ticket, i.e., both EncTicketPart and EncKDCRepPart.
566 * Note that this reset any already existing flags.
568 * Return value: Returns SHISHI_OK iff successful.
571 shishi_tkt_flags_set (Shishi_tkt * tkt, uint32_t flags)
573 int res;
575 res = shishi_encticketpart_flags_set (tkt->handle, tkt->encticketpart,
576 flags);
577 if (res != SHISHI_OK)
578 return res;
580 res = shishi_enckdcreppart_flags_set (tkt->handle, tkt->enckdcreppart,
581 flags);
582 if (res != SHISHI_OK)
583 return res;
585 return SHISHI_OK;
589 * shishi_tkt_flags_add:
590 * @tkt: input variable with ticket info.
591 * @flag: integer with flags to store in ticket.
593 * Add ticket flags to Ticket and EncKDCRepPart. This preserves all
594 * existing options.
596 * Return value: Returns SHISHI_OK iff successful.
599 shishi_tkt_flags_add (Shishi_tkt * tkt, uint32_t flag)
601 uint32_t flags;
602 int res;
604 res = shishi_tkt_flags (tkt, &flags);
605 if (res != SHISHI_OK)
606 return res;
608 flags |= flag;
610 res = shishi_tkt_flags_set (tkt, flags);
611 if (res != SHISHI_OK)
612 return res;
614 return SHISHI_OK;
618 * shishi_tkt_forwardable_p:
619 * @tkt: input variable with ticket info.
621 * Determine if ticket is forwardable.
623 * The FORWARDABLE flag in a ticket is normally only interpreted by
624 * the ticket-granting service. It can be ignored by application
625 * servers. The FORWARDABLE flag has an interpretation similar to
626 * that of the PROXIABLE flag, except ticket-granting tickets may also
627 * be issued with different network addresses. This flag is reset by
628 * default, but users MAY request that it be set by setting the
629 * FORWARDABLE option in the AS request when they request their
630 * initial ticket-granting ticket.
632 * Return value: Returns non-0 iff forwardable flag is set in ticket.
635 shishi_tkt_forwardable_p (Shishi_tkt * tkt)
637 uint32_t flags = 0;
639 shishi_tkt_flags (tkt, &flags);
641 return flags & SHISHI_TICKETFLAGS_FORWARDABLE;
645 * shishi_tkt_forwarded_p:
646 * @tkt: input variable with ticket info.
648 * Determine if ticket is forwarded.
650 * The FORWARDED flag is set by the TGS when a client presents a
651 * ticket with the FORWARDABLE flag set and requests a forwarded
652 * ticket by specifying the FORWARDED KDC option and supplying a set
653 * of addresses for the new ticket. It is also set in all tickets
654 * issued based on tickets with the FORWARDED flag set. Application
655 * servers may choose to process FORWARDED tickets differently than
656 * non-FORWARDED tickets.
658 * Return value: Returns non-0 iff forwarded flag is set in ticket.
661 shishi_tkt_forwarded_p (Shishi_tkt * tkt)
663 uint32_t flags = 0;
665 shishi_tkt_flags (tkt, &flags);
667 return flags & SHISHI_TICKETFLAGS_FORWARDED;
671 * shishi_tkt_proxiable_p:
672 * @tkt: input variable with ticket info.
674 * Determine if ticket is proxiable.
676 * The PROXIABLE flag in a ticket is normally only interpreted by the
677 * ticket-granting service. It can be ignored by application servers.
678 * When set, this flag tells the ticket-granting server that it is OK
679 * to issue a new ticket (but not a ticket-granting ticket) with a
680 * different network address based on this ticket. This flag is set if
681 * requested by the client on initial authentication. By default, the
682 * client will request that it be set when requesting a
683 * ticket-granting ticket, and reset when requesting any other ticket.
685 * Return value: Returns non-0 iff proxiable flag is set in ticket.
688 shishi_tkt_proxiable_p (Shishi_tkt * tkt)
690 uint32_t flags = 0;
692 shishi_tkt_flags (tkt, &flags);
694 return flags & SHISHI_TICKETFLAGS_PROXIABLE;
698 * shishi_tkt_proxy_p:
699 * @tkt: input variable with ticket info.
701 * Determine if ticket is proxy ticket.
703 * The PROXY flag is set in a ticket by the TGS when it issues a proxy
704 * ticket. Application servers MAY check this flag and at their
705 * option they MAY require additional authentication from the agent
706 * presenting the proxy in order to provide an audit trail.
708 * Return value: Returns non-0 iff proxy flag is set in ticket.
711 shishi_tkt_proxy_p (Shishi_tkt * tkt)
713 uint32_t flags = 0;
715 shishi_tkt_flags (tkt, &flags);
717 return flags & SHISHI_TICKETFLAGS_PROXY;
721 * shishi_tkt_may_postdate_p:
722 * @tkt: input variable with ticket info.
724 * Determine if ticket may be used to grant postdated tickets.
726 * The MAY-POSTDATE flag in a ticket is normally only interpreted by
727 * the ticket-granting service. It can be ignored by application
728 * servers. This flag MUST be set in a ticket-granting ticket in
729 * order to issue a postdated ticket based on the presented ticket. It
730 * is reset by default; it MAY be requested by a client by setting the
731 * ALLOW- POSTDATE option in the KRB_AS_REQ message. This flag does
732 * not allow a client to obtain a postdated ticket-granting ticket;
733 * postdated ticket-granting tickets can only by obtained by
734 * requesting the postdating in the KRB_AS_REQ message. The life
735 * (endtime-starttime) of a postdated ticket will be the remaining
736 * life of the ticket-granting ticket at the time of the request,
737 * unless the RENEWABLE option is also set, in which case it can be
738 * the full life (endtime-starttime) of the ticket-granting
739 * ticket. The KDC MAY limit how far in the future a ticket may be
740 * postdated.
742 * Return value: Returns non-0 iff may-postdate flag is set in ticket.
745 shishi_tkt_may_postdate_p (Shishi_tkt * tkt)
747 uint32_t flags = 0;
749 shishi_tkt_flags (tkt, &flags);
751 return flags & SHISHI_TICKETFLAGS_MAY_POSTDATE;
755 * shishi_tkt_postdated_p:
756 * @tkt: input variable with ticket info.
758 * Determine if ticket is postdated.
760 * The POSTDATED flag indicates that a ticket has been postdated. The
761 * application server can check the authtime field in the ticket to
762 * see when the original authentication occurred. Some services MAY
763 * choose to reject postdated tickets, or they may only accept them
764 * within a certain period after the original authentication. When the
765 * KDC issues a POSTDATED ticket, it will also be marked as INVALID,
766 * so that the application client MUST present the ticket to the KDC
767 * to be validated before use.
769 * Return value: Returns non-0 iff postdated flag is set in ticket.
772 shishi_tkt_postdated_p (Shishi_tkt * tkt)
774 uint32_t flags = 0;
776 shishi_tkt_flags (tkt, &flags);
778 return flags & SHISHI_TICKETFLAGS_POSTDATED;
782 * shishi_tkt_invalid_p:
783 * @tkt: input variable with ticket info.
785 * Determine if ticket is invalid.
787 * The INVALID flag indicates that a ticket is invalid. Application
788 * servers MUST reject tickets which have this flag set. A postdated
789 * ticket will be issued in this form. Invalid tickets MUST be
790 * validated by the KDC before use, by presenting them to the KDC in a
791 * TGS request with the VALIDATE option specified. The KDC will only
792 * validate tickets after their starttime has passed. The validation
793 * is required so that postdated tickets which have been stolen before
794 * their starttime can be rendered permanently invalid (through a
795 * hot-list mechanism).
797 * Return value: Returns non-0 iff invalid flag is set in ticket.
800 shishi_tkt_invalid_p (Shishi_tkt * tkt)
802 uint32_t flags = 0;
804 shishi_tkt_flags (tkt, &flags);
806 return flags & SHISHI_TICKETFLAGS_INVALID;
810 * shishi_tkt_renewable_p:
811 * @tkt: input variable with ticket info.
813 * Determine if ticket is renewable.
815 * The RENEWABLE flag in a ticket is normally only interpreted by the
816 * ticket-granting service (discussed below in section 3.3). It can
817 * usually be ignored by application servers. However, some
818 * particularly careful application servers MAY disallow renewable
819 * tickets.
821 * Return value: Returns non-0 iff renewable flag is set in ticket.
824 shishi_tkt_renewable_p (Shishi_tkt * tkt)
826 uint32_t flags = 0;
828 shishi_tkt_flags (tkt, &flags);
830 return flags & SHISHI_TICKETFLAGS_RENEWABLE;
834 * shishi_tkt_initial_p:
835 * @tkt: input variable with ticket info.
837 * Determine if ticket was issued using AS exchange.
839 * The INITIAL flag indicates that a ticket was issued using the AS
840 * protocol, rather than issued based on a ticket-granting ticket.
841 * Application servers that want to require the demonstrated knowledge
842 * of a client's secret key (e.g. a password-changing program) can
843 * insist that this flag be set in any tickets they accept, and thus
844 * be assured that the client's key was recently presented to the
845 * application client.
847 * Return value: Returns non-0 iff initial flag is set in ticket.
850 shishi_tkt_initial_p (Shishi_tkt * tkt)
852 uint32_t flags = 0;
854 shishi_tkt_flags (tkt, &flags);
856 return flags & SHISHI_TICKETFLAGS_INITIAL;
860 * shishi_tkt_pre_authent_p:
861 * @tkt: input variable with ticket info.
863 * Determine if ticket was pre-authenticated.
865 * The PRE-AUTHENT and HW-AUTHENT flags provide additional information
866 * about the initial authentication, regardless of whether the current
867 * ticket was issued directly (in which case INITIAL will also be set)
868 * or issued on the basis of a ticket-granting ticket (in which case
869 * the INITIAL flag is clear, but the PRE-AUTHENT and HW-AUTHENT flags
870 * are carried forward from the ticket-granting ticket).
872 * Return value: Returns non-0 iff pre-authent flag is set in ticket.
875 shishi_tkt_pre_authent_p (Shishi_tkt * tkt)
877 uint32_t flags = 0;
879 shishi_tkt_flags (tkt, &flags);
881 return flags & SHISHI_TICKETFLAGS_PRE_AUTHENT;
885 * shishi_tkt_hw_authent_p:
886 * @tkt: input variable with ticket info.
888 * Determine if ticket is authenticated using a hardware token.
890 * The PRE-AUTHENT and HW-AUTHENT flags provide additional information
891 * about the initial authentication, regardless of whether the current
892 * ticket was issued directly (in which case INITIAL will also be set)
893 * or issued on the basis of a ticket-granting ticket (in which case
894 * the INITIAL flag is clear, but the PRE-AUTHENT and HW-AUTHENT flags
895 * are carried forward from the ticket-granting ticket).
897 * Return value: Returns non-0 iff hw-authent flag is set in ticket.
900 shishi_tkt_hw_authent_p (Shishi_tkt * tkt)
902 uint32_t flags = 0;
904 shishi_tkt_flags (tkt, &flags);
906 return flags & SHISHI_TICKETFLAGS_HW_AUTHENT;
910 * shishi_tkt_transited_policy_checked_p:
911 * @tkt: input variable with ticket info.
913 * Determine if ticket has been policy checked for transit.
915 * The application server is ultimately responsible for accepting or
916 * rejecting authentication and SHOULD check that only suitably
917 * trusted KDCs are relied upon to authenticate a principal. The
918 * transited field in the ticket identifies which realms (and thus
919 * which KDCs) were involved in the authentication process and an
920 * application server would normally check this field. If any of these
921 * are untrusted to authenticate the indicated client principal
922 * (probably determined by a realm-based policy), the authentication
923 * attempt MUST be rejected. The presence of trusted KDCs in this list
924 * does not provide any guarantee; an untrusted KDC may have
925 * fabricated the list.
927 * While the end server ultimately decides whether authentication is
928 * valid, the KDC for the end server's realm MAY apply a realm
929 * specific policy for validating the transited field and accepting
930 * credentials for cross-realm authentication. When the KDC applies
931 * such checks and accepts such cross-realm authentication it will set
932 * the TRANSITED-POLICY-CHECKED flag in the service tickets it issues
933 * based on the cross-realm TGT. A client MAY request that the KDCs
934 * not check the transited field by setting the
935 * DISABLE-TRANSITED-CHECK flag. KDCs are encouraged but not required
936 * to honor this flag.
938 * Application servers MUST either do the transited-realm checks
939 * themselves, or reject cross-realm tickets without TRANSITED-POLICY-
940 * CHECKED set.
942 * Return value: Returns non-0 iff transited-policy-checked flag is
943 * set in ticket.
946 shishi_tkt_transited_policy_checked_p (Shishi_tkt * tkt)
948 uint32_t flags = 0;
950 shishi_tkt_flags (tkt, &flags);
952 return flags & SHISHI_TICKETFLAGS_TRANSITED_POLICY_CHECKED;
956 * shishi_tkt_ok_as_delegate_p:
957 * @tkt: input variable with ticket info.
959 * Determine if ticket is ok as delegated ticket.
961 * The copy of the ticket flags in the encrypted part of the KDC reply
962 * may have the OK-AS-DELEGATE flag set to indicates to the client
963 * that the server specified in the ticket has been determined by
964 * policy of the realm to be a suitable recipient of delegation. A
965 * client can use the presence of this flag to help it make a decision
966 * whether to delegate credentials (either grant a proxy or a
967 * forwarded ticket- granting ticket) to this server. It is
968 * acceptable to ignore the value of this flag. When setting this
969 * flag, an administrator should consider the security and placement
970 * of the server on which the service will run, as well as whether the
971 * service requires the use of delegated credentials.
973 * Return value: Returns non-0 iff ok-as-delegate flag is set in ticket.
976 shishi_tkt_ok_as_delegate_p (Shishi_tkt * tkt)
978 uint32_t flags = 0;
980 shishi_tkt_flags (tkt, &flags);
982 return flags & SHISHI_TICKETFLAGS_OK_AS_DELEGATE;
986 * shishi_tkt_keytype:
987 * @tkt: input variable with ticket info.
988 * @etype: pointer to encryption type that is set, see Shishi_etype.
990 * Extract encryption type of key in ticket (really EncKDCRepPart).
992 * Return value: Returns SHISHI_OK iff successful.
995 shishi_tkt_keytype (Shishi_tkt * tkt, int32_t * etype)
997 return shishi_asn1_read_int32 (tkt->handle, tkt->enckdcreppart,
998 "key.keytype", etype);
1002 * shishi_tkt_keytype_fast:
1003 * @tkt: input variable with ticket info.
1005 * Extract encryption type of key in ticket (really EncKDCRepPart).
1007 * Return value: Returns encryption type of session key in ticket
1008 * (really EncKDCRepPart), or -1 on error.
1010 int32_t
1011 shishi_tkt_keytype_fast (Shishi_tkt * tkt)
1013 int32_t etype = -1;
1014 int res;
1016 res = shishi_asn1_read_int32 (tkt->handle, tkt->enckdcreppart,
1017 "key.keytype", &etype);
1018 if (res != SHISHI_OK)
1019 return -1;
1021 return etype;
1025 * shishi_tkt_keytype_p:
1026 * @tkt: input variable with ticket info.
1027 * @etype: encryption type, see Shishi_etype.
1029 * Determine if key in ticket (really EncKDCRepPart) is of specified
1030 * key type (really encryption type).
1032 * Return value: Returns non-0 iff key in ticket is of specified
1033 * encryption type.
1036 shishi_tkt_keytype_p (Shishi_tkt * tkt, int32_t etype)
1038 int32_t tktetype;
1039 int rc;
1041 rc = shishi_asn1_read_int32 (tkt->handle, tkt->enckdcreppart,
1042 "key.keytype", &tktetype);
1043 if (rc != SHISHI_OK)
1044 return 0;
1046 return etype == tktetype;
1050 shishi_tkt_lastreq (Shishi_tkt * tkt,
1051 char **lrtime, size_t * lrtimelen, int32_t lrtype)
1053 char *format;
1054 int32_t tmplrtype;
1055 size_t i, n;
1056 int res;
1058 res = shishi_asn1_number_of_elements (tkt->handle, tkt->enckdcreppart,
1059 "last-req", &n);
1060 if (res != SHISHI_OK)
1061 return res;
1063 for (i = 1; i <= n; i++)
1065 asprintf (&format, "last-req.?%d.lr-type", i);
1066 res = shishi_asn1_read_int32 (tkt->handle, tkt->enckdcreppart,
1067 format, &tmplrtype);
1068 free (format);
1069 if (res != SHISHI_OK)
1070 return res;
1072 if (lrtype == tmplrtype)
1074 asprintf (&format, "last-req.?%d.lr-value", i);
1075 res = shishi_asn1_read (tkt->handle, tkt->enckdcreppart,
1076 format, lrtime, lrtimelen);
1077 free (format);
1078 if (res != SHISHI_OK)
1079 return res;
1081 return SHISHI_OK;
1085 return !SHISHI_OK;
1089 * shishi_tkt_lastreqc:
1090 * @tkt: input variable with ticket info.
1091 * @lrtype: lastreq type to extract, see Shishi_lrtype. E.g.,
1092 * SHISHI_LRTYPE_LAST_REQUEST.
1094 * Extract C time corresponding to given lastreq type field in the
1095 * ticket.
1097 * Return value: Returns C time interpretation of the specified
1098 * lastreq field, or (time_t) -1.
1100 time_t
1101 shishi_tkt_lastreqc (Shishi_tkt * tkt, Shishi_lrtype lrtype)
1103 char *lrtime;
1104 size_t lrtimelen;
1105 time_t t = (time_t) - 1;
1106 int res;
1108 res = shishi_tkt_lastreq (tkt, &lrtime, &lrtimelen, lrtype);
1109 if (res != SHISHI_OK)
1110 return t;
1112 if (lrtimelen == SHISHI_GENERALIZEDTIME_LENGTH)
1113 t = shishi_generalize_ctime (tkt->handle, lrtime);
1115 free (lrtime);
1117 return t;
1121 shishi_tkt_authtime (Shishi_tkt * tkt, char **authtime, size_t * authtimelen)
1123 return shishi_asn1_read (tkt->handle, tkt->enckdcreppart, "authtime",
1124 authtime, authtimelen);
1128 * shishi_tkt_authctime:
1129 * @tkt: input variable with ticket info.
1131 * Extract C time corresponding to the authtime field. The field
1132 * holds the time when the original authentication took place that
1133 * later resulted in this ticket.
1135 * Return value: Returns C time interpretation of the endtime in ticket.
1137 time_t
1138 shishi_tkt_authctime (Shishi_tkt * tkt)
1140 char *authtime;
1141 size_t authtimelen;
1142 time_t t = (time_t) - 1;
1143 int res;
1145 res = shishi_tkt_authtime (tkt, &authtime, &authtimelen);
1146 if (res != SHISHI_OK)
1147 return t;
1149 if (authtimelen == SHISHI_GENERALIZEDTIME_LENGTH + 1) /* XXX why +1 ? */
1150 t = shishi_generalize_ctime (tkt->handle, authtime);
1152 free (authtime);
1154 return t;
1158 shishi_tkt_starttime (Shishi_tkt * tkt,
1159 char **starttime, size_t * starttimelen)
1161 return shishi_asn1_read_optional (tkt->handle, tkt->enckdcreppart,
1162 "starttime", starttime, starttimelen);
1166 * shishi_tkt_startctime:
1167 * @tkt: input variable with ticket info.
1169 * Extract C time corresponding to the starttime field. The field
1170 * holds the time where the ticket start to be valid (typically in the
1171 * past).
1173 * Return value: Returns C time interpretation of the endtime in ticket.
1175 time_t
1176 shishi_tkt_startctime (Shishi_tkt * tkt)
1178 char *starttime;
1179 size_t starttimelen;
1180 time_t t = (time_t) - 1;
1181 int res;
1183 res = shishi_tkt_starttime (tkt, &starttime, &starttimelen);
1184 if (res != SHISHI_OK || starttimelen == 0)
1185 return t;
1187 if (starttimelen == SHISHI_GENERALIZEDTIME_LENGTH + 1) /* XXX why +1 ? */
1188 t = shishi_generalize_ctime (tkt->handle, starttime);
1190 free (starttime);
1192 return t;
1196 shishi_tkt_endtime (Shishi_tkt * tkt, char **endtime, size_t * endtimelen)
1198 return shishi_asn1_read (tkt->handle, tkt->enckdcreppart, "endtime",
1199 endtime, endtimelen);
1203 * shishi_tkt_endctime:
1204 * @tkt: input variable with ticket info.
1206 * Extract C time corresponding to the endtime field. The field holds
1207 * the time where the ticket stop being valid.
1209 * Return value: Returns C time interpretation of the endtime in ticket.
1211 time_t
1212 shishi_tkt_endctime (Shishi_tkt * tkt)
1214 char *endtime;
1215 size_t endtimelen;
1216 time_t t = (time_t) - 1;
1217 int res;
1219 res = shishi_tkt_endtime (tkt, &endtime, &endtimelen);
1220 if (res != SHISHI_OK)
1221 return t;
1223 if (endtimelen == SHISHI_GENERALIZEDTIME_LENGTH + 1) /* XXX why +1 ? */
1224 t = shishi_generalize_ctime (tkt->handle, endtime);
1226 free (endtime);
1228 return t;
1232 shishi_tkt_renew_till (Shishi_tkt * tkt,
1233 char **renewtill, size_t * renewtilllen)
1235 return shishi_asn1_read_optional (tkt->handle, tkt->enckdcreppart,
1236 "renew-till", renewtill, renewtilllen);
1240 * shishi_tkt_renew_tillc:
1241 * @tkt: input variable with ticket info.
1243 * Extract C time corresponding to the renew-till field. The field
1244 * holds the time where the ticket stop being valid for renewal.
1246 * Return value: Returns C time interpretation of the renew-till in ticket.
1248 time_t
1249 shishi_tkt_renew_tillc (Shishi_tkt * tkt)
1251 char *renewtill;
1252 size_t renewtilllen;
1253 time_t t = (time_t) - 1;
1254 int res;
1256 res = shishi_tkt_renew_till (tkt, &renewtill, &renewtilllen);
1257 if (res != SHISHI_OK || renewtilllen == 0)
1258 return t;
1260 if (renewtilllen == SHISHI_GENERALIZEDTIME_LENGTH + 1) /* XXX why +1 ? */
1261 t = shishi_generalize_ctime (tkt->handle, renewtill);
1263 free (renewtill);
1265 return t;
1269 * shishi_tkt_valid_at_time_p:
1270 * @tkt: input variable with ticket info.
1271 * @now: time to check for.
1273 * Determine if ticket is valid at a specific point in time.
1275 * Return value: Returns non-0 iff ticket is valid (not expired and
1276 * after starttime) at specified time.
1279 shishi_tkt_valid_at_time_p (Shishi_tkt * tkt, time_t now)
1281 time_t starttime, endtime;
1283 starttime = shishi_tkt_startctime (tkt);
1284 if (starttime == (time_t) - 1)
1285 starttime = shishi_tkt_authctime (tkt);
1286 endtime = shishi_tkt_endctime (tkt);
1288 return starttime <= now && now <= endtime;
1292 * shishi_tkt_valid_now_p:
1293 * @tkt: input variable with ticket info.
1295 * Determine if ticket is valid now.
1297 * Return value: Returns 0 iff ticket is invalid (expired or not yet
1298 * valid).
1301 shishi_tkt_valid_now_p (Shishi_tkt * tkt)
1303 return shishi_tkt_valid_at_time_p (tkt, time (NULL));
1307 * shishi_tkt_expired_p:
1308 * @tkt: input variable with ticket info.
1310 * Determine if ticket has expired (i.e., endtime is in the past).
1312 * Return value: Returns 0 iff ticket has expired.
1315 shishi_tkt_expired_p (Shishi_tkt * tkt)
1317 time_t endtime = shishi_tkt_endctime (tkt);
1318 time_t now = time (NULL);
1320 return endtime < now;
1324 * shishi_tkt_lastreq_pretty_print:
1325 * @tkt: input variable with ticket info.
1326 * @fh: file handle open for writing.
1328 * Print a human readable representation of the various lastreq fields
1329 * in the ticket (really EncKDCRepPart).
1331 void
1332 shishi_tkt_lastreq_pretty_print (Shishi_tkt * tkt, FILE * fh)
1334 time_t t;
1336 t = shishi_tkt_lastreqc (tkt, SHISHI_LRTYPE_LAST_INITIAL_TGT_REQUEST);
1337 if (t != (time_t) - 1)
1338 fprintf (fh, _("Time of last initial request for a TGT:\t%s"),
1339 ctime (&t));
1341 t = shishi_tkt_lastreqc (tkt, SHISHI_LRTYPE_LAST_INITIAL_REQUEST);
1342 if (t != (time_t) - 1)
1343 fprintf (fh, "Time of last initial request:\t%s", ctime (&t));
1345 t = shishi_tkt_lastreqc (tkt, SHISHI_LRTYPE_NEWEST_TGT_ISSUE);
1346 if (t != (time_t) - 1)
1347 fprintf (fh,
1348 "Time of issue for the newest ticket-granting ticket used:\t%s",
1349 ctime (&t));
1351 t = shishi_tkt_lastreqc (tkt, SHISHI_LRTYPE_LAST_RENEWAL);
1352 if (t != (time_t) - 1)
1353 fprintf (fh, "Time of the last renewal:\t%s", ctime (&t));
1355 t = shishi_tkt_lastreqc (tkt, SHISHI_LRTYPE_LAST_REQUEST);
1356 if (t != (time_t) - 1)
1357 fprintf (fh, "Time of last request:\t%s", ctime (&t));
1361 * shishi_tkt_pretty_print:
1362 * @tkt: input variable with ticket info.
1363 * @fh: file handle open for writing.
1365 * Print a human readable representation of a ticket to file handle.
1367 void
1368 shishi_tkt_pretty_print (Shishi_tkt * tkt, FILE * fh)
1370 char *buf;
1371 char *p;
1372 size_t buflen;
1373 int keytype, etype;
1374 uint32_t flags;
1375 int res;
1376 time_t t;
1377 time_t now = time (NULL);
1379 res = shishi_tkt_clientrealm (tkt, &buf, &buflen);
1380 if (res == SHISHI_OK)
1382 fprintf (fh, "%s:\n", buf);
1383 free (buf);
1385 else
1386 fprintf (fh, "<unknown>:\n");
1388 t = shishi_tkt_authctime (tkt);
1389 fprintf (fh, _("Authtime:\t%s"), ctime (&t));
1391 t = shishi_tkt_startctime (tkt);
1392 if (t != (time_t) - 1)
1394 p = ctime (&t);
1395 p[strlen (p) - 1] = '\0';
1396 fprintf (fh, _("Starttime:\t%s"), p);
1397 if (t > now)
1398 fprintf (fh, " NOT YET VALID");
1399 fprintf (fh, "\n");
1402 t = shishi_tkt_endctime (tkt);
1403 if (t != (time_t) - 1)
1405 p = ctime (&t);
1406 p[strlen (p) - 1] = '\0';
1407 fprintf (fh, _("Endtime:\t%s"), p);
1408 if (t < now)
1409 fprintf (fh, " EXPIRED");
1410 fprintf (fh, "\n");
1413 t = shishi_tkt_renew_tillc (tkt);
1414 if (t != (time_t) - 1)
1415 fprintf (fh, _("Renewable till:\t%s"), ctime (&t));
1417 res = shishi_tkt_server (tkt, &buf, NULL);
1418 if (res == SHISHI_OK)
1420 res = shishi_ticket_get_enc_part_etype (tkt->handle, tkt->ticket,
1421 &keytype);
1422 if (res == SHISHI_OK)
1423 fprintf (fh, _("Server:\t\t%s key %s (%d)\n"), buf,
1424 shishi_cipher_name (keytype), keytype);
1425 free (buf);
1428 res = shishi_tkt_keytype (tkt, &keytype);
1429 if (res == SHISHI_OK)
1430 res = shishi_kdcrep_get_enc_part_etype (tkt->handle, tkt->kdcrep, &etype);
1431 if (res == SHISHI_OK)
1432 fprintf (fh, _("Ticket key:\t%s (%d) protected by %s (%d)\n"),
1433 shishi_cipher_name (keytype), keytype,
1434 shishi_cipher_name (etype), etype);
1436 res = shishi_tkt_flags (tkt, &flags);
1437 if (res == SHISHI_OK && flags)
1439 fprintf (fh, _("Ticket flags:\t"));
1440 if (shishi_tkt_forwardable_p (tkt))
1441 fprintf (fh, "FORWARDABLE ");
1442 if (shishi_tkt_forwarded_p (tkt))
1443 fprintf (fh, "FORWARDED ");
1444 if (shishi_tkt_proxiable_p (tkt))
1445 fprintf (fh, "PROXIABLE ");
1446 if (shishi_tkt_proxy_p (tkt))
1447 fprintf (fh, "PROXY ");
1448 if (shishi_tkt_may_postdate_p (tkt))
1449 fprintf (fh, "MAYPOSTDATE ");
1450 if (shishi_tkt_postdated_p (tkt))
1451 fprintf (fh, "POSTDATED ");
1452 if (shishi_tkt_invalid_p (tkt))
1453 fprintf (fh, "INVALID ");
1454 if (shishi_tkt_renewable_p (tkt))
1455 fprintf (fh, "RENEWABLE ");
1456 if (shishi_tkt_initial_p (tkt))
1457 fprintf (fh, "INITIAL ");
1458 if (shishi_tkt_pre_authent_p (tkt))
1459 fprintf (fh, "PREAUTHENT ");
1460 if (shishi_tkt_hw_authent_p (tkt))
1461 fprintf (fh, "HWAUTHENT ");
1462 if (shishi_tkt_transited_policy_checked_p (tkt))
1463 fprintf (fh, "TRANSITEDPOLICYCHECKED ");
1464 if (shishi_tkt_ok_as_delegate_p (tkt))
1465 fprintf (fh, "OKASDELEGATE ");
1466 fprintf (fh, "(%d)\n", flags);
1471 shishi_tkt_decrypt (Shishi_tkt * tkt, Shishi_key * key)
1473 int rc;
1474 Shishi_asn1 encticketpart;
1476 rc = shishi_ticket_decrypt (tkt->handle, tkt->ticket, key, &encticketpart);
1477 if (rc != SHISHI_OK)
1478 return rc;
1480 tkt->encticketpart = encticketpart;
1482 return SHISHI_OK;