xref: /freebsd/contrib/telnet/libtelnet/rsaencpwd.c (revision 8fc257994d0ce2396196d7a06d50d20c8015f4b7)
1 /*-
2  * Copyright (c) 1992, 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 char sccsid[] = "@(#)rsaencpwd.c	8.3 (Berkeley) 5/30/95";
40 #endif /* not lint */
41 
42 
43 #ifdef	RSA_ENCPWD
44 /*
45  * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
46  * ALL RIGHTS RESERVED
47  *
48  * "Digital Equipment Corporation authorizes the reproduction,
49  * distribution and modification of this software subject to the following
50  * restrictions:
51  *
52  * 1.  Any partial or whole copy of this software, or any modification
53  * thereof, must include this copyright notice in its entirety.
54  *
55  * 2.  This software is supplied "as is" with no warranty of any kind,
56  * expressed or implied, for any purpose, including any warranty of fitness
57  * or merchantibility.  DIGITAL assumes no responsibility for the use or
58  * reliability of this software, nor promises to provide any form of
59  * support for it on any basis.
60  *
61  * 3.  Distribution of this software is authorized only if no profit or
62  * remuneration of any kind is received in exchange for such distribution.
63  *
64  * 4.  This software produces public key authentication certificates
65  * bearing an expiration date established by DIGITAL and RSA Data
66  * Security, Inc.  It may cease to generate certificates after the expiration
67  * date.  Any modification of this software that changes or defeats
68  * the expiration date or its effect is unauthorized.
69  *
70  * 5.  Software that will renew or extend the expiration date of
71  * authentication certificates produced by this software may be obtained
72  * from RSA Data Security, Inc., 10 Twin Dolphin Drive, Redwood City, CA
73  * 94065, (415)595-8782, or from DIGITAL"
74  *
75  */
76 
77 #include <sys/types.h>
78 #include <arpa/telnet.h>
79 #include <pwd.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83 
84 #include "encrypt.h"
85 #include "auth.h"
86 #include "misc.h"
87 #include "cdc.h"
88 
89 extern auth_debug_mode;
90 
91 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
92 			  		AUTHTYPE_RSA_ENCPWD, };
93 static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION,
94 					TELQUAL_NAME, };
95 
96 #define	RSA_ENCPWD_AUTH	0	/* Authentication data follows */
97 #define	RSA_ENCPWD_REJECT	1	/* Rejected (reason might follow) */
98 #define RSA_ENCPWD_ACCEPT	2	/* Accepted */
99 #define	RSA_ENCPWD_CHALLENGEKEY	3	/* Challenge and public key */
100 
101 #define NAME_SZ   40
102 #define CHAL_SZ   20
103 #define PWD_SZ    40
104 
105 static	KTEXT_ST auth;
106 static	char name[NAME_SZ];
107 static	char user_passwd[PWD_SZ];
108 static  char key_file[2*NAME_SZ];
109 static  char lhostname[NAME_SZ];
110 static char  challenge[CHAL_SZ];
111 static int   challenge_len;
112 
113 	static int
114 Data(ap, type, d, c)
115 	Authenticator *ap;
116 	int type;
117 	void *d;
118 	int c;
119 {
120 	unsigned char *p = str_data + 4;
121 	unsigned char *cd = (unsigned char *)d;
122 
123 	if (c == -1)
124 		c = strlen((char *)cd);
125 
126 	if (0) {
127 		printf("%s:%d: [%d] (%d)",
128 			str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
129 			str_data[3],
130 			type, c);
131 		printd(d, c);
132 		printf("\r\n");
133 	}
134 	*p++ = ap->type;
135 	*p++ = ap->way;
136 	if (type != NULL) *p++ = type;
137 	while (c-- > 0) {
138 		if ((*p++ = *cd++) == IAC)
139 			*p++ = IAC;
140 	}
141 	*p++ = IAC;
142 	*p++ = SE;
143 	if (str_data[3] == TELQUAL_IS)
144 		printsub('>', &str_data[2], p - (&str_data[2]));
145 	return(net_write(str_data, p - str_data));
146 }
147 
148 	int
149 rsaencpwd_init(ap, server)
150 	Authenticator *ap;
151 	int server;
152 {
153 	char  *cp;
154 	FILE  *fp;
155 
156 	if (server) {
157 		str_data[3] = TELQUAL_REPLY;
158 		memset(key_file, 0, sizeof(key_file));
159 		gethostname(lhostname, sizeof(lhostname));
160 		if ((cp = strchr(lhostname, '.')) != 0)  *cp = '\0';
161 		strcpy(key_file, "/etc/.");
162 		strcat(key_file, lhostname);
163 		strcat(key_file, "_privkey");
164 		if ((fp=fopen(key_file, "r"))==NULL) return(0);
165 		fclose(fp);
166 	} else {
167 		str_data[3] = TELQUAL_IS;
168 	}
169 	return(1);
170 }
171 
172 	int
173 rsaencpwd_send(ap)
174 	Authenticator *ap;
175 {
176 
177 	printf("[ Trying RSAENCPWD ... ]\n");
178 	if (!UserNameRequested) {
179 		return(0);
180 	}
181 	if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {
182 		return(0);
183 	}
184 	if (!Data(ap, NULL, (void *)NULL, 0)) {
185 		return(0);
186 	}
187 
188 
189 	return(1);
190 }
191 
192 	void
193 rsaencpwd_is(ap, data, cnt)
194 	Authenticator *ap;
195 	unsigned char *data;
196 	int cnt;
197 {
198 	Session_Key skey;
199 	Block datablock;
200 	char  r_passwd[PWD_SZ], r_user[NAME_SZ];
201 	char  *cp, key[160];
202 	char  chalkey[160], *ptr;
203 	FILE  *fp;
204 	int r, i, j, chalkey_len, len;
205 	time_t now;
206 
207 	cnt--;
208 	switch (*data++) {
209 	case RSA_ENCPWD_AUTH:
210 		memmove((void *)auth.dat, (void *)data, auth.length = cnt);
211 
212 		if ((fp=fopen(key_file, "r"))==NULL) {
213 		  Data(ap, RSA_ENCPWD_REJECT, (void *)"Auth failed", -1);
214 		  auth_finished(ap, AUTH_REJECT);
215 		  return;
216 		}
217 		/*
218 		 *  get privkey
219 		 */
220 		fscanf(fp, "%x;", &len);
221 		for (i=0;i<len;i++) {
222 		  j = getc(fp);  key[i]=j;
223 		}
224 		fclose(fp);
225 
226 		r = accept_rsa_encpwd(&auth, key, challenge,
227 				      challenge_len, r_passwd);
228 		if (r < 0) {
229 		  Data(ap, RSA_ENCPWD_REJECT, (void *)"Auth failed", -1);
230 		  auth_finished(ap, AUTH_REJECT);
231 		  return;
232 		}
233 		auth_encrypt_userpwd(r_passwd);
234 		if (rsaencpwd_passwdok(UserNameRequested, UserPassword) == 0) {
235 		  /*
236 		   *  illegal username and password
237 		   */
238 		  Data(ap, RSA_ENCPWD_REJECT, (void *)"Illegal password", -1);
239 		  auth_finished(ap, AUTH_REJECT);
240 		  return;
241 		}
242 
243 		Data(ap, RSA_ENCPWD_ACCEPT, (void *)0, 0);
244 		auth_finished(ap, AUTH_USER);
245 		break;
246 
247 
248 	case IAC:
249 
250 		/*
251 		 * If we are doing mutual authentication, get set up to send
252 		 * the challenge, and verify it when the response comes back.
253 		 */
254 		if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY) {
255 		  register int i;
256 
257 
258 		  time(&now);
259 		  if ((now % 2) == 0) {
260 		    sprintf(challenge, "%x", now);
261 		    challenge_len = strlen(challenge);
262 		  } else {
263 		    strcpy(challenge, "randchal");
264 		    challenge_len = 8;
265 		  }
266 
267 		  if ((fp=fopen(key_file, "r"))==NULL) {
268 		    Data(ap, RSA_ENCPWD_REJECT, (void *)"Auth failed", -1);
269 		    auth_finished(ap, AUTH_REJECT);
270 		    return;
271 		  }
272 		  /*
273 		   *  skip privkey
274 		   */
275 		  fscanf(fp, "%x;", &len);
276 		  for (i=0;i<len;i++) {
277 		    j = getc(fp);
278 		  }
279 		  /*
280 		   * get pubkey
281 		   */
282 		  fscanf(fp, "%x;", &len);
283 		  for (i=0;i<len;i++) {
284 		    j = getc(fp);  key[i]=j;
285 		  }
286 		  fclose(fp);
287 		  chalkey[0] = 0x30;
288 		  ptr = (char *) &chalkey[1];
289 		  chalkey_len = 1+NumEncodeLengthOctets(i)+i+1+NumEncodeLengthOctets(challenge_len)+challenge_len;
290 		  EncodeLength(ptr, chalkey_len);
291 		  ptr +=NumEncodeLengthOctets(chalkey_len);
292 		  *ptr++ = 0x04;  /* OCTET STRING */
293 		  *ptr++ = challenge_len;
294 		  memmove(ptr, challenge, challenge_len);
295 		  ptr += challenge_len;
296 		  *ptr++ = 0x04;  /* OCTET STRING */
297 		  EncodeLength(ptr, i);
298 		  ptr += NumEncodeLengthOctets(i);
299 		  memmove(ptr, key, i);
300 		  chalkey_len = 1+NumEncodeLengthOctets(chalkey_len)+chalkey_len;
301 		  Data(ap, RSA_ENCPWD_CHALLENGEKEY, (void *)chalkey, chalkey_len);
302 		}
303 		break;
304 
305 	default:
306 		Data(ap, RSA_ENCPWD_REJECT, 0, 0);
307 		break;
308 	}
309 }
310 
311 
312 	void
313 rsaencpwd_reply(ap, data, cnt)
314 	Authenticator *ap;
315 	unsigned char *data;
316 	int cnt;
317 {
318 	Session_Key skey;
319 	KTEXT_ST token;
320 	Block enckey;
321 	int r, pubkey_len;
322 	char	randchal[CHAL_SZ], *cp;
323 	char	chalkey[160], pubkey[128], *ptr;
324 
325 	if (cnt-- < 1)
326 		return;
327 	switch (*data++) {
328 	case RSA_ENCPWD_REJECT:
329 		if (cnt > 0) {
330 			printf("[ RSA_ENCPWD refuses authentication because %.*s ]\r\n",
331 				cnt, data);
332 		} else
333 			printf("[ RSA_ENCPWD refuses authentication ]\r\n");
334 		auth_send_retry();
335 		return;
336 	case RSA_ENCPWD_ACCEPT:
337 		printf("[ RSA_ENCPWD accepts you ]\n");
338 		auth_finished(ap, AUTH_USER);
339 		return;
340 	case RSA_ENCPWD_CHALLENGEKEY:
341 		/*
342 		 * Verify that the response to the challenge is correct.
343 		 */
344 
345 		memmove((void *)chalkey, (void *)data, cnt);
346 		ptr = (char *) &chalkey[0];
347 		ptr += DecodeHeaderLength(chalkey);
348 		if (*ptr != 0x04) {
349 		  return;
350 		}
351 		*ptr++;
352 		challenge_len = DecodeValueLength(ptr);
353 		ptr += NumEncodeLengthOctets(challenge_len);
354 		memmove(challenge, ptr, challenge_len);
355 		ptr += challenge_len;
356 		if (*ptr != 0x04) {
357 		  return;
358 		}
359 		*ptr++;
360 		pubkey_len = DecodeValueLength(ptr);
361 		ptr += NumEncodeLengthOctets(pubkey_len);
362 		memmove(pubkey, ptr, pubkey_len);
363 		memset(user_passwd, 0, sizeof(user_passwd));
364 		local_des_read_pw_string(user_passwd, sizeof(user_passwd)-1, "Password: ", 0);
365 		UserPassword = user_passwd;
366 		Challenge = challenge;
367 		r = init_rsa_encpwd(&token, user_passwd, challenge, challenge_len, pubkey);
368 		if (r < 0) {
369 		  token.length = 1;
370 		}
371 
372 		if (!Data(ap, RSA_ENCPWD_AUTH, (void *)token.dat, token.length)) {
373 		  return;
374 		}
375 
376 		break;
377 
378 	default:
379 		return;
380 	}
381 }
382 
383 	int
384 rsaencpwd_status(ap, name, level)
385 	Authenticator *ap;
386 	char *name;
387 	int level;
388 {
389 
390 	if (level < AUTH_USER)
391 		return(level);
392 
393 	if (UserNameRequested && rsaencpwd_passwdok(UserNameRequested, UserPassword)) {
394 		strcpy(name, UserNameRequested);
395 		return(AUTH_VALID);
396 	} else {
397 		return(AUTH_USER);
398 	}
399 }
400 
401 #define	BUMP(buf, len)		while (*(buf)) {++(buf), --(len);}
402 #define	ADDC(buf, len, c)	if ((len) > 0) {*(buf)++ = (c); --(len);}
403 
404 	void
405 rsaencpwd_printsub(data, cnt, buf, buflen)
406 	unsigned char *data, *buf;
407 	int cnt, buflen;
408 {
409 	char lbuf[32];
410 	register int i;
411 
412 	buf[buflen-1] = '\0';		/* make sure its NULL terminated */
413 	buflen -= 1;
414 
415 	switch(data[3]) {
416 	case RSA_ENCPWD_REJECT:	/* Rejected (reason might follow) */
417 		strncpy((char *)buf, " REJECT ", buflen);
418 		goto common;
419 
420 	case RSA_ENCPWD_ACCEPT:	/* Accepted (name might follow) */
421 		strncpy((char *)buf, " ACCEPT ", buflen);
422 	common:
423 		BUMP(buf, buflen);
424 		if (cnt <= 4)
425 			break;
426 		ADDC(buf, buflen, '"');
427 		for (i = 4; i < cnt; i++)
428 			ADDC(buf, buflen, data[i]);
429 		ADDC(buf, buflen, '"');
430 		ADDC(buf, buflen, '\0');
431 		break;
432 
433 	case RSA_ENCPWD_AUTH:		/* Authentication data follows */
434 		strncpy((char *)buf, " AUTH", buflen);
435 		goto common2;
436 
437 	case RSA_ENCPWD_CHALLENGEKEY:
438 		strncpy((char *)buf, " CHALLENGEKEY", buflen);
439 		goto common2;
440 
441 	default:
442 		sprintf(lbuf, " %d (unknown)", data[3]);
443 		strncpy((char *)buf, lbuf, buflen);
444 	common2:
445 		BUMP(buf, buflen);
446 		for (i = 4; i < cnt; i++) {
447 			sprintf(lbuf, " %d", data[i]);
448 			strncpy((char *)buf, lbuf, buflen);
449 			BUMP(buf, buflen);
450 		}
451 		break;
452 	}
453 }
454 
455 int rsaencpwd_passwdok(name, passwd)
456 char *name, *passwd;
457 {
458   char *crypt();
459   char *salt, *p;
460   struct passwd *pwd;
461   int   passwdok_status = 0;
462 
463   if (pwd = getpwnam(name))
464     salt = pwd->pw_passwd;
465   else salt = "xx";
466 
467   p = crypt(passwd, salt);
468 
469   if (pwd && !strcmp(p, pwd->pw_passwd)) {
470     passwdok_status = 1;
471   } else passwdok_status = 0;
472   return(passwdok_status);
473 }
474 
475 #endif
476