1 /* ap.c --- AP functions
2 * Copyright (C) 2002, 2003, 2004, 2006, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify it 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 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
30 Shishi_asn1 authenticator
;
33 Shishi_asn1 encapreppart
;
34 /* Key usage for encryption entire Authenticator ASN.1 blob, stored
36 int authenticatorkeyusage
;
37 /* Key usage for computing checksum of authenticatorcksumdata in the
38 Authenticator, in AP-REQ. */
39 int authenticatorcksumkeyusage
;
40 /* Sets the checksum algorithm type in Authenticator, in AP-REQ. If
41 there is data in authenticatorcksumdata to compute a checksum on,
42 this also indicate the algorithm to use for this computation. */
43 int32_t authenticatorcksumtype
;
44 /* Auxilliary application data to compute checksum on and store in
45 Authenticator, in AP-REQ. Note that data is not stored in
46 AP-REQ, only a checksum of it. */
47 char *authenticatorcksumdata
;
48 size_t authenticatorcksumdatalen
;
49 /* Raw checksum data to store in Authenticator, in AP-REQ.
50 Normally, this is the output of the checksum algorithm computed
51 on the data in authenticatorcksumdata, but some applications
52 (e.g., GSS-API) put something weird in the checksum field. */
53 char *authenticatorcksumraw
;
54 size_t authenticatorcksumrawlen
;
59 * @handle: shishi handle as allocated by shishi_init().
60 * @ap: pointer to new structure that holds information about AP exchange
62 * Create a new AP exchange with a random subkey of the default
63 * encryption type from configuration. Note that there is no
64 * guarantee that the receiver will understand that key type, you
65 * should probably use shishi_ap_etype() or shishi_ap_nosubkey()
66 * instead. In the future, this function will likely behave as
67 * shishi_ap_nosubkey() and shishi_ap_nosubkey() will be removed.
69 * Return value: Returns SHISHI_OK iff successful.
72 shishi_ap (Shishi
* handle
, Shishi_ap
** ap
)
76 res
= shishi_ap_nosubkey (handle
, ap
);
79 shishi_error_printf (handle
, "Could not create Authenticator: %s\n",
80 shishi_error (handle
));
84 res
= shishi_authenticator_add_random_subkey (handle
, (*ap
)->authenticator
);
87 shishi_error_printf (handle
, "Could not add random subkey in AP: %s\n",
88 shishi_strerror (res
));
97 * @handle: shishi handle as allocated by shishi_init().
98 * @ap: pointer to new structure that holds information about AP exchange
99 * @etype: encryption type of newly generated random subkey.
101 * Create a new AP exchange with a random subkey of indicated
104 * Return value: Returns SHISHI_OK iff successful.
107 shishi_ap_etype (Shishi
* handle
, Shishi_ap
** ap
, int etype
)
111 res
= shishi_ap_nosubkey (handle
, ap
);
112 if (res
!= SHISHI_OK
)
114 shishi_error_printf (handle
, "Could not create Authenticator: %s\n",
115 shishi_error (handle
));
119 res
= shishi_authenticator_add_random_subkey_etype (handle
,
120 (*ap
)->authenticator
,
122 if (res
!= SHISHI_OK
)
124 shishi_error_printf (handle
, "Could not add random subkey in AP: %s\n",
125 shishi_strerror (res
));
133 * shishi_ap_nosubkey:
134 * @handle: shishi handle as allocated by shishi_init().
135 * @ap: pointer to new structure that holds information about AP exchange
137 * Create a new AP exchange without subkey in authenticator.
139 * Return value: Returns SHISHI_OK iff successful.
142 shishi_ap_nosubkey (Shishi
* handle
, Shishi_ap
** ap
)
146 *ap
= xcalloc (1, sizeof (**ap
));
149 lap
->handle
= handle
;
150 lap
->authenticatorcksumtype
= SHISHI_NO_CKSUMTYPE
;
151 lap
->authenticatorcksumkeyusage
= SHISHI_KEYUSAGE_APREQ_AUTHENTICATOR_CKSUM
;
152 lap
->authenticatorkeyusage
= SHISHI_KEYUSAGE_APREQ_AUTHENTICATOR
;
154 lap
->authenticator
= shishi_authenticator (handle
);
155 if (lap
->authenticator
== NULL
)
157 shishi_error_printf (handle
, "Could not create Authenticator: %s\n",
158 shishi_error (handle
));
159 return SHISHI_ASN1_ERROR
;
162 lap
->apreq
= shishi_apreq (handle
);
163 if (lap
->apreq
== NULL
)
165 shishi_error_printf (handle
, "Could not create AP-REQ: %s\n",
166 shishi_error (handle
));
167 return SHISHI_ASN1_ERROR
;
170 lap
->aprep
= shishi_aprep (handle
);
171 if (lap
->aprep
== NULL
)
173 shishi_error_printf (handle
, "Could not create AP-REP: %s\n",
174 shishi_error (handle
));
175 return SHISHI_ASN1_ERROR
;
178 lap
->encapreppart
= shishi_encapreppart (handle
);
179 if (lap
->encapreppart
== NULL
)
181 shishi_error_printf (handle
, "Could not create EncAPRepPart: %s\n",
182 shishi_error (handle
));
183 return SHISHI_ASN1_ERROR
;
191 * @ap: structure that holds information about AP exchange
193 * Deallocate resources associated with AP exchange. This should be
194 * called by the application when it no longer need to utilize the AP
198 shishi_ap_done (Shishi_ap
* ap
)
200 if (ap
->authenticatorcksumdata
)
201 free (ap
->authenticatorcksumdata
);
202 if (ap
->authenticatorcksumraw
)
203 free (ap
->authenticatorcksumraw
);
204 shishi_asn1_done (ap
->handle
, ap
->authenticator
);
205 shishi_asn1_done (ap
->handle
, ap
->apreq
);
206 shishi_asn1_done (ap
->handle
, ap
->aprep
);
207 shishi_asn1_done (ap
->handle
, ap
->encapreppart
);
212 * shishi_ap_set_tktoptions:
213 * @ap: structure that holds information about AP exchange
214 * @tkt: ticket to set in AP.
215 * @options: AP-REQ options to set in AP.
217 * Set the ticket (see shishi_ap_tkt_set()) and set the AP-REQ
218 * apoptions (see shishi_apreq_options_set()).
220 * Return value: Returns SHISHI_OK iff successful.
223 shishi_ap_set_tktoptions (Shishi_ap
* ap
, Shishi_tkt
* tkt
, int options
)
227 shishi_ap_tkt_set (ap
, tkt
);
229 rc
= shishi_apreq_options_set (ap
->handle
, shishi_ap_req (ap
), options
);
232 printf ("Could not set AP-Options: %s", shishi_strerror (rc
));
240 * shishi_ap_set_tktoptionsdata:
241 * @ap: structure that holds information about AP exchange
242 * @tkt: ticket to set in AP.
243 * @options: AP-REQ options to set in AP.
244 * @data: input array with data to checksum in Authenticator.
245 * @len: length of input array with data to checksum in Authenticator.
247 * Set the ticket (see shishi_ap_tkt_set()) and set the AP-REQ
248 * apoptions (see shishi_apreq_options_set()) and set the
249 * Authenticator checksum data.
251 * Return value: Returns SHISHI_OK iff successful.
254 shishi_ap_set_tktoptionsdata (Shishi_ap
* ap
,
256 int options
, const char *data
, size_t len
)
260 shishi_ap_tkt_set (ap
, tkt
);
262 rc
= shishi_apreq_options_set (ap
->handle
, shishi_ap_req (ap
), options
);
265 printf ("Could not set AP-Options: %s", shishi_strerror (rc
));
269 shishi_ap_authenticator_cksumdata_set (ap
, data
, len
);
276 * shishi_ap_set_tktoptionsraw:
277 * @ap: structure that holds information about AP exchange
278 * @tkt: ticket to set in AP.
279 * @options: AP-REQ options to set in AP.
280 * @cksumtype: authenticator checksum type to set in AP.
281 * @data: input array with data to store in checksum field in Authenticator.
282 * @len: length of input array with data to store in checksum field in
285 * Set the ticket (see shishi_ap_tkt_set()) and set the AP-REQ
286 * apoptions (see shishi_apreq_options_set()) and set the raw
287 * Authenticator checksum data.
289 * Return value: Returns SHISHI_OK iff successful.
292 shishi_ap_set_tktoptionsraw (Shishi_ap
* ap
,
295 int32_t cksumtype
, const char *data
, size_t len
)
299 shishi_ap_tkt_set (ap
, tkt
);
301 rc
= shishi_apreq_options_set (ap
->handle
, shishi_ap_req (ap
), options
);
304 printf ("Could not set AP-Options: %s", shishi_strerror (rc
));
308 shishi_ap_authenticator_cksumraw_set (ap
, cksumtype
, data
, len
);
314 * shishi_ap_set_tktoptionsasn1usage:
315 * @ap: structure that holds information about AP exchange
316 * @tkt: ticket to set in AP.
317 * @options: AP-REQ options to set in AP.
318 * @node: input ASN.1 structure to store as authenticator checksum data.
319 * @field: field in ASN.1 structure to use.
320 * @authenticatorcksumkeyusage: key usage for checksum in authenticator.
321 * @authenticatorkeyusage: key usage for authenticator.
323 * Set ticket, options and authenticator checksum data using
324 * shishi_ap_set_tktoptionsdata(). The authenticator checksum data is
325 * the DER encoding of the ASN.1 field provided.
327 * Return value: Returns SHISHI_OK iff successful.
330 shishi_ap_set_tktoptionsasn1usage (Shishi_ap
* ap
,
335 int authenticatorcksumkeyusage
,
336 int authenticatorkeyusage
)
342 res
= shishi_asn1_to_der_field (ap
->handle
, node
, field
, &buf
, &buflen
);
343 if (res
!= SHISHI_OK
)
346 res
= shishi_ap_set_tktoptionsdata (ap
, tkt
, options
, buf
, buflen
);
347 if (res
!= SHISHI_OK
)
350 ap
->authenticatorcksumkeyusage
= authenticatorcksumkeyusage
;
351 ap
->authenticatorkeyusage
= authenticatorkeyusage
;
357 * shishi_ap_tktoptions:
358 * @handle: shishi handle as allocated by shishi_init().
359 * @ap: pointer to new structure that holds information about AP exchange
360 * @tkt: ticket to set in newly created AP.
361 * @options: AP-REQ options to set in newly created AP.
363 * Create a new AP exchange using shishi_ap(), and set the ticket and
364 * AP-REQ apoptions using shishi_ap_set_tktoption(). A random session
365 * key is added to the authenticator, using the same keytype as the
368 * Return value: Returns SHISHI_OK iff successful.
371 shishi_ap_tktoptions (Shishi
* handle
,
372 Shishi_ap
** ap
, Shishi_tkt
* tkt
, int options
)
376 rc
= shishi_ap_etype (handle
, ap
, shishi_tkt_keytype_fast (tkt
));
380 rc
= shishi_ap_set_tktoptions (*ap
, tkt
, options
);
388 * shishi_ap_tktoptionsdata:
389 * @handle: shishi handle as allocated by shishi_init().
390 * @ap: pointer to new structure that holds information about AP exchange
391 * @tkt: ticket to set in newly created AP.
392 * @options: AP-REQ options to set in newly created AP.
393 * @data: input array with data to checksum in Authenticator.
394 * @len: length of input array with data to checksum in Authenticator.
396 * Create a new AP exchange using shishi_ap(), and set the ticket,
397 * AP-REQ apoptions and the Authenticator checksum data using
398 * shishi_ap_set_tktoptionsdata(). A random session key is added to
399 * the authenticator, using the same keytype as the ticket.
401 * Return value: Returns SHISHI_OK iff successful.
404 shishi_ap_tktoptionsdata (Shishi
* handle
,
406 Shishi_tkt
* tkt
, int options
,
407 const char *data
, size_t len
)
411 rc
= shishi_ap_etype (handle
, ap
, shishi_tkt_keytype_fast (tkt
));
415 rc
= shishi_ap_set_tktoptionsdata (*ap
, tkt
, options
, data
, len
);
423 * shishi_ap_tktoptionsraw:
424 * @handle: shishi handle as allocated by shishi_init().
425 * @ap: pointer to new structure that holds information about AP exchange
426 * @tkt: ticket to set in newly created AP.
427 * @options: AP-REQ options to set in newly created AP.
428 * @cksumtype: authenticator checksum type to set in AP.
429 * @data: input array with data to store in checksum field in Authenticator.
430 * @len: length of input array with data to store in checksum field in
433 * Create a new AP exchange using shishi_ap(), and set the ticket,
434 * AP-REQ apoptions and the raw Authenticator checksum data field
435 * using shishi_ap_set_tktoptionsraw(). A random session key is added
436 * to the authenticator, using the same keytype as the ticket.
438 * Return value: Returns SHISHI_OK iff successful.
441 shishi_ap_tktoptionsraw (Shishi
* handle
,
443 Shishi_tkt
* tkt
, int options
,
444 int32_t cksumtype
, const char *data
, size_t len
)
448 rc
= shishi_ap_etype (handle
, ap
, shishi_tkt_keytype_fast (tkt
));
452 rc
= shishi_ap_set_tktoptionsraw (*ap
, tkt
, options
, cksumtype
, data
, len
);
460 * shishi_ap_etype_tktoptionsdata:
461 * @handle: shishi handle as allocated by shishi_init().
462 * @ap: pointer to new structure that holds information about AP exchange
463 * @etype: encryption type of newly generated random subkey.
464 * @tkt: ticket to set in newly created AP.
465 * @options: AP-REQ options to set in newly created AP.
466 * @data: input array with data to checksum in Authenticator.
467 * @len: length of input array with data to checksum in Authenticator.
469 * Create a new AP exchange using shishi_ap(), and set the ticket,
470 * AP-REQ apoptions and the Authenticator checksum data using
471 * shishi_ap_set_tktoptionsdata(). A random session key is added to
472 * the authenticator, using the same keytype as the ticket.
474 * Return value: Returns SHISHI_OK iff successful.
477 shishi_ap_etype_tktoptionsdata (Shishi
* handle
,
480 Shishi_tkt
* tkt
, int options
,
481 const char *data
, size_t len
)
485 rc
= shishi_ap_etype (handle
, ap
, etype
);
489 rc
= shishi_ap_set_tktoptionsdata (*ap
, tkt
, options
, data
, len
);
497 * shishi_ap_tktoptionsasn1usage:
498 * @handle: shishi handle as allocated by shishi_init().
499 * @ap: pointer to new structure that holds information about AP exchange
500 * @tkt: ticket to set in newly created AP.
501 * @options: AP-REQ options to set in newly created AP.
502 * @node: input ASN.1 structure to store as authenticator checksum data.
503 * @field: field in ASN.1 structure to use.
504 * @authenticatorcksumkeyusage: key usage for checksum in authenticator.
505 * @authenticatorkeyusage: key usage for authenticator.
507 * Create a new AP exchange using shishi_ap(), and set ticket, options
508 * and authenticator checksum data from the DER encoding of the ASN.1
509 * field using shishi_ap_set_tktoptionsasn1usage(). A random session
510 * key is added to the authenticator, using the same keytype as the
513 * Return value: Returns SHISHI_OK iff successful.
516 shishi_ap_tktoptionsasn1usage (Shishi
* handle
,
522 int authenticatorcksumkeyusage
,
523 int authenticatorkeyusage
)
527 rc
= shishi_ap_etype (handle
, ap
, shishi_tkt_keytype_fast (tkt
));
531 rc
= shishi_ap_set_tktoptionsasn1usage (*ap
, tkt
, options
,
533 authenticatorcksumkeyusage
,
534 authenticatorkeyusage
);
543 * @ap: structure that holds information about AP exchange
545 * Get Ticket from AP exchange.
547 * Return value: Returns the ticket from the AP exchange, or NULL if
548 * not yet set or an error occured.
551 shishi_ap_tkt (Shishi_ap
* ap
)
558 * @ap: structure that holds information about AP exchange
559 * @tkt: ticket to store in AP.
561 * Set the Ticket in the AP exchange.
564 shishi_ap_tkt_set (Shishi_ap
* ap
, Shishi_tkt
* tkt
)
570 * shishi_ap_authenticator_cksumdata:
571 * @ap: structure that holds information about AP exchange
572 * @out: output array that holds authenticator checksum data.
573 * @len: on input, maximum length of output array that holds
574 * authenticator checksum data, on output actual length of
575 * output array that holds authenticator checksum data.
577 * Get checksum data from Authenticator.
579 * Return value: Returns SHISHI_OK if successful, or
580 * SHISHI_TOO_SMALL_BUFFER if buffer provided was too small.
583 shishi_ap_authenticator_cksumdata (Shishi_ap
* ap
, char *out
, size_t * len
)
585 if (*len
< ap
->authenticatorcksumdatalen
)
586 return SHISHI_TOO_SMALL_BUFFER
;
587 if (ap
->authenticatorcksumdata
)
588 memcpy (out
, ap
->authenticatorcksumdata
, ap
->authenticatorcksumdatalen
);
589 *len
= ap
->authenticatorcksumdatalen
;
594 * shishi_ap_authenticator_cksumdata_set:
595 * @ap: structure that holds information about AP exchange
596 * @authenticatorcksumdata: input array with data to compute checksum
597 * on and store in Authenticator in AP-REQ.
598 * @authenticatorcksumdatalen: length of input array with data to
599 * compute checksum on and store in Authenticator in AP-REQ.
601 * Set the Authenticator Checksum Data in the AP exchange. This is
602 * the data that will be checksumed, and the checksum placed in the
603 * checksum field. It is not the actual checksum field. See also
604 * shishi_ap_authenticator_cksumraw_set.
607 shishi_ap_authenticator_cksumdata_set (Shishi_ap
* ap
,
608 const char *authenticatorcksumdata
,
609 size_t authenticatorcksumdatalen
)
611 ap
->authenticatorcksumdata
= xmemdup (authenticatorcksumdata
,
612 authenticatorcksumdatalen
);
613 ap
->authenticatorcksumdatalen
= authenticatorcksumdatalen
;
617 * shishi_ap_authenticator_cksumraw_set:
618 * @ap: structure that holds information about AP exchange
619 * @authenticatorcksumtype: authenticator checksum type to set in AP.
620 * @authenticatorcksumraw: input array with authenticator checksum
621 * field value to set in Authenticator in AP-REQ.
622 * @authenticatorcksumrawlen: length of input array with
623 * authenticator checksum field value to set in Authenticator in AP-REQ.
625 * Set the Authenticator Checksum Data in the AP exchange. This is
626 * the actual checksum field, not data to compute checksum on and then
627 * store in the checksum field. See also
628 * shishi_ap_authenticator_cksumdata_set.
631 shishi_ap_authenticator_cksumraw_set (Shishi_ap
* ap
,
632 int32_t authenticatorcksumtype
,
633 const char *authenticatorcksumraw
,
634 size_t authenticatorcksumrawlen
)
636 shishi_ap_authenticator_cksumtype_set (ap
, authenticatorcksumtype
);
637 ap
->authenticatorcksumraw
= xmemdup (authenticatorcksumraw
,
638 authenticatorcksumrawlen
);
639 ap
->authenticatorcksumrawlen
= authenticatorcksumrawlen
;
643 * shishi_ap_authenticator_cksumtype:
644 * @ap: structure that holds information about AP exchange
646 * Get the Authenticator Checksum Type in the AP exchange.
648 * Return value: Return the authenticator checksum type.
651 shishi_ap_authenticator_cksumtype (Shishi_ap
* ap
)
653 return ap
->authenticatorcksumtype
;
657 * shishi_ap_authenticator_cksumtype_set:
658 * @ap: structure that holds information about AP exchange
659 * @cksumtype: authenticator checksum type to set in AP.
661 * Set the Authenticator Checksum Type in the AP exchange.
664 shishi_ap_authenticator_cksumtype_set (Shishi_ap
* ap
, int32_t cksumtype
)
666 ap
->authenticatorcksumtype
= cksumtype
;
670 * shishi_ap_authenticator:
671 * @ap: structure that holds information about AP exchange
673 * Get ASN.1 Authenticator structure from AP exchange.
675 * Return value: Returns the Authenticator from the AP exchange, or
676 * NULL if not yet set or an error occured.
680 shishi_ap_authenticator (Shishi_ap
* ap
)
682 return ap
->authenticator
;
686 * shishi_ap_authenticator_set:
687 * @ap: structure that holds information about AP exchange
688 * @authenticator: authenticator to store in AP.
690 * Set the Authenticator in the AP exchange.
693 shishi_ap_authenticator_set (Shishi_ap
* ap
, Shishi_asn1 authenticator
)
695 if (ap
->authenticator
)
696 shishi_asn1_done (ap
->handle
, ap
->authenticator
);
697 ap
->authenticator
= authenticator
;
702 * @ap: structure that holds information about AP exchange
704 * Get ASN.1 AP-REQ structure from AP exchange.
706 * Return value: Returns the AP-REQ from the AP exchange, or NULL if
707 * not yet set or an error occured.
710 shishi_ap_req (Shishi_ap
* ap
)
718 * @ap: structure that holds information about AP exchange
719 * @apreq: apreq to store in AP.
721 * Set the AP-REQ in the AP exchange.
724 shishi_ap_req_set (Shishi_ap
* ap
, Shishi_asn1 apreq
)
727 shishi_asn1_done (ap
->handle
, ap
->apreq
);
733 * @ap: structure that holds information about AP exchange
734 * @out: pointer to output array with der encoding of AP-REQ.
735 * @outlen: pointer to length of output array with der encoding of AP-REQ.
737 * Build AP-REQ using shishi_ap_req_build() and DER encode it. @out
738 * is allocated by this function, and it is the responsibility of
739 * caller to deallocate it.
741 * Return value: Returns SHISHI_OK iff successful.
744 shishi_ap_req_der (Shishi_ap
* ap
, char **out
, size_t * outlen
)
748 rc
= shishi_ap_req_build (ap
);
752 rc
= shishi_asn1_to_der (ap
->handle
, ap
->apreq
, out
, outlen
);
760 * shishi_ap_req_der_set:
761 * @ap: structure that holds information about AP exchange
762 * @der: input array with DER encoded AP-REQ.
763 * @derlen: length of input array with DER encoded AP-REQ.
765 * DER decode AP-REQ and set it AP exchange. If decoding fails, the
766 * AP-REQ in the AP exchange is lost.
768 * Return value: Returns SHISHI_OK.
771 shishi_ap_req_der_set (Shishi_ap
* ap
, char *der
, size_t derlen
)
773 ap
->apreq
= shishi_der2asn1_apreq (ap
->handle
, der
, derlen
);
778 return SHISHI_ASN1_ERROR
;
782 * shishi_ap_req_build:
783 * @ap: structure that holds information about AP exchange
785 * Checksum data in authenticator and add ticket and authenticator to
788 * Return value: Returns SHISHI_OK iff successful.
791 shishi_ap_req_build (Shishi_ap
* ap
)
796 if (VERBOSE (ap
->handle
))
797 printf ("Building AP-REQ...\n");
799 if (VERBOSEASN1 (ap
->handle
))
801 shishi_ticket_print (ap
->handle
, stdout
, shishi_tkt_ticket (ap
->tkt
));
802 shishi_key_print (ap
->handle
, stdout
, shishi_tkt_key (ap
->tkt
));
806 res
= shishi_apreq_set_ticket (ap
->handle
, ap
->apreq
,
807 shishi_tkt_ticket (ap
->tkt
));
808 if (res
!= SHISHI_OK
)
810 shishi_error_printf (ap
->handle
, "Could not set ticket in AP-REQ: %s\n",
811 shishi_error (ap
->handle
));
815 cksumtype
= shishi_ap_authenticator_cksumtype (ap
);
816 if (ap
->authenticatorcksumraw
&& ap
->authenticatorcksumrawlen
> 0)
817 res
= shishi_authenticator_set_cksum (ap
->handle
, ap
->authenticator
,
819 ap
->authenticatorcksumraw
,
820 ap
->authenticatorcksumrawlen
);
821 else if (cksumtype
== SHISHI_NO_CKSUMTYPE
)
822 res
= shishi_authenticator_add_cksum (ap
->handle
, ap
->authenticator
,
823 shishi_tkt_key (ap
->tkt
),
824 ap
->authenticatorcksumkeyusage
,
825 ap
->authenticatorcksumdata
,
826 ap
->authenticatorcksumdatalen
);
828 res
= shishi_authenticator_add_cksum_type (ap
->handle
, ap
->authenticator
,
829 shishi_tkt_key (ap
->tkt
),
830 ap
->authenticatorcksumkeyusage
,
832 ap
->authenticatorcksumdata
,
833 ap
->authenticatorcksumdatalen
);
834 if (res
!= SHISHI_OK
)
836 shishi_error_printf (ap
->handle
,
837 "Could not add checksum to authenticator: %s\n",
838 shishi_error (ap
->handle
));
842 if (VERBOSE (ap
->handle
))
843 printf ("Got Authenticator...\n");
845 if (VERBOSEASN1 (ap
->handle
))
846 shishi_authenticator_print (ap
->handle
, stdout
, ap
->authenticator
);
848 res
= shishi_apreq_add_authenticator (ap
->handle
, ap
->apreq
,
849 shishi_tkt_key (ap
->tkt
),
850 ap
->authenticatorkeyusage
,
852 if (res
!= SHISHI_OK
)
854 shishi_error_printf (ap
->handle
, "Could not set authenticator: %s\n",
855 shishi_error (ap
->handle
));
859 if (VERBOSEASN1 (ap
->handle
))
860 shishi_apreq_print (ap
->handle
, stdout
, ap
->apreq
);
866 * shishi_ap_req_decode:
867 * @ap: structure that holds information about AP exchange
869 * Decode ticket in AP-REQ and set the Ticket fields in the AP
872 * Return value: Returns SHISHI_OK iff successful.
875 shishi_ap_req_decode (Shishi_ap
* ap
)
880 if (VERBOSEASN1 (ap
->handle
))
881 shishi_apreq_print (ap
->handle
, stdout
, ap
->apreq
);
883 rc
= shishi_apreq_get_ticket (ap
->handle
, ap
->apreq
, &ticket
);
886 shishi_error_printf (ap
->handle
,
887 "Could not extract ticket from AP-REQ: %s\n",
888 shishi_strerror (rc
));
892 if (VERBOSEASN1 (ap
->handle
))
893 shishi_ticket_print (ap
->handle
, stdout
, ticket
);
895 rc
= shishi_tkt (ap
->handle
, &ap
->tkt
);
899 shishi_tkt_ticket_set (ap
->tkt
, ticket
);
905 * shishi_ap_req_process_keyusage:
906 * @ap: structure that holds information about AP exchange
907 * @key: cryptographic key used to decrypt ticket in AP-REQ.
908 * @keyusage: key usage to use during decryption, for normal
909 * AP-REQ's this is normally SHISHI_KEYUSAGE_APREQ_AUTHENTICATOR,
910 * for AP-REQ's part of TGS-REQ's, this is normally
911 * SHISHI_KEYUSAGE_TGSREQ_APREQ_AUTHENTICATOR.
913 * Decrypt ticket in AP-REQ using supplied key and decrypt
914 * Authenticator in AP-REQ using key in decrypted ticket, and on
915 * success set the Ticket and Authenticator fields in the AP exchange.
917 * Return value: Returns SHISHI_OK iff successful.
920 shishi_ap_req_process_keyusage (Shishi_ap
* ap
,
921 Shishi_key
* key
, int32_t keyusage
)
923 Shishi_asn1 authenticator
;
927 rc
= shishi_ap_req_decode (ap
);
930 shishi_error_printf (ap
->handle
, "Error decoding ticket: %s\n",
931 shishi_strerror (rc
));
935 rc
= shishi_tkt_decrypt (ap
->tkt
, key
);
938 shishi_error_printf (ap
->handle
, "Error decrypting ticket: %s\n",
939 shishi_strerror (rc
));
943 rc
= shishi_encticketpart_get_key (ap
->handle
,
944 shishi_tkt_encticketpart (ap
->tkt
),
948 shishi_error_printf (ap
->handle
, "Could not get key from ticket: %s\n",
949 shishi_strerror (rc
));
953 if (VERBOSEASN1 (ap
->handle
))
954 shishi_encticketpart_print (ap
->handle
, stdout
,
955 shishi_tkt_encticketpart (ap
->tkt
));
957 rc
= shishi_apreq_decrypt (ap
->handle
, ap
->apreq
, tktkey
,
958 keyusage
, &authenticator
);
961 shishi_error_printf (ap
->handle
, "Error decrypting apreq: %s\n",
962 shishi_strerror (rc
));
966 /* XXX? verify checksum in authenticator. */
968 if (VERBOSEASN1 (ap
->handle
))
969 shishi_authenticator_print (ap
->handle
, stdout
, authenticator
);
971 if (ap
->authenticatorcksumdata
)
972 free (ap
->authenticatorcksumdata
);
974 rc
= shishi_authenticator_cksum (ap
->handle
, authenticator
,
975 &ap
->authenticatorcksumtype
,
976 &ap
->authenticatorcksumdata
,
977 &ap
->authenticatorcksumdatalen
);
980 shishi_error_printf (ap
->handle
,
981 "Error extracting authenticator checksum: %s\n",
982 shishi_strerror (rc
));
986 ap
->authenticator
= authenticator
;
992 * shishi_ap_req_process:
993 * @ap: structure that holds information about AP exchange
994 * @key: cryptographic key used to decrypt ticket in AP-REQ.
996 * Decrypt ticket in AP-REQ using supplied key and decrypt
997 * Authenticator in AP-REQ using key in decrypted ticket, and on
998 * success set the Ticket and Authenticator fields in the AP exchange.
1000 * Return value: Returns SHISHI_OK iff successful.
1003 shishi_ap_req_process (Shishi_ap
* ap
, Shishi_key
* key
)
1005 return shishi_ap_req_process_keyusage (ap
, key
,
1006 SHISHI_KEYUSAGE_APREQ_AUTHENTICATOR
);
1010 * shishi_ap_req_asn1:
1011 * @ap: structure that holds information about AP exchange
1012 * @apreq: output AP-REQ variable.
1014 * Build AP-REQ using shishi_ap_req_build() and return it.
1016 * Return value: Returns SHISHI_OK iff successful.
1019 shishi_ap_req_asn1 (Shishi_ap
* ap
, Shishi_asn1
* apreq
)
1023 rc
= shishi_ap_req_build (ap
);
1024 if (rc
!= SHISHI_OK
)
1034 * @ap: structure that holds information about AP exchange
1036 * Extract the application key from AP. If subkeys are used, it is
1037 * taken from the Authenticator, otherwise the session key is used.
1039 * Return value: Return application key from AP.
1042 shishi_ap_key (Shishi_ap
* ap
)
1046 /* XXX do real check if subkey is present, don't just assume error
1049 rc
= shishi_authenticator_get_subkey (ap
->handle
, ap
->authenticator
,
1051 if (rc
!= SHISHI_OK
)
1052 ap
->key
= shishi_tkt_key (ap
->tkt
);
1059 * @ap: structure that holds information about AP exchange
1061 * Get ASN.1 AP-REP structure from AP exchange.
1063 * Return value: Returns the AP-REP from the AP exchange, or NULL if
1064 * not yet set or an error occured.
1067 shishi_ap_rep (Shishi_ap
* ap
)
1073 * shishi_ap_rep_set:
1074 * @ap: structure that holds information about AP exchange
1075 * @aprep: aprep to store in AP.
1077 * Set the AP-REP in the AP exchange.
1080 shishi_ap_rep_set (Shishi_ap
* ap
, Shishi_asn1 aprep
)
1083 shishi_asn1_done (ap
->handle
, ap
->aprep
);
1088 * shishi_ap_rep_der:
1089 * @ap: structure that holds information about AP exchange
1090 * @out: output array with newly allocated DER encoding of AP-REP.
1091 * @outlen: length of output array with DER encoding of AP-REP.
1093 * Build AP-REP using shishi_ap_rep_build() and DER encode it. @out
1094 * is allocated by this function, and it is the responsibility of
1095 * caller to deallocate it.
1097 * Return value: Returns SHISHI_OK iff successful.
1100 shishi_ap_rep_der (Shishi_ap
* ap
, char **out
, size_t * outlen
)
1104 rc
= shishi_ap_rep_build (ap
);
1105 if (rc
!= SHISHI_OK
)
1108 rc
= shishi_asn1_to_der (ap
->handle
, ap
->aprep
, out
, outlen
);
1109 if (rc
!= SHISHI_OK
)
1116 * shishi_ap_rep_der_set:
1117 * @ap: structure that holds information about AP exchange
1118 * @der: input array with DER encoded AP-REP.
1119 * @derlen: length of input array with DER encoded AP-REP.
1121 * DER decode AP-REP and set it AP exchange. If decoding fails, the
1122 * AP-REP in the AP exchange remains.
1124 * Return value: Returns SHISHI_OK.
1127 shishi_ap_rep_der_set (Shishi_ap
* ap
, char *der
, size_t derlen
)
1131 aprep
= shishi_der2asn1_aprep (ap
->handle
, der
, derlen
);
1134 return SHISHI_ASN1_ERROR
;
1142 * shishi_ap_rep_build:
1143 * @ap: structure that holds information about AP exchange
1145 * Checksum data in authenticator and add ticket and authenticator to
1148 * Return value: Returns SHISHI_OK iff successful.
1151 shishi_ap_rep_build (Shishi_ap
* ap
)
1156 if (VERBOSE (ap
->handle
))
1157 printf ("Building AP-REP...\n");
1159 aprep
= shishi_aprep (ap
->handle
);
1160 rc
= shishi_aprep_enc_part_make (ap
->handle
, aprep
, ap
->encapreppart
,
1162 shishi_tkt_encticketpart (ap
->tkt
));
1163 if (rc
!= SHISHI_OK
)
1165 shishi_error_printf (ap
->handle
, "Error creating AP-REP: %s\n",
1166 shishi_strerror (rc
));
1170 if (VERBOSEASN1 (ap
->handle
))
1171 shishi_aprep_print (ap
->handle
, stdout
, aprep
);
1173 shishi_ap_rep_set (ap
, aprep
);
1179 * shishi_ap_rep_asn1:
1180 * @ap: structure that holds information about AP exchange
1181 * @aprep: output AP-REP variable.
1183 * Build AP-REP using shishi_ap_rep_build() and return it.
1185 * Return value: Returns SHISHI_OK iff successful.
1188 shishi_ap_rep_asn1 (Shishi_ap
* ap
, Shishi_asn1
* aprep
)
1192 rc
= shishi_ap_rep_build (ap
);
1193 if (rc
!= SHISHI_OK
)
1202 * shishi_ap_rep_verify:
1203 * @ap: structure that holds information about AP exchange
1205 * Verify AP-REP compared to Authenticator.
1207 * Return value: Returns SHISHI_OK, SHISHI_APREP_VERIFY_FAILED or an
1211 shishi_ap_rep_verify (Shishi_ap
* ap
)
1215 if (VERBOSE (ap
->handle
))
1216 printf ("Decrypting AP-REP...\n");
1218 if (VERBOSEASN1 (ap
->handle
))
1219 shishi_aprep_print (ap
->handle
, stdout
, ap
->aprep
);
1221 res
= shishi_aprep_decrypt (ap
->handle
, ap
->aprep
,
1222 shishi_tkt_key (ap
->tkt
),
1223 SHISHI_KEYUSAGE_ENCAPREPPART
,
1225 if (res
!= SHISHI_OK
)
1228 if (VERBOSEASN1 (ap
->handle
))
1229 shishi_encapreppart_print (ap
->handle
, stdout
, ap
->encapreppart
);
1231 res
= shishi_aprep_verify (ap
->handle
, ap
->authenticator
, ap
->encapreppart
);
1232 if (res
!= SHISHI_OK
)
1235 if (VERBOSE (ap
->handle
))
1236 printf ("Verified AP-REP successfully...\n");
1242 * shishi_ap_rep_verify_der:
1243 * @ap: structure that holds information about AP exchange
1244 * @der: input array with DER encoded AP-REP.
1245 * @derlen: length of input array with DER encoded AP-REP.
1247 * DER decode AP-REP and set it in AP exchange using
1248 * shishi_ap_rep_der_set() and verify it using shishi_ap_rep_verify().
1250 * Return value: Returns SHISHI_OK, SHISHI_APREP_VERIFY_FAILED or an
1254 shishi_ap_rep_verify_der (Shishi_ap
* ap
, char *der
, size_t derlen
)
1258 res
= shishi_ap_rep_der_set (ap
, der
, derlen
);
1259 if (res
!= SHISHI_OK
)
1262 res
= shishi_ap_rep_verify (ap
);
1263 if (res
!= SHISHI_OK
)
1270 * shishi_ap_rep_verify_asn1:
1271 * @ap: structure that holds information about AP exchange
1272 * @aprep: input AP-REP.
1274 * Set the AP-REP in the AP exchange using shishi_ap_rep_set() and
1275 * verify it using shishi_ap_rep_verify().
1277 * Return value: Returns SHISHI_OK, SHISHI_APREP_VERIFY_FAILED or an
1281 shishi_ap_rep_verify_asn1 (Shishi_ap
* ap
, Shishi_asn1 aprep
)
1285 shishi_ap_rep_set (ap
, aprep
);
1287 res
= shishi_ap_rep_verify (ap
);
1288 if (res
!= SHISHI_OK
)
1295 * shishi_ap_encapreppart:
1296 * @ap: structure that holds information about AP exchange
1298 * Get ASN.1 EncAPRepPart structure from AP exchange.
1300 * Return value: Returns the EncAPREPPart from the AP exchange, or
1301 * NULL if not yet set or an error occured.
1304 shishi_ap_encapreppart (Shishi_ap
* ap
)
1306 return ap
->encapreppart
;
1310 * shishi_ap_encapreppart_set:
1311 * @ap: structure that holds information about AP exchange
1312 * @encapreppart: EncAPRepPart to store in AP.
1314 * Set the EncAPRepPart in the AP exchange.
1317 shishi_ap_encapreppart_set (Shishi_ap
* ap
, Shishi_asn1 encapreppart
)
1319 if (ap
->encapreppart
)
1320 shishi_asn1_done (ap
->handle
, ap
->encapreppart
);
1321 ap
->encapreppart
= encapreppart
;
1324 #define APOPTION_RESERVED "reserved"
1325 #define APOPTION_USE_SESSION_KEY "use-session-key"
1326 #define APOPTION_MUTUAL_REQUIRED "mutual-required"
1327 #define APOPTION_UNKNOWN "unknown"
1330 * shishi_ap_option2string:
1331 * @option: enumerated AP-Option type, see Shishi_apoptions.
1333 * Convert AP-Option type to AP-Option name string. Note that @option
1334 * must be just one of the AP-Option types, it cannot be an binary
1335 * ORed indicating several AP-Options.
1337 * Return value: Returns static string with name of AP-Option that
1338 * must not be deallocated, or "unknown" if AP-Option was not understood.
1341 shishi_ap_option2string (Shishi_apoptions option
)
1347 case SHISHI_APOPTIONS_RESERVED
:
1348 str
= APOPTION_RESERVED
;
1351 case SHISHI_APOPTIONS_USE_SESSION_KEY
:
1352 str
= APOPTION_USE_SESSION_KEY
;
1355 case SHISHI_APOPTIONS_MUTUAL_REQUIRED
:
1356 str
= APOPTION_MUTUAL_REQUIRED
;
1360 str
= APOPTION_UNKNOWN
;
1368 * shishi_ap_string2option:
1369 * @str: zero terminated character array with name of AP-Option,
1370 * e.g. "use-session-key".
1372 * Convert AP-Option name to AP-Option type.
1374 * Return value: Returns enumerated type member corresponding to AP-Option,
1375 * or 0 if string was not understood.
1378 shishi_ap_string2option (const char *str
)
1382 if (strcasecmp (str
, APOPTION_RESERVED
) == 0)
1383 option
= SHISHI_APOPTIONS_RESERVED
;
1384 else if (strcasecmp (str
, APOPTION_USE_SESSION_KEY
) == 0)
1385 option
= SHISHI_APOPTIONS_USE_SESSION_KEY
;
1386 else if (strcasecmp (str
, APOPTION_MUTUAL_REQUIRED
) == 0)
1387 option
= SHISHI_APOPTIONS_MUTUAL_REQUIRED
;
1389 option
= strtol (str
, (char **) NULL
, 0);