This fixes "Spurious test failures on IRIX?" reported in April. Apparently
[openssl.git] / crypto / ui / ui_openssl.c
1 /* crypto/ui/ui_openssl.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Written by Richard Levitte (levitte@stacken.kth.se) and others
3  * for the OpenSSL project 2000/2001.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2000 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 <setjmp.h>
152 #include <errno.h>
153
154 #ifdef OPENSSL_SYS_VMS          /* prototypes for sys$whatever */
155 # include <starlet.h>
156 # ifdef __DECC
157 #  pragma message disable DOLLARID
158 # endif
159 #endif
160
161 #ifdef WIN_CONSOLE_BUG
162 # include <windows.h>
163 # include <wincon.h>
164 #endif
165
166
167 /* There are 5 types of terminal interface supported,
168  * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
169  */
170
171 #if defined(__sgi) && !defined(TERMIOS)
172 # define TERMIOS
173 # undef  TERMIO
174 # undef  SGTTY
175 #endif
176
177 #if defined(linux) && !defined(TERMIO)
178 # undef  TERMIOS
179 # define TERMIO
180 # undef  SGTTY
181 #endif
182
183 #ifdef _LIBC
184 # undef  TERMIOS
185 # define TERMIO
186 # undef  SGTTY
187 #endif
188
189 #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(MAC_OS_GUSI_SOURCE)
190 # undef  TERMIOS
191 # undef  TERMIO
192 # define SGTTY
193 #endif
194
195 #ifdef TERMIOS
196 # include <termios.h>
197 # define TTY_STRUCT             struct termios
198 # define TTY_FLAGS              c_lflag
199 # define TTY_get(tty,data)      tcgetattr(tty,data)
200 # define TTY_set(tty,data)      tcsetattr(tty,TCSANOW,data)
201 #endif
202
203 #ifdef TERMIO
204 # include <termio.h>
205 # define TTY_STRUCT             struct termio
206 # define TTY_FLAGS              c_lflag
207 # define TTY_get(tty,data)      ioctl(tty,TCGETA,data)
208 # define TTY_set(tty,data)      ioctl(tty,TCSETA,data)
209 #endif
210
211 #ifdef SGTTY
212 # include <sgtty.h>
213 # define TTY_STRUCT             struct sgttyb
214 # define TTY_FLAGS              sg_flags
215 # define TTY_get(tty,data)      ioctl(tty,TIOCGETP,data)
216 # define TTY_set(tty,data)      ioctl(tty,TIOCSETP,data)
217 #endif
218
219 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
220 # include <sys/ioctl.h>
221 #endif
222
223 #ifdef OPENSSL_SYS_MSDOS
224 # include <conio.h>
225 #endif
226
227 #ifdef OPENSSL_SYS_VMS
228 # include <ssdef.h>
229 # include <iodef.h>
230 # include <ttdef.h>
231 # include <descrip.h>
232 struct IOSB {
233         short iosb$w_value;
234         short iosb$w_count;
235         long  iosb$l_info;
236         };
237 #endif
238
239 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE)
240 /*
241  * This one needs work. As a matter of fact the code is unoperational
242  * and this is only a trick to get it compiled.
243  *                                      <appro@fy.chalmers.se>
244  */
245 # define TTY_STRUCT int
246 #endif
247
248 #ifndef NX509_SIG
249 # define NX509_SIG 32
250 #endif
251
252
253 /* Define globals.  They are protected by a lock */
254 #ifdef SIGACTION
255 static struct sigaction savsig[NX509_SIG];
256 #else
257 static void (*savsig[NX509_SIG])(int );
258 #endif
259 static jmp_buf save;
260
261 #ifdef OPENSSL_SYS_VMS
262 static struct IOSB iosb;
263 static $DESCRIPTOR(terminal,"TT");
264 static long tty_orig[3], tty_new[3]; /* XXX   Is there any guarantee that this will always suffice for the actual structures? */
265 static long status;
266 static unsigned short channel = 0;
267 #else
268 #ifndef OPENSSL_SYS_MSDOS
269 static TTY_STRUCT tty_orig,tty_new;
270 #endif
271 #endif
272 static FILE *tty_in, *tty_out;
273 static int is_a_tty;
274
275 /* Declare static functions */
276 static void read_till_nl(FILE *);
277 static void recsig(int);
278 static void pushsig(void);
279 static void popsig(void);
280 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
281 static int noecho_fgets(char *buf, int size, FILE *tty);
282 #endif
283 static int read_string_inner(UI *ui, UI_STRING *uis, int echo);
284
285 static int read_string(UI *ui, UI_STRING *uis);
286 static int write_string(UI *ui, UI_STRING *uis);
287
288 static int open_console(UI *ui);
289 static int echo_console(UI *ui);
290 static int noecho_console(UI *ui);
291 static int close_console(UI *ui);
292
293 static UI_METHOD ui_openssl =
294         {
295         "OpenSSL default user interface",
296         open_console,
297         write_string,
298         NULL,                   /* No flusher is needed for command lines */
299         read_string,
300         close_console,
301         NULL
302         };
303
304 /* The method with all the built-in thingies */
305 UI_METHOD *UI_OpenSSL(void)
306         {
307         return &ui_openssl;
308         }
309
310 /* The following function makes sure that info and error strings are printed
311    before any prompt. */
312 static int write_string(UI *ui, UI_STRING *uis)
313         {
314         switch (UI_get_string_type(uis))
315                 {
316         case UIT_VERIFY:
317         case UIT_PROMPT:
318                 break;
319         default:
320                 fputs(UI_get0_output_string(uis), tty_out);
321                 fflush(tty_out);
322                 break;
323                 }
324         return 1;
325         }
326
327 static int read_string(UI *ui, UI_STRING *uis)
328         {
329         int ok = 0;
330
331         switch (UI_get_string_type(uis))
332                 {
333         case UIT_PROMPT:
334                 fputs(UI_get0_output_string(uis), tty_out);
335                 fflush(tty_out);
336                 return read_string_inner(ui, uis,
337                         UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO);
338         case UIT_VERIFY:
339                 fprintf(tty_out,"Verifying - %s",
340                         UI_get0_output_string(uis));
341                 fflush(tty_out);
342                 if ((ok = read_string_inner(ui, uis,
343                         UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO)) <= 0)
344                         return ok;
345                 if (strcmp(UI_get0_result_string(uis),
346                         UI_get0_test_string(uis)) != 0)
347                         {
348                         fprintf(tty_out,"Verify failure\n");
349                         fflush(tty_out);
350                         return 0;
351                         }
352                 break;
353         default:
354                 break;
355                 }
356         return 1;
357         }
358
359
360 /* Internal functions to read a string without echoing */
361 static void read_till_nl(FILE *in)
362         {
363 #define SIZE 4
364         char buf[SIZE+1];
365
366         do      {
367                 fgets(buf,SIZE,in);
368                 } while (strchr(buf,'\n') == NULL);
369         }
370
371 static int read_string_inner(UI *ui, UI_STRING *uis, int echo)
372         {
373         static int ps;
374         int ok;
375         char *result = OPENSSL_malloc(BUFSIZ);
376         int maxsize = BUFSIZ-1;
377
378 #ifndef OPENSSL_SYS_WIN16
379         if ((ok = setjmp(save)))
380                 {
381                 if (ok == 1) ok=0;
382                 goto error;
383                 }
384         ok=0;
385         ps=0;
386
387         pushsig();
388         ps=1;
389
390         if (!echo) noecho_console(ui);
391         ps=2;
392
393         while (!ok)
394                 {
395                 char *p;
396
397                 result[0]='\0';
398 #ifdef OPENSSL_SYS_MSDOS
399                 if (!echo)
400                         noecho_fgets(result,maxsize,tty_in);
401                 else
402                         fgets(result,maxsize,tty_in);
403 #else
404                 fgets(result,maxsize,tty_in);
405 #endif
406                 if (feof(tty_in)) goto error;
407                 if (ferror(tty_in)) goto error;
408                 if ((p=(char *)strchr(result,'\n')) != NULL)
409                         *p='\0';
410                 else
411                         read_till_nl(tty_in);
412                 if (UI_set_result(uis, result) >= 0)
413                         ok=1;
414                 }
415
416 error:
417         if (!echo) fprintf(tty_out,"\n");
418         if (ps >= 2 && !echo)
419                 echo_console(ui);
420
421         if (ps >= 1)
422                 popsig();
423 #else
424         memset(result,0,BUFSIZ);
425         ok=1;
426 #endif
427
428         OPENSSL_free(result);
429         return ok;
430         }
431
432
433 /* Internal functions to open, handle and close a channel to the console.  */
434 static int open_console(UI *ui)
435         {
436         CRYPTO_w_lock(CRYPTO_LOCK_UI);
437         is_a_tty = 1;
438
439 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
440         tty_in=stdin;
441         tty_out=stderr;
442 #else
443 #  ifdef OPENSSL_SYS_MSDOS
444 #    define DEV_TTY "con"
445 #  else
446 #    define DEV_TTY "/dev/tty"
447 #  endif
448         if ((tty_in=fopen(DEV_TTY,"r")) == NULL)
449                 tty_in=stdin;
450         if ((tty_out=fopen(DEV_TTY,"w")) == NULL)
451                 tty_out=stderr;
452 #endif
453
454 #if defined(TTY_get) && !defined(VMS)
455         if (TTY_get(fileno(tty_in),&tty_orig) == -1)
456                 {
457 #ifdef ENOTTY
458                 if (errno == ENOTTY)
459                         is_a_tty=0;
460                 else
461 #endif
462 #ifdef EINVAL
463                 /* Ariel Glenn ariel@columbia.edu reports that solaris
464                  * can return EINVAL instead.  This should be ok */
465                 if (errno == EINVAL)
466                         is_a_tty=0;
467                 else
468 #endif
469                         return 0;
470                 }
471 #endif
472 #ifdef OPENSSL_SYS_VMS
473         status = sys$assign(&terminal,&channel,0,0);
474         if (status != SS$_NORMAL)
475                 return 0;
476         status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
477         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
478                 return 0;
479 #endif
480         return 1;
481         }
482
483 static int noecho_console(UI *ui)
484         {
485 #ifdef TTY_FLAGS
486         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
487         tty_new.TTY_FLAGS &= ~ECHO;
488 #endif
489
490 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
491         if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
492                 return 0;
493 #endif
494 #ifdef OPENSSL_SYS_VMS
495         tty_new[0] = tty_orig[0];
496         tty_new[1] = tty_orig[1] | TT$M_NOECHO;
497         tty_new[2] = tty_orig[2];
498         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
499         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
500                 return 0;
501 #endif
502         return 1;
503         }
504
505 static int echo_console(UI *ui)
506         {
507 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
508         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
509         tty_new.TTY_FLAGS |= ECHO;
510 #endif
511
512 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
513         if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
514                 return 0;
515 #endif
516 #ifdef OPENSSL_SYS_VMS
517         tty_new[0] = tty_orig[0];
518         tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
519         tty_new[2] = tty_orig[2];
520         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
521         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
522                 return 0;
523 #endif
524         return 1;
525         }
526
527 static int close_console(UI *ui)
528         {
529         if (tty_in != stderr) fclose(tty_in);
530         if (tty_out != stderr) fclose(tty_out);
531 #ifdef OPENSSL_SYS_VMS
532         status = sys$dassgn(channel);
533 #endif
534         CRYPTO_w_unlock(CRYPTO_LOCK_UI);
535
536         return 1;
537         }
538
539
540 /* Internal functions to handle signals and act on them */
541 static void pushsig(void)
542         {
543         int i;
544 #ifdef SIGACTION
545         struct sigaction sa;
546
547         memset(&sa,0,sizeof sa);
548         sa.sa_handler=recsig;
549 #endif
550
551         for (i=1; i<NX509_SIG; i++)
552                 {
553 #ifdef SIGUSR1
554                 if (i == SIGUSR1)
555                         continue;
556 #endif
557 #ifdef SIGUSR2
558                 if (i == SIGUSR2)
559                         continue;
560 #endif
561 #ifdef SIGKILL
562                 if (i == SIGKILL) /* We can't make any action on that. */
563                         continue;
564 #endif
565 #ifdef SIGACTION
566                 sigaction(i,&sa,&savsig[i]);
567 #else
568                 savsig[i]=signal(i,recsig);
569 #endif
570                 }
571
572 #ifdef SIGWINCH
573         signal(SIGWINCH,SIG_DFL);
574 #endif
575         }
576
577 static void popsig(void)
578         {
579         int i;
580
581         for (i=1; i<NX509_SIG; i++)
582                 {
583 #ifdef SIGUSR1
584                 if (i == SIGUSR1)
585                         continue;
586 #endif
587 #ifdef SIGUSR2
588                 if (i == SIGUSR2)
589                         continue;
590 #endif
591 #ifdef SIGACTION
592                 sigaction(i,&savsig[i],NULL);
593 #else
594                 signal(i,savsig[i]);
595 #endif
596                 }
597         }
598
599 static void recsig(int i)
600         {
601         switch(i)
602                 {
603         case SIGINT:
604                 longjmp(save,-1);
605                 break;
606         default:
607                 break;
608                 }
609         longjmp(save,1);
610         }
611
612
613 /* Internal functions specific for Windows */
614 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
615 static int noecho_fgets(char *buf, int size, FILE *tty)
616         {
617         int i;
618         char *p;
619
620         p=buf;
621         for (;;)
622                 {
623                 if (size == 0)
624                         {
625                         *p='\0';
626                         break;
627                         }
628                 size--;
629 #ifdef WIN16TTY
630                 i=_inchar();
631 #else
632                 i=getch();
633 #endif
634                 if (i == '\r') i='\n';
635                 *(p++)=i;
636                 if (i == '\n')
637                         {
638                         *p='\0';
639                         break;
640                         }
641                 }
642 #ifdef WIN_CONSOLE_BUG
643 /* Win95 has several evil console bugs: one of these is that the
644  * last character read using getch() is passed to the next read: this is
645  * usually a CR so this can be trouble. No STDIO fix seems to work but
646  * flushing the console appears to do the trick.
647  */
648                 {
649                         HANDLE inh;
650                         inh = GetStdHandle(STD_INPUT_HANDLE);
651                         FlushConsoleInputBuffer(inh);
652                 }
653 #endif
654         return(strlen(buf));
655         }
656 #endif