2 * Copyright (c) 1995, 1996, 1997, 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright 1992, 2002 by the Massachusetts Institute of Technology.
35 * All Rights Reserved.
37 * Export of this software from the United States of America may
38 * require a specific license from the United States Government.
39 * It is the responsibility of any person or organization contemplating
40 * export to obtain such a license before exporting.
42 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
43 * distribute this software and its documentation for any purpose and
44 * without fee is hereby granted, provided that the above copyright
45 * notice appear in all copies and that both that copyright notice and
46 * this permission notice appear in supporting documentation, and that
47 * the name of M.I.T. not be used in advertising or publicity pertaining
48 * to distribution of the software without specific, written prior
49 * permission. Furthermore if you modify this software you must label
50 * your software as modified software and not distribute it in such a
51 * fashion that it might be confused with the original M.I.T. software.
52 * M.I.T. makes no representations about the suitability of
53 * this software for any purpose. It is provided "as is" without express
54 * or implied warranty.
57 #include <afsconfig.h>
59 #include "../afs/param.h"
61 #include <afs/param.h>
66 #include "../afs/sysincludes.h"
67 #include "../afs/afsincludes.h"
68 #include "../afs/stds.h"
69 #include "../rx/xdr.h"
71 #include "../afs/lifetimes.h"
72 #include "../afs/rxkad.h"
73 #else /* defined(UKERNEL) */
75 #include <sys/types.h>
79 #include <netinet/in.h>
84 #include "lifetimes.h"
86 #endif /* defined(UKERNEL) */
88 #include "v5gen-rewrite.h"
89 #include "asn1-common.h"
98 * Principal conversion Taken from src/lib/krb5/krb/conv_princ from MIT Kerberos. If you
99 * find a need to change the services here, please consider opening a
100 * bug with MIT by sending mail to krb5-bugs@mit.edu.
103 extern afs_int32 des_cbc_encrypt(void * in, void * out,
104 register afs_int32 length,
105 des_key_schedule key, des_cblock *iv,
107 extern int des_key_sched(register des_cblock k, des_key_schedule schedule);
116 #define DO_REALM_CONVERSION 0x00000001
119 * Kadmin doesn't do realm conversion because it's currently
120 * kadmin/REALM.NAME. Zephyr doesn't because it's just zephyr/zephyr.
122 * "Realm conversion" is a bit of a misnomer; really, the v5 name is
123 * using a FQDN or something that looks like it, where the v4 name is
124 * just using the first label. Sometimes that second principal name
125 * component is a hostname, sometimes the realm name, sometimes it's
128 * This list should probably be more configurable, and more than
129 * likely on a per-realm basis, so locally-defined services can be
132 static const struct krb_convert sconv_list[] = {
133 /* Realm conversion, Change service name */
134 #define RC(V5NAME,V4NAME) { V5NAME, V4NAME, DO_REALM_CONVERSION, sizeof(V5NAME)-1 }
135 /* Realm conversion */
136 #define R(NAME) { NAME, NAME, DO_REALM_CONVERSION, sizeof(NAME)-1 }
137 /* No Realm conversion */
138 #define NR(NAME) { NAME, NAME, 0, sizeof(NAME)-1 }
189 krb5_des_decrypt(struct ktc_encryptionKey *, int, void *, size_t, void *,
196 tkt_DecodeTicket5(char *ticket, afs_int32 ticket_len,
197 int (*get_key) (void *, int, struct ktc_encryptionKey *),
198 char *get_key_rock, int serv_kvno, char *name, char *inst,
199 char *cell, struct ktc_encryptionKey *session_key, afs_int32 * host,
200 afs_int32 * start, afs_int32 * end, afs_int32 disableCheckdot)
202 char plain[MAXKRB5TICKETLEN];
203 struct ktc_encryptionKey serv_key;
204 Ticket t5; /* Must free */
205 EncTicketPart decr_part; /* Must free */
207 size_t siz, plainsiz;
209 char *v5_comp0, *v5_comp1, *c;
210 const struct krb_convert *p;
212 memset(&t5, 0, sizeof(t5));
213 memset(&decr_part, 0, sizeof(decr_part));
218 return RXKADBADTICKET; /* no ticket */
220 if (serv_kvno == RXKAD_TKT_TYPE_KERBEROS_V5) {
221 code = decode_Ticket(ticket, ticket_len, &t5, &siz);
228 code = decode_EncryptedData(ticket, ticket_len, &t5.enc_part, &siz);
233 /* If kvno is null, it's probably not included because it was kvno==0
235 if (t5.enc_part.kvno == NULL) {
238 v5_serv_kvno = *t5.enc_part.kvno;
241 /* Check that the key type really fit into 8 bytes */
242 switch (t5.enc_part.etype) {
243 case ETYPE_DES_CBC_CRC:
244 case ETYPE_DES_CBC_MD4:
245 case ETYPE_DES_CBC_MD5:
252 if (t5.enc_part.cipher.length > sizeof(plain)
253 || t5.enc_part.cipher.length % 8 != 0)
256 code = (*get_key) (get_key_rock, v5_serv_kvno, &serv_key);
260 /* Decrypt data here, save in plain, assume it will shrink */
262 krb5_des_decrypt(&serv_key, t5.enc_part.etype,
263 t5.enc_part.cipher.data, t5.enc_part.cipher.length,
269 code = decode_EncTicketPart(plain, plainsiz, &decr_part, &siz);
273 /* Extract realm and principal */
274 strncpy(cell, decr_part.crealm, MAXKTCNAMELEN);
275 cell[MAXKTCNAMELEN - 1] = '\0';
277 switch (decr_part.cname.name_string.len) {
279 v5_comp0 = decr_part.cname.name_string.val[0];
280 v5_comp1 = decr_part.cname.name_string.val[1];
283 if (strcmp(p->v5_str, v5_comp0) == 0) {
285 * It is, so set the new name now, and chop off
286 * instance's domain name if requested.
288 strncpy(name, p->v4_str, MAXKTCNAMELEN);
289 name[MAXKTCNAMELEN - 1] = '\0';
290 if (p->flags & DO_REALM_CONVERSION) {
291 c = strchr(v5_comp1, '.');
292 if (!c || (c - v5_comp1) >= MAXKTCNAMELEN - 1)
294 strncpy(inst, v5_comp1, c - v5_comp1);
295 inst[c - v5_comp1] = '\0';
303 strncpy(inst, decr_part.cname.name_string.val[1], MAXKTCNAMELEN);
304 inst[MAXKTCNAMELEN - 1] = '\0';
305 strncpy(name, decr_part.cname.name_string.val[0], MAXKTCNAMELEN);
306 name[MAXKTCNAMELEN - 1] = '\0';
310 strncpy(name, decr_part.cname.name_string.val[0], MAXKTCNAMELEN);
311 name[MAXKTCNAMELEN - 1] = '\0';
317 if (!disableCheckdot) {
319 * If the first part of the name_string contains a dot, punt since
320 * then we can't see the diffrence between the kerberos 5
321 * principals foo.root and foo/root later in the fileserver.
323 if (strchr(decr_part.cname.name_string.val[0], '.') != NULL)
327 /* Verify that decr_part.key is of right type */
328 switch (decr_part.key.keytype) {
329 case ETYPE_DES_CBC_CRC:
330 case ETYPE_DES_CBC_MD4:
331 case ETYPE_DES_CBC_MD5:
337 if (decr_part.key.keyvalue.length != 8)
340 /* Extract session key */
341 memcpy(session_key, decr_part.key.keyvalue.data, 8);
343 /* Check lifetimes and host addresses, flags etc */
345 time_t now = time(0); /* Use fast time package instead??? */
346 *start = decr_part.authtime;
347 if (decr_part.starttime)
348 *start = *decr_part.starttime;
350 if (*start - now > CLOCK_SKEW || decr_part.flags.invalid)
353 if (decr_part.flags.invalid)
356 if (now > decr_part.endtime)
358 *end = decr_part.endtime;
362 if (serv_kvno == RXKAD_TKT_TYPE_KERBEROS_V5)
365 free_EncryptedData(&t5.enc_part);
366 free_EncTicketPart(&decr_part);
367 memset(&serv_key, 0, sizeof(serv_key));
371 code = RXKADUNKNOWNKEY;
380 code = RXKADBADTICKET;
386 verify_checksum_md4(void *data, size_t len,
387 void *cksum, size_t cksumsz,
388 struct ktc_encryptionKey *key)
391 unsigned char tmp[16];
394 MD4_Update(&md4, data, len);
395 MD4_Final(tmp, &md4);
397 if (memcmp(tmp, cksum, cksumsz) != 0)
403 verify_checksum_md5(void *data, size_t len,
404 void *cksum, size_t cksumsz,
405 struct ktc_encryptionKey *key)
408 unsigned char tmp[16];
411 MD5_Update(&md5, data, len);
412 MD5_Final(tmp, &md5);
414 if (memcmp(tmp, cksum, cksumsz) != 0)
420 verify_checksum_crc(void *data, size_t len, void *cksum, size_t cksumsz,
421 struct ktc_encryptionKey *key)
426 _rxkad_crc_init_table();
427 crc = _rxkad_crc_update(data, len, 0);
429 r[1] = (crc >> 8) & 0xff;
430 r[2] = (crc >> 16) & 0xff;
431 r[3] = (crc >> 24) & 0xff;
433 if (memcmp(cksum, r, 4) != 0)
440 krb5_des_decrypt(struct ktc_encryptionKey *key, int etype, void *in,
441 size_t insz, void *out, size_t * outsz)
443 int (*cksum_func) (void *, size_t, void *, size_t,
444 struct ktc_encryptionKey *);
449 int ret = 1; /* failure */
453 des_key_sched(ktc_to_cblock(key), (struct des_ks_struct *)&s);
455 #define CONFOUNDERSZ 8
458 case ETYPE_DES_CBC_CRC:
459 memcpy(&ivec, key, sizeof(ivec));
461 cksum_func = verify_checksum_crc;
463 case ETYPE_DES_CBC_MD4:
464 memset(&ivec, 0, sizeof(ivec));
466 cksum_func = verify_checksum_md4;
468 case ETYPE_DES_CBC_MD5:
469 memset(&ivec, 0, sizeof(ivec));
471 cksum_func = verify_checksum_md5;
477 des_cbc_encrypt(in, out, insz, s, &ivec, 0);
479 memcpy(cksum, (char *)out + CONFOUNDERSZ, cksumsz);
480 memset((char *)out + CONFOUNDERSZ, 0, cksumsz);
483 ret = (*cksum_func) (out, insz, cksum, cksumsz, key);
485 *outsz = insz - CONFOUNDERSZ - cksumsz;
486 memmove(out, (char *)out + CONFOUNDERSZ + cksumsz, *outsz);