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