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
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.
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
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 $
35 * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
38 * "Digital Equipment Corporation authorizes the reproduction,
39 * distribution and modification of this software subject to the following
42 * 1. Any partial or whole copy of this software, or any modification
43 * thereof, must include this copyright notice in its entirety.
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.
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.
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.
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"
67 #include <sys/types.h>
68 #include <arpa/telnet.h>
79 extern auth_debug_mode;
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,
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 */
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;
110 unsigned char *p = str_data + 4;
111 unsigned char *cd = (unsigned char *)d;
114 c = strlen((char *)cd);
117 printf("%s:%d: [%d] (%d)",
118 str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
126 if (type != NULL) *p++ = type;
128 if ((*p++ = *cd++) == IAC)
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));
139 rsaencpwd_init(ap, 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);
157 str_data[3] = TELQUAL_IS;
167 printf("[ Trying RSAENCPWD ... ]\n");
168 if (!UserNameRequested) {
171 if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {
174 if (!Data(ap, NULL, NULL, 0)) {
183 rsaencpwd_is(ap, data, cnt)
188 char r_passwd[PWD_SZ];
190 char chalkey[160], *ptr;
192 int r, i, j, chalkey_len, len;
197 case RSA_ENCPWD_AUTH:
198 memmove((void *)auth.dat, (void *)data, auth.length = cnt);
200 if ((fp=fopen(key_file, "r"))==NULL) {
201 Data(ap, RSA_ENCPWD_REJECT, (void *)"Auth failed", -1);
202 auth_finished(ap, AUTH_REJECT);
208 fscanf(fp, "%x;", &len);
209 for (i=0;i<len;i++) {
210 j = getc(fp); key[i]=j;
214 r = accept_rsa_encpwd(&auth, key, challenge,
215 challenge_len, r_passwd);
217 Data(ap, RSA_ENCPWD_REJECT, (void *)"Auth failed", -1);
218 auth_finished(ap, AUTH_REJECT);
221 auth_encrypt_userpwd(r_passwd);
222 if (rsaencpwd_passwdok(UserNameRequested, UserPassword) == 0) {
224 * illegal username and password
226 Data(ap, RSA_ENCPWD_REJECT, (void *)"Illegal password", -1);
227 auth_finished(ap, AUTH_REJECT);
231 Data(ap, RSA_ENCPWD_ACCEPT, (void *)0, 0);
232 auth_finished(ap, AUTH_USER);
239 * If we are doing mutual authentication, get set up to send
240 * the challenge, and verify it when the response comes back.
242 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_ONE_WAY) {
247 if ((now % 2) == 0) {
248 sprintf(challenge, "%x", now);
249 challenge_len = strlen(challenge);
251 strcpy(challenge, "randchal");
255 if ((fp=fopen(key_file, "r"))==NULL) {
256 Data(ap, RSA_ENCPWD_REJECT, (void *)"Auth failed", -1);
257 auth_finished(ap, AUTH_REJECT);
263 fscanf(fp, "%x;", &len);
264 for (i=0;i<len;i++) {
270 fscanf(fp, "%x;", &len);
271 for (i=0;i<len;i++) {
272 j = getc(fp); key[i]=j;
276 ptr = (char *) &chalkey[1];
277 chalkey_len = 1+NumEncodeLengthOctets(i)+i+1+NumEncodeLengthOctets(challenge_len)+challenge_len;
278 EncodeLength(ptr, chalkey_len);
279 ptr +=NumEncodeLengthOctets(chalkey_len);
280 *ptr++ = 0x04; /* OCTET STRING */
281 *ptr++ = challenge_len;
282 memmove(ptr, challenge, challenge_len);
283 ptr += challenge_len;
284 *ptr++ = 0x04; /* OCTET STRING */
285 EncodeLength(ptr, i);
286 ptr += NumEncodeLengthOctets(i);
287 memmove(ptr, key, i);
288 chalkey_len = 1+NumEncodeLengthOctets(chalkey_len)+chalkey_len;
289 Data(ap, RSA_ENCPWD_CHALLENGEKEY, (void *)chalkey, chalkey_len);
294 Data(ap, RSA_ENCPWD_REJECT, 0, 0);
301 rsaencpwd_reply(ap, data, cnt)
308 char chalkey[160], pubkey[128], *ptr;
313 case RSA_ENCPWD_REJECT:
315 printf("[ RSA_ENCPWD refuses authentication because %.*s ]\r\n",
318 printf("[ RSA_ENCPWD refuses authentication ]\r\n");
321 case RSA_ENCPWD_ACCEPT:
322 printf("[ RSA_ENCPWD accepts you ]\n");
323 auth_finished(ap, AUTH_USER);
325 case RSA_ENCPWD_CHALLENGEKEY:
327 * Verify that the response to the challenge is correct.
330 memmove((void *)chalkey, (void *)data, cnt);
331 ptr = (char *) &chalkey[0];
332 ptr += DecodeHeaderLength(chalkey);
336 ptr++; /* skip 0x04 */
337 challenge_len = DecodeValueLength(ptr);
338 ptr += NumEncodeLengthOctets(challenge_len);
339 memmove(challenge, ptr, challenge_len);
340 ptr += challenge_len;
344 ptr++; /* skip 0x04 */
345 pubkey_len = DecodeValueLength(ptr);
346 ptr += NumEncodeLengthOctets(pubkey_len);
347 memmove(pubkey, ptr, pubkey_len);
348 memset(user_passwd, 0, sizeof(user_passwd));
349 local_des_read_pw_string(user_passwd, sizeof(user_passwd)-1, "Password: ", 0);
350 UserPassword = user_passwd;
351 Challenge = challenge;
352 r = init_rsa_encpwd(&token, user_passwd, challenge, challenge_len, pubkey);
357 if (!Data(ap, RSA_ENCPWD_AUTH, (void *)token.dat, token.length)) {
369 rsaencpwd_status(ap, name, level)
375 if (level < AUTH_USER)
378 if (UserNameRequested && rsaencpwd_passwdok(UserNameRequested, UserPassword)) {
379 strcpy(name, UserNameRequested);
386 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
387 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
390 rsaencpwd_printsub(data, cnt, buf, buflen)
391 unsigned char *data, *buf;
397 buf[buflen-1] = '\0'; /* make sure its NULL terminated */
401 case RSA_ENCPWD_REJECT: /* Rejected (reason might follow) */
402 strncpy((char *)buf, " REJECT ", buflen);
405 case RSA_ENCPWD_ACCEPT: /* Accepted (name might follow) */
406 strncpy((char *)buf, " ACCEPT ", buflen);
411 ADDC(buf, buflen, '"');
412 for (i = 4; i < cnt; i++)
413 ADDC(buf, buflen, data[i]);
414 ADDC(buf, buflen, '"');
415 ADDC(buf, buflen, '\0');
418 case RSA_ENCPWD_AUTH: /* Authentication data follows */
419 strncpy((char *)buf, " AUTH", buflen);
422 case RSA_ENCPWD_CHALLENGEKEY:
423 strncpy((char *)buf, " CHALLENGEKEY", buflen);
427 sprintf(lbuf, " %d (unknown)", data[3]);
428 strncpy((char *)buf, lbuf, buflen);
431 for (i = 4; i < cnt; i++) {
432 sprintf(lbuf, " %d", data[i]);
433 strncpy((char *)buf, lbuf, buflen);
440 int rsaencpwd_passwdok(name, passwd)
446 int passwdok_status = 0;
448 if (pwd = getpwnam(name))
449 salt = pwd->pw_passwd;
452 p = crypt(passwd, salt);
454 if (pwd && !strcmp(p, pwd->pw_passwd)) {
456 } else passwdok_status = 0;
457 return(passwdok_status);