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