Cleanup some copyright stuff
[openssl.git] / crypto / ui / ui_lib.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/e_os2.h>
13 #include <openssl/buffer.h>
14 #include <openssl/ui.h>
15 #include <openssl/err.h>
16 #include "ui_locl.h"
17
18 UI *UI_new(void)
19 {
20     return (UI_new_method(NULL));
21 }
22
23 UI *UI_new_method(const UI_METHOD *method)
24 {
25     UI *ret = OPENSSL_zalloc(sizeof(*ret));
26
27     if (ret == NULL) {
28         UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
29         return NULL;
30     }
31
32     ret->lock = CRYPTO_THREAD_lock_new();
33     if (ret->lock == NULL) {
34         UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
35         OPENSSL_free(ret);
36         return NULL;
37     }
38
39     if (method == NULL)
40         ret->meth = UI_get_default_method();
41     else
42         ret->meth = method;
43
44     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data)) {
45         OPENSSL_free(ret);
46         return NULL;
47     }
48     return ret;
49 }
50
51 static void free_string(UI_STRING *uis)
52 {
53     if (uis->flags & OUT_STRING_FREEABLE) {
54         OPENSSL_free((char *)uis->out_string);
55         switch (uis->type) {
56         case UIT_BOOLEAN:
57             OPENSSL_free((char *)uis->_.boolean_data.action_desc);
58             OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
59             OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
60             break;
61         case UIT_NONE:
62         case UIT_PROMPT:
63         case UIT_VERIFY:
64         case UIT_ERROR:
65         case UIT_INFO:
66             break;
67         }
68     }
69     OPENSSL_free(uis);
70 }
71
72 void UI_free(UI *ui)
73 {
74     if (ui == NULL)
75         return;
76     if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
77         ui->meth->ui_destroy_data(ui, ui->user_data);
78     }
79     sk_UI_STRING_pop_free(ui->strings, free_string);
80     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
81     CRYPTO_THREAD_lock_free(ui->lock);
82     OPENSSL_free(ui);
83 }
84
85 static int allocate_string_stack(UI *ui)
86 {
87     if (ui->strings == NULL) {
88         ui->strings = sk_UI_STRING_new_null();
89         if (ui->strings == NULL) {
90             return -1;
91         }
92     }
93     return 0;
94 }
95
96 static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
97                                           int prompt_freeable,
98                                           enum UI_string_types type,
99                                           int input_flags, char *result_buf)
100 {
101     UI_STRING *ret = NULL;
102
103     if (prompt == NULL) {
104         UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
105     } else if ((type == UIT_PROMPT || type == UIT_VERIFY
106                 || type == UIT_BOOLEAN) && result_buf == NULL) {
107         UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
108     } else if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL) {
109         ret->out_string = prompt;
110         ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
111         ret->input_flags = input_flags;
112         ret->type = type;
113         ret->result_buf = result_buf;
114     }
115     return ret;
116 }
117
118 static int general_allocate_string(UI *ui, const char *prompt,
119                                    int prompt_freeable,
120                                    enum UI_string_types type, int input_flags,
121                                    char *result_buf, int minsize, int maxsize,
122                                    const char *test_buf)
123 {
124     int ret = -1;
125     UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
126                                            type, input_flags, result_buf);
127
128     if (s != NULL) {
129         if (allocate_string_stack(ui) >= 0) {
130             s->_.string_data.result_minsize = minsize;
131             s->_.string_data.result_maxsize = maxsize;
132             s->_.string_data.test_buf = test_buf;
133             ret = sk_UI_STRING_push(ui->strings, s);
134             /* sk_push() returns 0 on error.  Let's adapt that */
135             if (ret <= 0) {
136                 ret--;
137                 free_string(s);
138             }
139         } else
140             free_string(s);
141     }
142     return ret;
143 }
144
145 static int general_allocate_boolean(UI *ui,
146                                     const char *prompt,
147                                     const char *action_desc,
148                                     const char *ok_chars,
149                                     const char *cancel_chars,
150                                     int prompt_freeable,
151                                     enum UI_string_types type,
152                                     int input_flags, char *result_buf)
153 {
154     int ret = -1;
155     UI_STRING *s;
156     const char *p;
157
158     if (ok_chars == NULL) {
159         UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
160     } else if (cancel_chars == NULL) {
161         UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
162     } else {
163         for (p = ok_chars; *p != '\0'; p++) {
164             if (strchr(cancel_chars, *p) != NULL) {
165                 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
166                       UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
167             }
168         }
169
170         s = general_allocate_prompt(ui, prompt, prompt_freeable,
171                                     type, input_flags, result_buf);
172
173         if (s != NULL) {
174             if (allocate_string_stack(ui) >= 0) {
175                 s->_.boolean_data.action_desc = action_desc;
176                 s->_.boolean_data.ok_chars = ok_chars;
177                 s->_.boolean_data.cancel_chars = cancel_chars;
178                 ret = sk_UI_STRING_push(ui->strings, s);
179                 /*
180                  * sk_push() returns 0 on error. Let's adapt that
181                  */
182                 if (ret <= 0) {
183                     ret--;
184                     free_string(s);
185                 }
186             } else
187                 free_string(s);
188         }
189     }
190     return ret;
191 }
192
193 /*
194  * Returns the index to the place in the stack or -1 for error.  Uses a
195  * direct reference to the prompt.
196  */
197 int UI_add_input_string(UI *ui, const char *prompt, int flags,
198                         char *result_buf, int minsize, int maxsize)
199 {
200     return general_allocate_string(ui, prompt, 0,
201                                    UIT_PROMPT, flags, result_buf, minsize,
202                                    maxsize, NULL);
203 }
204
205 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
206 int UI_dup_input_string(UI *ui, const char *prompt, int flags,
207                         char *result_buf, int minsize, int maxsize)
208 {
209     char *prompt_copy = NULL;
210
211     if (prompt != NULL) {
212         prompt_copy = OPENSSL_strdup(prompt);
213         if (prompt_copy == NULL) {
214             UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
215             return 0;
216         }
217     }
218
219     return general_allocate_string(ui, prompt_copy, 1,
220                                    UIT_PROMPT, flags, result_buf, minsize,
221                                    maxsize, NULL);
222 }
223
224 int UI_add_verify_string(UI *ui, const char *prompt, int flags,
225                          char *result_buf, int minsize, int maxsize,
226                          const char *test_buf)
227 {
228     return general_allocate_string(ui, prompt, 0,
229                                    UIT_VERIFY, flags, result_buf, minsize,
230                                    maxsize, test_buf);
231 }
232
233 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
234                          char *result_buf, int minsize, int maxsize,
235                          const char *test_buf)
236 {
237     char *prompt_copy = NULL;
238
239     if (prompt != NULL) {
240         prompt_copy = OPENSSL_strdup(prompt);
241         if (prompt_copy == NULL) {
242             UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
243             return -1;
244         }
245     }
246
247     return general_allocate_string(ui, prompt_copy, 1,
248                                    UIT_VERIFY, flags, result_buf, minsize,
249                                    maxsize, test_buf);
250 }
251
252 int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
253                          const char *ok_chars, const char *cancel_chars,
254                          int flags, char *result_buf)
255 {
256     return general_allocate_boolean(ui, prompt, action_desc,
257                                     ok_chars, cancel_chars, 0, UIT_BOOLEAN,
258                                     flags, result_buf);
259 }
260
261 int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
262                          const char *ok_chars, const char *cancel_chars,
263                          int flags, char *result_buf)
264 {
265     char *prompt_copy = NULL;
266     char *action_desc_copy = NULL;
267     char *ok_chars_copy = NULL;
268     char *cancel_chars_copy = NULL;
269
270     if (prompt != NULL) {
271         prompt_copy = OPENSSL_strdup(prompt);
272         if (prompt_copy == NULL) {
273             UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
274             goto err;
275         }
276     }
277
278     if (action_desc != NULL) {
279         action_desc_copy = OPENSSL_strdup(action_desc);
280         if (action_desc_copy == NULL) {
281             UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
282             goto err;
283         }
284     }
285
286     if (ok_chars != NULL) {
287         ok_chars_copy = OPENSSL_strdup(ok_chars);
288         if (ok_chars_copy == NULL) {
289             UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
290             goto err;
291         }
292     }
293
294     if (cancel_chars != NULL) {
295         cancel_chars_copy = OPENSSL_strdup(cancel_chars);
296         if (cancel_chars_copy == NULL) {
297             UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
298             goto err;
299         }
300     }
301
302     return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
303                                     ok_chars_copy, cancel_chars_copy, 1,
304                                     UIT_BOOLEAN, flags, result_buf);
305  err:
306     OPENSSL_free(prompt_copy);
307     OPENSSL_free(action_desc_copy);
308     OPENSSL_free(ok_chars_copy);
309     OPENSSL_free(cancel_chars_copy);
310     return -1;
311 }
312
313 int UI_add_info_string(UI *ui, const char *text)
314 {
315     return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
316                                    NULL);
317 }
318
319 int UI_dup_info_string(UI *ui, const char *text)
320 {
321     char *text_copy = NULL;
322
323     if (text != NULL) {
324         text_copy = OPENSSL_strdup(text);
325         if (text_copy == NULL) {
326             UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
327             return -1;
328         }
329     }
330
331     return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
332                                    0, 0, NULL);
333 }
334
335 int UI_add_error_string(UI *ui, const char *text)
336 {
337     return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
338                                    NULL);
339 }
340
341 int UI_dup_error_string(UI *ui, const char *text)
342 {
343     char *text_copy = NULL;
344
345     if (text != NULL) {
346         text_copy = OPENSSL_strdup(text);
347         if (text_copy == NULL) {
348             UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
349             return -1;
350         }
351     }
352     return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
353                                    0, 0, NULL);
354 }
355
356 char *UI_construct_prompt(UI *ui, const char *object_desc,
357                           const char *object_name)
358 {
359     char *prompt = NULL;
360
361     if (ui->meth->ui_construct_prompt != NULL)
362         prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
363     else {
364         char prompt1[] = "Enter ";
365         char prompt2[] = " for ";
366         char prompt3[] = ":";
367         int len = 0;
368
369         if (object_desc == NULL)
370             return NULL;
371         len = sizeof(prompt1) - 1 + strlen(object_desc);
372         if (object_name != NULL)
373             len += sizeof(prompt2) - 1 + strlen(object_name);
374         len += sizeof(prompt3) - 1;
375
376         prompt = OPENSSL_malloc(len + 1);
377         if (prompt == NULL)
378             return NULL;
379         OPENSSL_strlcpy(prompt, prompt1, len + 1);
380         OPENSSL_strlcat(prompt, object_desc, len + 1);
381         if (object_name != NULL) {
382             OPENSSL_strlcat(prompt, prompt2, len + 1);
383             OPENSSL_strlcat(prompt, object_name, len + 1);
384         }
385         OPENSSL_strlcat(prompt, prompt3, len + 1);
386     }
387     return prompt;
388 }
389
390 void *UI_add_user_data(UI *ui, void *user_data)
391 {
392     void *old_data = ui->user_data;
393
394     if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
395         ui->meth->ui_destroy_data(ui, old_data);
396         old_data = NULL;
397     }
398     ui->user_data = user_data;
399     ui->flags &= ~UI_FLAG_DUPL_DATA;
400     return old_data;
401 }
402
403 int UI_dup_user_data(UI *ui, void *user_data)
404 {
405     void *duplicate = NULL;
406
407     if (ui->meth->ui_duplicate_data == NULL
408         || ui->meth->ui_destroy_data == NULL) {
409         UIerr(UI_F_UI_DUP_USER_DATA, UI_R_USER_DATA_DUPLICATION_UNSUPPORTED);
410         return -1;
411     }
412
413     duplicate = ui->meth->ui_duplicate_data(ui, user_data);
414     if (duplicate == NULL) {
415         UIerr(UI_F_UI_DUP_USER_DATA, ERR_R_MALLOC_FAILURE);
416         return -1;
417     }
418
419     (void)UI_add_user_data(ui, duplicate);
420     ui->flags |= UI_FLAG_DUPL_DATA;
421
422     return 0;
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 != NULL
452         && ui->meth->ui_write_string(ui, &uis) <= 0)
453         return -1;
454     return 0;
455 }
456
457 int UI_process(UI *ui)
458 {
459     int i, ok = 0;
460     const char *state = "processing";
461
462     if (ui->meth->ui_open_session != NULL
463         && ui->meth->ui_open_session(ui) <= 0) {
464         state = "opening session";
465         ok = -1;
466         goto err;
467     }
468
469     if (ui->flags & UI_FLAG_PRINT_ERRORS)
470         ERR_print_errors_cb((int (*)(const char *, size_t, void *))
471                             print_error, (void *)ui);
472
473     for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
474         if (ui->meth->ui_write_string != NULL
475             && (ui->meth->ui_write_string(ui,
476                                           sk_UI_STRING_value(ui->strings, i))
477                 <= 0))
478         {
479             state = "writing strings";
480             ok = -1;
481             goto err;
482         }
483     }
484
485     if (ui->meth->ui_flush != NULL)
486         switch (ui->meth->ui_flush(ui)) {
487         case -1:               /* Interrupt/Cancel/something... */
488             ok = -2;
489             goto err;
490         case 0:                /* Errors */
491             state = "flushing";
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 != NULL) {
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                 state = "reading strings";
509                 ok = -1;
510                 goto err;
511             default:           /* Success */
512                 ok = 0;
513                 break;
514             }
515         }
516     }
517  err:
518     if (ui->meth->ui_close_session != NULL
519         && ui->meth->ui_close_session(ui) <= 0) {
520         if (state == NULL)
521             state = "closing session";
522         ok = -1;
523     }
524
525     if (ok == -1) {
526         UIerr(UI_F_UI_PROCESS, UI_R_PROCESSING_ERROR);
527         ERR_add_error_data(2, "while ", state);
528     }
529     return ok;
530 }
531
532 int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
533 {
534     if (ui == NULL) {
535         UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
536         return -1;
537     }
538     switch (cmd) {
539     case UI_CTRL_PRINT_ERRORS:
540         {
541             int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS);
542             if (i)
543                 ui->flags |= UI_FLAG_PRINT_ERRORS;
544             else
545                 ui->flags &= ~UI_FLAG_PRINT_ERRORS;
546             return save_flag;
547         }
548     case UI_CTRL_IS_REDOABLE:
549         return ! !(ui->flags & UI_FLAG_REDOABLE);
550     default:
551         break;
552     }
553     UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
554     return -1;
555 }
556
557 int UI_set_ex_data(UI *r, int idx, void *arg)
558 {
559     return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
560 }
561
562 void *UI_get_ex_data(UI *r, int idx)
563 {
564     return (CRYPTO_get_ex_data(&r->ex_data, idx));
565 }
566
567 const UI_METHOD *UI_get_method(UI *ui)
568 {
569     return ui->meth;
570 }
571
572 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
573 {
574     ui->meth = meth;
575     return ui->meth;
576 }
577
578 UI_METHOD *UI_create_method(const char *name)
579 {
580     UI_METHOD *ui_method = NULL;
581
582     if ((ui_method = OPENSSL_zalloc(sizeof(*ui_method))) == NULL
583         || (ui_method->name = OPENSSL_strdup(name)) == NULL
584         || !CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
585                                &ui_method->ex_data)) {
586         if (ui_method)
587             OPENSSL_free(ui_method->name);
588         OPENSSL_free(ui_method);
589         UIerr(UI_F_UI_CREATE_METHOD, ERR_R_MALLOC_FAILURE);
590         return NULL;
591     }
592     return ui_method;
593 }
594
595 /*
596  * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
597  * (that is, it hasn't been allocated using UI_create_method(), you deserve
598  * anything Murphy can throw at you and more! You have been warned.
599  */
600 void UI_destroy_method(UI_METHOD *ui_method)
601 {
602     if (ui_method == NULL)
603         return;
604     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
605                         &ui_method->ex_data);
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 != NULL) {
614         method->ui_open_session = opener;
615         return 0;
616     }
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 != NULL) {
624         method->ui_write_string = writer;
625         return 0;
626     }
627     return -1;
628 }
629
630 int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
631 {
632     if (method != NULL) {
633         method->ui_flush = flusher;
634         return 0;
635     }
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 != NULL) {
643         method->ui_read_string = reader;
644         return 0;
645     }
646     return -1;
647 }
648
649 int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
650 {
651     if (method != NULL) {
652         method->ui_close_session = closer;
653         return 0;
654     }
655     return -1;
656 }
657
658 int UI_method_set_data_duplicator(UI_METHOD *method,
659                                   void *(*duplicator) (UI *ui, void *ui_data),
660                                   void (*destructor)(UI *ui, void *ui_data))
661 {
662     if (method != NULL) {
663         method->ui_duplicate_data = duplicator;
664         method->ui_destroy_data = destructor;
665         return 0;
666     }
667     return -1;
668 }
669
670 int UI_method_set_prompt_constructor(UI_METHOD *method,
671                                      char *(*prompt_constructor) (UI *ui,
672                                                                   const char
673                                                                   *object_desc,
674                                                                   const char
675                                                                   *object_name))
676 {
677     if (method != NULL) {
678         method->ui_construct_prompt = prompt_constructor;
679         return 0;
680     }
681     return -1;
682 }
683
684 int UI_method_set_ex_data(UI_METHOD *method, int idx, void *data)
685 {
686     return CRYPTO_set_ex_data(&method->ex_data, idx, data);
687 }
688
689 int (*UI_method_get_opener(const UI_METHOD *method)) (UI *)
690 {
691     if (method != NULL)
692         return method->ui_open_session;
693     return NULL;
694 }
695
696 int (*UI_method_get_writer(const UI_METHOD *method)) (UI *, UI_STRING *)
697 {
698     if (method != NULL)
699         return method->ui_write_string;
700     return NULL;
701 }
702
703 int (*UI_method_get_flusher(const UI_METHOD *method)) (UI *)
704 {
705     if (method != NULL)
706         return method->ui_flush;
707     return NULL;
708 }
709
710 int (*UI_method_get_reader(const UI_METHOD *method)) (UI *, UI_STRING *)
711 {
712     if (method != NULL)
713         return method->ui_read_string;
714     return NULL;
715 }
716
717 int (*UI_method_get_closer(const UI_METHOD *method)) (UI *)
718 {
719     if (method != NULL)
720         return method->ui_close_session;
721     return NULL;
722 }
723
724 char *(*UI_method_get_prompt_constructor(const UI_METHOD *method))
725     (UI *, const char *, const char *)
726 {
727     if (method != NULL)
728         return method->ui_construct_prompt;
729     return NULL;
730 }
731
732 void *(*UI_method_get_data_duplicator(const UI_METHOD *method)) (UI *, void *)
733 {
734     if (method != NULL)
735         return method->ui_duplicate_data;
736     return NULL;
737 }
738
739 void (*UI_method_get_data_destructor(const UI_METHOD *method)) (UI *, void *)
740 {
741     if (method != NULL)
742         return method->ui_destroy_data;
743     return NULL;
744 }
745
746 const void *UI_method_get_ex_data(const UI_METHOD *method, int idx)
747 {
748     return CRYPTO_get_ex_data(&method->ex_data, idx);
749 }
750
751 enum UI_string_types UI_get_string_type(UI_STRING *uis)
752 {
753     return uis->type;
754 }
755
756 int UI_get_input_flags(UI_STRING *uis)
757 {
758     return uis->input_flags;
759 }
760
761 const char *UI_get0_output_string(UI_STRING *uis)
762 {
763     return uis->out_string;
764 }
765
766 const char *UI_get0_action_string(UI_STRING *uis)
767 {
768     switch (uis->type) {
769     case UIT_BOOLEAN:
770         return uis->_.boolean_data.action_desc;
771     case UIT_PROMPT:
772     case UIT_NONE:
773     case UIT_VERIFY:
774     case UIT_INFO:
775     case UIT_ERROR:
776         break;
777     }
778     return NULL;
779 }
780
781 const char *UI_get0_result_string(UI_STRING *uis)
782 {
783     switch (uis->type) {
784     case UIT_PROMPT:
785     case UIT_VERIFY:
786         return uis->result_buf;
787     case UIT_NONE:
788     case UIT_BOOLEAN:
789     case UIT_INFO:
790     case UIT_ERROR:
791         break;
792     }
793     return NULL;
794 }
795
796 const char *UI_get0_test_string(UI_STRING *uis)
797 {
798     switch (uis->type) {
799     case UIT_VERIFY:
800         return uis->_.string_data.test_buf;
801     case UIT_NONE:
802     case UIT_BOOLEAN:
803     case UIT_INFO:
804     case UIT_ERROR:
805     case UIT_PROMPT:
806         break;
807     }
808     return NULL;
809 }
810
811 int UI_get_result_minsize(UI_STRING *uis)
812 {
813     switch (uis->type) {
814     case UIT_PROMPT:
815     case UIT_VERIFY:
816         return uis->_.string_data.result_minsize;
817     case UIT_NONE:
818     case UIT_INFO:
819     case UIT_ERROR:
820     case UIT_BOOLEAN:
821         break;
822     }
823     return -1;
824 }
825
826 int UI_get_result_maxsize(UI_STRING *uis)
827 {
828     switch (uis->type) {
829     case UIT_PROMPT:
830     case UIT_VERIFY:
831         return uis->_.string_data.result_maxsize;
832     case UIT_NONE:
833     case UIT_INFO:
834     case UIT_ERROR:
835     case UIT_BOOLEAN:
836         break;
837     }
838     return -1;
839 }
840
841 int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
842 {
843     int l = strlen(result);
844
845     ui->flags &= ~UI_FLAG_REDOABLE;
846
847     switch (uis->type) {
848     case UIT_PROMPT:
849     case UIT_VERIFY:
850         {
851             char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
852             char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
853
854             BIO_snprintf(number1, sizeof(number1), "%d",
855                          uis->_.string_data.result_minsize);
856             BIO_snprintf(number2, sizeof(number2), "%d",
857                          uis->_.string_data.result_maxsize);
858
859             if (l < uis->_.string_data.result_minsize) {
860                 ui->flags |= UI_FLAG_REDOABLE;
861                 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL);
862                 ERR_add_error_data(5, "You must type in ",
863                                    number1, " to ", number2, " characters");
864                 return -1;
865             }
866             if (l > uis->_.string_data.result_maxsize) {
867                 ui->flags |= UI_FLAG_REDOABLE;
868                 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE);
869                 ERR_add_error_data(5, "You must type in ",
870                                    number1, " to ", number2, " characters");
871                 return -1;
872             }
873         }
874
875         if (uis->result_buf == NULL) {
876             UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
877             return -1;
878         }
879
880         OPENSSL_strlcpy(uis->result_buf, result,
881                     uis->_.string_data.result_maxsize + 1);
882         break;
883     case UIT_BOOLEAN:
884         {
885             const char *p;
886
887             if (uis->result_buf == NULL) {
888                 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
889                 return -1;
890             }
891
892             uis->result_buf[0] = '\0';
893             for (p = result; *p; p++) {
894                 if (strchr(uis->_.boolean_data.ok_chars, *p)) {
895                     uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
896                     break;
897                 }
898                 if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
899                     uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];
900                     break;
901                 }
902             }
903         }
904     case UIT_NONE:
905     case UIT_INFO:
906     case UIT_ERROR:
907         break;
908     }
909     return 0;
910 }