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>
13 * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
14 * [maybe others?], because it masks interfaces not discussed in standard,
15 * sigaction and fileno included. -pedantic would be more appropriate for the
16 * intended purposes, but we can't prevent users from adding -ansi.
18 #if defined(OPENSSL_SYS_VXWORKS)
19 # include <sys/types.h>
22 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
23 # ifndef _POSIX_C_SOURCE
24 # define _POSIX_C_SOURCE 2
32 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
33 # ifdef OPENSSL_UNISTD
34 # include OPENSSL_UNISTD
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)
45 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
52 /* 06-Apr-92 Luke Brennan Support for VMS */
54 #include "internal/cryptlib.h"
56 #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
59 # pragma message disable DOLLARID
63 #ifdef WIN_CONSOLE_BUG
65 # ifndef OPENSSL_SYS_WINCE
71 * There are 6 types of terminal interface supported, TERMIO, TERMIOS, VMS,
72 * MSDOS, WIN32 Console and SGTTY.
74 * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
75 * remain respected. Otherwise, we default to TERMIOS except for a few
76 * systems that require something different.
78 * Note: we do not use SGTTY unless it's defined by the configuration. We
79 * may eventually opt to remove it's use entirely.
82 #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
89 * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
91 # elif !defined(OPENSSL_SYS_VMS) \
92 && !defined(OPENSSL_SYS_MSDOS) \
93 && !defined(OPENSSL_SYS_VXWORKS)
102 # include <termios.h>
103 # define TTY_STRUCT struct termios
104 # define TTY_FLAGS c_lflag
105 # define TTY_get(tty,data) tcgetattr(tty,data)
106 # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
111 # define TTY_STRUCT struct termio
112 # define TTY_FLAGS c_lflag
113 # define TTY_get(tty,data) ioctl(tty,TCGETA,data)
114 # define TTY_set(tty,data) ioctl(tty,TCSETA,data)
119 # define TTY_STRUCT struct sgttyb
120 # define TTY_FLAGS sg_flags
121 # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
122 # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
125 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
126 # include <sys/ioctl.h>
129 #ifdef OPENSSL_SYS_MSDOS
133 #ifdef OPENSSL_SYS_VMS
137 # include <descrip.h>
146 # define NX509_SIG 32
149 /* Define globals. They are protected by a lock */
151 static struct sigaction savsig[NX509_SIG];
153 static void (*savsig[NX509_SIG]) (int);
156 #ifdef OPENSSL_SYS_VMS
157 static struct IOSB iosb;
158 static $DESCRIPTOR(terminal, "TT");
159 static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
160 * will always suffice for the actual
163 static unsigned short channel = 0;
164 #elif defined(_WIN32) && !defined(_WIN32_WCE)
165 static DWORD tty_orig, tty_new;
167 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
168 static TTY_STRUCT tty_orig, tty_new;
171 static FILE *tty_in, *tty_out;
174 /* Declare static functions */
175 #if !defined(OPENSSL_SYS_WINCE)
176 static int read_till_nl(FILE *);
177 static void recsig(int);
178 static void pushsig(void);
179 static void popsig(void);
181 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
182 static int noecho_fgets(char *buf, int size, FILE *tty);
184 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
186 static int read_string(UI *ui, UI_STRING *uis);
187 static int write_string(UI *ui, UI_STRING *uis);
189 static int open_console(UI *ui);
190 static int echo_console(UI *ui);
191 static int noecho_console(UI *ui);
192 static int close_console(UI *ui);
194 static UI_METHOD ui_openssl = {
195 "OpenSSL default user interface",
198 NULL, /* No flusher is needed for command lines */
204 /* The method with all the built-in thingies */
205 UI_METHOD *UI_OpenSSL(void)
211 * The following function makes sure that info and error strings are printed
214 static int write_string(UI *ui, UI_STRING *uis)
216 switch (UI_get_string_type(uis)) {
219 fputs(UI_get0_output_string(uis), tty_out);
231 static int read_string(UI *ui, UI_STRING *uis)
235 switch (UI_get_string_type(uis)) {
237 fputs(UI_get0_output_string(uis), tty_out);
238 fputs(UI_get0_action_string(uis), tty_out);
240 return read_string_inner(ui, uis,
241 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
244 fputs(UI_get0_output_string(uis), tty_out);
246 return read_string_inner(ui, uis,
247 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
250 fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
252 if ((ok = read_string_inner(ui, uis,
253 UI_get_input_flags(uis) &
254 UI_INPUT_FLAG_ECHO, 1)) <= 0)
256 if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
257 fprintf(tty_out, "Verify failure\n");
270 #if !defined(OPENSSL_SYS_WINCE)
271 /* Internal functions to read a string without echoing */
272 static int read_till_nl(FILE *in)
278 if (!fgets(buf, SIZE, in))
280 } while (strchr(buf, '\n') == NULL);
284 static volatile sig_atomic_t intr_signal;
287 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
292 int maxsize = BUFSIZ - 1;
293 #if !defined(OPENSSL_SYS_WINCE)
295 int echo_eol = !echo;
304 if (!echo && !noecho_console(ui))
312 # if defined(CP_UTF8)
313 if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
314 WCHAR wresult[BUFSIZ];
316 if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
317 wresult, maxsize, &numread, NULL)) {
319 wresult[numread-2] == L'\r' &&
320 wresult[numread-1] == L'\n') {
321 wresult[numread-2] = L'\n';
324 wresult[numread] = '\0';
325 if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
326 result, sizeof(result), NULL, 0) > 0)
329 OPENSSL_cleanse(wresult, sizeof(wresult));
333 if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
334 result, maxsize, &numread, NULL)) {
336 result[numread-2] == '\r' && result[numread-1] == '\n') {
337 result[numread-2] = '\n';
340 result[numread] = '\0';
344 # elif defined(OPENSSL_SYS_MSDOS)
346 noecho_fgets(result, maxsize, tty_in);
347 p = result; /* FIXME: noecho_fgets doesn't return errors */
350 p = fgets(result, maxsize, tty_in);
357 if ((p = (char *)strchr(result, '\n')) != NULL) {
360 } else if (!read_till_nl(tty_in))
362 if (UI_set_result(ui, uis, result) >= 0)
366 if (intr_signal == SIGINT)
369 fprintf(tty_out, "\n");
370 if (ps >= 2 && !echo && !echo_console(ui))
379 OPENSSL_cleanse(result, BUFSIZ);
383 /* Internal functions to open, handle and close a channel to the console. */
384 static int open_console(UI *ui)
386 CRYPTO_THREAD_write_lock(ui->lock);
389 #if defined(OPENSSL_SYS_VXWORKS)
392 #elif defined(_WIN32) && !defined(_WIN32_WCE)
393 if ((tty_out = fopen("conout$", "w")) == NULL)
396 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) {
400 if ((tty_in = fopen("conin$", "r")) == NULL)
404 # ifdef OPENSSL_SYS_MSDOS
405 # define DEV_TTY "con"
407 # define DEV_TTY "/dev/tty"
409 if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
411 if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
415 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
416 if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
424 * Ariel Glenn ariel@columbia.edu reports that solaris can return
425 * EINVAL instead. This should be ok
434 #ifdef OPENSSL_SYS_VMS
435 status = sys$assign(&terminal, &channel, 0, 0);
436 if (status != SS$_NORMAL)
439 sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
441 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
447 static int noecho_console(UI *ui)
450 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
451 tty_new.TTY_FLAGS &= ~ECHO;
454 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
455 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
458 #ifdef OPENSSL_SYS_VMS
459 tty_new[0] = tty_orig[0];
460 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
461 tty_new[2] = tty_orig[2];
463 sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
465 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
468 #if defined(_WIN32) && !defined(_WIN32_WCE)
471 tty_new &= ~ENABLE_ECHO_INPUT;
472 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
478 static int echo_console(UI *ui)
480 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
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
490 tty_new[0] = tty_orig[0];
491 tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
492 tty_new[2] = tty_orig[2];
494 sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
496 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
499 #if defined(_WIN32) && !defined(_WIN32_WCE)
502 tty_new |= ENABLE_ECHO_INPUT;
503 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
509 static int close_console(UI *ui)
513 if (tty_out != stderr)
515 #ifdef OPENSSL_SYS_VMS
516 status = sys$dassgn(channel);
518 CRYPTO_THREAD_unlock(ui->lock);
523 #if !defined(OPENSSL_SYS_WINCE)
524 /* Internal functions to handle signals and act on them */
525 static void pushsig(void)
527 # ifndef OPENSSL_SYS_WIN32
533 memset(&sa, 0, sizeof(sa));
534 sa.sa_handler = recsig;
537 # ifdef OPENSSL_SYS_WIN32
538 savsig[SIGABRT] = signal(SIGABRT, recsig);
539 savsig[SIGFPE] = signal(SIGFPE, recsig);
540 savsig[SIGILL] = signal(SIGILL, recsig);
541 savsig[SIGINT] = signal(SIGINT, recsig);
542 savsig[SIGSEGV] = signal(SIGSEGV, recsig);
543 savsig[SIGTERM] = signal(SIGTERM, recsig);
545 for (i = 1; i < NX509_SIG; i++) {
555 if (i == SIGKILL) /* We can't make any action on that. */
559 sigaction(i, &sa, &savsig[i]);
561 savsig[i] = signal(i, recsig);
567 signal(SIGWINCH, SIG_DFL);
571 static void popsig(void)
573 # ifdef OPENSSL_SYS_WIN32
574 signal(SIGABRT, savsig[SIGABRT]);
575 signal(SIGFPE, savsig[SIGFPE]);
576 signal(SIGILL, savsig[SIGILL]);
577 signal(SIGINT, savsig[SIGINT]);
578 signal(SIGSEGV, savsig[SIGSEGV]);
579 signal(SIGTERM, savsig[SIGTERM]);
582 for (i = 1; i < NX509_SIG; i++) {
592 sigaction(i, &savsig[i], NULL);
594 signal(i, savsig[i]);
600 static void recsig(int i)
606 /* Internal functions specific for Windows */
607 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
608 static int noecho_fgets(char *buf, int size, FILE *tty)
633 # ifdef WIN_CONSOLE_BUG
635 * Win95 has several evil console bugs: one of these is that the last
636 * character read using getch() is passed to the next read: this is
637 * usually a CR so this can be trouble. No STDIO fix seems to work but
638 * flushing the console appears to do the trick.
642 inh = GetStdHandle(STD_INPUT_HANDLE);
643 FlushConsoleInputBuffer(inh);
646 return (strlen(buf));