1 /* apreq.c --- AP-REQ 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
25 /* Get _shishi_print_armored_data, etc. */
28 #define SHISHI_APREQ_DEFAULT_PVNO "5"
29 #define SHISHI_APREQ_DEFAULT_PVNO_LEN 0
30 #define SHISHI_APREQ_DEFAULT_MSG_TYPE "14" /* KRB_AP_REQ */
31 #define SHISHI_APREQ_DEFAULT_MSG_TYPE_LEN 0
32 #define SHISHI_APREQ_DEFAULT_AP_OPTIONS "\x00\x00\x00\x00"
33 #define SHISHI_APREQ_DEFAULT_AP_OPTIONS_LEN 32
34 #define SHISHI_APREQ_DEFAULT_TICKET_TKT_VNO "5"
35 #define SHISHI_APREQ_DEFAULT_TICKET_TKT_VNO_LEN 0
36 #define SHISHI_APREQ_DEFAULT_TICKET_REALM ""
37 #define SHISHI_APREQ_DEFAULT_TICKET_REALM_LEN 0
38 #define SHISHI_APREQ_DEFAULT_TICKET_SNAME_NAME_TYPE "1" /* SHISHI_NT_PRINCIPAL */
39 #define SHISHI_APREQ_DEFAULT_TICKET_SNAME_NAME_TYPE_LEN 0
40 #define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_ETYPE "0"
41 #define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_ETYPE_LEN 0
42 #define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_KVNO "0"
43 #define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_KVNO_LEN 0
44 #define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_CIPHER ""
45 #define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_CIPHER_LEN 0
46 #define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_ETYPE "0"
47 #define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_ETYPE_LEN 0
48 #define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_KVNO "1"
49 #define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_KVNO_LEN 0
50 #define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_CIPHER ""
51 #define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_CIPHER_LEN 0
55 * @handle: shishi handle as allocated by shishi_init().
57 * This function creates a new AP-REQ, populated with some default
60 * Return value: Returns the AP-REQ or NULL on failure.
63 shishi_apreq (Shishi
* handle
)
68 node
= shishi_asn1_apreq (handle
);
72 res
= shishi_asn1_write (handle
, node
, "pvno",
73 SHISHI_APREQ_DEFAULT_PVNO
,
74 SHISHI_APREQ_DEFAULT_PVNO_LEN
);
78 res
= shishi_asn1_write (handle
, node
, "msg-type",
79 SHISHI_APREQ_DEFAULT_MSG_TYPE
,
80 SHISHI_APREQ_DEFAULT_MSG_TYPE_LEN
);
84 res
= shishi_asn1_write (handle
, node
, "ap-options",
85 SHISHI_APREQ_DEFAULT_AP_OPTIONS
,
86 SHISHI_APREQ_DEFAULT_AP_OPTIONS_LEN
);
90 res
= shishi_asn1_write (handle
, node
, "ticket.tkt-vno",
91 SHISHI_APREQ_DEFAULT_TICKET_TKT_VNO
,
92 SHISHI_APREQ_DEFAULT_TICKET_TKT_VNO_LEN
);
96 res
= shishi_asn1_write (handle
, node
, "ticket.realm",
97 SHISHI_APREQ_DEFAULT_TICKET_REALM
,
98 SHISHI_APREQ_DEFAULT_TICKET_REALM_LEN
);
102 res
= shishi_asn1_write (handle
, node
, "ticket.realm",
103 SHISHI_APREQ_DEFAULT_TICKET_REALM
,
104 SHISHI_APREQ_DEFAULT_TICKET_REALM_LEN
);
105 if (res
!= SHISHI_OK
)
108 res
= shishi_asn1_write (handle
, node
, "ticket.sname.name-type",
109 SHISHI_APREQ_DEFAULT_TICKET_SNAME_NAME_TYPE
,
110 SHISHI_APREQ_DEFAULT_TICKET_SNAME_NAME_TYPE_LEN
);
111 if (res
!= SHISHI_OK
)
114 res
= shishi_asn1_write (handle
, node
, "ticket.enc-part.etype",
115 SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_ETYPE
,
116 SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_ETYPE_LEN
);
117 if (res
!= SHISHI_OK
)
120 res
= shishi_asn1_write (handle
, node
, "ticket.enc-part.kvno",
121 SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_KVNO
,
122 SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_KVNO_LEN
);
123 if (res
!= SHISHI_OK
)
126 res
= shishi_asn1_write (handle
, node
, "ticket.enc-part.cipher",
127 SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_CIPHER
,
128 SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_CIPHER_LEN
);
129 if (res
!= SHISHI_OK
)
132 res
= shishi_asn1_write (handle
, node
, "authenticator.etype",
133 SHISHI_APREQ_DEFAULT_AUTHENTICATOR_ETYPE
,
134 SHISHI_APREQ_DEFAULT_AUTHENTICATOR_ETYPE_LEN
);
135 if (res
!= SHISHI_OK
)
138 res
= shishi_asn1_write (handle
, node
, "authenticator.kvno",
139 SHISHI_APREQ_DEFAULT_AUTHENTICATOR_KVNO
,
140 SHISHI_APREQ_DEFAULT_AUTHENTICATOR_KVNO_LEN
);
141 if (res
!= SHISHI_OK
)
144 res
= shishi_asn1_write (handle
, node
, "authenticator.cipher",
145 SHISHI_APREQ_DEFAULT_AUTHENTICATOR_CIPHER
,
146 SHISHI_APREQ_DEFAULT_AUTHENTICATOR_CIPHER_LEN
);
147 if (res
!= SHISHI_OK
)
154 shishi_asn1_done (handle
, node
);
159 * shishi_apreq_print:
160 * @handle: shishi handle as allocated by shishi_init().
161 * @fh: file handle open for writing.
162 * @apreq: AP-REQ to print.
164 * Print ASCII armored DER encoding of AP-REQ to file.
166 * Return value: Returns SHISHI_OK iff successful.
169 shishi_apreq_print (Shishi
* handle
, FILE * fh
, Shishi_asn1 apreq
)
171 return _shishi_print_armored_data (handle
, fh
, apreq
, "AP-REQ", NULL
);
176 * @handle: shishi handle as allocated by shishi_init().
177 * @fh: file handle open for writing.
178 * @apreq: AP-REQ to save.
180 * Save DER encoding of AP-REQ to file.
182 * Return value: Returns SHISHI_OK iff successful.
185 shishi_apreq_save (Shishi
* handle
, FILE * fh
, Shishi_asn1 apreq
)
187 return _shishi_save_data (handle
, fh
, apreq
, "AP-REQ");
191 * shishi_apreq_to_file:
192 * @handle: shishi handle as allocated by shishi_init().
193 * @apreq: AP-REQ to save.
194 * @filetype: input variable specifying type of file to be written,
195 * see Shishi_filetype.
196 * @filename: input variable with filename to write to.
198 * Write AP-REQ to file in specified TYPE. The file will be
199 * truncated if it exists.
201 * Return value: Returns SHISHI_OK iff successful.
204 shishi_apreq_to_file (Shishi
* handle
, Shishi_asn1 apreq
,
205 int filetype
, const char *filename
)
210 if (VERBOSE (handle
))
211 printf (_("Writing AP-REQ to %s...\n"), filename
);
213 fh
= fopen (filename
, "w");
215 return SHISHI_FOPEN_ERROR
;
217 if (VERBOSE (handle
))
218 printf (_("Writing AP-REQ in %s format...\n"),
219 filetype
== SHISHI_FILETYPE_TEXT
? "TEXT" : "DER");
221 if (filetype
== SHISHI_FILETYPE_TEXT
)
222 res
= shishi_apreq_print (handle
, fh
, apreq
);
224 res
= shishi_apreq_save (handle
, fh
, apreq
);
225 if (res
!= SHISHI_OK
)
230 return SHISHI_IO_ERROR
;
232 if (VERBOSE (handle
))
233 printf (_("Writing AP-REQ to %s...done\n"), filename
);
239 * shishi_apreq_parse:
240 * @handle: shishi handle as allocated by shishi_init().
241 * @fh: file handle open for reading.
242 * @apreq: output variable with newly allocated AP-REQ.
244 * Read ASCII armored DER encoded AP-REQ from file and populate given
247 * Return value: Returns SHISHI_OK iff successful.
250 shishi_apreq_parse (Shishi
* handle
, FILE * fh
, Shishi_asn1
* apreq
)
252 return _shishi_apreq_input (handle
, fh
, apreq
, 0);
257 * @handle: shishi handle as allocated by shishi_init().
258 * @fh: file handle open for reading.
259 * @apreq: output variable with newly allocated AP-REQ.
261 * Read DER encoded AP-REQ from file and populate given variable.
263 * Return value: Returns SHISHI_OK iff successful.
266 shishi_apreq_read (Shishi
* handle
, FILE * fh
, Shishi_asn1
* apreq
)
268 return _shishi_apreq_input (handle
, fh
, apreq
, 1);
272 * shishi_apreq_from_file:
273 * @handle: shishi handle as allocated by shishi_init().
274 * @apreq: output variable with newly allocated AP-REQ.
275 * @filetype: input variable specifying type of file to be read,
276 * see Shishi_filetype.
277 * @filename: input variable with filename to read from.
279 * Read AP-REQ from file in specified TYPE.
281 * Return value: Returns SHISHI_OK iff successful.
284 shishi_apreq_from_file (Shishi
* handle
, Shishi_asn1
* apreq
,
285 int filetype
, const char *filename
)
290 if (VERBOSE (handle
))
291 printf (_("Reading AP-REQ from %s...\n"), filename
);
293 fh
= fopen (filename
, "r");
295 return SHISHI_FOPEN_ERROR
;
297 if (VERBOSE (handle
))
298 printf (_("Reading AP-REQ in %s format...\n"),
299 filetype
== SHISHI_FILETYPE_TEXT
? "TEXT" : "DER");
301 if (filetype
== SHISHI_FILETYPE_TEXT
)
302 res
= shishi_apreq_parse (handle
, fh
, apreq
);
304 res
= shishi_apreq_read (handle
, fh
, apreq
);
305 if (res
!= SHISHI_OK
)
310 return SHISHI_IO_ERROR
;
312 if (VERBOSE (handle
))
313 printf (_("Reading AP-REQ from %s...done\n"), filename
);
319 * shishi_apreq_set_authenticator:
320 * @handle: shishi handle as allocated by shishi_init().
321 * @apreq: AP-REQ to add authenticator field to.
322 * @etype: encryption type used to encrypt authenticator.
323 * @kvno: version of the key used to encrypt authenticator.
324 * @buf: input array with encrypted authenticator.
325 * @buflen: size of input array with encrypted authenticator.
327 * Set the encrypted authenticator field in the AP-REP. The encrypted
328 * data is usually created by calling shishi_encrypt() on the DER
329 * encoded authenticator. To save time, you may want to use
330 * shishi_apreq_add_authenticator() instead, which calculates the
331 * encrypted data and calls this function in one step.
333 * Return value: Returns SHISHI_OK on success.
336 shishi_apreq_set_authenticator (Shishi
* handle
,
338 int32_t etype
, uint32_t kvno
,
339 const char *buf
, size_t buflen
)
343 res
= shishi_asn1_write (handle
, apreq
, "authenticator.cipher",
345 if (res
!= SHISHI_OK
)
348 if (kvno
== UINT32_MAX
)
349 res
= shishi_asn1_write (handle
, apreq
, "authenticator.kvno", NULL
, 0);
351 res
= shishi_asn1_write_int32 (handle
, apreq
, "authenticator.kvno", kvno
);
352 if (res
!= SHISHI_OK
)
355 res
= shishi_asn1_write_int32 (handle
, apreq
, "authenticator.etype", etype
);
356 if (res
!= SHISHI_OK
)
363 * shishi_apreq_add_authenticator:
364 * @handle: shishi handle as allocated by shishi_init().
365 * @apreq: AP-REQ to add authenticator field to.
366 * @key: key to to use for encryption.
367 * @keyusage: cryptographic key usage value to use in encryption.
368 * @authenticator: authenticator as allocated by shishi_authenticator().
370 * Encrypts DER encoded authenticator using key and store it in the
373 * Return value: Returns SHISHI_OK iff successful.
376 shishi_apreq_add_authenticator (Shishi
* handle
,
379 int keyusage
, Shishi_asn1 authenticator
)
387 res
= shishi_asn1_to_der (handle
, authenticator
, &der
, &derlen
);
388 if (res
!= SHISHI_OK
)
390 shishi_error_printf (handle
, "Could not DER encode authenticator: %s\n",
391 shishi_strerror (res
));
395 res
= shishi_encrypt (handle
, key
, keyusage
, der
, derlen
, &buf
, &buflen
);
399 if (res
!= SHISHI_OK
)
401 shishi_error_printf (handle
, "Cannot encrypt authenticator.\n");
405 res
= shishi_apreq_set_authenticator (handle
, apreq
, shishi_key_type (key
),
406 shishi_key_version (key
),
413 * shishi_apreq_set_ticket:
414 * @handle: shishi handle as allocated by shishi_init().
415 * @apreq: AP-REQ to add ticket field to.
416 * @ticket: input ticket to copy into AP-REQ ticket field.
418 * Copy ticket into AP-REQ.
420 * Return value: Returns SHISHI_OK iff successful.
423 shishi_apreq_set_ticket (Shishi
* handle
, Shishi_asn1 apreq
,
431 res
= shishi_asn1_read (handle
, ticket
, "tkt-vno", &buf
, &buflen
);
432 if (res
!= SHISHI_OK
)
435 res
= shishi_asn1_write (handle
, apreq
, "ticket.tkt-vno", buf
, buflen
);
437 if (res
!= SHISHI_OK
)
440 res
= shishi_asn1_read (handle
, ticket
, "realm", &buf
, &buflen
);
441 if (res
!= SHISHI_OK
)
444 res
= shishi_asn1_write (handle
, apreq
, "ticket.realm", buf
, buflen
);
446 if (res
!= SHISHI_OK
)
449 res
= shishi_asn1_read (handle
, ticket
, "sname.name-type", &buf
, &buflen
);
450 if (res
!= SHISHI_OK
)
453 res
= shishi_asn1_write (handle
, apreq
, "ticket.sname.name-type",
456 if (res
!= SHISHI_OK
)
459 res
= shishi_asn1_number_of_elements (handle
, ticket
,
460 "sname.name-string", &n
);
461 if (res
!= SHISHI_OK
)
464 for (i
= 1; i
<= n
; i
++)
466 res
= shishi_asn1_write (handle
, apreq
,
467 "ticket.sname.name-string", "NEW", 1);
468 if (res
!= SHISHI_OK
)
471 asprintf (&format
, "sname.name-string.?%d", i
);
472 res
= shishi_asn1_read (handle
, ticket
, format
, &buf
, &buflen
);
474 if (res
!= SHISHI_OK
)
477 asprintf (&format
, "ticket.sname.name-string.?%d", i
);
478 res
= shishi_asn1_write (handle
, apreq
, format
, buf
, buflen
);
481 if (res
!= SHISHI_OK
)
485 res
= shishi_asn1_read (handle
, ticket
, "enc-part.etype", &buf
, &buflen
);
486 if (res
!= SHISHI_OK
)
489 res
= shishi_asn1_write (handle
, apreq
, "ticket.enc-part.etype",
492 if (res
!= SHISHI_OK
)
495 res
= shishi_asn1_read (handle
, ticket
, "enc-part.kvno", &buf
, &buflen
);
496 if (res
!= SHISHI_OK
&& res
!= SHISHI_ASN1_NO_ELEMENT
)
499 if (res
== SHISHI_ASN1_NO_ELEMENT
)
500 res
= shishi_asn1_write (handle
, apreq
, "ticket.enc-part.kvno", NULL
, 0);
503 res
= shishi_asn1_write (handle
, apreq
, "ticket.enc-part.kvno",
507 if (res
!= SHISHI_OK
)
510 res
= shishi_asn1_read (handle
, ticket
, "enc-part.cipher", &buf
, &buflen
);
511 if (res
!= SHISHI_OK
)
514 res
= shishi_asn1_write (handle
, apreq
, "ticket.enc-part.cipher",
517 if (res
!= SHISHI_OK
)
524 * shishi_apreq_options:
525 * @handle: shishi handle as allocated by shishi_init().
526 * @apreq: AP-REQ to get options from.
527 * @flags: Output integer containing options from AP-REQ.
529 * Extract the AP-Options from AP-REQ into output integer.
531 * Return value: Returns SHISHI_OK iff successful.
534 shishi_apreq_options (Shishi
* handle
, Shishi_asn1 apreq
, uint32_t * flags
)
536 return shishi_asn1_read_bitstring (handle
, apreq
, "ap-options", flags
);
540 * shishi_apreq_use_session_key_p:
541 * @handle: shishi handle as allocated by shishi_init().
542 * @apreq: AP-REQ as allocated by shishi_apreq().
544 * Return non-0 iff the "Use session key" option is set in the AP-REQ.
546 * Return value: Returns SHISHI_OK iff successful.
549 shishi_apreq_use_session_key_p (Shishi
* handle
, Shishi_asn1 apreq
)
551 uint32_t options
= 0;
553 shishi_apreq_options (handle
, apreq
, &options
);
555 return options
& SHISHI_APOPTIONS_USE_SESSION_KEY
;
559 * shishi_apreq_mutual_required_p:
560 * @handle: shishi handle as allocated by shishi_init().
561 * @apreq: AP-REQ as allocated by shishi_apreq().
563 * Return non-0 iff the "Mutual required" option is set in the AP-REQ.
565 * Return value: Returns SHISHI_OK iff successful.
568 shishi_apreq_mutual_required_p (Shishi
* handle
, Shishi_asn1 apreq
)
570 uint32_t options
= 0;
572 shishi_apreq_options (handle
, apreq
, &options
);
574 return options
& SHISHI_APOPTIONS_MUTUAL_REQUIRED
;
578 * shishi_apreq_options_set:
579 * @handle: shishi handle as allocated by shishi_init().
580 * @apreq: AP-REQ as allocated by shishi_apreq().
581 * @options: Options to set in AP-REQ.
583 * Set the AP-Options in AP-REQ to indicate integer.
585 * Return value: Returns SHISHI_OK iff successful.
588 shishi_apreq_options_set (Shishi
* handle
, Shishi_asn1 apreq
,
593 res
= shishi_asn1_write_bitstring (handle
, apreq
, "ap-options", options
);
594 if (res
!= SHISHI_OK
)
601 * shishi_apreq_options_add:
602 * @handle: shishi handle as allocated by shishi_init().
603 * @apreq: AP-REQ as allocated by shishi_apreq().
604 * @option: Options to add in AP-REQ.
606 * Add the AP-Options in AP-REQ. Options not set in input parameter
607 * @option are preserved in the AP-REQ.
609 * Return value: Returns SHISHI_OK iff successful.
612 shishi_apreq_options_add (Shishi
* handle
, Shishi_asn1 apreq
, uint32_t option
)
617 res
= shishi_apreq_options (handle
, apreq
, &options
);
618 if (res
!= SHISHI_OK
)
623 res
= shishi_apreq_options_set (handle
, apreq
, options
);
624 if (res
!= SHISHI_OK
)
631 * shishi_apreq_options_remove:
632 * @handle: shishi handle as allocated by shishi_init().
633 * @apreq: AP-REQ as allocated by shishi_apreq().
634 * @option: Options to remove from AP-REQ.
636 * Remove the AP-Options from AP-REQ. Options not set in input
637 * parameter @option are preserved in the AP-REQ.
639 * Return value: Returns SHISHI_OK iff successful.
642 shishi_apreq_options_remove (Shishi
* handle
,
643 Shishi_asn1 apreq
, uint32_t option
)
648 res
= shishi_apreq_options (handle
, apreq
, &options
);
649 if (res
!= SHISHI_OK
)
652 options
&= ~(options
& option
);
654 res
= shishi_apreq_options_set (handle
, apreq
, options
);
655 if (res
!= SHISHI_OK
)
662 * shishi_apreq_get_authenticator_etype:
663 * @handle: shishi handle as allocated by shishi_init().
664 * @apreq: AP-REQ variable to get value from.
665 * @etype: output variable that holds the value.
667 * Extract AP-REQ.authenticator.etype.
669 * Return value: Returns SHISHI_OK iff successful.
672 shishi_apreq_get_authenticator_etype (Shishi
* handle
,
673 Shishi_asn1 apreq
, int32_t * etype
)
675 return shishi_asn1_read_int32 (handle
, apreq
, "authenticator.etype", etype
);
679 * shishi_apreq_get_ticket:
680 * @handle: shishi handle as allocated by shishi_init().
681 * @apreq: AP-REQ variable to get ticket from.
682 * @ticket: output variable to hold extracted ticket.
684 * Extract ticket from AP-REQ.
686 * Return value: Returns SHISHI_OK iff successful.
689 shishi_apreq_get_ticket (Shishi
* handle
,
690 Shishi_asn1 apreq
, Shishi_asn1
* ticket
)
697 /* there's GOT to be an easier way to do this */
699 *ticket
= shishi_ticket (handle
);
701 return SHISHI_ASN1_ERROR
;
703 res
= shishi_asn1_read (handle
, apreq
, "ticket.tkt-vno", &buf
, &buflen
);
704 if (res
!= SHISHI_OK
)
707 res
= shishi_asn1_write (handle
, *ticket
, "tkt-vno", buf
, buflen
);
709 if (res
!= SHISHI_OK
)
712 res
= shishi_asn1_read (handle
, apreq
, "ticket.realm", &buf
, &buflen
);
713 if (res
!= SHISHI_OK
)
716 res
= shishi_asn1_write (handle
, *ticket
, "realm", buf
, buflen
);
718 if (res
!= SHISHI_OK
)
721 res
= shishi_asn1_read (handle
, apreq
, "ticket.sname.name-type",
723 if (res
!= SHISHI_OK
)
726 res
= shishi_asn1_write (handle
, *ticket
, "sname.name-type", buf
, buflen
);
728 if (res
!= SHISHI_OK
)
731 res
= shishi_asn1_number_of_elements (handle
, apreq
,
732 "ticket.sname.name-string", &n
);
733 if (res
!= SHISHI_OK
)
736 for (i
= 1; i
<= n
; i
++)
738 res
= shishi_asn1_write (handle
, *ticket
, "sname.name-string",
740 if (res
!= SHISHI_OK
)
743 asprintf (&format
, "ticket.sname.name-string.?%d", i
);
744 res
= shishi_asn1_read (handle
, apreq
, format
, &buf
, &buflen
);
746 if (res
!= SHISHI_OK
)
749 asprintf (&format
, "sname.name-string.?%d", i
);
750 res
= shishi_asn1_write (handle
, *ticket
, format
, buf
, buflen
);
753 if (res
!= SHISHI_OK
)
757 res
= shishi_asn1_read (handle
, apreq
, "ticket.enc-part.etype",
759 if (res
!= SHISHI_OK
)
762 res
= shishi_asn1_write (handle
, *ticket
, "enc-part.etype", buf
, buflen
);
764 if (res
!= SHISHI_OK
)
767 res
= shishi_asn1_read (handle
, apreq
, "ticket.enc-part.kvno",
769 if (res
!= SHISHI_OK
&& res
!= SHISHI_ASN1_NO_ELEMENT
)
772 if (res
== SHISHI_ASN1_NO_ELEMENT
)
773 res
= shishi_asn1_write (handle
, *ticket
, "enc-part.kvno", NULL
, 0);
776 res
= shishi_asn1_write (handle
, *ticket
, "enc-part.kvno", buf
, buflen
);
779 if (res
!= SHISHI_OK
)
782 res
= shishi_asn1_read (handle
, apreq
, "ticket.enc-part.cipher",
784 if (res
!= SHISHI_OK
)
787 res
= shishi_asn1_write (handle
, *ticket
, "enc-part.cipher", buf
, buflen
);
789 if (res
!= SHISHI_OK
)
795 shishi_asn1_done (handle
, *ticket
);
800 shishi_apreq_decrypt (Shishi
* handle
,
803 int keyusage
, Shishi_asn1
* authenticator
)
813 res
= shishi_apreq_get_authenticator_etype (handle
, apreq
, &etype
);
814 if (res
!= SHISHI_OK
)
817 if (etype
!= shishi_key_type (key
))
818 return SHISHI_APREQ_BAD_KEYTYPE
;
820 res
= shishi_asn1_read (handle
, apreq
, "authenticator.cipher",
821 &cipher
, &cipherlen
);
822 if (res
!= SHISHI_OK
)
825 res
= shishi_decrypt (handle
, key
, keyusage
,
826 cipher
, cipherlen
, &buf
, &buflen
);
828 if (res
!= SHISHI_OK
)
830 shishi_error_printf (handle
,
831 "decrypt fail, most likely wrong password\n");
832 return SHISHI_APREQ_DECRYPT_FAILED
;
835 /* The crypto is so 1980; no length indicator. Trim off pad bytes
836 until we can parse it. */
837 for (i
= 0; i
< 8; i
++)
839 if (VERBOSEASN1 (handle
))
840 printf ("Trying with %d pad in enckdcrep...\n", i
);
842 *authenticator
= shishi_der2asn1_authenticator (handle
, &buf
[0],
844 if (*authenticator
!= NULL
)
848 if (*authenticator
== NULL
)
850 shishi_error_printf (handle
, "Could not DER decode Authenticator. "
851 "Password probably correct (decrypt ok) though\n");
852 return SHISHI_ASN1_ERROR
;