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