Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/console/console.c

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3
4 #include <openssl/base.h>
5
6 #include "internal.h"
7 #include "../internal.h"
8
9 // Some platforms (e.g. nanolibc, baremetal) do not support terminal operations
10 // (no termios, no /dev/tty). Provide stub implementations that indicate no
11 // console is available. Higher-level APIs that depend on console I/O, such as
12 // EVP_read_pw_string and EVP_read_pw_string_min, will fail on these platforms.
13 //
14 // OpenSSL uses OPENSSL_NO_UI_CONSOLE to gate out the entire UI_OpenSSL()
15 // method. Our approach differs: we stub at the lower openssl_console_* layer
16 // so that the rest of the code compiles unconditionally and fails gracefully
17 // at runtime.
18 #if defined(OPENSSL_NO_TTY)
19
20 void openssl_console_acquire_mutex(void) {}
21
22 void openssl_console_release_mutex(void) {}
23
24 int openssl_console_open(void) {
25 // No console available
26 return 0;
27 }
28
29 int openssl_console_close(void) {
30 return 1;
31 }
32
33 int openssl_console_write(const char *str) {
34 (void)str;
35 return 0;
36 }
37
38 int openssl_console_read(char *buf, int minsize, int maxsize, int echo) {
39 (void)buf;
40 (void)minsize;
41 (void)maxsize;
42 (void)echo;
43 return -1; // Error: no console available
44 }
45
46 #else // !OPENSSL_NO_TTY
47
48 #include <assert.h>
49 #include <ctype.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <signal.h>
53 #include <errno.h>
54
55 #include <openssl/buf.h>
56 #include <openssl/err.h>
57 #include <openssl/evp.h>
58 #include <openssl/pem.h>
59 #include <openssl/x509.h>
60
61
62 // We support two types of terminal interface:
63 // - termios for Linux/Unix
64 // - WIN32 Console for Windows
65 #if !defined(OPENSSL_WINDOWS)
66 #include <termios.h>
67 #define DEV_TTY "/dev/tty"
68 #define TTY_STRUCT struct termios
69 #define TTY_FLAGS c_lflag
70 #define TTY_get(tty,data) tcgetattr(tty, data)
71 #define TTY_set(tty,data) tcsetattr(tty, TCSANOW, data)
72 #else /* OPENSSL_WINDOWS */
73 #include <windows.h>
74 #endif
75
76 #define NUM_SIG 32
77
78 static volatile sig_atomic_t intr_signal;
79 static struct CRYPTO_STATIC_MUTEX console_global_mutex = CRYPTO_STATIC_MUTEX_INIT;
80
81 #if !defined(OPENSSL_WINDOWS)
82 static struct sigaction savsig[NUM_SIG];
83 #else
84 static void (*savsig[NUM_SIG]) (int);
85 #endif
86
87 #if defined(OPENSSL_WINDOWS)
88 DWORD tty_orig, tty_new;
89 #else
90 TTY_STRUCT tty_orig, tty_new;
91 #endif
92
93 FILE *tty_in, *tty_out;
94 int is_a_tty;
95
96
97 static void popsig(void) {
98 #if !defined(OPENSSL_WINDOWS)
99 for (int i = 1; i < NUM_SIG; i++) {
100 if (i == SIGUSR1 || i == SIGUSR2 || i == SIGKILL) {
101 continue;
102 }
103 sigaction(i, &savsig[i], NULL);
104 }
105 #else
106 signal(SIGABRT, savsig[SIGABRT]);
107 signal(SIGFPE, savsig[SIGFPE]);
108 signal(SIGILL, savsig[SIGILL]);
109 signal(SIGINT, savsig[SIGINT]);
110 signal(SIGSEGV, savsig[SIGSEGV]);
111 signal(SIGTERM, savsig[SIGTERM]);
112 #endif
113 }
114
115 static void recsig(int signal) {
116 intr_signal = signal;
117 }
118
119 static int discard_line_remainder(FILE *in) {
120 char buf[5];
121
122 do {
123 if (!fgets(buf, 4, in)) {
124 if (ferror(in)) {
125 OPENSSL_PUT_ERROR(PEM, PEM_R_PROBLEMS_GETTING_PASSWORD);
126 ERR_add_error_data(2, "System error: ", strerror(errno));
127 clearerr(tty_in);
128 } else if(feof(in)) {
129 return 1;
130 }
131 return 0;
132 }
133 } while (strchr(buf, '\n') == NULL);
134 return 1;
135 }
136
137 /* Signal handling functions */
138 static void pushsig(void) {
139 #if !defined(OPENSSL_WINDOWS)
140 struct sigaction sa;
141 OPENSSL_cleanse(&sa, sizeof(sa));
142
143 sa.sa_handler = recsig;
144
145 for (int i = 1; i < NUM_SIG; i++) {
146 if (i == SIGUSR1 || i == SIGUSR2 || i == SIGKILL) {
147 continue;
148 }
149 sigaction(i, &sa, &savsig[i]);
150 }
151 #else // Specific error codes for windows
152 savsig[SIGABRT] = signal(SIGABRT, recsig);
153 savsig[SIGFPE] = signal(SIGFPE, recsig);
154 savsig[SIGILL] = signal(SIGILL, recsig);
155 savsig[SIGINT] = signal(SIGINT, recsig);
156 savsig[SIGSEGV] = signal(SIGSEGV, recsig);
157 savsig[SIGTERM] = signal(SIGTERM, recsig);
158 #endif
159
160 // set SIGWINCH handler to default so our workflow is not
161 // triggered on user resizing of window
162 #if defined(SIGWINCH) && defined(SIG_DFL)
163 signal(SIGWINCH, SIG_DFL);
164 #endif
165 }
166
167 /* Console management functions */
168 void openssl_console_acquire_mutex(void) {
169 CRYPTO_STATIC_MUTEX_lock_write(&console_global_mutex);
170 }
171
172 void openssl_console_release_mutex(void) {
173 CRYPTO_STATIC_MUTEX_unlock_write(&console_global_mutex);
174 }
175
176 int openssl_console_open(void) {
177 is_a_tty = 1;
178 assert(CRYPTO_STATIC_MUTEX_is_write_locked(&console_global_mutex));
179
180 // Check for test environment variable first (platform-independent)
181 const char* test_mode = getenv("AWSLC_CONSOLE_NO_TTY_DETECT");
182 if (test_mode != NULL) {
183 tty_in = stdin;
184 tty_out = stderr;
185 is_a_tty = 0;
186 return 1;
187 }
188
189 #if !defined(OPENSSL_WINDOWS)
190 if ((tty_in = fopen(DEV_TTY, "r")) == NULL) {
191 tty_in = stdin;
192 }
193 if ((tty_out = fopen(DEV_TTY, "w")) == NULL) {
194 tty_out = stderr;
195 }
196 if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
197 if (errno == ENOTTY || errno == EINVAL || errno == ENXIO || errno == EIO
198 || errno == EPERM || errno == ENODEV) {
199 is_a_tty = 0;
200 } else {
201 OPENSSL_PUT_ERROR(PEM, ERR_R_INTERNAL_ERROR);
202 return 0;
203 }
204 }
205 #else
206 DWORD console_mode;
207 HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
208
209 // Check if console (equivalent to checking for /dev/tty on Linux)
210 if (GetConsoleMode(hStdIn, &console_mode)) {
211 // It's a real console, use conin$ and conout$ to bypass any redirection
212 if ((tty_in = fopen("conin$", "r")) == NULL) {
213 tty_in = stdin;
214 }
215 if ((tty_out = fopen("conout$", "w")) == NULL) {
216 tty_out = stderr;
217 }
218
219 tty_orig = console_mode;
220 } else {
221 // Not a console, use stdin/stderr
222 tty_in = stdin;
223 tty_out = stderr;
224 is_a_tty = 0;
225 }
226 #endif
227 return 1;
228 }
229
230 int openssl_console_close(void) {
231 assert(CRYPTO_STATIC_MUTEX_is_write_locked(&console_global_mutex));
232 if (tty_in != stdin) {
233 fclose(tty_in);
234 }
235 if (tty_out != stderr) {
236 fclose(tty_out);
237 }
238
239 return 1;
240 }
241
242 static int openssl_console_echo_disable(void) {
243 #if !defined(OPENSSL_WINDOWS)
244 OPENSSL_memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
245 tty_new.TTY_FLAGS &= ~ECHO;
246
247 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1)) {
248 OPENSSL_PUT_ERROR(PEM, ERR_R_INTERNAL_ERROR);
249 return 0;
250 }
251 #else
252 if (is_a_tty) {
253 tty_new = tty_orig;
254 tty_new &= ~ENABLE_ECHO_INPUT;
255 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
256 }
257 #endif
258
259 return 1;
260 }
261
262 static int openssl_console_echo_enable(void) {
263 #if !defined(OPENSSL_WINDOWS)
264 OPENSSL_memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
265 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1)) {
266 OPENSSL_PUT_ERROR(PEM, ERR_R_INTERNAL_ERROR);
267 return 0;
268 }
269 #else
270 if (is_a_tty) {
271 tty_new = tty_orig;
272 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
273 }
274 #endif
275 return 1;
276 }
277
278 int openssl_console_write(const char *str) {
279 assert(CRYPTO_STATIC_MUTEX_is_write_locked(&console_global_mutex));
280 if (fputs(str, tty_out) < 0 || fflush(tty_out) != 0) {
281 OPENSSL_PUT_ERROR(PEM, PEM_R_PROBLEMS_GETTING_PASSWORD);
282 if (ferror(tty_out)) {
283 ERR_add_error_data(2, "System error: ", strerror(errno));
284 clearerr(tty_out);
285 }
286 return 0;
287 }
288
289 return 1;
290 }
291
292 // returns 0 on success, -1 on error, -2 if interrupt signal received
293 int openssl_console_read(char *buf, int minsize, int maxsize, int echo) {
294 int ok = 0;
295 char *p = NULL;
296 int echo_eol = !echo;
297
298 intr_signal = 0;
299 int phase = 0;
300
301 if (!buf || maxsize < minsize) {
302 return -1;
303 }
304
305 assert(CRYPTO_STATIC_MUTEX_is_write_locked(&console_global_mutex));
306
307 pushsig();
308 phase = 1;
309
310 if (!echo && !openssl_console_echo_disable()) {
311 goto error;
312 }
313 phase = 2;
314
315 buf[0] = '\0';
316 #if defined(OPENSSL_WINDOWS)
317 if (is_a_tty) {
318 DWORD numread;
319 // for now assuming UTF-8....
320 WCHAR wresult[BUFSIZ];
321 OPENSSL_cleanse(wresult, sizeof(wresult));
322
323 // Cap the read count to the wresult buffer size to prevent overflow.
324 DWORD read_count = (DWORD)maxsize;
325 if (read_count >= (sizeof(wresult) / sizeof(wresult[0]))) {
326 read_count = (sizeof(wresult) / sizeof(wresult[0])) - 1;
327 }
328
329 if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
330 wresult, read_count, &numread, NULL)) {
331 if (numread >= 2 && wresult[numread-2] == L'\r' &&
332 wresult[numread-1] == L'\n') {
333 wresult[numread-2] = L'\n';
334 numread--;
335 }
336 wresult[numread] = L'\0';
337 if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1, buf, maxsize + 1, NULL, 0) > 0) {
338 p = buf;
339 }
340 OPENSSL_cleanse(wresult, sizeof(wresult));
341 }
342 } else {
343 p = fgets(buf, maxsize, tty_in);
344 }
345 #else
346 p = fgets(buf, maxsize, tty_in);
347 #endif
348 if (p == NULL || feof(tty_in) || ferror(tty_in)) {
349 OPENSSL_PUT_ERROR(PEM, PEM_R_PROBLEMS_GETTING_PASSWORD);
350 if (ferror(tty_in)) {
351 ERR_add_error_data(2, "System error: ", strerror(errno));
352 clearerr(tty_in);
353 }
354 ok = -1;
355 goto error;
356 }
357
358 // check if we see a new line, otherwise clear out remaining input buffer
359 if ((p = strchr(buf, '\n')) != NULL) {
360 *p = '\0';
361 } else if (!discard_line_remainder(tty_in)) {
362 ok = -1;
363 goto error;
364 }
365
366 // Validate that input meets the minimum length requirement. We accept buffers |buf| of any size
367 // but enforce a maximum password length of 1024 characters for compatibility with OpenSSL.
368 // Unlike OpenSSL's higher-level APIs which silently truncate buffers exceeding this limit,
369 // we explicitly check the password length after reading it from the user. This approach maintains
370 // OpenSSL compatibility while avoiding silent truncation.
371 size_t input_len = strlen(buf);
372 if (input_len < (size_t)minsize || input_len > (size_t)MAX_PASSWORD_LENGTH) {
373 ok = -1;
374 }
375
376 error:
377 if (intr_signal == SIGINT) {
378 ok = -2; // interrupted
379 }
380 if (echo_eol) {
381 fprintf(tty_out, "\n");
382 }
383 if (phase >= 2 && !echo && !openssl_console_echo_enable()) {
384 ok = -1; // general errors
385 }
386 if (phase >= 1) {
387 popsig();
388 }
389
390 return ok;
391 }
392
393 #endif // !OPENSSL_NO_TTY