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