Security fix.
[openssl.git] / crypto / ui / ui_openssl.c
1 /* crypto/ui/ui_openssl.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Written by Richard Levitte (richard@levitte.org) and others
3  * for the OpenSSL project 2001.
4  */
5 /* ====================================================================
6  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    openssl-core@openssl.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 /* The lowest level part of this file was previously in crypto/des/read_pwd.c,
60  * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
61  * All rights reserved.
62  *
63  * This package is an SSL implementation written
64  * by Eric Young (eay@cryptsoft.com).
65  * The implementation was written so as to conform with Netscapes SSL.
66  * 
67  * This library is free for commercial and non-commercial use as long as
68  * the following conditions are aheared to.  The following conditions
69  * apply to all code found in this distribution, be it the RC4, RSA,
70  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
71  * included with this distribution is covered by the same copyright terms
72  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
73  * 
74  * Copyright remains Eric Young's, and as such any Copyright notices in
75  * the code are not to be removed.
76  * If this package is used in a product, Eric Young should be given attribution
77  * as the author of the parts of the library used.
78  * This can be in the form of a textual message at program startup or
79  * in documentation (online or textual) provided with the package.
80  * 
81  * Redistribution and use in source and binary forms, with or without
82  * modification, are permitted provided that the following conditions
83  * are met:
84  * 1. Redistributions of source code must retain the copyright
85  *    notice, this list of conditions and the following disclaimer.
86  * 2. Redistributions in binary form must reproduce the above copyright
87  *    notice, this list of conditions and the following disclaimer in the
88  *    documentation and/or other materials provided with the distribution.
89  * 3. All advertising materials mentioning features or use of this software
90  *    must display the following acknowledgement:
91  *    "This product includes cryptographic software written by
92  *     Eric Young (eay@cryptsoft.com)"
93  *    The word 'cryptographic' can be left out if the rouines from the library
94  *    being used are not cryptographic related :-).
95  * 4. If you include any Windows specific code (or a derivative thereof) from 
96  *    the apps directory (application code) you must include an acknowledgement:
97  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
98  * 
99  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
100  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
102  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
103  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
104  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
105  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
106  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
107  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
108  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
109  * SUCH DAMAGE.
110  * 
111  * The licence and distribution terms for any publically available version or
112  * derivative of this code cannot be changed.  i.e. this code cannot simply be
113  * copied and put under another distribution licence
114  * [including the GNU Public Licence.]
115  */
116
117
118 #include <openssl/e_os2.h>
119
120 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
121 # ifdef OPENSSL_UNISTD
122 #  include OPENSSL_UNISTD
123 # else
124 #  include <unistd.h>
125 # endif
126 /* If unistd.h defines _POSIX_VERSION, we conclude that we
127  * are on a POSIX system and have sigaction and termios. */
128 # if defined(_POSIX_VERSION)
129
130 #  define SIGACTION
131 #  if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
132 #   define TERMIOS
133 #  endif
134
135 # endif
136 #endif
137
138 #ifdef WIN16TTY
139 # undef OPENSSL_SYS_WIN16
140 # undef WIN16
141 # undef _WINDOWS
142 # include <graph.h>
143 #endif
144
145 /* 06-Apr-92 Luke Brennan    Support for VMS */
146 #include "ui_locl.h"
147 #include "cryptlib.h"
148 #include <signal.h>
149 #include <stdio.h>
150 #include <string.h>
151 #include <errno.h>
152
153 #ifdef OPENSSL_SYS_VMS          /* prototypes for sys$whatever */
154 # include <starlet.h>
155 # ifdef __DECC
156 #  pragma message disable DOLLARID
157 # endif
158 #endif
159
160 #ifdef WIN_CONSOLE_BUG
161 # include <windows.h>
162 # include <wincon.h>
163 #endif
164
165
166 /* There are 5 types of terminal interface supported,
167  * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
168  */
169
170 #if defined(__sgi) && !defined(TERMIOS)
171 # define TERMIOS
172 # undef  TERMIO
173 # undef  SGTTY
174 #endif
175
176 #if defined(linux) && !defined(TERMIO)
177 # undef  TERMIOS
178 # define TERMIO
179 # undef  SGTTY
180 #endif
181
182 #ifdef _LIBC
183 # undef  TERMIOS
184 # define TERMIO
185 # undef  SGTTY
186 #endif
187
188 #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(MAC_OS_GUSI_SOURCE)
189 # undef  TERMIOS
190 # undef  TERMIO
191 # define SGTTY
192 #endif
193
194 #ifdef TERMIOS
195 # include <termios.h>
196 # define TTY_STRUCT             struct termios
197 # define TTY_FLAGS              c_lflag
198 # define TTY_get(tty,data)      tcgetattr(tty,data)
199 # define TTY_set(tty,data)      tcsetattr(tty,TCSANOW,data)
200 #endif
201
202 #ifdef TERMIO
203 # include <termio.h>
204 # define TTY_STRUCT             struct termio
205 # define TTY_FLAGS              c_lflag
206 # define TTY_get(tty,data)      ioctl(tty,TCGETA,data)
207 # define TTY_set(tty,data)      ioctl(tty,TCSETA,data)
208 #endif
209
210 #ifdef SGTTY
211 # include <sgtty.h>
212 # define TTY_STRUCT             struct sgttyb
213 # define TTY_FLAGS              sg_flags
214 # define TTY_get(tty,data)      ioctl(tty,TIOCGETP,data)
215 # define TTY_set(tty,data)      ioctl(tty,TIOCSETP,data)
216 #endif
217
218 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
219 # include <sys/ioctl.h>
220 #endif
221
222 #ifdef OPENSSL_SYS_MSDOS
223 # include <conio.h>
224 #endif
225
226 #ifdef OPENSSL_SYS_VMS
227 # include <ssdef.h>
228 # include <iodef.h>
229 # include <ttdef.h>
230 # include <descrip.h>
231 struct IOSB {
232         short iosb$w_value;
233         short iosb$w_count;
234         long  iosb$l_info;
235         };
236 #endif
237
238 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE)
239 /*
240  * This one needs work. As a matter of fact the code is unoperational
241  * and this is only a trick to get it compiled.
242  *                                      <appro@fy.chalmers.se>
243  */
244 # define TTY_STRUCT int
245 #endif
246
247 #ifndef NX509_SIG
248 # define NX509_SIG 32
249 #endif
250
251
252 /* Define globals.  They are protected by a lock */
253 #ifdef SIGACTION
254 static struct sigaction savsig[NX509_SIG];
255 #else
256 static void (*savsig[NX509_SIG])(int );
257 #endif
258
259 #ifdef OPENSSL_SYS_VMS
260 static struct IOSB iosb;
261 static $DESCRIPTOR(terminal,"TT");
262 static long tty_orig[3], tty_new[3]; /* XXX   Is there any guarantee that this will always suffice for the actual structures? */
263 static long status;
264 static unsigned short channel = 0;
265 #else
266 #ifndef OPENSSL_SYS_MSDOS
267 static TTY_STRUCT tty_orig,tty_new;
268 #endif
269 #endif
270 static FILE *tty_in, *tty_out;
271 static int is_a_tty;
272
273 /* Declare static functions */
274 static void read_till_nl(FILE *);
275 static void recsig(int);
276 static void pushsig(void);
277 static void popsig(void);
278 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
279 static int noecho_fgets(char *buf, int size, FILE *tty);
280 #endif
281 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
282
283 static int read_string(UI *ui, UI_STRING *uis);
284 static int write_string(UI *ui, UI_STRING *uis);
285
286 static int open_console(UI *ui);
287 static int echo_console(UI *ui);
288 static int noecho_console(UI *ui);
289 static int close_console(UI *ui);
290
291 static UI_METHOD ui_openssl =
292         {
293         "OpenSSL default user interface",
294         open_console,
295         write_string,
296         NULL,                   /* No flusher is needed for command lines */
297         read_string,
298         close_console,
299         NULL
300         };
301
302 /* The method with all the built-in thingies */
303 UI_METHOD *UI_OpenSSL(void)
304         {
305         return &ui_openssl;
306         }
307
308 /* The following function makes sure that info and error strings are printed
309    before any prompt. */
310 static int write_string(UI *ui, UI_STRING *uis)
311         {
312         switch (UI_get_string_type(uis))
313                 {
314         case UIT_ERROR:
315         case UIT_INFO:
316                 fputs(UI_get0_output_string(uis), tty_out);
317                 fflush(tty_out);
318                 break;
319         default:
320                 break;
321                 }
322         return 1;
323         }
324
325 static int read_string(UI *ui, UI_STRING *uis)
326         {
327         int ok = 0;
328
329         switch (UI_get_string_type(uis))
330                 {
331         case UIT_BOOLEAN:
332                 fputs(UI_get0_output_string(uis), tty_out);
333                 fputs(UI_get0_action_string(uis), tty_out);
334                 fflush(tty_out);
335                 return read_string_inner(ui, uis,
336                         UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0);
337         case UIT_PROMPT:
338                 fputs(UI_get0_output_string(uis), tty_out);
339                 fflush(tty_out);
340                 return read_string_inner(ui, uis,
341                         UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1);
342         case UIT_VERIFY:
343                 fprintf(tty_out,"Verifying - %s",
344                         UI_get0_output_string(uis));
345                 fflush(tty_out);
346                 if ((ok = read_string_inner(ui, uis,
347                         UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1)) <= 0)
348                         return ok;
349                 if (strcmp(UI_get0_result_string(uis),
350                         UI_get0_test_string(uis)) != 0)
351                         {
352                         fprintf(tty_out,"Verify failure\n");
353                         fflush(tty_out);
354                         return 0;
355                         }
356                 break;
357         default:
358                 break;
359                 }
360         return 1;
361         }
362
363
364 /* Internal functions to read a string without echoing */
365 static void read_till_nl(FILE *in)
366         {
367 #define SIZE 4
368         char buf[SIZE+1];
369
370         do      {
371                 fgets(buf,SIZE,in);
372                 } while (strchr(buf,'\n') == NULL);
373         }
374
375 static sig_atomic_t intr_signal;
376
377 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
378         {
379         static int ps;
380         int ok;
381         char result[BUFSIZ];
382         int maxsize = BUFSIZ-1;
383         char *p;
384
385 #ifndef OPENSSL_SYS_WIN16
386         intr_signal=0;
387         ok=0;
388         ps=0;
389
390         pushsig();
391         ps=1;
392
393         if (!echo && !noecho_console(ui))
394                 goto error;
395         ps=2;
396
397         result[0]='\0';
398 #ifdef OPENSSL_SYS_MSDOS
399         if (!echo)
400                 {
401                 noecho_fgets(result,maxsize,tty_in);
402                 p=result; /* FIXME: noecho_fgets doesn't return errors */
403                 }
404         else
405                 p=fgets(result,maxsize,tty_in);
406 #else
407         p=fgets(result,maxsize,tty_in);
408 #endif
409         if(!p)
410                 goto error;
411         if (feof(tty_in)) goto error;
412         if (ferror(tty_in)) goto error;
413         if ((p=(char *)strchr(result,'\n')) != NULL)
414                 {
415                 if (strip_nl)
416                         *p='\0';
417                 }
418         else
419                 read_till_nl(tty_in);
420         if (UI_set_result(ui, uis, result) >= 0)
421                 ok=1;
422
423 error:
424         if (intr_signal == SIGINT)
425                 ok=-1;
426         if (!echo) fprintf(tty_out,"\n");
427         if (ps >= 2 && !echo && !echo_console(ui))
428                 ok=0;
429
430         if (ps >= 1)
431                 popsig();
432 #else
433         ok=1;
434 #endif
435
436         memset(result,0,BUFSIZ);
437         return ok;
438         }
439
440
441 /* Internal functions to open, handle and close a channel to the console.  */
442 static int open_console(UI *ui)
443         {
444         CRYPTO_w_lock(CRYPTO_LOCK_UI);
445         is_a_tty = 1;
446
447 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
448         tty_in=stdin;
449         tty_out=stderr;
450 #else
451 #  ifdef OPENSSL_SYS_MSDOS
452 #    define DEV_TTY "con"
453 #  else
454 #    define DEV_TTY "/dev/tty"
455 #  endif
456         if ((tty_in=fopen(DEV_TTY,"r")) == NULL)
457                 tty_in=stdin;
458         if ((tty_out=fopen(DEV_TTY,"w")) == NULL)
459                 tty_out=stderr;
460 #endif
461
462 #if defined(TTY_get) && !defined(VMS)
463         if (TTY_get(fileno(tty_in),&tty_orig) == -1)
464                 {
465 #ifdef ENOTTY
466                 if (errno == ENOTTY)
467                         is_a_tty=0;
468                 else
469 #endif
470 #ifdef EINVAL
471                 /* Ariel Glenn ariel@columbia.edu reports that solaris
472                  * can return EINVAL instead.  This should be ok */
473                 if (errno == EINVAL)
474                         is_a_tty=0;
475                 else
476 #endif
477                         return 0;
478                 }
479 #endif
480 #ifdef OPENSSL_SYS_VMS
481         status = sys$assign(&terminal,&channel,0,0);
482         if (status != SS$_NORMAL)
483                 return 0;
484         status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
485         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
486                 return 0;
487 #endif
488         return 1;
489         }
490
491 static int noecho_console(UI *ui)
492         {
493 #ifdef TTY_FLAGS
494         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
495         tty_new.TTY_FLAGS &= ~ECHO;
496 #endif
497
498 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
499         if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
500                 return 0;
501 #endif
502 #ifdef OPENSSL_SYS_VMS
503         tty_new[0] = tty_orig[0];
504         tty_new[1] = tty_orig[1] | TT$M_NOECHO;
505         tty_new[2] = tty_orig[2];
506         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
507         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
508                 return 0;
509 #endif
510         return 1;
511         }
512
513 static int echo_console(UI *ui)
514         {
515 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
516         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
517         tty_new.TTY_FLAGS |= ECHO;
518 #endif
519
520 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
521         if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
522                 return 0;
523 #endif
524 #ifdef OPENSSL_SYS_VMS
525         tty_new[0] = tty_orig[0];
526         tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
527         tty_new[2] = tty_orig[2];
528         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
529         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
530                 return 0;
531 #endif
532         return 1;
533         }
534
535 static int close_console(UI *ui)
536         {
537         if (tty_in != stderr) fclose(tty_in);
538         if (tty_out != stderr) fclose(tty_out);
539 #ifdef OPENSSL_SYS_VMS
540         status = sys$dassgn(channel);
541 #endif
542         CRYPTO_w_unlock(CRYPTO_LOCK_UI);
543
544         return 1;
545         }
546
547
548 /* Internal functions to handle signals and act on them */
549 static void pushsig(void)
550         {
551         int i;
552 #ifdef SIGACTION
553         struct sigaction sa;
554
555         memset(&sa,0,sizeof sa);
556         sa.sa_handler=recsig;
557 #endif
558
559         for (i=1; i<NX509_SIG; i++)
560                 {
561 #ifdef SIGUSR1
562                 if (i == SIGUSR1)
563                         continue;
564 #endif
565 #ifdef SIGUSR2
566                 if (i == SIGUSR2)
567                         continue;
568 #endif
569 #ifdef SIGKILL
570                 if (i == SIGKILL) /* We can't make any action on that. */
571                         continue;
572 #endif
573 #ifdef SIGACTION
574                 sigaction(i,&sa,&savsig[i]);
575 #else
576                 savsig[i]=signal(i,recsig);
577 #endif
578                 }
579
580 #ifdef SIGWINCH
581         signal(SIGWINCH,SIG_DFL);
582 #endif
583         }
584
585 static void popsig(void)
586         {
587         int i;
588
589         for (i=1; i<NX509_SIG; i++)
590                 {
591 #ifdef SIGUSR1
592                 if (i == SIGUSR1)
593                         continue;
594 #endif
595 #ifdef SIGUSR2
596                 if (i == SIGUSR2)
597                         continue;
598 #endif
599 #ifdef SIGACTION
600                 sigaction(i,&savsig[i],NULL);
601 #else
602                 signal(i,savsig[i]);
603 #endif
604                 }
605         }
606
607 static void recsig(int i)
608         {
609         intr_signal=i;
610         }
611
612 /* Internal functions specific for Windows */
613 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
614 static int noecho_fgets(char *buf, int size, FILE *tty)
615         {
616         int i;
617         char *p;
618
619         p=buf;
620         for (;;)
621                 {
622                 if (size == 0)
623                         {
624                         *p='\0';
625                         break;
626                         }
627                 size--;
628 #ifdef WIN16TTY
629                 i=_inchar();
630 #else
631                 i=getch();
632 #endif
633                 if (i == '\r') i='\n';
634                 *(p++)=i;
635                 if (i == '\n')
636                         {
637                         *p='\0';
638                         break;
639                         }
640                 }
641 #ifdef WIN_CONSOLE_BUG
642 /* Win95 has several evil console bugs: one of these is that the
643  * last character read using getch() is passed to the next read: this is
644  * usually a CR so this can be trouble. No STDIO fix seems to work but
645  * flushing the console appears to do the trick.
646  */
647                 {
648                         HANDLE inh;
649                         inh = GetStdHandle(STD_INPUT_HANDLE);
650                         FlushConsoleInputBuffer(inh);
651                 }
652 #endif
653         return(strlen(buf));
654         }
655 #endif