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