bb66291ad5906f089701eeb2a0d63fb41c3532f9
[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
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_out,"Verifying - %s",
313                         UI_get0_output_string(uis));
314                 fflush(tty_out);
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_out,"Verify failure\n");
321                         fflush(tty_out);
322                         return 0;
323                         }
324                 break;
325         case UI_VERIFY_ECHO:
326                 fprintf(tty_out,"Verifying - %s",
327                         UI_get0_output_string(uis));
328                 fflush(tty_out);
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_out,"Verify failure\n");
335                         fflush(tty_out);
336                         return 0;
337                         }
338                 break;
339         case UI_STRING_NOECHO:
340                 fputs(UI_get0_output_string(uis), tty_out);
341                 fflush(tty_out);
342                 return read_string_inner(ui, uis, 0);
343         case UI_STRING_ECHO:
344                 fputs(UI_get0_output_string(uis), tty_out);
345                 fflush(tty_out);
346                 return read_string_inner(ui, uis, 1);
347         default:
348                 fputs(UI_get0_output_string(uis), tty_out);
349                 fflush(tty_out);
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_in);
397                 else
398                         fgets(result,maxsize,tty_in);
399 #else
400                 fgets(result,maxsize,tty_in);
401 #endif
402                 if (feof(tty_in)) goto error;
403                 if (ferror(tty_in)) goto error;
404                 if ((p=(char *)strchr(result,'\n')) != NULL)
405                         *p='\0';
406                 else
407                         read_till_nl(tty_in);
408                 if (UI_set_result(uis, result) >= 0)
409                         ok=1;
410                 }
411
412 error:
413         if (!echo) fprintf(tty_out,"\n");
414         if (ps >= 2 && !echo)
415                 echo_console(ui);
416
417         if (ps >= 1)
418                 popsig();
419 #else
420         memset(result,0,BUFSIZ);
421         ok=1;
422 #endif
423
424         OPENSSL_free(result);
425         return ok;
426         }
427
428
429 /* Internal functions to open, handle and close a channel to the console.  */
430 static int open_console(UI *ui)
431         {
432         CRYPTO_w_lock(CRYPTO_LOCK_UI);
433         is_a_tty = 1;
434
435 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
436         tty_in=stdin;
437         tty_out=stderr;
438 #else
439 #  ifdef OPENSSL_SYS_MSDOS
440 #    define DEV_TTY "con"
441 #  else
442 #    define DEV_TTY "/dev/tty"
443 #  endif
444         if ((tty_in=fopen(DEV_TTY,"r")) == NULL)
445                 tty_in=stdin;
446         if ((tty_out=fopen(DEV_TTY,"w")) == NULL)
447                 tty_out=stderr;
448 #endif
449
450 #if defined(TTY_get) && !defined(VMS)
451         if (TTY_get(fileno(tty_in),&tty_orig) == -1)
452                 {
453 #ifdef ENOTTY
454                 if (errno == ENOTTY)
455                         is_a_tty=0;
456                 else
457 #endif
458 #ifdef EINVAL
459                 /* Ariel Glenn ariel@columbia.edu reports that solaris
460                  * can return EINVAL instead.  This should be ok */
461                 if (errno == EINVAL)
462                         is_a_tty=0;
463                 else
464 #endif
465                         return 0;
466                 }
467 #endif
468 #ifdef OPENSSL_SYS_VMS
469         status = sys$assign(&terminal,&channel,0,0);
470         if (status != SS$_NORMAL)
471                 return 0;
472         status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
473         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
474                 return 0;
475 #endif
476         return 1;
477         }
478
479 static int noecho_console(UI *ui)
480         {
481 #ifdef TTY_FLAGS
482         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
483         tty_new.TTY_FLAGS &= ~ECHO;
484 #endif
485
486 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
487         if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
488                 return 0;
489 #endif
490 #ifdef OPENSSL_SYS_VMS
491         tty_new[0] = tty_orig[0];
492         tty_new[1] = tty_orig[1] | TT$M_NOECHO;
493         tty_new[2] = tty_orig[2];
494         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
495         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
496                 return 0;
497 #endif
498         return 1;
499         }
500
501 static int echo_console(UI *ui)
502         {
503 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
504         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
505         tty_new.TTY_FLAGS |= ECHO;
506 #endif
507
508 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
509         if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
510                 return 0;
511 #endif
512 #ifdef OPENSSL_SYS_VMS
513         tty_new[0] = tty_orig[0];
514         tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
515         tty_new[2] = tty_orig[2];
516         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
517         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
518                 return 0;
519 #endif
520         return 1;
521         }
522
523 static int close_console(UI *ui)
524         {
525         if (tty_in != stderr) fclose(tty_in);
526         if (tty_out != stderr) fclose(tty_out);
527 #ifdef OPENSSL_SYS_VMS
528         status = sys$dassgn(channel);
529 #endif
530         CRYPTO_w_unlock(CRYPTO_LOCK_UI);
531
532         return 1;
533         }
534
535
536 /* Internal functions to handle signals and act on them */
537 static void pushsig(void)
538         {
539         int i;
540 #ifdef SIGACTION
541         struct sigaction sa;
542
543         memset(&sa,0,sizeof sa);
544         sa.sa_handler=recsig;
545 #endif
546
547         for (i=1; i<NX509_SIG; i++)
548                 {
549 #ifdef SIGUSR1
550                 if (i == SIGUSR1)
551                         continue;
552 #endif
553 #ifdef SIGUSR2
554                 if (i == SIGUSR2)
555                         continue;
556 #endif
557 #ifdef SIGKILL
558                 if (i == SIGKILL) /* We can't make any action on that. */
559                         continue;
560 #endif
561 #ifdef SIGACTION
562                 sigaction(i,&sa,&savsig[i]);
563 #else
564                 savsig[i]=signal(i,recsig);
565 #endif
566                 }
567
568 #ifdef SIGWINCH
569         signal(SIGWINCH,SIG_DFL);
570 #endif
571         }
572
573 static void popsig(void)
574         {
575         int i;
576
577         for (i=1; i<NX509_SIG; i++)
578                 {
579 #ifdef SIGUSR1
580                 if (i == SIGUSR1)
581                         continue;
582 #endif
583 #ifdef SIGUSR2
584                 if (i == SIGUSR2)
585                         continue;
586 #endif
587 #ifdef SIGACTION
588                 sigaction(i,&savsig[i],NULL);
589 #else
590                 signal(i,savsig[i]);
591 #endif
592                 }
593         }
594
595 static void recsig(int i)
596         {
597         longjmp(save,1);
598 #ifdef LINT
599         i=i;
600 #endif
601         }
602
603
604 /* Internal functions specific for Windows */
605 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
606 static int noecho_fgets(char *buf, int size, FILE *tty)
607         {
608         int i;
609         char *p;
610
611         p=buf;
612         for (;;)
613                 {
614                 if (size == 0)
615                         {
616                         *p='\0';
617                         break;
618                         }
619                 size--;
620 #ifdef WIN16TTY
621                 i=_inchar();
622 #else
623                 i=getch();
624 #endif
625                 if (i == '\r') i='\n';
626                 *(p++)=i;
627                 if (i == '\n')
628                         {
629                         *p='\0';
630                         break;
631                         }
632                 }
633 #ifdef WIN_CONSOLE_BUG
634 /* Win95 has several evil console bugs: one of these is that the
635  * last character read using getch() is passed to the next read: this is
636  * usually a CR so this can be trouble. No STDIO fix seems to work but
637  * flushing the console appears to do the trick.
638  */
639                 {
640                         HANDLE inh;
641                         inh = GetStdHandle(STD_INPUT_HANDLE);
642                         FlushConsoleInputBuffer(inh);
643                 }
644 #endif
645         return(strlen(buf));
646         }
647 #endif