1 /* $OpenBSD: ui_openssl.c,v 1.22 2014/07/11 08:44:49 jsing Exp $ */
2 /* Written by Richard Levitte (richard@levitte.org) and others
3 * for the OpenSSL project 2001.
5 /* ====================================================================
6 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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.
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.
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).
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.
81 * Redistribution and use in source and binary forms, with or without
82 * modification, are permitted provided that the following conditions
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)"
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
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.]
117 #include <sys/ioctl.h>
119 #include <openssl/opensslconf.h>
133 /* Define globals. They are protected by a lock */
134 static void (*savsig
[NX509_SIG
])(int );
137 static FILE *tty_in
, *tty_out
;
140 /* Declare static functions */
141 static int read_till_nl(FILE *);
142 static void recsig(int);
143 static void pushsig(void);
144 static void popsig(void);
145 static int read_string_inner(UI
*ui
, UI_STRING
*uis
, int echo
, int strip_nl
);
147 static int read_string(UI
*ui
, UI_STRING
*uis
);
148 static int write_string(UI
*ui
, UI_STRING
*uis
);
150 static int open_console(UI
*ui
);
151 static int echo_console(UI
*ui
);
152 static int noecho_console(UI
*ui
);
153 static int close_console(UI
*ui
);
155 static UI_METHOD ui_openssl
= {
156 .name
= "OpenSSL default user interface",
157 .ui_open_session
= open_console
,
158 .ui_write_string
= write_string
,
159 .ui_read_string
= read_string
,
160 .ui_close_session
= close_console
,
163 /* The method with all the built-in thingies */
170 /* The following function makes sure that info and error strings are printed
171 before any prompt. */
173 write_string(UI
*ui
, UI_STRING
*uis
)
175 switch (UI_get_string_type(uis
)) {
178 fputs(UI_get0_output_string(uis
), tty_out
);
188 read_string(UI
*ui
, UI_STRING
*uis
)
192 switch (UI_get_string_type(uis
)) {
194 fputs(UI_get0_output_string(uis
), tty_out
);
195 fputs(UI_get0_action_string(uis
), tty_out
);
197 return read_string_inner(ui
, uis
,
198 UI_get_input_flags(uis
) & UI_INPUT_FLAG_ECHO
, 0);
200 fputs(UI_get0_output_string(uis
), tty_out
);
202 return read_string_inner(ui
, uis
,
203 UI_get_input_flags(uis
) & UI_INPUT_FLAG_ECHO
, 1);
205 fprintf(tty_out
, "Verifying - %s",
206 UI_get0_output_string(uis
));
208 if ((ok
= read_string_inner(ui
, uis
, UI_get_input_flags(uis
) &
209 UI_INPUT_FLAG_ECHO
, 1)) <= 0)
211 if (strcmp(UI_get0_result_string(uis
),
212 UI_get0_test_string(uis
)) != 0) {
213 fprintf(tty_out
, "Verify failure\n");
225 /* Internal functions to read a string without echoing */
227 read_till_nl(FILE *in
)
233 if (!fgets(buf
, SIZE
, in
))
235 } while (strchr(buf
, '\n') == NULL
);
239 static volatile sig_atomic_t intr_signal
;
242 read_string_inner(UI
*ui
, UI_STRING
*uis
, int echo
, int strip_nl
)
247 int maxsize
= BUFSIZ
- 1;
258 if (!echo
&& !noecho_console(ui
))
263 p
= fgets(result
, maxsize
, tty_in
);
270 if ((p
= strchr(result
, '\n')) != NULL
) {
273 } else if (!read_till_nl(tty_in
))
275 if (UI_set_result(ui
, uis
, result
) >= 0)
279 if (intr_signal
== SIGINT
)
282 fprintf(tty_out
, "\n");
283 if (ps
>= 2 && !echo
&& !echo_console(ui
))
289 explicit_bzero(result
, BUFSIZ
);
294 /* Internal functions to open, handle and close a channel to the console. */
298 CRYPTO_w_lock(CRYPTO_LOCK_UI
);
304 HANDLE handle
= GetStdHandle(STD_INPUT_HANDLE
);
305 if (handle
!= NULL
&& handle
!= INVALID_HANDLE_VALUE
) {
306 if (GetFileType(handle
) == FILE_TYPE_CHAR
)
307 return GetConsoleMode(handle
, &console_mode
);
315 noecho_console(UI
*ui
)
317 HANDLE handle
= GetStdHandle(STD_INPUT_HANDLE
);
318 if (handle
!= NULL
&& handle
!= INVALID_HANDLE_VALUE
) {
319 if (GetFileType(handle
) == FILE_TYPE_CHAR
)
320 return SetConsoleMode(handle
, console_mode
& ~ENABLE_ECHO_INPUT
);
330 HANDLE handle
= GetStdHandle(STD_INPUT_HANDLE
);
331 if (handle
!= NULL
&& handle
!= INVALID_HANDLE_VALUE
) {
332 if (GetFileType(handle
) == FILE_TYPE_CHAR
)
333 return SetConsoleMode(handle
, console_mode
);
341 close_console(UI
*ui
)
345 if (tty_out
!= stderr
)
347 CRYPTO_w_unlock(CRYPTO_LOCK_UI
);
352 /* Internal functions to handle signals and act on them */
356 savsig
[SIGABRT
] = signal(SIGABRT
, recsig
);
357 savsig
[SIGFPE
] = signal(SIGFPE
, recsig
);
358 savsig
[SIGILL
] = signal(SIGILL
, recsig
);
359 savsig
[SIGINT
] = signal(SIGINT
, recsig
);
360 savsig
[SIGSEGV
] = signal(SIGSEGV
, recsig
);
361 savsig
[SIGTERM
] = signal(SIGTERM
, recsig
);
367 signal(SIGABRT
, savsig
[SIGABRT
]);
368 signal(SIGFPE
, savsig
[SIGFPE
]);
369 signal(SIGILL
, savsig
[SIGILL
]);
370 signal(SIGINT
, savsig
[SIGINT
]);
371 signal(SIGSEGV
, savsig
[SIGSEGV
]);
372 signal(SIGTERM
, savsig
[SIGTERM
]);