1 /* crypto/ui/ui_openssl.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Written by Richard Levitte (levitte@stacken.kth.se) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
59 #if !defined(MSDOS) && !defined(VMS) && !defined(WIN32)
60 #include <openssl/opensslconf.h>
62 # include OPENSSL_UNISTD
66 /* If unistd.h defines _POSIX_VERSION, we conclude that we
67 * are on a POSIX system and have sigaction and termios. */
68 #if defined(_POSIX_VERSION)
71 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
78 /* #define SIGACTION */ /* Define this if you have sigaction() */
86 /* 06-Apr-92 Luke Brennan Support for VMS */
95 #ifdef VMS /* prototypes for sys$whatever */
98 #pragma message disable DOLLARID
102 #ifdef WIN_CONSOLE_BUG
108 /* There are 5 types of terminal interface supported,
109 * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
112 #if defined(__sgi) && !defined(TERMIOS)
118 #if defined(linux) && !defined(TERMIO)
130 #if !defined(TERMIO) && !defined(TERMIOS) && !defined(VMS) && !defined(MSDOS) && !defined(MAC_OS_pre_X) && !defined(MAC_OS_GUSI_SOURCE)
138 #define TTY_STRUCT struct termios
139 #define TTY_FLAGS c_lflag
140 #define TTY_get(tty,data) tcgetattr(tty,data)
141 #define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
146 #define TTY_STRUCT struct termio
147 #define TTY_FLAGS c_lflag
148 #define TTY_get(tty,data) ioctl(tty,TCGETA,data)
149 #define TTY_set(tty,data) ioctl(tty,TCSETA,data)
154 #define TTY_STRUCT struct sgttyb
155 #define TTY_FLAGS sg_flags
156 #define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
157 #define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
160 #if !defined(_LIBC) && !defined(MSDOS) && !defined(VMS) && !defined(MAC_OS_pre_X)
161 #include <sys/ioctl.h>
166 #define fgets(a,b,c) noecho_fgets(a,b,c)
181 #if defined(MAC_OS_pre_X) || defined(MAC_OS_GUSI_SOURCE)
183 * This one needs work. As a matter of fact the code is unoperational
184 * and this is only a trick to get it compiled.
185 * <appro@fy.chalmers.se>
187 #define TTY_STRUCT int
195 /* Define globals. They are protected by a lock */
197 static struct sigaction savsig[NX509_SIG];
199 static void (*savsig[NX509_SIG])(int );
204 static struct IOSB iosb;
205 static $DESCRIPTOR(terminal,"TT");
206 static long tty_orig[3], tty_new[3];
208 static unsigned short channel = 0;
211 static TTY_STRUCT tty_orig,tty_new;
217 /* Declare static functions */
218 static void read_till_nl(FILE *);
219 static void recsig(int);
220 static void pushsig(void);
221 static void popsig(void);
222 #if defined(MSDOS) && !defined(WIN16)
223 static int noecho_fgets(char *buf, int size, FILE *tty);
225 static int read_string_inner(UI *ui, UI_STRING *uis, int echo);
227 static int read_string(UI *ui, UI_STRING *uis);
229 static int open_console(UI *ui);
230 static int echo_console(UI *ui);
231 static int noecho_console(UI *ui);
232 static int close_console(UI *ui);
234 static UI_METHOD ui_openssl =
236 "OpenSSL default user interface",
239 NULL, /* The reader function writes as well */
243 /* The method with all the built-in thingies */
244 UI_METHOD *UI_OpenSSL(void)
249 static int read_string(UI *ui, UI_STRING *uis)
251 switch (UI_get_string_type(uis))
253 case UI_VERIFY_NOECHO:
254 fprintf(tty,"Verifying - %s",
255 UI_get0_output_string(uis));
257 if (read_string_inner(ui, uis, 0) == 0)
259 if (strcmp(UI_get0_result_string(uis),
260 UI_get0_test_string(uis)) != 0)
262 fprintf(tty,"Verify failure\n");
268 fprintf(tty,"Verifying - %s",
269 UI_get0_output_string(uis));
271 if (read_string_inner(ui, uis, 1) == 0)
273 if (strcmp(UI_get0_result_string(uis),
274 UI_get0_test_string(uis)) != 0)
276 fprintf(tty,"Verify failure\n");
281 case UI_STRING_NOECHO:
282 fputs(UI_get0_output_string(uis), tty);
284 return read_string_inner(ui, uis, 0);
286 fputs(UI_get0_output_string(uis), tty);
288 return read_string_inner(ui, uis, 1);
290 fputs(UI_get0_output_string(uis), tty);
298 /* Internal functions to read a string without echoing */
299 static void read_till_nl(FILE *in)
306 } while (strchr(buf,'\n') == NULL);
309 static int read_string_inner(UI *ui, UI_STRING *uis, int echo)
313 char *result = OPENSSL_malloc(BUFSIZ);
314 int maxsize = BUFSIZ-1;
328 if (!echo) noecho_console(ui);
336 fgets(result,maxsize,tty);
337 if (feof(tty)) goto error;
338 if (ferror(tty)) goto error;
339 if ((p=(char *)strchr(result,'\n')) != NULL)
341 else read_till_nl(tty);
342 if (UI_set_result(uis, result) >= 0)
347 if (!echo) fprintf(tty,"\n");
348 if (ps >= 2 && !echo)
354 memset(result,0,BUFSIZ);
358 OPENSSL_free(result);
363 /* Internal functions to open, handle and close a channel to the console. */
364 static int open_console(UI *ui)
366 CRYPTO_w_lock(CRYPTO_LOCK_UI);
370 if ((tty=fopen("con","w+")) == NULL)
372 #elif defined(MAC_OS_pre_X)
375 if ((tty=fopen("/dev/tty","w+")) == NULL)
379 #if defined(TTY_get) && !defined(VMS)
380 if (TTY_get(fileno(tty),&tty_orig) == -1)
388 /* Ariel Glenn ariel@columbia.edu reports that solaris
389 * can return EINVAL instead. This should be ok */
398 status = sys$assign(&terminal,&channel,0,0);
399 if (status != SS$_NORMAL)
401 status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
402 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
408 static int noecho_console(UI *ui)
411 memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
412 tty_new.TTY_FLAGS &= ~ECHO;
415 #if defined(TTY_set) && !defined(VMS)
416 if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
420 tty_new[0] = tty_orig[0];
421 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
422 tty_new[2] = tty_orig[2];
423 status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
424 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
430 static int echo_console(UI *ui)
432 #if defined(TTY_set) && !defined(VMS)
433 memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
434 tty_new.TTY_FLAGS |= ECHO;
437 #if defined(TTY_set) && !defined(VMS)
438 if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
442 tty_new[0] = tty_orig[0];
443 tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
444 tty_new[2] = tty_orig[2];
445 status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
446 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
452 static int close_console(UI *ui)
454 if (stdin != tty) fclose(tty);
456 status = sys$dassgn(channel);
458 CRYPTO_w_unlock(CRYPTO_LOCK_UI);
464 /* Internal functions to handle signals and act on them */
465 static void pushsig(void)
471 memset(&sa,0,sizeof sa);
472 sa.sa_handler=recsig;
475 for (i=1; i<NX509_SIG; i++)
486 if (i == SIGKILL) /* We can't make any action on that. */
490 sigaction(i,&sa,&savsig[i]);
492 savsig[i]=signal(i,recsig);
497 signal(SIGWINCH,SIG_DFL);
501 static void popsig(void)
505 for (i=1; i<NX509_SIG; i++)
516 sigaction(i,&savsig[i],NULL);
523 static void recsig(int i)
532 /* Internal functions specific for Windows */
533 #if defined(MSDOS) && !defined(WIN16)
534 static int noecho_fgets(char *buf, int size, FILE *tty)
553 if (i == '\r') i='\n';
561 #ifdef WIN_CONSOLE_BUG
562 /* Win95 has several evil console bugs: one of these is that the
563 * last character read using getch() is passed to the next read: this is
564 * usually a CR so this can be trouble. No STDIO fix seems to work but
565 * flushing the console appears to do the trick.
569 inh = GetStdHandle(STD_INPUT_HANDLE);
570 FlushConsoleInputBuffer(inh);