2 * Copyright 2001-2017 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/e_os2.h>
11 #include <openssl/err.h>
12 #include <openssl/ui.h>
15 #ifndef OPENSSL_NO_UI_CONSOLE
17 * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
18 * [maybe others?], because it masks interfaces not discussed in standard,
19 * sigaction and fileno included. -pedantic would be more appropriate for the
20 * intended purposes, but we can't prevent users from adding -ansi.
22 # if defined(OPENSSL_SYS_VXWORKS)
23 # include <sys/types.h>
26 # if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
27 # ifndef _POSIX_C_SOURCE
28 # define _POSIX_C_SOURCE 2
36 # if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
37 # ifdef OPENSSL_UNISTD
38 # include OPENSSL_UNISTD
43 * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
44 * system and have sigaction and termios.
46 # if defined(_POSIX_VERSION)
49 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
56 /* 06-Apr-92 Luke Brennan Support for VMS */
58 # include "internal/cryptlib.h"
60 # ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
63 # pragma message disable DOLLARID
67 # ifdef WIN_CONSOLE_BUG
69 # ifndef OPENSSL_SYS_WINCE
75 * There are 6 types of terminal interface supported, TERMIO, TERMIOS, VMS,
76 * MSDOS, WIN32 Console and SGTTY.
78 * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
79 * remain respected. Otherwise, we default to TERMIOS except for a few
80 * systems that require something different.
82 * Note: we do not use SGTTY unless it's defined by the configuration. We
83 * may eventually opt to remove it's use entirely.
86 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
93 * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
95 # elif !defined(OPENSSL_SYS_VMS) \
96 && !defined(OPENSSL_SYS_MSDOS) \
97 && !defined(OPENSSL_SYS_VXWORKS)
106 # include <termios.h>
107 # define TTY_STRUCT struct termios
108 # define TTY_FLAGS c_lflag
109 # define TTY_get(tty,data) tcgetattr(tty,data)
110 # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
115 # define TTY_STRUCT struct termio
116 # define TTY_FLAGS c_lflag
117 # define TTY_get(tty,data) ioctl(tty,TCGETA,data)
118 # define TTY_set(tty,data) ioctl(tty,TCSETA,data)
123 # define TTY_STRUCT struct sgttyb
124 # define TTY_FLAGS sg_flags
125 # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
126 # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
129 # if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
130 # include <sys/ioctl.h>
133 # ifdef OPENSSL_SYS_MSDOS
137 # ifdef OPENSSL_SYS_VMS
141 # include <descrip.h>
150 # define NX509_SIG 32
153 /* Define globals. They are protected by a lock */
155 static struct sigaction savsig[NX509_SIG];
157 static void (*savsig[NX509_SIG]) (int);
160 # ifdef OPENSSL_SYS_VMS
161 static struct IOSB iosb;
162 static $DESCRIPTOR(terminal, "TT");
163 static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
164 * will always suffice for the actual
167 static unsigned short channel = 0;
168 # elif defined(_WIN32) && !defined(_WIN32_WCE)
169 static DWORD tty_orig, tty_new;
171 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
172 static TTY_STRUCT tty_orig, tty_new;
175 static FILE *tty_in, *tty_out;
178 /* Declare static functions */
179 # if !defined(OPENSSL_SYS_WINCE)
180 static int read_till_nl(FILE *);
181 static void recsig(int);
182 static void pushsig(void);
183 static void popsig(void);
185 # if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
186 static int noecho_fgets(char *buf, int size, FILE *tty);
188 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
190 static int read_string(UI *ui, UI_STRING *uis);
191 static int write_string(UI *ui, UI_STRING *uis);
193 static int open_console(UI *ui);
194 static int echo_console(UI *ui);
195 static int noecho_console(UI *ui);
196 static int close_console(UI *ui);
199 * The following function makes sure that info and error strings are printed
202 static int write_string(UI *ui, UI_STRING *uis)
204 switch (UI_get_string_type(uis)) {
207 fputs(UI_get0_output_string(uis), tty_out);
219 static int read_string(UI *ui, UI_STRING *uis)
223 switch (UI_get_string_type(uis)) {
225 fputs(UI_get0_output_string(uis), tty_out);
226 fputs(UI_get0_action_string(uis), tty_out);
228 return read_string_inner(ui, uis,
229 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
232 fputs(UI_get0_output_string(uis), tty_out);
234 return read_string_inner(ui, uis,
235 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
238 fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
240 if ((ok = read_string_inner(ui, uis,
241 UI_get_input_flags(uis) &
242 UI_INPUT_FLAG_ECHO, 1)) <= 0)
244 if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
245 fprintf(tty_out, "Verify failure\n");
258 # if !defined(OPENSSL_SYS_WINCE)
259 /* Internal functions to read a string without echoing */
260 static int read_till_nl(FILE *in)
266 if (!fgets(buf, SIZE, in))
268 } while (strchr(buf, '\n') == NULL);
272 static volatile sig_atomic_t intr_signal;
275 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
280 int maxsize = BUFSIZ - 1;
281 # if !defined(OPENSSL_SYS_WINCE)
283 int echo_eol = !echo;
292 if (!echo && !noecho_console(ui))
300 # if defined(CP_UTF8)
301 if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
302 WCHAR wresult[BUFSIZ];
304 if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
305 wresult, maxsize, &numread, NULL)) {
307 wresult[numread-2] == L'\r' &&
308 wresult[numread-1] == L'\n') {
309 wresult[numread-2] = L'\n';
312 wresult[numread] = '\0';
313 if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
314 result, sizeof(result), NULL, 0) > 0)
317 OPENSSL_cleanse(wresult, sizeof(wresult));
321 if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
322 result, maxsize, &numread, NULL)) {
324 result[numread-2] == '\r' && result[numread-1] == '\n') {
325 result[numread-2] = '\n';
328 result[numread] = '\0';
332 # elif defined(OPENSSL_SYS_MSDOS)
334 noecho_fgets(result, maxsize, tty_in);
335 p = result; /* FIXME: noecho_fgets doesn't return errors */
338 p = fgets(result, maxsize, tty_in);
345 if ((p = (char *)strchr(result, '\n')) != NULL) {
348 } else if (!read_till_nl(tty_in))
350 if (UI_set_result(ui, uis, result) >= 0)
354 if (intr_signal == SIGINT)
357 fprintf(tty_out, "\n");
358 if (ps >= 2 && !echo && !echo_console(ui))
367 OPENSSL_cleanse(result, BUFSIZ);
371 /* Internal functions to open, handle and close a channel to the console. */
372 static int open_console(UI *ui)
374 CRYPTO_THREAD_write_lock(ui->lock);
377 # if defined(OPENSSL_SYS_VXWORKS)
380 # elif defined(_WIN32) && !defined(_WIN32_WCE)
381 if ((tty_out = fopen("conout$", "w")) == NULL)
384 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) {
388 if ((tty_in = fopen("conin$", "r")) == NULL)
392 # ifdef OPENSSL_SYS_MSDOS
393 # define DEV_TTY "con"
395 # define DEV_TTY "/dev/tty"
397 if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
399 if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
403 # if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
404 if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
412 * Ariel Glenn ariel@columbia.edu reports that solaris can return
413 * EINVAL instead. This should be ok
421 * MacOS X returns ENODEV (Operation not supported by device),
422 * which seems appropriate.
430 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%d", errno);
431 UIerr(UI_F_OPEN_CONSOLE, UI_R_UNKNOWN_TTYGET_ERRNO_VALUE);
432 ERR_add_error_data(2, "errno=", tmp_num);
438 # ifdef OPENSSL_SYS_VMS
439 status = sys$assign(&terminal, &channel, 0, 0);
441 /* if there isn't a TT device, something is very wrong */
442 if (status != SS$_NORMAL) {
445 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
446 UIerr(UI_F_OPEN_CONSOLE, UI_R_SYSASSIGN_ERROR);
447 ERR_add_error_data(2, "status=", tmp_num);
451 status = sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12,
454 /* If IO$_SENSEMODE doesn't work, this is not a terminal device */
455 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
461 static int noecho_console(UI *ui)
464 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
465 tty_new.TTY_FLAGS &= ~ECHO;
468 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
469 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
472 # ifdef OPENSSL_SYS_VMS
474 tty_new[0] = tty_orig[0];
475 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
476 tty_new[2] = tty_orig[2];
477 status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
479 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
482 BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
484 BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
486 UIerr(UI_F_NOECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
487 ERR_add_error_data(5, "status=", tmp_num[0],
488 ",", "iosb.iosb$w_value=", tmp_num[1]);
493 # if defined(_WIN32) && !defined(_WIN32_WCE)
496 tty_new &= ~ENABLE_ECHO_INPUT;
497 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
503 static int echo_console(UI *ui)
505 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
506 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
507 tty_new.TTY_FLAGS |= ECHO;
510 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
511 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
514 # ifdef OPENSSL_SYS_VMS
516 tty_new[0] = tty_orig[0];
517 tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
518 tty_new[2] = tty_orig[2];
519 status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
521 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
524 BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
526 BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
528 UIerr(UI_F_ECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
529 ERR_add_error_data(5, "status=", tmp_num[0],
530 ",", "iosb.iosb$w_value=", tmp_num[1]);
535 # if defined(_WIN32) && !defined(_WIN32_WCE)
538 tty_new |= ENABLE_ECHO_INPUT;
539 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
545 static int close_console(UI *ui)
549 if (tty_out != stderr)
551 # ifdef OPENSSL_SYS_VMS
552 status = sys$dassgn(channel);
553 if (status != SS$_NORMAL) {
556 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
557 UIerr(UI_F_CLOSE_CONSOLE, UI_R_SYSDASSGN_ERROR);
558 ERR_add_error_data(2, "status=", tmp_num);
562 CRYPTO_THREAD_unlock(ui->lock);
567 # if !defined(OPENSSL_SYS_WINCE)
568 /* Internal functions to handle signals and act on them */
569 static void pushsig(void)
571 # ifndef OPENSSL_SYS_WIN32
577 memset(&sa, 0, sizeof(sa));
578 sa.sa_handler = recsig;
581 # ifdef OPENSSL_SYS_WIN32
582 savsig[SIGABRT] = signal(SIGABRT, recsig);
583 savsig[SIGFPE] = signal(SIGFPE, recsig);
584 savsig[SIGILL] = signal(SIGILL, recsig);
585 savsig[SIGINT] = signal(SIGINT, recsig);
586 savsig[SIGSEGV] = signal(SIGSEGV, recsig);
587 savsig[SIGTERM] = signal(SIGTERM, recsig);
589 for (i = 1; i < NX509_SIG; i++) {
599 if (i == SIGKILL) /* We can't make any action on that. */
603 sigaction(i, &sa, &savsig[i]);
605 savsig[i] = signal(i, recsig);
611 signal(SIGWINCH, SIG_DFL);
615 static void popsig(void)
617 # ifdef OPENSSL_SYS_WIN32
618 signal(SIGABRT, savsig[SIGABRT]);
619 signal(SIGFPE, savsig[SIGFPE]);
620 signal(SIGILL, savsig[SIGILL]);
621 signal(SIGINT, savsig[SIGINT]);
622 signal(SIGSEGV, savsig[SIGSEGV]);
623 signal(SIGTERM, savsig[SIGTERM]);
626 for (i = 1; i < NX509_SIG; i++) {
636 sigaction(i, &savsig[i], NULL);
638 signal(i, savsig[i]);
644 static void recsig(int i)
650 /* Internal functions specific for Windows */
651 # if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
652 static int noecho_fgets(char *buf, int size, FILE *tty)
677 # ifdef WIN_CONSOLE_BUG
679 * Win95 has several evil console bugs: one of these is that the last
680 * character read using getch() is passed to the next read: this is
681 * usually a CR so this can be trouble. No STDIO fix seems to work but
682 * flushing the console appears to do the trick.
686 inh = GetStdHandle(STD_INPUT_HANDLE);
687 FlushConsoleInputBuffer(inh);
690 return (strlen(buf));
694 static UI_METHOD ui_openssl = {
695 "OpenSSL default user interface",
698 NULL, /* No flusher is needed for command lines */
704 /* The method with all the built-in console thingies */
705 UI_METHOD *UI_OpenSSL(void)
710 static const UI_METHOD *default_UI_meth = &ui_openssl;
714 static const UI_METHOD *default_UI_meth = NULL;
718 void UI_set_default_method(const UI_METHOD *meth)
720 default_UI_meth = meth;
723 const UI_METHOD *UI_get_default_method(void)
725 return default_UI_meth;