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 /* 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.
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.
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).
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.
81 * Redistribution and use in source and binary forms, with or without
82 * modification, are permitted provided that the following conditions
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)"
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
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.]
118 #include <openssl/e_os2.h>
120 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
121 # ifdef OPENSSL_UNISTD
122 # include OPENSSL_UNISTD
126 /* If unistd.h defines _POSIX_VERSION, we conclude that we
127 * are on a POSIX system and have sigaction and termios. */
128 # if defined(_POSIX_VERSION)
131 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
139 # undef OPENSSL_SYS_WIN16
145 /* 06-Apr-92 Luke Brennan Support for VMS */
147 #include "cryptlib.h"
154 #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
155 # include <starlet.h>
157 # pragma message disable DOLLARID
161 #ifdef WIN_CONSOLE_BUG
162 # include <windows.h>
167 /* There are 5 types of terminal interface supported,
168 * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
171 #if defined(__sgi) && !defined(TERMIOS)
177 #if defined(linux) && !defined(TERMIO)
189 #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(MAC_OS_GUSI_SOURCE)
196 # include <termios.h>
197 # define TTY_STRUCT struct termios
198 # define TTY_FLAGS c_lflag
199 # define TTY_get(tty,data) tcgetattr(tty,data)
200 # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
205 # define TTY_STRUCT struct termio
206 # define TTY_FLAGS c_lflag
207 # define TTY_get(tty,data) ioctl(tty,TCGETA,data)
208 # define TTY_set(tty,data) ioctl(tty,TCSETA,data)
213 # define TTY_STRUCT struct sgttyb
214 # define TTY_FLAGS sg_flags
215 # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
216 # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
219 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
220 # include <sys/ioctl.h>
223 #ifdef OPENSSL_SYS_MSDOS
227 #ifdef OPENSSL_SYS_VMS
231 # include <descrip.h>
239 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE)
241 * This one needs work. As a matter of fact the code is unoperational
242 * and this is only a trick to get it compiled.
243 * <appro@fy.chalmers.se>
245 # define TTY_STRUCT int
249 # define NX509_SIG 32
253 /* Define globals. They are protected by a lock */
255 static struct sigaction savsig[NX509_SIG];
257 static void (*savsig[NX509_SIG])(int );
261 #ifdef OPENSSL_SYS_VMS
262 static struct IOSB iosb;
263 static $DESCRIPTOR(terminal,"TT");
264 static long tty_orig[3], tty_new[3];
266 static unsigned short channel = 0;
268 #ifndef OPENSSL_SYS_MSDOS
269 static TTY_STRUCT tty_orig,tty_new;
275 /* Declare static functions */
276 static void read_till_nl(FILE *);
277 static void recsig(int);
278 static void pushsig(void);
279 static void popsig(void);
280 #if defined(OENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
281 static int noecho_fgets(char *buf, int size, FILE *tty);
283 static int read_string_inner(UI *ui, UI_STRING *uis, int echo);
285 static int read_string(UI *ui, UI_STRING *uis);
287 static int open_console(UI *ui);
288 static int echo_console(UI *ui);
289 static int noecho_console(UI *ui);
290 static int close_console(UI *ui);
292 static UI_METHOD ui_openssl =
294 "OpenSSL default user interface",
297 NULL, /* The reader function writes as well */
301 /* The method with all the built-in thingies */
302 UI_METHOD *UI_OpenSSL(void)
307 static int read_string(UI *ui, UI_STRING *uis)
309 switch (UI_get_string_type(uis))
311 case UI_VERIFY_NOECHO:
312 fprintf(tty,"Verifying - %s",
313 UI_get0_output_string(uis));
315 if (read_string_inner(ui, uis, 0) == 0)
317 if (strcmp(UI_get0_result_string(uis),
318 UI_get0_test_string(uis)) != 0)
320 fprintf(tty,"Verify failure\n");
326 fprintf(tty,"Verifying - %s",
327 UI_get0_output_string(uis));
329 if (read_string_inner(ui, uis, 1) == 0)
331 if (strcmp(UI_get0_result_string(uis),
332 UI_get0_test_string(uis)) != 0)
334 fprintf(tty,"Verify failure\n");
339 case UI_STRING_NOECHO:
340 fputs(UI_get0_output_string(uis), tty);
342 return read_string_inner(ui, uis, 0);
344 fputs(UI_get0_output_string(uis), tty);
346 return read_string_inner(ui, uis, 1);
348 fputs(UI_get0_output_string(uis), tty);
356 /* Internal functions to read a string without echoing */
357 static void read_till_nl(FILE *in)
364 } while (strchr(buf,'\n') == NULL);
367 static int read_string_inner(UI *ui, UI_STRING *uis, int echo)
371 char *result = OPENSSL_malloc(BUFSIZ);
372 int maxsize = BUFSIZ-1;
374 #ifndef OPENSSL_SYS_WIN16
386 if (!echo) noecho_console(ui);
394 #ifdef OPENSSL_SYS_MSDOS
396 noecho_fgets(result,maxsize,tty);
399 fgets(result,maxsize,tty);
400 if (feof(tty)) goto error;
401 if (ferror(tty)) goto error;
402 if ((p=(char *)strchr(result,'\n')) != NULL)
404 else read_till_nl(tty);
405 if (UI_set_result(uis, result) >= 0)
410 if (!echo) fprintf(tty,"\n");
411 if (ps >= 2 && !echo)
417 memset(result,0,BUFSIZ);
421 OPENSSL_free(result);
426 /* Internal functions to open, handle and close a channel to the console. */
427 static int open_console(UI *ui)
429 CRYPTO_w_lock(CRYPTO_LOCK_UI);
432 #ifdef OPENSSL_SYS_MSDOS
433 if ((tty=fopen("con","w+")) == NULL)
435 #elif defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
438 if ((tty=fopen("/dev/tty","w+")) == NULL)
442 #if defined(TTY_get) && !defined(VMS)
443 if (TTY_get(fileno(tty),&tty_orig) == -1)
451 /* Ariel Glenn ariel@columbia.edu reports that solaris
452 * can return EINVAL instead. This should be ok */
460 #ifdef OPENSSL_SYS_VMS
461 status = sys$assign(&terminal,&channel,0,0);
462 if (status != SS$_NORMAL)
464 status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
465 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
471 static int noecho_console(UI *ui)
474 memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
475 tty_new.TTY_FLAGS &= ~ECHO;
478 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
479 if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
482 #ifdef OPENSSL_SYS_VMS
483 tty_new[0] = tty_orig[0];
484 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
485 tty_new[2] = tty_orig[2];
486 status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
487 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
493 static int echo_console(UI *ui)
495 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
496 memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
497 tty_new.TTY_FLAGS |= ECHO;
500 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
501 if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
504 #ifdef OPENSSL_SYS_VMS
505 tty_new[0] = tty_orig[0];
506 tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
507 tty_new[2] = tty_orig[2];
508 status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
509 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
515 static int close_console(UI *ui)
517 if (stdin != tty) fclose(tty);
518 #ifdef OPENSSL_SYS_VMS
519 status = sys$dassgn(channel);
521 CRYPTO_w_unlock(CRYPTO_LOCK_UI);
527 /* Internal functions to handle signals and act on them */
528 static void pushsig(void)
534 memset(&sa,0,sizeof sa);
535 sa.sa_handler=recsig;
538 for (i=1; i<NX509_SIG; i++)
549 if (i == SIGKILL) /* We can't make any action on that. */
553 sigaction(i,&sa,&savsig[i]);
555 savsig[i]=signal(i,recsig);
560 signal(SIGWINCH,SIG_DFL);
564 static void popsig(void)
568 for (i=1; i<NX509_SIG; i++)
579 sigaction(i,&savsig[i],NULL);
586 static void recsig(int i)
595 /* Internal functions specific for Windows */
596 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
597 static int noecho_fgets(char *buf, int size, FILE *tty)
616 if (i == '\r') i='\n';
624 #ifdef WIN_CONSOLE_BUG
625 /* Win95 has several evil console bugs: one of these is that the
626 * last character read using getch() is passed to the next read: this is
627 * usually a CR so this can be trouble. No STDIO fix seems to work but
628 * flushing the console appears to do the trick.
632 inh = GetStdHandle(STD_INPUT_HANDLE);
633 FlushConsoleInputBuffer(inh);