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
->meth
= UI_get_default_method();
87 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI
, ret
, &ret
->ex_data
);
91 static void free_string(UI_STRING
*uis
)
93 if (uis
->flags
& OUT_STRING_FREEABLE
) {
94 OPENSSL_free((char *)uis
->out_string
);
97 OPENSSL_free((char *)uis
->_
.boolean_data
.action_desc
);
98 OPENSSL_free((char *)uis
->_
.boolean_data
.ok_chars
);
99 OPENSSL_free((char *)uis
->_
.boolean_data
.cancel_chars
);
112 sk_UI_STRING_pop_free(ui
->strings
, free_string
);
113 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI
, ui
, &ui
->ex_data
);
117 static int allocate_string_stack(UI
*ui
)
119 if (ui
->strings
== NULL
) {
120 ui
->strings
= sk_UI_STRING_new_null();
121 if (ui
->strings
== NULL
) {
128 static UI_STRING
*general_allocate_prompt(UI
*ui
, const char *prompt
,
130 enum UI_string_types type
,
131 int input_flags
, char *result_buf
)
133 UI_STRING
*ret
= NULL
;
135 if (prompt
== NULL
) {
136 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT
, ERR_R_PASSED_NULL_PARAMETER
);
137 } else if ((type
== UIT_PROMPT
|| type
== UIT_VERIFY
138 || type
== UIT_BOOLEAN
) && result_buf
== NULL
) {
139 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT
, UI_R_NO_RESULT_BUFFER
);
140 } else if ((ret
= OPENSSL_malloc(sizeof(*ret
))) != NULL
) {
141 ret
->out_string
= prompt
;
142 ret
->flags
= prompt_freeable
? OUT_STRING_FREEABLE
: 0;
143 ret
->input_flags
= input_flags
;
145 ret
->result_buf
= result_buf
;
150 static int general_allocate_string(UI
*ui
, const char *prompt
,
152 enum UI_string_types type
, int input_flags
,
153 char *result_buf
, int minsize
, int maxsize
,
154 const char *test_buf
)
157 UI_STRING
*s
= general_allocate_prompt(ui
, prompt
, prompt_freeable
,
158 type
, input_flags
, result_buf
);
161 if (allocate_string_stack(ui
) >= 0) {
162 s
->_
.string_data
.result_minsize
= minsize
;
163 s
->_
.string_data
.result_maxsize
= maxsize
;
164 s
->_
.string_data
.test_buf
= test_buf
;
165 ret
= sk_UI_STRING_push(ui
->strings
, s
);
166 /* sk_push() returns 0 on error. Let's adapt that */
175 static int general_allocate_boolean(UI
*ui
,
177 const char *action_desc
,
178 const char *ok_chars
,
179 const char *cancel_chars
,
181 enum UI_string_types type
,
182 int input_flags
, char *result_buf
)
188 if (ok_chars
== NULL
) {
189 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN
, ERR_R_PASSED_NULL_PARAMETER
);
190 } else if (cancel_chars
== NULL
) {
191 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN
, ERR_R_PASSED_NULL_PARAMETER
);
193 for (p
= ok_chars
; *p
; p
++) {
194 if (strchr(cancel_chars
, *p
)) {
195 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN
,
196 UI_R_COMMON_OK_AND_CANCEL_CHARACTERS
);
200 s
= general_allocate_prompt(ui
, prompt
, prompt_freeable
,
201 type
, input_flags
, result_buf
);
204 if (allocate_string_stack(ui
) >= 0) {
205 s
->_
.boolean_data
.action_desc
= action_desc
;
206 s
->_
.boolean_data
.ok_chars
= ok_chars
;
207 s
->_
.boolean_data
.cancel_chars
= cancel_chars
;
208 ret
= sk_UI_STRING_push(ui
->strings
, s
);
210 * sk_push() returns 0 on error. Let's adapt that
222 * Returns the index to the place in the stack or -1 for error. Uses a
223 * direct reference to the prompt.
225 int UI_add_input_string(UI
*ui
, const char *prompt
, int flags
,
226 char *result_buf
, int minsize
, int maxsize
)
228 return general_allocate_string(ui
, prompt
, 0,
229 UIT_PROMPT
, flags
, result_buf
, minsize
,
233 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
234 int UI_dup_input_string(UI
*ui
, const char *prompt
, int flags
,
235 char *result_buf
, int minsize
, int maxsize
)
237 char *prompt_copy
= NULL
;
240 prompt_copy
= OPENSSL_strdup(prompt
);
241 if (prompt_copy
== NULL
) {
242 UIerr(UI_F_UI_DUP_INPUT_STRING
, ERR_R_MALLOC_FAILURE
);
247 return general_allocate_string(ui
, prompt_copy
, 1,
248 UIT_PROMPT
, flags
, result_buf
, minsize
,
252 int UI_add_verify_string(UI
*ui
, const char *prompt
, int flags
,
253 char *result_buf
, int minsize
, int maxsize
,
254 const char *test_buf
)
256 return general_allocate_string(ui
, prompt
, 0,
257 UIT_VERIFY
, flags
, result_buf
, minsize
,
261 int UI_dup_verify_string(UI
*ui
, const char *prompt
, int flags
,
262 char *result_buf
, int minsize
, int maxsize
,
263 const char *test_buf
)
265 char *prompt_copy
= NULL
;
268 prompt_copy
= OPENSSL_strdup(prompt
);
269 if (prompt_copy
== NULL
) {
270 UIerr(UI_F_UI_DUP_VERIFY_STRING
, ERR_R_MALLOC_FAILURE
);
275 return general_allocate_string(ui
, prompt_copy
, 1,
276 UIT_VERIFY
, flags
, result_buf
, minsize
,
280 int UI_add_input_boolean(UI
*ui
, const char *prompt
, const char *action_desc
,
281 const char *ok_chars
, const char *cancel_chars
,
282 int flags
, char *result_buf
)
284 return general_allocate_boolean(ui
, prompt
, action_desc
,
285 ok_chars
, cancel_chars
, 0, UIT_BOOLEAN
,
289 int UI_dup_input_boolean(UI
*ui
, const char *prompt
, const char *action_desc
,
290 const char *ok_chars
, const char *cancel_chars
,
291 int flags
, char *result_buf
)
293 char *prompt_copy
= NULL
;
294 char *action_desc_copy
= NULL
;
295 char *ok_chars_copy
= NULL
;
296 char *cancel_chars_copy
= NULL
;
299 prompt_copy
= OPENSSL_strdup(prompt
);
300 if (prompt_copy
== NULL
) {
301 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN
, ERR_R_MALLOC_FAILURE
);
307 action_desc_copy
= OPENSSL_strdup(action_desc
);
308 if (action_desc_copy
== NULL
) {
309 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN
, ERR_R_MALLOC_FAILURE
);
315 ok_chars_copy
= OPENSSL_strdup(ok_chars
);
316 if (ok_chars_copy
== NULL
) {
317 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN
, ERR_R_MALLOC_FAILURE
);
323 cancel_chars_copy
= OPENSSL_strdup(cancel_chars
);
324 if (cancel_chars_copy
== NULL
) {
325 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN
, ERR_R_MALLOC_FAILURE
);
330 return general_allocate_boolean(ui
, prompt_copy
, action_desc_copy
,
331 ok_chars_copy
, cancel_chars_copy
, 1,
332 UIT_BOOLEAN
, flags
, result_buf
);
334 OPENSSL_free(prompt_copy
);
335 OPENSSL_free(action_desc_copy
);
336 OPENSSL_free(ok_chars_copy
);
337 OPENSSL_free(cancel_chars_copy
);
341 int UI_add_info_string(UI
*ui
, const char *text
)
343 return general_allocate_string(ui
, text
, 0, UIT_INFO
, 0, NULL
, 0, 0,
347 int UI_dup_info_string(UI
*ui
, const char *text
)
349 char *text_copy
= NULL
;
352 text_copy
= OPENSSL_strdup(text
);
353 if (text_copy
== NULL
) {
354 UIerr(UI_F_UI_DUP_INFO_STRING
, ERR_R_MALLOC_FAILURE
);
359 return general_allocate_string(ui
, text_copy
, 1, UIT_INFO
, 0, NULL
,
363 int UI_add_error_string(UI
*ui
, const char *text
)
365 return general_allocate_string(ui
, text
, 0, UIT_ERROR
, 0, NULL
, 0, 0,
369 int UI_dup_error_string(UI
*ui
, const char *text
)
371 char *text_copy
= NULL
;
374 text_copy
= OPENSSL_strdup(text
);
375 if (text_copy
== NULL
) {
376 UIerr(UI_F_UI_DUP_ERROR_STRING
, ERR_R_MALLOC_FAILURE
);
380 return general_allocate_string(ui
, text_copy
, 1, UIT_ERROR
, 0, NULL
,
384 char *UI_construct_prompt(UI
*ui
, const char *object_desc
,
385 const char *object_name
)
389 if (ui
->meth
->ui_construct_prompt
)
390 prompt
= ui
->meth
->ui_construct_prompt(ui
, object_desc
, object_name
);
392 char prompt1
[] = "Enter ";
393 char prompt2
[] = " for ";
394 char prompt3
[] = ":";
397 if (object_desc
== NULL
)
399 len
= sizeof(prompt1
) - 1 + strlen(object_desc
);
401 len
+= sizeof(prompt2
) - 1 + strlen(object_name
);
402 len
+= sizeof(prompt3
) - 1;
404 prompt
= OPENSSL_malloc(len
+ 1);
407 OPENSSL_strlcpy(prompt
, prompt1
, len
+ 1);
408 OPENSSL_strlcat(prompt
, object_desc
, len
+ 1);
410 OPENSSL_strlcat(prompt
, prompt2
, len
+ 1);
411 OPENSSL_strlcat(prompt
, object_name
, len
+ 1);
413 OPENSSL_strlcat(prompt
, prompt3
, len
+ 1);
418 void *UI_add_user_data(UI
*ui
, void *user_data
)
420 void *old_data
= ui
->user_data
;
421 ui
->user_data
= user_data
;
425 void *UI_get0_user_data(UI
*ui
)
427 return ui
->user_data
;
430 const char *UI_get0_result(UI
*ui
, int i
)
433 UIerr(UI_F_UI_GET0_RESULT
, UI_R_INDEX_TOO_SMALL
);
436 if (i
>= sk_UI_STRING_num(ui
->strings
)) {
437 UIerr(UI_F_UI_GET0_RESULT
, UI_R_INDEX_TOO_LARGE
);
440 return UI_get0_result_string(sk_UI_STRING_value(ui
->strings
, i
));
443 static int print_error(const char *str
, size_t len
, UI
*ui
)
447 memset(&uis
, 0, sizeof(uis
));
448 uis
.type
= UIT_ERROR
;
449 uis
.out_string
= str
;
451 if (ui
->meth
->ui_write_string
&& !ui
->meth
->ui_write_string(ui
, &uis
))
456 int UI_process(UI
*ui
)
460 if (ui
->meth
->ui_open_session
&& !ui
->meth
->ui_open_session(ui
))
463 if (ui
->flags
& UI_FLAG_PRINT_ERRORS
)
464 ERR_print_errors_cb((int (*)(const char *, size_t, void *))
465 print_error
, (void *)ui
);
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
)))
477 if (ui
->meth
->ui_flush
)
478 switch (ui
->meth
->ui_flush(ui
)) {
479 case -1: /* Interrupt/Cancel/something... */
485 default: /* Success */
490 for (i
= 0; i
< sk_UI_STRING_num(ui
->strings
); i
++) {
491 if (ui
->meth
->ui_read_string
) {
492 switch (ui
->meth
->ui_read_string(ui
,
493 sk_UI_STRING_value(ui
->strings
,
495 case -1: /* Interrupt/Cancel/something... */
501 default: /* Success */
508 if (ui
->meth
->ui_close_session
&& !ui
->meth
->ui_close_session(ui
))
513 int UI_ctrl(UI
*ui
, int cmd
, long i
, void *p
, void (*f
) (void))
516 UIerr(UI_F_UI_CTRL
, ERR_R_PASSED_NULL_PARAMETER
);
520 case UI_CTRL_PRINT_ERRORS
:
522 int save_flag
= ! !(ui
->flags
& UI_FLAG_PRINT_ERRORS
);
524 ui
->flags
|= UI_FLAG_PRINT_ERRORS
;
526 ui
->flags
&= ~UI_FLAG_PRINT_ERRORS
;
529 case UI_CTRL_IS_REDOABLE
:
530 return ! !(ui
->flags
& UI_FLAG_REDOABLE
);
534 UIerr(UI_F_UI_CTRL
, UI_R_UNKNOWN_CONTROL_COMMAND
);
538 int UI_set_ex_data(UI
*r
, int idx
, void *arg
)
540 return (CRYPTO_set_ex_data(&r
->ex_data
, idx
, arg
));
543 void *UI_get_ex_data(UI
*r
, int idx
)
545 return (CRYPTO_get_ex_data(&r
->ex_data
, idx
));
548 void UI_set_default_method(const UI_METHOD
*meth
)
550 default_UI_meth
= meth
;
553 const UI_METHOD
*UI_get_default_method(void)
555 if (default_UI_meth
== NULL
) {
556 default_UI_meth
= UI_OpenSSL();
558 return default_UI_meth
;
561 const UI_METHOD
*UI_get_method(UI
*ui
)
566 const UI_METHOD
*UI_set_method(UI
*ui
, const UI_METHOD
*meth
)
572 UI_METHOD
*UI_create_method(char *name
)
574 UI_METHOD
*ui_method
= OPENSSL_zalloc(sizeof(*ui_method
));
576 if (ui_method
!= NULL
)
577 ui_method
->name
= OPENSSL_strdup(name
);
582 * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
583 * (that is, it hasn't been allocated using UI_create_method(), you deserve
584 * anything Murphy can throw at you and more! You have been warned.
586 void UI_destroy_method(UI_METHOD
*ui_method
)
588 OPENSSL_free(ui_method
->name
);
589 ui_method
->name
= NULL
;
590 OPENSSL_free(ui_method
);
593 int UI_method_set_opener(UI_METHOD
*method
, int (*opener
) (UI
*ui
))
596 method
->ui_open_session
= opener
;
602 int UI_method_set_writer(UI_METHOD
*method
,
603 int (*writer
) (UI
*ui
, UI_STRING
*uis
))
606 method
->ui_write_string
= writer
;
612 int UI_method_set_flusher(UI_METHOD
*method
, int (*flusher
) (UI
*ui
))
615 method
->ui_flush
= flusher
;
621 int UI_method_set_reader(UI_METHOD
*method
,
622 int (*reader
) (UI
*ui
, UI_STRING
*uis
))
625 method
->ui_read_string
= reader
;
631 int UI_method_set_closer(UI_METHOD
*method
, int (*closer
) (UI
*ui
))
634 method
->ui_close_session
= closer
;
640 int UI_method_set_prompt_constructor(UI_METHOD
*method
,
641 char *(*prompt_constructor
) (UI
*ui
,
648 method
->ui_construct_prompt
= prompt_constructor
;
654 int (*UI_method_get_opener(UI_METHOD
*method
)) (UI
*) {
656 return method
->ui_open_session
;
661 int (*UI_method_get_writer(UI_METHOD
*method
)) (UI
*, UI_STRING
*) {
663 return method
->ui_write_string
;
668 int (*UI_method_get_flusher(UI_METHOD
*method
)) (UI
*) {
670 return method
->ui_flush
;
675 int (*UI_method_get_reader(UI_METHOD
*method
)) (UI
*, UI_STRING
*) {
677 return method
->ui_read_string
;
682 int (*UI_method_get_closer(UI_METHOD
*method
)) (UI
*) {
684 return method
->ui_close_session
;
689 char *(*UI_method_get_prompt_constructor(UI_METHOD
*method
)) (UI
*,
693 return method
->ui_construct_prompt
;
698 enum UI_string_types
UI_get_string_type(UI_STRING
*uis
)
705 int UI_get_input_flags(UI_STRING
*uis
)
709 return uis
->input_flags
;
712 const char *UI_get0_output_string(UI_STRING
*uis
)
716 return uis
->out_string
;
719 const char *UI_get0_action_string(UI_STRING
*uis
)
726 return uis
->_
.boolean_data
.action_desc
;
732 const char *UI_get0_result_string(UI_STRING
*uis
)
739 return uis
->result_buf
;
745 const char *UI_get0_test_string(UI_STRING
*uis
)
751 return uis
->_
.string_data
.test_buf
;
757 int UI_get_result_minsize(UI_STRING
*uis
)
764 return uis
->_
.string_data
.result_minsize
;
770 int UI_get_result_maxsize(UI_STRING
*uis
)
777 return uis
->_
.string_data
.result_maxsize
;
783 int UI_set_result(UI
*ui
, UI_STRING
*uis
, const char *result
)
785 int l
= strlen(result
);
787 ui
->flags
&= ~UI_FLAG_REDOABLE
;
795 char number1
[DECIMAL_SIZE(uis
->_
.string_data
.result_minsize
) + 1];
796 char number2
[DECIMAL_SIZE(uis
->_
.string_data
.result_maxsize
) + 1];
798 BIO_snprintf(number1
, sizeof(number1
), "%d",
799 uis
->_
.string_data
.result_minsize
);
800 BIO_snprintf(number2
, sizeof(number2
), "%d",
801 uis
->_
.string_data
.result_maxsize
);
803 if (l
< uis
->_
.string_data
.result_minsize
) {
804 ui
->flags
|= UI_FLAG_REDOABLE
;
805 UIerr(UI_F_UI_SET_RESULT
, UI_R_RESULT_TOO_SMALL
);
806 ERR_add_error_data(5, "You must type in ",
807 number1
, " to ", number2
, " characters");
810 if (l
> uis
->_
.string_data
.result_maxsize
) {
811 ui
->flags
|= UI_FLAG_REDOABLE
;
812 UIerr(UI_F_UI_SET_RESULT
, UI_R_RESULT_TOO_LARGE
);
813 ERR_add_error_data(5, "You must type in ",
814 number1
, " to ", number2
, " characters");
819 if (!uis
->result_buf
) {
820 UIerr(UI_F_UI_SET_RESULT
, UI_R_NO_RESULT_BUFFER
);
824 OPENSSL_strlcpy(uis
->result_buf
, result
,
825 uis
->_
.string_data
.result_maxsize
+ 1);
831 if (!uis
->result_buf
) {
832 UIerr(UI_F_UI_SET_RESULT
, UI_R_NO_RESULT_BUFFER
);
836 uis
->result_buf
[0] = '\0';
837 for (p
= result
; *p
; p
++) {
838 if (strchr(uis
->_
.boolean_data
.ok_chars
, *p
)) {
839 uis
->result_buf
[0] = uis
->_
.boolean_data
.ok_chars
[0];
842 if (strchr(uis
->_
.boolean_data
.cancel_chars
, *p
)) {
843 uis
->result_buf
[0] = uis
->_
.boolean_data
.cancel_chars
[0];