Convert CRYPTO_LOCK_UI to new multi-threading API
[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
83     ret->lock = CRYPTO_THREAD_lock_new();
84     if (ret->lock == NULL) {
85         UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
86         OPENSSL_free(ret);
87         return NULL;
88     }
89
90     if (method == NULL)
91         ret->meth = UI_get_default_method();
92     else
93         ret->meth = method;
94
95     CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data);
96     return ret;
97 }
98
99 static void free_string(UI_STRING *uis)
100 {
101     if (uis->flags & OUT_STRING_FREEABLE) {
102         OPENSSL_free((char *)uis->out_string);
103         switch (uis->type) {
104         case UIT_BOOLEAN:
105             OPENSSL_free((char *)uis->_.boolean_data.action_desc);
106             OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
107             OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
108             break;
109         default:
110             break;
111         }
112     }
113     OPENSSL_free(uis);
114 }
115
116 void UI_free(UI *ui)
117 {
118     if (ui == NULL)
119         return;
120     sk_UI_STRING_pop_free(ui->strings, free_string);
121     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
122     CRYPTO_THREAD_lock_free(ui->lock);
123     OPENSSL_free(ui);
124 }
125
126 static int allocate_string_stack(UI *ui)
127 {
128     if (ui->strings == NULL) {
129         ui->strings = sk_UI_STRING_new_null();
130         if (ui->strings == NULL) {
131             return -1;
132         }
133     }
134     return 0;
135 }
136
137 static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
138                                           int prompt_freeable,
139                                           enum UI_string_types type,
140                                           int input_flags, char *result_buf)
141 {
142     UI_STRING *ret = NULL;
143
144     if (prompt == NULL) {
145         UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
146     } else if ((type == UIT_PROMPT || type == UIT_VERIFY
147                 || type == UIT_BOOLEAN) && result_buf == NULL) {
148         UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
149     } else if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL) {
150         ret->out_string = prompt;
151         ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
152         ret->input_flags = input_flags;
153         ret->type = type;
154         ret->result_buf = result_buf;
155     }
156     return ret;
157 }
158
159 static int general_allocate_string(UI *ui, const char *prompt,
160                                    int prompt_freeable,
161                                    enum UI_string_types type, int input_flags,
162                                    char *result_buf, int minsize, int maxsize,
163                                    const char *test_buf)
164 {
165     int ret = -1;
166     UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
167                                            type, input_flags, result_buf);
168
169     if (s) {
170         if (allocate_string_stack(ui) >= 0) {
171             s->_.string_data.result_minsize = minsize;
172             s->_.string_data.result_maxsize = maxsize;
173             s->_.string_data.test_buf = test_buf;
174             ret = sk_UI_STRING_push(ui->strings, s);
175             /* sk_push() returns 0 on error.  Let's adapt that */
176             if (ret <= 0)
177                 ret--;
178         } else
179             free_string(s);
180     }
181     return ret;
182 }
183
184 static int general_allocate_boolean(UI *ui,
185                                     const char *prompt,
186                                     const char *action_desc,
187                                     const char *ok_chars,
188                                     const char *cancel_chars,
189                                     int prompt_freeable,
190                                     enum UI_string_types type,
191                                     int input_flags, char *result_buf)
192 {
193     int ret = -1;
194     UI_STRING *s;
195     const char *p;
196
197     if (ok_chars == NULL) {
198         UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
199     } else if (cancel_chars == NULL) {
200         UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
201     } else {
202         for (p = ok_chars; *p; p++) {
203             if (strchr(cancel_chars, *p)) {
204                 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
205                       UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
206             }
207         }
208
209         s = general_allocate_prompt(ui, prompt, prompt_freeable,
210                                     type, input_flags, result_buf);
211
212         if (s) {
213             if (allocate_string_stack(ui) >= 0) {
214                 s->_.boolean_data.action_desc = action_desc;
215                 s->_.boolean_data.ok_chars = ok_chars;
216                 s->_.boolean_data.cancel_chars = cancel_chars;
217                 ret = sk_UI_STRING_push(ui->strings, s);
218                 /*
219                  * sk_push() returns 0 on error. Let's adapt that
220                  */
221                 if (ret <= 0)
222                     ret--;
223             } else
224                 free_string(s);
225         }
226     }
227     return ret;
228 }
229
230 /*
231  * Returns the index to the place in the stack or -1 for error.  Uses a
232  * direct reference to the prompt.
233  */
234 int UI_add_input_string(UI *ui, const char *prompt, int flags,
235                         char *result_buf, int minsize, int maxsize)
236 {
237     return general_allocate_string(ui, prompt, 0,
238                                    UIT_PROMPT, flags, result_buf, minsize,
239                                    maxsize, NULL);
240 }
241
242 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
243 int UI_dup_input_string(UI *ui, const char *prompt, int flags,
244                         char *result_buf, int minsize, int maxsize)
245 {
246     char *prompt_copy = NULL;
247
248     if (prompt) {
249         prompt_copy = OPENSSL_strdup(prompt);
250         if (prompt_copy == NULL) {
251             UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
252             return 0;
253         }
254     }
255
256     return general_allocate_string(ui, prompt_copy, 1,
257                                    UIT_PROMPT, flags, result_buf, minsize,
258                                    maxsize, NULL);
259 }
260
261 int UI_add_verify_string(UI *ui, const char *prompt, int flags,
262                          char *result_buf, int minsize, int maxsize,
263                          const char *test_buf)
264 {
265     return general_allocate_string(ui, prompt, 0,
266                                    UIT_VERIFY, flags, result_buf, minsize,
267                                    maxsize, test_buf);
268 }
269
270 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
271                          char *result_buf, int minsize, int maxsize,
272                          const char *test_buf)
273 {
274     char *prompt_copy = NULL;
275
276     if (prompt) {
277         prompt_copy = OPENSSL_strdup(prompt);
278         if (prompt_copy == NULL) {
279             UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
280             return -1;
281         }
282     }
283
284     return general_allocate_string(ui, prompt_copy, 1,
285                                    UIT_VERIFY, flags, result_buf, minsize,
286                                    maxsize, test_buf);
287 }
288
289 int UI_add_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     return general_allocate_boolean(ui, prompt, action_desc,
294                                     ok_chars, cancel_chars, 0, UIT_BOOLEAN,
295                                     flags, result_buf);
296 }
297
298 int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
299                          const char *ok_chars, const char *cancel_chars,
300                          int flags, char *result_buf)
301 {
302     char *prompt_copy = NULL;
303     char *action_desc_copy = NULL;
304     char *ok_chars_copy = NULL;
305     char *cancel_chars_copy = NULL;
306
307     if (prompt) {
308         prompt_copy = OPENSSL_strdup(prompt);
309         if (prompt_copy == NULL) {
310             UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
311             goto err;
312         }
313     }
314
315     if (action_desc) {
316         action_desc_copy = OPENSSL_strdup(action_desc);
317         if (action_desc_copy == NULL) {
318             UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
319             goto err;
320         }
321     }
322
323     if (ok_chars) {
324         ok_chars_copy = OPENSSL_strdup(ok_chars);
325         if (ok_chars_copy == NULL) {
326             UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
327             goto err;
328         }
329     }
330
331     if (cancel_chars) {
332         cancel_chars_copy = OPENSSL_strdup(cancel_chars);
333         if (cancel_chars_copy == NULL) {
334             UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
335             goto err;
336         }
337     }
338
339     return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
340                                     ok_chars_copy, cancel_chars_copy, 1,
341                                     UIT_BOOLEAN, flags, result_buf);
342  err:
343     OPENSSL_free(prompt_copy);
344     OPENSSL_free(action_desc_copy);
345     OPENSSL_free(ok_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 = OPENSSL_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 = OPENSSL_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 = OPENSSL_malloc(len + 1);
414         if (prompt == NULL)
415             return NULL;
416         OPENSSL_strlcpy(prompt, prompt1, len + 1);
417         OPENSSL_strlcat(prompt, object_desc, len + 1);
418         if (object_name) {
419             OPENSSL_strlcat(prompt, prompt2, len + 1);
420             OPENSSL_strlcat(prompt, object_name, len + 1);
421         }
422         OPENSSL_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_set_ex_data(UI *r, int idx, void *arg)
548 {
549     return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
550 }
551
552 void *UI_get_ex_data(UI *r, int idx)
553 {
554     return (CRYPTO_get_ex_data(&r->ex_data, idx));
555 }
556
557 void UI_set_default_method(const UI_METHOD *meth)
558 {
559     default_UI_meth = meth;
560 }
561
562 const UI_METHOD *UI_get_default_method(void)
563 {
564     if (default_UI_meth == NULL) {
565         default_UI_meth = UI_OpenSSL();
566     }
567     return default_UI_meth;
568 }
569
570 const UI_METHOD *UI_get_method(UI *ui)
571 {
572     return ui->meth;
573 }
574
575 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
576 {
577     ui->meth = meth;
578     return ui->meth;
579 }
580
581 UI_METHOD *UI_create_method(char *name)
582 {
583     UI_METHOD *ui_method = OPENSSL_zalloc(sizeof(*ui_method));
584
585     if (ui_method != NULL)
586         ui_method->name = OPENSSL_strdup(name);
587     return ui_method;
588 }
589
590 /*
591  * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
592  * (that is, it hasn't been allocated using UI_create_method(), you deserve
593  * anything Murphy can throw at you and more! You have been warned.
594  */
595 void UI_destroy_method(UI_METHOD *ui_method)
596 {
597     OPENSSL_free(ui_method->name);
598     ui_method->name = NULL;
599     OPENSSL_free(ui_method);
600 }
601
602 int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui))
603 {
604     if (method) {
605         method->ui_open_session = opener;
606         return 0;
607     } else
608         return -1;
609 }
610
611 int UI_method_set_writer(UI_METHOD *method,
612                          int (*writer) (UI *ui, UI_STRING *uis))
613 {
614     if (method) {
615         method->ui_write_string = writer;
616         return 0;
617     } else
618         return -1;
619 }
620
621 int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
622 {
623     if (method) {
624         method->ui_flush = flusher;
625         return 0;
626     } else
627         return -1;
628 }
629
630 int UI_method_set_reader(UI_METHOD *method,
631                          int (*reader) (UI *ui, UI_STRING *uis))
632 {
633     if (method) {
634         method->ui_read_string = reader;
635         return 0;
636     } else
637         return -1;
638 }
639
640 int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
641 {
642     if (method) {
643         method->ui_close_session = closer;
644         return 0;
645     } else
646         return -1;
647 }
648
649 int UI_method_set_prompt_constructor(UI_METHOD *method,
650                                      char *(*prompt_constructor) (UI *ui,
651                                                                   const char
652                                                                   *object_desc,
653                                                                   const char
654                                                                   *object_name))
655 {
656     if (method) {
657         method->ui_construct_prompt = prompt_constructor;
658         return 0;
659     } else
660         return -1;
661 }
662
663 int (*UI_method_get_opener(UI_METHOD *method)) (UI *) {
664     if (method)
665         return method->ui_open_session;
666     else
667         return NULL;
668 }
669
670 int (*UI_method_get_writer(UI_METHOD *method)) (UI *, UI_STRING *) {
671     if (method)
672         return method->ui_write_string;
673     else
674         return NULL;
675 }
676
677 int (*UI_method_get_flusher(UI_METHOD *method)) (UI *) {
678     if (method)
679         return method->ui_flush;
680     else
681         return NULL;
682 }
683
684 int (*UI_method_get_reader(UI_METHOD *method)) (UI *, UI_STRING *) {
685     if (method)
686         return method->ui_read_string;
687     else
688         return NULL;
689 }
690
691 int (*UI_method_get_closer(UI_METHOD *method)) (UI *) {
692     if (method)
693         return method->ui_close_session;
694     else
695         return NULL;
696 }
697
698 char *(*UI_method_get_prompt_constructor(UI_METHOD *method)) (UI *,
699                                                               const char *,
700                                                               const char *) {
701     if (method)
702         return method->ui_construct_prompt;
703     else
704         return NULL;
705 }
706
707 enum UI_string_types UI_get_string_type(UI_STRING *uis)
708 {
709     if (!uis)
710         return UIT_NONE;
711     return uis->type;
712 }
713
714 int UI_get_input_flags(UI_STRING *uis)
715 {
716     if (!uis)
717         return 0;
718     return uis->input_flags;
719 }
720
721 const char *UI_get0_output_string(UI_STRING *uis)
722 {
723     if (!uis)
724         return NULL;
725     return uis->out_string;
726 }
727
728 const char *UI_get0_action_string(UI_STRING *uis)
729 {
730     if (!uis)
731         return NULL;
732     switch (uis->type) {
733     case UIT_PROMPT:
734     case UIT_BOOLEAN:
735         return uis->_.boolean_data.action_desc;
736     default:
737         return NULL;
738     }
739 }
740
741 const char *UI_get0_result_string(UI_STRING *uis)
742 {
743     if (!uis)
744         return NULL;
745     switch (uis->type) {
746     case UIT_PROMPT:
747     case UIT_VERIFY:
748         return uis->result_buf;
749     default:
750         return NULL;
751     }
752 }
753
754 const char *UI_get0_test_string(UI_STRING *uis)
755 {
756     if (!uis)
757         return NULL;
758     switch (uis->type) {
759     case UIT_VERIFY:
760         return uis->_.string_data.test_buf;
761     default:
762         return NULL;
763     }
764 }
765
766 int UI_get_result_minsize(UI_STRING *uis)
767 {
768     if (!uis)
769         return -1;
770     switch (uis->type) {
771     case UIT_PROMPT:
772     case UIT_VERIFY:
773         return uis->_.string_data.result_minsize;
774     default:
775         return -1;
776     }
777 }
778
779 int UI_get_result_maxsize(UI_STRING *uis)
780 {
781     if (!uis)
782         return -1;
783     switch (uis->type) {
784     case UIT_PROMPT:
785     case UIT_VERIFY:
786         return uis->_.string_data.result_maxsize;
787     default:
788         return -1;
789     }
790 }
791
792 int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
793 {
794     int l = strlen(result);
795
796     ui->flags &= ~UI_FLAG_REDOABLE;
797
798     if (!uis)
799         return -1;
800     switch (uis->type) {
801     case UIT_PROMPT:
802     case UIT_VERIFY:
803         {
804             char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
805             char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
806
807             BIO_snprintf(number1, sizeof(number1), "%d",
808                          uis->_.string_data.result_minsize);
809             BIO_snprintf(number2, sizeof(number2), "%d",
810                          uis->_.string_data.result_maxsize);
811
812             if (l < uis->_.string_data.result_minsize) {
813                 ui->flags |= UI_FLAG_REDOABLE;
814                 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL);
815                 ERR_add_error_data(5, "You must type in ",
816                                    number1, " to ", number2, " characters");
817                 return -1;
818             }
819             if (l > uis->_.string_data.result_maxsize) {
820                 ui->flags |= UI_FLAG_REDOABLE;
821                 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE);
822                 ERR_add_error_data(5, "You must type in ",
823                                    number1, " to ", number2, " characters");
824                 return -1;
825             }
826         }
827
828         if (!uis->result_buf) {
829             UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
830             return -1;
831         }
832
833         OPENSSL_strlcpy(uis->result_buf, result,
834                     uis->_.string_data.result_maxsize + 1);
835         break;
836     case UIT_BOOLEAN:
837         {
838             const char *p;
839
840             if (!uis->result_buf) {
841                 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
842                 return -1;
843             }
844
845             uis->result_buf[0] = '\0';
846             for (p = result; *p; p++) {
847                 if (strchr(uis->_.boolean_data.ok_chars, *p)) {
848                     uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
849                     break;
850                 }
851                 if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
852                     uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];
853                     break;
854                 }
855             }
856         }
857     default:
858         break;
859     }
860     return 0;
861 }