1 /* $OpenBSD: ui_lib.c,v 1.30 2015/02/10 11:22:21 jsing Exp $ */
2 /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
61 #include <openssl/opensslconf.h>
63 #include <openssl/buffer.h>
64 #include <openssl/err.h>
65 #include <openssl/ui.h>
69 static const UI_METHOD *default_UI_meth = NULL;
74 return (UI_new_method(NULL));
78 UI_new_method(const UI_METHOD *method)
82 ret = malloc(sizeof(UI));
84 UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
88 ret->meth = UI_get_default_method();
93 ret->user_data = NULL;
95 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data);
100 free_string(UI_STRING *uis)
102 if (uis->flags & OUT_STRING_FREEABLE) {
103 free((char *) uis->out_string);
106 free((char *)uis->_.boolean_data.action_desc);
107 free((char *)uis->_.boolean_data.ok_chars);
108 free((char *)uis->_.boolean_data.cancel_chars);
122 sk_UI_STRING_pop_free(ui->strings, free_string);
123 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
128 allocate_string_stack(UI *ui)
130 if (ui->strings == NULL) {
131 ui->strings = sk_UI_STRING_new_null();
132 if (ui->strings == NULL) {
140 general_allocate_prompt(UI *ui, const char *prompt, int prompt_freeable,
141 enum UI_string_types type, int input_flags, char *result_buf)
143 UI_STRING *ret = NULL;
145 if (prompt == NULL) {
146 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,
147 ERR_R_PASSED_NULL_PARAMETER);
148 } else if ((type == UIT_PROMPT || type == UIT_VERIFY ||
149 type == UIT_BOOLEAN) && result_buf == NULL) {
150 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
151 } else if ((ret = malloc(sizeof(UI_STRING)))) {
152 ret->out_string = prompt;
153 ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
154 ret->input_flags = input_flags;
156 ret->result_buf = result_buf;
162 general_allocate_string(UI *ui, const char *prompt, int prompt_freeable,
163 enum UI_string_types type, int input_flags, char *result_buf, int minsize,
164 int maxsize, const char *test_buf)
167 UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
168 type, input_flags, result_buf);
171 if (allocate_string_stack(ui) >= 0) {
172 s->_.string_data.result_minsize = minsize;
173 s->_.string_data.result_maxsize = maxsize;
174 s->_.string_data.test_buf = test_buf;
175 ret = sk_UI_STRING_push(ui->strings, s);
176 /* sk_push() returns 0 on error. Let's adapt that */
186 general_allocate_boolean(UI *ui, const char *prompt, const char *action_desc,
187 const char *ok_chars, const char *cancel_chars, int prompt_freeable,
188 enum UI_string_types type, int input_flags, char *result_buf)
194 if (ok_chars == NULL) {
195 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
196 ERR_R_PASSED_NULL_PARAMETER);
197 } else if (cancel_chars == NULL) {
198 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
199 ERR_R_PASSED_NULL_PARAMETER);
201 for (p = ok_chars; *p; p++) {
202 if (strchr(cancel_chars, *p)) {
203 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
204 UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
208 s = general_allocate_prompt(ui, prompt, prompt_freeable,
209 type, input_flags, result_buf);
212 if (allocate_string_stack(ui) >= 0) {
213 s->_.boolean_data.action_desc = action_desc;
214 s->_.boolean_data.ok_chars = ok_chars;
215 s->_.boolean_data.cancel_chars = cancel_chars;
216 ret = sk_UI_STRING_push(ui->strings, s);
218 * sk_push() returns 0 on error. Let's adapt
230 /* Returns the index to the place in the stack or -1 for error. Uses a
231 direct reference to the prompt. */
233 UI_add_input_string(UI *ui, const char *prompt, int flags, char *result_buf,
234 int minsize, int maxsize)
236 return general_allocate_string(ui, prompt, 0, UIT_PROMPT, flags,
237 result_buf, minsize, maxsize, NULL);
240 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
242 UI_dup_input_string(UI *ui, const char *prompt, int flags, char *result_buf,
243 int minsize, int maxsize)
245 char *prompt_copy = NULL;
248 prompt_copy = strdup(prompt);
249 if (prompt_copy == NULL) {
250 UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
254 return general_allocate_string(ui, prompt_copy, 1, UIT_PROMPT, flags,
255 result_buf, minsize, maxsize, NULL);
259 UI_add_verify_string(UI *ui, const char *prompt, int flags, char *result_buf,
260 int minsize, int maxsize, const char *test_buf)
262 return general_allocate_string(ui, prompt, 0, UIT_VERIFY, flags,
263 result_buf, minsize, maxsize, test_buf);
267 UI_dup_verify_string(UI *ui, const char *prompt, int flags,
268 char *result_buf, int minsize, int maxsize, const char *test_buf)
270 char *prompt_copy = NULL;
273 prompt_copy = strdup(prompt);
274 if (prompt_copy == NULL) {
275 UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
279 return general_allocate_string(ui, prompt_copy, 1, UIT_VERIFY, flags,
280 result_buf, minsize, maxsize, test_buf);
284 UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
285 const char *ok_chars, const char *cancel_chars, int flags, char *result_buf)
287 return general_allocate_boolean(ui, prompt, action_desc, ok_chars,
288 cancel_chars, 0, UIT_BOOLEAN, flags, result_buf);
292 UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
293 const char *ok_chars, const char *cancel_chars, int flags, char *result_buf)
295 char *prompt_copy = NULL;
296 char *action_desc_copy = NULL;
297 char *ok_chars_copy = NULL;
298 char *cancel_chars_copy = NULL;
301 prompt_copy = strdup(prompt);
302 if (prompt_copy == NULL) {
303 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
308 action_desc_copy = strdup(action_desc);
309 if (action_desc_copy == NULL) {
310 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
315 ok_chars_copy = strdup(ok_chars);
316 if (ok_chars_copy == NULL) {
317 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
322 cancel_chars_copy = strdup(cancel_chars);
323 if (cancel_chars_copy == NULL) {
324 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
328 return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
329 ok_chars_copy, cancel_chars_copy, 1, UIT_BOOLEAN, flags,
334 free(action_desc_copy);
336 free(cancel_chars_copy);
341 UI_add_info_string(UI *ui, const char *text)
343 return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
348 UI_dup_info_string(UI *ui, const char *text)
350 char *text_copy = NULL;
353 text_copy = strdup(text);
354 if (text_copy == NULL) {
355 UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
359 return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
364 UI_add_error_string(UI *ui, const char *text)
366 return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
371 UI_dup_error_string(UI *ui, const char *text)
373 char *text_copy = NULL;
376 text_copy = strdup(text);
377 if (text_copy == NULL) {
378 UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
382 return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
387 UI_construct_prompt(UI *ui, const char *object_desc, const char *object_name)
391 if (ui->meth->ui_construct_prompt)
392 return ui->meth->ui_construct_prompt(ui, object_desc,
395 if (object_desc == NULL)
398 if (object_name == NULL) {
399 if (asprintf(&prompt, "Enter %s:", object_desc) == -1)
402 if (asprintf(&prompt, "Enter %s for %s:", object_desc,
411 UI_add_user_data(UI *ui, void *user_data)
413 void *old_data = ui->user_data;
415 ui->user_data = user_data;
420 UI_get0_user_data(UI *ui)
422 return ui->user_data;
426 UI_get0_result(UI *ui, int i)
429 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL);
432 if (i >= sk_UI_STRING_num(ui->strings)) {
433 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE);
436 return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
440 print_error(const char *str, size_t len, UI *ui)
444 memset(&uis, 0, sizeof(uis));
445 uis.type = UIT_ERROR;
446 uis.out_string = str;
448 if (ui->meth->ui_write_string &&
449 !ui->meth->ui_write_string(ui, &uis))
459 if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
462 if (ui->flags & UI_FLAG_PRINT_ERRORS)
464 (int (*)(const char *, size_t, void *)) print_error,
467 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
468 if (ui->meth->ui_write_string &&
469 !ui->meth->ui_write_string(ui,
470 sk_UI_STRING_value(ui->strings, i))) {
476 if (ui->meth->ui_flush)
477 switch (ui->meth->ui_flush(ui)) {
478 case -1: /* Interrupt/Cancel/something... */
484 default: /* Success */
489 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
490 if (ui->meth->ui_read_string) {
491 switch (ui->meth->ui_read_string(ui,
492 sk_UI_STRING_value(ui->strings, i))) {
493 case -1: /* Interrupt/Cancel/something... */
494 ui->flags &= ~UI_FLAG_REDOABLE;
500 default: /* Success */
508 if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
514 UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
517 UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
521 case UI_CTRL_PRINT_ERRORS:
523 int save_flag = !!(ui->flags & UI_FLAG_PRINT_ERRORS);
525 ui->flags |= UI_FLAG_PRINT_ERRORS;
527 ui->flags &= ~UI_FLAG_PRINT_ERRORS;
530 case UI_CTRL_IS_REDOABLE:
531 return !!(ui->flags & UI_FLAG_REDOABLE);
535 UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
540 UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
541 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
543 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp,
544 new_func, dup_func, free_func);
548 UI_set_ex_data(UI *r, int idx, void *arg)
550 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
554 UI_get_ex_data(UI *r, int idx)
556 return (CRYPTO_get_ex_data(&r->ex_data, idx));
560 UI_set_default_method(const UI_METHOD *meth)
562 default_UI_meth = meth;
566 UI_get_default_method(void)
568 if (default_UI_meth == NULL) {
569 default_UI_meth = UI_OpenSSL();
571 return default_UI_meth;
575 UI_get_method(UI *ui)
581 UI_set_method(UI *ui, const UI_METHOD *meth)
589 UI_create_method(char *name)
591 UI_METHOD *ui_method = calloc(1, sizeof(UI_METHOD));
593 if (ui_method && name)
594 ui_method->name = strdup(name);
599 /* BIG FSCKING WARNING!!!! If you use this on a statically allocated method
600 (that is, it hasn't been allocated using UI_create_method(), you deserve
601 anything Murphy can throw at you and more! You have been warned. */
603 UI_destroy_method(UI_METHOD *ui_method)
605 free(ui_method->name);
606 ui_method->name = NULL;
611 UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui))
614 method->ui_open_session = opener;
621 UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis))
624 method->ui_write_string = writer;
631 UI_method_set_flusher(UI_METHOD *method, int (*flusher)(UI *ui))
634 method->ui_flush = flusher;
641 UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis))
644 method->ui_read_string = reader;
651 UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
654 method->ui_close_session = closer;
661 UI_method_set_prompt_constructor(UI_METHOD *method,
662 char *(*prompt_constructor)(UI *ui, const char *object_desc,
663 const char *object_name))
666 method->ui_construct_prompt = prompt_constructor;
673 (*UI_method_get_opener(UI_METHOD * method))(UI *)
676 return method->ui_open_session;
682 (*UI_method_get_writer(UI_METHOD *method))(UI *, UI_STRING *)
685 return method->ui_write_string;
691 (*UI_method_get_flusher(UI_METHOD *method)) (UI *)
694 return method->ui_flush;
700 (*UI_method_get_reader(UI_METHOD *method))(UI *, UI_STRING *)
703 return method->ui_read_string;
709 (*UI_method_get_closer(UI_METHOD *method))(UI *)
712 return method->ui_close_session;
718 (*UI_method_get_prompt_constructor(UI_METHOD *method))(UI *, const char *,
722 return method->ui_construct_prompt;
728 UI_get_string_type(UI_STRING *uis)
736 UI_get_input_flags(UI_STRING *uis)
740 return uis->input_flags;
744 UI_get0_output_string(UI_STRING *uis)
748 return uis->out_string;
752 UI_get0_action_string(UI_STRING *uis)
759 return uis->_.boolean_data.action_desc;
766 UI_get0_result_string(UI_STRING *uis)
773 return uis->result_buf;
780 UI_get0_test_string(UI_STRING *uis)
786 return uis->_.string_data.test_buf;
793 UI_get_result_minsize(UI_STRING *uis)
800 return uis->_.string_data.result_minsize;
807 UI_get_result_maxsize(UI_STRING *uis)
814 return uis->_.string_data.result_maxsize;
821 UI_set_result(UI *ui, UI_STRING *uis, const char *result)
823 int l = strlen(result);
825 ui->flags &= ~UI_FLAG_REDOABLE;
832 if (l < uis->_.string_data.result_minsize) {
833 ui->flags |= UI_FLAG_REDOABLE;
834 UIerr(UI_F_UI_SET_RESULT,
835 UI_R_RESULT_TOO_SMALL);
836 ERR_asprintf_error_data
837 ("You must type in %d to %d characters",
838 uis->_.string_data.result_minsize,
839 uis->_.string_data.result_maxsize);
842 if (l > uis->_.string_data.result_maxsize) {
843 ui->flags |= UI_FLAG_REDOABLE;
844 UIerr(UI_F_UI_SET_RESULT,
845 UI_R_RESULT_TOO_LARGE);
846 ERR_asprintf_error_data
847 ("You must type in %d to %d characters",
848 uis->_.string_data.result_minsize,
849 uis->_.string_data.result_maxsize);
852 if (!uis->result_buf) {
853 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
856 strlcpy(uis->result_buf, result,
857 uis->_.string_data.result_maxsize + 1);
863 if (!uis->result_buf) {
864 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
867 uis->result_buf[0] = '\0';
868 for (p = result; *p; p++) {
869 if (strchr(uis->_.boolean_data.ok_chars, *p)) {
871 uis->_.boolean_data.ok_chars[0];
874 if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
876 uis->_.boolean_data.cancel_chars[0];