1 /* aprep.c --- AP-REP functions.
2 * Copyright (C) 2002, 2003, 2004, 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_APREP_DEFAULT_PVNO "5"
29 #define SHISHI_APREP_DEFAULT_PVNO_LEN 0
30 #define SHISHI_APREP_DEFAULT_MSG_TYPE "15" /* KRB_AP_REP */
31 #define SHISHI_APREP_DEFAULT_MSG_TYPE_LEN 0
32 #define SHISHI_APREP_DEFAULT_ENC_PART_ETYPE "0"
33 #define SHISHI_APREP_DEFAULT_ENC_PART_ETYPE_LEN 0
34 #define SHISHI_APREP_DEFAULT_ENC_PART_KVNO "0"
35 #define SHISHI_APREP_DEFAULT_ENC_PART_KVNO_LEN 0
36 #define SHISHI_APREP_DEFAULT_ENC_PART_CIPHER ""
37 #define SHISHI_APREP_DEFAULT_ENC_PART_CIPHER_LEN 0
41 * @handle: shishi handle as allocated by shishi_init().
43 * This function creates a new AP-REP, populated with some default
46 * Return value: Returns the authenticator or NULL on
50 shishi_aprep (Shishi
* handle
)
55 node
= shishi_asn1_aprep (handle
);
59 res
= shishi_asn1_write (handle
, node
, "pvno",
60 SHISHI_APREP_DEFAULT_PVNO
,
61 SHISHI_APREP_DEFAULT_PVNO_LEN
);
65 res
= shishi_asn1_write (handle
, node
, "msg-type",
66 SHISHI_APREP_DEFAULT_MSG_TYPE
,
67 SHISHI_APREP_DEFAULT_MSG_TYPE_LEN
);
71 res
= shishi_asn1_write (handle
, node
, "enc-part.etype",
72 SHISHI_APREP_DEFAULT_ENC_PART_ETYPE
,
73 SHISHI_APREP_DEFAULT_ENC_PART_ETYPE_LEN
);
77 res
= shishi_asn1_write (handle
, node
, "enc-part.kvno",
78 SHISHI_APREP_DEFAULT_ENC_PART_KVNO
,
79 SHISHI_APREP_DEFAULT_ENC_PART_KVNO_LEN
);
83 res
= shishi_asn1_write (handle
, node
, "enc-part.cipher",
84 SHISHI_APREP_DEFAULT_ENC_PART_CIPHER
,
85 SHISHI_APREP_DEFAULT_ENC_PART_CIPHER_LEN
);
92 shishi_asn1_done (handle
, node
);
98 * @handle: shishi handle as allocated by shishi_init().
99 * @fh: file handle open for writing.
100 * @aprep: AP-REP to print.
102 * Print ASCII armored DER encoding of AP-REP to file.
104 * Return value: Returns SHISHI_OK iff successful.
107 shishi_aprep_print (Shishi
* handle
, FILE * fh
, Shishi_asn1 aprep
)
109 return _shishi_print_armored_data (handle
, fh
, aprep
, "AP-REP", NULL
);
114 * @handle: shishi handle as allocated by shishi_init().
115 * @fh: file handle open for writing.
116 * @aprep: AP-REP to save.
118 * Save DER encoding of AP-REP to file.
120 * Return value: Returns SHISHI_OK iff successful.
123 shishi_aprep_save (Shishi
* handle
, FILE * fh
, Shishi_asn1 aprep
)
125 return _shishi_save_data (handle
, fh
, aprep
, "AP-REP");
129 * shishi_aprep_to_file:
130 * @handle: shishi handle as allocated by shishi_init().
131 * @aprep: AP-REP to save.
132 * @filetype: input variable specifying type of file to be written,
133 * see Shishi_filetype.
134 * @filename: input variable with filename to write to.
136 * Write AP-REP to file in specified TYPE. The file will be
137 * truncated if it exists.
139 * Return value: Returns SHISHI_OK iff successful.
142 shishi_aprep_to_file (Shishi
* handle
, Shishi_asn1 aprep
,
143 int filetype
, const char *filename
)
148 if (VERBOSE (handle
))
149 printf (_("Writing AP-REP to %s...\n"), filename
);
151 fh
= fopen (filename
, "w");
153 return SHISHI_FOPEN_ERROR
;
155 if (VERBOSE (handle
))
156 printf (_("Writing AP-REP in %s format...\n"),
157 filetype
== SHISHI_FILETYPE_TEXT
? "TEXT" : "DER");
159 if (filetype
== SHISHI_FILETYPE_TEXT
)
160 res
= shishi_aprep_print (handle
, fh
, aprep
);
162 res
= shishi_aprep_save (handle
, fh
, aprep
);
163 if (res
!= SHISHI_OK
)
168 return SHISHI_IO_ERROR
;
170 if (VERBOSE (handle
))
171 printf (_("Writing AP-REP to %s...done\n"), filename
);
177 * shishi_aprep_parse:
178 * @handle: shishi handle as allocated by shishi_init().
179 * @fh: file handle open for reading.
180 * @aprep: output variable with newly allocated AP-REP.
182 * Read ASCII armored DER encoded AP-REP from file and populate given
185 * Return value: Returns SHISHI_OK iff successful.
188 shishi_aprep_parse (Shishi
* handle
, FILE * fh
, Shishi_asn1
* aprep
)
190 return _shishi_aprep_input (handle
, fh
, aprep
, 0);
195 * @handle: shishi handle as allocated by shishi_init().
196 * @fh: file handle open for reading.
197 * @aprep: output variable with newly allocated AP-REP.
199 * Read DER encoded AP-REP from file and populate given variable.
201 * Return value: Returns SHISHI_OK iff successful.
204 shishi_aprep_read (Shishi
* handle
, FILE * fh
, Shishi_asn1
* aprep
)
206 return _shishi_aprep_input (handle
, fh
, aprep
, 1);
210 * shishi_aprep_from_file:
211 * @handle: shishi handle as allocated by shishi_init().
212 * @aprep: output variable with newly allocated AP-REP.
213 * @filetype: input variable specifying type of file to be read,
214 * see Shishi_filetype.
215 * @filename: input variable with filename to read from.
217 * Read AP-REP from file in specified TYPE.
219 * Return value: Returns SHISHI_OK iff successful.
222 shishi_aprep_from_file (Shishi
* handle
, Shishi_asn1
* aprep
,
223 int filetype
, const char *filename
)
228 if (VERBOSE (handle
))
229 printf (_("Reading AP-REP from %s...\n"), filename
);
231 fh
= fopen (filename
, "r");
233 return SHISHI_FOPEN_ERROR
;
235 if (VERBOSE (handle
))
236 printf (_("Reading AP-REP in %s format...\n"),
237 filetype
== SHISHI_FILETYPE_TEXT
? "TEXT" : "DER");
239 if (filetype
== SHISHI_FILETYPE_TEXT
)
240 res
= shishi_aprep_parse (handle
, fh
, aprep
);
242 res
= shishi_aprep_read (handle
, fh
, aprep
);
243 if (res
!= SHISHI_OK
)
248 return SHISHI_IO_ERROR
;
250 if (VERBOSE (handle
))
251 printf (_("Reading AP-REP from %s...done\n"), filename
);
257 shishi_aprep_enc_part_set (Shishi
* handle
,
259 int etype
, const char *buf
, size_t buflen
)
263 res
= shishi_asn1_write (handle
, aprep
, "enc-part.cipher", buf
, buflen
);
264 if (res
!= SHISHI_OK
)
267 res
= shishi_asn1_write_integer (handle
, aprep
, "enc-part.etype", etype
);
268 if (res
!= SHISHI_OK
)
275 shishi_aprep_enc_part_add (Shishi
* handle
,
277 Shishi_asn1 encticketpart
,
278 Shishi_asn1 encapreppart
)
287 res
= shishi_encticketpart_get_key (handle
, encticketpart
, &key
);
288 if (res
!= SHISHI_OK
)
291 res
= shishi_asn1_to_der (handle
, encapreppart
, &der
, &derlen
);
292 if (res
!= SHISHI_OK
)
294 shishi_error_printf (handle
, "Could not DER encode authenticator: %s\n",
295 shishi_strerror (res
));
299 der
= xrealloc (der
, derlen
+ 8);
301 while ((derlen
% 8) != 0)
307 res
= shishi_encrypt (handle
, key
, SHISHI_KEYUSAGE_ENCAPREPPART
,
308 der
, derlen
, &buf
, &buflen
);
312 if (res
!= SHISHI_OK
)
314 shishi_error_printf (handle
, "APRep encryption failed\n");
318 res
= shishi_aprep_enc_part_set (handle
, aprep
, shishi_key_type (key
),
327 shishi_aprep_enc_part_make (Shishi
* handle
,
329 Shishi_asn1 encapreppart
,
330 Shishi_asn1 authenticator
,
331 Shishi_asn1 encticketpart
)
335 res
= shishi_encapreppart_time_copy (handle
, encapreppart
, authenticator
);
336 if (res
!= SHISHI_OK
)
338 shishi_error_printf (handle
, "Could not copy time: %s\n",
339 shishi_error (handle
));
343 res
= shishi_aprep_enc_part_add (handle
, aprep
, encticketpart
,
345 if (res
!= SHISHI_OK
)
347 shishi_error_printf (handle
, "Could not add encapreppart: %s\n",
348 shishi_error (handle
));
356 * shishi_aprep_get_enc_part_etype:
357 * @handle: shishi handle as allocated by shishi_init().
358 * @aprep: AP-REP variable to get value from.
359 * @etype: output variable that holds the value.
361 * Extract AP-REP.enc-part.etype.
363 * Return value: Returns SHISHI_OK iff successful.
366 shishi_aprep_get_enc_part_etype (Shishi
* handle
,
367 Shishi_asn1 aprep
, int32_t * etype
)
369 return shishi_asn1_read_int32 (handle
, aprep
, "enc-part.etype", etype
);
373 shishi_aprep_decrypt (Shishi
* handle
,
376 int keyusage
, Shishi_asn1
* encapreppart
)
386 res
= shishi_aprep_get_enc_part_etype (handle
, aprep
, &etype
);
387 if (res
!= SHISHI_OK
)
390 if (etype
!= shishi_key_type (key
))
391 return SHISHI_APREP_BAD_KEYTYPE
;
393 res
= shishi_asn1_read (handle
, aprep
, "enc-part.cipher",
394 &cipher
, &cipherlen
);
395 if (res
!= SHISHI_OK
)
398 res
= shishi_decrypt (handle
, key
, keyusage
, cipher
, cipherlen
,
401 if (res
!= SHISHI_OK
)
403 shishi_error_printf (handle
,
404 "APRep decryption failed, wrong password?\n");
408 /* The crypto is so 1980; no length indicator. Trim off pad bytes
409 until we can parse it. */
410 for (i
= 0; i
< 8; i
++)
412 if (VERBOSEASN1 (handle
))
413 printf ("Trying with %d pad in enckdcrep...\n", i
);
415 *encapreppart
= shishi_der2asn1_encapreppart (handle
, &buf
[0],
417 if (*encapreppart
!= NULL
)
421 if (*encapreppart
== NULL
)
423 shishi_error_printf (handle
, "Could not DER decode EncAPRepPart. "
424 "Password probably correct (decrypt ok) though\n");
425 return SHISHI_ASN1_ERROR
;
432 shishi_aprep_verify (Shishi
* handle
,
433 Shishi_asn1 authenticator
, Shishi_asn1 encapreppart
)
435 char *authenticatorctime
;
436 char *encapreppartctime
;
437 uint32_t authenticatorcusec
, encapreppartcusec
;
442 3.2.5. Receipt of KRB_AP_REP message
444 If a KRB_AP_REP message is returned, the client uses the session key from
445 the credentials obtained for the server[3.10] to decrypt the message, and
446 verifies that the timestamp and microsecond fields match those in the
447 Authenticator it sent to the server. If they match, then the client is
448 assured that the server is genuine. The sequence number and subkey (if
449 present) are retained for later use.
453 res
= shishi_authenticator_ctime (handle
, authenticator
,
454 &authenticatorctime
);
455 if (res
!= SHISHI_OK
)
458 res
= shishi_authenticator_cusec_get (handle
, authenticator
,
459 &authenticatorcusec
);
460 if (res
!= SHISHI_OK
)
463 res
= shishi_encapreppart_ctime (handle
, encapreppart
, &encapreppartctime
);
464 if (res
!= SHISHI_OK
)
467 res
= shishi_encapreppart_cusec_get (handle
, encapreppart
,
469 if (res
!= SHISHI_OK
)
472 if (VERBOSE (handle
))
474 printf ("authenticator cusec %08x ctime %s\n", authenticatorcusec
,
476 printf ("encapreppart cusec %08x ctime %s\n", encapreppartcusec
,
480 different
= authenticatorcusec
!= encapreppartcusec
||
481 strcmp (authenticatorctime
, encapreppartctime
) != 0;
483 free (authenticatorctime
);
484 free (encapreppartctime
);
487 return SHISHI_APREP_VERIFY_FAILED
;