No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / telnet / libtelnet / rsaencpwd.c
bloba08bff6f045c76269e8c866650a5a0c118a7e518
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
34 #include <config.h>
36 __RCSID("$Heimdal: rsaencpwd.c 22071 2007-11-14 20:04:50Z lha $"
37 "$NetBSD$");
39 #ifdef RSA_ENCPWD
41 * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
42 * ALL RIGHTS RESERVED
44 * "Digital Equipment Corporation authorizes the reproduction,
45 * distribution and modification of this software subject to the following
46 * restrictions:
48 * 1. Any partial or whole copy of this software, or any modification
49 * thereof, must include this copyright notice in its entirety.
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.
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.
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.
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"
73 #include <sys/types.h>
74 #ifdef HAVE_ARPA_TELNET_H
75 #include <arpa/telnet.h>
76 #endif
77 #include <pwd.h>
78 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #ifdef SOCKS
83 #include <socks.h>
84 #endif
86 #include "encrypt.h"
87 #include "auth.h"
88 #include "misc.h"
89 #include "cdc.h"
91 extern auth_debug_mode;
93 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
94 AUTHTYPE_RSA_ENCPWD, };
95 static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION,
96 TELQUAL_NAME, };
98 #define RSA_ENCPWD_AUTH 0 /* Authentication data follows */
99 #define RSA_ENCPWD_REJECT 1 /* Rejected (reason might follow) */
100 #define RSA_ENCPWD_ACCEPT 2 /* Accepted */
101 #define RSA_ENCPWD_CHALLENGEKEY 3 /* Challenge and public key */
103 #define NAME_SZ 40
104 #define CHAL_SZ 20
105 #define PWD_SZ 40
107 static KTEXT_ST auth;
108 static char name[NAME_SZ];
109 static char user_passwd[PWD_SZ];
110 static char key_file[2*NAME_SZ];
111 static char lhostname[NAME_SZ];
112 static char challenge[CHAL_SZ];
113 static int challenge_len;
115 static int
116 Data(ap, type, d, c)
117 Authenticator *ap;
118 int type;
119 void *d;
120 int c;
122 unsigned char *p = str_data + 4;
123 unsigned char *cd = (unsigned char *)d;
125 if (c == -1)
126 c = strlen((char *)cd);
128 if (0) {
129 printf("%s:%d: [%d] (%d)",
130 str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
131 str_data[3],
132 type, c);
133 printd(d, c);
134 printf("\r\n");
136 *p++ = ap->type;
137 *p++ = ap->way;
138 if (type != NULL) *p++ = type;
139 while (c-- > 0) {
140 if ((*p++ = *cd++) == IAC)
141 *p++ = IAC;
143 *p++ = IAC;
144 *p++ = SE;
145 if (str_data[3] == TELQUAL_IS)
146 printsub('>', &str_data[2], p - (&str_data[2]));
147 return(telnet_net_write(str_data, p - str_data));
151 rsaencpwd_init(ap, server)
152 Authenticator *ap;
153 int server;
155 char *cp;
156 FILE *fp;
158 if (server) {
159 str_data[3] = TELQUAL_REPLY;
160 memset(key_file, 0, sizeof(key_file));
161 gethostname(lhostname, sizeof(lhostname));
162 if ((cp = strchr(lhostname, '.')) != 0) *cp = '\0';
163 snprintf(key_file, sizeof(key_file),
164 SYSCONFDIR "/.%s_privkey", lhostname);
165 if ((fp=fopen(key_file, "r"))==NULL) return(0);
166 fclose(fp);
167 } else {
168 str_data[3] = TELQUAL_IS;
170 return(1);
174 rsaencpwd_send(ap)
175 Authenticator *ap;
178 printf("[ Trying RSAENCPWD ... ]\r\n");
179 if (!UserNameRequested) {
180 return(0);
182 if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {
183 return(0);
185 if (!Data(ap, NULL, NULL, 0)) {
186 return(0);
190 return(1);
193 void
194 rsaencpwd_is(ap, data, cnt)
195 Authenticator *ap;
196 unsigned char *data;
197 int cnt;
199 Session_Key skey;
200 des_cblock datablock;
201 char r_passwd[PWD_SZ], r_user[NAME_SZ];
202 char *cp, key[160];
203 char chalkey[160], *ptr;
204 FILE *fp;
205 int r, i, j, chalkey_len, len;
206 time_t now;
208 cnt--;
209 switch (*data++) {
210 case RSA_ENCPWD_AUTH:
211 memmove(auth.dat, data, auth.length = cnt);
213 if ((fp=fopen(key_file, "r"))==NULL) {
214 Data(ap, RSA_ENCPWD_REJECT, "Auth failed", -1);
215 auth_finished(ap, AUTH_REJECT);
216 return;
219 * get privkey
221 fscanf(fp, "%x;", &len);
222 for (i=0;i<len;i++) {
223 j = getc(fp); key[i]=j;
225 fclose(fp);
227 r = accept_rsa_encpwd(&auth, key, challenge,
228 challenge_len, r_passwd);
229 if (r < 0) {
230 Data(ap, RSA_ENCPWD_REJECT, "Auth failed", -1);
231 auth_finished(ap, AUTH_REJECT);
232 return;
234 auth_encrypt_userpwd(r_passwd);
235 if (rsaencpwd_passwdok(UserNameRequested, UserPassword) == 0) {
237 * illegal username and password
239 Data(ap, RSA_ENCPWD_REJECT, "Illegal password", -1);
240 auth_finished(ap, AUTH_REJECT);
241 return;
244 Data(ap, RSA_ENCPWD_ACCEPT, 0, 0);
245 auth_finished(ap, AUTH_USER);
246 break;
249 case IAC:
252 * If we are doing mutual authentication, get set up to send
253 * the challenge, and verify it when the response comes back.
255 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY) {
256 int i;
259 time(&now);
260 if ((now % 2) == 0) {
261 snprintf(challenge, sizeof(challenge), "%x", now);
262 challenge_len = strlen(challenge);
263 } else {
264 strlcpy(challenge, "randchal", sizeof(challenge));
265 challenge_len = 8;
268 if ((fp=fopen(key_file, "r"))==NULL) {
269 Data(ap, RSA_ENCPWD_REJECT, "Auth failed", -1);
270 auth_finished(ap, AUTH_REJECT);
271 return;
274 * skip privkey
276 fscanf(fp, "%x;", &len);
277 for (i=0;i<len;i++) {
278 j = getc(fp);
281 * get pubkey
283 fscanf(fp, "%x;", &len);
284 for (i=0;i<len;i++) {
285 j = getc(fp); key[i]=j;
287 fclose(fp);
288 chalkey[0] = 0x30;
289 ptr = (char *) &chalkey[1];
290 chalkey_len = 1+NumEncodeLengthOctets(i)+i+1+NumEncodeLengthOctets(challenge_len)+challenge_len;
291 EncodeLength(ptr, chalkey_len);
292 ptr +=NumEncodeLengthOctets(chalkey_len);
293 *ptr++ = 0x04; /* OCTET STRING */
294 *ptr++ = challenge_len;
295 memmove(ptr, challenge, challenge_len);
296 ptr += challenge_len;
297 *ptr++ = 0x04; /* OCTET STRING */
298 EncodeLength(ptr, i);
299 ptr += NumEncodeLengthOctets(i);
300 memmove(ptr, key, i);
301 chalkey_len = 1+NumEncodeLengthOctets(chalkey_len)+chalkey_len;
302 Data(ap, RSA_ENCPWD_CHALLENGEKEY, chalkey, chalkey_len);
304 break;
306 default:
307 Data(ap, RSA_ENCPWD_REJECT, 0, 0);
308 break;
313 void
314 rsaencpwd_reply(ap, data, cnt)
315 Authenticator *ap;
316 unsigned char *data;
317 int cnt;
319 Session_Key skey;
320 KTEXT_ST token;
321 des_cblock enckey;
322 int r, pubkey_len;
323 char randchal[CHAL_SZ], *cp;
324 char chalkey[160], pubkey[128], *ptr;
326 if (cnt-- < 1)
327 return;
328 switch (*data++) {
329 case RSA_ENCPWD_REJECT:
330 if (cnt > 0) {
331 printf("[ RSA_ENCPWD refuses authentication because %.*s ]\r\n",
332 cnt, data);
333 } else
334 printf("[ RSA_ENCPWD refuses authentication ]\r\n");
335 auth_send_retry();
336 return;
337 case RSA_ENCPWD_ACCEPT:
338 printf("[ RSA_ENCPWD accepts you ]\r\n");
339 auth_finished(ap, AUTH_USER);
340 return;
341 case RSA_ENCPWD_CHALLENGEKEY:
343 * Verify that the response to the challenge is correct.
346 memmove(chalkey, data, cnt);
347 ptr = (char *) &chalkey[0];
348 ptr += DecodeHeaderLength(chalkey);
349 if (*ptr != 0x04) {
350 return;
352 *ptr++;
353 challenge_len = DecodeValueLength(ptr);
354 ptr += NumEncodeLengthOctets(challenge_len);
355 memmove(challenge, ptr, challenge_len);
356 ptr += challenge_len;
357 if (*ptr != 0x04) {
358 return;
360 *ptr++;
361 pubkey_len = DecodeValueLength(ptr);
362 ptr += NumEncodeLengthOctets(pubkey_len);
363 memmove(pubkey, ptr, pubkey_len);
364 memset(user_passwd, 0, sizeof(user_passwd));
365 des_read_pw_string(user_passwd, sizeof(user_passwd)-1, "Password: ", 0);
366 UserPassword = user_passwd;
367 Challenge = challenge;
368 r = init_rsa_encpwd(&token, user_passwd, challenge, challenge_len, pubkey);
369 if (r < 0) {
370 token.length = 1;
373 if (!Data(ap, RSA_ENCPWD_AUTH, token.dat, token.length)) {
374 return;
377 break;
379 default:
380 return;
385 rsaencpwd_status(ap, name, name_sz, level)
386 Authenticator *ap;
387 char *name;
388 size_t name_sz;
389 int level;
392 if (level < AUTH_USER)
393 return(level);
395 if (UserNameRequested && rsaencpwd_passwdok(UserNameRequested, UserPassword)) {
396 strlcpy(name, UserNameRequested, name_sz);
397 return(AUTH_VALID);
398 } else {
399 return(AUTH_USER);
403 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
404 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
406 void
407 rsaencpwd_printsub(data, cnt, buf, buflen)
408 unsigned char *data, *buf;
409 int cnt, buflen;
411 int i;
413 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
414 buflen -= 1;
416 switch(data[3]) {
417 case RSA_ENCPWD_REJECT: /* Rejected (reason might follow) */
418 strlcpy((char *)buf, " REJECT ", buflen);
419 goto common;
421 case RSA_ENCPWD_ACCEPT: /* Accepted (name might follow) */
422 strlcpy((char *)buf, " ACCEPT ", buflen);
423 common:
424 BUMP(buf, buflen);
425 if (cnt <= 4)
426 break;
427 ADDC(buf, buflen, '"');
428 for (i = 4; i < cnt; i++)
429 ADDC(buf, buflen, data[i]);
430 ADDC(buf, buflen, '"');
431 ADDC(buf, buflen, '\0');
432 break;
434 case RSA_ENCPWD_AUTH: /* Authentication data follows */
435 strlcpy((char *)buf, " AUTH", buflen);
436 goto common2;
438 case RSA_ENCPWD_CHALLENGEKEY:
439 strlcpy((char *)buf, " CHALLENGEKEY", buflen);
440 goto common2;
442 default:
443 snprintf(buf, buflen, " %d (unknown)", data[3]);
444 common2:
445 BUMP(buf, buflen);
446 for (i = 4; i < cnt; i++) {
447 snprintf(buf, buflen, " %d", data[i]);
448 BUMP(buf, buflen);
450 break;
454 int rsaencpwd_passwdok(name, passwd)
455 char *name, *passwd;
457 char *crypt();
458 char *salt, *p;
459 struct passwd *pwd;
460 int passwdok_status = 0;
462 if (pwd = k_getpwnam(name))
463 salt = pwd->pw_passwd;
464 else salt = "xx";
466 p = crypt(passwd, salt);
468 if (pwd && !strcmp(p, pwd->pw_passwd)) {
469 passwdok_status = 1;
470 } else passwdok_status = 0;
471 return(passwdok_status);
474 #endif
476 #ifdef notdef
478 prkey(msg, key)
479 char *msg;
480 unsigned char *key;
482 int i;
483 printf("%s:", msg);
484 for (i = 0; i < 8; i++)
485 printf(" %3d", key[i]);
486 printf("\r\n");
488 #endif