1 /* crypto/ui/ui_lib.c -*- mode:C; c-file-style: "eay" -*- */
3 * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@openssl.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/e_os2.h>
63 #include <openssl/buffer.h>
64 #include <openssl/ui.h>
65 #include <openssl/err.h>
68 static const UI_METHOD *default_UI_meth = NULL;
72 return (UI_new_method(NULL));
75 UI *UI_new_method(const UI_METHOD *method)
77 UI *ret = OPENSSL_malloc(sizeof(*ret));
80 UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
84 ret->meth = UI_get_default_method();
89 ret->user_data = NULL;
91 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data);
95 static void free_string(UI_STRING *uis)
97 if (uis->flags & OUT_STRING_FREEABLE) {
98 OPENSSL_free((char *)uis->out_string);
101 OPENSSL_free((char *)uis->_.boolean_data.action_desc);
102 OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
103 OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
116 sk_UI_STRING_pop_free(ui->strings, free_string);
117 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
121 static int allocate_string_stack(UI *ui)
123 if (ui->strings == NULL) {
124 ui->strings = sk_UI_STRING_new_null();
125 if (ui->strings == NULL) {
132 static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
134 enum UI_string_types type,
135 int input_flags, char *result_buf)
137 UI_STRING *ret = NULL;
139 if (prompt == NULL) {
140 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
141 } else if ((type == UIT_PROMPT || type == UIT_VERIFY
142 || type == UIT_BOOLEAN) && result_buf == NULL) {
143 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
144 } else if ((ret = OPENSSL_malloc(sizeof(*ret)))) {
145 ret->out_string = prompt;
146 ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
147 ret->input_flags = input_flags;
149 ret->result_buf = result_buf;
154 static int general_allocate_string(UI *ui, const char *prompt,
156 enum UI_string_types type, int input_flags,
157 char *result_buf, int minsize, int maxsize,
158 const char *test_buf)
161 UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
162 type, input_flags, result_buf);
165 if (allocate_string_stack(ui) >= 0) {
166 s->_.string_data.result_minsize = minsize;
167 s->_.string_data.result_maxsize = maxsize;
168 s->_.string_data.test_buf = test_buf;
169 ret = sk_UI_STRING_push(ui->strings, s);
170 /* sk_push() returns 0 on error. Let's addapt that */
179 static int general_allocate_boolean(UI *ui,
181 const char *action_desc,
182 const char *ok_chars,
183 const char *cancel_chars,
185 enum UI_string_types type,
186 int input_flags, char *result_buf)
192 if (ok_chars == NULL) {
193 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
194 } else if (cancel_chars == NULL) {
195 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
197 for (p = ok_chars; *p; p++) {
198 if (strchr(cancel_chars, *p)) {
199 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
200 UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
204 s = general_allocate_prompt(ui, prompt, prompt_freeable,
205 type, input_flags, result_buf);
208 if (allocate_string_stack(ui) >= 0) {
209 s->_.boolean_data.action_desc = action_desc;
210 s->_.boolean_data.ok_chars = ok_chars;
211 s->_.boolean_data.cancel_chars = cancel_chars;
212 ret = sk_UI_STRING_push(ui->strings, s);
214 * sk_push() returns 0 on error. Let's addapt that
226 * Returns the index to the place in the stack or -1 for error. Uses a
227 * direct reference to the prompt.
229 int UI_add_input_string(UI *ui, const char *prompt, int flags,
230 char *result_buf, int minsize, int maxsize)
232 return general_allocate_string(ui, prompt, 0,
233 UIT_PROMPT, flags, result_buf, minsize,
237 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
238 int UI_dup_input_string(UI *ui, const char *prompt, int flags,
239 char *result_buf, int minsize, int maxsize)
241 char *prompt_copy = NULL;
244 prompt_copy = BUF_strdup(prompt);
245 if (prompt_copy == NULL) {
246 UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
251 return general_allocate_string(ui, prompt_copy, 1,
252 UIT_PROMPT, flags, result_buf, minsize,
256 int UI_add_verify_string(UI *ui, const char *prompt, int flags,
257 char *result_buf, int minsize, int maxsize,
258 const char *test_buf)
260 return general_allocate_string(ui, prompt, 0,
261 UIT_VERIFY, flags, result_buf, minsize,
265 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
266 char *result_buf, int minsize, int maxsize,
267 const char *test_buf)
269 char *prompt_copy = NULL;
272 prompt_copy = BUF_strdup(prompt);
273 if (prompt_copy == NULL) {
274 UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
279 return general_allocate_string(ui, prompt_copy, 1,
280 UIT_VERIFY, flags, result_buf, minsize,
284 int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
285 const char *ok_chars, const char *cancel_chars,
286 int flags, char *result_buf)
288 return general_allocate_boolean(ui, prompt, action_desc,
289 ok_chars, cancel_chars, 0, UIT_BOOLEAN,
293 int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
294 const char *ok_chars, const char *cancel_chars,
295 int flags, char *result_buf)
297 char *prompt_copy = NULL;
298 char *action_desc_copy = NULL;
299 char *ok_chars_copy = NULL;
300 char *cancel_chars_copy = NULL;
303 prompt_copy = BUF_strdup(prompt);
304 if (prompt_copy == NULL) {
305 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
311 action_desc_copy = BUF_strdup(action_desc);
312 if (action_desc_copy == NULL) {
313 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
319 ok_chars_copy = BUF_strdup(ok_chars);
320 if (ok_chars_copy == NULL) {
321 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
327 cancel_chars_copy = BUF_strdup(cancel_chars);
328 if (cancel_chars_copy == NULL) {
329 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
334 return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
335 ok_chars_copy, cancel_chars_copy, 1,
336 UIT_BOOLEAN, flags, result_buf);
338 OPENSSL_free(prompt_copy);
339 OPENSSL_free(action_desc_copy);
340 OPENSSL_free(ok_chars_copy);
341 OPENSSL_free(cancel_chars_copy);
345 int UI_add_info_string(UI *ui, const char *text)
347 return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
351 int UI_dup_info_string(UI *ui, const char *text)
353 char *text_copy = NULL;
356 text_copy = BUF_strdup(text);
357 if (text_copy == NULL) {
358 UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
363 return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
367 int UI_add_error_string(UI *ui, const char *text)
369 return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
373 int UI_dup_error_string(UI *ui, const char *text)
375 char *text_copy = NULL;
378 text_copy = BUF_strdup(text);
379 if (text_copy == NULL) {
380 UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
384 return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
388 char *UI_construct_prompt(UI *ui, const char *object_desc,
389 const char *object_name)
393 if (ui->meth->ui_construct_prompt)
394 prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
396 char prompt1[] = "Enter ";
397 char prompt2[] = " for ";
398 char prompt3[] = ":";
401 if (object_desc == NULL)
403 len = sizeof(prompt1) - 1 + strlen(object_desc);
405 len += sizeof(prompt2) - 1 + strlen(object_name);
406 len += sizeof(prompt3) - 1;
408 prompt = OPENSSL_malloc(len + 1);
411 BUF_strlcpy(prompt, prompt1, len + 1);
412 BUF_strlcat(prompt, object_desc, len + 1);
414 BUF_strlcat(prompt, prompt2, len + 1);
415 BUF_strlcat(prompt, object_name, len + 1);
417 BUF_strlcat(prompt, prompt3, len + 1);
422 void *UI_add_user_data(UI *ui, void *user_data)
424 void *old_data = ui->user_data;
425 ui->user_data = user_data;
429 void *UI_get0_user_data(UI *ui)
431 return ui->user_data;
434 const char *UI_get0_result(UI *ui, int i)
437 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL);
440 if (i >= sk_UI_STRING_num(ui->strings)) {
441 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE);
444 return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
447 static int print_error(const char *str, size_t len, UI *ui)
451 memset(&uis, 0, sizeof(uis));
452 uis.type = UIT_ERROR;
453 uis.out_string = str;
455 if (ui->meth->ui_write_string && !ui->meth->ui_write_string(ui, &uis))
460 int UI_process(UI *ui)
464 if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
467 if (ui->flags & UI_FLAG_PRINT_ERRORS)
468 ERR_print_errors_cb((int (*)(const char *, size_t, void *))
469 print_error, (void *)ui);
471 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
472 if (ui->meth->ui_write_string
473 && !ui->meth->ui_write_string(ui,
474 sk_UI_STRING_value(ui->strings, i)))
481 if (ui->meth->ui_flush)
482 switch (ui->meth->ui_flush(ui)) {
483 case -1: /* Interrupt/Cancel/something... */
489 default: /* Success */
494 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
495 if (ui->meth->ui_read_string) {
496 switch (ui->meth->ui_read_string(ui,
497 sk_UI_STRING_value(ui->strings,
499 case -1: /* Interrupt/Cancel/something... */
505 default: /* Success */
512 if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
517 int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
520 UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
524 case UI_CTRL_PRINT_ERRORS:
526 int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS);
528 ui->flags |= UI_FLAG_PRINT_ERRORS;
530 ui->flags &= ~UI_FLAG_PRINT_ERRORS;
533 case UI_CTRL_IS_REDOABLE:
534 return ! !(ui->flags & UI_FLAG_REDOABLE);
538 UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
542 int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
543 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
545 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp,
546 new_func, dup_func, free_func);
549 int UI_set_ex_data(UI *r, int idx, void *arg)
551 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
554 void *UI_get_ex_data(UI *r, int idx)
556 return (CRYPTO_get_ex_data(&r->ex_data, idx));
559 void UI_set_default_method(const UI_METHOD *meth)
561 default_UI_meth = meth;
564 const UI_METHOD *UI_get_default_method(void)
566 if (default_UI_meth == NULL) {
567 default_UI_meth = UI_OpenSSL();
569 return default_UI_meth;
572 const UI_METHOD *UI_get_method(UI *ui)
577 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
583 UI_METHOD *UI_create_method(char *name)
585 UI_METHOD *ui_method = OPENSSL_malloc(sizeof(*ui_method));
588 memset(ui_method, 0, sizeof(*ui_method));
589 ui_method->name = BUF_strdup(name);
595 * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
596 * (that is, it hasn't been allocated using UI_create_method(), you deserve
597 * anything Murphy can throw at you and more! You have been warned.
599 void UI_destroy_method(UI_METHOD *ui_method)
601 OPENSSL_free(ui_method->name);
602 ui_method->name = NULL;
603 OPENSSL_free(ui_method);
606 int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui))
609 method->ui_open_session = opener;
615 int UI_method_set_writer(UI_METHOD *method,
616 int (*writer) (UI *ui, UI_STRING *uis))
619 method->ui_write_string = writer;
625 int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
628 method->ui_flush = flusher;
634 int UI_method_set_reader(UI_METHOD *method,
635 int (*reader) (UI *ui, UI_STRING *uis))
638 method->ui_read_string = reader;
644 int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
647 method->ui_close_session = closer;
653 int UI_method_set_prompt_constructor(UI_METHOD *method,
654 char *(*prompt_constructor) (UI *ui,
661 method->ui_construct_prompt = prompt_constructor;
667 int (*UI_method_get_opener(UI_METHOD *method)) (UI *) {
669 return method->ui_open_session;
674 int (*UI_method_get_writer(UI_METHOD *method)) (UI *, UI_STRING *) {
676 return method->ui_write_string;
681 int (*UI_method_get_flusher(UI_METHOD *method)) (UI *) {
683 return method->ui_flush;
688 int (*UI_method_get_reader(UI_METHOD *method)) (UI *, UI_STRING *) {
690 return method->ui_read_string;
695 int (*UI_method_get_closer(UI_METHOD *method)) (UI *) {
697 return method->ui_close_session;
702 char *(*UI_method_get_prompt_constructor(UI_METHOD *method)) (UI *,
706 return method->ui_construct_prompt;
711 enum UI_string_types UI_get_string_type(UI_STRING *uis)
718 int UI_get_input_flags(UI_STRING *uis)
722 return uis->input_flags;
725 const char *UI_get0_output_string(UI_STRING *uis)
729 return uis->out_string;
732 const char *UI_get0_action_string(UI_STRING *uis)
739 return uis->_.boolean_data.action_desc;
745 const char *UI_get0_result_string(UI_STRING *uis)
752 return uis->result_buf;
758 const char *UI_get0_test_string(UI_STRING *uis)
764 return uis->_.string_data.test_buf;
770 int UI_get_result_minsize(UI_STRING *uis)
777 return uis->_.string_data.result_minsize;
783 int UI_get_result_maxsize(UI_STRING *uis)
790 return uis->_.string_data.result_maxsize;
796 int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
798 int l = strlen(result);
800 ui->flags &= ~UI_FLAG_REDOABLE;
808 char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
809 char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
811 BIO_snprintf(number1, sizeof(number1), "%d",
812 uis->_.string_data.result_minsize);
813 BIO_snprintf(number2, sizeof(number2), "%d",
814 uis->_.string_data.result_maxsize);
816 if (l < uis->_.string_data.result_minsize) {
817 ui->flags |= UI_FLAG_REDOABLE;
818 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL);
819 ERR_add_error_data(5, "You must type in ",
820 number1, " to ", number2, " characters");
823 if (l > uis->_.string_data.result_maxsize) {
824 ui->flags |= UI_FLAG_REDOABLE;
825 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE);
826 ERR_add_error_data(5, "You must type in ",
827 number1, " to ", number2, " characters");
832 if (!uis->result_buf) {
833 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
837 BUF_strlcpy(uis->result_buf, result,
838 uis->_.string_data.result_maxsize + 1);
844 if (!uis->result_buf) {
845 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
849 uis->result_buf[0] = '\0';
850 for (p = result; *p; p++) {
851 if (strchr(uis->_.boolean_data.ok_chars, *p)) {
852 uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
855 if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
856 uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];