Typo.
[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         if ((tty=fopen("con","w+")) == NULL)
434                 tty=stdin;
435 #elif defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
436         tty=stdin;
437 #else
438         if ((tty=fopen("/dev/tty","w+")) == NULL)
439                 tty=stdin;
440 #endif
441
442 #if defined(TTY_get) && !defined(VMS)
443         if (TTY_get(fileno(tty),&tty_orig) == -1)
444                 {
445 #ifdef ENOTTY
446                 if (errno == ENOTTY)
447                         is_a_tty=0;
448                 else
449 #endif
450 #ifdef EINVAL
451                 /* Ariel Glenn ariel@columbia.edu reports that solaris
452                  * can return EINVAL instead.  This should be ok */
453                 if (errno == EINVAL)
454                         is_a_tty=0;
455                 else
456 #endif
457                         return 0;
458                 }
459 #endif
460 #ifdef OPENSSL_SYS_VMS
461         status = sys$assign(&terminal,&channel,0,0);
462         if (status != SS$_NORMAL)
463                 return 0;
464         status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
465         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
466                 return 0;
467 #endif
468         return 1;
469         }
470
471 static int noecho_console(UI *ui)
472         {
473 #ifdef TTY_FLAGS
474         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
475         tty_new.TTY_FLAGS &= ~ECHO;
476 #endif
477
478 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
479         if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
480                 return 0;
481 #endif
482 #ifdef OPENSSL_SYS_VMS
483         tty_new[0] = tty_orig[0];
484         tty_new[1] = tty_orig[1] | TT$M_NOECHO;
485         tty_new[2] = tty_orig[2];
486         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
487         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
488                 return 0;
489 #endif
490         return 1;
491         }
492
493 static int echo_console(UI *ui)
494         {
495 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
496         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
497         tty_new.TTY_FLAGS |= ECHO;
498 #endif
499
500 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
501         if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
502                 return 0;
503 #endif
504 #ifdef OPENSSL_SYS_VMS
505         tty_new[0] = tty_orig[0];
506         tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
507         tty_new[2] = tty_orig[2];
508         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
509         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
510                 return 0;
511 #endif
512         return 1;
513         }
514
515 static int close_console(UI *ui)
516         {
517         if (stdin != tty) fclose(tty);
518 #ifdef OPENSSL_SYS_VMS
519         status = sys$dassgn(channel);
520 #endif
521         CRYPTO_w_unlock(CRYPTO_LOCK_UI);
522
523         return 1;
524         }
525
526
527 /* Internal functions to handle signals and act on them */
528 static void pushsig(void)
529         {
530         int i;
531 #ifdef SIGACTION
532         struct sigaction sa;
533
534         memset(&sa,0,sizeof sa);
535         sa.sa_handler=recsig;
536 #endif
537
538         for (i=1; i<NX509_SIG; i++)
539                 {
540 #ifdef SIGUSR1
541                 if (i == SIGUSR1)
542                         continue;
543 #endif
544 #ifdef SIGUSR2
545                 if (i == SIGUSR2)
546                         continue;
547 #endif
548 #ifdef SIGKILL
549                 if (i == SIGKILL) /* We can't make any action on that. */
550                         continue;
551 #endif
552 #ifdef SIGACTION
553                 sigaction(i,&sa,&savsig[i]);
554 #else
555                 savsig[i]=signal(i,recsig);
556 #endif
557                 }
558
559 #ifdef SIGWINCH
560         signal(SIGWINCH,SIG_DFL);
561 #endif
562         }
563
564 static void popsig(void)
565         {
566         int i;
567
568         for (i=1; i<NX509_SIG; i++)
569                 {
570 #ifdef SIGUSR1
571                 if (i == SIGUSR1)
572                         continue;
573 #endif
574 #ifdef SIGUSR2
575                 if (i == SIGUSR2)
576                         continue;
577 #endif
578 #ifdef SIGACTION
579                 sigaction(i,&savsig[i],NULL);
580 #else
581                 signal(i,savsig[i]);
582 #endif
583                 }
584         }
585
586 static void recsig(int i)
587         {
588         longjmp(save,1);
589 #ifdef LINT
590         i=i;
591 #endif
592         }
593
594
595 /* Internal functions specific for Windows */
596 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
597 static int noecho_fgets(char *buf, int size, FILE *tty)
598         {
599         int i;
600         char *p;
601
602         p=buf;
603         for (;;)
604                 {
605                 if (size == 0)
606                         {
607                         *p='\0';
608                         break;
609                         }
610                 size--;
611 #ifdef WIN16TTY
612                 i=_inchar();
613 #else
614                 i=getch();
615 #endif
616                 if (i == '\r') i='\n';
617                 *(p++)=i;
618                 if (i == '\n')
619                         {
620                         *p='\0';
621                         break;
622                         }
623                 }
624 #ifdef WIN_CONSOLE_BUG
625 /* Win95 has several evil console bugs: one of these is that the
626  * last character read using getch() is passed to the next read: this is
627  * usually a CR so this can be trouble. No STDIO fix seems to work but
628  * flushing the console appears to do the trick.
629  */
630                 {
631                         HANDLE inh;
632                         inh = GetStdHandle(STD_INPUT_HANDLE);
633                         FlushConsoleInputBuffer(inh);
634                 }
635 #endif
636         return(strlen(buf));
637         }
638 #endif