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