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