2 * Copyright 2001-2018 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
11 #include <openssl/e_os2.h>
12 #include <openssl/err.h>
13 #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) && _POSIX_VERSION>=199309L
49 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
57 # include "internal/cryptlib.h"
59 # ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
62 # pragma message disable DOLLARID
66 # ifdef WIN_CONSOLE_BUG
68 # ifndef OPENSSL_SYS_WINCE
74 * There are 6 types of terminal interface supported, TERMIO, TERMIOS, VMS,
75 * MSDOS, WIN32 Console and SGTTY.
77 * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
78 * remain respected. Otherwise, we default to TERMIOS except for a few
79 * systems that require something different.
81 * Note: we do not use SGTTY unless it's defined by the configuration. We
82 * may eventually opt to remove it's use entirely.
85 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
92 * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
94 # elif !defined(OPENSSL_SYS_VMS) \
95 && !defined(OPENSSL_SYS_MSDOS) \
96 && !defined(OPENSSL_SYS_VXWORKS)
105 # include <termios.h>
106 # define TTY_STRUCT struct termios
107 # define TTY_FLAGS c_lflag
108 # define TTY_get(tty,data) tcgetattr(tty,data)
109 # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
114 # define TTY_STRUCT struct termio
115 # define TTY_FLAGS c_lflag
116 # define TTY_get(tty,data) ioctl(tty,TCGETA,data)
117 # define TTY_set(tty,data) ioctl(tty,TCSETA,data)
122 # define TTY_STRUCT struct sgttyb
123 # define TTY_FLAGS sg_flags
124 # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
125 # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
128 # if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
129 # include <sys/ioctl.h>
132 # ifdef OPENSSL_SYS_MSDOS
136 # ifdef OPENSSL_SYS_VMS
140 # include <descrip.h>
149 # define NX509_SIG 32
152 /* Define globals. They are protected by a lock */
154 static struct sigaction savsig[NX509_SIG];
156 static void (*savsig[NX509_SIG]) (int);
159 # ifdef OPENSSL_SYS_VMS
160 static struct IOSB iosb;
161 static $DESCRIPTOR(terminal, "TT");
162 static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
163 * will always suffice for the actual
166 static unsigned short channel = 0;
167 # elif defined(_WIN32) && !defined(_WIN32_WCE)
168 static DWORD tty_orig, tty_new;
170 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
171 static TTY_STRUCT tty_orig, tty_new;
174 static FILE *tty_in, *tty_out;
177 /* Declare static functions */
178 # if !defined(OPENSSL_SYS_WINCE)
179 static int read_till_nl(FILE *);
180 static void recsig(int);
181 static void pushsig(void);
182 static void popsig(void);
184 # if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
185 static int noecho_fgets(char *buf, int size, FILE *tty);
187 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
189 static int read_string(UI *ui, UI_STRING *uis);
190 static int write_string(UI *ui, UI_STRING *uis);
192 static int open_console(UI *ui);
193 static int echo_console(UI *ui);
194 static int noecho_console(UI *ui);
195 static int close_console(UI *ui);
198 * The following function makes sure that info and error strings are printed
201 static int write_string(UI *ui, UI_STRING *uis)
203 switch (UI_get_string_type(uis)) {
206 fputs(UI_get0_output_string(uis), tty_out);
218 static int read_string(UI *ui, UI_STRING *uis)
222 switch (UI_get_string_type(uis)) {
224 fputs(UI_get0_output_string(uis), tty_out);
225 fputs(UI_get0_action_string(uis), tty_out);
227 return read_string_inner(ui, uis,
228 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
231 fputs(UI_get0_output_string(uis), tty_out);
233 return read_string_inner(ui, uis,
234 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
237 fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
239 if ((ok = read_string_inner(ui, uis,
240 UI_get_input_flags(uis) &
241 UI_INPUT_FLAG_ECHO, 1)) <= 0)
243 if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
244 fprintf(tty_out, "Verify failure\n");
257 # if !defined(OPENSSL_SYS_WINCE)
258 /* Internal functions to read a string without echoing */
259 static int read_till_nl(FILE *in)
265 if (!fgets(buf, SIZE, in))
267 } while (strchr(buf, '\n') == NULL);
271 static volatile sig_atomic_t intr_signal;
274 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
279 int maxsize = BUFSIZ - 1;
280 # if !defined(OPENSSL_SYS_WINCE)
282 int echo_eol = !echo;
291 if (!echo && !noecho_console(ui))
299 # if defined(CP_UTF8)
300 if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
301 WCHAR wresult[BUFSIZ];
303 if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
304 wresult, maxsize, &numread, NULL)) {
306 wresult[numread-2] == L'\r' &&
307 wresult[numread-1] == L'\n') {
308 wresult[numread-2] = L'\n';
311 wresult[numread] = '\0';
312 if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
313 result, sizeof(result), NULL, 0) > 0)
316 OPENSSL_cleanse(wresult, sizeof(wresult));
320 if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
321 result, maxsize, &numread, NULL)) {
323 result[numread-2] == '\r' && result[numread-1] == '\n') {
324 result[numread-2] = '\n';
327 result[numread] = '\0';
331 # elif defined(OPENSSL_SYS_MSDOS)
333 noecho_fgets(result, maxsize, tty_in);
334 p = result; /* FIXME: noecho_fgets doesn't return errors */
337 p = fgets(result, maxsize, tty_in);
344 if ((p = (char *)strchr(result, '\n')) != NULL) {
347 } else if (!read_till_nl(tty_in))
349 if (UI_set_result(ui, uis, result) >= 0)
353 if (intr_signal == SIGINT)
356 fprintf(tty_out, "\n");
357 if (ps >= 2 && !echo && !echo_console(ui))
366 OPENSSL_cleanse(result, BUFSIZ);
370 /* Internal functions to open, handle and close a channel to the console. */
371 static int open_console(UI *ui)
373 CRYPTO_THREAD_write_lock(ui->lock);
376 # if defined(OPENSSL_SYS_VXWORKS)
379 # elif defined(_WIN32) && !defined(_WIN32_WCE)
380 if ((tty_out = fopen("conout$", "w")) == NULL)
383 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) {
387 if ((tty_in = fopen("conin$", "r")) == NULL)
391 # ifdef OPENSSL_SYS_MSDOS
392 # define DEV_TTY "con"
394 # define DEV_TTY "/dev/tty"
396 if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
398 if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
402 # if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
403 if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
411 * Ariel Glenn reports that solaris can return EINVAL instead.
420 * Solaris can return ENXIO.
429 * Linux can return EIO.
438 * MacOS X returns ENODEV (Operation not supported by device),
439 * which seems appropriate.
447 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%d", errno);
448 UIerr(UI_F_OPEN_CONSOLE, UI_R_UNKNOWN_TTYGET_ERRNO_VALUE);
449 ERR_add_error_data(2, "errno=", tmp_num);
455 # ifdef OPENSSL_SYS_VMS
456 status = sys$assign(&terminal, &channel, 0, 0);
458 /* if there isn't a TT device, something is very wrong */
459 if (status != SS$_NORMAL) {
462 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
463 UIerr(UI_F_OPEN_CONSOLE, UI_R_SYSASSIGN_ERROR);
464 ERR_add_error_data(2, "status=", tmp_num);
468 status = sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12,
471 /* If IO$_SENSEMODE doesn't work, this is not a terminal device */
472 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
478 static int noecho_console(UI *ui)
481 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
482 tty_new.TTY_FLAGS &= ~ECHO;
485 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
486 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
489 # 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,
496 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
499 BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
501 BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
503 UIerr(UI_F_NOECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
504 ERR_add_error_data(5, "status=", tmp_num[0],
505 ",", "iosb.iosb$w_value=", tmp_num[1]);
510 # if defined(_WIN32) && !defined(_WIN32_WCE)
513 tty_new &= ~ENABLE_ECHO_INPUT;
514 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
520 static int echo_console(UI *ui)
522 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
523 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
524 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
527 # ifdef OPENSSL_SYS_VMS
529 tty_new[0] = tty_orig[0];
530 tty_new[1] = tty_orig[1];
531 tty_new[2] = tty_orig[2];
532 status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
534 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
537 BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
539 BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
541 UIerr(UI_F_ECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
542 ERR_add_error_data(5, "status=", tmp_num[0],
543 ",", "iosb.iosb$w_value=", tmp_num[1]);
548 # if defined(_WIN32) && !defined(_WIN32_WCE)
551 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
557 static int close_console(UI *ui)
561 if (tty_out != stderr)
563 # ifdef OPENSSL_SYS_VMS
564 status = sys$dassgn(channel);
565 if (status != SS$_NORMAL) {
568 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
569 UIerr(UI_F_CLOSE_CONSOLE, UI_R_SYSDASSGN_ERROR);
570 ERR_add_error_data(2, "status=", tmp_num);
574 CRYPTO_THREAD_unlock(ui->lock);
579 # if !defined(OPENSSL_SYS_WINCE)
580 /* Internal functions to handle signals and act on them */
581 static void pushsig(void)
583 # ifndef OPENSSL_SYS_WIN32
589 memset(&sa, 0, sizeof(sa));
590 sa.sa_handler = recsig;
593 # ifdef OPENSSL_SYS_WIN32
594 savsig[SIGABRT] = signal(SIGABRT, recsig);
595 savsig[SIGFPE] = signal(SIGFPE, recsig);
596 savsig[SIGILL] = signal(SIGILL, recsig);
597 savsig[SIGINT] = signal(SIGINT, recsig);
598 savsig[SIGSEGV] = signal(SIGSEGV, recsig);
599 savsig[SIGTERM] = signal(SIGTERM, recsig);
601 for (i = 1; i < NX509_SIG; i++) {
611 if (i == SIGKILL) /* We can't make any action on that. */
615 sigaction(i, &sa, &savsig[i]);
617 savsig[i] = signal(i, recsig);
623 signal(SIGWINCH, SIG_DFL);
627 static void popsig(void)
629 # ifdef OPENSSL_SYS_WIN32
630 signal(SIGABRT, savsig[SIGABRT]);
631 signal(SIGFPE, savsig[SIGFPE]);
632 signal(SIGILL, savsig[SIGILL]);
633 signal(SIGINT, savsig[SIGINT]);
634 signal(SIGSEGV, savsig[SIGSEGV]);
635 signal(SIGTERM, savsig[SIGTERM]);
638 for (i = 1; i < NX509_SIG; i++) {
648 sigaction(i, &savsig[i], NULL);
650 signal(i, savsig[i]);
656 static void recsig(int i)
662 /* Internal functions specific for Windows */
663 # if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
664 static int noecho_fgets(char *buf, int size, FILE *tty)
689 # ifdef WIN_CONSOLE_BUG
691 * Win95 has several evil console bugs: one of these is that the last
692 * character read using getch() is passed to the next read: this is
693 * usually a CR so this can be trouble. No STDIO fix seems to work but
694 * flushing the console appears to do the trick.
698 inh = GetStdHandle(STD_INPUT_HANDLE);
699 FlushConsoleInputBuffer(inh);
706 static UI_METHOD ui_openssl = {
707 "OpenSSL default user interface",
710 NULL, /* No flusher is needed for command lines */
716 /* The method with all the built-in console thingies */
717 UI_METHOD *UI_OpenSSL(void)
722 static const UI_METHOD *default_UI_meth = &ui_openssl;
726 static const UI_METHOD *default_UI_meth = NULL;
730 void UI_set_default_method(const UI_METHOD *meth)
732 default_UI_meth = meth;
735 const UI_METHOD *UI_get_default_method(void)
737 return default_UI_meth;