2 * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
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).
60 #include "internal/cryptlib.h"
61 #include <openssl/e_os2.h>
62 #include <openssl/buffer.h>
63 #include <openssl/ui.h>
64 #include <openssl/err.h>
67 static const UI_METHOD *default_UI_meth = NULL;
71 return (UI_new_method(NULL));
74 UI *UI_new_method(const UI_METHOD *method)
76 UI *ret = OPENSSL_zalloc(sizeof(*ret));
79 UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
83 ret->lock = CRYPTO_THREAD_lock_new();
84 if (ret->lock == NULL) {
85 UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
91 ret->meth = UI_get_default_method();
95 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data)) {
102 static void free_string(UI_STRING *uis)
104 if (uis->flags & OUT_STRING_FREEABLE) {
105 OPENSSL_free((char *)uis->out_string);
108 OPENSSL_free((char *)uis->_.boolean_data.action_desc);
109 OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
110 OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
123 sk_UI_STRING_pop_free(ui->strings, free_string);
124 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
125 CRYPTO_THREAD_lock_free(ui->lock);
129 static int allocate_string_stack(UI *ui)
131 if (ui->strings == NULL) {
132 ui->strings = sk_UI_STRING_new_null();
133 if (ui->strings == NULL) {
140 static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
142 enum UI_string_types type,
143 int input_flags, char *result_buf)
145 UI_STRING *ret = NULL;
147 if (prompt == NULL) {
148 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
149 } else if ((type == UIT_PROMPT || type == UIT_VERIFY
150 || type == UIT_BOOLEAN) && result_buf == NULL) {
151 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
152 } else if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL) {
153 ret->out_string = prompt;
154 ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
155 ret->input_flags = input_flags;
157 ret->result_buf = result_buf;
162 static int general_allocate_string(UI *ui, const char *prompt,
164 enum UI_string_types type, int input_flags,
165 char *result_buf, int minsize, int maxsize,
166 const char *test_buf)
169 UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
170 type, input_flags, result_buf);
173 if (allocate_string_stack(ui) >= 0) {
174 s->_.string_data.result_minsize = minsize;
175 s->_.string_data.result_maxsize = maxsize;
176 s->_.string_data.test_buf = test_buf;
177 ret = sk_UI_STRING_push(ui->strings, s);
178 /* sk_push() returns 0 on error. Let's adapt that */
187 static int general_allocate_boolean(UI *ui,
189 const char *action_desc,
190 const char *ok_chars,
191 const char *cancel_chars,
193 enum UI_string_types type,
194 int input_flags, char *result_buf)
200 if (ok_chars == NULL) {
201 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
202 } else if (cancel_chars == NULL) {
203 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
205 for (p = ok_chars; *p; p++) {
206 if (strchr(cancel_chars, *p)) {
207 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
208 UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
212 s = general_allocate_prompt(ui, prompt, prompt_freeable,
213 type, input_flags, result_buf);
216 if (allocate_string_stack(ui) >= 0) {
217 s->_.boolean_data.action_desc = action_desc;
218 s->_.boolean_data.ok_chars = ok_chars;
219 s->_.boolean_data.cancel_chars = cancel_chars;
220 ret = sk_UI_STRING_push(ui->strings, s);
222 * sk_push() returns 0 on error. Let's adapt that
234 * Returns the index to the place in the stack or -1 for error. Uses a
235 * direct reference to the prompt.
237 int UI_add_input_string(UI *ui, const char *prompt, int flags,
238 char *result_buf, int minsize, int maxsize)
240 return general_allocate_string(ui, prompt, 0,
241 UIT_PROMPT, flags, result_buf, minsize,
245 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
246 int UI_dup_input_string(UI *ui, const char *prompt, int flags,
247 char *result_buf, int minsize, int maxsize)
249 char *prompt_copy = NULL;
252 prompt_copy = OPENSSL_strdup(prompt);
253 if (prompt_copy == NULL) {
254 UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
259 return general_allocate_string(ui, prompt_copy, 1,
260 UIT_PROMPT, flags, result_buf, minsize,
264 int UI_add_verify_string(UI *ui, const char *prompt, int flags,
265 char *result_buf, int minsize, int maxsize,
266 const char *test_buf)
268 return general_allocate_string(ui, prompt, 0,
269 UIT_VERIFY, flags, result_buf, minsize,
273 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
274 char *result_buf, int minsize, int maxsize,
275 const char *test_buf)
277 char *prompt_copy = NULL;
280 prompt_copy = OPENSSL_strdup(prompt);
281 if (prompt_copy == NULL) {
282 UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
287 return general_allocate_string(ui, prompt_copy, 1,
288 UIT_VERIFY, flags, result_buf, minsize,
292 int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
293 const char *ok_chars, const char *cancel_chars,
294 int flags, char *result_buf)
296 return general_allocate_boolean(ui, prompt, action_desc,
297 ok_chars, cancel_chars, 0, UIT_BOOLEAN,
301 int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
302 const char *ok_chars, const char *cancel_chars,
303 int flags, char *result_buf)
305 char *prompt_copy = NULL;
306 char *action_desc_copy = NULL;
307 char *ok_chars_copy = NULL;
308 char *cancel_chars_copy = NULL;
311 prompt_copy = OPENSSL_strdup(prompt);
312 if (prompt_copy == NULL) {
313 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
319 action_desc_copy = OPENSSL_strdup(action_desc);
320 if (action_desc_copy == NULL) {
321 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
327 ok_chars_copy = OPENSSL_strdup(ok_chars);
328 if (ok_chars_copy == NULL) {
329 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
335 cancel_chars_copy = OPENSSL_strdup(cancel_chars);
336 if (cancel_chars_copy == NULL) {
337 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
342 return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
343 ok_chars_copy, cancel_chars_copy, 1,
344 UIT_BOOLEAN, flags, result_buf);
346 OPENSSL_free(prompt_copy);
347 OPENSSL_free(action_desc_copy);
348 OPENSSL_free(ok_chars_copy);
349 OPENSSL_free(cancel_chars_copy);
353 int UI_add_info_string(UI *ui, const char *text)
355 return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
359 int UI_dup_info_string(UI *ui, const char *text)
361 char *text_copy = NULL;
364 text_copy = OPENSSL_strdup(text);
365 if (text_copy == NULL) {
366 UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
371 return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
375 int UI_add_error_string(UI *ui, const char *text)
377 return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
381 int UI_dup_error_string(UI *ui, const char *text)
383 char *text_copy = NULL;
386 text_copy = OPENSSL_strdup(text);
387 if (text_copy == NULL) {
388 UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
392 return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
396 char *UI_construct_prompt(UI *ui, const char *object_desc,
397 const char *object_name)
401 if (ui->meth->ui_construct_prompt)
402 prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
404 char prompt1[] = "Enter ";
405 char prompt2[] = " for ";
406 char prompt3[] = ":";
409 if (object_desc == NULL)
411 len = sizeof(prompt1) - 1 + strlen(object_desc);
413 len += sizeof(prompt2) - 1 + strlen(object_name);
414 len += sizeof(prompt3) - 1;
416 prompt = OPENSSL_malloc(len + 1);
419 OPENSSL_strlcpy(prompt, prompt1, len + 1);
420 OPENSSL_strlcat(prompt, object_desc, len + 1);
422 OPENSSL_strlcat(prompt, prompt2, len + 1);
423 OPENSSL_strlcat(prompt, object_name, len + 1);
425 OPENSSL_strlcat(prompt, prompt3, len + 1);
430 void *UI_add_user_data(UI *ui, void *user_data)
432 void *old_data = ui->user_data;
433 ui->user_data = user_data;
437 void *UI_get0_user_data(UI *ui)
439 return ui->user_data;
442 const char *UI_get0_result(UI *ui, int i)
445 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL);
448 if (i >= sk_UI_STRING_num(ui->strings)) {
449 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE);
452 return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
455 static int print_error(const char *str, size_t len, UI *ui)
459 memset(&uis, 0, sizeof(uis));
460 uis.type = UIT_ERROR;
461 uis.out_string = str;
463 if (ui->meth->ui_write_string && !ui->meth->ui_write_string(ui, &uis))
468 int UI_process(UI *ui)
472 if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
475 if (ui->flags & UI_FLAG_PRINT_ERRORS)
476 ERR_print_errors_cb((int (*)(const char *, size_t, void *))
477 print_error, (void *)ui);
479 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
480 if (ui->meth->ui_write_string
481 && !ui->meth->ui_write_string(ui,
482 sk_UI_STRING_value(ui->strings, i)))
489 if (ui->meth->ui_flush)
490 switch (ui->meth->ui_flush(ui)) {
491 case -1: /* Interrupt/Cancel/something... */
497 default: /* Success */
502 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
503 if (ui->meth->ui_read_string) {
504 switch (ui->meth->ui_read_string(ui,
505 sk_UI_STRING_value(ui->strings,
507 case -1: /* Interrupt/Cancel/something... */
513 default: /* Success */
520 if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
525 int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
528 UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
532 case UI_CTRL_PRINT_ERRORS:
534 int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS);
536 ui->flags |= UI_FLAG_PRINT_ERRORS;
538 ui->flags &= ~UI_FLAG_PRINT_ERRORS;
541 case UI_CTRL_IS_REDOABLE:
542 return ! !(ui->flags & UI_FLAG_REDOABLE);
546 UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
550 int UI_set_ex_data(UI *r, int idx, void *arg)
552 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
555 void *UI_get_ex_data(UI *r, int idx)
557 return (CRYPTO_get_ex_data(&r->ex_data, idx));
560 void UI_set_default_method(const UI_METHOD *meth)
562 default_UI_meth = meth;
565 const UI_METHOD *UI_get_default_method(void)
567 if (default_UI_meth == NULL) {
568 default_UI_meth = UI_OpenSSL();
570 return default_UI_meth;
573 const UI_METHOD *UI_get_method(UI *ui)
578 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
584 UI_METHOD *UI_create_method(char *name)
586 UI_METHOD *ui_method = OPENSSL_zalloc(sizeof(*ui_method));
588 if (ui_method != NULL)
589 ui_method->name = OPENSSL_strdup(name);
594 * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
595 * (that is, it hasn't been allocated using UI_create_method(), you deserve
596 * anything Murphy can throw at you and more! You have been warned.
598 void UI_destroy_method(UI_METHOD *ui_method)
600 OPENSSL_free(ui_method->name);
601 ui_method->name = NULL;
602 OPENSSL_free(ui_method);
605 int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui))
608 method->ui_open_session = opener;
614 int UI_method_set_writer(UI_METHOD *method,
615 int (*writer) (UI *ui, UI_STRING *uis))
618 method->ui_write_string = writer;
624 int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
627 method->ui_flush = flusher;
633 int UI_method_set_reader(UI_METHOD *method,
634 int (*reader) (UI *ui, UI_STRING *uis))
637 method->ui_read_string = reader;
643 int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
646 method->ui_close_session = closer;
652 int UI_method_set_prompt_constructor(UI_METHOD *method,
653 char *(*prompt_constructor) (UI *ui,
660 method->ui_construct_prompt = prompt_constructor;
666 int (*UI_method_get_opener(UI_METHOD *method)) (UI *) {
668 return method->ui_open_session;
673 int (*UI_method_get_writer(UI_METHOD *method)) (UI *, UI_STRING *) {
675 return method->ui_write_string;
680 int (*UI_method_get_flusher(UI_METHOD *method)) (UI *) {
682 return method->ui_flush;
687 int (*UI_method_get_reader(UI_METHOD *method)) (UI *, UI_STRING *) {
689 return method->ui_read_string;
694 int (*UI_method_get_closer(UI_METHOD *method)) (UI *) {
696 return method->ui_close_session;
701 char *(*UI_method_get_prompt_constructor(UI_METHOD *method)) (UI *,
705 return method->ui_construct_prompt;
710 enum UI_string_types UI_get_string_type(UI_STRING *uis)
717 int UI_get_input_flags(UI_STRING *uis)
721 return uis->input_flags;
724 const char *UI_get0_output_string(UI_STRING *uis)
728 return uis->out_string;
731 const char *UI_get0_action_string(UI_STRING *uis)
738 return uis->_.boolean_data.action_desc;
744 const char *UI_get0_result_string(UI_STRING *uis)
751 return uis->result_buf;
757 const char *UI_get0_test_string(UI_STRING *uis)
763 return uis->_.string_data.test_buf;
769 int UI_get_result_minsize(UI_STRING *uis)
776 return uis->_.string_data.result_minsize;
782 int UI_get_result_maxsize(UI_STRING *uis)
789 return uis->_.string_data.result_maxsize;
795 int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
797 int l = strlen(result);
799 ui->flags &= ~UI_FLAG_REDOABLE;
807 char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
808 char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
810 BIO_snprintf(number1, sizeof(number1), "%d",
811 uis->_.string_data.result_minsize);
812 BIO_snprintf(number2, sizeof(number2), "%d",
813 uis->_.string_data.result_maxsize);
815 if (l < uis->_.string_data.result_minsize) {
816 ui->flags |= UI_FLAG_REDOABLE;
817 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL);
818 ERR_add_error_data(5, "You must type in ",
819 number1, " to ", number2, " characters");
822 if (l > uis->_.string_data.result_maxsize) {
823 ui->flags |= UI_FLAG_REDOABLE;
824 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE);
825 ERR_add_error_data(5, "You must type in ",
826 number1, " to ", number2, " characters");
831 if (!uis->result_buf) {
832 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
836 OPENSSL_strlcpy(uis->result_buf, result,
837 uis->_.string_data.result_maxsize + 1);
843 if (!uis->result_buf) {
844 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
848 uis->result_buf[0] = '\0';
849 for (p = result; *p; p++) {
850 if (strchr(uis->_.boolean_data.ok_chars, *p)) {
851 uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
854 if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
855 uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];