xref: /freebsd/contrib/telnet/libtelnet/rsaencpwd.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
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 #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 
78 #ifdef	__STDC__
79 #include <stdlib.h>
80 #endif
81 #ifdef	NO_STRING_H
82 #include <strings.h>
83 #else
84 #include <string.h>
85 #endif
86 
87 #include "encrypt.h"
88 #include "auth.h"
89 #include "misc.h"
90 #include "cdc.h"
91 
92 extern auth_debug_mode;
93 
94 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
95 			  		AUTHTYPE_RSA_ENCPWD, };
96 static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION,
97 					TELQUAL_NAME, };
98 
99 #define	RSA_ENCPWD_AUTH	0	/* Authentication data follows */
100 #define	RSA_ENCPWD_REJECT	1	/* Rejected (reason might follow) */
101 #define RSA_ENCPWD_ACCEPT	2	/* Accepted */
102 #define	RSA_ENCPWD_CHALLENGEKEY	3	/* Challenge and public key */
103 
104 #define NAME_SZ   40
105 #define CHAL_SZ   20
106 #define PWD_SZ    40
107 
108 static	KTEXT_ST auth;
109 static	char name[NAME_SZ];
110 static	char user_passwd[PWD_SZ];
111 static  char key_file[2*NAME_SZ];
112 static  char lhostname[NAME_SZ];
113 static char  challenge[CHAL_SZ];
114 static int   challenge_len;
115 
116 	static int
117 Data(ap, type, d, c)
118 	Authenticator *ap;
119 	int type;
120 	void *d;
121 	int c;
122 {
123 	unsigned char *p = str_data + 4;
124 	unsigned char *cd = (unsigned char *)d;
125 
126 	if (c == -1)
127 		c = strlen((char *)cd);
128 
129 	if (0) {
130 		printf("%s:%d: [%d] (%d)",
131 			str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
132 			str_data[3],
133 			type, c);
134 		printd(d, c);
135 		printf("\r\n");
136 	}
137 	*p++ = ap->type;
138 	*p++ = ap->way;
139 	if (type != NULL) *p++ = type;
140 	while (c-- > 0) {
141 		if ((*p++ = *cd++) == IAC)
142 			*p++ = IAC;
143 	}
144 	*p++ = IAC;
145 	*p++ = SE;
146 	if (str_data[3] == TELQUAL_IS)
147 		printsub('>', &str_data[2], p - (&str_data[2]));
148 	return(net_write(str_data, p - str_data));
149 }
150 
151 	int
152 rsaencpwd_init(ap, server)
153 	Authenticator *ap;
154 	int server;
155 {
156 	char  *cp;
157 	FILE  *fp;
158 
159 	if (server) {
160 		str_data[3] = TELQUAL_REPLY;
161 		memset(key_file, 0, sizeof(key_file));
162 		gethostname(lhostname, sizeof(lhostname));
163 		if ((cp = strchr(lhostname, '.')) != 0)  *cp = '\0';
164 		strcpy(key_file, "/etc/.");
165 		strcat(key_file, lhostname);
166 		strcat(key_file, "_privkey");
167 		if ((fp=fopen(key_file, "r"))==NULL) return(0);
168 		fclose(fp);
169 	} else {
170 		str_data[3] = TELQUAL_IS;
171 	}
172 	return(1);
173 }
174 
175 	int
176 rsaencpwd_send(ap)
177 	Authenticator *ap;
178 {
179 
180 	printf("[ Trying RSAENCPWD ... ]\n");
181 	if (!UserNameRequested) {
182 		return(0);
183 	}
184 	if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {
185 		return(0);
186 	}
187 	if (!Data(ap, NULL, (void *)NULL, 0)) {
188 		return(0);
189 	}
190 
191 
192 	return(1);
193 }
194 
195 	void
196 rsaencpwd_is(ap, data, cnt)
197 	Authenticator *ap;
198 	unsigned char *data;
199 	int cnt;
200 {
201 	Session_Key skey;
202 	Block datablock;
203 	char  r_passwd[PWD_SZ], r_user[NAME_SZ];
204 	char  *cp, key[160];
205 	char  chalkey[160], *ptr;
206 	FILE  *fp;
207 	int r, i, j, chalkey_len, len;
208 	time_t now;
209 
210 	cnt--;
211 	switch (*data++) {
212 	case RSA_ENCPWD_AUTH:
213 		memmove((void *)auth.dat, (void *)data, auth.length = cnt);
214 
215 		if ((fp=fopen(key_file, "r"))==NULL) {
216 		  Data(ap, RSA_ENCPWD_REJECT, (void *)"Auth failed", -1);
217 		  auth_finished(ap, AUTH_REJECT);
218 		  return;
219 		}
220 		/*
221 		 *  get privkey
222 		 */
223 		fscanf(fp, "%x;", &len);
224 		for (i=0;i<len;i++) {
225 		  j = getc(fp);  key[i]=j;
226 		}
227 		fclose(fp);
228 
229 		r = accept_rsa_encpwd(&auth, key, challenge,
230 				      challenge_len, r_passwd);
231 		if (r < 0) {
232 		  Data(ap, RSA_ENCPWD_REJECT, (void *)"Auth failed", -1);
233 		  auth_finished(ap, AUTH_REJECT);
234 		  return;
235 		}
236 		auth_encrypt_userpwd(r_passwd);
237 		if (rsaencpwd_passwdok(UserNameRequested, UserPassword) == 0) {
238 		  /*
239 		   *  illegal username and password
240 		   */
241 		  Data(ap, RSA_ENCPWD_REJECT, (void *)"Illegal password", -1);
242 		  auth_finished(ap, AUTH_REJECT);
243 		  return;
244 		}
245 
246 		Data(ap, RSA_ENCPWD_ACCEPT, (void *)0, 0);
247 		auth_finished(ap, AUTH_USER);
248 		break;
249 
250 
251 	case IAC:
252 
253 		/*
254 		 * If we are doing mutual authentication, get set up to send
255 		 * the challenge, and verify it when the response comes back.
256 		 */
257 		if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY) {
258 		  register int i;
259 
260 
261 		  time(&now);
262 		  if ((now % 2) == 0) {
263 		    sprintf(challenge, "%x", now);
264 		    challenge_len = strlen(challenge);
265 		  } else {
266 		    strcpy(challenge, "randchal");
267 		    challenge_len = 8;
268 		  }
269 
270 		  if ((fp=fopen(key_file, "r"))==NULL) {
271 		    Data(ap, RSA_ENCPWD_REJECT, (void *)"Auth failed", -1);
272 		    auth_finished(ap, AUTH_REJECT);
273 		    return;
274 		  }
275 		  /*
276 		   *  skip privkey
277 		   */
278 		  fscanf(fp, "%x;", &len);
279 		  for (i=0;i<len;i++) {
280 		    j = getc(fp);
281 		  }
282 		  /*
283 		   * get pubkey
284 		   */
285 		  fscanf(fp, "%x;", &len);
286 		  for (i=0;i<len;i++) {
287 		    j = getc(fp);  key[i]=j;
288 		  }
289 		  fclose(fp);
290 		  chalkey[0] = 0x30;
291 		  ptr = (char *) &chalkey[1];
292 		  chalkey_len = 1+NumEncodeLengthOctets(i)+i+1+NumEncodeLengthOctets(challenge_len)+challenge_len;
293 		  EncodeLength(ptr, chalkey_len);
294 		  ptr +=NumEncodeLengthOctets(chalkey_len);
295 		  *ptr++ = 0x04;  /* OCTET STRING */
296 		  *ptr++ = challenge_len;
297 		  memmove(ptr, challenge, challenge_len);
298 		  ptr += challenge_len;
299 		  *ptr++ = 0x04;  /* OCTET STRING */
300 		  EncodeLength(ptr, i);
301 		  ptr += NumEncodeLengthOctets(i);
302 		  memmove(ptr, key, i);
303 		  chalkey_len = 1+NumEncodeLengthOctets(chalkey_len)+chalkey_len;
304 		  Data(ap, RSA_ENCPWD_CHALLENGEKEY, (void *)chalkey, chalkey_len);
305 		}
306 		break;
307 
308 	default:
309 		Data(ap, RSA_ENCPWD_REJECT, 0, 0);
310 		break;
311 	}
312 }
313 
314 
315 	void
316 rsaencpwd_reply(ap, data, cnt)
317 	Authenticator *ap;
318 	unsigned char *data;
319 	int cnt;
320 {
321 	Session_Key skey;
322 	KTEXT_ST token;
323 	Block enckey;
324 	int r, pubkey_len;
325 	char	randchal[CHAL_SZ], *cp;
326 	char	chalkey[160], pubkey[128], *ptr;
327 
328 	if (cnt-- < 1)
329 		return;
330 	switch (*data++) {
331 	case RSA_ENCPWD_REJECT:
332 		if (cnt > 0) {
333 			printf("[ RSA_ENCPWD refuses authentication because %.*s ]\r\n",
334 				cnt, data);
335 		} else
336 			printf("[ RSA_ENCPWD refuses authentication ]\r\n");
337 		auth_send_retry();
338 		return;
339 	case RSA_ENCPWD_ACCEPT:
340 		printf("[ RSA_ENCPWD accepts you ]\n");
341 		auth_finished(ap, AUTH_USER);
342 		return;
343 	case RSA_ENCPWD_CHALLENGEKEY:
344 		/*
345 		 * Verify that the response to the challenge is correct.
346 		 */
347 
348 		memmove((void *)chalkey, (void *)data, cnt);
349 		ptr = (char *) &chalkey[0];
350 		ptr += DecodeHeaderLength(chalkey);
351 		if (*ptr != 0x04) {
352 		  return;
353 		}
354 		*ptr++;
355 		challenge_len = DecodeValueLength(ptr);
356 		ptr += NumEncodeLengthOctets(challenge_len);
357 		memmove(challenge, ptr, challenge_len);
358 		ptr += challenge_len;
359 		if (*ptr != 0x04) {
360 		  return;
361 		}
362 		*ptr++;
363 		pubkey_len = DecodeValueLength(ptr);
364 		ptr += NumEncodeLengthOctets(pubkey_len);
365 		memmove(pubkey, ptr, pubkey_len);
366 		memset(user_passwd, 0, sizeof(user_passwd));
367 		local_des_read_pw_string(user_passwd, sizeof(user_passwd)-1, "Password: ", 0);
368 		UserPassword = user_passwd;
369 		Challenge = challenge;
370 		r = init_rsa_encpwd(&token, user_passwd, challenge, challenge_len, pubkey);
371 		if (r < 0) {
372 		  token.length = 1;
373 		}
374 
375 		if (!Data(ap, RSA_ENCPWD_AUTH, (void *)token.dat, token.length)) {
376 		  return;
377 		}
378 
379 		break;
380 
381 	default:
382 		return;
383 	}
384 }
385 
386 	int
387 rsaencpwd_status(ap, name, level)
388 	Authenticator *ap;
389 	char *name;
390 	int level;
391 {
392 
393 	if (level < AUTH_USER)
394 		return(level);
395 
396 	if (UserNameRequested && rsaencpwd_passwdok(UserNameRequested, UserPassword)) {
397 		strcpy(name, UserNameRequested);
398 		return(AUTH_VALID);
399 	} else {
400 		return(AUTH_USER);
401 	}
402 }
403 
404 #define	BUMP(buf, len)		while (*(buf)) {++(buf), --(len);}
405 #define	ADDC(buf, len, c)	if ((len) > 0) {*(buf)++ = (c); --(len);}
406 
407 	void
408 rsaencpwd_printsub(data, cnt, buf, buflen)
409 	unsigned char *data, *buf;
410 	int cnt, buflen;
411 {
412 	char lbuf[32];
413 	register int i;
414 
415 	buf[buflen-1] = '\0';		/* make sure its NULL terminated */
416 	buflen -= 1;
417 
418 	switch(data[3]) {
419 	case RSA_ENCPWD_REJECT:	/* Rejected (reason might follow) */
420 		strncpy((char *)buf, " REJECT ", buflen);
421 		goto common;
422 
423 	case RSA_ENCPWD_ACCEPT:	/* Accepted (name might follow) */
424 		strncpy((char *)buf, " ACCEPT ", buflen);
425 	common:
426 		BUMP(buf, buflen);
427 		if (cnt <= 4)
428 			break;
429 		ADDC(buf, buflen, '"');
430 		for (i = 4; i < cnt; i++)
431 			ADDC(buf, buflen, data[i]);
432 		ADDC(buf, buflen, '"');
433 		ADDC(buf, buflen, '\0');
434 		break;
435 
436 	case RSA_ENCPWD_AUTH:		/* Authentication data follows */
437 		strncpy((char *)buf, " AUTH", buflen);
438 		goto common2;
439 
440 	case RSA_ENCPWD_CHALLENGEKEY:
441 		strncpy((char *)buf, " CHALLENGEKEY", buflen);
442 		goto common2;
443 
444 	default:
445 		sprintf(lbuf, " %d (unknown)", data[3]);
446 		strncpy((char *)buf, lbuf, buflen);
447 	common2:
448 		BUMP(buf, buflen);
449 		for (i = 4; i < cnt; i++) {
450 			sprintf(lbuf, " %d", data[i]);
451 			strncpy((char *)buf, lbuf, buflen);
452 			BUMP(buf, buflen);
453 		}
454 		break;
455 	}
456 }
457 
458 int rsaencpwd_passwdok(name, passwd)
459 char *name, *passwd;
460 {
461   char *crypt();
462   char *salt, *p;
463   struct passwd *pwd;
464   int   passwdok_status = 0;
465 
466   if (pwd = getpwnam(name))
467     salt = pwd->pw_passwd;
468   else salt = "xx";
469 
470   p = crypt(passwd, salt);
471 
472   if (pwd && !strcmp(p, pwd->pw_passwd)) {
473     passwdok_status = 1;
474   } else passwdok_status = 0;
475   return(passwdok_status);
476 }
477 
478 #endif
479 
480 #ifdef notdef
481 
482 prkey(msg, key)
483 	char *msg;
484 	unsigned char *key;
485 {
486 	register int i;
487 	printf("%s:", msg);
488 	for (i = 0; i < 8; i++)
489 		printf(" %3d", key[i]);
490 	printf("\r\n");
491 }
492 #endif
493