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