1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University 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. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 31 * SUCH DAMAGE. 32 * 33 * $FreeBSD$ 34 */ 35 36 #ifndef lint 37 static const char sccsid[] = "@(#)kerberos.c 8.3 (Berkeley) 5/30/95"; 38 #endif /* not lint */ 39 40 /* 41 * Copyright (C) 1990 by the Massachusetts Institute of Technology 42 * 43 * Export of this software from the United States of America is assumed 44 * to require a specific license from the United States Government. 45 * It is the responsibility of any person or organization contemplating 46 * export to obtain such a license before exporting. 47 * 48 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 49 * distribute this software and its documentation for any purpose and 50 * without fee is hereby granted, provided that the above copyright 51 * notice appear in all copies and that both that copyright notice and 52 * this permission notice appear in supporting documentation, and that 53 * the name of M.I.T. not be used in advertising or publicity pertaining 54 * to distribution of the software without specific, written prior 55 * permission. M.I.T. makes no representations about the suitability of 56 * this software for any purpose. It is provided "as is" without express 57 * or implied warranty. 58 */ 59 60 #ifdef KRB4 61 #include <sys/types.h> 62 #include <arpa/telnet.h> 63 #include <stdio.h> 64 #include <openssl/des.h> /* BSD wont include this in krb.h, so we do it here */ 65 #include <krb.h> 66 #ifdef __STDC__ 67 #include <stdlib.h> 68 #endif 69 #ifdef NO_STRING_H 70 #include <strings.h> 71 #else 72 #include <string.h> 73 #endif 74 75 #include "encrypt.h" 76 #include "auth.h" 77 #include "misc.h" 78 79 int kerberos4_cksum P((unsigned char *, int)); 80 int kuserok P((AUTH_DAT *, char *)); 81 82 extern auth_debug_mode; 83 84 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0, 85 AUTHTYPE_KERBEROS_V4, }; 86 87 #define KRB_AUTH 0 /* Authentication data follows */ 88 #define KRB_REJECT 1 /* Rejected (reason might follow) */ 89 #define KRB_ACCEPT 2 /* Accepted */ 90 #define KRB_CHALLENGE 3 /* Challenge for mutual auth. */ 91 #define KRB_RESPONSE 4 /* Response for mutual auth. */ 92 93 #define KRB_SERVICE_NAME "rcmd" 94 95 static KTEXT_ST auth; 96 static char name[ANAME_SZ]; 97 static AUTH_DAT adat = { 0 }; 98 #ifdef ENCRYPTION 99 static Block session_key = { 0 }; 100 static des_key_schedule sched; 101 static Block challenge = { 0 }; 102 #endif /* ENCRYPTION */ 103 104 static int 105 Data(ap, type, d, c) 106 Authenticator *ap; 107 int type; 108 void *d; 109 int c; 110 { 111 unsigned char *p = str_data + 4; 112 unsigned char *cd = (unsigned char *)d; 113 114 if (c == -1) 115 c = strlen((char *)cd); 116 117 if (auth_debug_mode) { 118 printf("%s:%d: [%d] (%d)", 119 str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY", 120 str_data[3], 121 type, c); 122 printd(d, c); 123 printf("\r\n"); 124 } 125 *p++ = ap->type; 126 *p++ = ap->way; 127 *p++ = type; 128 while (c-- > 0) { 129 if ((*p++ = *cd++) == IAC) 130 *p++ = IAC; 131 } 132 *p++ = IAC; 133 *p++ = SE; 134 if (str_data[3] == TELQUAL_IS) 135 printsub('>', &str_data[2], p - (&str_data[2])); 136 return(net_write(str_data, p - str_data)); 137 } 138 139 int 140 kerberos4_init(ap, server) 141 Authenticator *ap; 142 int server; 143 { 144 FILE *fp; 145 146 if (server) { 147 str_data[3] = TELQUAL_REPLY; 148 if ((fp = fopen(KEYFILE, "r")) == NULL) 149 return(0); 150 fclose(fp); 151 } else { 152 str_data[3] = TELQUAL_IS; 153 } 154 return(1); 155 } 156 157 char dst_realm_buf[REALM_SZ], *dest_realm = NULL; 158 int dst_realm_sz = REALM_SZ; 159 160 int 161 kerberos4_send(ap) 162 Authenticator *ap; 163 { 164 KTEXT_ST auth; 165 char instance[INST_SZ]; 166 char *realm; 167 char *krb_realmofhost(); 168 char *krb_get_phost(); 169 CREDENTIALS cred; 170 int r; 171 172 printf("[ Trying KERBEROS4 ... ]\n"); 173 if (!UserNameRequested) { 174 if (auth_debug_mode) { 175 printf("Kerberos V4: no user name supplied\r\n"); 176 } 177 return(0); 178 } 179 180 memset(instance, 0, sizeof(instance)); 181 182 if ((realm = krb_get_phost(RemoteHostName))) 183 strncpy(instance, realm, sizeof(instance)); 184 185 instance[sizeof(instance)-1] = '\0'; 186 187 realm = dest_realm ? dest_realm : krb_realmofhost(RemoteHostName); 188 189 if (!realm) { 190 printf("Kerberos V4: no realm for %s\r\n", RemoteHostName); 191 return(0); 192 } 193 if ((r = krb_mk_req(&auth, KRB_SERVICE_NAME, instance, realm, 0L))) { 194 printf("mk_req failed: %s\r\n", krb_err_txt[r]); 195 return(0); 196 } 197 if ((r = krb_get_cred(KRB_SERVICE_NAME, instance, realm, &cred))) { 198 printf("get_cred failed: %s\r\n", krb_err_txt[r]); 199 return(0); 200 } 201 if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) { 202 if (auth_debug_mode) 203 printf("Not enough room for user name\r\n"); 204 return(0); 205 } 206 if (auth_debug_mode) 207 printf("Sent %d bytes of authentication data\r\n", auth.length); 208 if (!Data(ap, KRB_AUTH, (void *)auth.dat, auth.length)) { 209 if (auth_debug_mode) 210 printf("Not enough room for authentication data\r\n"); 211 return(0); 212 } 213 #ifdef ENCRYPTION 214 /* 215 * If we are doing mutual authentication, get set up to send 216 * the challenge, and verify it when the response comes back. 217 */ 218 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) { 219 register int i; 220 221 des_key_sched(&cred.session, sched); 222 des_init_random_number_generator(&cred.session); 223 des_new_random_key(&session_key); 224 des_ecb_encrypt(&session_key, &session_key, sched, 0); 225 des_ecb_encrypt(&session_key, &challenge, sched, 0); 226 /* 227 * Increment the challenge by 1, and encrypt it for 228 * later comparison. 229 */ 230 for (i = 7; i >= 0; --i) { 231 register int x; 232 x = (unsigned int)challenge[i] + 1; 233 challenge[i] = x; /* ignore overflow */ 234 if (x < 256) /* if no overflow, all done */ 235 break; 236 } 237 des_ecb_encrypt(&challenge, &challenge, sched, 1); 238 } 239 #endif /* ENCRYPTION */ 240 241 if (auth_debug_mode) { 242 printf("CK: %d:", kerberos4_cksum(auth.dat, auth.length)); 243 printd(auth.dat, auth.length); 244 printf("\r\n"); 245 printf("Sent Kerberos V4 credentials to server\r\n"); 246 } 247 return(1); 248 } 249 250 void 251 kerberos4_is(ap, data, cnt) 252 Authenticator *ap; 253 unsigned char *data; 254 int cnt; 255 { 256 #ifdef ENCRYPTION 257 Session_Key skey; 258 Block datablock; 259 #endif /* ENCRYPTION */ 260 char realm[REALM_SZ]; 261 char instance[INST_SZ]; 262 int r; 263 264 if (cnt-- < 1) 265 return; 266 switch (*data++) { 267 case KRB_AUTH: 268 if (krb_get_lrealm(realm, 1) != KSUCCESS) { 269 Data(ap, KRB_REJECT, (void *)"No local V4 Realm.", -1); 270 auth_finished(ap, AUTH_REJECT); 271 if (auth_debug_mode) 272 printf("No local realm\r\n"); 273 return; 274 } 275 memmove((void *)auth.dat, (void *)data, auth.length = cnt); 276 if (auth_debug_mode) { 277 printf("Got %d bytes of authentication data\r\n", cnt); 278 printf("CK: %d:", kerberos4_cksum(auth.dat, auth.length)); 279 printd(auth.dat, auth.length); 280 printf("\r\n"); 281 } 282 instance[0] = '*'; instance[1] = 0; 283 if ((r = krb_rd_req(&auth, KRB_SERVICE_NAME, 284 instance, 0, &adat, ""))) { 285 if (auth_debug_mode) 286 printf("Kerberos failed him as %s\r\n", name); 287 Data(ap, KRB_REJECT, (void *)krb_err_txt[r], -1); 288 auth_finished(ap, AUTH_REJECT); 289 return; 290 } 291 #ifdef ENCRYPTION 292 memmove((void *)session_key, (void *)adat.session, sizeof(Block)); 293 #endif /* ENCRYPTION */ 294 krb_kntoln(&adat, name); 295 296 if (UserNameRequested && !kuserok(&adat, UserNameRequested)) 297 Data(ap, KRB_ACCEPT, (void *)0, 0); 298 else 299 Data(ap, KRB_REJECT, 300 (void *)"user is not authorized", -1); 301 auth_finished(ap, AUTH_USER); 302 break; 303 304 case KRB_CHALLENGE: 305 #ifndef ENCRYPTION 306 Data(ap, KRB_RESPONSE, (void *)0, 0); 307 #else /* ENCRYPTION */ 308 if (!VALIDKEY(session_key)) { 309 /* 310 * We don't have a valid session key, so just 311 * send back a response with an empty session 312 * key. 313 */ 314 Data(ap, KRB_RESPONSE, (void *)0, 0); 315 break; 316 } 317 318 /* 319 * Initialize the random number generator since it's 320 * used later on by the encryption routine. 321 */ 322 des_init_random_number_generator(&session_key); 323 des_key_sched(&session_key, sched); 324 memmove((void *)datablock, (void *)data, sizeof(Block)); 325 /* 326 * Take the received encrypted challenge, and encrypt 327 * it again to get a unique session_key for the 328 * ENCRYPT option. 329 */ 330 des_ecb_encrypt(&datablock, &session_key, sched, 1); 331 skey.type = SK_DES; 332 skey.length = 8; 333 skey.data = session_key; 334 encrypt_session_key(&skey, 1); 335 /* 336 * Now decrypt the received encrypted challenge, 337 * increment by one, re-encrypt it and send it back. 338 */ 339 des_ecb_encrypt(&datablock, &challenge, sched, 0); 340 for (r = 7; r >= 0; r--) { 341 register int t; 342 t = (unsigned int)challenge[r] + 1; 343 challenge[r] = t; /* ignore overflow */ 344 if (t < 256) /* if no overflow, all done */ 345 break; 346 } 347 des_ecb_encrypt(&challenge, &challenge, sched, 1); 348 Data(ap, KRB_RESPONSE, (void *)challenge, sizeof(challenge)); 349 #endif /* ENCRYPTION */ 350 break; 351 352 default: 353 if (auth_debug_mode) 354 printf("Unknown Kerberos option %d\r\n", data[-1]); 355 Data(ap, KRB_REJECT, 0, 0); 356 break; 357 } 358 } 359 360 void 361 kerberos4_reply(ap, data, cnt) 362 Authenticator *ap; 363 unsigned char *data; 364 int cnt; 365 { 366 #ifdef ENCRYPTION 367 Session_Key skey; 368 #endif /* ENCRYPTION */ 369 370 if (cnt-- < 1) 371 return; 372 switch (*data++) { 373 case KRB_REJECT: 374 if (cnt > 0) { 375 printf("[ Kerberos V4 refuses authentication because %.*s ]\r\n", 376 cnt, data); 377 } else 378 printf("[ Kerberos V4 refuses authentication ]\r\n"); 379 auth_send_retry(); 380 return; 381 case KRB_ACCEPT: 382 printf("[ Kerberos V4 accepts you ]\n"); 383 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) { 384 /* 385 * Send over the encrypted challenge. 386 */ 387 #ifndef ENCRYPTION 388 Data(ap, KRB_CHALLENGE, (void *)0, 0); 389 #else /* ENCRYPTION */ 390 Data(ap, KRB_CHALLENGE, (void *)session_key, 391 sizeof(session_key)); 392 des_ecb_encrypt(&session_key, &session_key, sched, 1); 393 skey.type = SK_DES; 394 skey.length = 8; 395 skey.data = session_key; 396 encrypt_session_key(&skey, 0); 397 #endif /* ENCRYPTION */ 398 return; 399 } 400 auth_finished(ap, AUTH_USER); 401 return; 402 case KRB_RESPONSE: 403 #ifdef ENCRYPTION 404 /* 405 * Verify that the response to the challenge is correct. 406 */ 407 if ((cnt != sizeof(Block)) || 408 (0 != memcmp((void *)data, (void *)challenge, 409 sizeof(challenge)))) 410 { 411 #endif /* ENCRYPTION */ 412 printf("[ Kerberos V4 challenge failed!!! ]\r\n"); 413 auth_send_retry(); 414 return; 415 #ifdef ENCRYPTION 416 } 417 printf("[ Kerberos V4 challenge successful ]\r\n"); 418 auth_finished(ap, AUTH_USER); 419 #endif /* ENCRYPTION */ 420 break; 421 default: 422 if (auth_debug_mode) 423 printf("Unknown Kerberos option %d\r\n", data[-1]); 424 return; 425 } 426 } 427 428 int 429 kerberos4_status(ap, name, level) 430 Authenticator *ap; 431 char *name; 432 int level; 433 { 434 if (level < AUTH_USER) 435 return(level); 436 437 if (UserNameRequested && !kuserok(&adat, UserNameRequested)) { 438 strcpy(name, UserNameRequested); 439 return(AUTH_VALID); 440 } else 441 return(AUTH_USER); 442 } 443 444 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);} 445 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);} 446 447 void 448 kerberos4_printsub(data, cnt, buf, buflen) 449 unsigned char *data, *buf; 450 int cnt, buflen; 451 { 452 char lbuf[32]; 453 register int i; 454 455 buf[buflen-1] = '\0'; /* make sure its NULL terminated */ 456 buflen -= 1; 457 458 switch(data[3]) { 459 case KRB_REJECT: /* Rejected (reason might follow) */ 460 strncpy((char *)buf, " REJECT ", buflen); 461 goto common; 462 463 case KRB_ACCEPT: /* Accepted (name might follow) */ 464 strncpy((char *)buf, " ACCEPT ", buflen); 465 common: 466 BUMP(buf, buflen); 467 if (cnt <= 4) 468 break; 469 ADDC(buf, buflen, '"'); 470 for (i = 4; i < cnt; i++) 471 ADDC(buf, buflen, data[i]); 472 ADDC(buf, buflen, '"'); 473 ADDC(buf, buflen, '\0'); 474 break; 475 476 case KRB_AUTH: /* Authentication data follows */ 477 strncpy((char *)buf, " AUTH", buflen); 478 goto common2; 479 480 case KRB_CHALLENGE: 481 strncpy((char *)buf, " CHALLENGE", buflen); 482 goto common2; 483 484 case KRB_RESPONSE: 485 strncpy((char *)buf, " RESPONSE", buflen); 486 goto common2; 487 488 default: 489 sprintf(lbuf, " %d (unknown)", data[3]); 490 strncpy((char *)buf, lbuf, buflen); 491 common2: 492 BUMP(buf, buflen); 493 for (i = 4; i < cnt; i++) { 494 sprintf(lbuf, " %d", data[i]); 495 strncpy((char *)buf, lbuf, buflen); 496 BUMP(buf, buflen); 497 } 498 break; 499 } 500 } 501 502 int 503 kerberos4_cksum(d, n) 504 unsigned char *d; 505 int n; 506 { 507 int ck = 0; 508 509 /* 510 * A comment is probably needed here for those not 511 * well versed in the "C" language. Yes, this is 512 * supposed to be a "switch" with the body of the 513 * "switch" being a "while" statement. The whole 514 * purpose of the switch is to allow us to jump into 515 * the middle of the while() loop, and then not have 516 * to do any more switch()s. 517 * 518 * Some compilers will spit out a warning message 519 * about the loop not being entered at the top. 520 */ 521 switch (n&03) 522 while (n > 0) { 523 case 0: 524 ck ^= (int)*d++ << 24; 525 --n; 526 case 3: 527 ck ^= (int)*d++ << 16; 528 --n; 529 case 2: 530 ck ^= (int)*d++ << 8; 531 --n; 532 case 1: 533 ck ^= (int)*d++; 534 --n; 535 } 536 return(ck); 537 } 538 #endif 539 540 #ifdef notdef 541 542 prkey(msg, key) 543 char *msg; 544 unsigned char *key; 545 { 546 register int i; 547 printf("%s:", msg); 548 for (i = 0; i < 8; i++) 549 printf(" %3d", key[i]); 550 printf("\r\n"); 551 } 552 #endif 553