Exdata test was never enabled.
[oweals/openssl.git] / crypto / ui / ui_openssl.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/e_os2.h>
11 #include <openssl/err.h>
12
13 /*
14  * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
15  * [maybe others?], because it masks interfaces not discussed in standard,
16  * sigaction and fileno included. -pedantic would be more appropriate for the
17  * intended purposes, but we can't prevent users from adding -ansi.
18  */
19 #if defined(OPENSSL_SYS_VXWORKS)
20 # include <sys/types.h>
21 #endif
22
23 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
24 # ifndef _POSIX_C_SOURCE
25 #  define _POSIX_C_SOURCE 2
26 # endif
27 #endif
28 #include <signal.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
34 # ifdef OPENSSL_UNISTD
35 #  include OPENSSL_UNISTD
36 # else
37 #  include <unistd.h>
38 # endif
39 /*
40  * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
41  * system and have sigaction and termios.
42  */
43 # if defined(_POSIX_VERSION)
44
45 #  define SIGACTION
46 #  if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
47 #   define TERMIOS
48 #  endif
49
50 # endif
51 #endif
52
53 /* 06-Apr-92 Luke Brennan    Support for VMS */
54 #include "ui_locl.h"
55 #include "internal/cryptlib.h"
56
57 #ifdef OPENSSL_SYS_VMS          /* prototypes for sys$whatever */
58 # include <starlet.h>
59 # ifdef __DECC
60 #  pragma message disable DOLLARID
61 # endif
62 #endif
63
64 #ifdef WIN_CONSOLE_BUG
65 # include <windows.h>
66 # ifndef OPENSSL_SYS_WINCE
67 #  include <wincon.h>
68 # endif
69 #endif
70
71 /*
72  * There are 6 types of terminal interface supported, TERMIO, TERMIOS, VMS,
73  * MSDOS, WIN32 Console and SGTTY.
74  *
75  * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
76  * remain respected.  Otherwise, we default to TERMIOS except for a few
77  * systems that require something different.
78  *
79  * Note: we do not use SGTTY unless it's defined by the configuration.  We
80  * may eventually opt to remove it's use entirely.
81  */
82
83 #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
84
85 # if defined(_LIBC)
86 #  undef  TERMIOS
87 #  define TERMIO
88 #  undef  SGTTY
89 /*
90  * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
91  */
92 # elif !defined(OPENSSL_SYS_VMS) \
93         && !defined(OPENSSL_SYS_MSDOS) \
94         && !defined(OPENSSL_SYS_VXWORKS)
95 #  define TERMIOS
96 #  undef  TERMIO
97 #  undef  SGTTY
98 # endif
99
100 #endif
101
102 #ifdef TERMIOS
103 # include <termios.h>
104 # define TTY_STRUCT             struct termios
105 # define TTY_FLAGS              c_lflag
106 # define TTY_get(tty,data)      tcgetattr(tty,data)
107 # define TTY_set(tty,data)      tcsetattr(tty,TCSANOW,data)
108 #endif
109
110 #ifdef TERMIO
111 # include <termio.h>
112 # define TTY_STRUCT             struct termio
113 # define TTY_FLAGS              c_lflag
114 # define TTY_get(tty,data)      ioctl(tty,TCGETA,data)
115 # define TTY_set(tty,data)      ioctl(tty,TCSETA,data)
116 #endif
117
118 #ifdef SGTTY
119 # include <sgtty.h>
120 # define TTY_STRUCT             struct sgttyb
121 # define TTY_FLAGS              sg_flags
122 # define TTY_get(tty,data)      ioctl(tty,TIOCGETP,data)
123 # define TTY_set(tty,data)      ioctl(tty,TIOCSETP,data)
124 #endif
125
126 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
127 # include <sys/ioctl.h>
128 #endif
129
130 #ifdef OPENSSL_SYS_MSDOS
131 # include <conio.h>
132 #endif
133
134 #ifdef OPENSSL_SYS_VMS
135 # include <ssdef.h>
136 # include <iodef.h>
137 # include <ttdef.h>
138 # include <descrip.h>
139 struct IOSB {
140     short iosb$w_value;
141     short iosb$w_count;
142     long iosb$l_info;
143 };
144 #endif
145
146 #ifndef NX509_SIG
147 # define NX509_SIG 32
148 #endif
149
150 /* Define globals.  They are protected by a lock */
151 #ifdef SIGACTION
152 static struct sigaction savsig[NX509_SIG];
153 #else
154 static void (*savsig[NX509_SIG]) (int);
155 #endif
156
157 #ifdef OPENSSL_SYS_VMS
158 static struct IOSB iosb;
159 static $DESCRIPTOR(terminal, "TT");
160 static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
161                                       * will always suffice for the actual
162                                       * structures? */
163 static long status;
164 static unsigned short channel = 0;
165 #elif defined(_WIN32) && !defined(_WIN32_WCE)
166 static DWORD tty_orig, tty_new;
167 #else
168 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
169 static TTY_STRUCT tty_orig, tty_new;
170 # endif
171 #endif
172 static FILE *tty_in, *tty_out;
173 static int is_a_tty;
174
175 /* Declare static functions */
176 #if !defined(OPENSSL_SYS_WINCE)
177 static int read_till_nl(FILE *);
178 static void recsig(int);
179 static void pushsig(void);
180 static void popsig(void);
181 #endif
182 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
183 static int noecho_fgets(char *buf, int size, FILE *tty);
184 #endif
185 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
186
187 static int read_string(UI *ui, UI_STRING *uis);
188 static int write_string(UI *ui, UI_STRING *uis);
189
190 static int open_console(UI *ui);
191 static int echo_console(UI *ui);
192 static int noecho_console(UI *ui);
193 static int close_console(UI *ui);
194
195 static UI_METHOD ui_openssl = {
196     "OpenSSL default user interface",
197     open_console,
198     write_string,
199     NULL,                       /* No flusher is needed for command lines */
200     read_string,
201     close_console,
202     NULL
203 };
204
205 /* The method with all the built-in thingies */
206 UI_METHOD *UI_OpenSSL(void)
207 {
208     return &ui_openssl;
209 }
210
211 /*
212  * The following function makes sure that info and error strings are printed
213  * before any prompt.
214  */
215 static int write_string(UI *ui, UI_STRING *uis)
216 {
217     switch (UI_get_string_type(uis)) {
218     case UIT_ERROR:
219     case UIT_INFO:
220         fputs(UI_get0_output_string(uis), tty_out);
221         fflush(tty_out);
222         break;
223     default:
224         break;
225     }
226     return 1;
227 }
228
229 static int read_string(UI *ui, UI_STRING *uis)
230 {
231     int ok = 0;
232
233     switch (UI_get_string_type(uis)) {
234     case UIT_BOOLEAN:
235         fputs(UI_get0_output_string(uis), tty_out);
236         fputs(UI_get0_action_string(uis), tty_out);
237         fflush(tty_out);
238         return read_string_inner(ui, uis,
239                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
240                                  0);
241     case UIT_PROMPT:
242         fputs(UI_get0_output_string(uis), tty_out);
243         fflush(tty_out);
244         return read_string_inner(ui, uis,
245                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
246                                  1);
247     case UIT_VERIFY:
248         fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
249         fflush(tty_out);
250         if ((ok = read_string_inner(ui, uis,
251                                     UI_get_input_flags(uis) &
252                                     UI_INPUT_FLAG_ECHO, 1)) <= 0)
253             return ok;
254         if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
255             fprintf(tty_out, "Verify failure\n");
256             fflush(tty_out);
257             return 0;
258         }
259         break;
260     default:
261         break;
262     }
263     return 1;
264 }
265
266 #if !defined(OPENSSL_SYS_WINCE)
267 /* Internal functions to read a string without echoing */
268 static int read_till_nl(FILE *in)
269 {
270 # define SIZE 4
271     char buf[SIZE + 1];
272
273     do {
274         if (!fgets(buf, SIZE, in))
275             return 0;
276     } while (strchr(buf, '\n') == NULL);
277     return 1;
278 }
279
280 static volatile sig_atomic_t intr_signal;
281 #endif
282
283 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
284 {
285     static int ps;
286     int ok;
287     char result[BUFSIZ];
288     int maxsize = BUFSIZ - 1;
289 #if !defined(OPENSSL_SYS_WINCE)
290     char *p = NULL;
291     int echo_eol = !echo;
292
293     intr_signal = 0;
294     ok = 0;
295     ps = 0;
296
297     pushsig();
298     ps = 1;
299
300     if (!echo && !noecho_console(ui))
301         goto error;
302     ps = 2;
303
304     result[0] = '\0';
305 # if defined(_WIN32)
306     if (is_a_tty) {
307         DWORD numread;
308 #  if defined(CP_UTF8)
309         if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
310             WCHAR wresult[BUFSIZ];
311
312             if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
313                          wresult, maxsize, &numread, NULL)) {
314                 if (numread >= 2 &&
315                     wresult[numread-2] == L'\r' &&
316                     wresult[numread-1] == L'\n') {
317                     wresult[numread-2] = L'\n';
318                     numread--;
319                 }
320                 wresult[numread] = '\0';
321                 if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
322                                         result, sizeof(result), NULL, 0) > 0)
323                     p = result;
324
325                 OPENSSL_cleanse(wresult, sizeof(wresult));
326             }
327         } else
328 #  endif
329         if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
330                          result, maxsize, &numread, NULL)) {
331             if (numread >= 2 &&
332                 result[numread-2] == '\r' && result[numread-1] == '\n') {
333                 result[numread-2] = '\n';
334                 numread--;
335             }
336             result[numread] = '\0';
337             p = result;
338         }
339     } else
340 # elif defined(OPENSSL_SYS_MSDOS)
341     if (!echo) {
342         noecho_fgets(result, maxsize, tty_in);
343         p = result;             /* FIXME: noecho_fgets doesn't return errors */
344     } else
345 # endif
346     p = fgets(result, maxsize, tty_in);
347     if (p == NULL)
348         goto error;
349     if (feof(tty_in))
350         goto error;
351     if (ferror(tty_in))
352         goto error;
353     if ((p = (char *)strchr(result, '\n')) != NULL) {
354         if (strip_nl)
355             *p = '\0';
356     } else if (!read_till_nl(tty_in))
357         goto error;
358     if (UI_set_result(ui, uis, result) >= 0)
359         ok = 1;
360
361  error:
362     if (intr_signal == SIGINT)
363         ok = -1;
364     if (echo_eol)
365         fprintf(tty_out, "\n");
366     if (ps >= 2 && !echo && !echo_console(ui))
367         ok = 0;
368
369     if (ps >= 1)
370         popsig();
371 #else
372     ok = 1;
373 #endif
374
375     OPENSSL_cleanse(result, BUFSIZ);
376     return ok;
377 }
378
379 /* Internal functions to open, handle and close a channel to the console.  */
380 static int open_console(UI *ui)
381 {
382     CRYPTO_THREAD_write_lock(ui->lock);
383     is_a_tty = 1;
384
385 #if defined(OPENSSL_SYS_VXWORKS)
386     tty_in = stdin;
387     tty_out = stderr;
388 #elif defined(_WIN32) && !defined(_WIN32_WCE)
389     if ((tty_out = fopen("conout$", "w")) == NULL)
390         tty_out = stderr;
391
392     if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) {
393         tty_in = stdin;
394     } else {
395         is_a_tty = 0;
396         if ((tty_in = fopen("conin$", "r")) == NULL)
397             tty_in = stdin;
398     }
399 #else
400 # ifdef OPENSSL_SYS_MSDOS
401 #  define DEV_TTY "con"
402 # else
403 #  define DEV_TTY "/dev/tty"
404 # endif
405     if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
406         tty_in = stdin;
407     if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
408         tty_out = stderr;
409 #endif
410
411 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
412     if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
413 # ifdef ENOTTY
414         if (errno == ENOTTY)
415             is_a_tty = 0;
416         else
417 # endif
418 # ifdef EINVAL
419             /*
420              * Ariel Glenn ariel@columbia.edu reports that solaris can return
421              * EINVAL instead.  This should be ok
422              */
423         if (errno == EINVAL)
424             is_a_tty = 0;
425         else
426 # endif
427 # ifdef ENODEV
428             /*
429              * MacOS X returns ENODEV (Operation not supported by device),
430              * which seems appropriate.
431              */
432         if (errno == ENODEV)
433             is_a_tty = 0;
434         else
435 # endif
436             {
437                 char tmp_num[10];
438                 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%d", errno);
439                 UIerr(UI_F_OPEN_CONSOLE, UI_R_UNKNOWN_TTYGET_ERRNO_VALUE);
440                 ERR_add_error_data(2, "errno=", tmp_num);
441
442                 return 0;
443             }
444     }
445 #endif
446 #ifdef OPENSSL_SYS_VMS
447     status = sys$assign(&terminal, &channel, 0, 0);
448
449     /* if there isn't a TT device, something is very wrong */
450     if (status != SS$_NORMAL) {
451         char tmp_num[12];
452
453         BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
454         UIerr(UI_F_OPEN_CONSOLE, UI_R_SYSASSIGN_ERROR);
455         ERR_add_error_data(2, "status=", tmp_num);
456         return 0;
457     }
458
459     status = sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12,
460                       0, 0, 0, 0);
461
462     /* If IO$_SENSEMODE doesn't work, this is not a terminal device */
463     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
464         is_a_tty = 0;
465 #endif
466     return 1;
467 }
468
469 static int noecho_console(UI *ui)
470 {
471 #ifdef TTY_FLAGS
472     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
473     tty_new.TTY_FLAGS &= ~ECHO;
474 #endif
475
476 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
477     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
478         return 0;
479 #endif
480 #ifdef OPENSSL_SYS_VMS
481     if (is_a_tty) {
482         tty_new[0] = tty_orig[0];
483         tty_new[1] = tty_orig[1] | TT$M_NOECHO;
484         tty_new[2] = tty_orig[2];
485         status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
486                           0, 0, 0, 0);
487         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
488             char tmp_num[2][12];
489
490             BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
491                          status);
492             BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
493                          iosb.iosb$w_value);
494             UIerr(UI_F_NOECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
495             ERR_add_error_data(5, "status=", tmp_num[0],
496                                ",", "iosb.iosb$w_value=", tmp_num[1]);
497             return 0;
498         }
499     }
500 #endif
501 #if defined(_WIN32) && !defined(_WIN32_WCE)
502     if (is_a_tty) {
503         tty_new = tty_orig;
504         tty_new &= ~ENABLE_ECHO_INPUT;
505         SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
506     }
507 #endif
508     return 1;
509 }
510
511 static int echo_console(UI *ui)
512 {
513 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
514     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
515     tty_new.TTY_FLAGS |= ECHO;
516 #endif
517
518 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
519     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
520         return 0;
521 #endif
522 #ifdef OPENSSL_SYS_VMS
523     if (is_a_tty) {
524         tty_new[0] = tty_orig[0];
525         tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
526         tty_new[2] = tty_orig[2];
527         status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
528                           0, 0, 0, 0);
529         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
530             char tmp_num[2][12];
531
532             BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
533                          status);
534             BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
535                          iosb.iosb$w_value);
536             UIerr(UI_F_ECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
537             ERR_add_error_data(5, "status=", tmp_num[0],
538                                ",", "iosb.iosb$w_value=", tmp_num[1]);
539             return 0;
540         }
541     }
542 #endif
543 #if defined(_WIN32) && !defined(_WIN32_WCE)
544     if (is_a_tty) {
545         tty_new = tty_orig;
546         tty_new |= ENABLE_ECHO_INPUT;
547         SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
548     }
549 #endif
550     return 1;
551 }
552
553 static int close_console(UI *ui)
554 {
555     if (tty_in != stdin)
556         fclose(tty_in);
557     if (tty_out != stderr)
558         fclose(tty_out);
559 #ifdef OPENSSL_SYS_VMS
560     status = sys$dassgn(channel);
561     if (status != SS$_NORMAL) {
562         char tmp_num[12];
563
564         BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
565         UIerr(UI_F_CLOSE_CONSOLE, UI_R_SYSDASSGN_ERROR);
566         ERR_add_error_data(2, "status=", tmp_num);
567         return 0;
568     }
569 #endif
570     CRYPTO_THREAD_unlock(ui->lock);
571
572     return 1;
573 }
574
575 #if !defined(OPENSSL_SYS_WINCE)
576 /* Internal functions to handle signals and act on them */
577 static void pushsig(void)
578 {
579 # ifndef OPENSSL_SYS_WIN32
580     int i;
581 # endif
582 # ifdef SIGACTION
583     struct sigaction sa;
584
585     memset(&sa, 0, sizeof(sa));
586     sa.sa_handler = recsig;
587 # endif
588
589 # ifdef OPENSSL_SYS_WIN32
590     savsig[SIGABRT] = signal(SIGABRT, recsig);
591     savsig[SIGFPE] = signal(SIGFPE, recsig);
592     savsig[SIGILL] = signal(SIGILL, recsig);
593     savsig[SIGINT] = signal(SIGINT, recsig);
594     savsig[SIGSEGV] = signal(SIGSEGV, recsig);
595     savsig[SIGTERM] = signal(SIGTERM, recsig);
596 # else
597     for (i = 1; i < NX509_SIG; i++) {
598 #  ifdef SIGUSR1
599         if (i == SIGUSR1)
600             continue;
601 #  endif
602 #  ifdef SIGUSR2
603         if (i == SIGUSR2)
604             continue;
605 #  endif
606 #  ifdef SIGKILL
607         if (i == SIGKILL)       /* We can't make any action on that. */
608             continue;
609 #  endif
610 #  ifdef SIGACTION
611         sigaction(i, &sa, &savsig[i]);
612 #  else
613         savsig[i] = signal(i, recsig);
614 #  endif
615     }
616 # endif
617
618 # ifdef SIGWINCH
619     signal(SIGWINCH, SIG_DFL);
620 # endif
621 }
622
623 static void popsig(void)
624 {
625 # ifdef OPENSSL_SYS_WIN32
626     signal(SIGABRT, savsig[SIGABRT]);
627     signal(SIGFPE, savsig[SIGFPE]);
628     signal(SIGILL, savsig[SIGILL]);
629     signal(SIGINT, savsig[SIGINT]);
630     signal(SIGSEGV, savsig[SIGSEGV]);
631     signal(SIGTERM, savsig[SIGTERM]);
632 # else
633     int i;
634     for (i = 1; i < NX509_SIG; i++) {
635 #  ifdef SIGUSR1
636         if (i == SIGUSR1)
637             continue;
638 #  endif
639 #  ifdef SIGUSR2
640         if (i == SIGUSR2)
641             continue;
642 #  endif
643 #  ifdef SIGACTION
644         sigaction(i, &savsig[i], NULL);
645 #  else
646         signal(i, savsig[i]);
647 #  endif
648     }
649 # endif
650 }
651
652 static void recsig(int i)
653 {
654     intr_signal = i;
655 }
656 #endif
657
658 /* Internal functions specific for Windows */
659 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
660 static int noecho_fgets(char *buf, int size, FILE *tty)
661 {
662     int i;
663     char *p;
664
665     p = buf;
666     for (;;) {
667         if (size == 0) {
668             *p = '\0';
669             break;
670         }
671         size--;
672 # if defined(_WIN32)
673         i = _getch();
674 # else
675         i = getch();
676 # endif
677         if (i == '\r')
678             i = '\n';
679         *(p++) = i;
680         if (i == '\n') {
681             *p = '\0';
682             break;
683         }
684     }
685 # ifdef WIN_CONSOLE_BUG
686     /*
687      * Win95 has several evil console bugs: one of these is that the last
688      * character read using getch() is passed to the next read: this is
689      * usually a CR so this can be trouble. No STDIO fix seems to work but
690      * flushing the console appears to do the trick.
691      */
692     {
693         HANDLE inh;
694         inh = GetStdHandle(STD_INPUT_HANDLE);
695         FlushConsoleInputBuffer(inh);
696     }
697 # endif
698     return (strlen(buf));
699 }
700 #endif