2 * Copyright 2001-2016 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>
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.
19 #if defined(OPENSSL_SYS_VXWORKS)
20 # include <sys/types.h>
23 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
24 # ifndef _POSIX_C_SOURCE
25 # define _POSIX_C_SOURCE 2
33 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
34 # ifdef OPENSSL_UNISTD
35 # include OPENSSL_UNISTD
40 * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
41 * system and have sigaction and termios.
43 # if defined(_POSIX_VERSION)
46 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
53 /* 06-Apr-92 Luke Brennan Support for VMS */
55 #include "internal/cryptlib.h"
57 #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
60 # pragma message disable DOLLARID
64 #ifdef WIN_CONSOLE_BUG
66 # ifndef OPENSSL_SYS_WINCE
72 * There are 6 types of terminal interface supported, TERMIO, TERMIOS, VMS,
73 * MSDOS, WIN32 Console and SGTTY.
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.
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.
83 #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
90 * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
92 # elif !defined(OPENSSL_SYS_VMS) \
93 && !defined(OPENSSL_SYS_MSDOS) \
94 && !defined(OPENSSL_SYS_VXWORKS)
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)
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)
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)
126 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
127 # include <sys/ioctl.h>
130 #ifdef OPENSSL_SYS_MSDOS
134 #ifdef OPENSSL_SYS_VMS
138 # include <descrip.h>
147 # define NX509_SIG 32
150 /* Define globals. They are protected by a lock */
152 static struct sigaction savsig[NX509_SIG];
154 static void (*savsig[NX509_SIG]) (int);
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
164 static unsigned short channel = 0;
165 #elif defined(_WIN32) && !defined(_WIN32_WCE)
166 static DWORD tty_orig, tty_new;
168 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
169 static TTY_STRUCT tty_orig, tty_new;
172 static FILE *tty_in, *tty_out;
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);
182 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
183 static int noecho_fgets(char *buf, int size, FILE *tty);
185 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
187 static int read_string(UI *ui, UI_STRING *uis);
188 static int write_string(UI *ui, UI_STRING *uis);
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);
195 static UI_METHOD ui_openssl = {
196 "OpenSSL default user interface",
199 NULL, /* No flusher is needed for command lines */
205 static const UI_METHOD *default_UI_meth = &ui_openssl;
207 void UI_set_default_method(const UI_METHOD *meth)
209 default_UI_meth = meth;
212 const UI_METHOD *UI_get_default_method(void)
214 return default_UI_meth;
217 /* The method with all the built-in thingies */
218 UI_METHOD *UI_OpenSSL(void)
224 * The following function makes sure that info and error strings are printed
227 static int write_string(UI *ui, UI_STRING *uis)
229 switch (UI_get_string_type(uis)) {
232 fputs(UI_get0_output_string(uis), tty_out);
244 static int read_string(UI *ui, UI_STRING *uis)
248 switch (UI_get_string_type(uis)) {
250 fputs(UI_get0_output_string(uis), tty_out);
251 fputs(UI_get0_action_string(uis), tty_out);
253 return read_string_inner(ui, uis,
254 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
257 fputs(UI_get0_output_string(uis), tty_out);
259 return read_string_inner(ui, uis,
260 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
263 fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
265 if ((ok = read_string_inner(ui, uis,
266 UI_get_input_flags(uis) &
267 UI_INPUT_FLAG_ECHO, 1)) <= 0)
269 if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
270 fprintf(tty_out, "Verify failure\n");
283 #if !defined(OPENSSL_SYS_WINCE)
284 /* Internal functions to read a string without echoing */
285 static int read_till_nl(FILE *in)
291 if (!fgets(buf, SIZE, in))
293 } while (strchr(buf, '\n') == NULL);
297 static volatile sig_atomic_t intr_signal;
300 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
305 int maxsize = BUFSIZ - 1;
306 #if !defined(OPENSSL_SYS_WINCE)
308 int echo_eol = !echo;
317 if (!echo && !noecho_console(ui))
325 # if defined(CP_UTF8)
326 if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
327 WCHAR wresult[BUFSIZ];
329 if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
330 wresult, maxsize, &numread, NULL)) {
332 wresult[numread-2] == L'\r' &&
333 wresult[numread-1] == L'\n') {
334 wresult[numread-2] = L'\n';
337 wresult[numread] = '\0';
338 if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
339 result, sizeof(result), NULL, 0) > 0)
342 OPENSSL_cleanse(wresult, sizeof(wresult));
346 if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
347 result, maxsize, &numread, NULL)) {
349 result[numread-2] == '\r' && result[numread-1] == '\n') {
350 result[numread-2] = '\n';
353 result[numread] = '\0';
357 # elif defined(OPENSSL_SYS_MSDOS)
359 noecho_fgets(result, maxsize, tty_in);
360 p = result; /* FIXME: noecho_fgets doesn't return errors */
363 p = fgets(result, maxsize, tty_in);
370 if ((p = (char *)strchr(result, '\n')) != NULL) {
373 } else if (!read_till_nl(tty_in))
375 if (UI_set_result(ui, uis, result) >= 0)
379 if (intr_signal == SIGINT)
382 fprintf(tty_out, "\n");
383 if (ps >= 2 && !echo && !echo_console(ui))
392 OPENSSL_cleanse(result, BUFSIZ);
396 /* Internal functions to open, handle and close a channel to the console. */
397 static int open_console(UI *ui)
399 CRYPTO_THREAD_write_lock(ui->lock);
402 #if defined(OPENSSL_SYS_VXWORKS)
405 #elif defined(_WIN32) && !defined(_WIN32_WCE)
406 if ((tty_out = fopen("conout$", "w")) == NULL)
409 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) {
413 if ((tty_in = fopen("conin$", "r")) == NULL)
417 # ifdef OPENSSL_SYS_MSDOS
418 # define DEV_TTY "con"
420 # define DEV_TTY "/dev/tty"
422 if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
424 if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
428 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
429 if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
437 * Ariel Glenn ariel@columbia.edu reports that solaris can return
438 * EINVAL instead. This should be ok
446 * MacOS X returns ENODEV (Operation not supported by device),
447 * which seems appropriate.
455 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%d", errno);
456 UIerr(UI_F_OPEN_CONSOLE, UI_R_UNKNOWN_TTYGET_ERRNO_VALUE);
457 ERR_add_error_data(2, "errno=", tmp_num);
463 #ifdef OPENSSL_SYS_VMS
464 status = sys$assign(&terminal, &channel, 0, 0);
466 /* if there isn't a TT device, something is very wrong */
467 if (status != SS$_NORMAL) {
470 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
471 UIerr(UI_F_OPEN_CONSOLE, UI_R_SYSASSIGN_ERROR);
472 ERR_add_error_data(2, "status=", tmp_num);
476 status = sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12,
479 /* If IO$_SENSEMODE doesn't work, this is not a terminal device */
480 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
486 static int noecho_console(UI *ui)
489 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
490 tty_new.TTY_FLAGS &= ~ECHO;
493 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
494 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
497 #ifdef OPENSSL_SYS_VMS
499 tty_new[0] = tty_orig[0];
500 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
501 tty_new[2] = tty_orig[2];
502 status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
504 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
507 BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
509 BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
511 UIerr(UI_F_NOECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
512 ERR_add_error_data(5, "status=", tmp_num[0],
513 ",", "iosb.iosb$w_value=", tmp_num[1]);
518 #if defined(_WIN32) && !defined(_WIN32_WCE)
521 tty_new &= ~ENABLE_ECHO_INPUT;
522 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
528 static int echo_console(UI *ui)
530 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
531 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
532 tty_new.TTY_FLAGS |= ECHO;
535 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
536 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
539 #ifdef OPENSSL_SYS_VMS
541 tty_new[0] = tty_orig[0];
542 tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
543 tty_new[2] = tty_orig[2];
544 status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
546 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
549 BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
551 BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
553 UIerr(UI_F_ECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
554 ERR_add_error_data(5, "status=", tmp_num[0],
555 ",", "iosb.iosb$w_value=", tmp_num[1]);
560 #if defined(_WIN32) && !defined(_WIN32_WCE)
563 tty_new |= ENABLE_ECHO_INPUT;
564 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
570 static int close_console(UI *ui)
574 if (tty_out != stderr)
576 #ifdef OPENSSL_SYS_VMS
577 status = sys$dassgn(channel);
578 if (status != SS$_NORMAL) {
581 BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
582 UIerr(UI_F_CLOSE_CONSOLE, UI_R_SYSDASSGN_ERROR);
583 ERR_add_error_data(2, "status=", tmp_num);
587 CRYPTO_THREAD_unlock(ui->lock);
592 #if !defined(OPENSSL_SYS_WINCE)
593 /* Internal functions to handle signals and act on them */
594 static void pushsig(void)
596 # ifndef OPENSSL_SYS_WIN32
602 memset(&sa, 0, sizeof(sa));
603 sa.sa_handler = recsig;
606 # ifdef OPENSSL_SYS_WIN32
607 savsig[SIGABRT] = signal(SIGABRT, recsig);
608 savsig[SIGFPE] = signal(SIGFPE, recsig);
609 savsig[SIGILL] = signal(SIGILL, recsig);
610 savsig[SIGINT] = signal(SIGINT, recsig);
611 savsig[SIGSEGV] = signal(SIGSEGV, recsig);
612 savsig[SIGTERM] = signal(SIGTERM, recsig);
614 for (i = 1; i < NX509_SIG; i++) {
624 if (i == SIGKILL) /* We can't make any action on that. */
628 sigaction(i, &sa, &savsig[i]);
630 savsig[i] = signal(i, recsig);
636 signal(SIGWINCH, SIG_DFL);
640 static void popsig(void)
642 # ifdef OPENSSL_SYS_WIN32
643 signal(SIGABRT, savsig[SIGABRT]);
644 signal(SIGFPE, savsig[SIGFPE]);
645 signal(SIGILL, savsig[SIGILL]);
646 signal(SIGINT, savsig[SIGINT]);
647 signal(SIGSEGV, savsig[SIGSEGV]);
648 signal(SIGTERM, savsig[SIGTERM]);
651 for (i = 1; i < NX509_SIG; i++) {
661 sigaction(i, &savsig[i], NULL);
663 signal(i, savsig[i]);
669 static void recsig(int i)
675 /* Internal functions specific for Windows */
676 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
677 static int noecho_fgets(char *buf, int size, FILE *tty)
702 # ifdef WIN_CONSOLE_BUG
704 * Win95 has several evil console bugs: one of these is that the last
705 * character read using getch() is passed to the next read: this is
706 * usually a CR so this can be trouble. No STDIO fix seems to work but
707 * flushing the console appears to do the trick.
711 inh = GetStdHandle(STD_INPUT_HANDLE);
712 FlushConsoleInputBuffer(inh);
715 return (strlen(buf));