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