summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ui/ui_openssl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/ui/ui_openssl.c')
-rw-r--r--src/lib/libcrypto/ui/ui_openssl.c315
1 files changed, 163 insertions, 152 deletions
diff --git a/src/lib/libcrypto/ui/ui_openssl.c b/src/lib/libcrypto/ui/ui_openssl.c
index 6000c24b1c..94a999baa8 100644
--- a/src/lib/libcrypto/ui/ui_openssl.c
+++ b/src/lib/libcrypto/ui/ui_openssl.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -63,21 +63,21 @@
63 * This package is an SSL implementation written 63 * This package is an SSL implementation written
64 * by Eric Young (eay@cryptsoft.com). 64 * by Eric Young (eay@cryptsoft.com).
65 * The implementation was written so as to conform with Netscapes SSL. 65 * The implementation was written so as to conform with Netscapes SSL.
66 * 66 *
67 * This library is free for commercial and non-commercial use as long as 67 * This library is free for commercial and non-commercial use as long as
68 * the following conditions are aheared to. The following conditions 68 * the following conditions are aheared to. The following conditions
69 * apply to all code found in this distribution, be it the RC4, RSA, 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 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 71 * included with this distribution is covered by the same copyright terms
72 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 72 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
73 * 73 *
74 * Copyright remains Eric Young's, and as such any Copyright notices in 74 * Copyright remains Eric Young's, and as such any Copyright notices in
75 * the code are not to be removed. 75 * the code are not to be removed.
76 * If this package is used in a product, Eric Young should be given attribution 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. 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 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. 79 * in documentation (online or textual) provided with the package.
80 * 80 *
81 * Redistribution and use in source and binary forms, with or without 81 * Redistribution and use in source and binary forms, with or without
82 * modification, are permitted provided that the following conditions 82 * modification, are permitted provided that the following conditions
83 * are met: 83 * are met:
@@ -92,10 +92,10 @@
92 * Eric Young (eay@cryptsoft.com)" 92 * Eric Young (eay@cryptsoft.com)"
93 * The word 'cryptographic' can be left out if the rouines from the library 93 * The word 'cryptographic' can be left out if the rouines from the library
94 * being used are not cryptographic related :-). 94 * being used are not cryptographic related :-).
95 * 4. If you include any Windows specific code (or a derivative thereof) from 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: 96 * the apps directory (application code) you must include an acknowledgement:
97 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 97 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
98 * 98 *
99 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 99 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
100 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 100 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 101 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -107,7 +107,7 @@
107 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 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 108 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
109 * SUCH DAMAGE. 109 * SUCH DAMAGE.
110 * 110 *
111 * The licence and distribution terms for any publically available version or 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 112 * derivative of this code cannot be changed. i.e. this code cannot simply be
113 * copied and put under another distribution licence 113 * copied and put under another distribution licence
@@ -125,14 +125,14 @@
125#include <unistd.h> 125#include <unistd.h>
126/* If unistd.h defines _POSIX_VERSION, we conclude that we 126/* If unistd.h defines _POSIX_VERSION, we conclude that we
127 * are on a POSIX system and have sigaction and termios. */ 127 * are on a POSIX system and have sigaction and termios. */
128# if defined(_POSIX_VERSION) 128#if defined(_POSIX_VERSION)
129 129
130# define SIGACTION 130#define SIGACTION
131# if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY) 131#if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
132# define TERMIOS 132#define TERMIOS
133# endif 133#endif
134 134
135# endif 135#endif
136 136
137#include "ui_locl.h" 137#include "ui_locl.h"
138#include "cryptlib.h" 138#include "cryptlib.h"
@@ -143,31 +143,31 @@
143 */ 143 */
144 144
145#ifdef _LIBC 145#ifdef _LIBC
146# undef TERMIOS 146#undef TERMIOS
147# define TERMIO 147#define TERMIO
148# undef SGTTY 148#undef SGTTY
149#endif 149#endif
150 150
151#ifdef TERMIOS 151#ifdef TERMIOS
152# include <termios.h> 152#include <termios.h>
153# define TTY_STRUCT struct termios 153#define TTY_STRUCT struct termios
154# define TTY_FLAGS c_lflag 154#define TTY_FLAGS c_lflag
155# define TTY_get(tty,data) tcgetattr(tty,data) 155#define TTY_get(tty,data) tcgetattr(tty,data)
156# define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data) 156#define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
157#endif 157#endif
158 158
159#ifdef TERMIO 159#ifdef TERMIO
160# include <termio.h> 160#include <termio.h>
161# define TTY_STRUCT struct termio 161#define TTY_STRUCT struct termio
162# define TTY_FLAGS c_lflag 162#define TTY_FLAGS c_lflag
163# define TTY_get(tty,data) ioctl(tty,TCGETA,data) 163#define TTY_get(tty,data) ioctl(tty,TCGETA,data)
164# define TTY_set(tty,data) ioctl(tty,TCSETA,data) 164#define TTY_set(tty,data) ioctl(tty,TCSETA,data)
165#endif 165#endif
166 166
167# include <sys/ioctl.h> 167#include <sys/ioctl.h>
168 168
169#ifndef NX509_SIG 169#ifndef NX509_SIG
170# define NX509_SIG 32 170#define NX509_SIG 32
171#endif 171#endif
172 172
173 173
@@ -175,10 +175,10 @@
175#ifdef SIGACTION 175#ifdef SIGACTION
176static struct sigaction savsig[NX509_SIG]; 176static struct sigaction savsig[NX509_SIG];
177#else 177#else
178static void (*savsig[NX509_SIG])(int ); 178static void (*savsig[NX509_SIG]) (int);
179#endif 179#endif
180 180
181static TTY_STRUCT tty_orig,tty_new; 181static TTY_STRUCT tty_orig, tty_new;
182static FILE *tty_in, *tty_out; 182static FILE *tty_in, *tty_out;
183static int is_a_tty; 183static int is_a_tty;
184 184
@@ -187,18 +187,18 @@ static int read_till_nl(FILE *);
187static void recsig(int); 187static void recsig(int);
188static void pushsig(void); 188static void pushsig(void);
189static void popsig(void); 189static void popsig(void);
190static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl); 190static int read_string_inner(UI * ui, UI_STRING * uis, int echo, int strip_nl);
191 191
192static int read_string(UI *ui, UI_STRING *uis); 192static int read_string(UI * ui, UI_STRING * uis);
193static int write_string(UI *ui, UI_STRING *uis); 193static int write_string(UI * ui, UI_STRING * uis);
194 194
195static int open_console(UI *ui); 195static int open_console(UI * ui);
196static int echo_console(UI *ui); 196static int echo_console(UI * ui);
197static int noecho_console(UI *ui); 197static int noecho_console(UI * ui);
198static int close_console(UI *ui); 198static int close_console(UI * ui);
199 199
200static UI_METHOD ui_openssl = 200static UI_METHOD ui_openssl =
201 { 201{
202 "OpenSSL default user interface", 202 "OpenSSL default user interface",
203 open_console, 203 open_console,
204 write_string, 204 write_string,
@@ -206,216 +206,226 @@ static UI_METHOD ui_openssl =
206 read_string, 206 read_string,
207 close_console, 207 close_console,
208 NULL 208 NULL
209 }; 209};
210 210
211/* The method with all the built-in thingies */ 211/* The method with all the built-in thingies */
212UI_METHOD *UI_OpenSSL(void) 212UI_METHOD *
213 { 213UI_OpenSSL(void)
214{
214 return &ui_openssl; 215 return &ui_openssl;
215 } 216}
216 217
217/* The following function makes sure that info and error strings are printed 218/* The following function makes sure that info and error strings are printed
218 before any prompt. */ 219 before any prompt. */
219static int write_string(UI *ui, UI_STRING *uis) 220static int
220 { 221write_string(UI * ui, UI_STRING * uis)
221 switch (UI_get_string_type(uis)) 222{
222 { 223 switch (UI_get_string_type(uis)) {
223 case UIT_ERROR: 224 case UIT_ERROR:
224 case UIT_INFO: 225 case UIT_INFO:
225 fputs(UI_get0_output_string(uis), tty_out); 226 fputs(UI_get0_output_string(uis), tty_out);
226 fflush(tty_out); 227 fflush(tty_out);
227 break; 228 break;
228 default: 229 default:
229 break; 230 break;
230 }
231 return 1;
232 } 231 }
232 return 1;
233}
233 234
234static int read_string(UI *ui, UI_STRING *uis) 235static int
235 { 236read_string(UI * ui, UI_STRING * uis)
237{
236 int ok = 0; 238 int ok = 0;
237 239
238 switch (UI_get_string_type(uis)) 240 switch (UI_get_string_type(uis)) {
239 {
240 case UIT_BOOLEAN: 241 case UIT_BOOLEAN:
241 fputs(UI_get0_output_string(uis), tty_out); 242 fputs(UI_get0_output_string(uis), tty_out);
242 fputs(UI_get0_action_string(uis), tty_out); 243 fputs(UI_get0_action_string(uis), tty_out);
243 fflush(tty_out); 244 fflush(tty_out);
244 return read_string_inner(ui, uis, 245 return read_string_inner(ui, uis,
245 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0); 246 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0);
246 case UIT_PROMPT: 247 case UIT_PROMPT:
247 fputs(UI_get0_output_string(uis), tty_out); 248 fputs(UI_get0_output_string(uis), tty_out);
248 fflush(tty_out); 249 fflush(tty_out);
249 return read_string_inner(ui, uis, 250 return read_string_inner(ui, uis,
250 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1); 251 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1);
251 case UIT_VERIFY: 252 case UIT_VERIFY:
252 fprintf(tty_out,"Verifying - %s", 253 fprintf(tty_out, "Verifying - %s",
253 UI_get0_output_string(uis)); 254 UI_get0_output_string(uis));
254 fflush(tty_out); 255 fflush(tty_out);
255 if ((ok = read_string_inner(ui, uis, 256 if ((ok = read_string_inner(ui, uis,
256 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1)) <= 0) 257 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1)) <= 0)
257 return ok; 258 return ok;
258 if (strcmp(UI_get0_result_string(uis), 259 if (strcmp(UI_get0_result_string(uis),
259 UI_get0_test_string(uis)) != 0) 260 UI_get0_test_string(uis)) != 0) {
260 { 261 fprintf(tty_out, "Verify failure\n");
261 fprintf(tty_out,"Verify failure\n");
262 fflush(tty_out); 262 fflush(tty_out);
263 return 0; 263 return 0;
264 } 264 }
265 break; 265 break;
266 default: 266 default:
267 break; 267 break;
268 }
269 return 1;
270 } 268 }
269 return 1;
270}
271 271
272 272
273/* Internal functions to read a string without echoing */ 273/* Internal functions to read a string without echoing */
274static int read_till_nl(FILE *in) 274static int
275 { 275read_till_nl(FILE * in)
276{
276#define SIZE 4 277#define SIZE 4
277 char buf[SIZE+1]; 278 char buf[SIZE + 1];
278 279
279 do { 280 do {
280 if (!fgets(buf,SIZE,in)) 281 if (!fgets(buf, SIZE, in))
281 return 0; 282 return 0;
282 } while (strchr(buf,'\n') == NULL); 283 } while (strchr(buf, '\n') == NULL);
283 return 1; 284 return 1;
284 } 285}
285 286
286static volatile sig_atomic_t intr_signal; 287static volatile sig_atomic_t intr_signal;
287 288
288static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl) 289static int
289 { 290read_string_inner(UI * ui, UI_STRING * uis, int echo, int strip_nl)
291{
290 static int ps; 292 static int ps;
291 int ok; 293 int ok;
292 char result[BUFSIZ]; 294 char result[BUFSIZ];
293 int maxsize = BUFSIZ-1; 295 int maxsize = BUFSIZ - 1;
294 char *p; 296 char *p;
295 297
296 intr_signal=0; 298 intr_signal = 0;
297 ok=0; 299 ok = 0;
298 ps=0; 300 ps = 0;
299 301
300 pushsig(); 302 pushsig();
301 ps=1; 303 ps = 1;
302 304
303 if (!echo && !noecho_console(ui)) 305 if (!echo && !noecho_console(ui))
304 goto error; 306 goto error;
305 ps=2; 307 ps = 2;
306 308
307 result[0]='\0'; 309 result[0] = '\0';
308 p=fgets(result,maxsize,tty_in); 310 p = fgets(result, maxsize, tty_in);
309 if(!p) 311 if (!p)
310 goto error; 312 goto error;
311 if (feof(tty_in)) goto error; 313 if (feof(tty_in))
312 if (ferror(tty_in)) goto error; 314 goto error;
313 if ((p=(char *)strchr(result,'\n')) != NULL) 315 if (ferror(tty_in))
314 { 316 goto error;
317 if ((p = (char *) strchr(result, '\n')) != NULL) {
315 if (strip_nl) 318 if (strip_nl)
316 *p='\0'; 319 *p = '\0';
317 } 320 } else if (!read_till_nl(tty_in))
318 else 321 goto error;
319 if (!read_till_nl(tty_in))
320 goto error;
321 if (UI_set_result(ui, uis, result) >= 0) 322 if (UI_set_result(ui, uis, result) >= 0)
322 ok=1; 323 ok = 1;
323 324
324error: 325error:
325 if (intr_signal == SIGINT) 326 if (intr_signal == SIGINT)
326 ok=-1; 327 ok = -1;
327 if (!echo) fprintf(tty_out,"\n"); 328 if (!echo)
329 fprintf(tty_out, "\n");
328 if (ps >= 2 && !echo && !echo_console(ui)) 330 if (ps >= 2 && !echo && !echo_console(ui))
329 ok=0; 331 ok = 0;
330 332
331 if (ps >= 1) 333 if (ps >= 1)
332 popsig(); 334 popsig();
333 335
334 OPENSSL_cleanse(result,BUFSIZ); 336 OPENSSL_cleanse(result, BUFSIZ);
335 return ok; 337 return ok;
336 } 338}
337 339
338 340
339/* Internal functions to open, handle and close a channel to the console. */ 341/* Internal functions to open, handle and close a channel to the console. */
340static int open_console(UI *ui) 342static int
341 { 343open_console(UI * ui)
344{
342 CRYPTO_w_lock(CRYPTO_LOCK_UI); 345 CRYPTO_w_lock(CRYPTO_LOCK_UI);
343 is_a_tty = 1; 346 is_a_tty = 1;
344 347
345# define DEV_TTY "/dev/tty" 348#define DEV_TTY "/dev/tty"
346 if ((tty_in=fopen(DEV_TTY,"r")) == NULL) 349 if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
347 tty_in=stdin; 350 tty_in = stdin;
348 if ((tty_out=fopen(DEV_TTY,"w")) == NULL) 351 if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
349 tty_out=stderr; 352 tty_out = stderr;
350 353
351#if defined(TTY_get) 354#if defined(TTY_get)
352 if (TTY_get(fileno(tty_in),&tty_orig) == -1) 355 if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
353 {
354 if (errno == ENOTTY) 356 if (errno == ENOTTY)
355 is_a_tty=0; 357 is_a_tty = 0;
356 else 358 else
357 /* Ariel Glenn ariel@columbia.edu reports that solaris 359 /*
358 * can return EINVAL instead. This should be ok */ 360 * Ariel Glenn ariel@columbia.edu reports that
361 * solaris can return EINVAL instead. This should be
362 * ok
363 */
359 if (errno == EINVAL) 364 if (errno == EINVAL)
360 is_a_tty=0; 365 is_a_tty = 0;
361 else 366 else
362 return 0; 367 return 0;
363 } 368 }
364#endif 369#endif
365 return 1; 370 return 1;
366 } 371}
367 372
368static int noecho_console(UI *ui) 373static int
369 { 374noecho_console(UI * ui)
375{
370#ifdef TTY_FLAGS 376#ifdef TTY_FLAGS
371 memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig)); 377 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
372 tty_new.TTY_FLAGS &= ~ECHO; 378 tty_new.TTY_FLAGS &= ~ECHO;
373#endif 379#endif
374 380
375#if defined(TTY_set) 381#if defined(TTY_set)
376 if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1)) 382 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
377 return 0; 383 return 0;
378#endif 384#endif
379 return 1; 385 return 1;
380 } 386}
381 387
382static int echo_console(UI *ui) 388static int
383 { 389echo_console(UI * ui)
390{
384#if defined(TTY_set) 391#if defined(TTY_set)
385 memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig)); 392 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
386 tty_new.TTY_FLAGS |= ECHO; 393 tty_new.TTY_FLAGS |= ECHO;
387#endif 394#endif
388 395
389#if defined(TTY_set) 396#if defined(TTY_set)
390 if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1)) 397 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
391 return 0; 398 return 0;
392#endif 399#endif
393 return 1; 400 return 1;
394 } 401}
395 402
396static int close_console(UI *ui) 403static int
397 { 404close_console(UI * ui)
398 if (tty_in != stdin) fclose(tty_in); 405{
399 if (tty_out != stderr) fclose(tty_out); 406 if (tty_in != stdin)
407 fclose(tty_in);
408 if (tty_out != stderr)
409 fclose(tty_out);
400 CRYPTO_w_unlock(CRYPTO_LOCK_UI); 410 CRYPTO_w_unlock(CRYPTO_LOCK_UI);
401 411
402 return 1; 412 return 1;
403 } 413}
404 414
405 415
406/* Internal functions to handle signals and act on them */ 416/* Internal functions to handle signals and act on them */
407static void pushsig(void) 417static void
408 { 418pushsig(void)
419{
409 int i; 420 int i;
410#ifdef SIGACTION 421#ifdef SIGACTION
411 struct sigaction sa; 422 struct sigaction sa;
412 423
413 memset(&sa,0,sizeof sa); 424 memset(&sa, 0, sizeof sa);
414 sa.sa_handler=recsig; 425 sa.sa_handler = recsig;
415#endif 426#endif
416 427
417 for (i=1; i<NX509_SIG; i++) 428 for (i = 1; i < NX509_SIG; i++) {
418 {
419#ifdef SIGUSR1 429#ifdef SIGUSR1
420 if (i == SIGUSR1) 430 if (i == SIGUSR1)
421 continue; 431 continue;
@@ -425,39 +435,40 @@ static void pushsig(void)
425 continue; 435 continue;
426#endif 436#endif
427#ifdef SIGKILL 437#ifdef SIGKILL
428 if (i == SIGKILL) /* We can't make any action on that. */ 438 if (i == SIGKILL) /* We can't make any action on that. */
429 continue; 439 continue;
430#endif 440#endif
431#ifdef SIGACTION 441#ifdef SIGACTION
432 sigaction(i,&sa,&savsig[i]); 442 sigaction(i, &sa, &savsig[i]);
433#else 443#else
434 savsig[i]=signal(i,recsig); 444 savsig[i] = signal(i, recsig);
435#endif 445#endif
436 } 446 }
437 447
438#ifdef SIGWINCH 448#ifdef SIGWINCH
439 signal(SIGWINCH,SIG_DFL); 449 signal(SIGWINCH, SIG_DFL);
440#endif 450#endif
441 } 451}
442 452
443static void popsig(void) 453static void
444 { 454popsig(void)
455{
445 int i; 456 int i;
446 for (i=1; i<NX509_SIG; i++) 457 for (i = 1; i < NX509_SIG; i++) {
447 {
448 if (i == SIGUSR1) 458 if (i == SIGUSR1)
449 continue; 459 continue;
450 if (i == SIGUSR2) 460 if (i == SIGUSR2)
451 continue; 461 continue;
452#ifdef SIGACTION 462#ifdef SIGACTION
453 sigaction(i,&savsig[i],NULL); 463 sigaction(i, &savsig[i], NULL);
454#else 464#else
455 signal(i,savsig[i]); 465 signal(i, savsig[i]);
456#endif 466#endif
457 }
458 } 467 }
468}
459 469
460static void recsig(int i) 470static void
461 { 471recsig(int i)
462 intr_signal=i; 472{
463 } 473 intr_signal = i;
474}