blob: 06270f09cd425b910790bdebbd914772935ed896 [file] [log] [blame]
The Android Open Source Project656d9c72009-03-03 19:30:25 -08001/* 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
118#include <openssl/e_os2.h>
119
120/* need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
121 * [maybe others?], because it masks interfaces not discussed in standard,
122 * sigaction and fileno included. -pedantic would be more appropriate for
123 * the intended purposes, but we can't prevent users from adding -ansi.
124 */
125#define _POSIX_C_SOURCE 1
126#include <signal.h>
127#include <stdio.h>
128#include <string.h>
129#include <errno.h>
130
131#if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
132# ifdef OPENSSL_UNISTD
133# include OPENSSL_UNISTD
134# else
135# include <unistd.h>
136# endif
137/* If unistd.h defines _POSIX_VERSION, we conclude that we
138 * are on a POSIX system and have sigaction and termios. */
139# if defined(_POSIX_VERSION)
140
141# define SIGACTION
142# if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
143# define TERMIOS
144# endif
145
146# endif
147#endif
148
149#ifdef WIN16TTY
150# undef OPENSSL_SYS_WIN16
151# undef WIN16
152# undef _WINDOWS
153# include <graph.h>
154#endif
155
156/* 06-Apr-92 Luke Brennan Support for VMS */
157#include "ui_locl.h"
158#include "cryptlib.h"
159
160#ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
161# include <starlet.h>
162# ifdef __DECC
163# pragma message disable DOLLARID
164# endif
165#endif
166
167#ifdef WIN_CONSOLE_BUG
168# include <windows.h>
169#ifndef OPENSSL_SYS_WINCE
170# include <wincon.h>
171#endif
172#endif
173
174
175/* There are 5 types of terminal interface supported,
176 * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
177 */
178
179#if defined(__sgi) && !defined(TERMIOS)
180# define TERMIOS
181# undef TERMIO
182# undef SGTTY
183#endif
184
185#if defined(linux) && !defined(TERMIO)
186# undef TERMIOS
187# define TERMIO
188# undef SGTTY
189#endif
190
191#ifdef _LIBC
192# undef TERMIOS
193# define TERMIO
194# undef SGTTY
195#endif
196
197#if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(MAC_OS_GUSI_SOURCE)
198# undef TERMIOS
199# undef TERMIO
200# define SGTTY
201#endif
202
203#if defined(OPENSSL_SYS_VXWORKS)
204#undef TERMIOS
205#undef TERMIO
206#undef SGTTY
207#endif
208
209#if defined(OPENSSL_SYS_NETWARE)
210#undef TERMIOS
211#undef TERMIO
212#undef SGTTY
213#endif
214
215#ifdef TERMIOS
216# include <termios.h>
217# define TTY_STRUCT struct termios
218# define TTY_FLAGS c_lflag
219# define TTY_get(tty,data) tcgetattr(tty,data)
220# define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
221#endif
222
223#ifdef TERMIO
224# include <termio.h>
225# define TTY_STRUCT struct termio
226# define TTY_FLAGS c_lflag
227# define TTY_get(tty,data) ioctl(tty,TCGETA,data)
228# define TTY_set(tty,data) ioctl(tty,TCSETA,data)
229#endif
230
231#ifdef SGTTY
232# include <sgtty.h>
233# define TTY_STRUCT struct sgttyb
234# define TTY_FLAGS sg_flags
235# define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
236# define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
237#endif
238
239#if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_SUNOS)
240# include <sys/ioctl.h>
241#endif
242
243#ifdef OPENSSL_SYS_MSDOS
244# include <conio.h>
245#endif
246
247#ifdef OPENSSL_SYS_VMS
248# include <ssdef.h>
249# include <iodef.h>
250# include <ttdef.h>
251# include <descrip.h>
252struct IOSB {
253 short iosb$w_value;
254 short iosb$w_count;
255 long iosb$l_info;
256 };
257#endif
258
259#ifdef OPENSSL_SYS_SUNOS
260 typedef int sig_atomic_t;
261#endif
262
263#if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE) || defined(OPENSSL_SYS_NETWARE)
264/*
265 * This one needs work. As a matter of fact the code is unoperational
266 * and this is only a trick to get it compiled.
267 * <appro@fy.chalmers.se>
268 */
269# define TTY_STRUCT int
270#endif
271
272#ifndef NX509_SIG
273# define NX509_SIG 32
274#endif
275
276
277/* Define globals. They are protected by a lock */
278#ifdef SIGACTION
279static struct sigaction savsig[NX509_SIG];
280#else
281static void (*savsig[NX509_SIG])(int );
282#endif
283
284#ifdef OPENSSL_SYS_VMS
285static struct IOSB iosb;
286static $DESCRIPTOR(terminal,"TT");
287static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this will always suffice for the actual structures? */
288static long status;
289static unsigned short channel = 0;
290#else
291#if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
292static TTY_STRUCT tty_orig,tty_new;
293#endif
294#endif
295static FILE *tty_in, *tty_out;
296static int is_a_tty;
297
298/* Declare static functions */
299#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
Brian Carlstrom98d58bb2010-03-09 09:56:55 -0800300static int read_till_nl(FILE *);
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800301static void recsig(int);
302static void pushsig(void);
303static void popsig(void);
304#endif
305#if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
306static int noecho_fgets(char *buf, int size, FILE *tty);
307#endif
308static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
309
310static int read_string(UI *ui, UI_STRING *uis);
311static int write_string(UI *ui, UI_STRING *uis);
312
313static int open_console(UI *ui);
314static int echo_console(UI *ui);
315static int noecho_console(UI *ui);
316static int close_console(UI *ui);
317
318static UI_METHOD ui_openssl =
319 {
320 "OpenSSL default user interface",
321 open_console,
322 write_string,
323 NULL, /* No flusher is needed for command lines */
324 read_string,
325 close_console,
326 NULL
327 };
328
329/* The method with all the built-in thingies */
330UI_METHOD *UI_OpenSSL(void)
331 {
332 return &ui_openssl;
333 }
334
335/* The following function makes sure that info and error strings are printed
336 before any prompt. */
337static int write_string(UI *ui, UI_STRING *uis)
338 {
339 switch (UI_get_string_type(uis))
340 {
341 case UIT_ERROR:
342 case UIT_INFO:
343 fputs(UI_get0_output_string(uis), tty_out);
344 fflush(tty_out);
345 break;
346 default:
347 break;
348 }
349 return 1;
350 }
351
352static int read_string(UI *ui, UI_STRING *uis)
353 {
354 int ok = 0;
355
356 switch (UI_get_string_type(uis))
357 {
358 case UIT_BOOLEAN:
359 fputs(UI_get0_output_string(uis), tty_out);
360 fputs(UI_get0_action_string(uis), tty_out);
361 fflush(tty_out);
362 return read_string_inner(ui, uis,
363 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0);
364 case UIT_PROMPT:
365 fputs(UI_get0_output_string(uis), tty_out);
366 fflush(tty_out);
367 return read_string_inner(ui, uis,
368 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1);
369 case UIT_VERIFY:
370 fprintf(tty_out,"Verifying - %s",
371 UI_get0_output_string(uis));
372 fflush(tty_out);
373 if ((ok = read_string_inner(ui, uis,
374 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1)) <= 0)
375 return ok;
376 if (strcmp(UI_get0_result_string(uis),
377 UI_get0_test_string(uis)) != 0)
378 {
379 fprintf(tty_out,"Verify failure\n");
380 fflush(tty_out);
381 return 0;
382 }
383 break;
384 default:
385 break;
386 }
387 return 1;
388 }
389
390
391#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
392/* Internal functions to read a string without echoing */
Brian Carlstrom98d58bb2010-03-09 09:56:55 -0800393static int read_till_nl(FILE *in)
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800394 {
395#define SIZE 4
396 char buf[SIZE+1];
397
398 do {
Brian Carlstrom98d58bb2010-03-09 09:56:55 -0800399 if (!fgets(buf,SIZE,in))
400 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800401 } while (strchr(buf,'\n') == NULL);
Brian Carlstrom98d58bb2010-03-09 09:56:55 -0800402 return 1;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800403 }
404
405static volatile sig_atomic_t intr_signal;
406#endif
407
408static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
409 {
410 static int ps;
411 int ok;
412 char result[BUFSIZ];
413 int maxsize = BUFSIZ-1;
414#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
415 char *p;
416
417 intr_signal=0;
418 ok=0;
419 ps=0;
420
421 pushsig();
422 ps=1;
423
424 if (!echo && !noecho_console(ui))
425 goto error;
426 ps=2;
427
428 result[0]='\0';
429#ifdef OPENSSL_SYS_MSDOS
430 if (!echo)
431 {
432 noecho_fgets(result,maxsize,tty_in);
433 p=result; /* FIXME: noecho_fgets doesn't return errors */
434 }
435 else
436 p=fgets(result,maxsize,tty_in);
437#else
438 p=fgets(result,maxsize,tty_in);
439#endif
440 if(!p)
441 goto error;
442 if (feof(tty_in)) goto error;
443 if (ferror(tty_in)) goto error;
444 if ((p=(char *)strchr(result,'\n')) != NULL)
445 {
446 if (strip_nl)
447 *p='\0';
448 }
449 else
Brian Carlstrom98d58bb2010-03-09 09:56:55 -0800450 if (!read_till_nl(tty_in))
451 goto error;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800452 if (UI_set_result(ui, uis, result) >= 0)
453 ok=1;
454
455error:
456 if (intr_signal == SIGINT)
457 ok=-1;
458 if (!echo) fprintf(tty_out,"\n");
459 if (ps >= 2 && !echo && !echo_console(ui))
460 ok=0;
461
462 if (ps >= 1)
463 popsig();
464#else
465 ok=1;
466#endif
467
468 OPENSSL_cleanse(result,BUFSIZ);
469 return ok;
470 }
471
472
473/* Internal functions to open, handle and close a channel to the console. */
474static int open_console(UI *ui)
475 {
476 CRYPTO_w_lock(CRYPTO_LOCK_UI);
477 is_a_tty = 1;
478
479#if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE)
480 tty_in=stdin;
481 tty_out=stderr;
482#else
483# ifdef OPENSSL_SYS_MSDOS
484# define DEV_TTY "con"
485# else
486# define DEV_TTY "/dev/tty"
487# endif
488 if ((tty_in=fopen(DEV_TTY,"r")) == NULL)
489 tty_in=stdin;
490 if ((tty_out=fopen(DEV_TTY,"w")) == NULL)
491 tty_out=stderr;
492#endif
493
494#if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
495 if (TTY_get(fileno(tty_in),&tty_orig) == -1)
496 {
497#ifdef ENOTTY
498 if (errno == ENOTTY)
499 is_a_tty=0;
500 else
501#endif
502#ifdef EINVAL
503 /* Ariel Glenn ariel@columbia.edu reports that solaris
504 * can return EINVAL instead. This should be ok */
505 if (errno == EINVAL)
506 is_a_tty=0;
507 else
508#endif
509 return 0;
510 }
511#endif
512#ifdef OPENSSL_SYS_VMS
513 status = sys$assign(&terminal,&channel,0,0);
514 if (status != SS$_NORMAL)
515 return 0;
516 status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
517 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
518 return 0;
519#endif
520 return 1;
521 }
522
523static int noecho_console(UI *ui)
524 {
525#ifdef TTY_FLAGS
526 memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
527 tty_new.TTY_FLAGS &= ~ECHO;
528#endif
529
530#if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
531 if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
532 return 0;
533#endif
534#ifdef OPENSSL_SYS_VMS
535 tty_new[0] = tty_orig[0];
536 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
537 tty_new[2] = tty_orig[2];
538 status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
539 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
540 return 0;
541#endif
542 return 1;
543 }
544
545static int echo_console(UI *ui)
546 {
547#if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
548 memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
549 tty_new.TTY_FLAGS |= ECHO;
550#endif
551
552#if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
553 if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
554 return 0;
555#endif
556#ifdef OPENSSL_SYS_VMS
557 tty_new[0] = tty_orig[0];
558 tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
559 tty_new[2] = tty_orig[2];
560 status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
561 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
562 return 0;
563#endif
564 return 1;
565 }
566
567static int close_console(UI *ui)
568 {
569 if (tty_in != stdin) fclose(tty_in);
570 if (tty_out != stderr) fclose(tty_out);
571#ifdef OPENSSL_SYS_VMS
572 status = sys$dassgn(channel);
573#endif
574 CRYPTO_w_unlock(CRYPTO_LOCK_UI);
575
576 return 1;
577 }
578
579
580#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
581/* Internal functions to handle signals and act on them */
582static void pushsig(void)
583 {
584#ifndef OPENSSL_SYS_WIN32
585 int i;
586#endif
587#ifdef SIGACTION
588 struct sigaction sa;
589
590 memset(&sa,0,sizeof sa);
591 sa.sa_handler=recsig;
592#endif
593
594#ifdef OPENSSL_SYS_WIN32
595 savsig[SIGABRT]=signal(SIGABRT,recsig);
596 savsig[SIGFPE]=signal(SIGFPE,recsig);
597 savsig[SIGILL]=signal(SIGILL,recsig);
598 savsig[SIGINT]=signal(SIGINT,recsig);
599 savsig[SIGSEGV]=signal(SIGSEGV,recsig);
600 savsig[SIGTERM]=signal(SIGTERM,recsig);
601#else
602 for (i=1; i<NX509_SIG; i++)
603 {
604#ifdef SIGUSR1
605 if (i == SIGUSR1)
606 continue;
607#endif
608#ifdef SIGUSR2
609 if (i == SIGUSR2)
610 continue;
611#endif
612#ifdef SIGKILL
613 if (i == SIGKILL) /* We can't make any action on that. */
614 continue;
615#endif
616#ifdef SIGACTION
617 sigaction(i,&sa,&savsig[i]);
618#else
619 savsig[i]=signal(i,recsig);
620#endif
621 }
622#endif
623
624#ifdef SIGWINCH
625 signal(SIGWINCH,SIG_DFL);
626#endif
627 }
628
629static void popsig(void)
630 {
631#ifdef OPENSSL_SYS_WIN32
632 signal(SIGABRT,savsig[SIGABRT]);
633 signal(SIGFPE,savsig[SIGFPE]);
634 signal(SIGILL,savsig[SIGILL]);
635 signal(SIGINT,savsig[SIGINT]);
636 signal(SIGSEGV,savsig[SIGSEGV]);
637 signal(SIGTERM,savsig[SIGTERM]);
638#else
639 int i;
640 for (i=1; i<NX509_SIG; i++)
641 {
642#ifdef SIGUSR1
643 if (i == SIGUSR1)
644 continue;
645#endif
646#ifdef SIGUSR2
647 if (i == SIGUSR2)
648 continue;
649#endif
650#ifdef SIGACTION
651 sigaction(i,&savsig[i],NULL);
652#else
653 signal(i,savsig[i]);
654#endif
655 }
656#endif
657 }
658
659static void recsig(int i)
660 {
661 intr_signal=i;
662 }
663#endif
664
665/* Internal functions specific for Windows */
666#if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
667static int noecho_fgets(char *buf, int size, FILE *tty)
668 {
669 int i;
670 char *p;
671
672 p=buf;
673 for (;;)
674 {
675 if (size == 0)
676 {
677 *p='\0';
678 break;
679 }
680 size--;
681#ifdef WIN16TTY
682 i=_inchar();
Nagendra Modadugue45f1062009-09-30 11:36:48 -0700683#elif defined(_WIN32)
684 i=_getch();
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800685#else
686 i=getch();
687#endif
688 if (i == '\r') i='\n';
689 *(p++)=i;
690 if (i == '\n')
691 {
692 *p='\0';
693 break;
694 }
695 }
696#ifdef WIN_CONSOLE_BUG
697/* Win95 has several evil console bugs: one of these is that the
698 * last character read using getch() is passed to the next read: this is
699 * usually a CR so this can be trouble. No STDIO fix seems to work but
700 * flushing the console appears to do the trick.
701 */
702 {
703 HANDLE inh;
704 inh = GetStdHandle(STD_INPUT_HANDLE);
705 FlushConsoleInputBuffer(inh);
706 }
707#endif
708 return(strlen(buf));
709 }
710#endif