2 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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)
39 * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
40 * system and have sigaction and termios.
42 # if defined(_POSIX_VERSION) && _POSIX_VERSION>=199309L
45 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
53 # include "internal/cryptlib.h"
55 # ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
58 # pragma message disable DOLLARID
62 # ifdef WIN_CONSOLE_BUG
64 # ifndef OPENSSL_SYS_WINCE
70 * There are 6 types of terminal interface supported, TERMIO, TERMIOS, VMS,
71 * MSDOS, WIN32 Console and SGTTY.
73 * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
74 * remain respected. Otherwise, we default to TERMIOS except for a few
75 * systems that require something different.
77 * Note: we do not use SGTTY unless it's defined by the configuration. We
78 * may eventually opt to remove its use entirely.
81 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
88 * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
90 # elif !defined(OPENSSL_SYS_VMS) \
91 && !defined(OPENSSL_SYS_MSDOS) \
92 && !defined(OPENSSL_SYS_VXWORKS)
100 # if defined(OPENSSL_SYS_VXWORKS)
107 # include <termios.h>
108 # define TTY_STRUCT struct termios
109 # define TTY_FLAGS c_lflag
110 # define TTY_get(tty,data) tcgetattr(tty,data)
111 # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
116 # define TTY_STRUCT struct termio
117 # define TTY_FLAGS c_lflag
118 # define TTY_get(tty,data) ioctl(tty,TCGETA,data)
119 # define TTY_set(tty,data) ioctl(tty,TCSETA,data)
124 # define TTY_STRUCT struct sgttyb
125 # define TTY_FLAGS sg_flags
126 # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
127 # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
130 # if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
131 # include <sys/ioctl.h>
134 # ifdef OPENSSL_SYS_MSDOS
138 # ifdef OPENSSL_SYS_VMS
142 # include <descrip.h>
151 # define NX509_SIG 32
154 /* Define globals. They are protected by a lock */
156 static struct sigaction savsig
[NX509_SIG
];
158 static void (*savsig
[NX509_SIG
]) (int);
161 # ifdef OPENSSL_SYS_VMS
162 static struct IOSB iosb
;
163 static $
DESCRIPTOR(terminal
, "TT");
164 static long tty_orig
[3], tty_new
[3]; /* XXX Is there any guarantee that this
165 * will always suffice for the actual
168 static unsigned short channel
= 0;
169 # elif defined(_WIN32) && !defined(_WIN32_WCE)
170 static DWORD tty_orig
, tty_new
;
172 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
173 static TTY_STRUCT tty_orig
, tty_new
;
176 static FILE *tty_in
, *tty_out
;
179 /* Declare static functions */
180 # if !defined(OPENSSL_SYS_WINCE)
181 static int read_till_nl(FILE *);
182 static void recsig(int);
183 static void pushsig(void);
184 static void popsig(void);
186 # if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
187 static int noecho_fgets(char *buf
, int size
, FILE *tty
);
189 static int read_string_inner(UI
*ui
, UI_STRING
*uis
, int echo
, int strip_nl
);
191 static int read_string(UI
*ui
, UI_STRING
*uis
);
192 static int write_string(UI
*ui
, UI_STRING
*uis
);
194 static int open_console(UI
*ui
);
195 static int echo_console(UI
*ui
);
196 static int noecho_console(UI
*ui
);
197 static int close_console(UI
*ui
);
200 * The following function makes sure that info and error strings are printed
203 static int write_string(UI
*ui
, UI_STRING
*uis
)
205 switch (UI_get_string_type(uis
)) {
208 fputs(UI_get0_output_string(uis
), tty_out
);
220 static int read_string(UI
*ui
, UI_STRING
*uis
)
224 switch (UI_get_string_type(uis
)) {
226 fputs(UI_get0_output_string(uis
), tty_out
);
227 fputs(UI_get0_action_string(uis
), tty_out
);
229 return read_string_inner(ui
, uis
,
230 UI_get_input_flags(uis
) & UI_INPUT_FLAG_ECHO
,
233 fputs(UI_get0_output_string(uis
), tty_out
);
235 return read_string_inner(ui
, uis
,
236 UI_get_input_flags(uis
) & UI_INPUT_FLAG_ECHO
,
239 fprintf(tty_out
, "Verifying - %s", UI_get0_output_string(uis
));
241 if ((ok
= read_string_inner(ui
, uis
,
242 UI_get_input_flags(uis
) &
243 UI_INPUT_FLAG_ECHO
, 1)) <= 0)
245 if (strcmp(UI_get0_result_string(uis
), UI_get0_test_string(uis
)) != 0) {
246 fprintf(tty_out
, "Verify failure\n");
259 # if !defined(OPENSSL_SYS_WINCE)
260 /* Internal functions to read a string without echoing */
261 static int read_till_nl(FILE *in
)
267 if (!fgets(buf
, SIZE
, in
))
269 } while (strchr(buf
, '\n') == NULL
);
273 static volatile sig_atomic_t intr_signal
;
276 static int read_string_inner(UI
*ui
, UI_STRING
*uis
, int echo
, int strip_nl
)
281 int maxsize
= BUFSIZ
- 1;
282 # if !defined(OPENSSL_SYS_WINCE)
284 int echo_eol
= !echo
;
293 if (!echo
&& !noecho_console(ui
))
301 # if defined(CP_UTF8)
302 if (GetEnvironmentVariableW(L
"OPENSSL_WIN32_UTF8", NULL
, 0) != 0) {
303 WCHAR wresult
[BUFSIZ
];
305 if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE
),
306 wresult
, maxsize
, &numread
, NULL
)) {
308 wresult
[numread
-2] == L
'\r' &&
309 wresult
[numread
-1] == L
'\n') {
310 wresult
[numread
-2] = L
'\n';
313 wresult
[numread
] = '\0';
314 if (WideCharToMultiByte(CP_UTF8
, 0, wresult
, -1,
315 result
, sizeof(result
), NULL
, 0) > 0)
318 OPENSSL_cleanse(wresult
, sizeof(wresult
));
322 if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE
),
323 result
, maxsize
, &numread
, NULL
)) {
325 result
[numread
-2] == '\r' && result
[numread
-1] == '\n') {
326 result
[numread
-2] = '\n';
329 result
[numread
] = '\0';
333 # elif defined(OPENSSL_SYS_MSDOS)
335 noecho_fgets(result
, maxsize
, tty_in
);
336 p
= result
; /* FIXME: noecho_fgets doesn't return errors */
339 p
= fgets(result
, maxsize
, tty_in
);
346 if ((p
= (char *)strchr(result
, '\n')) != NULL
) {
349 } else if (!read_till_nl(tty_in
))
351 if (UI_set_result(ui
, uis
, result
) >= 0)
355 if (intr_signal
== SIGINT
)
358 fprintf(tty_out
, "\n");
359 if (ps
>= 2 && !echo
&& !echo_console(ui
))
368 OPENSSL_cleanse(result
, BUFSIZ
);
372 /* Internal functions to open, handle and close a channel to the console. */
373 static int open_console(UI
*ui
)
375 CRYPTO_THREAD_write_lock(ui
->lock
);
378 # if defined(OPENSSL_SYS_VXWORKS)
381 # elif defined(_WIN32) && !defined(_WIN32_WCE)
382 if ((tty_out
= fopen("conout$", "w")) == NULL
)
385 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), &tty_orig
)) {
389 if ((tty_in
= fopen("conin$", "r")) == NULL
)
393 # ifdef OPENSSL_SYS_MSDOS
394 # define DEV_TTY "con"
396 # define DEV_TTY "/dev/tty"
398 if ((tty_in
= fopen(DEV_TTY
, "r")) == NULL
)
400 if ((tty_out
= fopen(DEV_TTY
, "w")) == NULL
)
404 # if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
405 if (TTY_get(fileno(tty_in
), &tty_orig
) == -1) {
413 * Ariel Glenn reports that solaris can return EINVAL instead.
422 * Solaris can return ENXIO.
431 * Linux can return EIO.
440 * MacOS X returns ENODEV (Operation not supported by device),
441 * which seems appropriate.
449 BIO_snprintf(tmp_num
, sizeof(tmp_num
) - 1, "%d", errno
);
450 UIerr(UI_F_OPEN_CONSOLE
, UI_R_UNKNOWN_TTYGET_ERRNO_VALUE
);
451 ERR_add_error_data(2, "errno=", tmp_num
);
457 # ifdef OPENSSL_SYS_VMS
458 status
= sys$
assign(&terminal
, &channel
, 0, 0);
460 /* if there isn't a TT device, something is very wrong */
461 if (status
!= SS$_NORMAL
) {
464 BIO_snprintf(tmp_num
, sizeof(tmp_num
) - 1, "%%X%08X", status
);
465 UIerr(UI_F_OPEN_CONSOLE
, UI_R_SYSASSIGN_ERROR
);
466 ERR_add_error_data(2, "status=", tmp_num
);
470 status
= sys$
qiow(0, channel
, IO$_SENSEMODE
, &iosb
, 0, 0, tty_orig
, 12,
473 /* If IO$_SENSEMODE doesn't work, this is not a terminal device */
474 if ((status
!= SS$_NORMAL
) || (iosb
.iosb$w_value
!= SS$_NORMAL
))
480 static int noecho_console(UI
*ui
)
483 memcpy(&(tty_new
), &(tty_orig
), sizeof(tty_orig
));
484 tty_new
.TTY_FLAGS
&= ~ECHO
;
487 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
488 if (is_a_tty
&& (TTY_set(fileno(tty_in
), &tty_new
) == -1))
491 # ifdef OPENSSL_SYS_VMS
493 tty_new
[0] = tty_orig
[0];
494 tty_new
[1] = tty_orig
[1] | TT$M_NOECHO
;
495 tty_new
[2] = tty_orig
[2];
496 status
= sys$
qiow(0, channel
, IO$_SETMODE
, &iosb
, 0, 0, tty_new
, 12,
498 if ((status
!= SS$_NORMAL
) || (iosb
.iosb$w_value
!= SS$_NORMAL
)) {
501 BIO_snprintf(tmp_num
[0], sizeof(tmp_num
[0]) - 1, "%%X%08X",
503 BIO_snprintf(tmp_num
[1], sizeof(tmp_num
[1]) - 1, "%%X%08X",
505 UIerr(UI_F_NOECHO_CONSOLE
, UI_R_SYSQIOW_ERROR
);
506 ERR_add_error_data(5, "status=", tmp_num
[0],
507 ",", "iosb.iosb$w_value=", tmp_num
[1]);
512 # if defined(_WIN32) && !defined(_WIN32_WCE)
515 tty_new
&= ~ENABLE_ECHO_INPUT
;
516 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), tty_new
);
522 static int echo_console(UI
*ui
)
524 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
525 memcpy(&(tty_new
), &(tty_orig
), sizeof(tty_orig
));
526 if (is_a_tty
&& (TTY_set(fileno(tty_in
), &tty_new
) == -1))
529 # ifdef OPENSSL_SYS_VMS
531 tty_new
[0] = tty_orig
[0];
532 tty_new
[1] = tty_orig
[1];
533 tty_new
[2] = tty_orig
[2];
534 status
= sys$
qiow(0, channel
, IO$_SETMODE
, &iosb
, 0, 0, tty_new
, 12,
536 if ((status
!= SS$_NORMAL
) || (iosb
.iosb$w_value
!= SS$_NORMAL
)) {
539 BIO_snprintf(tmp_num
[0], sizeof(tmp_num
[0]) - 1, "%%X%08X",
541 BIO_snprintf(tmp_num
[1], sizeof(tmp_num
[1]) - 1, "%%X%08X",
543 UIerr(UI_F_ECHO_CONSOLE
, UI_R_SYSQIOW_ERROR
);
544 ERR_add_error_data(5, "status=", tmp_num
[0],
545 ",", "iosb.iosb$w_value=", tmp_num
[1]);
550 # if defined(_WIN32) && !defined(_WIN32_WCE)
553 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), tty_new
);
559 static int close_console(UI
*ui
)
563 if (tty_out
!= stderr
)
565 # ifdef OPENSSL_SYS_VMS
566 status
= sys$
dassgn(channel
);
567 if (status
!= SS$_NORMAL
) {
570 BIO_snprintf(tmp_num
, sizeof(tmp_num
) - 1, "%%X%08X", status
);
571 UIerr(UI_F_CLOSE_CONSOLE
, UI_R_SYSDASSGN_ERROR
);
572 ERR_add_error_data(2, "status=", tmp_num
);
576 CRYPTO_THREAD_unlock(ui
->lock
);
581 # if !defined(OPENSSL_SYS_WINCE)
582 /* Internal functions to handle signals and act on them */
583 static void pushsig(void)
585 # ifndef OPENSSL_SYS_WIN32
591 memset(&sa
, 0, sizeof(sa
));
592 sa
.sa_handler
= recsig
;
595 # ifdef OPENSSL_SYS_WIN32
596 savsig
[SIGABRT
] = signal(SIGABRT
, recsig
);
597 savsig
[SIGFPE
] = signal(SIGFPE
, recsig
);
598 savsig
[SIGILL
] = signal(SIGILL
, recsig
);
599 savsig
[SIGINT
] = signal(SIGINT
, recsig
);
600 savsig
[SIGSEGV
] = signal(SIGSEGV
, recsig
);
601 savsig
[SIGTERM
] = signal(SIGTERM
, recsig
);
603 for (i
= 1; i
< NX509_SIG
; i
++) {
613 if (i
== SIGKILL
) /* We can't make any action on that. */
617 sigaction(i
, &sa
, &savsig
[i
]);
619 savsig
[i
] = signal(i
, recsig
);
625 signal(SIGWINCH
, SIG_DFL
);
629 static void popsig(void)
631 # ifdef OPENSSL_SYS_WIN32
632 signal(SIGABRT
, savsig
[SIGABRT
]);
633 signal(SIGFPE
, savsig
[SIGFPE
]);
634 signal(SIGILL
, savsig
[SIGILL
]);
635 signal(SIGINT
, savsig
[SIGINT
]);
636 signal(SIGSEGV
, savsig
[SIGSEGV
]);
637 signal(SIGTERM
, savsig
[SIGTERM
]);
640 for (i
= 1; i
< NX509_SIG
; i
++) {
650 sigaction(i
, &savsig
[i
], NULL
);
652 signal(i
, savsig
[i
]);
658 static void recsig(int i
)
664 /* Internal functions specific for Windows */
665 # if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
666 static int noecho_fgets(char *buf
, int size
, FILE *tty
)
691 # ifdef WIN_CONSOLE_BUG
693 * Win95 has several evil console bugs: one of these is that the last
694 * character read using getch() is passed to the next read: this is
695 * usually a CR so this can be trouble. No STDIO fix seems to work but
696 * flushing the console appears to do the trick.
700 inh
= GetStdHandle(STD_INPUT_HANDLE
);
701 FlushConsoleInputBuffer(inh
);
708 static UI_METHOD ui_openssl
= {
709 "OpenSSL default user interface",
712 NULL
, /* No flusher is needed for command lines */
718 /* The method with all the built-in console thingies */
719 UI_METHOD
*UI_OpenSSL(void)
724 static const UI_METHOD
*default_UI_meth
= &ui_openssl
;
728 static const UI_METHOD
*default_UI_meth
= NULL
;
732 void UI_set_default_method(const UI_METHOD
*meth
)
734 default_UI_meth
= meth
;
737 const UI_METHOD
*UI_get_default_method(void)
739 return default_UI_meth
;