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