1 /* $OpenBSD: ui_lib.c,v 1.51 2023/02/16 08:38:17 tb Exp $ */
2 /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3 * 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 #include <string.h>
60
61 #include <openssl/opensslconf.h>
62
63 #include <openssl/buffer.h>
64 #include <openssl/err.h>
65 #include <openssl/ui.h>
66
67 #include "ui_local.h"
68
69 static const UI_METHOD *default_UI_meth = NULL;
70
71 UI *
UI_new(void)72 UI_new(void)
73 {
74 return (UI_new_method(NULL));
75 }
76 LCRYPTO_ALIAS(UI_new);
77
78 UI *
UI_new_method(const UI_METHOD * method)79 UI_new_method(const UI_METHOD *method)
80 {
81 UI *ret;
82
83 if ((ret = calloc(1, sizeof(UI))) == NULL) {
84 UIerror(ERR_R_MALLOC_FAILURE);
85 return NULL;
86 }
87 if ((ret->meth = method) == NULL)
88 ret->meth = UI_get_default_method();
89 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data);
90
91 return ret;
92 }
93 LCRYPTO_ALIAS(UI_new_method);
94
95 static void
free_string(UI_STRING * uis)96 free_string(UI_STRING *uis)
97 {
98 if (uis == NULL)
99 return;
100 if (uis->flags & OUT_STRING_FREEABLE) {
101 free((char *) uis->out_string);
102 switch (uis->type) {
103 case UIT_BOOLEAN:
104 free((char *)uis->_.boolean_data.action_desc);
105 free((char *)uis->_.boolean_data.ok_chars);
106 free((char *)uis->_.boolean_data.cancel_chars);
107 break;
108 default:
109 break;
110 }
111 }
112 free(uis);
113 }
114
115 void
UI_free(UI * ui)116 UI_free(UI *ui)
117 {
118 if (ui == NULL)
119 return;
120
121 sk_UI_STRING_pop_free(ui->strings, free_string);
122 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
123 free(ui);
124 }
125 LCRYPTO_ALIAS(UI_free);
126
127 static int
allocate_string_stack(UI * ui)128 allocate_string_stack(UI *ui)
129 {
130 if (ui->strings == NULL) {
131 if ((ui->strings = sk_UI_STRING_new_null()) == NULL) {
132 UIerror(ERR_R_MALLOC_FAILURE);
133 return -1;
134 }
135 }
136 return 0;
137 }
138
139 static UI_STRING *
general_allocate_prompt(const char * prompt,int dup_prompt,enum UI_string_types type,int input_flags,char * result_buf)140 general_allocate_prompt(const char *prompt, int dup_prompt,
141 enum UI_string_types type, int input_flags, char *result_buf)
142 {
143 UI_STRING *uis = NULL;
144
145 if (prompt == NULL) {
146 UIerror(ERR_R_PASSED_NULL_PARAMETER);
147 goto err;
148 }
149 if ((type == UIT_PROMPT || type == UIT_VERIFY || type == UIT_BOOLEAN) &&
150 result_buf == NULL) {
151 UIerror(UI_R_NO_RESULT_BUFFER);
152 goto err;
153 }
154
155 if ((uis = calloc(1, sizeof(UI_STRING))) == NULL) {
156 UIerror(ERR_R_MALLOC_FAILURE);
157 goto err;
158 }
159 uis->out_string = prompt;
160 if (dup_prompt) {
161 if ((uis->out_string = strdup(prompt)) == NULL) {
162 UIerror(ERR_R_MALLOC_FAILURE);
163 goto err;
164 }
165 uis->flags = OUT_STRING_FREEABLE;
166 }
167 uis->input_flags = input_flags;
168 uis->type = type;
169 uis->result_buf = result_buf;
170
171 return uis;
172
173 err:
174 free_string(uis);
175 return NULL;
176 }
177
178 static int
general_allocate_string(UI * ui,const char * prompt,int dup_prompt,enum UI_string_types type,int input_flags,char * result_buf,int minsize,int maxsize,const char * test_buf)179 general_allocate_string(UI *ui, const char *prompt, int dup_prompt,
180 enum UI_string_types type, int input_flags, char *result_buf, int minsize,
181 int maxsize, const char *test_buf)
182 {
183 UI_STRING *s;
184 int ret;
185
186 if ((s = general_allocate_prompt(prompt, dup_prompt, type, input_flags,
187 result_buf)) == NULL)
188 goto err;
189 s->_.string_data.result_minsize = minsize;
190 s->_.string_data.result_maxsize = maxsize;
191 s->_.string_data.test_buf = test_buf;
192
193 if (allocate_string_stack(ui) < 0)
194 goto err;
195 if ((ret = sk_UI_STRING_push(ui->strings, s)) <= 0)
196 goto err;
197
198 return ret;
199
200 err:
201 free_string(s);
202 return -1;
203 }
204
205 static int
general_allocate_boolean(UI * ui,const char * prompt,const char * action_desc,const char * ok_chars,const char * cancel_chars,int dup_strings,enum UI_string_types type,int input_flags,char * result_buf)206 general_allocate_boolean(UI *ui, const char *prompt, const char *action_desc,
207 const char *ok_chars, const char *cancel_chars, int dup_strings,
208 enum UI_string_types type, int input_flags, char *result_buf)
209 {
210 UI_STRING *s = NULL;
211 int ret;
212
213 if (ok_chars == NULL || cancel_chars == NULL) {
214 UIerror(ERR_R_PASSED_NULL_PARAMETER);
215 goto err;
216 }
217 if (ok_chars[strcspn(ok_chars, cancel_chars)] != '\0') {
218 UIerror(UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
219 goto err;
220 }
221
222 if ((s = general_allocate_prompt(prompt, dup_strings, type, input_flags,
223 result_buf)) == NULL)
224 goto err;
225
226 if (dup_strings) {
227 if (action_desc != NULL) {
228 if ((s->_.boolean_data.action_desc =
229 strdup(action_desc)) == NULL) {
230 UIerror(ERR_R_MALLOC_FAILURE);
231 goto err;
232 }
233 }
234 if ((s->_.boolean_data.ok_chars = strdup(ok_chars)) == NULL) {
235 UIerror(ERR_R_MALLOC_FAILURE);
236 goto err;
237 }
238 if ((s->_.boolean_data.cancel_chars = strdup(cancel_chars)) ==
239 NULL) {
240 UIerror(ERR_R_MALLOC_FAILURE);
241 goto err;
242 }
243 } else {
244 s->_.boolean_data.action_desc = action_desc;
245 s->_.boolean_data.ok_chars = ok_chars;
246 s->_.boolean_data.cancel_chars = cancel_chars;
247 }
248
249 if (allocate_string_stack(ui) < 0)
250 goto err;
251 if ((ret = sk_UI_STRING_push(ui->strings, s)) <= 0)
252 goto err;
253
254 return ret;
255
256 err:
257 free_string(s);
258 return -1;
259 }
260
261 /*
262 * Returns the index to the place in the stack or -1 for error. Uses a
263 * direct reference to the prompt.
264 */
265 int
UI_add_input_string(UI * ui,const char * prompt,int flags,char * result_buf,int minsize,int maxsize)266 UI_add_input_string(UI *ui, const char *prompt, int flags, char *result_buf,
267 int minsize, int maxsize)
268 {
269 return general_allocate_string(ui, prompt, 0, UIT_PROMPT, flags,
270 result_buf, minsize, maxsize, NULL);
271 }
272 LCRYPTO_ALIAS(UI_add_input_string);
273
274 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt. */
275 int
UI_dup_input_string(UI * ui,const char * prompt,int flags,char * result_buf,int minsize,int maxsize)276 UI_dup_input_string(UI *ui, const char *prompt, int flags, char *result_buf,
277 int minsize, int maxsize)
278 {
279 return general_allocate_string(ui, prompt, 1, UIT_PROMPT, flags,
280 result_buf, minsize, maxsize, NULL);
281 }
282 LCRYPTO_ALIAS(UI_dup_input_string);
283
284 int
UI_add_verify_string(UI * ui,const char * prompt,int flags,char * result_buf,int minsize,int maxsize,const char * test_buf)285 UI_add_verify_string(UI *ui, const char *prompt, int flags, char *result_buf,
286 int minsize, int maxsize, const char *test_buf)
287 {
288 return general_allocate_string(ui, prompt, 0, UIT_VERIFY, flags,
289 result_buf, minsize, maxsize, test_buf);
290 }
291 LCRYPTO_ALIAS(UI_add_verify_string);
292
293 int
UI_dup_verify_string(UI * ui,const char * prompt,int flags,char * result_buf,int minsize,int maxsize,const char * test_buf)294 UI_dup_verify_string(UI *ui, const char *prompt, int flags,
295 char *result_buf, int minsize, int maxsize, const char *test_buf)
296 {
297 return general_allocate_string(ui, prompt, 1, UIT_VERIFY, flags,
298 result_buf, minsize, maxsize, test_buf);
299 }
300 LCRYPTO_ALIAS(UI_dup_verify_string);
301
302 int
UI_add_input_boolean(UI * ui,const char * prompt,const char * action_desc,const char * ok_chars,const char * cancel_chars,int flags,char * result_buf)303 UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
304 const char *ok_chars, const char *cancel_chars, int flags, char *result_buf)
305 {
306 return general_allocate_boolean(ui, prompt, action_desc, ok_chars,
307 cancel_chars, 0, UIT_BOOLEAN, flags, result_buf);
308 }
309 LCRYPTO_ALIAS(UI_add_input_boolean);
310
311 int
UI_dup_input_boolean(UI * ui,const char * prompt,const char * action_desc,const char * ok_chars,const char * cancel_chars,int flags,char * result_buf)312 UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
313 const char *ok_chars, const char *cancel_chars, int flags, char *result_buf)
314 {
315 return general_allocate_boolean(ui, prompt, action_desc, ok_chars,
316 cancel_chars, 1, UIT_BOOLEAN, flags, result_buf);
317 }
318 LCRYPTO_ALIAS(UI_dup_input_boolean);
319
320 int
UI_add_info_string(UI * ui,const char * text)321 UI_add_info_string(UI *ui, const char *text)
322 {
323 return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
324 NULL);
325 }
326 LCRYPTO_ALIAS(UI_add_info_string);
327
328 int
UI_dup_info_string(UI * ui,const char * text)329 UI_dup_info_string(UI *ui, const char *text)
330 {
331 return general_allocate_string(ui, text, 1, UIT_INFO, 0, NULL, 0, 0,
332 NULL);
333 }
334 LCRYPTO_ALIAS(UI_dup_info_string);
335
336 int
UI_add_error_string(UI * ui,const char * text)337 UI_add_error_string(UI *ui, const char *text)
338 {
339 return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
340 NULL);
341 }
342 LCRYPTO_ALIAS(UI_add_error_string);
343
344 int
UI_dup_error_string(UI * ui,const char * text)345 UI_dup_error_string(UI *ui, const char *text)
346 {
347 return general_allocate_string(ui, text, 1, UIT_ERROR, 0, NULL, 0, 0,
348 NULL);
349 }
350 LCRYPTO_ALIAS(UI_dup_error_string);
351
352 char *
UI_construct_prompt(UI * ui,const char * object_desc,const char * object_name)353 UI_construct_prompt(UI *ui, const char *object_desc, const char *object_name)
354 {
355 char *prompt;
356
357 if (ui->meth->ui_construct_prompt)
358 return ui->meth->ui_construct_prompt(ui, object_desc,
359 object_name);
360
361 if (object_desc == NULL)
362 return NULL;
363
364 if (object_name == NULL) {
365 if (asprintf(&prompt, "Enter %s:", object_desc) == -1)
366 return (NULL);
367 } else {
368 if (asprintf(&prompt, "Enter %s for %s:", object_desc,
369 object_name) == -1)
370 return (NULL);
371 }
372
373 return prompt;
374 }
375 LCRYPTO_ALIAS(UI_construct_prompt);
376
377 void *
UI_add_user_data(UI * ui,void * user_data)378 UI_add_user_data(UI *ui, void *user_data)
379 {
380 void *old_data = ui->user_data;
381
382 ui->user_data = user_data;
383
384 return old_data;
385 }
386 LCRYPTO_ALIAS(UI_add_user_data);
387
388 void *
UI_get0_user_data(UI * ui)389 UI_get0_user_data(UI *ui)
390 {
391 return ui->user_data;
392 }
393 LCRYPTO_ALIAS(UI_get0_user_data);
394
395 const char *
UI_get0_result(UI * ui,int i)396 UI_get0_result(UI *ui, int i)
397 {
398 if (i < 0) {
399 UIerror(UI_R_INDEX_TOO_SMALL);
400 return NULL;
401 }
402 if (i >= sk_UI_STRING_num(ui->strings)) {
403 UIerror(UI_R_INDEX_TOO_LARGE);
404 return NULL;
405 }
406 return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
407 }
408 LCRYPTO_ALIAS(UI_get0_result);
409
410 static int
print_error(const char * str,size_t len,void * arg)411 print_error(const char *str, size_t len, void *arg)
412 {
413 UI *ui = arg;
414 UI_STRING uis;
415
416 memset(&uis, 0, sizeof(uis));
417 uis.type = UIT_ERROR;
418 uis.out_string = str;
419
420 if (ui->meth->ui_write_string &&
421 !ui->meth->ui_write_string(ui, &uis))
422 return -1;
423 return 0;
424 }
425
426 int
UI_process(UI * ui)427 UI_process(UI *ui)
428 {
429 int i, ok = 0;
430
431 if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
432 return -1;
433
434 if (ui->flags & UI_FLAG_PRINT_ERRORS)
435 ERR_print_errors_cb(print_error, ui);
436
437 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
438 if (ui->meth->ui_write_string &&
439 !ui->meth->ui_write_string(ui,
440 sk_UI_STRING_value(ui->strings, i))) {
441 ok = -1;
442 goto err;
443 }
444 }
445
446 if (ui->meth->ui_flush)
447 switch (ui->meth->ui_flush(ui)) {
448 case -1: /* Interrupt/Cancel/something... */
449 ok = -2;
450 goto err;
451 case 0: /* Errors */
452 ok = -1;
453 goto err;
454 default: /* Success */
455 ok = 0;
456 break;
457 }
458
459 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
460 if (ui->meth->ui_read_string) {
461 switch (ui->meth->ui_read_string(ui,
462 sk_UI_STRING_value(ui->strings, i))) {
463 case -1: /* Interrupt/Cancel/something... */
464 ui->flags &= ~UI_FLAG_REDOABLE;
465 ok = -2;
466 goto err;
467 case 0: /* Errors */
468 ok = -1;
469 goto err;
470 default: /* Success */
471 ok = 0;
472 break;
473 }
474 }
475 }
476
477 err:
478 if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
479 return -1;
480 return ok;
481 }
482 LCRYPTO_ALIAS(UI_process);
483
484 int
UI_ctrl(UI * ui,int cmd,long i,void * p,void (* f)(void))485 UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
486 {
487 if (ui == NULL) {
488 UIerror(ERR_R_PASSED_NULL_PARAMETER);
489 return -1;
490 }
491
492 switch (cmd) {
493 case UI_CTRL_PRINT_ERRORS:
494 {
495 int save_flag = !!(ui->flags & UI_FLAG_PRINT_ERRORS);
496 if (i)
497 ui->flags |= UI_FLAG_PRINT_ERRORS;
498 else
499 ui->flags &= ~UI_FLAG_PRINT_ERRORS;
500 return save_flag;
501 }
502 case UI_CTRL_IS_REDOABLE:
503 return !!(ui->flags & UI_FLAG_REDOABLE);
504 default:
505 break;
506 }
507 UIerror(UI_R_UNKNOWN_CONTROL_COMMAND);
508 return -1;
509 }
510 LCRYPTO_ALIAS(UI_ctrl);
511
512 int
UI_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)513 UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
514 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
515 {
516 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp,
517 new_func, dup_func, free_func);
518 }
519 LCRYPTO_ALIAS(UI_get_ex_new_index);
520
521 int
UI_set_ex_data(UI * r,int idx,void * arg)522 UI_set_ex_data(UI *r, int idx, void *arg)
523 {
524 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
525 }
526 LCRYPTO_ALIAS(UI_set_ex_data);
527
528 void *
UI_get_ex_data(UI * r,int idx)529 UI_get_ex_data(UI *r, int idx)
530 {
531 return (CRYPTO_get_ex_data(&r->ex_data, idx));
532 }
533 LCRYPTO_ALIAS(UI_get_ex_data);
534
535 void
UI_set_default_method(const UI_METHOD * method)536 UI_set_default_method(const UI_METHOD *method)
537 {
538 default_UI_meth = method;
539 }
540 LCRYPTO_ALIAS(UI_set_default_method);
541
542 const UI_METHOD *
UI_get_default_method(void)543 UI_get_default_method(void)
544 {
545 if (default_UI_meth == NULL)
546 default_UI_meth = UI_OpenSSL();
547
548 return default_UI_meth;
549 }
550 LCRYPTO_ALIAS(UI_get_default_method);
551
552 const UI_METHOD *
UI_get_method(UI * ui)553 UI_get_method(UI *ui)
554 {
555 return ui->meth;
556 }
557 LCRYPTO_ALIAS(UI_get_method);
558
559 const UI_METHOD *
UI_set_method(UI * ui,const UI_METHOD * method)560 UI_set_method(UI *ui, const UI_METHOD *method)
561 {
562 ui->meth = method;
563
564 return ui->meth;
565 }
566 LCRYPTO_ALIAS(UI_set_method);
567
568 UI_METHOD *
UI_create_method(const char * name)569 UI_create_method(const char *name)
570 {
571 UI_METHOD *method = NULL;
572
573 if ((method = calloc(1, sizeof(UI_METHOD))) == NULL)
574 goto err;
575
576 if (name != NULL) {
577 if ((method->name = strdup(name)) == NULL)
578 goto err;
579 }
580
581 return method;
582
583 err:
584 UI_destroy_method(method);
585
586 return NULL;
587 }
588 LCRYPTO_ALIAS(UI_create_method);
589
590 void
UI_destroy_method(UI_METHOD * method)591 UI_destroy_method(UI_METHOD *method)
592 {
593 if (method == NULL)
594 return;
595
596 free(method->name);
597 free(method);
598 }
599 LCRYPTO_ALIAS(UI_destroy_method);
600
601 int
UI_method_set_opener(UI_METHOD * method,int (* opener)(UI * ui))602 UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui))
603 {
604 if (method == NULL)
605 return -1;
606
607 method->ui_open_session = opener;
608
609 return 0;
610 }
611 LCRYPTO_ALIAS(UI_method_set_opener);
612
613 int
UI_method_set_writer(UI_METHOD * method,int (* writer)(UI * ui,UI_STRING * uis))614 UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis))
615 {
616 if (method == NULL)
617 return -1;
618
619 method->ui_write_string = writer;
620
621 return 0;
622 }
623 LCRYPTO_ALIAS(UI_method_set_writer);
624
625 int
UI_method_set_flusher(UI_METHOD * method,int (* flusher)(UI * ui))626 UI_method_set_flusher(UI_METHOD *method, int (*flusher)(UI *ui))
627 {
628 if (method == NULL)
629 return -1;
630
631 method->ui_flush = flusher;
632
633 return 0;
634 }
635 LCRYPTO_ALIAS(UI_method_set_flusher);
636
637 int
UI_method_set_reader(UI_METHOD * method,int (* reader)(UI * ui,UI_STRING * uis))638 UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis))
639 {
640 if (method == NULL)
641 return -1;
642
643 method->ui_read_string = reader;
644
645 return 0;
646 }
647 LCRYPTO_ALIAS(UI_method_set_reader);
648
649 int
UI_method_set_closer(UI_METHOD * method,int (* closer)(UI * ui))650 UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
651 {
652 if (method == NULL)
653 return -1;
654
655 method->ui_close_session = closer;
656
657 return 0;
658 }
659 LCRYPTO_ALIAS(UI_method_set_closer);
660
661 int
UI_method_set_prompt_constructor(UI_METHOD * method,char * (* prompt_constructor)(UI * ui,const char * object_desc,const char * object_name))662 UI_method_set_prompt_constructor(UI_METHOD *method,
663 char *(*prompt_constructor)(UI *ui, const char *object_desc,
664 const char *object_name))
665 {
666 if (method == NULL)
667 return -1;
668
669 method->ui_construct_prompt = prompt_constructor;
670
671 return 0;
672 }
673 LCRYPTO_ALIAS(UI_method_set_prompt_constructor);
674
675 int
UI_method_get_opener(const UI_METHOD * method)676 (*UI_method_get_opener(const UI_METHOD * method))(UI *)
677 {
678 if (method == NULL)
679 return NULL;
680
681 return method->ui_open_session;
682 }
683 LCRYPTO_ALIAS(UI_method_get_opener);
684
685 int
UI_method_get_writer(const UI_METHOD * method)686 (*UI_method_get_writer(const UI_METHOD *method))(UI *, UI_STRING *)
687 {
688 if (method == NULL)
689 return NULL;
690
691 return method->ui_write_string;
692 }
693 LCRYPTO_ALIAS(UI_method_get_writer);
694
695 int
UI_method_get_flusher(const UI_METHOD * method)696 (*UI_method_get_flusher(const UI_METHOD *method)) (UI *)
697 {
698 if (method == NULL)
699 return NULL;
700
701 return method->ui_flush;
702 }
703 LCRYPTO_ALIAS(UI_method_get_flusher);
704
705 int
UI_method_get_reader(const UI_METHOD * method)706 (*UI_method_get_reader(const UI_METHOD *method))(UI *, UI_STRING *)
707 {
708 if (method == NULL)
709 return NULL;
710
711 return method->ui_read_string;
712 }
713 LCRYPTO_ALIAS(UI_method_get_reader);
714
715 int
UI_method_get_closer(const UI_METHOD * method)716 (*UI_method_get_closer(const UI_METHOD *method))(UI *)
717 {
718 if (method == NULL)
719 return NULL;
720
721 return method->ui_close_session;
722 }
723 LCRYPTO_ALIAS(UI_method_get_closer);
724
725 char *
UI_method_get_prompt_constructor(const UI_METHOD * method)726 (*UI_method_get_prompt_constructor(const UI_METHOD *method))(UI *, const char *,
727 const char *)
728 {
729 if (method == NULL)
730 return NULL;
731
732 return method->ui_construct_prompt;
733 }
734 LCRYPTO_ALIAS(UI_method_get_prompt_constructor);
735
736 enum UI_string_types
UI_get_string_type(UI_STRING * uis)737 UI_get_string_type(UI_STRING *uis)
738 {
739 if (uis == NULL)
740 return UIT_NONE;
741
742 return uis->type;
743 }
744 LCRYPTO_ALIAS(UI_get_string_type);
745
746 int
UI_get_input_flags(UI_STRING * uis)747 UI_get_input_flags(UI_STRING *uis)
748 {
749 if (uis == NULL)
750 return 0;
751
752 return uis->input_flags;
753 }
754 LCRYPTO_ALIAS(UI_get_input_flags);
755
756 const char *
UI_get0_output_string(UI_STRING * uis)757 UI_get0_output_string(UI_STRING *uis)
758 {
759 if (uis == NULL)
760 return NULL;
761
762 return uis->out_string;
763 }
764 LCRYPTO_ALIAS(UI_get0_output_string);
765
766 const char *
UI_get0_action_string(UI_STRING * uis)767 UI_get0_action_string(UI_STRING *uis)
768 {
769 if (uis == NULL)
770 return NULL;
771
772 switch (uis->type) {
773 case UIT_PROMPT:
774 case UIT_BOOLEAN:
775 return uis->_.boolean_data.action_desc;
776 default:
777 return NULL;
778 }
779 }
780 LCRYPTO_ALIAS(UI_get0_action_string);
781
782 const char *
UI_get0_result_string(UI_STRING * uis)783 UI_get0_result_string(UI_STRING *uis)
784 {
785 if (uis == NULL)
786 return NULL;
787
788 switch (uis->type) {
789 case UIT_PROMPT:
790 case UIT_VERIFY:
791 return uis->result_buf;
792 default:
793 return NULL;
794 }
795 }
796 LCRYPTO_ALIAS(UI_get0_result_string);
797
798 const char *
UI_get0_test_string(UI_STRING * uis)799 UI_get0_test_string(UI_STRING *uis)
800 {
801 if (uis == NULL)
802 return NULL;
803
804 switch (uis->type) {
805 case UIT_VERIFY:
806 return uis->_.string_data.test_buf;
807 default:
808 return NULL;
809 }
810 }
811 LCRYPTO_ALIAS(UI_get0_test_string);
812
813 int
UI_get_result_minsize(UI_STRING * uis)814 UI_get_result_minsize(UI_STRING *uis)
815 {
816 if (uis == NULL)
817 return -1;
818
819 switch (uis->type) {
820 case UIT_PROMPT:
821 case UIT_VERIFY:
822 return uis->_.string_data.result_minsize;
823 default:
824 return -1;
825 }
826 }
827 LCRYPTO_ALIAS(UI_get_result_minsize);
828
829 int
UI_get_result_maxsize(UI_STRING * uis)830 UI_get_result_maxsize(UI_STRING *uis)
831 {
832 if (uis == NULL)
833 return -1;
834
835 switch (uis->type) {
836 case UIT_PROMPT:
837 case UIT_VERIFY:
838 return uis->_.string_data.result_maxsize;
839 default:
840 return -1;
841 }
842 }
843 LCRYPTO_ALIAS(UI_get_result_maxsize);
844
845 int
UI_set_result(UI * ui,UI_STRING * uis,const char * result)846 UI_set_result(UI *ui, UI_STRING *uis, const char *result)
847 {
848 const char *p;
849 int l = strlen(result);
850
851 ui->flags &= ~UI_FLAG_REDOABLE;
852
853 if (uis == NULL)
854 return -1;
855
856 switch (uis->type) {
857 case UIT_PROMPT:
858 case UIT_VERIFY:
859 if (l < uis->_.string_data.result_minsize) {
860 ui->flags |= UI_FLAG_REDOABLE;
861 UIerror(UI_R_RESULT_TOO_SMALL);
862 ERR_asprintf_error_data
863 ("You must type in %d to %d characters",
864 uis->_.string_data.result_minsize,
865 uis->_.string_data.result_maxsize);
866 return -1;
867 }
868 if (l > uis->_.string_data.result_maxsize) {
869 ui->flags |= UI_FLAG_REDOABLE;
870 UIerror(UI_R_RESULT_TOO_LARGE);
871 ERR_asprintf_error_data
872 ("You must type in %d to %d characters",
873 uis->_.string_data.result_minsize,
874 uis->_.string_data.result_maxsize);
875 return -1;
876 }
877 if (!uis->result_buf) {
878 UIerror(UI_R_NO_RESULT_BUFFER);
879 return -1;
880 }
881 strlcpy(uis->result_buf, result,
882 uis->_.string_data.result_maxsize + 1);
883 break;
884 case UIT_BOOLEAN:
885 if (!uis->result_buf) {
886 UIerror(UI_R_NO_RESULT_BUFFER);
887 return -1;
888 }
889 uis->result_buf[0] = '\0';
890 for (p = result; *p; p++) {
891 if (strchr(uis->_.boolean_data.ok_chars, *p)) {
892 uis->result_buf[0] =
893 uis->_.boolean_data.ok_chars[0];
894 break;
895 }
896 if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
897 uis->result_buf[0] =
898 uis->_.boolean_data.cancel_chars[0];
899 break;
900 }
901 }
902 default:
903 break;
904 }
905 return 0;
906 }
907 LCRYPTO_ALIAS(UI_set_result);
908