crypto/ui/ui_openssl.c: UTF-y Windows code path.
[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     default:
223         break;
224     }
225     return 1;
226 }
227
228 static int read_string(UI *ui, UI_STRING *uis)
229 {
230     int ok = 0;
231
232     switch (UI_get_string_type(uis)) {
233     case UIT_BOOLEAN:
234         fputs(UI_get0_output_string(uis), tty_out);
235         fputs(UI_get0_action_string(uis), tty_out);
236         fflush(tty_out);
237         return read_string_inner(ui, uis,
238                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
239                                  0);
240     case UIT_PROMPT:
241         fputs(UI_get0_output_string(uis), tty_out);
242         fflush(tty_out);
243         return read_string_inner(ui, uis,
244                                  UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
245                                  1);
246     case UIT_VERIFY:
247         fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
248         fflush(tty_out);
249         if ((ok = read_string_inner(ui, uis,
250                                     UI_get_input_flags(uis) &
251                                     UI_INPUT_FLAG_ECHO, 1)) <= 0)
252             return ok;
253         if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
254             fprintf(tty_out, "Verify failure\n");
255             fflush(tty_out);
256             return 0;
257         }
258         break;
259     default:
260         break;
261     }
262     return 1;
263 }
264
265 #if !defined(OPENSSL_SYS_WINCE)
266 /* Internal functions to read a string without echoing */
267 static int read_till_nl(FILE *in)
268 {
269 # define SIZE 4
270     char buf[SIZE + 1];
271
272     do {
273         if (!fgets(buf, SIZE, in))
274             return 0;
275     } while (strchr(buf, '\n') == NULL);
276     return 1;
277 }
278
279 static volatile sig_atomic_t intr_signal;
280 #endif
281
282 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
283 {
284     static int ps;
285     int ok;
286     char result[BUFSIZ];
287     int maxsize = BUFSIZ - 1;
288 #if !defined(OPENSSL_SYS_WINCE)
289     char *p = NULL;
290     int echo_eol = !echo;
291
292     intr_signal = 0;
293     ok = 0;
294     ps = 0;
295
296     pushsig();
297     ps = 1;
298
299     if (!echo && !noecho_console(ui))
300         goto error;
301     ps = 2;
302
303     result[0] = '\0';
304 # if defined(_WIN32)
305     if (is_a_tty) {
306         DWORD numread;
307 #  if defined(CP_UTF8)
308         WCHAR wresult[BUFSIZ];
309
310         if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
311                          wresult, maxsize, &numread, NULL)) {
312             if (numread >= 2 &&
313                 wresult[numread-2] == L'\r' && wresult[numread-1] == L'\n') {
314                 wresult[numread-2] = L'\n';
315                 numread--;
316                 echo_eol = 0;
317             }
318             wresult[numread] = '\0';
319             if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
320                                     result, sizeof(result), NULL, 0) > 0)
321                 p = result;
322
323             OPENSSL_cleanse(wresult, sizeof(wresult));
324         }
325 #  else
326         if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
327                          result, maxsize, &numread, NULL)) {
328             if (numread >= 2 &&
329                 result[numread-2] == '\r' && result[numread-1] == '\n') {
330                 result[numread-2] = '\n';
331                 numread--;
332                 echo_eol = 0;
333             }
334             result[numread] = '\0';
335             p = result;
336         }
337 #  endif
338     } else
339 # elif defined(OPENSSL_SYS_MSDOS)
340     if (!echo) {
341         noecho_fgets(result, maxsize, tty_in);
342         p = result;             /* FIXME: noecho_fgets doesn't return errors */
343     } else
344 # endif
345     p = fgets(result, maxsize, tty_in);
346     if (!p)
347         goto error;
348     if (feof(tty_in))
349         goto error;
350     if (ferror(tty_in))
351         goto error;
352     if ((p = (char *)strchr(result, '\n')) != NULL) {
353         if (strip_nl)
354             *p = '\0';
355     } else if (!read_till_nl(tty_in))
356         goto error;
357     if (UI_set_result(ui, uis, result) >= 0)
358         ok = 1;
359
360  error:
361     if (intr_signal == SIGINT)
362         ok = -1;
363     if (echo_eol)
364         fprintf(tty_out, "\n");
365     if (ps >= 2 && !echo && !echo_console(ui))
366         ok = 0;
367
368     if (ps >= 1)
369         popsig();
370 #else
371     ok = 1;
372 #endif
373
374     OPENSSL_cleanse(result, BUFSIZ);
375     return ok;
376 }
377
378 /* Internal functions to open, handle and close a channel to the console.  */
379 static int open_console(UI *ui)
380 {
381     CRYPTO_THREAD_write_lock(ui->lock);
382     is_a_tty = 1;
383
384 #if defined(OPENSSL_SYS_VXWORKS)
385     tty_in = stdin;
386     tty_out = stderr;
387 #elif defined(_WIN32) && !defined(_WIN32_WCE)
388     if ((tty_out = fopen("conout$", "w")) == NULL)
389         tty_out = stderr;
390
391     if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) {
392         tty_in = stdin;
393     } else {
394         is_a_tty = 0;
395         if ((tty_in = fopen("conin$", "r")) == NULL)
396             tty_in = stdin;
397     }
398 #else
399 # ifdef OPENSSL_SYS_MSDOS
400 #  define DEV_TTY "con"
401 # else
402 #  define DEV_TTY "/dev/tty"
403 # endif
404     if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
405         tty_in = stdin;
406     if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
407         tty_out = stderr;
408 #endif
409
410 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
411     if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
412 # ifdef ENOTTY
413         if (errno == ENOTTY)
414             is_a_tty = 0;
415         else
416 # endif
417 # ifdef EINVAL
418             /*
419              * Ariel Glenn ariel@columbia.edu reports that solaris can return
420              * EINVAL instead.  This should be ok
421              */
422         if (errno == EINVAL)
423             is_a_tty = 0;
424         else
425 # endif
426             return 0;
427     }
428 #endif
429 #ifdef OPENSSL_SYS_VMS
430     status = sys$assign(&terminal, &channel, 0, 0);
431     if (status != SS$_NORMAL)
432         return 0;
433     status =
434         sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
435                  0, 0);
436     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
437         return 0;
438 #endif
439     return 1;
440 }
441
442 static int noecho_console(UI *ui)
443 {
444 #ifdef TTY_FLAGS
445     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
446     tty_new.TTY_FLAGS &= ~ECHO;
447 #endif
448
449 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
450     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
451         return 0;
452 #endif
453 #ifdef OPENSSL_SYS_VMS
454     tty_new[0] = tty_orig[0];
455     tty_new[1] = tty_orig[1] | TT$M_NOECHO;
456     tty_new[2] = tty_orig[2];
457     status =
458         sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
459                  0);
460     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
461         return 0;
462 #endif
463 #if defined(_WIN32) && !defined(_WIN32_WCE)
464     if (is_a_tty) {
465         tty_new = tty_orig;
466         tty_new &= ~ENABLE_ECHO_INPUT;
467         SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
468     }
469 #endif
470     return 1;
471 }
472
473 static int echo_console(UI *ui)
474 {
475 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
476     memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
477     tty_new.TTY_FLAGS |= ECHO;
478 #endif
479
480 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
481     if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
482         return 0;
483 #endif
484 #ifdef OPENSSL_SYS_VMS
485     tty_new[0] = tty_orig[0];
486     tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
487     tty_new[2] = tty_orig[2];
488     status =
489         sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
490                  0);
491     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
492         return 0;
493 #endif
494 #if defined(_WIN32) && !defined(_WIN32_WCE)
495     if (is_a_tty) {
496         tty_new = tty_orig;
497         tty_new |= ENABLE_ECHO_INPUT;
498         SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
499     }
500 #endif
501     return 1;
502 }
503
504 static int close_console(UI *ui)
505 {
506     if (tty_in != stdin)
507         fclose(tty_in);
508     if (tty_out != stderr)
509         fclose(tty_out);
510 #ifdef OPENSSL_SYS_VMS
511     status = sys$dassgn(channel);
512 #endif
513     CRYPTO_THREAD_unlock(ui->lock);
514
515     return 1;
516 }
517
518 #if !defined(OPENSSL_SYS_WINCE)
519 /* Internal functions to handle signals and act on them */
520 static void pushsig(void)
521 {
522 # ifndef OPENSSL_SYS_WIN32
523     int i;
524 # endif
525 # ifdef SIGACTION
526     struct sigaction sa;
527
528     memset(&sa, 0, sizeof(sa));
529     sa.sa_handler = recsig;
530 # endif
531
532 # ifdef OPENSSL_SYS_WIN32
533     savsig[SIGABRT] = signal(SIGABRT, recsig);
534     savsig[SIGFPE] = signal(SIGFPE, recsig);
535     savsig[SIGILL] = signal(SIGILL, recsig);
536     savsig[SIGINT] = signal(SIGINT, recsig);
537     savsig[SIGSEGV] = signal(SIGSEGV, recsig);
538     savsig[SIGTERM] = signal(SIGTERM, recsig);
539 # else
540     for (i = 1; i < NX509_SIG; i++) {
541 #  ifdef SIGUSR1
542         if (i == SIGUSR1)
543             continue;
544 #  endif
545 #  ifdef SIGUSR2
546         if (i == SIGUSR2)
547             continue;
548 #  endif
549 #  ifdef SIGKILL
550         if (i == SIGKILL)       /* We can't make any action on that. */
551             continue;
552 #  endif
553 #  ifdef SIGACTION
554         sigaction(i, &sa, &savsig[i]);
555 #  else
556         savsig[i] = signal(i, recsig);
557 #  endif
558     }
559 # endif
560
561 # ifdef SIGWINCH
562     signal(SIGWINCH, SIG_DFL);
563 # endif
564 }
565
566 static void popsig(void)
567 {
568 # ifdef OPENSSL_SYS_WIN32
569     signal(SIGABRT, savsig[SIGABRT]);
570     signal(SIGFPE, savsig[SIGFPE]);
571     signal(SIGILL, savsig[SIGILL]);
572     signal(SIGINT, savsig[SIGINT]);
573     signal(SIGSEGV, savsig[SIGSEGV]);
574     signal(SIGTERM, savsig[SIGTERM]);
575 # else
576     int i;
577     for (i = 1; i < NX509_SIG; i++) {
578 #  ifdef SIGUSR1
579         if (i == SIGUSR1)
580             continue;
581 #  endif
582 #  ifdef SIGUSR2
583         if (i == SIGUSR2)
584             continue;
585 #  endif
586 #  ifdef SIGACTION
587         sigaction(i, &savsig[i], NULL);
588 #  else
589         signal(i, savsig[i]);
590 #  endif
591     }
592 # endif
593 }
594
595 static void recsig(int i)
596 {
597     intr_signal = i;
598 }
599 #endif
600
601 /* Internal functions specific for Windows */
602 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
603 static int noecho_fgets(char *buf, int size, FILE *tty)
604 {
605     int i;
606     char *p;
607
608     p = buf;
609     for (;;) {
610         if (size == 0) {
611             *p = '\0';
612             break;
613         }
614         size--;
615 # if defined(_WIN32)
616         i = _getch();
617 # else
618         i = getch();
619 # endif
620         if (i == '\r')
621             i = '\n';
622         *(p++) = i;
623         if (i == '\n') {
624             *p = '\0';
625             break;
626         }
627     }
628 # ifdef WIN_CONSOLE_BUG
629     /*
630      * Win95 has several evil console bugs: one of these is that the last
631      * character read using getch() is passed to the next read: this is
632      * usually a CR so this can be trouble. No STDIO fix seems to work but
633      * flushing the console appears to do the trick.
634      */
635     {
636         HANDLE inh;
637         inh = GetStdHandle(STD_INPUT_HANDLE);
638         FlushConsoleInputBuffer(inh);
639     }
640 # endif
641     return (strlen(buf));
642 }
643 #endif