1*cedac418Stb /* $OpenBSD: ui_openssl.c,v 1.28 2023/02/16 08:38:17 tb Exp $ */
2da347917Sbeck /* Written by Richard Levitte (richard@levitte.org) and others
3da347917Sbeck * for the OpenSSL project 2001.
4da347917Sbeck */
5da347917Sbeck /* ====================================================================
6da347917Sbeck * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
7da347917Sbeck *
8da347917Sbeck * Redistribution and use in source and binary forms, with or without
9da347917Sbeck * modification, are permitted provided that the following conditions
10da347917Sbeck * are met:
11da347917Sbeck *
12da347917Sbeck * 1. Redistributions of source code must retain the above copyright
13da347917Sbeck * notice, this list of conditions and the following disclaimer.
14da347917Sbeck *
15da347917Sbeck * 2. Redistributions in binary form must reproduce the above copyright
16da347917Sbeck * notice, this list of conditions and the following disclaimer in
17da347917Sbeck * the documentation and/or other materials provided with the
18da347917Sbeck * distribution.
19da347917Sbeck *
20da347917Sbeck * 3. All advertising materials mentioning features or use of this
21da347917Sbeck * software must display the following acknowledgment:
22da347917Sbeck * "This product includes software developed by the OpenSSL Project
23da347917Sbeck * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24da347917Sbeck *
25da347917Sbeck * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26da347917Sbeck * endorse or promote products derived from this software without
27da347917Sbeck * prior written permission. For written permission, please contact
28da347917Sbeck * openssl-core@openssl.org.
29da347917Sbeck *
30da347917Sbeck * 5. Products derived from this software may not be called "OpenSSL"
31da347917Sbeck * nor may "OpenSSL" appear in their names without prior written
32da347917Sbeck * permission of the OpenSSL Project.
33da347917Sbeck *
34da347917Sbeck * 6. Redistributions of any form whatsoever must retain the following
35da347917Sbeck * acknowledgment:
36da347917Sbeck * "This product includes software developed by the OpenSSL Project
37da347917Sbeck * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38da347917Sbeck *
39da347917Sbeck * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40da347917Sbeck * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41da347917Sbeck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42da347917Sbeck * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43da347917Sbeck * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44da347917Sbeck * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45da347917Sbeck * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46da347917Sbeck * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47da347917Sbeck * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48da347917Sbeck * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49da347917Sbeck * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50da347917Sbeck * OF THE POSSIBILITY OF SUCH DAMAGE.
51da347917Sbeck * ====================================================================
52da347917Sbeck *
53da347917Sbeck * This product includes cryptographic software written by Eric Young
54da347917Sbeck * (eay@cryptsoft.com). This product includes software written by Tim
55da347917Sbeck * Hudson (tjh@cryptsoft.com).
56da347917Sbeck *
57da347917Sbeck */
58da347917Sbeck
59da347917Sbeck /* The lowest level part of this file was previously in crypto/des/read_pwd.c,
60da347917Sbeck * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
61da347917Sbeck * All rights reserved.
62da347917Sbeck *
63da347917Sbeck * This package is an SSL implementation written
64da347917Sbeck * by Eric Young (eay@cryptsoft.com).
65da347917Sbeck * The implementation was written so as to conform with Netscapes SSL.
66da347917Sbeck *
67da347917Sbeck * This library is free for commercial and non-commercial use as long as
68da347917Sbeck * the following conditions are aheared to. The following conditions
69da347917Sbeck * apply to all code found in this distribution, be it the RC4, RSA,
70da347917Sbeck * lhash, DES, etc., code; not just the SSL code. The SSL documentation
71da347917Sbeck * included with this distribution is covered by the same copyright terms
72da347917Sbeck * except that the holder is Tim Hudson (tjh@cryptsoft.com).
73da347917Sbeck *
74da347917Sbeck * Copyright remains Eric Young's, and as such any Copyright notices in
75da347917Sbeck * the code are not to be removed.
76da347917Sbeck * If this package is used in a product, Eric Young should be given attribution
77da347917Sbeck * as the author of the parts of the library used.
78da347917Sbeck * This can be in the form of a textual message at program startup or
79da347917Sbeck * in documentation (online or textual) provided with the package.
80da347917Sbeck *
81da347917Sbeck * Redistribution and use in source and binary forms, with or without
82da347917Sbeck * modification, are permitted provided that the following conditions
83da347917Sbeck * are met:
84da347917Sbeck * 1. Redistributions of source code must retain the copyright
85da347917Sbeck * notice, this list of conditions and the following disclaimer.
86da347917Sbeck * 2. Redistributions in binary form must reproduce the above copyright
87da347917Sbeck * notice, this list of conditions and the following disclaimer in the
88da347917Sbeck * documentation and/or other materials provided with the distribution.
89da347917Sbeck * 3. All advertising materials mentioning features or use of this software
90da347917Sbeck * must display the following acknowledgement:
91da347917Sbeck * "This product includes cryptographic software written by
92da347917Sbeck * Eric Young (eay@cryptsoft.com)"
93da347917Sbeck * The word 'cryptographic' can be left out if the rouines from the library
94da347917Sbeck * being used are not cryptographic related :-).
95da347917Sbeck * 4. If you include any Windows specific code (or a derivative thereof) from
96da347917Sbeck * the apps directory (application code) you must include an acknowledgement:
97da347917Sbeck * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
98da347917Sbeck *
99da347917Sbeck * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
100da347917Sbeck * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101da347917Sbeck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
102da347917Sbeck * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
103da347917Sbeck * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
104da347917Sbeck * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
105da347917Sbeck * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
106da347917Sbeck * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
107da347917Sbeck * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
108da347917Sbeck * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
109da347917Sbeck * SUCH DAMAGE.
110da347917Sbeck *
111da347917Sbeck * The licence and distribution terms for any publically available version or
112da347917Sbeck * derivative of this code cannot be changed. i.e. this code cannot simply be
113da347917Sbeck * copied and put under another distribution licence
114da347917Sbeck * [including the GNU Public Licence.]
115da347917Sbeck */
116da347917Sbeck
117b6ab114eSjsing #include <sys/ioctl.h>
118b6ab114eSjsing
11920175b85Sjsing #include <openssl/opensslconf.h>
120da347917Sbeck
121b6ab114eSjsing #include <errno.h>
1224fcf65c5Sdjm #include <signal.h>
1234fcf65c5Sdjm #include <stdio.h>
1244fcf65c5Sdjm #include <string.h>
125924aff6aSjsing #include <termios.h>
126b6ab114eSjsing #include <unistd.h>
127da347917Sbeck
128c9675a23Stb #include "ui_local.h"
129da347917Sbeck
130da347917Sbeck #ifndef NX509_SIG
131da347917Sbeck #define NX509_SIG 32
132da347917Sbeck #endif
133da347917Sbeck
134da347917Sbeck /* Define globals. They are protected by a lock */
135da347917Sbeck static struct sigaction savsig[NX509_SIG];
136da347917Sbeck
137142d36aaSguenther static struct termios tty_orig;
138da347917Sbeck static FILE *tty_in, *tty_out;
139da347917Sbeck static int is_a_tty;
140da347917Sbeck
141da347917Sbeck /* Declare static functions */
1420a5d6edeSdjm static int read_till_nl(FILE *);
143da347917Sbeck static void recsig(int);
144da347917Sbeck static void pushsig(void);
145da347917Sbeck static void popsig(void);
146da347917Sbeck static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
147da347917Sbeck
148da347917Sbeck static int read_string(UI *ui, UI_STRING *uis);
149da347917Sbeck static int write_string(UI *ui, UI_STRING *uis);
150da347917Sbeck
151da347917Sbeck static int open_console(UI *ui);
152da347917Sbeck static int echo_console(UI *ui);
153da347917Sbeck static int noecho_console(UI *ui);
154da347917Sbeck static int close_console(UI *ui);
155da347917Sbeck
156c3bf3550Sjsing static UI_METHOD ui_openssl = {
157e402ce74Smiod .name = "OpenSSL default user interface",
158e402ce74Smiod .ui_open_session = open_console,
159e402ce74Smiod .ui_write_string = write_string,
160e402ce74Smiod .ui_read_string = read_string,
161e402ce74Smiod .ui_close_session = close_console,
162da347917Sbeck };
163da347917Sbeck
164da347917Sbeck /* The method with all the built-in thingies */
165764af611Stedu UI_METHOD *
UI_OpenSSL(void)166764af611Stedu UI_OpenSSL(void)
167da347917Sbeck {
168da347917Sbeck return &ui_openssl;
169da347917Sbeck }
170*cedac418Stb LCRYPTO_ALIAS(UI_OpenSSL);
171da347917Sbeck
172da347917Sbeck /* The following function makes sure that info and error strings are printed
173da347917Sbeck before any prompt. */
174764af611Stedu static int
write_string(UI * ui,UI_STRING * uis)175764af611Stedu write_string(UI *ui, UI_STRING *uis)
176da347917Sbeck {
177764af611Stedu switch (UI_get_string_type(uis)) {
178da347917Sbeck case UIT_ERROR:
179da347917Sbeck case UIT_INFO:
180da347917Sbeck fputs(UI_get0_output_string(uis), tty_out);
181da347917Sbeck fflush(tty_out);
182da347917Sbeck break;
183da347917Sbeck default:
184da347917Sbeck break;
185da347917Sbeck }
186da347917Sbeck return 1;
187da347917Sbeck }
188da347917Sbeck
189764af611Stedu static int
read_string(UI * ui,UI_STRING * uis)190764af611Stedu read_string(UI *ui, UI_STRING *uis)
191da347917Sbeck {
192da347917Sbeck int ok = 0;
193da347917Sbeck
194764af611Stedu switch (UI_get_string_type(uis)) {
195da347917Sbeck case UIT_BOOLEAN:
196da347917Sbeck fputs(UI_get0_output_string(uis), tty_out);
197da347917Sbeck fputs(UI_get0_action_string(uis), tty_out);
198da347917Sbeck fflush(tty_out);
199da347917Sbeck return read_string_inner(ui, uis,
200da347917Sbeck UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0);
201da347917Sbeck case UIT_PROMPT:
202da347917Sbeck fputs(UI_get0_output_string(uis), tty_out);
203da347917Sbeck fflush(tty_out);
204da347917Sbeck return read_string_inner(ui, uis,
205da347917Sbeck UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1);
206da347917Sbeck case UIT_VERIFY:
207da347917Sbeck fprintf(tty_out, "Verifying - %s",
208da347917Sbeck UI_get0_output_string(uis));
209da347917Sbeck fflush(tty_out);
210ba586daeSjsing if ((ok = read_string_inner(ui, uis, UI_get_input_flags(uis) &
211ba586daeSjsing UI_INPUT_FLAG_ECHO, 1)) <= 0)
212da347917Sbeck return ok;
213da347917Sbeck if (strcmp(UI_get0_result_string(uis),
214764af611Stedu UI_get0_test_string(uis)) != 0) {
215da347917Sbeck fprintf(tty_out, "Verify failure\n");
216da347917Sbeck fflush(tty_out);
217da347917Sbeck return 0;
218da347917Sbeck }
219da347917Sbeck break;
220da347917Sbeck default:
221da347917Sbeck break;
222da347917Sbeck }
223da347917Sbeck return 1;
224da347917Sbeck }
225da347917Sbeck
226da347917Sbeck
227da347917Sbeck /* Internal functions to read a string without echoing */
228764af611Stedu static int
read_till_nl(FILE * in)229764af611Stedu read_till_nl(FILE *in)
230da347917Sbeck {
231da347917Sbeck #define SIZE 4
232da347917Sbeck char buf[SIZE + 1];
233da347917Sbeck
234da347917Sbeck do {
23597222eddSmiod if (!fgets(buf, SIZE, in))
23697222eddSmiod return 0;
237da347917Sbeck } while (strchr(buf, '\n') == NULL);
2380a5d6edeSdjm return 1;
239da347917Sbeck }
240da347917Sbeck
241ab8d4a1fSderaadt static volatile sig_atomic_t intr_signal;
242da347917Sbeck
243764af611Stedu static int
read_string_inner(UI * ui,UI_STRING * uis,int echo,int strip_nl)244764af611Stedu read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
245da347917Sbeck {
246da347917Sbeck static int ps;
247da347917Sbeck int ok;
248da347917Sbeck char result[BUFSIZ];
249da347917Sbeck int maxsize = BUFSIZ - 1;
250da347917Sbeck char *p;
251da347917Sbeck
252da347917Sbeck intr_signal = 0;
253da347917Sbeck ok = 0;
254da347917Sbeck ps = 0;
255da347917Sbeck
256da347917Sbeck pushsig();
257da347917Sbeck ps = 1;
258da347917Sbeck
259da347917Sbeck if (!echo && !noecho_console(ui))
260da347917Sbeck goto error;
261da347917Sbeck ps = 2;
262da347917Sbeck
263da347917Sbeck result[0] = '\0';
264da347917Sbeck p = fgets(result, maxsize, tty_in);
265da347917Sbeck if (!p)
266da347917Sbeck goto error;
267764af611Stedu if (feof(tty_in))
268764af611Stedu goto error;
269764af611Stedu if (ferror(tty_in))
270764af611Stedu goto error;
27138bc2169Sderaadt if ((p = strchr(result, '\n')) != NULL) {
272da347917Sbeck if (strip_nl)
273da347917Sbeck *p = '\0';
274764af611Stedu } else if (!read_till_nl(tty_in))
2750a5d6edeSdjm goto error;
276da347917Sbeck if (UI_set_result(ui, uis, result) >= 0)
277da347917Sbeck ok = 1;
278da347917Sbeck
279da347917Sbeck error:
280da347917Sbeck if (intr_signal == SIGINT)
281da347917Sbeck ok = -1;
282764af611Stedu if (!echo)
283764af611Stedu fprintf(tty_out, "\n");
284da347917Sbeck if (ps >= 2 && !echo && !echo_console(ui))
285da347917Sbeck ok = 0;
286da347917Sbeck
287da347917Sbeck if (ps >= 1)
288da347917Sbeck popsig();
289da347917Sbeck
2900f777b12Sjsing explicit_bzero(result, BUFSIZ);
291da347917Sbeck return ok;
292da347917Sbeck }
293da347917Sbeck
294da347917Sbeck
295da347917Sbeck /* Internal functions to open, handle and close a channel to the console. */
296764af611Stedu static int
open_console(UI * ui)297764af611Stedu open_console(UI *ui)
298da347917Sbeck {
299da347917Sbeck CRYPTO_w_lock(CRYPTO_LOCK_UI);
300da347917Sbeck is_a_tty = 1;
301da347917Sbeck
302da347917Sbeck #define DEV_TTY "/dev/tty"
303da347917Sbeck if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
304da347917Sbeck tty_in = stdin;
305da347917Sbeck if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
306da347917Sbeck tty_out = stderr;
307da347917Sbeck
308924aff6aSjsing if (tcgetattr(fileno(tty_in), &tty_orig) == -1) {
309da347917Sbeck if (errno == ENOTTY)
310da347917Sbeck is_a_tty = 0;
311da347917Sbeck else
312764af611Stedu /*
313764af611Stedu * Ariel Glenn ariel@columbia.edu reports that
314764af611Stedu * solaris can return EINVAL instead. This should be
315764af611Stedu * ok
316764af611Stedu */
317da347917Sbeck if (errno == EINVAL)
318da347917Sbeck is_a_tty = 0;
319da347917Sbeck else
320da347917Sbeck return 0;
321da347917Sbeck }
322924aff6aSjsing
323da347917Sbeck return 1;
324da347917Sbeck }
325da347917Sbeck
326764af611Stedu static int
noecho_console(UI * ui)327764af611Stedu noecho_console(UI *ui)
328da347917Sbeck {
329142d36aaSguenther struct termios tty_new = tty_orig;
330142d36aaSguenther
331924aff6aSjsing tty_new.c_lflag &= ~ECHO;
332924aff6aSjsing if (is_a_tty && (tcsetattr(fileno(tty_in), TCSANOW, &tty_new) == -1))
333da347917Sbeck return 0;
334da347917Sbeck return 1;
335da347917Sbeck }
336da347917Sbeck
337764af611Stedu static int
echo_console(UI * ui)338764af611Stedu echo_console(UI *ui)
339da347917Sbeck {
340142d36aaSguenther if (is_a_tty && (tcsetattr(fileno(tty_in), TCSANOW, &tty_orig) == -1))
341da347917Sbeck return 0;
342da347917Sbeck return 1;
343da347917Sbeck }
344da347917Sbeck
345764af611Stedu static int
close_console(UI * ui)346764af611Stedu close_console(UI *ui)
347da347917Sbeck {
348764af611Stedu if (tty_in != stdin)
349764af611Stedu fclose(tty_in);
350764af611Stedu if (tty_out != stderr)
351764af611Stedu fclose(tty_out);
352da347917Sbeck CRYPTO_w_unlock(CRYPTO_LOCK_UI);
353da347917Sbeck
354da347917Sbeck return 1;
355da347917Sbeck }
356da347917Sbeck
357da347917Sbeck
358da347917Sbeck /* Internal functions to handle signals and act on them */
359764af611Stedu static void
pushsig(void)360764af611Stedu pushsig(void)
361da347917Sbeck {
362da347917Sbeck int i;
363da347917Sbeck struct sigaction sa;
364da347917Sbeck
365da347917Sbeck memset(&sa, 0, sizeof sa);
366da347917Sbeck sa.sa_handler = recsig;
367da347917Sbeck
368764af611Stedu for (i = 1; i < NX509_SIG; i++) {
369da347917Sbeck if (i == SIGUSR1)
370da347917Sbeck continue;
371da347917Sbeck if (i == SIGUSR2)
372da347917Sbeck continue;
373da347917Sbeck if (i == SIGKILL) /* We can't make any action on that. */
374da347917Sbeck continue;
375da347917Sbeck sigaction(i, &sa, &savsig[i]);
376da347917Sbeck }
377da347917Sbeck
378da347917Sbeck signal(SIGWINCH, SIG_DFL);
379da347917Sbeck }
380da347917Sbeck
381764af611Stedu static void
popsig(void)382764af611Stedu popsig(void)
383da347917Sbeck {
384da347917Sbeck int i;
385764af611Stedu for (i = 1; i < NX509_SIG; i++) {
386da347917Sbeck if (i == SIGUSR1)
387da347917Sbeck continue;
388da347917Sbeck if (i == SIGUSR2)
389da347917Sbeck continue;
390da347917Sbeck sigaction(i, &savsig[i], NULL);
391da347917Sbeck }
392da347917Sbeck }
393da347917Sbeck
394764af611Stedu static void
recsig(int i)395764af611Stedu recsig(int i)
396da347917Sbeck {
397da347917Sbeck intr_signal = i;
398da347917Sbeck }
399