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);
228 static int read_string(UI *ui, UI_STRING *uis)
232 switch (UI_get_string_type(uis)) {
234 fputs(UI_get0_output_string(uis), tty_out);
235 fputs(UI_get0_action_string(uis), tty_out);
237 return read_string_inner(ui, uis,
238 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
241 fputs(UI_get0_output_string(uis), tty_out);
243 return read_string_inner(ui, uis,
244 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
247 fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
249 if ((ok = read_string_inner(ui, uis,
250 UI_get_input_flags(uis) &
251 UI_INPUT_FLAG_ECHO, 1)) <= 0)
253 if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
254 fprintf(tty_out, "Verify failure\n");
265 #if !defined(OPENSSL_SYS_WINCE)
266 /* Internal functions to read a string without echoing */
267 static int read_till_nl(FILE *in)
273 if (!fgets(buf, SIZE, in))
275 } while (strchr(buf, '\n') == NULL);
279 static volatile sig_atomic_t intr_signal;
282 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
287 int maxsize = BUFSIZ - 1;
288 #if !defined(OPENSSL_SYS_WINCE)
290 int echo_eol = !echo;
299 if (!echo && !noecho_console(ui))
307 # if defined(CP_UTF8)
308 WCHAR wresult[BUFSIZ];
310 if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
311 wresult, maxsize, &numread, NULL)) {
313 wresult[numread-2] == L'\r' && wresult[numread-1] == L'\n') {
314 wresult[numread-2] = L'\n';
318 wresult[numread] = '\0';
319 if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
320 result, sizeof(result), NULL, 0) > 0)
323 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';
334 result[numread] = '\0';
339 # elif defined(OPENSSL_SYS_MSDOS)
341 noecho_fgets(result, maxsize, tty_in);
342 p = result; /* FIXME: noecho_fgets doesn't return errors */
345 p = fgets(result, maxsize, tty_in);
352 if ((p = (char *)strchr(result, '\n')) != NULL) {
355 } else if (!read_till_nl(tty_in))
357 if (UI_set_result(ui, uis, result) >= 0)
361 if (intr_signal == SIGINT)
364 fprintf(tty_out, "\n");
365 if (ps >= 2 && !echo && !echo_console(ui))
374 OPENSSL_cleanse(result, BUFSIZ);
378 /* Internal functions to open, handle and close a channel to the console. */
379 static int open_console(UI *ui)
381 CRYPTO_THREAD_write_lock(ui->lock);
384 #if defined(OPENSSL_SYS_VXWORKS)
387 #elif defined(_WIN32) && !defined(_WIN32_WCE)
388 if ((tty_out = fopen("conout$", "w")) == NULL)
391 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) {
395 if ((tty_in = fopen("conin$", "r")) == NULL)
399 # ifdef OPENSSL_SYS_MSDOS
400 # define DEV_TTY "con"
402 # define DEV_TTY "/dev/tty"
404 if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
406 if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
410 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
411 if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
419 * Ariel Glenn ariel@columbia.edu reports that solaris can return
420 * EINVAL instead. This should be ok
429 #ifdef OPENSSL_SYS_VMS
430 status = sys$assign(&terminal, &channel, 0, 0);
431 if (status != SS$_NORMAL)
434 sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
436 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
442 static int noecho_console(UI *ui)
445 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
446 tty_new.TTY_FLAGS &= ~ECHO;
449 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
450 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
453 #ifdef OPENSSL_SYS_VMS
454 tty_new[0] = tty_orig[0];
455 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
456 tty_new[2] = tty_orig[2];
458 sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
460 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
463 #if defined(_WIN32) && !defined(_WIN32_WCE)
466 tty_new &= ~ENABLE_ECHO_INPUT;
467 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
473 static int echo_console(UI *ui)
475 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
476 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
477 tty_new.TTY_FLAGS |= ECHO;
480 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
481 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
484 #ifdef OPENSSL_SYS_VMS
485 tty_new[0] = tty_orig[0];
486 tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
487 tty_new[2] = tty_orig[2];
489 sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
491 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
494 #if defined(_WIN32) && !defined(_WIN32_WCE)
497 tty_new |= ENABLE_ECHO_INPUT;
498 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
504 static int close_console(UI *ui)
508 if (tty_out != stderr)
510 #ifdef OPENSSL_SYS_VMS
511 status = sys$dassgn(channel);
513 CRYPTO_THREAD_unlock(ui->lock);
518 #if !defined(OPENSSL_SYS_WINCE)
519 /* Internal functions to handle signals and act on them */
520 static void pushsig(void)
522 # ifndef OPENSSL_SYS_WIN32
528 memset(&sa, 0, sizeof(sa));
529 sa.sa_handler = recsig;
532 # ifdef OPENSSL_SYS_WIN32
533 savsig[SIGABRT] = signal(SIGABRT, recsig);
534 savsig[SIGFPE] = signal(SIGFPE, recsig);
535 savsig[SIGILL] = signal(SIGILL, recsig);
536 savsig[SIGINT] = signal(SIGINT, recsig);
537 savsig[SIGSEGV] = signal(SIGSEGV, recsig);
538 savsig[SIGTERM] = signal(SIGTERM, recsig);
540 for (i = 1; i < NX509_SIG; i++) {
550 if (i == SIGKILL) /* We can't make any action on that. */
554 sigaction(i, &sa, &savsig[i]);
556 savsig[i] = signal(i, recsig);
562 signal(SIGWINCH, SIG_DFL);
566 static void popsig(void)
568 # ifdef OPENSSL_SYS_WIN32
569 signal(SIGABRT, savsig[SIGABRT]);
570 signal(SIGFPE, savsig[SIGFPE]);
571 signal(SIGILL, savsig[SIGILL]);
572 signal(SIGINT, savsig[SIGINT]);
573 signal(SIGSEGV, savsig[SIGSEGV]);
574 signal(SIGTERM, savsig[SIGTERM]);
577 for (i = 1; i < NX509_SIG; i++) {
587 sigaction(i, &savsig[i], NULL);
589 signal(i, savsig[i]);
595 static void recsig(int i)
601 /* Internal functions specific for Windows */
602 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
603 static int noecho_fgets(char *buf, int size, FILE *tty)
628 # ifdef WIN_CONSOLE_BUG
630 * Win95 has several evil console bugs: one of these is that the last
631 * character read using getch() is passed to the next read: this is
632 * usually a CR so this can be trouble. No STDIO fix seems to work but
633 * flushing the console appears to do the trick.
637 inh = GetStdHandle(STD_INPUT_HANDLE);
638 FlushConsoleInputBuffer(inh);
641 return (strlen(buf));