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