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