1 /* crypto/ui/ui_lib.c */
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 IMPLEMENT_STACK_OF(UI_STRING_ST)
70 static const UI_METHOD *default_UI_meth = NULL;
74 return (UI_new_method(NULL));
77 UI *UI_new_method(const UI_METHOD *method)
81 ret = (UI *)OPENSSL_malloc(sizeof(UI));
83 UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
87 ret->meth = UI_get_default_method();
92 ret->user_data = NULL;
94 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data);
98 static void free_string(UI_STRING *uis)
100 if (uis->flags & OUT_STRING_FREEABLE) {
101 OPENSSL_free((char *)uis->out_string);
104 OPENSSL_free((char *)uis->_.boolean_data.action_desc);
105 OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
106 OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
119 sk_UI_STRING_pop_free(ui->strings, free_string);
120 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
124 static int allocate_string_stack(UI *ui)
126 if (ui->strings == NULL) {
127 ui->strings = sk_UI_STRING_new_null();
128 if (ui->strings == NULL) {
135 static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
137 enum UI_string_types type,
138 int input_flags, char *result_buf)
140 UI_STRING *ret = NULL;
142 if (prompt == NULL) {
143 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
144 } else if ((type == UIT_PROMPT || type == UIT_VERIFY
145 || type == UIT_BOOLEAN) && result_buf == NULL) {
146 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
147 } else if ((ret = (UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING)))) {
148 ret->out_string = prompt;
149 ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
150 ret->input_flags = input_flags;
152 ret->result_buf = result_buf;
157 static int general_allocate_string(UI *ui, const char *prompt,
159 enum UI_string_types type, int input_flags,
160 char *result_buf, int minsize, int maxsize,
161 const char *test_buf)
164 UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
165 type, input_flags, result_buf);
168 if (allocate_string_stack(ui) >= 0) {
169 s->_.string_data.result_minsize = minsize;
170 s->_.string_data.result_maxsize = maxsize;
171 s->_.string_data.test_buf = test_buf;
172 ret = sk_UI_STRING_push(ui->strings, s);
173 /* sk_push() returns 0 on error. Let's addapt that */
182 static int general_allocate_boolean(UI *ui,
184 const char *action_desc,
185 const char *ok_chars,
186 const char *cancel_chars,
188 enum UI_string_types type,
189 int input_flags, char *result_buf)
195 if (ok_chars == NULL) {
196 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
197 } else if (cancel_chars == NULL) {
198 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
200 for (p = ok_chars; *p; p++) {
201 if (strchr(cancel_chars, *p)) {
202 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
203 UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
207 s = general_allocate_prompt(ui, prompt, prompt_freeable,
208 type, input_flags, result_buf);
211 if (allocate_string_stack(ui) >= 0) {
212 s->_.boolean_data.action_desc = action_desc;
213 s->_.boolean_data.ok_chars = ok_chars;
214 s->_.boolean_data.cancel_chars = cancel_chars;
215 ret = sk_UI_STRING_push(ui->strings, s);
217 * sk_push() returns 0 on error. Let's addapt that
229 * Returns the index to the place in the stack or -1 for error. Uses a
230 * direct reference to the prompt.
232 int UI_add_input_string(UI *ui, const char *prompt, int flags,
233 char *result_buf, int minsize, int maxsize)
235 return general_allocate_string(ui, prompt, 0,
236 UIT_PROMPT, flags, result_buf, minsize,
240 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
241 int UI_dup_input_string(UI *ui, const char *prompt, int flags,
242 char *result_buf, int minsize, int maxsize)
244 char *prompt_copy = NULL;
247 prompt_copy = BUF_strdup(prompt);
248 if (prompt_copy == NULL) {
249 UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
254 return general_allocate_string(ui, prompt_copy, 1,
255 UIT_PROMPT, flags, result_buf, minsize,
259 int UI_add_verify_string(UI *ui, const char *prompt, int flags,
260 char *result_buf, int minsize, int maxsize,
261 const char *test_buf)
263 return general_allocate_string(ui, prompt, 0,
264 UIT_VERIFY, flags, result_buf, minsize,
268 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
269 char *result_buf, int minsize, int maxsize,
270 const char *test_buf)
272 char *prompt_copy = NULL;
275 prompt_copy = BUF_strdup(prompt);
276 if (prompt_copy == NULL) {
277 UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
282 return general_allocate_string(ui, prompt_copy, 1,
283 UIT_VERIFY, flags, result_buf, minsize,
287 int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
288 const char *ok_chars, const char *cancel_chars,
289 int flags, char *result_buf)
291 return general_allocate_boolean(ui, prompt, action_desc,
292 ok_chars, cancel_chars, 0, UIT_BOOLEAN,
296 int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
297 const char *ok_chars, const char *cancel_chars,
298 int flags, char *result_buf)
300 char *prompt_copy = NULL;
301 char *action_desc_copy = NULL;
302 char *ok_chars_copy = NULL;
303 char *cancel_chars_copy = NULL;
306 prompt_copy = BUF_strdup(prompt);
307 if (prompt_copy == NULL) {
308 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
314 action_desc_copy = BUF_strdup(action_desc);
315 if (action_desc_copy == NULL) {
316 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
322 ok_chars_copy = BUF_strdup(ok_chars);
323 if (ok_chars_copy == NULL) {
324 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
330 cancel_chars_copy = BUF_strdup(cancel_chars);
331 if (cancel_chars_copy == NULL) {
332 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
337 return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
338 ok_chars_copy, cancel_chars_copy, 1,
339 UIT_BOOLEAN, flags, result_buf);
342 OPENSSL_free(prompt_copy);
343 if (action_desc_copy)
344 OPENSSL_free(action_desc_copy);
346 OPENSSL_free(ok_chars_copy);
347 if (cancel_chars_copy)
348 OPENSSL_free(cancel_chars_copy);
352 int UI_add_info_string(UI *ui, const char *text)
354 return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
358 int UI_dup_info_string(UI *ui, const char *text)
360 char *text_copy = NULL;
363 text_copy = BUF_strdup(text);
364 if (text_copy == NULL) {
365 UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
370 return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
374 int UI_add_error_string(UI *ui, const char *text)
376 return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
380 int UI_dup_error_string(UI *ui, const char *text)
382 char *text_copy = NULL;
385 text_copy = BUF_strdup(text);
386 if (text_copy == NULL) {
387 UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
391 return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
395 char *UI_construct_prompt(UI *ui, const char *object_desc,
396 const char *object_name)
400 if (ui->meth->ui_construct_prompt)
401 prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
403 char prompt1[] = "Enter ";
404 char prompt2[] = " for ";
405 char prompt3[] = ":";
408 if (object_desc == NULL)
410 len = sizeof(prompt1) - 1 + strlen(object_desc);
412 len += sizeof(prompt2) - 1 + strlen(object_name);
413 len += sizeof(prompt3) - 1;
415 prompt = (char *)OPENSSL_malloc(len + 1);
418 BUF_strlcpy(prompt, prompt1, len + 1);
419 BUF_strlcat(prompt, object_desc, len + 1);
421 BUF_strlcat(prompt, prompt2, len + 1);
422 BUF_strlcat(prompt, object_name, len + 1);
424 BUF_strlcat(prompt, prompt3, len + 1);
429 void *UI_add_user_data(UI *ui, void *user_data)
431 void *old_data = ui->user_data;
432 ui->user_data = user_data;
436 void *UI_get0_user_data(UI *ui)
438 return ui->user_data;
441 const char *UI_get0_result(UI *ui, int i)
444 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL);
447 if (i >= sk_UI_STRING_num(ui->strings)) {
448 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE);
451 return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
454 static int print_error(const char *str, size_t len, UI *ui)
458 memset(&uis, 0, sizeof(uis));
459 uis.type = UIT_ERROR;
460 uis.out_string = str;
462 if (ui->meth->ui_write_string && !ui->meth->ui_write_string(ui, &uis))
467 int UI_process(UI *ui)
471 if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
474 if (ui->flags & UI_FLAG_PRINT_ERRORS)
475 ERR_print_errors_cb((int (*)(const char *, size_t, void *))
476 print_error, (void *)ui);
478 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
479 if (ui->meth->ui_write_string
480 && !ui->meth->ui_write_string(ui,
481 sk_UI_STRING_value(ui->strings, i)))
488 if (ui->meth->ui_flush)
489 switch (ui->meth->ui_flush(ui)) {
490 case -1: /* Interrupt/Cancel/something... */
496 default: /* Success */
501 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
502 if (ui->meth->ui_read_string) {
503 switch (ui->meth->ui_read_string(ui,
504 sk_UI_STRING_value(ui->strings,
506 case -1: /* Interrupt/Cancel/something... */
512 default: /* Success */
519 if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
524 int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
527 UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
531 case UI_CTRL_PRINT_ERRORS:
533 int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS);
535 ui->flags |= UI_FLAG_PRINT_ERRORS;
537 ui->flags &= ~UI_FLAG_PRINT_ERRORS;
540 case UI_CTRL_IS_REDOABLE:
541 return ! !(ui->flags & UI_FLAG_REDOABLE);
545 UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
549 int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
550 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
552 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp,
553 new_func, dup_func, free_func);
556 int UI_set_ex_data(UI *r, int idx, void *arg)
558 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
561 void *UI_get_ex_data(UI *r, int idx)
563 return (CRYPTO_get_ex_data(&r->ex_data, idx));
566 void UI_set_default_method(const UI_METHOD *meth)
568 default_UI_meth = meth;
571 const UI_METHOD *UI_get_default_method(void)
573 if (default_UI_meth == NULL) {
574 default_UI_meth = UI_OpenSSL();
576 return default_UI_meth;
579 const UI_METHOD *UI_get_method(UI *ui)
584 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
590 UI_METHOD *UI_create_method(char *name)
592 UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
595 memset(ui_method, 0, sizeof(*ui_method));
596 ui_method->name = BUF_strdup(name);
602 * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
603 * (that is, it hasn't been allocated using UI_create_method(), you deserve
604 * anything Murphy can throw at you and more! You have been warned.
606 void UI_destroy_method(UI_METHOD *ui_method)
608 OPENSSL_free(ui_method->name);
609 ui_method->name = NULL;
610 OPENSSL_free(ui_method);
613 int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui))
616 method->ui_open_session = opener;
622 int UI_method_set_writer(UI_METHOD *method,
623 int (*writer) (UI *ui, UI_STRING *uis))
626 method->ui_write_string = writer;
632 int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
635 method->ui_flush = flusher;
641 int UI_method_set_reader(UI_METHOD *method,
642 int (*reader) (UI *ui, UI_STRING *uis))
645 method->ui_read_string = reader;
651 int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
654 method->ui_close_session = closer;
660 int UI_method_set_prompt_constructor(UI_METHOD *method,
661 char *(*prompt_constructor) (UI *ui,
668 method->ui_construct_prompt = prompt_constructor;
674 int (*UI_method_get_opener(UI_METHOD *method)) (UI *) {
676 return method->ui_open_session;
681 int (*UI_method_get_writer(UI_METHOD *method)) (UI *, UI_STRING *) {
683 return method->ui_write_string;
688 int (*UI_method_get_flusher(UI_METHOD *method)) (UI *) {
690 return method->ui_flush;
695 int (*UI_method_get_reader(UI_METHOD *method)) (UI *, UI_STRING *) {
697 return method->ui_read_string;
702 int (*UI_method_get_closer(UI_METHOD *method)) (UI *) {
704 return method->ui_close_session;
709 char *(*UI_method_get_prompt_constructor(UI_METHOD *method)) (UI *,
713 return method->ui_construct_prompt;
718 enum UI_string_types UI_get_string_type(UI_STRING *uis)
725 int UI_get_input_flags(UI_STRING *uis)
729 return uis->input_flags;
732 const char *UI_get0_output_string(UI_STRING *uis)
736 return uis->out_string;
739 const char *UI_get0_action_string(UI_STRING *uis)
746 return uis->_.boolean_data.action_desc;
752 const char *UI_get0_result_string(UI_STRING *uis)
759 return uis->result_buf;
765 const char *UI_get0_test_string(UI_STRING *uis)
771 return uis->_.string_data.test_buf;
777 int UI_get_result_minsize(UI_STRING *uis)
784 return uis->_.string_data.result_minsize;
790 int UI_get_result_maxsize(UI_STRING *uis)
797 return uis->_.string_data.result_maxsize;
803 int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
805 int l = strlen(result);
807 ui->flags &= ~UI_FLAG_REDOABLE;
815 char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
816 char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
818 BIO_snprintf(number1, sizeof(number1), "%d",
819 uis->_.string_data.result_minsize);
820 BIO_snprintf(number2, sizeof(number2), "%d",
821 uis->_.string_data.result_maxsize);
823 if (l < uis->_.string_data.result_minsize) {
824 ui->flags |= UI_FLAG_REDOABLE;
825 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL);
826 ERR_add_error_data(5, "You must type in ",
827 number1, " to ", number2, " characters");
830 if (l > uis->_.string_data.result_maxsize) {
831 ui->flags |= UI_FLAG_REDOABLE;
832 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE);
833 ERR_add_error_data(5, "You must type in ",
834 number1, " to ", number2, " characters");
839 if (!uis->result_buf) {
840 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
844 BUF_strlcpy(uis->result_buf, result,
845 uis->_.string_data.result_maxsize + 1);
851 if (!uis->result_buf) {
852 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
856 uis->result_buf[0] = '\0';
857 for (p = result; *p; p++) {
858 if (strchr(uis->_.boolean_data.ok_chars, *p)) {
859 uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
862 if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
863 uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];