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