Fix for new UI functions under Win32.
[oweals/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) for the OpenSSL
3  * project 2000.
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];
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;
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
287 static int open_console(UI *ui);
288 static int echo_console(UI *ui);
289 static int noecho_console(UI *ui);
290 static int close_console(UI *ui);
291
292 static UI_METHOD ui_openssl =
293         {
294         "OpenSSL default user interface",
295         open_console,
296         read_string,
297         NULL,                   /* The reader function writes as well */
298         close_console,
299         };
300
301 /* The method with all the built-in thingies */
302 UI_METHOD *UI_OpenSSL(void)
303         {
304         return &ui_openssl;
305         }
306
307 static int read_string(UI *ui, UI_STRING *uis)
308         {
309         switch (UI_get_string_type(uis))
310                 {
311         case UI_VERIFY_NOECHO:
312                 fprintf(tty,"Verifying - %s",
313                         UI_get0_output_string(uis));
314                 fflush(tty);
315                 if (read_string_inner(ui, uis, 0) == 0)
316                         return 0;
317                 if (strcmp(UI_get0_result_string(uis),
318                         UI_get0_test_string(uis)) != 0)
319                         {
320                         fprintf(tty,"Verify failure\n");
321                         fflush(tty);
322                         return 0;
323                         }
324                 break;
325         case UI_VERIFY_ECHO:
326                 fprintf(tty,"Verifying - %s",
327                         UI_get0_output_string(uis));
328                 fflush(tty);
329                 if (read_string_inner(ui, uis, 1) == 0)
330                         return 0;
331                 if (strcmp(UI_get0_result_string(uis),
332                         UI_get0_test_string(uis)) != 0)
333                         {
334                         fprintf(tty,"Verify failure\n");
335                         fflush(tty);
336                         return 0;
337                         }
338                 break;
339         case UI_STRING_NOECHO:
340                 fputs(UI_get0_output_string(uis), tty);
341                 fflush(tty);
342                 return read_string_inner(ui, uis, 0);
343         case UI_STRING_ECHO:
344                 fputs(UI_get0_output_string(uis), tty);
345                 fflush(tty);
346                 return read_string_inner(ui, uis, 1);
347         default:
348                 fputs(UI_get0_output_string(uis), tty);
349                 fflush(tty);
350                 break;
351                 }
352         return 1;
353         }
354
355
356 /* Internal functions to read a string without echoing */
357 static void read_till_nl(FILE *in)
358         {
359 #define SIZE 4
360         char buf[SIZE+1];
361
362         do      {
363                 fgets(buf,SIZE,in);
364                 } while (strchr(buf,'\n') == NULL);
365         }
366
367 static int read_string_inner(UI *ui, UI_STRING *uis, int echo)
368         {
369         static int ps;
370         int ok;
371         char *result = OPENSSL_malloc(BUFSIZ);
372         int maxsize = BUFSIZ-1;
373
374 #ifndef OPENSSL_SYS_WIN16
375         if (setjmp(save))
376                 {
377                 ok=0;
378                 goto error;
379                 }
380         ok=0;
381         ps=0;
382
383         pushsig();
384         ps=1;
385
386         if (!echo) noecho_console(ui);
387         ps=2;
388
389         while (!ok)
390                 {
391                 char *p;
392
393                 result[0]='\0';
394 #ifdef OPENSSL_SYS_MSDOS
395                 if (!echo)
396                         noecho_fgets(result,maxsize,tty);
397                 else
398 #endif
399                         fgets(result,maxsize,tty);
400                 if (feof(tty)) goto error;
401                 if (ferror(tty)) goto error;
402                 if ((p=(char *)strchr(result,'\n')) != NULL)
403                         *p='\0';
404                 else    read_till_nl(tty);
405                 if (UI_set_result(uis, result) >= 0)
406                         ok=1;
407                 }
408
409 error:
410         if (!echo) fprintf(tty,"\n");
411         if (ps >= 2 && !echo)
412                 echo_console(ui);
413
414         if (ps >= 1)
415                 popsig();
416 #else
417         memset(result,0,BUFSIZ);
418         ok=1;
419 #endif
420
421         OPENSSL_free(result);
422         return ok;
423         }
424
425
426 /* Internal functions to open, handle and close a channel to the console.  */
427 static int open_console(UI *ui)
428         {
429         CRYPTO_w_lock(CRYPTO_LOCK_UI);
430         is_a_tty = 1;
431
432 #ifdef OPENSSL_SYS_MSDOS
433         /* For some bizarre reason this call to fopen() on Windows 
434          * fails if the mode is "w+" or "r+", whereas "w" works fine.
435          */
436         if ((tty=fopen("con","w")) == NULL)
437                 tty=stdin;
438 #elif defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
439         tty=stdin;
440 #else
441         if ((tty=fopen("/dev/tty","w+")) == NULL)
442                 tty=stdin;
443 #endif
444
445 #if defined(TTY_get) && !defined(VMS)
446         if (TTY_get(fileno(tty),&tty_orig) == -1)
447                 {
448 #ifdef ENOTTY
449                 if (errno == ENOTTY)
450                         is_a_tty=0;
451                 else
452 #endif
453 #ifdef EINVAL
454                 /* Ariel Glenn ariel@columbia.edu reports that solaris
455                  * can return EINVAL instead.  This should be ok */
456                 if (errno == EINVAL)
457                         is_a_tty=0;
458                 else
459 #endif
460                         return 0;
461                 }
462 #endif
463 #ifdef OPENSSL_SYS_VMS
464         status = sys$assign(&terminal,&channel,0,0);
465         if (status != SS$_NORMAL)
466                 return 0;
467         status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
468         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
469                 return 0;
470 #endif
471         return 1;
472         }
473
474 static int noecho_console(UI *ui)
475         {
476 #ifdef TTY_FLAGS
477         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
478         tty_new.TTY_FLAGS &= ~ECHO;
479 #endif
480
481 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
482         if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
483                 return 0;
484 #endif
485 #ifdef OPENSSL_SYS_VMS
486         tty_new[0] = tty_orig[0];
487         tty_new[1] = tty_orig[1] | TT$M_NOECHO;
488         tty_new[2] = tty_orig[2];
489         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
490         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
491                 return 0;
492 #endif
493         return 1;
494         }
495
496 static int echo_console(UI *ui)
497         {
498 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
499         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
500         tty_new.TTY_FLAGS |= ECHO;
501 #endif
502
503 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
504         if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
505                 return 0;
506 #endif
507 #ifdef OPENSSL_SYS_VMS
508         tty_new[0] = tty_orig[0];
509         tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
510         tty_new[2] = tty_orig[2];
511         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
512         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
513                 return 0;
514 #endif
515         return 1;
516         }
517
518 static int close_console(UI *ui)
519         {
520         if (stdin != tty) fclose(tty);
521 #ifdef OPENSSL_SYS_VMS
522         status = sys$dassgn(channel);
523 #endif
524         CRYPTO_w_unlock(CRYPTO_LOCK_UI);
525
526         return 1;
527         }
528
529
530 /* Internal functions to handle signals and act on them */
531 static void pushsig(void)
532         {
533         int i;
534 #ifdef SIGACTION
535         struct sigaction sa;
536
537         memset(&sa,0,sizeof sa);
538         sa.sa_handler=recsig;
539 #endif
540
541         for (i=1; i<NX509_SIG; i++)
542                 {
543 #ifdef SIGUSR1
544                 if (i == SIGUSR1)
545                         continue;
546 #endif
547 #ifdef SIGUSR2
548                 if (i == SIGUSR2)
549                         continue;
550 #endif
551 #ifdef SIGKILL
552                 if (i == SIGKILL) /* We can't make any action on that. */
553                         continue;
554 #endif
555 #ifdef SIGACTION
556                 sigaction(i,&sa,&savsig[i]);
557 #else
558                 savsig[i]=signal(i,recsig);
559 #endif
560                 }
561
562 #ifdef SIGWINCH
563         signal(SIGWINCH,SIG_DFL);
564 #endif
565         }
566
567 static void popsig(void)
568         {
569         int i;
570
571         for (i=1; i<NX509_SIG; i++)
572                 {
573 #ifdef SIGUSR1
574                 if (i == SIGUSR1)
575                         continue;
576 #endif
577 #ifdef SIGUSR2
578                 if (i == SIGUSR2)
579                         continue;
580 #endif
581 #ifdef SIGACTION
582                 sigaction(i,&savsig[i],NULL);
583 #else
584                 signal(i,savsig[i]);
585 #endif
586                 }
587         }
588
589 static void recsig(int i)
590         {
591         longjmp(save,1);
592 #ifdef LINT
593         i=i;
594 #endif
595         }
596
597
598 /* Internal functions specific for Windows */
599 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
600 static int noecho_fgets(char *buf, int size, FILE *tty)
601         {
602         int i;
603         char *p;
604
605         p=buf;
606         for (;;)
607                 {
608                 if (size == 0)
609                         {
610                         *p='\0';
611                         break;
612                         }
613                 size--;
614 #ifdef WIN16TTY
615                 i=_inchar();
616 #else
617                 i=getch();
618 #endif
619                 if (i == '\r') i='\n';
620                 *(p++)=i;
621                 if (i == '\n')
622                         {
623                         *p='\0';
624                         break;
625                         }
626                 }
627 #ifdef WIN_CONSOLE_BUG
628 /* Win95 has several evil console bugs: one of these is that the
629  * last character read using getch() is passed to the next read: this is
630  * usually a CR so this can be trouble. No STDIO fix seems to work but
631  * flushing the console appears to do the trick.
632  */
633                 {
634                         HANDLE inh;
635                         inh = GetStdHandle(STD_INPUT_HANDLE);
636                         FlushConsoleInputBuffer(inh);
637                 }
638 #endif
639         return(strlen(buf));
640         }
641 #endif