15c87c606SMark Murray /* crypto/ui/ui_openssl.c -*- mode:C; c-file-style: "eay" -*- */ 26f9291ceSJung-uk Kim /* 36f9291ceSJung-uk Kim * Written by Richard Levitte (richard@levitte.org) and others for the 46f9291ceSJung-uk Kim * OpenSSL project 2001. 55c87c606SMark Murray */ 65c87c606SMark Murray /* ==================================================================== 75c87c606SMark Murray * Copyright (c) 2001 The OpenSSL Project. All rights reserved. 85c87c606SMark Murray * 95c87c606SMark Murray * Redistribution and use in source and binary forms, with or without 105c87c606SMark Murray * modification, are permitted provided that the following conditions 115c87c606SMark Murray * are met: 125c87c606SMark Murray * 135c87c606SMark Murray * 1. Redistributions of source code must retain the above copyright 145c87c606SMark Murray * notice, this list of conditions and the following disclaimer. 155c87c606SMark Murray * 165c87c606SMark Murray * 2. Redistributions in binary form must reproduce the above copyright 175c87c606SMark Murray * notice, this list of conditions and the following disclaimer in 185c87c606SMark Murray * the documentation and/or other materials provided with the 195c87c606SMark Murray * distribution. 205c87c606SMark Murray * 215c87c606SMark Murray * 3. All advertising materials mentioning features or use of this 225c87c606SMark Murray * software must display the following acknowledgment: 235c87c606SMark Murray * "This product includes software developed by the OpenSSL Project 245c87c606SMark Murray * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 255c87c606SMark Murray * 265c87c606SMark Murray * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 275c87c606SMark Murray * endorse or promote products derived from this software without 285c87c606SMark Murray * prior written permission. For written permission, please contact 295c87c606SMark Murray * openssl-core@openssl.org. 305c87c606SMark Murray * 315c87c606SMark Murray * 5. Products derived from this software may not be called "OpenSSL" 325c87c606SMark Murray * nor may "OpenSSL" appear in their names without prior written 335c87c606SMark Murray * permission of the OpenSSL Project. 345c87c606SMark Murray * 355c87c606SMark Murray * 6. Redistributions of any form whatsoever must retain the following 365c87c606SMark Murray * acknowledgment: 375c87c606SMark Murray * "This product includes software developed by the OpenSSL Project 385c87c606SMark Murray * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 395c87c606SMark Murray * 405c87c606SMark Murray * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 415c87c606SMark Murray * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 425c87c606SMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 435c87c606SMark Murray * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 445c87c606SMark Murray * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 455c87c606SMark Murray * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 465c87c606SMark Murray * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 475c87c606SMark Murray * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 485c87c606SMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 495c87c606SMark Murray * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 505c87c606SMark Murray * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 515c87c606SMark Murray * OF THE POSSIBILITY OF SUCH DAMAGE. 525c87c606SMark Murray * ==================================================================== 535c87c606SMark Murray * 545c87c606SMark Murray * This product includes cryptographic software written by Eric Young 555c87c606SMark Murray * (eay@cryptsoft.com). This product includes software written by Tim 565c87c606SMark Murray * Hudson (tjh@cryptsoft.com). 575c87c606SMark Murray * 585c87c606SMark Murray */ 595c87c606SMark Murray 606f9291ceSJung-uk Kim /*- 616f9291ceSJung-uk Kim * The lowest level part of this file was previously in crypto/des/read_pwd.c, 625c87c606SMark Murray * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 635c87c606SMark Murray * All rights reserved. 645c87c606SMark Murray * 655c87c606SMark Murray * This package is an SSL implementation written 665c87c606SMark Murray * by Eric Young (eay@cryptsoft.com). 675c87c606SMark Murray * The implementation was written so as to conform with Netscapes SSL. 685c87c606SMark Murray * 695c87c606SMark Murray * This library is free for commercial and non-commercial use as long as 705c87c606SMark Murray * the following conditions are aheared to. The following conditions 715c87c606SMark Murray * apply to all code found in this distribution, be it the RC4, RSA, 725c87c606SMark Murray * lhash, DES, etc., code; not just the SSL code. The SSL documentation 735c87c606SMark Murray * included with this distribution is covered by the same copyright terms 745c87c606SMark Murray * except that the holder is Tim Hudson (tjh@cryptsoft.com). 755c87c606SMark Murray * 765c87c606SMark Murray * Copyright remains Eric Young's, and as such any Copyright notices in 775c87c606SMark Murray * the code are not to be removed. 785c87c606SMark Murray * If this package is used in a product, Eric Young should be given attribution 795c87c606SMark Murray * as the author of the parts of the library used. 805c87c606SMark Murray * This can be in the form of a textual message at program startup or 815c87c606SMark Murray * in documentation (online or textual) provided with the package. 825c87c606SMark Murray * 835c87c606SMark Murray * Redistribution and use in source and binary forms, with or without 845c87c606SMark Murray * modification, are permitted provided that the following conditions 855c87c606SMark Murray * are met: 865c87c606SMark Murray * 1. Redistributions of source code must retain the copyright 875c87c606SMark Murray * notice, this list of conditions and the following disclaimer. 885c87c606SMark Murray * 2. Redistributions in binary form must reproduce the above copyright 895c87c606SMark Murray * notice, this list of conditions and the following disclaimer in the 905c87c606SMark Murray * documentation and/or other materials provided with the distribution. 915c87c606SMark Murray * 3. All advertising materials mentioning features or use of this software 925c87c606SMark Murray * must display the following acknowledgement: 935c87c606SMark Murray * "This product includes cryptographic software written by 945c87c606SMark Murray * Eric Young (eay@cryptsoft.com)" 955c87c606SMark Murray * The word 'cryptographic' can be left out if the rouines from the library 965c87c606SMark Murray * being used are not cryptographic related :-). 975c87c606SMark Murray * 4. If you include any Windows specific code (or a derivative thereof) from 985c87c606SMark Murray * the apps directory (application code) you must include an acknowledgement: 995c87c606SMark Murray * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 1005c87c606SMark Murray * 1015c87c606SMark Murray * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 1025c87c606SMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1035c87c606SMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1045c87c606SMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1055c87c606SMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1065c87c606SMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1075c87c606SMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1085c87c606SMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 1095c87c606SMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 1105c87c606SMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 1115c87c606SMark Murray * SUCH DAMAGE. 1125c87c606SMark Murray * 1135c87c606SMark Murray * The licence and distribution terms for any publically available version or 1145c87c606SMark Murray * derivative of this code cannot be changed. i.e. this code cannot simply be 1155c87c606SMark Murray * copied and put under another distribution licence 1165c87c606SMark Murray * [including the GNU Public Licence.] 1175c87c606SMark Murray */ 1185c87c606SMark Murray 1195c87c606SMark Murray #include <openssl/e_os2.h> 1205c87c606SMark Murray 1216f9291ceSJung-uk Kim /* 1226f9291ceSJung-uk Kim * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc 1233b4e3dcbSSimon L. B. Nielsen * [maybe others?], because it masks interfaces not discussed in standard, 1246f9291ceSJung-uk Kim * sigaction and fileno included. -pedantic would be more appropriate for the 1256f9291ceSJung-uk Kim * intended purposes, but we can't prevent users from adding -ansi. 1263b4e3dcbSSimon L. B. Nielsen */ 12709286989SJung-uk Kim #if defined(OPENSSL_SYSNAME_VXWORKS) 12809286989SJung-uk Kim # include <sys/types.h> 12909286989SJung-uk Kim #endif 13009286989SJung-uk Kim 1311f13597dSJung-uk Kim #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS) 13209286989SJung-uk Kim # ifndef _POSIX_C_SOURCE 1331f13597dSJung-uk Kim # define _POSIX_C_SOURCE 2 1341f13597dSJung-uk Kim # endif 13509286989SJung-uk Kim #endif 1363b4e3dcbSSimon L. B. Nielsen #include <signal.h> 1373b4e3dcbSSimon L. B. Nielsen #include <stdio.h> 1383b4e3dcbSSimon L. B. Nielsen #include <string.h> 1393b4e3dcbSSimon L. B. Nielsen #include <errno.h> 1403b4e3dcbSSimon L. B. Nielsen 1415c87c606SMark Murray #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) 1425c87c606SMark Murray # ifdef OPENSSL_UNISTD 1435c87c606SMark Murray # include OPENSSL_UNISTD 1445c87c606SMark Murray # else 1455c87c606SMark Murray # include <unistd.h> 1465c87c606SMark Murray # endif 1476f9291ceSJung-uk Kim /* 1486f9291ceSJung-uk Kim * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX 1496f9291ceSJung-uk Kim * system and have sigaction and termios. 1506f9291ceSJung-uk Kim */ 1515c87c606SMark Murray # if defined(_POSIX_VERSION) 1525c87c606SMark Murray 1535c87c606SMark Murray # define SIGACTION 1545c87c606SMark Murray # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY) 1555c87c606SMark Murray # define TERMIOS 1565c87c606SMark Murray # endif 1575c87c606SMark Murray 1585c87c606SMark Murray # endif 1595c87c606SMark Murray #endif 1605c87c606SMark Murray 1615c87c606SMark Murray #ifdef WIN16TTY 1625c87c606SMark Murray # undef OPENSSL_SYS_WIN16 1635c87c606SMark Murray # undef WIN16 1645c87c606SMark Murray # undef _WINDOWS 1655c87c606SMark Murray # include <graph.h> 1665c87c606SMark Murray #endif 1675c87c606SMark Murray 1685c87c606SMark Murray /* 06-Apr-92 Luke Brennan Support for VMS */ 1695c87c606SMark Murray #include "ui_locl.h" 1705c87c606SMark Murray #include "cryptlib.h" 1715c87c606SMark Murray 1725c87c606SMark Murray #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */ 1735c87c606SMark Murray # include <starlet.h> 1745c87c606SMark Murray # ifdef __DECC 1755c87c606SMark Murray # pragma message disable DOLLARID 1765c87c606SMark Murray # endif 1775c87c606SMark Murray #endif 1785c87c606SMark Murray 1795c87c606SMark Murray #ifdef WIN_CONSOLE_BUG 1805c87c606SMark Murray # include <windows.h> 1815c87c606SMark Murray # ifndef OPENSSL_SYS_WINCE 1825c87c606SMark Murray # include <wincon.h> 1835c87c606SMark Murray # endif 1845c87c606SMark Murray #endif 1855c87c606SMark Murray 1866f9291ceSJung-uk Kim /* 1876f9291ceSJung-uk Kim * There are 5 types of terminal interface supported, TERMIO, TERMIOS, VMS, 1886f9291ceSJung-uk Kim * MSDOS and SGTTY. 1896f9291ceSJung-uk Kim * 1906f9291ceSJung-uk Kim * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will 1916f9291ceSJung-uk Kim * remain respected. Otherwise, we default to TERMIOS except for a few 1926f9291ceSJung-uk Kim * systems that require something different. 1936f9291ceSJung-uk Kim * 1946f9291ceSJung-uk Kim * Note: we do not use SGTTY unless it's defined by the configuration. We 1956f9291ceSJung-uk Kim * may eventually opt to remove it's use entirely. 1965c87c606SMark Murray */ 1975c87c606SMark Murray 1986f9291ceSJung-uk Kim #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY) 1996f9291ceSJung-uk Kim 2006f9291ceSJung-uk Kim # if defined(_LIBC) 2016f9291ceSJung-uk Kim # undef TERMIOS 2026f9291ceSJung-uk Kim # define TERMIO 2036f9291ceSJung-uk Kim # undef SGTTY 2046f9291ceSJung-uk Kim /* 2056f9291ceSJung-uk Kim * We know that VMS, MSDOS, VXWORKS, NETWARE use entirely other mechanisms. 2066f9291ceSJung-uk Kim * MAC_OS_GUSI_SOURCE should probably go away, but that needs to be confirmed. 2076f9291ceSJung-uk Kim */ 2086f9291ceSJung-uk Kim # elif !defined(OPENSSL_SYS_VMS) \ 2096f9291ceSJung-uk Kim && !defined(OPENSSL_SYS_MSDOS) \ 2106f9291ceSJung-uk Kim && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) \ 2116f9291ceSJung-uk Kim && !defined(MAC_OS_GUSI_SOURCE) \ 2126f9291ceSJung-uk Kim && !defined(OPENSSL_SYS_VXWORKS) \ 2136f9291ceSJung-uk Kim && !defined(OPENSSL_SYS_NETWARE) 2145c87c606SMark Murray # define TERMIOS 2155c87c606SMark Murray # undef TERMIO 2165c87c606SMark Murray # undef SGTTY 2175c87c606SMark Murray # endif 2185c87c606SMark Murray 2193b4e3dcbSSimon L. B. Nielsen #endif 2203b4e3dcbSSimon L. B. Nielsen 2215c87c606SMark Murray #ifdef TERMIOS 2225c87c606SMark Murray # include <termios.h> 2235c87c606SMark Murray # define TTY_STRUCT struct termios 2245c87c606SMark Murray # define TTY_FLAGS c_lflag 2255c87c606SMark Murray # define TTY_get(tty,data) tcgetattr(tty,data) 2265c87c606SMark Murray # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data) 2275c87c606SMark Murray #endif 2285c87c606SMark Murray 2295c87c606SMark Murray #ifdef TERMIO 2305c87c606SMark Murray # include <termio.h> 2315c87c606SMark Murray # define TTY_STRUCT struct termio 2325c87c606SMark Murray # define TTY_FLAGS c_lflag 2335c87c606SMark Murray # define TTY_get(tty,data) ioctl(tty,TCGETA,data) 2345c87c606SMark Murray # define TTY_set(tty,data) ioctl(tty,TCSETA,data) 2355c87c606SMark Murray #endif 2365c87c606SMark Murray 2375c87c606SMark Murray #ifdef SGTTY 2385c87c606SMark Murray # include <sgtty.h> 2395c87c606SMark Murray # define TTY_STRUCT struct sgttyb 2405c87c606SMark Murray # define TTY_FLAGS sg_flags 2415c87c606SMark Murray # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data) 2425c87c606SMark Murray # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data) 2435c87c606SMark Murray #endif 2445c87c606SMark Murray 2455c87c606SMark Murray #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_SUNOS) 2465c87c606SMark Murray # include <sys/ioctl.h> 2475c87c606SMark Murray #endif 2485c87c606SMark Murray 2495c87c606SMark Murray #ifdef OPENSSL_SYS_MSDOS 2505c87c606SMark Murray # include <conio.h> 2515c87c606SMark Murray #endif 2525c87c606SMark Murray 2535c87c606SMark Murray #ifdef OPENSSL_SYS_VMS 2545c87c606SMark Murray # include <ssdef.h> 2555c87c606SMark Murray # include <iodef.h> 2565c87c606SMark Murray # include <ttdef.h> 2575c87c606SMark Murray # include <descrip.h> 2585c87c606SMark Murray struct IOSB { 2595c87c606SMark Murray short iosb$w_value; 2605c87c606SMark Murray short iosb$w_count; 2615c87c606SMark Murray long iosb$l_info; 2625c87c606SMark Murray }; 2635c87c606SMark Murray #endif 2645c87c606SMark Murray 2655c87c606SMark Murray #ifdef OPENSSL_SYS_SUNOS 2665c87c606SMark Murray typedef int sig_atomic_t; 2675c87c606SMark Murray #endif 2685c87c606SMark Murray 2693b4e3dcbSSimon L. B. Nielsen #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE) || defined(OPENSSL_SYS_NETWARE) 2705c87c606SMark Murray /* 2715c87c606SMark Murray * This one needs work. As a matter of fact the code is unoperational 2725c87c606SMark Murray * and this is only a trick to get it compiled. 2735c87c606SMark Murray * <appro@fy.chalmers.se> 2745c87c606SMark Murray */ 2755c87c606SMark Murray # define TTY_STRUCT int 2765c87c606SMark Murray #endif 2775c87c606SMark Murray 2785c87c606SMark Murray #ifndef NX509_SIG 2795c87c606SMark Murray # define NX509_SIG 32 2805c87c606SMark Murray #endif 2815c87c606SMark Murray 2825c87c606SMark Murray /* Define globals. They are protected by a lock */ 2835c87c606SMark Murray #ifdef SIGACTION 2845c87c606SMark Murray static struct sigaction savsig[NX509_SIG]; 2855c87c606SMark Murray #else 2865c87c606SMark Murray static void (*savsig[NX509_SIG]) (int); 2875c87c606SMark Murray #endif 2885c87c606SMark Murray 2895c87c606SMark Murray #ifdef OPENSSL_SYS_VMS 2905c87c606SMark Murray static struct IOSB iosb; 2915c87c606SMark Murray static $DESCRIPTOR(terminal, "TT"); 2926f9291ceSJung-uk Kim static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this 2936f9291ceSJung-uk Kim * will always suffice for the actual 2946f9291ceSJung-uk Kim * structures? */ 2955c87c606SMark Murray static long status; 2965c87c606SMark Murray static unsigned short channel = 0; 2975c87c606SMark Murray #else 2985c87c606SMark Murray # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__) 2995c87c606SMark Murray static TTY_STRUCT tty_orig, tty_new; 3005c87c606SMark Murray # endif 3015c87c606SMark Murray #endif 3025c87c606SMark Murray static FILE *tty_in, *tty_out; 3035c87c606SMark Murray static int is_a_tty; 3045c87c606SMark Murray 3055c87c606SMark Murray /* Declare static functions */ 3065c87c606SMark Murray #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) 3076a599222SSimon L. B. Nielsen static int read_till_nl(FILE *); 3085c87c606SMark Murray static void recsig(int); 3095c87c606SMark Murray static void pushsig(void); 3105c87c606SMark Murray static void popsig(void); 3115c87c606SMark Murray #endif 3125c87c606SMark Murray #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) 3135c87c606SMark Murray static int noecho_fgets(char *buf, int size, FILE *tty); 3145c87c606SMark Murray #endif 3155c87c606SMark Murray static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl); 3165c87c606SMark Murray 3175c87c606SMark Murray static int read_string(UI *ui, UI_STRING *uis); 3185c87c606SMark Murray static int write_string(UI *ui, UI_STRING *uis); 3195c87c606SMark Murray 3205c87c606SMark Murray static int open_console(UI *ui); 3215c87c606SMark Murray static int echo_console(UI *ui); 3225c87c606SMark Murray static int noecho_console(UI *ui); 3235c87c606SMark Murray static int close_console(UI *ui); 3245c87c606SMark Murray 3256f9291ceSJung-uk Kim static UI_METHOD ui_openssl = { 3265c87c606SMark Murray "OpenSSL default user interface", 3275c87c606SMark Murray open_console, 3285c87c606SMark Murray write_string, 3295c87c606SMark Murray NULL, /* No flusher is needed for command lines */ 3305c87c606SMark Murray read_string, 3315c87c606SMark Murray close_console, 3325c87c606SMark Murray NULL 3335c87c606SMark Murray }; 3345c87c606SMark Murray 3355c87c606SMark Murray /* The method with all the built-in thingies */ 3365c87c606SMark Murray UI_METHOD *UI_OpenSSL(void) 3375c87c606SMark Murray { 3385c87c606SMark Murray return &ui_openssl; 3395c87c606SMark Murray } 3405c87c606SMark Murray 3416f9291ceSJung-uk Kim /* 3426f9291ceSJung-uk Kim * The following function makes sure that info and error strings are printed 3436f9291ceSJung-uk Kim * before any prompt. 3446f9291ceSJung-uk Kim */ 3455c87c606SMark Murray static int write_string(UI *ui, UI_STRING *uis) 3465c87c606SMark Murray { 3476f9291ceSJung-uk Kim switch (UI_get_string_type(uis)) { 3485c87c606SMark Murray case UIT_ERROR: 3495c87c606SMark Murray case UIT_INFO: 3505c87c606SMark Murray fputs(UI_get0_output_string(uis), tty_out); 3515c87c606SMark Murray fflush(tty_out); 3525c87c606SMark Murray break; 3535c87c606SMark Murray default: 3545c87c606SMark Murray break; 3555c87c606SMark Murray } 3565c87c606SMark Murray return 1; 3575c87c606SMark Murray } 3585c87c606SMark Murray 3595c87c606SMark Murray static int read_string(UI *ui, UI_STRING *uis) 3605c87c606SMark Murray { 3615c87c606SMark Murray int ok = 0; 3625c87c606SMark Murray 3636f9291ceSJung-uk Kim switch (UI_get_string_type(uis)) { 3645c87c606SMark Murray case UIT_BOOLEAN: 3655c87c606SMark Murray fputs(UI_get0_output_string(uis), tty_out); 3665c87c606SMark Murray fputs(UI_get0_action_string(uis), tty_out); 3675c87c606SMark Murray fflush(tty_out); 3685c87c606SMark Murray return read_string_inner(ui, uis, 3696f9291ceSJung-uk Kim UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 3706f9291ceSJung-uk Kim 0); 3715c87c606SMark Murray case UIT_PROMPT: 3725c87c606SMark Murray fputs(UI_get0_output_string(uis), tty_out); 3735c87c606SMark Murray fflush(tty_out); 3745c87c606SMark Murray return read_string_inner(ui, uis, 3756f9291ceSJung-uk Kim UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 3766f9291ceSJung-uk Kim 1); 3775c87c606SMark Murray case UIT_VERIFY: 3786f9291ceSJung-uk Kim fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis)); 3795c87c606SMark Murray fflush(tty_out); 3805c87c606SMark Murray if ((ok = read_string_inner(ui, uis, 3816f9291ceSJung-uk Kim UI_get_input_flags(uis) & 3826f9291ceSJung-uk Kim UI_INPUT_FLAG_ECHO, 1)) <= 0) 3835c87c606SMark Murray return ok; 3846f9291ceSJung-uk Kim if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) { 3855c87c606SMark Murray fprintf(tty_out, "Verify failure\n"); 3865c87c606SMark Murray fflush(tty_out); 3875c87c606SMark Murray return 0; 3885c87c606SMark Murray } 3895c87c606SMark Murray break; 3905c87c606SMark Murray default: 3915c87c606SMark Murray break; 3925c87c606SMark Murray } 3935c87c606SMark Murray return 1; 3945c87c606SMark Murray } 3955c87c606SMark Murray 3965c87c606SMark Murray #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) 3975c87c606SMark Murray /* Internal functions to read a string without echoing */ 3986a599222SSimon L. B. Nielsen static int read_till_nl(FILE *in) 3995c87c606SMark Murray { 4005c87c606SMark Murray # define SIZE 4 4015c87c606SMark Murray char buf[SIZE + 1]; 4025c87c606SMark Murray 4035c87c606SMark Murray do { 4046a599222SSimon L. B. Nielsen if (!fgets(buf, SIZE, in)) 4056a599222SSimon L. B. Nielsen return 0; 4065c87c606SMark Murray } while (strchr(buf, '\n') == NULL); 4076a599222SSimon L. B. Nielsen return 1; 4085c87c606SMark Murray } 4095c87c606SMark Murray 4105c87c606SMark Murray static volatile sig_atomic_t intr_signal; 4115c87c606SMark Murray #endif 4125c87c606SMark Murray 4135c87c606SMark Murray static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl) 4145c87c606SMark Murray { 4155c87c606SMark Murray static int ps; 4165c87c606SMark Murray int ok; 4175c87c606SMark Murray char result[BUFSIZ]; 4185c87c606SMark Murray int maxsize = BUFSIZ - 1; 4195c87c606SMark Murray #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) 4205c87c606SMark Murray char *p; 4215c87c606SMark Murray 4225c87c606SMark Murray intr_signal = 0; 4235c87c606SMark Murray ok = 0; 4245c87c606SMark Murray ps = 0; 4255c87c606SMark Murray 4265c87c606SMark Murray pushsig(); 4275c87c606SMark Murray ps = 1; 4285c87c606SMark Murray 4295c87c606SMark Murray if (!echo && !noecho_console(ui)) 4305c87c606SMark Murray goto error; 4315c87c606SMark Murray ps = 2; 4325c87c606SMark Murray 4335c87c606SMark Murray result[0] = '\0'; 4345c87c606SMark Murray # ifdef OPENSSL_SYS_MSDOS 4356f9291ceSJung-uk Kim if (!echo) { 4365c87c606SMark Murray noecho_fgets(result, maxsize, tty_in); 4375c87c606SMark Murray p = result; /* FIXME: noecho_fgets doesn't return errors */ 4386f9291ceSJung-uk Kim } else 4395c87c606SMark Murray p = fgets(result, maxsize, tty_in); 4405c87c606SMark Murray # else 4415c87c606SMark Murray p = fgets(result, maxsize, tty_in); 4425c87c606SMark Murray # endif 4435c87c606SMark Murray if (!p) 4445c87c606SMark Murray goto error; 4456f9291ceSJung-uk Kim if (feof(tty_in)) 4466f9291ceSJung-uk Kim goto error; 4476f9291ceSJung-uk Kim if (ferror(tty_in)) 4486f9291ceSJung-uk Kim goto error; 4496f9291ceSJung-uk Kim if ((p = (char *)strchr(result, '\n')) != NULL) { 4505c87c606SMark Murray if (strip_nl) 4515c87c606SMark Murray *p = '\0'; 4526f9291ceSJung-uk Kim } else if (!read_till_nl(tty_in)) 4536a599222SSimon L. B. Nielsen goto error; 4545c87c606SMark Murray if (UI_set_result(ui, uis, result) >= 0) 4555c87c606SMark Murray ok = 1; 4565c87c606SMark Murray 4575c87c606SMark Murray error: 4585c87c606SMark Murray if (intr_signal == SIGINT) 4595c87c606SMark Murray ok = -1; 4606f9291ceSJung-uk Kim if (!echo) 4616f9291ceSJung-uk Kim fprintf(tty_out, "\n"); 4625c87c606SMark Murray if (ps >= 2 && !echo && !echo_console(ui)) 4635c87c606SMark Murray ok = 0; 4645c87c606SMark Murray 4655c87c606SMark Murray if (ps >= 1) 4665c87c606SMark Murray popsig(); 4675c87c606SMark Murray #else 4685c87c606SMark Murray ok = 1; 4695c87c606SMark Murray #endif 4705c87c606SMark Murray 4715c87c606SMark Murray OPENSSL_cleanse(result, BUFSIZ); 4725c87c606SMark Murray return ok; 4735c87c606SMark Murray } 4745c87c606SMark Murray 4755c87c606SMark Murray /* Internal functions to open, handle and close a channel to the console. */ 4765c87c606SMark Murray static int open_console(UI *ui) 4775c87c606SMark Murray { 4785c87c606SMark Murray CRYPTO_w_lock(CRYPTO_LOCK_UI); 4795c87c606SMark Murray is_a_tty = 1; 4805c87c606SMark Murray 4811f13597dSJung-uk Kim #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS) 4825c87c606SMark Murray tty_in = stdin; 4835c87c606SMark Murray tty_out = stderr; 4845c87c606SMark Murray #else 4855c87c606SMark Murray # ifdef OPENSSL_SYS_MSDOS 4865c87c606SMark Murray # define DEV_TTY "con" 4875c87c606SMark Murray # else 4885c87c606SMark Murray # define DEV_TTY "/dev/tty" 4895c87c606SMark Murray # endif 4905c87c606SMark Murray if ((tty_in = fopen(DEV_TTY, "r")) == NULL) 4915c87c606SMark Murray tty_in = stdin; 4925c87c606SMark Murray if ((tty_out = fopen(DEV_TTY, "w")) == NULL) 4935c87c606SMark Murray tty_out = stderr; 4945c87c606SMark Murray #endif 4955c87c606SMark Murray 4965c87c606SMark Murray #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS) 4976f9291ceSJung-uk Kim if (TTY_get(fileno(tty_in), &tty_orig) == -1) { 4985c87c606SMark Murray # ifdef ENOTTY 4995c87c606SMark Murray if (errno == ENOTTY) 5005c87c606SMark Murray is_a_tty = 0; 5015c87c606SMark Murray else 5025c87c606SMark Murray # endif 5035c87c606SMark Murray # ifdef EINVAL 5046f9291ceSJung-uk Kim /* 5056f9291ceSJung-uk Kim * Ariel Glenn ariel@columbia.edu reports that solaris can return 5066f9291ceSJung-uk Kim * EINVAL instead. This should be ok 5076f9291ceSJung-uk Kim */ 5085c87c606SMark Murray if (errno == EINVAL) 5095c87c606SMark Murray is_a_tty = 0; 5105c87c606SMark Murray else 5115c87c606SMark Murray # endif 5125c87c606SMark Murray return 0; 5135c87c606SMark Murray } 5145c87c606SMark Murray #endif 5155c87c606SMark Murray #ifdef OPENSSL_SYS_VMS 5165c87c606SMark Murray status = sys$assign(&terminal, &channel, 0, 0); 5175c87c606SMark Murray if (status != SS$_NORMAL) 5185c87c606SMark Murray return 0; 5196f9291ceSJung-uk Kim status = 5206f9291ceSJung-uk Kim sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0, 5216f9291ceSJung-uk Kim 0, 0); 5225c87c606SMark Murray if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) 5235c87c606SMark Murray return 0; 5245c87c606SMark Murray #endif 5255c87c606SMark Murray return 1; 5265c87c606SMark Murray } 5275c87c606SMark Murray 5285c87c606SMark Murray static int noecho_console(UI *ui) 5295c87c606SMark Murray { 5305c87c606SMark Murray #ifdef TTY_FLAGS 5315c87c606SMark Murray memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig)); 5325c87c606SMark Murray tty_new.TTY_FLAGS &= ~ECHO; 5335c87c606SMark Murray #endif 5345c87c606SMark Murray 5355c87c606SMark Murray #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) 5365c87c606SMark Murray if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1)) 5375c87c606SMark Murray return 0; 5385c87c606SMark Murray #endif 5395c87c606SMark Murray #ifdef OPENSSL_SYS_VMS 5405c87c606SMark Murray tty_new[0] = tty_orig[0]; 5415c87c606SMark Murray tty_new[1] = tty_orig[1] | TT$M_NOECHO; 5425c87c606SMark Murray tty_new[2] = tty_orig[2]; 5436f9291ceSJung-uk Kim status = 5446f9291ceSJung-uk Kim sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0, 5456f9291ceSJung-uk Kim 0); 5465c87c606SMark Murray if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) 5475c87c606SMark Murray return 0; 5485c87c606SMark Murray #endif 5495c87c606SMark Murray return 1; 5505c87c606SMark Murray } 5515c87c606SMark Murray 5525c87c606SMark Murray static int echo_console(UI *ui) 5535c87c606SMark Murray { 5545c87c606SMark Murray #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) 5555c87c606SMark Murray memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig)); 5565c87c606SMark Murray tty_new.TTY_FLAGS |= ECHO; 5575c87c606SMark Murray #endif 5585c87c606SMark Murray 5595c87c606SMark Murray #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) 5605c87c606SMark Murray if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1)) 5615c87c606SMark Murray return 0; 5625c87c606SMark Murray #endif 5635c87c606SMark Murray #ifdef OPENSSL_SYS_VMS 5645c87c606SMark Murray tty_new[0] = tty_orig[0]; 5655c87c606SMark Murray tty_new[1] = tty_orig[1] & ~TT$M_NOECHO; 5665c87c606SMark Murray tty_new[2] = tty_orig[2]; 5676f9291ceSJung-uk Kim status = 5686f9291ceSJung-uk Kim sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0, 5696f9291ceSJung-uk Kim 0); 5705c87c606SMark Murray if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) 5715c87c606SMark Murray return 0; 5725c87c606SMark Murray #endif 5735c87c606SMark Murray return 1; 5745c87c606SMark Murray } 5755c87c606SMark Murray 5765c87c606SMark Murray static int close_console(UI *ui) 5775c87c606SMark Murray { 5786f9291ceSJung-uk Kim if (tty_in != stdin) 5796f9291ceSJung-uk Kim fclose(tty_in); 5806f9291ceSJung-uk Kim if (tty_out != stderr) 5816f9291ceSJung-uk Kim fclose(tty_out); 5825c87c606SMark Murray #ifdef OPENSSL_SYS_VMS 5835c87c606SMark Murray status = sys$dassgn(channel); 5845c87c606SMark Murray #endif 5855c87c606SMark Murray CRYPTO_w_unlock(CRYPTO_LOCK_UI); 5865c87c606SMark Murray 5875c87c606SMark Murray return 1; 5885c87c606SMark Murray } 5895c87c606SMark Murray 5905c87c606SMark Murray #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) 5915c87c606SMark Murray /* Internal functions to handle signals and act on them */ 5925c87c606SMark Murray static void pushsig(void) 5935c87c606SMark Murray { 5943b4e3dcbSSimon L. B. Nielsen # ifndef OPENSSL_SYS_WIN32 5955c87c606SMark Murray int i; 5963b4e3dcbSSimon L. B. Nielsen # endif 5975c87c606SMark Murray # ifdef SIGACTION 5985c87c606SMark Murray struct sigaction sa; 5995c87c606SMark Murray 6005c87c606SMark Murray memset(&sa, 0, sizeof sa); 6015c87c606SMark Murray sa.sa_handler = recsig; 6025c87c606SMark Murray # endif 6035c87c606SMark Murray 6043b4e3dcbSSimon L. B. Nielsen # ifdef OPENSSL_SYS_WIN32 6053b4e3dcbSSimon L. B. Nielsen savsig[SIGABRT] = signal(SIGABRT, recsig); 6063b4e3dcbSSimon L. B. Nielsen savsig[SIGFPE] = signal(SIGFPE, recsig); 6073b4e3dcbSSimon L. B. Nielsen savsig[SIGILL] = signal(SIGILL, recsig); 6083b4e3dcbSSimon L. B. Nielsen savsig[SIGINT] = signal(SIGINT, recsig); 6093b4e3dcbSSimon L. B. Nielsen savsig[SIGSEGV] = signal(SIGSEGV, recsig); 6103b4e3dcbSSimon L. B. Nielsen savsig[SIGTERM] = signal(SIGTERM, recsig); 6113b4e3dcbSSimon L. B. Nielsen # else 6126f9291ceSJung-uk Kim for (i = 1; i < NX509_SIG; i++) { 6135c87c606SMark Murray # ifdef SIGUSR1 6145c87c606SMark Murray if (i == SIGUSR1) 6155c87c606SMark Murray continue; 6165c87c606SMark Murray # endif 6175c87c606SMark Murray # ifdef SIGUSR2 6185c87c606SMark Murray if (i == SIGUSR2) 6195c87c606SMark Murray continue; 6205c87c606SMark Murray # endif 6215c87c606SMark Murray # ifdef SIGKILL 6225c87c606SMark Murray if (i == SIGKILL) /* We can't make any action on that. */ 6235c87c606SMark Murray continue; 6245c87c606SMark Murray # endif 6255c87c606SMark Murray # ifdef SIGACTION 6265c87c606SMark Murray sigaction(i, &sa, &savsig[i]); 6275c87c606SMark Murray # else 6285c87c606SMark Murray savsig[i] = signal(i, recsig); 6295c87c606SMark Murray # endif 6305c87c606SMark Murray } 6313b4e3dcbSSimon L. B. Nielsen # endif 6325c87c606SMark Murray 6335c87c606SMark Murray # ifdef SIGWINCH 6345c87c606SMark Murray signal(SIGWINCH, SIG_DFL); 6355c87c606SMark Murray # endif 6365c87c606SMark Murray } 6375c87c606SMark Murray 6385c87c606SMark Murray static void popsig(void) 6395c87c606SMark Murray { 6403b4e3dcbSSimon L. B. Nielsen # ifdef OPENSSL_SYS_WIN32 6413b4e3dcbSSimon L. B. Nielsen signal(SIGABRT, savsig[SIGABRT]); 6423b4e3dcbSSimon L. B. Nielsen signal(SIGFPE, savsig[SIGFPE]); 6433b4e3dcbSSimon L. B. Nielsen signal(SIGILL, savsig[SIGILL]); 6443b4e3dcbSSimon L. B. Nielsen signal(SIGINT, savsig[SIGINT]); 6453b4e3dcbSSimon L. B. Nielsen signal(SIGSEGV, savsig[SIGSEGV]); 6463b4e3dcbSSimon L. B. Nielsen signal(SIGTERM, savsig[SIGTERM]); 6473b4e3dcbSSimon L. B. Nielsen # else 6485c87c606SMark Murray int i; 6496f9291ceSJung-uk Kim for (i = 1; i < NX509_SIG; i++) { 6505c87c606SMark Murray # ifdef SIGUSR1 6515c87c606SMark Murray if (i == SIGUSR1) 6525c87c606SMark Murray continue; 6535c87c606SMark Murray # endif 6545c87c606SMark Murray # ifdef SIGUSR2 6555c87c606SMark Murray if (i == SIGUSR2) 6565c87c606SMark Murray continue; 6575c87c606SMark Murray # endif 6585c87c606SMark Murray # ifdef SIGACTION 6595c87c606SMark Murray sigaction(i, &savsig[i], NULL); 6605c87c606SMark Murray # else 6615c87c606SMark Murray signal(i, savsig[i]); 6625c87c606SMark Murray # endif 6635c87c606SMark Murray } 6643b4e3dcbSSimon L. B. Nielsen # endif 6655c87c606SMark Murray } 6665c87c606SMark Murray 6675c87c606SMark Murray static void recsig(int i) 6685c87c606SMark Murray { 6695c87c606SMark Murray intr_signal = i; 6705c87c606SMark Murray } 6715c87c606SMark Murray #endif 6725c87c606SMark Murray 6735c87c606SMark Murray /* Internal functions specific for Windows */ 6745c87c606SMark Murray #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) 6755c87c606SMark Murray static int noecho_fgets(char *buf, int size, FILE *tty) 6765c87c606SMark Murray { 6775c87c606SMark Murray int i; 6785c87c606SMark Murray char *p; 6795c87c606SMark Murray 6805c87c606SMark Murray p = buf; 6816f9291ceSJung-uk Kim for (;;) { 6826f9291ceSJung-uk Kim if (size == 0) { 6835c87c606SMark Murray *p = '\0'; 6845c87c606SMark Murray break; 6855c87c606SMark Murray } 6865c87c606SMark Murray size--; 6875c87c606SMark Murray # ifdef WIN16TTY 6885c87c606SMark Murray i = _inchar(); 689db522d3aSSimon L. B. Nielsen # elif defined(_WIN32) 690db522d3aSSimon L. B. Nielsen i = _getch(); 6915c87c606SMark Murray # else 6925c87c606SMark Murray i = getch(); 6935c87c606SMark Murray # endif 6946f9291ceSJung-uk Kim if (i == '\r') 6956f9291ceSJung-uk Kim i = '\n'; 6965c87c606SMark Murray *(p++) = i; 6976f9291ceSJung-uk Kim if (i == '\n') { 6985c87c606SMark Murray *p = '\0'; 6995c87c606SMark Murray break; 7005c87c606SMark Murray } 7015c87c606SMark Murray } 7025c87c606SMark Murray # ifdef WIN_CONSOLE_BUG 7036f9291ceSJung-uk Kim /* 7046f9291ceSJung-uk Kim * Win95 has several evil console bugs: one of these is that the last 7056f9291ceSJung-uk Kim * character read using getch() is passed to the next read: this is 7065c87c606SMark Murray * usually a CR so this can be trouble. No STDIO fix seems to work but 7075c87c606SMark Murray * flushing the console appears to do the trick. 7085c87c606SMark Murray */ 7095c87c606SMark Murray { 7105c87c606SMark Murray HANDLE inh; 7115c87c606SMark Murray inh = GetStdHandle(STD_INPUT_HANDLE); 7125c87c606SMark Murray FlushConsoleInputBuffer(inh); 7135c87c606SMark Murray } 7145c87c606SMark Murray # endif 7155c87c606SMark Murray return (strlen(buf)); 7165c87c606SMark Murray } 7175c87c606SMark Murray #endif 718