Make sure the RSA OAEP test is skipped under -DRSAref because
[openssl.git] / crypto / des / read_pwd.c
1 /* crypto/des/read_pwd.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 /* #define SIGACTION */ /* Define this if you have sigaction() */
60 #ifdef WIN16TTY
61 #undef WIN16
62 #undef _WINDOWS
63 #include <graph.h>
64 #endif
65
66 /* 06-Apr-92 Luke Brennan    Support for VMS */
67 #include "des_locl.h"
68 #include "cryptlib.h"
69 #include <signal.h>
70 #include <string.h>
71 #include <setjmp.h>
72 #include <errno.h>
73
74 #ifdef WIN_CONSOLE_BUG
75 #include <windows.h>
76 #include <wincon.h>
77 #endif
78
79
80 /* There are 5 types of terminal interface supported,
81  * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
82  */
83
84 #if defined(__sgi) && !defined(TERMIOS)
85 #define TERMIOS
86 #undef  TERMIO
87 #undef  SGTTY
88 #endif
89
90 #if defined(linux) && !defined(TERMIO)
91 #undef  TERMIOS
92 #define TERMIO
93 #undef  SGTTY
94 #endif
95
96 #ifdef _LIBC
97 #undef  TERMIOS
98 #define TERMIO
99 #undef  SGTTY
100 #endif
101
102 #if !defined(TERMIO) && !defined(TERMIOS) && !defined(VMS) && !defined(MSDOS)
103 #undef  TERMIOS
104 #undef  TERMIO
105 #define SGTTY
106 #endif
107
108 #ifdef TERMIOS
109 #include <termios.h>
110 #define TTY_STRUCT              struct termios
111 #define TTY_FLAGS               c_lflag
112 #define TTY_get(tty,data)       tcgetattr(tty,data)
113 #define TTY_set(tty,data)       tcsetattr(tty,TCSANOW,data)
114 #endif
115
116 #ifdef TERMIO
117 #include <termio.h>
118 #define TTY_STRUCT              struct termio
119 #define TTY_FLAGS               c_lflag
120 #define TTY_get(tty,data)       ioctl(tty,TCGETA,data)
121 #define TTY_set(tty,data)       ioctl(tty,TCSETA,data)
122 #endif
123
124 #ifdef SGTTY
125 #include <sgtty.h>
126 #define TTY_STRUCT              struct sgttyb
127 #define TTY_FLAGS               sg_flags
128 #define TTY_get(tty,data)       ioctl(tty,TIOCGETP,data)
129 #define TTY_set(tty,data)       ioctl(tty,TIOCSETP,data)
130 #endif
131
132 #if !defined(_LIBC) && !defined(MSDOS) && !defined(VMS)
133 #include <sys/ioctl.h>
134 #endif
135
136 #ifdef MSDOS
137 #include <conio.h>
138 #define fgets(a,b,c) noecho_fgets(a,b,c)
139 #endif
140
141 #ifdef VMS
142 #include <ssdef.h>
143 #include <iodef.h>
144 #include <ttdef.h>
145 #include <descrip.h>
146 struct IOSB {
147         short iosb$w_value;
148         short iosb$w_count;
149         long  iosb$l_info;
150         };
151 #endif
152
153 #ifndef NX509_SIG
154 #define NX509_SIG 32
155 #endif
156
157 #ifndef NOPROTO
158 static void read_till_nl(FILE *);
159 static void recsig(int);
160 static void pushsig(void);
161 static void popsig(void);
162 #if defined(MSDOS) && !defined(WIN16)
163 static int noecho_fgets(char *buf, int size, FILE *tty);
164 #endif
165 #else
166 static void read_till_nl();
167 static void recsig();
168 static void pushsig();
169 static void popsig();
170 #if defined(MSDOS) && !defined(WIN16)
171 static int noecho_fgets();
172 #endif
173 #endif
174
175 #ifdef SIGACTION
176  static struct sigaction savsig[NX509_SIG];
177 #else
178 # ifndef NOPROTO
179   static void (*savsig[NX509_SIG])(int );
180 # else
181   static void (*savsig[NX509_SIG])();
182 # endif
183 #endif
184 static jmp_buf save;
185
186 int des_read_pw_string(buf, length, prompt, verify)
187 char *buf;
188 int length;
189 const char *prompt;
190 int verify;
191         {
192         char buff[BUFSIZ];
193         int ret;
194
195         ret=des_read_pw(buf,buff,(length>BUFSIZ)?BUFSIZ:length,prompt,verify);
196         memset(buff,0,BUFSIZ);
197         return(ret);
198         }
199
200 #ifndef WIN16
201
202 static void read_till_nl(in)
203 FILE *in;
204         {
205 #define SIZE 4
206         char buf[SIZE+1];
207
208         do      {
209                 fgets(buf,SIZE,in);
210                 } while (strchr(buf,'\n') == NULL);
211         }
212
213
214 /* return 0 if ok, 1 (or -1) otherwise */
215 int des_read_pw(buf, buff, size, prompt, verify)
216 char *buf;
217 char *buff;
218 int size;
219 const char *prompt;
220 int verify;
221         {
222 #ifdef VMS
223         struct IOSB iosb;
224         $DESCRIPTOR(terminal,"TT");
225         long tty_orig[3], tty_new[3];
226         long status;
227         unsigned short channel = 0;
228 #else
229 #ifndef MSDOS
230         TTY_STRUCT tty_orig,tty_new;
231 #endif
232 #endif
233         int number;
234         int ok;
235         /* statics are simply to avoid warnings about longjmp clobbering
236            things */
237         static int ps;
238         int is_a_tty;
239         static FILE *tty;
240         char *p;
241
242         if (setjmp(save))
243                 {
244                 ok=0;
245                 goto error;
246                 }
247
248         number=5;
249         ok=0;
250         ps=0;
251         is_a_tty=1;
252         tty=NULL;
253
254 #ifndef MSDOS
255         if ((tty=fopen("/dev/tty","r")) == NULL)
256                 tty=stdin;
257 #else /* MSDOS */
258         if ((tty=fopen("con","r")) == NULL)
259                 tty=stdin;
260 #endif /* MSDOS */
261
262 #if defined(TTY_get) && !defined(VMS)
263         if (TTY_get(fileno(tty),&tty_orig) == -1)
264                 {
265 #ifdef ENOTTY
266                 if (errno == ENOTTY)
267                         is_a_tty=0;
268                 else
269 #endif
270 #ifdef EINVAL
271                 /* Ariel Glenn ariel@columbia.edu reports that solaris
272                  * can return EINVAL instead.  This should be ok */
273                 if (errno == EINVAL)
274                         is_a_tty=0;
275                 else
276 #endif
277                         return(-1);
278                 }
279         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
280 #endif
281 #ifdef VMS
282         status = SYS$ASSIGN(&terminal,&channel,0,0);
283         if (status != SS$_NORMAL)
284                 return(-1);
285         status=SYS$QIOW(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
286         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
287                 return(-1);
288 #endif
289
290         pushsig();
291         ps=1;
292
293 #ifdef TTY_FLAGS
294         tty_new.TTY_FLAGS &= ~ECHO;
295 #endif
296
297 #if defined(TTY_set) && !defined(VMS)
298         if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
299                 return(-1);
300 #endif
301 #ifdef VMS
302         tty_new[0] = tty_orig[0];
303         tty_new[1] = tty_orig[1] | TT$M_NOECHO;
304         tty_new[2] = tty_orig[2];
305         status = SYS$QIOW(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
306         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
307                 return(-1);
308 #endif
309         ps=2;
310
311         while ((!ok) && (number--))
312                 {
313                 fputs(prompt,stderr);
314                 fflush(stderr);
315
316                 buf[0]='\0';
317                 fgets(buf,size,tty);
318                 if (feof(tty)) goto error;
319                 if (ferror(tty)) goto error;
320                 if ((p=(char *)strchr(buf,'\n')) != NULL)
321                         *p='\0';
322                 else    read_till_nl(tty);
323                 if (verify)
324                         {
325                         fprintf(stderr,"\nVerifying password - %s",prompt);
326                         fflush(stderr);
327                         buff[0]='\0';
328                         fgets(buff,size,tty);
329                         if (feof(tty)) goto error;
330                         if ((p=(char *)strchr(buff,'\n')) != NULL)
331                                 *p='\0';
332                         else    read_till_nl(tty);
333                                 
334                         if (strcmp(buf,buff) != 0)
335                                 {
336                                 fprintf(stderr,"\nVerify failure");
337                                 fflush(stderr);
338                                 break;
339                                 /* continue; */
340                                 }
341                         }
342                 ok=1;
343                 }
344
345 error:
346         fprintf(stderr,"\n");
347 #ifdef DEBUG
348         perror("fgets(tty)");
349 #endif
350         /* What can we do if there is an error? */
351 #if defined(TTY_set) && !defined(VMS) 
352         if (ps >= 2) TTY_set(fileno(tty),&tty_orig);
353 #endif
354 #ifdef VMS
355         if (ps >= 2)
356                 status = SYS$QIOW(0,channel,IO$_SETMODE,&iosb,0,0
357                         ,tty_orig,12,0,0,0,0);
358 #endif
359         
360         if (ps >= 1) popsig();
361         if (stdin != tty) fclose(tty);
362 #ifdef VMS
363         status = SYS$DASSGN(channel);
364 #endif
365         return(!ok);
366         }
367
368 #else /* WIN16 */
369
370 int des_read_pw(buf, buff, size, prompt, verify)
371 char *buf;
372 char *buff;
373 int size;
374 char *prompt;
375 int verify;
376         { 
377         memset(buf,0,size);
378         memset(buff,0,size);
379         return(0);
380         }
381
382 #endif
383
384 static void pushsig()
385         {
386         int i;
387
388         for (i=1; i<NX509_SIG; i++)
389                 {
390 #ifdef SIGUSR1
391                 if (i == SIGUSR1)
392                         continue;
393 #endif
394 #ifdef SIGUSR2
395                 if (i == SIGUSR2)
396                         continue;
397 #endif
398 #ifdef SIGACTION
399                 sigaction(i,NULL,&savsig[i]);
400 #else
401                 savsig[i]=signal(i,recsig);
402 #endif
403                 }
404
405 #ifdef SIGWINCH
406         signal(SIGWINCH,SIG_DFL);
407 #endif
408         }
409
410 static void popsig()
411         {
412         int i;
413
414         for (i=1; i<NX509_SIG; i++)
415                 {
416 #ifdef SIGUSR1
417                 if (i == SIGUSR1)
418                         continue;
419 #endif
420 #ifdef SIGUSR2
421                 if (i == SIGUSR2)
422                         continue;
423 #endif
424 #ifdef SIGACTION
425                 sigaction(i,&savsig[i],NULL);
426 #else
427                 signal(i,savsig[i]);
428 #endif
429                 }
430         }
431
432 static void recsig(i)
433 int i;
434         {
435         longjmp(save,1);
436 #ifdef LINT
437         i=i;
438 #endif
439         }
440
441 #if defined(MSDOS) && !defined(WIN16)
442 static int noecho_fgets(buf,size,tty)
443 char *buf;
444 int size;
445 FILE *tty;
446         {
447         int i;
448         char *p;
449
450         p=buf;
451         for (;;)
452                 {
453                 if (size == 0)
454                         {
455                         *p='\0';
456                         break;
457                         }
458                 size--;
459 #ifdef WIN16TTY
460                 i=_inchar();
461 #else
462                 i=getch();
463 #endif
464                 if (i == '\r') i='\n';
465                 *(p++)=i;
466                 if (i == '\n')
467                         {
468                         *p='\0';
469                         break;
470                         }
471                 }
472 #ifdef WIN_CONSOLE_BUG
473 /* Win95 has several evil console bugs: one of these is that the
474  * last character read using getch() is passed to the next read: this is
475  * usually a CR so this can be trouble. No STDIO fix seems to work but
476  * flushing the console appears to do the trick.
477  */
478                 {
479                         HANDLE inh;
480                         inh = GetStdHandle(STD_INPUT_HANDLE);
481                         FlushConsoleInputBuffer(inh);
482                 }
483 #endif
484         return(strlen(buf));
485         }
486 #endif