ssl/libcrypto/src/crypto/ui/ui_openssl.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* crypto/ui/ui_openssl.c -*- mode:C; c-file-style: "eay" -*- */
       
     2 /* Written by Richard Levitte (richard@levitte.org) and others
       
     3  * for the OpenSSL project 2001.
       
     4  */
       
     5 /* ====================================================================
       
     6  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
       
     7  *
       
     8  * Redistribution and use in source and binary forms, with or without
       
     9  * modification, are permitted provided that the following conditions
       
    10  * are met:
       
    11  *
       
    12  * 1. Redistributions of source code must retain the above copyright
       
    13  *    notice, this list of conditions and the following disclaimer. 
       
    14  *
       
    15  * 2. Redistributions in binary form must reproduce the above copyright
       
    16  *    notice, this list of conditions and the following disclaimer in
       
    17  *    the documentation and/or other materials provided with the
       
    18  *    distribution.
       
    19  *
       
    20  * 3. All advertising materials mentioning features or use of this
       
    21  *    software must display the following acknowledgment:
       
    22  *    "This product includes software developed by the OpenSSL Project
       
    23  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
       
    24  *
       
    25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
       
    26  *    endorse or promote products derived from this software without
       
    27  *    prior written permission. For written permission, please contact
       
    28  *    openssl-core@openssl.org.
       
    29  *
       
    30  * 5. Products derived from this software may not be called "OpenSSL"
       
    31  *    nor may "OpenSSL" appear in their names without prior written
       
    32  *    permission of the OpenSSL Project.
       
    33  *
       
    34  * 6. Redistributions of any form whatsoever must retain the following
       
    35  *    acknowledgment:
       
    36  *    "This product includes software developed by the OpenSSL Project
       
    37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
       
    38  *
       
    39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
       
    40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
       
    43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
       
    45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
       
    48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
       
    50  * OF THE POSSIBILITY OF SUCH DAMAGE.
       
    51  * ====================================================================
       
    52  *
       
    53  * This product includes cryptographic software written by Eric Young
       
    54  * (eay@cryptsoft.com).  This product includes software written by Tim
       
    55  * Hudson (tjh@cryptsoft.com).
       
    56  *
       
    57  */
       
    58 
       
    59 /* The lowest level part of this file was previously in crypto/des/read_pwd.c,
       
    60  * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
       
    61  * All rights reserved.
       
    62  *
       
    63  * This package is an SSL implementation written
       
    64  * by Eric Young (eay@cryptsoft.com).
       
    65  * The implementation was written so as to conform with Netscapes SSL.
       
    66  * 
       
    67  * This library is free for commercial and non-commercial use as long as
       
    68  * the following conditions are aheared to.  The following conditions
       
    69  * apply to all code found in this distribution, be it the RC4, RSA,
       
    70  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
       
    71  * included with this distribution is covered by the same copyright terms
       
    72  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
       
    73  * 
       
    74  * Copyright remains Eric Young's, and as such any Copyright notices in
       
    75  * the code are not to be removed.
       
    76  * If this package is used in a product, Eric Young should be given attribution
       
    77  * as the author of the parts of the library used.
       
    78  * This can be in the form of a textual message at program startup or
       
    79  * in documentation (online or textual) provided with the package.
       
    80  * 
       
    81  * Redistribution and use in source and binary forms, with or without
       
    82  * modification, are permitted provided that the following conditions
       
    83  * are met:
       
    84  * 1. Redistributions of source code must retain the copyright
       
    85  *    notice, this list of conditions and the following disclaimer.
       
    86  * 2. Redistributions in binary form must reproduce the above copyright
       
    87  *    notice, this list of conditions and the following disclaimer in the
       
    88  *    documentation and/or other materials provided with the distribution.
       
    89  * 3. All advertising materials mentioning features or use of this software
       
    90  *    must display the following acknowledgement:
       
    91  *    "This product includes cryptographic software written by
       
    92  *     Eric Young (eay@cryptsoft.com)"
       
    93  *    The word 'cryptographic' can be left out if the rouines from the library
       
    94  *    being used are not cryptographic related :-).
       
    95  * 4. If you include any Windows specific code (or a derivative thereof) from 
       
    96  *    the apps directory (application code) you must include an acknowledgement:
       
    97  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
       
    98  * 
       
    99  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
       
   100  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
   101  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
   102  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
       
   103  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
       
   104  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
       
   105  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
   106  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
       
   107  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
       
   108  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
       
   109  * SUCH DAMAGE.
       
   110  * 
       
   111  * The licence and distribution terms for any publically available version or
       
   112  * derivative of this code cannot be changed.  i.e. this code cannot simply be
       
   113  * copied and put under another distribution licence
       
   114  * [including the GNU Public Licence.]
       
   115  */
       
   116 /*
       
   117  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
   118  */
       
   119 
       
   120 
       
   121 #include <openssl/e_os2.h>
       
   122 
       
   123 /* need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
       
   124  * [maybe others?], because it masks interfaces not discussed in standard,
       
   125  * sigaction and fileno included. -pedantic would be more appropriate for
       
   126  * the intended purposes, but we can't prevent users from adding -ansi.
       
   127  */
       
   128  
       
   129 #define _POSIX_C_SOURCE 1
       
   130 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) 
       
   131 #undef _POSIX_C_SOURCE
       
   132 #endif
       
   133 #ifndef SYMBIAN
       
   134 #include <signal.h> 
       
   135 #endif
       
   136 #include <stdio.h>
       
   137 #include <string.h>
       
   138 #include <errno.h>
       
   139 
       
   140 
       
   141 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
       
   142 # ifdef OPENSSL_UNISTD
       
   143 #  include OPENSSL_UNISTD
       
   144 # else
       
   145 #  include <unistd.h>
       
   146 # endif
       
   147 /* If unistd.h defines _POSIX_VERSION, we conclude that we
       
   148  * are on a POSIX system and have sigaction and termios. */
       
   149 # if defined(_POSIX_VERSION) && !defined(SYMBIAN)
       
   150 #  define SIGACTION
       
   151 #  if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
       
   152 #   define TERMIOS
       
   153 #  endif
       
   154 
       
   155 # endif
       
   156 #endif
       
   157 
       
   158 #ifdef WIN16TTY
       
   159 # undef OPENSSL_SYS_WIN16
       
   160 # undef WIN16
       
   161 # undef _WINDOWS
       
   162 # include <graph.h>
       
   163 #endif
       
   164 
       
   165 /* 06-Apr-92 Luke Brennan    Support for VMS */
       
   166 #include "ui_locl.h"
       
   167 #include "cryptlib.h"
       
   168 
       
   169 #ifdef OPENSSL_SYS_VMS		/* prototypes for sys$whatever */
       
   170 # include <starlet.h>
       
   171 # ifdef __DECC
       
   172 #  pragma message disable DOLLARID
       
   173 # endif
       
   174 #endif
       
   175 
       
   176 #ifndef SYMBIAN
       
   177 #ifdef WIN_CONSOLE_BUG
       
   178 # include <windows.h>
       
   179 #ifndef OPENSSL_SYS_WINCE
       
   180 # include <wincon.h>
       
   181 #endif
       
   182 #endif
       
   183 #endif
       
   184 
       
   185 /* There are 5 types of terminal interface supported,
       
   186  * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
       
   187  */
       
   188 
       
   189 #if defined(__sgi) && !defined(TERMIOS)
       
   190 # define TERMIOS
       
   191 # undef  TERMIO
       
   192 # undef  SGTTY
       
   193 #endif
       
   194 
       
   195 #if defined(linux) && !defined(TERMIO)
       
   196 # undef  TERMIOS
       
   197 # define TERMIO
       
   198 # undef  SGTTY
       
   199 #endif
       
   200 
       
   201 #ifdef _LIBC
       
   202 # undef  TERMIOS
       
   203 # define TERMIO
       
   204 # undef  SGTTY
       
   205 #endif
       
   206 
       
   207 #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(MAC_OS_GUSI_SOURCE)
       
   208 # undef  TERMIOS
       
   209 # undef  TERMIO
       
   210 # define SGTTY
       
   211 #endif
       
   212 
       
   213 #if defined(OPENSSL_SYS_VXWORKS)
       
   214 #undef TERMIOS
       
   215 #undef TERMIO
       
   216 #undef SGTTY
       
   217 #endif
       
   218 
       
   219 #if defined(OPENSSL_SYS_NETWARE)
       
   220 #undef TERMIOS
       
   221 #undef TERMIO
       
   222 #undef SGTTY
       
   223 #endif
       
   224 
       
   225 #if defined(SYMBIAN)
       
   226 #undef TERMIOS
       
   227 #undef TERMIO
       
   228 #undef SGTTY
       
   229 #endif
       
   230 
       
   231 #ifdef TERMIOS
       
   232 # include <termios.h>
       
   233 # define TTY_STRUCT		struct termios
       
   234 # define TTY_FLAGS		c_lflag
       
   235 # define TTY_get(tty,data)	tcgetattr(tty,data)
       
   236 # define TTY_set(tty,data)	tcsetattr(tty,TCSANOW,data)
       
   237 #endif
       
   238 
       
   239 #ifdef TERMIO
       
   240 # include <termio.h>
       
   241 # define TTY_STRUCT		struct termio
       
   242 # define TTY_FLAGS		c_lflag
       
   243 # define TTY_get(tty,data)	ioctl(tty,TCGETA,data)
       
   244 # define TTY_set(tty,data)	ioctl(tty,TCSETA,data)
       
   245 #endif
       
   246 
       
   247 #ifdef SGTTY
       
   248 # include <sgtty.h>
       
   249 # define TTY_STRUCT		struct sgttyb
       
   250 # define TTY_FLAGS		sg_flags
       
   251 # define TTY_get(tty,data)	ioctl(tty,TIOCGETP,data)
       
   252 # define TTY_set(tty,data)	ioctl(tty,TIOCSETP,data)
       
   253 #endif
       
   254 
       
   255 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_SUNOS)
       
   256 # include <sys/ioctl.h>
       
   257 #endif
       
   258 
       
   259 #ifdef OPENSSL_SYS_MSDOS
       
   260 # include <conio.h>
       
   261 #endif
       
   262 
       
   263 #ifdef OPENSSL_SYS_VMS
       
   264 # include <ssdef.h>
       
   265 # include <iodef.h>
       
   266 # include <ttdef.h>
       
   267 # include <descrip.h>
       
   268 struct IOSB {
       
   269 	short iosb$w_value;
       
   270 	short iosb$w_count;
       
   271 	long  iosb$l_info;
       
   272 	};
       
   273 #endif
       
   274 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
       
   275 #include "libcrypto_wsd_macros.h"
       
   276 #include "libcrypto_wsd.h"
       
   277 #endif
       
   278 
       
   279 #ifdef OPENSSL_SYS_SUNOS
       
   280 	typedef int sig_atomic_t;
       
   281 #endif
       
   282 
       
   283 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE) || defined(OPENSSL_SYS_NETWARE)
       
   284 /*
       
   285  * This one needs work. As a matter of fact the code is unoperational
       
   286  * and this is only a trick to get it compiled.
       
   287  *					<appro@fy.chalmers.se>
       
   288  */
       
   289 # define TTY_STRUCT int
       
   290 #endif
       
   291 
       
   292 #ifndef NX509_SIG
       
   293 # define NX509_SIG 32
       
   294 #endif
       
   295 
       
   296 
       
   297 /* Define globals.  They are protected by a lock */
       
   298 #ifdef SIGACTION
       
   299 static struct sigaction savsig[NX509_SIG];
       
   300 #else
       
   301 static void (*savsig[NX509_SIG])(int );
       
   302 #endif
       
   303 
       
   304 #ifdef OPENSSL_SYS_VMS
       
   305 static struct IOSB iosb;
       
   306 static $DESCRIPTOR(terminal,"TT");
       
   307 static long tty_orig[3], tty_new[3]; /* XXX   Is there any guarantee that this will always suffice for the actual structures? */
       
   308 static long status;
       
   309 static unsigned short channel = 0;
       
   310 #else
       
   311 #ifndef SYMBIAN
       
   312 #if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
       
   313 static TTY_STRUCT tty_orig,tty_new;
       
   314 #endif
       
   315 #endif
       
   316 #endif
       
   317 #ifndef EMULATOR
       
   318 static FILE *tty_in, *tty_out;
       
   319 static int is_a_tty;
       
   320 #else
       
   321 GET_STATIC_VAR_FROM_TLS(tty_in,ui_openssl,FILE *)
       
   322 #define tty_in (*GET_WSD_VAR_NAME(tty_in,ui_openssl, s)())
       
   323 
       
   324 GET_STATIC_VAR_FROM_TLS(tty_out,ui_openssl,FILE *)
       
   325 #define tty_out (*GET_WSD_VAR_NAME(tty_out,ui_openssl, s)())
       
   326   
       
   327 GET_STATIC_VAR_FROM_TLS(is_a_tty,ui_openssl,int)
       
   328 #define is_a_tty (*GET_WSD_VAR_NAME(is_a_tty,ui_openssl, s)())
       
   329 
       
   330 #endif
       
   331 /* Declare static functions */
       
   332 #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
       
   333 static void read_till_nl(FILE *);
       
   334 static void recsig(int);
       
   335 static void pushsig(void);
       
   336 static void popsig(void);
       
   337 #endif
       
   338 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
       
   339 static int noecho_fgets(char *buf, int size, FILE *tty);
       
   340 #endif
       
   341 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
       
   342 
       
   343 static int read_string(UI *ui, UI_STRING *uis);
       
   344 static int write_string(UI *ui, UI_STRING *uis);
       
   345 
       
   346 static int open_console(UI *ui);
       
   347 static int echo_console(UI *ui);
       
   348 static int noecho_console(UI *ui);
       
   349 static int close_console(UI *ui);
       
   350 
       
   351 #ifndef EMULATOR
       
   352 static UI_METHOD ui_openssl =
       
   353 	{
       
   354 	"OpenSSL default user interface",
       
   355 	open_console,
       
   356 	write_string,
       
   357 	NULL,			/* No flusher is needed for command lines */
       
   358 	read_string,
       
   359 	close_console,
       
   360 	NULL
       
   361 	};
       
   362 #else
       
   363 GET_STATIC_VAR_FROM_TLS(ui_openssl,ui_openssl,UI_METHOD)
       
   364 #define ui_openssl (*GET_WSD_VAR_NAME(ui_openssl,ui_openssl, s)())
       
   365 const UI_METHOD temp_s_ui_openssl =
       
   366 	{
       
   367 	"OpenSSL default user interface",
       
   368 	open_console,
       
   369 	write_string,
       
   370 	NULL,			/* No flusher is needed for command lines */
       
   371 	read_string,
       
   372 	close_console,
       
   373 	NULL
       
   374 	};
       
   375 
       
   376 #endif
       
   377 /* The method with all the built-in thingies */
       
   378 EXPORT_C UI_METHOD *UI_OpenSSL(void)
       
   379 	{
       
   380 	return &ui_openssl;
       
   381 	}
       
   382 
       
   383 /* The following function makes sure that info and error strings are printed
       
   384    before any prompt. */
       
   385 static int write_string(UI *ui, UI_STRING *uis)
       
   386 	{
       
   387 	switch (UI_get_string_type(uis))
       
   388 		{
       
   389 	case UIT_ERROR:
       
   390 	case UIT_INFO:
       
   391 		fputs(UI_get0_output_string(uis), tty_out);
       
   392 		fflush(tty_out);
       
   393 		break;
       
   394 	default:
       
   395 		break;
       
   396 		}
       
   397 	return 1;
       
   398 	}
       
   399 
       
   400 static int read_string(UI *ui, UI_STRING *uis)
       
   401 	{
       
   402 	int ok = 0;
       
   403 
       
   404 	switch (UI_get_string_type(uis))
       
   405 		{
       
   406 	case UIT_BOOLEAN:
       
   407 		fputs(UI_get0_output_string(uis), tty_out);
       
   408 		fputs(UI_get0_action_string(uis), tty_out);
       
   409 		fflush(tty_out);
       
   410 		return read_string_inner(ui, uis,
       
   411 			UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0);
       
   412 	case UIT_PROMPT:
       
   413 		fputs(UI_get0_output_string(uis), tty_out);
       
   414 		fflush(tty_out);
       
   415 		return read_string_inner(ui, uis,
       
   416 			UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1);
       
   417 	case UIT_VERIFY:
       
   418 		fprintf(tty_out,"Verifying - %s",
       
   419 			UI_get0_output_string(uis));
       
   420 		fflush(tty_out);
       
   421 		if ((ok = read_string_inner(ui, uis,
       
   422 			UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1)) <= 0)
       
   423 			return ok;
       
   424 		if (strcmp(UI_get0_result_string(uis),
       
   425 			UI_get0_test_string(uis)) != 0)
       
   426 			{
       
   427 			fprintf(tty_out,"Verify failure\n");
       
   428 			fflush(tty_out);
       
   429 			return 0;
       
   430 			}
       
   431 		break;
       
   432 	default:
       
   433 		break;
       
   434 		}
       
   435 	return 1;
       
   436 	}
       
   437 
       
   438 #ifndef SYMBIAN
       
   439 #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
       
   440 /* Internal functions to read a string without echoing */
       
   441 static void read_till_nl(FILE *in)
       
   442 	{
       
   443 #define SIZE 4
       
   444 	char buf[SIZE+1];
       
   445 
       
   446 	do	{
       
   447 		fgets(buf,SIZE,in);
       
   448 		} while (strchr(buf,'\n') == NULL);
       
   449 	}
       
   450 
       
   451 static volatile sig_atomic_t intr_signal;
       
   452 #endif
       
   453 #endif
       
   454 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
       
   455 	{
       
   456 	static int ps;
       
   457 	int ok;
       
   458 	char result[BUFSIZ];
       
   459 	int maxsize = BUFSIZ-1;
       
   460 #ifndef SYMBIAN	
       
   461 #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
       
   462 	char *p;
       
   463 
       
   464 	intr_signal=0;
       
   465 	ok=0;
       
   466 	ps=0;
       
   467 
       
   468 	pushsig();
       
   469 	ps=1;
       
   470 
       
   471 	if (!echo && !noecho_console(ui))
       
   472 		goto error;
       
   473 	ps=2;
       
   474 
       
   475 	result[0]='\0';
       
   476 #ifdef OPENSSL_SYS_MSDOS
       
   477 	if (!echo)
       
   478 		{
       
   479 		noecho_fgets(result,maxsize,tty_in);
       
   480 		p=result; /* FIXME: noecho_fgets doesn't return errors */
       
   481 		}
       
   482 	else
       
   483 		p=fgets(result,maxsize,tty_in);
       
   484 #else
       
   485 	p=fgets(result,maxsize,tty_in);
       
   486 #endif
       
   487 	if(!p)
       
   488 		goto error;
       
   489 	if (feof(tty_in)) goto error;
       
   490 	if (ferror(tty_in)) goto error;
       
   491 	if ((p=(char *)strchr(result,'\n')) != NULL)
       
   492 		{
       
   493 		if (strip_nl)
       
   494 			*p='\0';
       
   495 		}
       
   496 	else
       
   497 		read_till_nl(tty_in);
       
   498 	if (UI_set_result(ui, uis, result) >= 0)
       
   499 		ok=1;
       
   500 
       
   501 error:
       
   502 	if (intr_signal == SIGINT)
       
   503 		ok=-1;
       
   504 	if (!echo) fprintf(tty_out,"\n");
       
   505 	if (ps >= 2 && !echo && !echo_console(ui))
       
   506 		ok=0;
       
   507 
       
   508 	if (ps >= 1)
       
   509 		popsig();
       
   510 #else
       
   511 	ok=1;
       
   512 #endif
       
   513 #endif
       
   514 	OPENSSL_cleanse(result,BUFSIZ);
       
   515 	return ok;
       
   516 	}
       
   517 
       
   518 
       
   519 /* Internal functions to open, handle and close a channel to the console.  */
       
   520 static int open_console(UI *ui)
       
   521 	{
       
   522 	CRYPTO_w_lock(CRYPTO_LOCK_UI);
       
   523 	is_a_tty = 1;
       
   524 
       
   525 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE)
       
   526 	tty_in=stdin;
       
   527 	tty_out=stderr;
       
   528 #else
       
   529 #  ifdef OPENSSL_SYS_MSDOS
       
   530 #    define DEV_TTY "con"
       
   531 #  else
       
   532 #    define DEV_TTY "/dev/tty"
       
   533 #  endif
       
   534 	if ((tty_in=fopen(DEV_TTY,"r")) == NULL)
       
   535 		tty_in=stdin;
       
   536 	if ((tty_out=fopen(DEV_TTY,"w")) == NULL)
       
   537 		tty_out=stderr;
       
   538 #endif
       
   539 
       
   540 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
       
   541  	if (TTY_get(fileno(tty_in),&tty_orig) == -1)
       
   542 		{
       
   543 #ifdef ENOTTY
       
   544 		if (errno == ENOTTY)
       
   545 			is_a_tty=0;
       
   546 		else
       
   547 #endif
       
   548 #ifdef EINVAL
       
   549 		/* Ariel Glenn ariel@columbia.edu reports that solaris
       
   550 		 * can return EINVAL instead.  This should be ok */
       
   551 		if (errno == EINVAL)
       
   552 			is_a_tty=0;
       
   553 		else
       
   554 #endif
       
   555 			return 0;
       
   556 		}
       
   557 #endif
       
   558 #ifdef OPENSSL_SYS_VMS
       
   559 	status = sys$assign(&terminal,&channel,0,0);
       
   560 	if (status != SS$_NORMAL)
       
   561 		return 0;
       
   562 	status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
       
   563 	if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
       
   564 		return 0;
       
   565 #endif
       
   566 	return 1;
       
   567 	}
       
   568 
       
   569 static int noecho_console(UI *ui)
       
   570 	{
       
   571 #ifdef TTY_FLAGS
       
   572 	memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
       
   573 	tty_new.TTY_FLAGS &= ~ECHO;
       
   574 #endif
       
   575 
       
   576 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
       
   577 	if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
       
   578 		return 0;
       
   579 #endif
       
   580 #ifdef OPENSSL_SYS_VMS
       
   581 	tty_new[0] = tty_orig[0];
       
   582 	tty_new[1] = tty_orig[1] | TT$M_NOECHO;
       
   583 	tty_new[2] = tty_orig[2];
       
   584 	status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
       
   585 	if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
       
   586 		return 0;
       
   587 #endif
       
   588 	return 1;
       
   589 	}
       
   590 
       
   591 static int echo_console(UI *ui)
       
   592 	{
       
   593 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
       
   594 	memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
       
   595 	tty_new.TTY_FLAGS |= ECHO;
       
   596 #endif
       
   597 
       
   598 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
       
   599 	if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
       
   600 		return 0;
       
   601 #endif
       
   602 #ifdef OPENSSL_SYS_VMS
       
   603 	tty_new[0] = tty_orig[0];
       
   604 	tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
       
   605 	tty_new[2] = tty_orig[2];
       
   606 	status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
       
   607 	if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
       
   608 		return 0;
       
   609 #endif
       
   610 	return 1;
       
   611 	}
       
   612 
       
   613 static int close_console(UI *ui)
       
   614 	{
       
   615 	if (tty_in != stdin) fclose(tty_in);
       
   616 	if (tty_out != stderr) fclose(tty_out);
       
   617 #ifdef OPENSSL_SYS_VMS
       
   618 	status = sys$dassgn(channel);
       
   619 #endif
       
   620 	CRYPTO_w_unlock(CRYPTO_LOCK_UI);
       
   621 
       
   622 	return 1;
       
   623 	}
       
   624 
       
   625 
       
   626 #ifndef SYMBIAN
       
   627 #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
       
   628 /* Internal functions to handle signals and act on them */
       
   629 static void pushsig(void)
       
   630 	{
       
   631 #ifndef OPENSSL_SYS_WIN32
       
   632 	int i;
       
   633 #endif
       
   634 #ifdef SIGACTION
       
   635 	struct sigaction sa;
       
   636 
       
   637 	memset(&sa,0,sizeof sa);
       
   638 	sa.sa_handler=recsig;
       
   639 #endif
       
   640 
       
   641 #ifdef OPENSSL_SYS_WIN32
       
   642 	savsig[SIGABRT]=signal(SIGABRT,recsig);
       
   643 	savsig[SIGFPE]=signal(SIGFPE,recsig);
       
   644 	savsig[SIGILL]=signal(SIGILL,recsig);
       
   645 	savsig[SIGINT]=signal(SIGINT,recsig);
       
   646 	savsig[SIGSEGV]=signal(SIGSEGV,recsig);
       
   647 	savsig[SIGTERM]=signal(SIGTERM,recsig);
       
   648 #else
       
   649 	for (i=1; i<NX509_SIG; i++)
       
   650 		{
       
   651 #ifdef SIGUSR1
       
   652 		if (i == SIGUSR1)
       
   653 			continue;
       
   654 #endif
       
   655 #ifdef SIGUSR2
       
   656 		if (i == SIGUSR2)
       
   657 			continue;
       
   658 #endif
       
   659 #ifdef SIGKILL
       
   660 		if (i == SIGKILL) /* We can't make any action on that. */
       
   661 			continue;
       
   662 #endif
       
   663 #ifdef SIGACTION
       
   664 		sigaction(i,&sa,&savsig[i]);
       
   665 #else
       
   666 		savsig[i]=signal(i,recsig);
       
   667 #endif
       
   668 		}
       
   669 #endif
       
   670 
       
   671 #ifdef SIGWINCH
       
   672 	signal(SIGWINCH,SIG_DFL);
       
   673 #endif
       
   674 	}
       
   675 
       
   676 static void popsig(void)
       
   677 	{
       
   678 #ifdef OPENSSL_SYS_WIN32
       
   679 	signal(SIGABRT,savsig[SIGABRT]);
       
   680 	signal(SIGFPE,savsig[SIGFPE]);
       
   681 	signal(SIGILL,savsig[SIGILL]);
       
   682 	signal(SIGINT,savsig[SIGINT]);
       
   683 	signal(SIGSEGV,savsig[SIGSEGV]);
       
   684 	signal(SIGTERM,savsig[SIGTERM]);
       
   685 #else
       
   686 	int i;
       
   687 	for (i=1; i<NX509_SIG; i++)
       
   688 		{
       
   689 #ifdef SIGUSR1
       
   690 		if (i == SIGUSR1)
       
   691 			continue;
       
   692 #endif
       
   693 #ifdef SIGUSR2
       
   694 		if (i == SIGUSR2)
       
   695 			continue;
       
   696 #endif
       
   697 #ifdef SIGACTION
       
   698 		sigaction(i,&savsig[i],NULL);
       
   699 #else
       
   700 		signal(i,savsig[i]);
       
   701 #endif
       
   702 		}
       
   703 #endif
       
   704 	}
       
   705 
       
   706 static void recsig(int i)
       
   707 	{
       
   708 	intr_signal=i;
       
   709 	}
       
   710 #endif
       
   711 #endif
       
   712 
       
   713 /* Internal functions specific for Windows */
       
   714 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
       
   715 static int noecho_fgets(char *buf, int size, FILE *tty)
       
   716 	{
       
   717 	int i;
       
   718 	char *p;
       
   719 
       
   720 	p=buf;
       
   721 	for (;;)
       
   722 		{
       
   723 		if (size == 0)
       
   724 			{
       
   725 			*p='\0';
       
   726 			break;
       
   727 			}
       
   728 		size--;
       
   729 #ifdef WIN16TTY
       
   730 		i=_inchar();
       
   731 #else
       
   732 		i=getch();
       
   733 #endif
       
   734 		if (i == '\r') i='\n';
       
   735 		*(p++)=i;
       
   736 		if (i == '\n')
       
   737 			{
       
   738 			*p='\0';
       
   739 			break;
       
   740 			}
       
   741 		}
       
   742 #ifndef SYMBIAN		
       
   743 #ifdef WIN_CONSOLE_BUG
       
   744 /* Win95 has several evil console bugs: one of these is that the
       
   745  * last character read using getch() is passed to the next read: this is
       
   746  * usually a CR so this can be trouble. No STDIO fix seems to work but
       
   747  * flushing the console appears to do the trick.
       
   748  */
       
   749 		{
       
   750 			HANDLE inh;
       
   751 			inh = GetStdHandle(STD_INPUT_HANDLE);
       
   752 			FlushConsoleInputBuffer(inh);
       
   753 		}
       
   754 #endif
       
   755 #endif
       
   756 	return(strlen(buf));
       
   757 	}
       
   758 #endif
       
   759