xref: /onnv-gate/usr/src/common/openssl/crypto/ui/ui_openssl.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/ui/ui_openssl.c -*- mode:C; c-file-style: "eay" -*- */
20Sstevel@tonic-gate /* Written by Richard Levitte (richard@levitte.org) and others
30Sstevel@tonic-gate  * for the OpenSSL project 2001.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    openssl-core@openssl.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /* The lowest level part of this file was previously in crypto/des/read_pwd.c,
600Sstevel@tonic-gate  * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
610Sstevel@tonic-gate  * All rights reserved.
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * This package is an SSL implementation written
640Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
650Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
680Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
690Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
700Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
710Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
720Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
750Sstevel@tonic-gate  * the code are not to be removed.
760Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
770Sstevel@tonic-gate  * as the author of the parts of the library used.
780Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
790Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
820Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
830Sstevel@tonic-gate  * are met:
840Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
850Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
860Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
870Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
880Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
890Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
900Sstevel@tonic-gate  *    must display the following acknowledgement:
910Sstevel@tonic-gate  *    "This product includes cryptographic software written by
920Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
930Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
940Sstevel@tonic-gate  *    being used are not cryptographic related :-).
950Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
960Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
970Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1000Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1010Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1020Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1030Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1040Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1050Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1060Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1070Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1080Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1090Sstevel@tonic-gate  * SUCH DAMAGE.
1100Sstevel@tonic-gate  *
1110Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
1120Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
1130Sstevel@tonic-gate  * copied and put under another distribution licence
1140Sstevel@tonic-gate  * [including the GNU Public Licence.]
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #include <openssl/e_os2.h>
1190Sstevel@tonic-gate 
120*2139Sjp161948 /* need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
121*2139Sjp161948  * [maybe others?], because it masks interfaces not discussed in standard,
122*2139Sjp161948  * sigaction and fileno included. -pedantic would be more appropriate for
123*2139Sjp161948  * the intended purposes, but we can't prevent users from adding -ansi.
124*2139Sjp161948  */
125*2139Sjp161948 #define _POSIX_C_SOURCE 1
126*2139Sjp161948 #include <signal.h>
127*2139Sjp161948 #include <stdio.h>
128*2139Sjp161948 #include <string.h>
129*2139Sjp161948 #include <errno.h>
130*2139Sjp161948 
1310Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
1320Sstevel@tonic-gate # ifdef OPENSSL_UNISTD
1330Sstevel@tonic-gate #  include OPENSSL_UNISTD
1340Sstevel@tonic-gate # else
1350Sstevel@tonic-gate #  include <unistd.h>
1360Sstevel@tonic-gate # endif
1370Sstevel@tonic-gate /* If unistd.h defines _POSIX_VERSION, we conclude that we
1380Sstevel@tonic-gate  * are on a POSIX system and have sigaction and termios. */
1390Sstevel@tonic-gate # if defined(_POSIX_VERSION)
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #  define SIGACTION
1420Sstevel@tonic-gate #  if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
1430Sstevel@tonic-gate #   define TERMIOS
1440Sstevel@tonic-gate #  endif
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate # endif
1470Sstevel@tonic-gate #endif
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate #ifdef WIN16TTY
1500Sstevel@tonic-gate # undef OPENSSL_SYS_WIN16
1510Sstevel@tonic-gate # undef WIN16
1520Sstevel@tonic-gate # undef _WINDOWS
1530Sstevel@tonic-gate # include <graph.h>
1540Sstevel@tonic-gate #endif
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /* 06-Apr-92 Luke Brennan    Support for VMS */
1570Sstevel@tonic-gate #include "ui_locl.h"
1580Sstevel@tonic-gate #include "cryptlib.h"
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS		/* prototypes for sys$whatever */
1610Sstevel@tonic-gate # include <starlet.h>
1620Sstevel@tonic-gate # ifdef __DECC
1630Sstevel@tonic-gate #  pragma message disable DOLLARID
1640Sstevel@tonic-gate # endif
1650Sstevel@tonic-gate #endif
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate #ifdef WIN_CONSOLE_BUG
1680Sstevel@tonic-gate # include <windows.h>
1690Sstevel@tonic-gate #ifndef OPENSSL_SYS_WINCE
1700Sstevel@tonic-gate # include <wincon.h>
1710Sstevel@tonic-gate #endif
1720Sstevel@tonic-gate #endif
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /* There are 5 types of terminal interface supported,
1760Sstevel@tonic-gate  * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate #if defined(__sgi) && !defined(TERMIOS)
1800Sstevel@tonic-gate # define TERMIOS
1810Sstevel@tonic-gate # undef  TERMIO
1820Sstevel@tonic-gate # undef  SGTTY
1830Sstevel@tonic-gate #endif
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate #if defined(linux) && !defined(TERMIO)
1860Sstevel@tonic-gate # undef  TERMIOS
1870Sstevel@tonic-gate # define TERMIO
1880Sstevel@tonic-gate # undef  SGTTY
1890Sstevel@tonic-gate #endif
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate #ifdef _LIBC
1920Sstevel@tonic-gate # undef  TERMIOS
1930Sstevel@tonic-gate # define TERMIO
1940Sstevel@tonic-gate # undef  SGTTY
1950Sstevel@tonic-gate #endif
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(MAC_OS_GUSI_SOURCE)
1980Sstevel@tonic-gate # undef  TERMIOS
1990Sstevel@tonic-gate # undef  TERMIO
2000Sstevel@tonic-gate # define SGTTY
2010Sstevel@tonic-gate #endif
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate #if defined(OPENSSL_SYS_VXWORKS)
2040Sstevel@tonic-gate #undef TERMIOS
2050Sstevel@tonic-gate #undef TERMIO
2060Sstevel@tonic-gate #undef SGTTY
2070Sstevel@tonic-gate #endif
2080Sstevel@tonic-gate 
209*2139Sjp161948 #if defined(OPENSSL_SYS_NETWARE)
210*2139Sjp161948 #undef TERMIOS
211*2139Sjp161948 #undef TERMIO
212*2139Sjp161948 #undef SGTTY
213*2139Sjp161948 #endif
214*2139Sjp161948 
2150Sstevel@tonic-gate #ifdef TERMIOS
2160Sstevel@tonic-gate # include <termios.h>
2170Sstevel@tonic-gate # define TTY_STRUCT		struct termios
2180Sstevel@tonic-gate # define TTY_FLAGS		c_lflag
2190Sstevel@tonic-gate # define TTY_get(tty,data)	tcgetattr(tty,data)
2200Sstevel@tonic-gate # define TTY_set(tty,data)	tcsetattr(tty,TCSANOW,data)
2210Sstevel@tonic-gate #endif
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate #ifdef TERMIO
2240Sstevel@tonic-gate # include <termio.h>
2250Sstevel@tonic-gate # define TTY_STRUCT		struct termio
2260Sstevel@tonic-gate # define TTY_FLAGS		c_lflag
2270Sstevel@tonic-gate # define TTY_get(tty,data)	ioctl(tty,TCGETA,data)
2280Sstevel@tonic-gate # define TTY_set(tty,data)	ioctl(tty,TCSETA,data)
2290Sstevel@tonic-gate #endif
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate #ifdef SGTTY
2320Sstevel@tonic-gate # include <sgtty.h>
2330Sstevel@tonic-gate # define TTY_STRUCT		struct sgttyb
2340Sstevel@tonic-gate # define TTY_FLAGS		sg_flags
2350Sstevel@tonic-gate # define TTY_get(tty,data)	ioctl(tty,TIOCGETP,data)
2360Sstevel@tonic-gate # define TTY_set(tty,data)	ioctl(tty,TIOCSETP,data)
2370Sstevel@tonic-gate #endif
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_SUNOS)
2400Sstevel@tonic-gate # include <sys/ioctl.h>
2410Sstevel@tonic-gate #endif
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate #ifdef OPENSSL_SYS_MSDOS
2440Sstevel@tonic-gate # include <conio.h>
2450Sstevel@tonic-gate #endif
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
2480Sstevel@tonic-gate # include <ssdef.h>
2490Sstevel@tonic-gate # include <iodef.h>
2500Sstevel@tonic-gate # include <ttdef.h>
2510Sstevel@tonic-gate # include <descrip.h>
2520Sstevel@tonic-gate struct IOSB {
2530Sstevel@tonic-gate 	short iosb$w_value;
2540Sstevel@tonic-gate 	short iosb$w_count;
2550Sstevel@tonic-gate 	long  iosb$l_info;
2560Sstevel@tonic-gate 	};
2570Sstevel@tonic-gate #endif
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate #ifdef OPENSSL_SYS_SUNOS
2600Sstevel@tonic-gate 	typedef int sig_atomic_t;
2610Sstevel@tonic-gate #endif
2620Sstevel@tonic-gate 
263*2139Sjp161948 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE) || defined(OPENSSL_SYS_NETWARE)
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate  * This one needs work. As a matter of fact the code is unoperational
2660Sstevel@tonic-gate  * and this is only a trick to get it compiled.
2670Sstevel@tonic-gate  *					<appro@fy.chalmers.se>
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate # define TTY_STRUCT int
2700Sstevel@tonic-gate #endif
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate #ifndef NX509_SIG
2730Sstevel@tonic-gate # define NX509_SIG 32
2740Sstevel@tonic-gate #endif
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /* Define globals.  They are protected by a lock */
2780Sstevel@tonic-gate #ifdef SIGACTION
2790Sstevel@tonic-gate static struct sigaction savsig[NX509_SIG];
2800Sstevel@tonic-gate #else
2810Sstevel@tonic-gate static void (*savsig[NX509_SIG])(int );
2820Sstevel@tonic-gate #endif
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
2850Sstevel@tonic-gate static struct IOSB iosb;
2860Sstevel@tonic-gate static $DESCRIPTOR(terminal,"TT");
2870Sstevel@tonic-gate static long tty_orig[3], tty_new[3]; /* XXX   Is there any guarantee that this will always suffice for the actual structures? */
2880Sstevel@tonic-gate static long status;
2890Sstevel@tonic-gate static unsigned short channel = 0;
2900Sstevel@tonic-gate #else
2910Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
2920Sstevel@tonic-gate static TTY_STRUCT tty_orig,tty_new;
2930Sstevel@tonic-gate #endif
2940Sstevel@tonic-gate #endif
2950Sstevel@tonic-gate static FILE *tty_in, *tty_out;
2960Sstevel@tonic-gate static int is_a_tty;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate /* Declare static functions */
2990Sstevel@tonic-gate #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
3000Sstevel@tonic-gate static void read_till_nl(FILE *);
3010Sstevel@tonic-gate static void recsig(int);
3020Sstevel@tonic-gate static void pushsig(void);
3030Sstevel@tonic-gate static void popsig(void);
3040Sstevel@tonic-gate #endif
3050Sstevel@tonic-gate #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
3060Sstevel@tonic-gate static int noecho_fgets(char *buf, int size, FILE *tty);
3070Sstevel@tonic-gate #endif
3080Sstevel@tonic-gate static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate static int read_string(UI *ui, UI_STRING *uis);
3110Sstevel@tonic-gate static int write_string(UI *ui, UI_STRING *uis);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate static int open_console(UI *ui);
3140Sstevel@tonic-gate static int echo_console(UI *ui);
3150Sstevel@tonic-gate static int noecho_console(UI *ui);
3160Sstevel@tonic-gate static int close_console(UI *ui);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate static UI_METHOD ui_openssl =
3190Sstevel@tonic-gate 	{
3200Sstevel@tonic-gate 	"OpenSSL default user interface",
3210Sstevel@tonic-gate 	open_console,
3220Sstevel@tonic-gate 	write_string,
3230Sstevel@tonic-gate 	NULL,			/* No flusher is needed for command lines */
3240Sstevel@tonic-gate 	read_string,
3250Sstevel@tonic-gate 	close_console,
3260Sstevel@tonic-gate 	NULL
3270Sstevel@tonic-gate 	};
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate /* The method with all the built-in thingies */
UI_OpenSSL(void)3300Sstevel@tonic-gate UI_METHOD *UI_OpenSSL(void)
3310Sstevel@tonic-gate 	{
3320Sstevel@tonic-gate 	return &ui_openssl;
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate /* The following function makes sure that info and error strings are printed
3360Sstevel@tonic-gate    before any prompt. */
write_string(UI * ui,UI_STRING * uis)3370Sstevel@tonic-gate static int write_string(UI *ui, UI_STRING *uis)
3380Sstevel@tonic-gate 	{
3390Sstevel@tonic-gate 	switch (UI_get_string_type(uis))
3400Sstevel@tonic-gate 		{
3410Sstevel@tonic-gate 	case UIT_ERROR:
3420Sstevel@tonic-gate 	case UIT_INFO:
3430Sstevel@tonic-gate 		fputs(UI_get0_output_string(uis), tty_out);
3440Sstevel@tonic-gate 		fflush(tty_out);
3450Sstevel@tonic-gate 		break;
3460Sstevel@tonic-gate 	default:
3470Sstevel@tonic-gate 		break;
3480Sstevel@tonic-gate 		}
3490Sstevel@tonic-gate 	return 1;
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
read_string(UI * ui,UI_STRING * uis)3520Sstevel@tonic-gate static int read_string(UI *ui, UI_STRING *uis)
3530Sstevel@tonic-gate 	{
3540Sstevel@tonic-gate 	int ok = 0;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	switch (UI_get_string_type(uis))
3570Sstevel@tonic-gate 		{
3580Sstevel@tonic-gate 	case UIT_BOOLEAN:
3590Sstevel@tonic-gate 		fputs(UI_get0_output_string(uis), tty_out);
3600Sstevel@tonic-gate 		fputs(UI_get0_action_string(uis), tty_out);
3610Sstevel@tonic-gate 		fflush(tty_out);
3620Sstevel@tonic-gate 		return read_string_inner(ui, uis,
3630Sstevel@tonic-gate 			UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0);
3640Sstevel@tonic-gate 	case UIT_PROMPT:
3650Sstevel@tonic-gate 		fputs(UI_get0_output_string(uis), tty_out);
3660Sstevel@tonic-gate 		fflush(tty_out);
3670Sstevel@tonic-gate 		return read_string_inner(ui, uis,
3680Sstevel@tonic-gate 			UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1);
3690Sstevel@tonic-gate 	case UIT_VERIFY:
3700Sstevel@tonic-gate 		fprintf(tty_out,"Verifying - %s",
3710Sstevel@tonic-gate 			UI_get0_output_string(uis));
3720Sstevel@tonic-gate 		fflush(tty_out);
3730Sstevel@tonic-gate 		if ((ok = read_string_inner(ui, uis,
3740Sstevel@tonic-gate 			UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1)) <= 0)
3750Sstevel@tonic-gate 			return ok;
3760Sstevel@tonic-gate 		if (strcmp(UI_get0_result_string(uis),
3770Sstevel@tonic-gate 			UI_get0_test_string(uis)) != 0)
3780Sstevel@tonic-gate 			{
3790Sstevel@tonic-gate 			fprintf(tty_out,"Verify failure\n");
3800Sstevel@tonic-gate 			fflush(tty_out);
3810Sstevel@tonic-gate 			return 0;
3820Sstevel@tonic-gate 			}
3830Sstevel@tonic-gate 		break;
3840Sstevel@tonic-gate 	default:
3850Sstevel@tonic-gate 		break;
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 	return 1;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
3920Sstevel@tonic-gate /* Internal functions to read a string without echoing */
read_till_nl(FILE * in)3930Sstevel@tonic-gate static void read_till_nl(FILE *in)
3940Sstevel@tonic-gate 	{
3950Sstevel@tonic-gate #define SIZE 4
3960Sstevel@tonic-gate 	char buf[SIZE+1];
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	do	{
3990Sstevel@tonic-gate 		fgets(buf,SIZE,in);
4000Sstevel@tonic-gate 		} while (strchr(buf,'\n') == NULL);
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate static volatile sig_atomic_t intr_signal;
4040Sstevel@tonic-gate #endif
4050Sstevel@tonic-gate 
read_string_inner(UI * ui,UI_STRING * uis,int echo,int strip_nl)4060Sstevel@tonic-gate static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
4070Sstevel@tonic-gate 	{
4080Sstevel@tonic-gate 	static int ps;
4090Sstevel@tonic-gate 	int ok;
4100Sstevel@tonic-gate 	char result[BUFSIZ];
4110Sstevel@tonic-gate 	int maxsize = BUFSIZ-1;
4120Sstevel@tonic-gate #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
4130Sstevel@tonic-gate 	char *p;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	intr_signal=0;
4160Sstevel@tonic-gate 	ok=0;
4170Sstevel@tonic-gate 	ps=0;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	pushsig();
4200Sstevel@tonic-gate 	ps=1;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if (!echo && !noecho_console(ui))
4230Sstevel@tonic-gate 		goto error;
4240Sstevel@tonic-gate 	ps=2;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	result[0]='\0';
4270Sstevel@tonic-gate #ifdef OPENSSL_SYS_MSDOS
4280Sstevel@tonic-gate 	if (!echo)
4290Sstevel@tonic-gate 		{
4300Sstevel@tonic-gate 		noecho_fgets(result,maxsize,tty_in);
4310Sstevel@tonic-gate 		p=result; /* FIXME: noecho_fgets doesn't return errors */
4320Sstevel@tonic-gate 		}
4330Sstevel@tonic-gate 	else
4340Sstevel@tonic-gate 		p=fgets(result,maxsize,tty_in);
4350Sstevel@tonic-gate #else
4360Sstevel@tonic-gate 	p=fgets(result,maxsize,tty_in);
4370Sstevel@tonic-gate #endif
4380Sstevel@tonic-gate 	if(!p)
4390Sstevel@tonic-gate 		goto error;
4400Sstevel@tonic-gate 	if (feof(tty_in)) goto error;
4410Sstevel@tonic-gate 	if (ferror(tty_in)) goto error;
4420Sstevel@tonic-gate 	if ((p=(char *)strchr(result,'\n')) != NULL)
4430Sstevel@tonic-gate 		{
4440Sstevel@tonic-gate 		if (strip_nl)
4450Sstevel@tonic-gate 			*p='\0';
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 	else
4480Sstevel@tonic-gate 		read_till_nl(tty_in);
4490Sstevel@tonic-gate 	if (UI_set_result(ui, uis, result) >= 0)
4500Sstevel@tonic-gate 		ok=1;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate error:
4530Sstevel@tonic-gate 	if (intr_signal == SIGINT)
4540Sstevel@tonic-gate 		ok=-1;
4550Sstevel@tonic-gate 	if (!echo) fprintf(tty_out,"\n");
4560Sstevel@tonic-gate 	if (ps >= 2 && !echo && !echo_console(ui))
4570Sstevel@tonic-gate 		ok=0;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if (ps >= 1)
4600Sstevel@tonic-gate 		popsig();
4610Sstevel@tonic-gate #else
4620Sstevel@tonic-gate 	ok=1;
4630Sstevel@tonic-gate #endif
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	OPENSSL_cleanse(result,BUFSIZ);
4660Sstevel@tonic-gate 	return ok;
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate /* Internal functions to open, handle and close a channel to the console.  */
open_console(UI * ui)4710Sstevel@tonic-gate static int open_console(UI *ui)
4720Sstevel@tonic-gate 	{
4730Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_UI);
4740Sstevel@tonic-gate 	is_a_tty = 1;
4750Sstevel@tonic-gate 
476*2139Sjp161948 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE)
4770Sstevel@tonic-gate 	tty_in=stdin;
4780Sstevel@tonic-gate 	tty_out=stderr;
4790Sstevel@tonic-gate #else
4800Sstevel@tonic-gate #  ifdef OPENSSL_SYS_MSDOS
4810Sstevel@tonic-gate #    define DEV_TTY "con"
4820Sstevel@tonic-gate #  else
4830Sstevel@tonic-gate #    define DEV_TTY "/dev/tty"
4840Sstevel@tonic-gate #  endif
4850Sstevel@tonic-gate 	if ((tty_in=fopen(DEV_TTY,"r")) == NULL)
4860Sstevel@tonic-gate 		tty_in=stdin;
4870Sstevel@tonic-gate 	if ((tty_out=fopen(DEV_TTY,"w")) == NULL)
4880Sstevel@tonic-gate 		tty_out=stderr;
4890Sstevel@tonic-gate #endif
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
492*2139Sjp161948  	if (TTY_get(fileno(tty_in),&tty_orig) == -1)
4930Sstevel@tonic-gate 		{
4940Sstevel@tonic-gate #ifdef ENOTTY
4950Sstevel@tonic-gate 		if (errno == ENOTTY)
4960Sstevel@tonic-gate 			is_a_tty=0;
4970Sstevel@tonic-gate 		else
4980Sstevel@tonic-gate #endif
4990Sstevel@tonic-gate #ifdef EINVAL
5000Sstevel@tonic-gate 		/* Ariel Glenn ariel@columbia.edu reports that solaris
5010Sstevel@tonic-gate 		 * can return EINVAL instead.  This should be ok */
5020Sstevel@tonic-gate 		if (errno == EINVAL)
5030Sstevel@tonic-gate 			is_a_tty=0;
5040Sstevel@tonic-gate 		else
5050Sstevel@tonic-gate #endif
5060Sstevel@tonic-gate 			return 0;
5070Sstevel@tonic-gate 		}
5080Sstevel@tonic-gate #endif
5090Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
5100Sstevel@tonic-gate 	status = sys$assign(&terminal,&channel,0,0);
5110Sstevel@tonic-gate 	if (status != SS$_NORMAL)
5120Sstevel@tonic-gate 		return 0;
5130Sstevel@tonic-gate 	status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
5140Sstevel@tonic-gate 	if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
5150Sstevel@tonic-gate 		return 0;
5160Sstevel@tonic-gate #endif
5170Sstevel@tonic-gate 	return 1;
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
noecho_console(UI * ui)5200Sstevel@tonic-gate static int noecho_console(UI *ui)
5210Sstevel@tonic-gate 	{
5220Sstevel@tonic-gate #ifdef TTY_FLAGS
5230Sstevel@tonic-gate 	memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
5240Sstevel@tonic-gate 	tty_new.TTY_FLAGS &= ~ECHO;
5250Sstevel@tonic-gate #endif
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
5280Sstevel@tonic-gate 	if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
5290Sstevel@tonic-gate 		return 0;
5300Sstevel@tonic-gate #endif
5310Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
5320Sstevel@tonic-gate 	tty_new[0] = tty_orig[0];
5330Sstevel@tonic-gate 	tty_new[1] = tty_orig[1] | TT$M_NOECHO;
5340Sstevel@tonic-gate 	tty_new[2] = tty_orig[2];
5350Sstevel@tonic-gate 	status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
5360Sstevel@tonic-gate 	if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
5370Sstevel@tonic-gate 		return 0;
5380Sstevel@tonic-gate #endif
5390Sstevel@tonic-gate 	return 1;
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
echo_console(UI * ui)5420Sstevel@tonic-gate static int echo_console(UI *ui)
5430Sstevel@tonic-gate 	{
5440Sstevel@tonic-gate #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
5450Sstevel@tonic-gate 	memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
5460Sstevel@tonic-gate 	tty_new.TTY_FLAGS |= ECHO;
5470Sstevel@tonic-gate #endif
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
5500Sstevel@tonic-gate 	if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
5510Sstevel@tonic-gate 		return 0;
5520Sstevel@tonic-gate #endif
5530Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
5540Sstevel@tonic-gate 	tty_new[0] = tty_orig[0];
5550Sstevel@tonic-gate 	tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
5560Sstevel@tonic-gate 	tty_new[2] = tty_orig[2];
5570Sstevel@tonic-gate 	status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
5580Sstevel@tonic-gate 	if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
5590Sstevel@tonic-gate 		return 0;
5600Sstevel@tonic-gate #endif
5610Sstevel@tonic-gate 	return 1;
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
close_console(UI * ui)5640Sstevel@tonic-gate static int close_console(UI *ui)
5650Sstevel@tonic-gate 	{
5660Sstevel@tonic-gate 	if (tty_in != stdin) fclose(tty_in);
5670Sstevel@tonic-gate 	if (tty_out != stderr) fclose(tty_out);
5680Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
5690Sstevel@tonic-gate 	status = sys$dassgn(channel);
5700Sstevel@tonic-gate #endif
5710Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_UI);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	return 1;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
5780Sstevel@tonic-gate /* Internal functions to handle signals and act on them */
pushsig(void)5790Sstevel@tonic-gate static void pushsig(void)
5800Sstevel@tonic-gate 	{
581*2139Sjp161948 #ifndef OPENSSL_SYS_WIN32
5820Sstevel@tonic-gate 	int i;
583*2139Sjp161948 #endif
5840Sstevel@tonic-gate #ifdef SIGACTION
5850Sstevel@tonic-gate 	struct sigaction sa;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	memset(&sa,0,sizeof sa);
5880Sstevel@tonic-gate 	sa.sa_handler=recsig;
5890Sstevel@tonic-gate #endif
5900Sstevel@tonic-gate 
591*2139Sjp161948 #ifdef OPENSSL_SYS_WIN32
592*2139Sjp161948 	savsig[SIGABRT]=signal(SIGABRT,recsig);
593*2139Sjp161948 	savsig[SIGFPE]=signal(SIGFPE,recsig);
594*2139Sjp161948 	savsig[SIGILL]=signal(SIGILL,recsig);
595*2139Sjp161948 	savsig[SIGINT]=signal(SIGINT,recsig);
596*2139Sjp161948 	savsig[SIGSEGV]=signal(SIGSEGV,recsig);
597*2139Sjp161948 	savsig[SIGTERM]=signal(SIGTERM,recsig);
598*2139Sjp161948 #else
5990Sstevel@tonic-gate 	for (i=1; i<NX509_SIG; i++)
6000Sstevel@tonic-gate 		{
6010Sstevel@tonic-gate #ifdef SIGUSR1
6020Sstevel@tonic-gate 		if (i == SIGUSR1)
6030Sstevel@tonic-gate 			continue;
6040Sstevel@tonic-gate #endif
6050Sstevel@tonic-gate #ifdef SIGUSR2
6060Sstevel@tonic-gate 		if (i == SIGUSR2)
6070Sstevel@tonic-gate 			continue;
6080Sstevel@tonic-gate #endif
6090Sstevel@tonic-gate #ifdef SIGKILL
6100Sstevel@tonic-gate 		if (i == SIGKILL) /* We can't make any action on that. */
6110Sstevel@tonic-gate 			continue;
6120Sstevel@tonic-gate #endif
6130Sstevel@tonic-gate #ifdef SIGACTION
6140Sstevel@tonic-gate 		sigaction(i,&sa,&savsig[i]);
6150Sstevel@tonic-gate #else
6160Sstevel@tonic-gate 		savsig[i]=signal(i,recsig);
6170Sstevel@tonic-gate #endif
6180Sstevel@tonic-gate 		}
619*2139Sjp161948 #endif
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate #ifdef SIGWINCH
6220Sstevel@tonic-gate 	signal(SIGWINCH,SIG_DFL);
6230Sstevel@tonic-gate #endif
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 
popsig(void)6260Sstevel@tonic-gate static void popsig(void)
6270Sstevel@tonic-gate 	{
628*2139Sjp161948 #ifdef OPENSSL_SYS_WIN32
629*2139Sjp161948 	signal(SIGABRT,savsig[SIGABRT]);
630*2139Sjp161948 	signal(SIGFPE,savsig[SIGFPE]);
631*2139Sjp161948 	signal(SIGILL,savsig[SIGILL]);
632*2139Sjp161948 	signal(SIGINT,savsig[SIGINT]);
633*2139Sjp161948 	signal(SIGSEGV,savsig[SIGSEGV]);
634*2139Sjp161948 	signal(SIGTERM,savsig[SIGTERM]);
635*2139Sjp161948 #else
6360Sstevel@tonic-gate 	int i;
6370Sstevel@tonic-gate 	for (i=1; i<NX509_SIG; i++)
6380Sstevel@tonic-gate 		{
6390Sstevel@tonic-gate #ifdef SIGUSR1
6400Sstevel@tonic-gate 		if (i == SIGUSR1)
6410Sstevel@tonic-gate 			continue;
6420Sstevel@tonic-gate #endif
6430Sstevel@tonic-gate #ifdef SIGUSR2
6440Sstevel@tonic-gate 		if (i == SIGUSR2)
6450Sstevel@tonic-gate 			continue;
6460Sstevel@tonic-gate #endif
6470Sstevel@tonic-gate #ifdef SIGACTION
6480Sstevel@tonic-gate 		sigaction(i,&savsig[i],NULL);
6490Sstevel@tonic-gate #else
6500Sstevel@tonic-gate 		signal(i,savsig[i]);
6510Sstevel@tonic-gate #endif
6520Sstevel@tonic-gate 		}
653*2139Sjp161948 #endif
6540Sstevel@tonic-gate 	}
6550Sstevel@tonic-gate 
recsig(int i)6560Sstevel@tonic-gate static void recsig(int i)
6570Sstevel@tonic-gate 	{
6580Sstevel@tonic-gate 	intr_signal=i;
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate #endif
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate /* Internal functions specific for Windows */
6630Sstevel@tonic-gate #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
noecho_fgets(char * buf,int size,FILE * tty)6640Sstevel@tonic-gate static int noecho_fgets(char *buf, int size, FILE *tty)
6650Sstevel@tonic-gate 	{
6660Sstevel@tonic-gate 	int i;
6670Sstevel@tonic-gate 	char *p;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	p=buf;
6700Sstevel@tonic-gate 	for (;;)
6710Sstevel@tonic-gate 		{
6720Sstevel@tonic-gate 		if (size == 0)
6730Sstevel@tonic-gate 			{
6740Sstevel@tonic-gate 			*p='\0';
6750Sstevel@tonic-gate 			break;
6760Sstevel@tonic-gate 			}
6770Sstevel@tonic-gate 		size--;
6780Sstevel@tonic-gate #ifdef WIN16TTY
6790Sstevel@tonic-gate 		i=_inchar();
6800Sstevel@tonic-gate #else
6810Sstevel@tonic-gate 		i=getch();
6820Sstevel@tonic-gate #endif
6830Sstevel@tonic-gate 		if (i == '\r') i='\n';
6840Sstevel@tonic-gate 		*(p++)=i;
6850Sstevel@tonic-gate 		if (i == '\n')
6860Sstevel@tonic-gate 			{
6870Sstevel@tonic-gate 			*p='\0';
6880Sstevel@tonic-gate 			break;
6890Sstevel@tonic-gate 			}
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate #ifdef WIN_CONSOLE_BUG
6920Sstevel@tonic-gate /* Win95 has several evil console bugs: one of these is that the
6930Sstevel@tonic-gate  * last character read using getch() is passed to the next read: this is
6940Sstevel@tonic-gate  * usually a CR so this can be trouble. No STDIO fix seems to work but
6950Sstevel@tonic-gate  * flushing the console appears to do the trick.
6960Sstevel@tonic-gate  */
6970Sstevel@tonic-gate 		{
6980Sstevel@tonic-gate 			HANDLE inh;
6990Sstevel@tonic-gate 			inh = GetStdHandle(STD_INPUT_HANDLE);
7000Sstevel@tonic-gate 			FlushConsoleInputBuffer(inh);
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate #endif
7030Sstevel@tonic-gate 	return(strlen(buf));
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate #endif
706