ui_openssl.c 19 KB

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