Zero stack variable with DSA nonce
[oweals/openssl.git] / crypto / ui / ui_openssl.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/e_os2.h>
11
12 /*
13  * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
14  * [maybe others?], because it masks interfaces not discussed in standard,
15  * sigaction and fileno included. -pedantic would be more appropriate for the
16  * intended purposes, but we can't prevent users from adding -ansi.
17  */
18 #if defined(OPENSSL_SYS_VXWORKS)
19 # include <sys/types.h>
20 #endif
21
22 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
23 # ifndef _POSIX_C_SOURCE
24 #  define _POSIX_C_SOURCE 2
25 # endif
26 #endif
27 #include <signal.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
33 # ifdef OPENSSL_UNISTD
34 #  include OPENSSL_UNISTD
35 # else
36 #  include <unistd.h>
37 # endif
38 /*
39  * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
40  * system and have sigaction and termios.
41  */
42 # if defined(_POSIX_VERSION)
43
44 #  define SIGACTION
45 #  if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
46 #   define TERMIOS
47 #  endif
48
49 # endif
50 #endif
51
52 /* 06-Apr-92 Luke Brennan    Support for VMS */
53 #include "ui_locl.h"
54 #include "internal/cryptlib.h"
55
56 #ifdef OPENSSL_SYS_VMS          /* prototypes for sys$whatever */
57 # include <starlet.h>
58 # ifdef __DECC
59 #  pragma message disable DOLLARID
60 # endif
61 #endif
62
63 #ifdef WIN_CONSOLE_BUG
64 # include <windows.h>
65 # ifndef OPENSSL_SYS_WINCE
66 #  include <wincon.h>
67 # endif
68 #endif
69
70 /*
71  * There are 6 types of terminal interface supported, TERMIO, TERMIOS, VMS,
72  * MSDOS, WIN32 Console and SGTTY.
73  *
74  * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
75  * remain respected.  Otherwise, we default to TERMIOS except for a few
76  * systems that require something different.
77  *
78  * Note: we do not use SGTTY unless it's defined by the configuration.  We
79  * may eventually opt to remove it's use entirely.
80  */
81
82 #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
83
84 # if defined(_LIBC)
85 #  undef  TERMIOS
86 #  define TERMIO
87 #  undef  SGTTY
88 /*
89  * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
90  */
91 # elif !defined(OPENSSL_SYS_VMS) \
92         && !defined(OPENSSL_SYS_MSDOS) \
93         && !defined(OPENSSL_SYS_VXWORKS)
94 #  define TERMIOS
95 #  undef  TERMIO
96 #  undef  SGTTY
97 # endif
98
99 #endif
100
101 #ifdef TERMIOS
102 # include <termios.h>
103 # define TTY_STRUCT             struct termios
104 # define TTY_FLAGS              c_lflag
105 # define TTY_get(tty,data)      tcgetattr(tty,data)
106 # define TTY_set(tty,data)      tcsetattr(tty,TCSANOW,data)
107 #endif
108
109 #ifdef TERMIO
110 # include <termio.h>
111 # define TTY_STRUCT             struct termio
112 # define TTY_FLAGS              c_lflag
113 # define TTY_get(tty,data)      ioctl(tty,TCGETA,data)
114 # define TTY_set(tty,data)      ioctl(tty,TCSETA,data)
115 #endif
116
117 #ifdef SGTTY
118 # include <sgtty.h>
119 # define TTY_STRUCT             struct sgttyb
120 # define TTY_FLAGS              sg_flags
121 # define TTY_get(tty,data)      ioctl(tty,TIOCGETP,data)
122 # define TTY_set(tty,data)      ioctl(tty,TIOCSETP,data)
123 #endif
124
125 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
126 # include <sys/ioctl.h>
127 #endif
128
129 #ifdef OPENSSL_SYS_MSDOS
130 # include <conio.h>
131 #endif
132
133 #ifdef OPENSSL_SYS_VMS
134 # include <ssdef.h>
135 # include <iodef.h>
136 # include <ttdef.h>
137 # include <descrip.h>
138 struct IOSB {
139     short iosb$w_value;
140     short iosb$w_count;
141     long iosb$l_info;
142 };
143 #endif
144
145 #ifndef NX509_SIG
146 # define NX509_SIG 32
147 #endif
148
149 /* Define globals.  They are protected by a lock */
150 #ifdef SIGACTION
151 static struct sigaction savsig[NX509_SIG];
152 #else
153 static void (*savsig[NX509_SIG]) (int);
154 #endif
155
156 #ifdef OPENSSL_SYS_VMS
157 static struct IOSB iosb;
158 static $DESCRIPTOR(terminal, "TT");
159 static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
160                                       * will always suffice for the actual
161                                       * structures? */
162 static long status;
163 static unsigned short channel = 0;
164 #elif defined(_WIN32) && !defined(_WIN32_WCE)
165 static DWORD tty_orig, tty_new;
166 #else
167 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
168 static TTY_STRUCT tty_orig, tty_new;
169 # endif
170 #endif
171 static FILE *tty_in, *tty_out;
172 static int is_a_tty;
173
174 /* Declare static functions */
175 #if !defined(OPENSSL_SYS_WINCE)
176 static int read_till_nl(FILE *);
177 static void recsig(int);
178 static void pushsig(void);
179 static void popsig(void);
180 #endif
181 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
182 static int noecho_fgets(char *buf, int size, FILE *tty);
183 #endif
184 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
185
186 static int read_string(UI *ui, UI_STRING *uis);
187 static int write_string(UI *ui, UI_STRING *uis);
188
189 static int open_console(UI *ui);
190 static int echo_console(UI *ui);
191 static int noecho_console(UI *ui);
192 static int close_console(UI *ui);
193
194 static UI_METHOD ui_openssl = {
195     "OpenSSL default user interface",
196     open_console,
197     write_string,
198     NULL,                       /* No flusher is needed for command lines */
199     read_string,
200     close_console,
201     NULL
202 };
203
204 /* The method with all the built-in thingies */
205 UI_METHOD *UI_OpenSSL(void)
206 {
207     return &ui_openssl;
208 }
209
210 /*
211  * The following function makes sure that info and error strings are printed
212  * before any prompt.
213  */
214 static int write_string(UI *ui, UI_STRING *uis)
215 {
216     switch (UI_get_string_type(uis)) {
217     case UIT_ERROR:
218     case UIT_INFO:
219         fputs(UI_get0_output_string(uis), tty_out);
220         fflush(tty_out);
221         break;
222     case UIT_NONE:
223     case UIT_PROMPT:
224     case UIT_VERIFY:
225     case UIT_BOOLEAN:
226         break;
227     }
228     return 1;
229 }
230
231 static int read_string(UI *ui, UI_STRING *uis)
232 {
233     int ok = 0;
234
235     switch (UI_get_string_type(uis)) {
236     case UIT_BOOLEAN:
237         fputs(UI_get0_output_string(uis), tty_out);
238         fputs(UI_get0_action_string(uis), tty_out);
239         fflush(tty_out);
240         return read_string_inner(ui, uis,
241                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
242                                  0);
243     case UIT_PROMPT:
244         fputs(UI_get0_output_string(uis), tty_out);
245         fflush(tty_out);
246         return read_string_inner(ui, uis,
247                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
248                                  1);
249     case UIT_VERIFY:
250         fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
251         fflush(tty_out);
252         if ((ok = read_string_inner(ui, uis,
253                                     UI_get_input_flags(uis) &
254                                     UI_INPUT_FLAG_ECHO, 1)) <= 0)
255             return ok;
256         if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
257             fprintf(tty_out, "Verify failure\n");
258             fflush(tty_out);
259             return 0;
260         }
261         break;
262     case UIT_NONE:
263     case UIT_INFO:
264     case UIT_ERROR:
265         break;
266     }
267     return 1;
268 }
269
270 #if !defined(OPENSSL_SYS_WINCE)
271 /* Internal functions to read a string without echoing */
272 static int read_till_nl(FILE *in)
273 {
274 # define SIZE 4
275     char buf[SIZE + 1];
276
277     do {
278         if (!fgets(buf, SIZE, in))
279             return 0;
280     } while (strchr(buf, '\n') == NULL);
281     return 1;
282 }
283
284 static volatile sig_atomic_t intr_signal;
285 #endif
286
287 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
288 {
289     static int ps;
290     int ok;
291     char result[BUFSIZ];
292     int maxsize = BUFSIZ - 1;
293 #if !defined(OPENSSL_SYS_WINCE)
294     char *p = NULL;
295     int echo_eol = !echo;
296
297     intr_signal = 0;
298     ok = 0;
299     ps = 0;
300
301     pushsig();
302     ps = 1;
303
304     if (!echo && !noecho_console(ui))
305         goto error;
306     ps = 2;
307
308     result[0] = '\0';
309 # if defined(_WIN32)
310     if (is_a_tty) {
311         DWORD numread;
312 #  if defined(CP_UTF8)
313         if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
314             WCHAR wresult[BUFSIZ];
315
316             if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
317                          wresult, maxsize, &numread, NULL)) {
318                 if (numread >= 2 &&
319                     wresult[numread-2] == L'\r' &&
320                     wresult[numread-1] == L'\n') {
321                     wresult[numread-2] = L'\n';
322                     numread--;
323                 }
324                 wresult[numread] = '\0';
325                 if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
326                                         result, sizeof(result), NULL, 0) > 0)
327                     p = result;
328
329                 OPENSSL_cleanse(wresult, sizeof(wresult));
330             }
331         } else
332 #  endif
333         if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
334                          result, maxsize, &numread, NULL)) {
335             if (numread >= 2 &&
336                 result[numread-2] == '\r' && result[numread-1] == '\n') {
337                 result[numread-2] = '\n';
338                 numread--;
339             }
340             result[numread] = '\0';
341             p = result;
342         }
343     } else
344 # elif defined(OPENSSL_SYS_MSDOS)
345     if (!echo) {
346         noecho_fgets(result, maxsize, tty_in);
347         p = result;             /* FIXME: noecho_fgets doesn't return errors */
348     } else
349 # endif
350     p = fgets(result, maxsize, tty_in);
351     if (!p)
352         goto error;
353     if (feof(tty_in))
354         goto error;
355     if (ferror(tty_in))
356         goto error;
357     if ((p = (char *)strchr(result, '\n')) != NULL) {
358         if (strip_nl)
359             *p = '\0';
360     } else if (!read_till_nl(tty_in))
361         goto error;
362     if (UI_set_result(ui, uis, result) >= 0)
363         ok = 1;
364
365  error:
366     if (intr_signal == SIGINT)
367         ok = -1;
368     if (echo_eol)
369         fprintf(tty_out, "\n");
370     if (ps >= 2 && !echo && !echo_console(ui))
371         ok = 0;
372
373     if (ps >= 1)
374         popsig();
375 #else
376     ok = 1;
377 #endif
378
379     OPENSSL_cleanse(result, BUFSIZ);
380     return ok;
381 }
382
383 /* Internal functions to open, handle and close a channel to the console.  */
384 static int open_console(UI *ui)
385 {
386     CRYPTO_THREAD_write_lock(ui->lock);
387     is_a_tty = 1;
388
389 #if defined(OPENSSL_SYS_VXWORKS)
390     tty_in = stdin;
391     tty_out = stderr;
392 #elif defined(_WIN32) && !defined(_WIN32_WCE)
393     if ((tty_out = fopen("conout$", "w")) == NULL)
394         tty_out = stderr;
395
396     if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) {
397         tty_in = stdin;
398     } else {
399         is_a_tty = 0;
400         if ((tty_in = fopen("conin$", "r")) == NULL)
401             tty_in = stdin;
402     }
403 #else
404 # ifdef OPENSSL_SYS_MSDOS
405 #  define DEV_TTY "con"
406 # else
407 #  define DEV_TTY "/dev/tty"
408 # endif
409     if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
410         tty_in = stdin;
411     if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
412         tty_out = stderr;
413 #endif
414
415 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
416     if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
417 # ifdef ENOTTY
418         if (errno == ENOTTY)
419             is_a_tty = 0;
420         else
421 # endif
422 # ifdef EINVAL
423             /*
424              * Ariel Glenn ariel@columbia.edu reports that solaris can return
425              * EINVAL instead.  This should be ok
426              */
427         if (errno == EINVAL)
428             is_a_tty = 0;
429         else
430 # endif
431             return 0;
432     }
433 #endif
434 #ifdef OPENSSL_SYS_VMS
435     status = sys$assign(&terminal, &channel, 0, 0);
436     if (status != SS$_NORMAL)
437         return 0;
438     status =
439         sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
440                  0, 0);
441     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
442         return 0;
443 #endif
444     return 1;
445 }
446
447 static int noecho_console(UI *ui)
448 {
449 #ifdef TTY_FLAGS
450     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
451     tty_new.TTY_FLAGS &= ~ECHO;
452 #endif
453
454 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
455     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
456         return 0;
457 #endif
458 #ifdef OPENSSL_SYS_VMS
459     tty_new[0] = tty_orig[0];
460     tty_new[1] = tty_orig[1] | TT$M_NOECHO;
461     tty_new[2] = tty_orig[2];
462     status =
463         sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
464                  0);
465     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
466         return 0;
467 #endif
468 #if defined(_WIN32) && !defined(_WIN32_WCE)
469     if (is_a_tty) {
470         tty_new = tty_orig;
471         tty_new &= ~ENABLE_ECHO_INPUT;
472         SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
473     }
474 #endif
475     return 1;
476 }
477
478 static int echo_console(UI *ui)
479 {
480 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
481     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
482     tty_new.TTY_FLAGS |= ECHO;
483 #endif
484
485 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
486     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
487         return 0;
488 #endif
489 #ifdef OPENSSL_SYS_VMS
490     tty_new[0] = tty_orig[0];
491     tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
492     tty_new[2] = tty_orig[2];
493     status =
494         sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
495                  0);
496     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
497         return 0;
498 #endif
499 #if defined(_WIN32) && !defined(_WIN32_WCE)
500     if (is_a_tty) {
501         tty_new = tty_orig;
502         tty_new |= ENABLE_ECHO_INPUT;
503         SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
504     }
505 #endif
506     return 1;
507 }
508
509 static int close_console(UI *ui)
510 {
511     if (tty_in != stdin)
512         fclose(tty_in);
513     if (tty_out != stderr)
514         fclose(tty_out);
515 #ifdef OPENSSL_SYS_VMS
516     status = sys$dassgn(channel);
517 #endif
518     CRYPTO_THREAD_unlock(ui->lock);
519
520     return 1;
521 }
522
523 #if !defined(OPENSSL_SYS_WINCE)
524 /* Internal functions to handle signals and act on them */
525 static void pushsig(void)
526 {
527 # ifndef OPENSSL_SYS_WIN32
528     int i;
529 # endif
530 # ifdef SIGACTION
531     struct sigaction sa;
532
533     memset(&sa, 0, sizeof(sa));
534     sa.sa_handler = recsig;
535 # endif
536
537 # ifdef OPENSSL_SYS_WIN32
538     savsig[SIGABRT] = signal(SIGABRT, recsig);
539     savsig[SIGFPE] = signal(SIGFPE, recsig);
540     savsig[SIGILL] = signal(SIGILL, recsig);
541     savsig[SIGINT] = signal(SIGINT, recsig);
542     savsig[SIGSEGV] = signal(SIGSEGV, recsig);
543     savsig[SIGTERM] = signal(SIGTERM, recsig);
544 # else
545     for (i = 1; i < NX509_SIG; i++) {
546 #  ifdef SIGUSR1
547         if (i == SIGUSR1)
548             continue;
549 #  endif
550 #  ifdef SIGUSR2
551         if (i == SIGUSR2)
552             continue;
553 #  endif
554 #  ifdef SIGKILL
555         if (i == SIGKILL)       /* We can't make any action on that. */
556             continue;
557 #  endif
558 #  ifdef SIGACTION
559         sigaction(i, &sa, &savsig[i]);
560 #  else
561         savsig[i] = signal(i, recsig);
562 #  endif
563     }
564 # endif
565
566 # ifdef SIGWINCH
567     signal(SIGWINCH, SIG_DFL);
568 # endif
569 }
570
571 static void popsig(void)
572 {
573 # ifdef OPENSSL_SYS_WIN32
574     signal(SIGABRT, savsig[SIGABRT]);
575     signal(SIGFPE, savsig[SIGFPE]);
576     signal(SIGILL, savsig[SIGILL]);
577     signal(SIGINT, savsig[SIGINT]);
578     signal(SIGSEGV, savsig[SIGSEGV]);
579     signal(SIGTERM, savsig[SIGTERM]);
580 # else
581     int i;
582     for (i = 1; i < NX509_SIG; i++) {
583 #  ifdef SIGUSR1
584         if (i == SIGUSR1)
585             continue;
586 #  endif
587 #  ifdef SIGUSR2
588         if (i == SIGUSR2)
589             continue;
590 #  endif
591 #  ifdef SIGACTION
592         sigaction(i, &savsig[i], NULL);
593 #  else
594         signal(i, savsig[i]);
595 #  endif
596     }
597 # endif
598 }
599
600 static void recsig(int i)
601 {
602     intr_signal = i;
603 }
604 #endif
605
606 /* Internal functions specific for Windows */
607 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
608 static int noecho_fgets(char *buf, int size, FILE *tty)
609 {
610     int i;
611     char *p;
612
613     p = buf;
614     for (;;) {
615         if (size == 0) {
616             *p = '\0';
617             break;
618         }
619         size--;
620 # if defined(_WIN32)
621         i = _getch();
622 # else
623         i = getch();
624 # endif
625         if (i == '\r')
626             i = '\n';
627         *(p++) = i;
628         if (i == '\n') {
629             *p = '\0';
630             break;
631         }
632     }
633 # ifdef WIN_CONSOLE_BUG
634     /*
635      * Win95 has several evil console bugs: one of these is that the last
636      * character read using getch() is passed to the next read: this is
637      * usually a CR so this can be trouble. No STDIO fix seems to work but
638      * flushing the console appears to do the trick.
639      */
640     {
641         HANDLE inh;
642         inh = GetStdHandle(STD_INPUT_HANDLE);
643         FlushConsoleInputBuffer(inh);
644     }
645 # endif
646     return (strlen(buf));
647 }
648 #endif