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