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)
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 its 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)
104 # if defined(OPENSSL_SYS_VXWORKS)
111 # include <termios.h>
112 # define TTY_STRUCT struct termios
113 # define TTY_FLAGS c_lflag
114 # define TTY_get(tty,data) tcgetattr(tty,data)
115 # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
120 # define TTY_STRUCT struct termio
121 # define TTY_FLAGS c_lflag
122 # define TTY_get(tty,data) ioctl(tty,TCGETA,data)
123 # define TTY_set(tty,data) ioctl(tty,TCSETA,data)
128 # define TTY_STRUCT struct sgttyb
129 # define TTY_FLAGS sg_flags
130 # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
131 # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
134 # if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
135 # include <sys/ioctl.h>
138 # ifdef OPENSSL_SYS_MSDOS
142 # ifdef OPENSSL_SYS_VMS
146 # include <descrip.h>
155 # define NX509_SIG 32
158 /* Define globals. They are protected by a lock */
160 static struct sigaction savsig
[NX509_SIG
];
162 static void (*savsig
[NX509_SIG
]) (int);
165 # ifdef OPENSSL_SYS_VMS
166 static struct IOSB iosb
;
167 static $
DESCRIPTOR(terminal
, "TT");
168 static long tty_orig
[3], tty_new
[3]; /* XXX Is there any guarantee that this
169 * will always suffice for the actual
172 static unsigned short channel
= 0;
173 # elif defined(_WIN32) && !defined(_WIN32_WCE)
174 static DWORD tty_orig
, tty_new
;
176 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
177 static TTY_STRUCT tty_orig
, tty_new
;
180 static FILE *tty_in
, *tty_out
;
183 /* Declare static functions */
184 # if !defined(OPENSSL_SYS_WINCE)
185 static int read_till_nl(FILE *);
186 static void recsig(int);
187 static void pushsig(void);
188 static void popsig(void);
190 # if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
191 static int noecho_fgets(char *buf
, int size
, FILE *tty
);
193 static int read_string_inner(UI
*ui
, UI_STRING
*uis
, int echo
, int strip_nl
);
195 static int read_string(UI
*ui
, UI_STRING
*uis
);
196 static int write_string(UI
*ui
, UI_STRING
*uis
);
198 static int open_console(UI
*ui
);
199 static int echo_console(UI
*ui
);
200 static int noecho_console(UI
*ui
);
201 static int close_console(UI
*ui
);
204 * The following function makes sure that info and error strings are printed
207 static int write_string(UI
*ui
, UI_STRING
*uis
)
209 switch (UI_get_string_type(uis
)) {
212 fputs(UI_get0_output_string(uis
), tty_out
);
224 static int read_string(UI
*ui
, UI_STRING
*uis
)
228 switch (UI_get_string_type(uis
)) {
230 fputs(UI_get0_output_string(uis
), tty_out
);
231 fputs(UI_get0_action_string(uis
), tty_out
);
233 return read_string_inner(ui
, uis
,
234 UI_get_input_flags(uis
) & UI_INPUT_FLAG_ECHO
,
237 fputs(UI_get0_output_string(uis
), tty_out
);
239 return read_string_inner(ui
, uis
,
240 UI_get_input_flags(uis
) & UI_INPUT_FLAG_ECHO
,
243 fprintf(tty_out
, "Verifying - %s", UI_get0_output_string(uis
));
245 if ((ok
= read_string_inner(ui
, uis
,
246 UI_get_input_flags(uis
) &
247 UI_INPUT_FLAG_ECHO
, 1)) <= 0)
249 if (strcmp(UI_get0_result_string(uis
), UI_get0_test_string(uis
)) != 0) {
250 fprintf(tty_out
, "Verify failure\n");
263 # if !defined(OPENSSL_SYS_WINCE)
264 /* Internal functions to read a string without echoing */
265 static int read_till_nl(FILE *in
)
271 if (!fgets(buf
, SIZE
, in
))
273 } while (strchr(buf
, '\n') == NULL
);
277 static volatile sig_atomic_t intr_signal
;
280 static int read_string_inner(UI
*ui
, UI_STRING
*uis
, int echo
, int strip_nl
)
285 int maxsize
= BUFSIZ
- 1;
286 # if !defined(OPENSSL_SYS_WINCE)
288 int echo_eol
= !echo
;
297 if (!echo
&& !noecho_console(ui
))
305 # if defined(CP_UTF8)
306 if (GetEnvironmentVariableW(L
"OPENSSL_WIN32_UTF8", NULL
, 0) != 0) {
307 WCHAR wresult
[BUFSIZ
];
309 if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE
),
310 wresult
, maxsize
, &numread
, NULL
)) {
312 wresult
[numread
-2] == L
'\r' &&
313 wresult
[numread
-1] == L
'\n') {
314 wresult
[numread
-2] = L
'\n';
317 wresult
[numread
] = '\0';
318 if (WideCharToMultiByte(CP_UTF8
, 0, wresult
, -1,
319 result
, sizeof(result
), NULL
, 0) > 0)
322 OPENSSL_cleanse(wresult
, sizeof(wresult
));
326 if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE
),
327 result
, maxsize
, &numread
, NULL
)) {
329 result
[numread
-2] == '\r' && result
[numread
-1] == '\n') {
330 result
[numread
-2] = '\n';
333 result
[numread
] = '\0';
337 # elif defined(OPENSSL_SYS_MSDOS)
339 noecho_fgets(result
, maxsize
, tty_in
);
340 p
= result
; /* FIXME: noecho_fgets doesn't return errors */
343 p
= fgets(result
, maxsize
, tty_in
);
350 if ((p
= (char *)strchr(result
, '\n')) != NULL
) {
353 } else if (!read_till_nl(tty_in
))
355 if (UI_set_result(ui
, uis
, result
) >= 0)
359 if (intr_signal
== SIGINT
)
362 fprintf(tty_out
, "\n");
363 if (ps
>= 2 && !echo
&& !echo_console(ui
))
372 OPENSSL_cleanse(result
, BUFSIZ
);
376 /* Internal functions to open, handle and close a channel to the console. */
377 static int open_console(UI
*ui
)
379 CRYPTO_THREAD_write_lock(ui
->lock
);
382 # if defined(OPENSSL_SYS_VXWORKS)
385 # elif defined(_WIN32) && !defined(_WIN32_WCE)
386 if ((tty_out
= fopen("conout$", "w")) == NULL
)
389 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), &tty_orig
)) {
393 if ((tty_in
= fopen("conin$", "r")) == NULL
)
397 # ifdef OPENSSL_SYS_MSDOS
398 # define DEV_TTY "con"
400 # define DEV_TTY "/dev/tty"
402 if ((tty_in
= fopen(DEV_TTY
, "r")) == NULL
)
404 if ((tty_out
= fopen(DEV_TTY
, "w")) == NULL
)
408 # if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
409 if (TTY_get(fileno(tty_in
), &tty_orig
) == -1) {
417 * Ariel Glenn reports that solaris can return EINVAL instead.
426 * Solaris can return ENXIO.
435 * Linux can return EIO.
444 * MacOS X returns ENODEV (Operation not supported by device),
445 * which seems appropriate.
453 BIO_snprintf(tmp_num
, sizeof(tmp_num
) - 1, "%d", errno
);
454 UIerr(UI_F_OPEN_CONSOLE
, UI_R_UNKNOWN_TTYGET_ERRNO_VALUE
);
455 ERR_add_error_data(2, "errno=", tmp_num
);
461 # ifdef OPENSSL_SYS_VMS
462 status
= sys$
assign(&terminal
, &channel
, 0, 0);
464 /* if there isn't a TT device, something is very wrong */
465 if (status
!= SS$_NORMAL
) {
468 BIO_snprintf(tmp_num
, sizeof(tmp_num
) - 1, "%%X%08X", status
);
469 UIerr(UI_F_OPEN_CONSOLE
, UI_R_SYSASSIGN_ERROR
);
470 ERR_add_error_data(2, "status=", tmp_num
);
474 status
= sys$
qiow(0, channel
, IO$_SENSEMODE
, &iosb
, 0, 0, tty_orig
, 12,
477 /* If IO$_SENSEMODE doesn't work, this is not a terminal device */
478 if ((status
!= SS$_NORMAL
) || (iosb
.iosb$w_value
!= SS$_NORMAL
))
484 static int noecho_console(UI
*ui
)
487 memcpy(&(tty_new
), &(tty_orig
), sizeof(tty_orig
));
488 tty_new
.TTY_FLAGS
&= ~ECHO
;
491 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
492 if (is_a_tty
&& (TTY_set(fileno(tty_in
), &tty_new
) == -1))
495 # ifdef OPENSSL_SYS_VMS
497 tty_new
[0] = tty_orig
[0];
498 tty_new
[1] = tty_orig
[1] | TT$M_NOECHO
;
499 tty_new
[2] = tty_orig
[2];
500 status
= sys$
qiow(0, channel
, IO$_SETMODE
, &iosb
, 0, 0, tty_new
, 12,
502 if ((status
!= SS$_NORMAL
) || (iosb
.iosb$w_value
!= SS$_NORMAL
)) {
505 BIO_snprintf(tmp_num
[0], sizeof(tmp_num
[0]) - 1, "%%X%08X",
507 BIO_snprintf(tmp_num
[1], sizeof(tmp_num
[1]) - 1, "%%X%08X",
509 UIerr(UI_F_NOECHO_CONSOLE
, UI_R_SYSQIOW_ERROR
);
510 ERR_add_error_data(5, "status=", tmp_num
[0],
511 ",", "iosb.iosb$w_value=", tmp_num
[1]);
516 # if defined(_WIN32) && !defined(_WIN32_WCE)
519 tty_new
&= ~ENABLE_ECHO_INPUT
;
520 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), tty_new
);
526 static int echo_console(UI
*ui
)
528 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
529 memcpy(&(tty_new
), &(tty_orig
), sizeof(tty_orig
));
530 if (is_a_tty
&& (TTY_set(fileno(tty_in
), &tty_new
) == -1))
533 # ifdef OPENSSL_SYS_VMS
535 tty_new
[0] = tty_orig
[0];
536 tty_new
[1] = tty_orig
[1];
537 tty_new
[2] = tty_orig
[2];
538 status
= sys$
qiow(0, channel
, IO$_SETMODE
, &iosb
, 0, 0, tty_new
, 12,
540 if ((status
!= SS$_NORMAL
) || (iosb
.iosb$w_value
!= SS$_NORMAL
)) {
543 BIO_snprintf(tmp_num
[0], sizeof(tmp_num
[0]) - 1, "%%X%08X",
545 BIO_snprintf(tmp_num
[1], sizeof(tmp_num
[1]) - 1, "%%X%08X",
547 UIerr(UI_F_ECHO_CONSOLE
, UI_R_SYSQIOW_ERROR
);
548 ERR_add_error_data(5, "status=", tmp_num
[0],
549 ",", "iosb.iosb$w_value=", tmp_num
[1]);
554 # if defined(_WIN32) && !defined(_WIN32_WCE)
557 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), tty_new
);
563 static int close_console(UI
*ui
)
567 if (tty_out
!= stderr
)
569 # ifdef OPENSSL_SYS_VMS
570 status
= sys$
dassgn(channel
);
571 if (status
!= SS$_NORMAL
) {
574 BIO_snprintf(tmp_num
, sizeof(tmp_num
) - 1, "%%X%08X", status
);
575 UIerr(UI_F_CLOSE_CONSOLE
, UI_R_SYSDASSGN_ERROR
);
576 ERR_add_error_data(2, "status=", tmp_num
);
580 CRYPTO_THREAD_unlock(ui
->lock
);
585 # if !defined(OPENSSL_SYS_WINCE)
586 /* Internal functions to handle signals and act on them */
587 static void pushsig(void)
589 # ifndef OPENSSL_SYS_WIN32
595 memset(&sa
, 0, sizeof(sa
));
596 sa
.sa_handler
= recsig
;
599 # ifdef OPENSSL_SYS_WIN32
600 savsig
[SIGABRT
] = signal(SIGABRT
, recsig
);
601 savsig
[SIGFPE
] = signal(SIGFPE
, recsig
);
602 savsig
[SIGILL
] = signal(SIGILL
, recsig
);
603 savsig
[SIGINT
] = signal(SIGINT
, recsig
);
604 savsig
[SIGSEGV
] = signal(SIGSEGV
, recsig
);
605 savsig
[SIGTERM
] = signal(SIGTERM
, recsig
);
607 for (i
= 1; i
< NX509_SIG
; i
++) {
617 if (i
== SIGKILL
) /* We can't make any action on that. */
621 sigaction(i
, &sa
, &savsig
[i
]);
623 savsig
[i
] = signal(i
, recsig
);
629 signal(SIGWINCH
, SIG_DFL
);
633 static void popsig(void)
635 # ifdef OPENSSL_SYS_WIN32
636 signal(SIGABRT
, savsig
[SIGABRT
]);
637 signal(SIGFPE
, savsig
[SIGFPE
]);
638 signal(SIGILL
, savsig
[SIGILL
]);
639 signal(SIGINT
, savsig
[SIGINT
]);
640 signal(SIGSEGV
, savsig
[SIGSEGV
]);
641 signal(SIGTERM
, savsig
[SIGTERM
]);
644 for (i
= 1; i
< NX509_SIG
; i
++) {
654 sigaction(i
, &savsig
[i
], NULL
);
656 signal(i
, savsig
[i
]);
662 static void recsig(int i
)
668 /* Internal functions specific for Windows */
669 # if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
670 static int noecho_fgets(char *buf
, int size
, FILE *tty
)
695 # ifdef WIN_CONSOLE_BUG
697 * Win95 has several evil console bugs: one of these is that the last
698 * character read using getch() is passed to the next read: this is
699 * usually a CR so this can be trouble. No STDIO fix seems to work but
700 * flushing the console appears to do the trick.
704 inh
= GetStdHandle(STD_INPUT_HANDLE
);
705 FlushConsoleInputBuffer(inh
);
712 static UI_METHOD ui_openssl
= {
713 "OpenSSL default user interface",
716 NULL
, /* No flusher is needed for command lines */
722 /* The method with all the built-in console thingies */
723 UI_METHOD
*UI_OpenSSL(void)
728 static const UI_METHOD
*default_UI_meth
= &ui_openssl
;
732 static const UI_METHOD
*default_UI_meth
= NULL
;
736 void UI_set_default_method(const UI_METHOD
*meth
)
738 default_UI_meth
= meth
;
741 const UI_METHOD
*UI_get_default_method(void)
743 return default_UI_meth
;