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 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI
, ret
, &ret
->ex_data
);
99 static void free_string(UI_STRING
*uis
)
101 if (uis
->flags
& OUT_STRING_FREEABLE
) {
102 OPENSSL_free((char *)uis
->out_string
);
105 OPENSSL_free((char *)uis
->_
.boolean_data
.action_desc
);
106 OPENSSL_free((char *)uis
->_
.boolean_data
.ok_chars
);
107 OPENSSL_free((char *)uis
->_
.boolean_data
.cancel_chars
);
120 sk_UI_STRING_pop_free(ui
->strings
, free_string
);
121 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI
, ui
, &ui
->ex_data
);
122 CRYPTO_THREAD_lock_free(ui
->lock
);
126 static int allocate_string_stack(UI
*ui
)
128 if (ui
->strings
== NULL
) {
129 ui
->strings
= sk_UI_STRING_new_null();
130 if (ui
->strings
== NULL
) {
137 static UI_STRING
*general_allocate_prompt(UI
*ui
, const char *prompt
,
139 enum UI_string_types type
,
140 int input_flags
, char *result_buf
)
142 UI_STRING
*ret
= NULL
;
144 if (prompt
== NULL
) {
145 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT
, ERR_R_PASSED_NULL_PARAMETER
);
146 } else if ((type
== UIT_PROMPT
|| type
== UIT_VERIFY
147 || type
== UIT_BOOLEAN
) && result_buf
== NULL
) {
148 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT
, UI_R_NO_RESULT_BUFFER
);
149 } else if ((ret
= OPENSSL_malloc(sizeof(*ret
))) != NULL
) {
150 ret
->out_string
= prompt
;
151 ret
->flags
= prompt_freeable
? OUT_STRING_FREEABLE
: 0;
152 ret
->input_flags
= input_flags
;
154 ret
->result_buf
= result_buf
;
159 static int general_allocate_string(UI
*ui
, const char *prompt
,
161 enum UI_string_types type
, int input_flags
,
162 char *result_buf
, int minsize
, int maxsize
,
163 const char *test_buf
)
166 UI_STRING
*s
= general_allocate_prompt(ui
, prompt
, prompt_freeable
,
167 type
, input_flags
, result_buf
);
170 if (allocate_string_stack(ui
) >= 0) {
171 s
->_
.string_data
.result_minsize
= minsize
;
172 s
->_
.string_data
.result_maxsize
= maxsize
;
173 s
->_
.string_data
.test_buf
= test_buf
;
174 ret
= sk_UI_STRING_push(ui
->strings
, s
);
175 /* sk_push() returns 0 on error. Let's adapt that */
184 static int general_allocate_boolean(UI
*ui
,
186 const char *action_desc
,
187 const char *ok_chars
,
188 const char *cancel_chars
,
190 enum UI_string_types type
,
191 int input_flags
, char *result_buf
)
197 if (ok_chars
== NULL
) {
198 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN
, ERR_R_PASSED_NULL_PARAMETER
);
199 } else if (cancel_chars
== NULL
) {
200 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN
, ERR_R_PASSED_NULL_PARAMETER
);
202 for (p
= ok_chars
; *p
; p
++) {
203 if (strchr(cancel_chars
, *p
)) {
204 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN
,
205 UI_R_COMMON_OK_AND_CANCEL_CHARACTERS
);
209 s
= general_allocate_prompt(ui
, prompt
, prompt_freeable
,
210 type
, input_flags
, result_buf
);
213 if (allocate_string_stack(ui
) >= 0) {
214 s
->_
.boolean_data
.action_desc
= action_desc
;
215 s
->_
.boolean_data
.ok_chars
= ok_chars
;
216 s
->_
.boolean_data
.cancel_chars
= cancel_chars
;
217 ret
= sk_UI_STRING_push(ui
->strings
, s
);
219 * sk_push() returns 0 on error. Let's adapt that
231 * Returns the index to the place in the stack or -1 for error. Uses a
232 * direct reference to the prompt.
234 int UI_add_input_string(UI
*ui
, const char *prompt
, int flags
,
235 char *result_buf
, int minsize
, int maxsize
)
237 return general_allocate_string(ui
, prompt
, 0,
238 UIT_PROMPT
, flags
, result_buf
, minsize
,
242 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
243 int UI_dup_input_string(UI
*ui
, const char *prompt
, int flags
,
244 char *result_buf
, int minsize
, int maxsize
)
246 char *prompt_copy
= NULL
;
249 prompt_copy
= OPENSSL_strdup(prompt
);
250 if (prompt_copy
== NULL
) {
251 UIerr(UI_F_UI_DUP_INPUT_STRING
, ERR_R_MALLOC_FAILURE
);
256 return general_allocate_string(ui
, prompt_copy
, 1,
257 UIT_PROMPT
, flags
, result_buf
, minsize
,
261 int UI_add_verify_string(UI
*ui
, const char *prompt
, int flags
,
262 char *result_buf
, int minsize
, int maxsize
,
263 const char *test_buf
)
265 return general_allocate_string(ui
, prompt
, 0,
266 UIT_VERIFY
, flags
, result_buf
, minsize
,
270 int UI_dup_verify_string(UI
*ui
, const char *prompt
, int flags
,
271 char *result_buf
, int minsize
, int maxsize
,
272 const char *test_buf
)
274 char *prompt_copy
= NULL
;
277 prompt_copy
= OPENSSL_strdup(prompt
);
278 if (prompt_copy
== NULL
) {
279 UIerr(UI_F_UI_DUP_VERIFY_STRING
, ERR_R_MALLOC_FAILURE
);
284 return general_allocate_string(ui
, prompt_copy
, 1,
285 UIT_VERIFY
, flags
, result_buf
, minsize
,
289 int UI_add_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 return general_allocate_boolean(ui
, prompt
, action_desc
,
294 ok_chars
, cancel_chars
, 0, UIT_BOOLEAN
,
298 int UI_dup_input_boolean(UI
*ui
, const char *prompt
, const char *action_desc
,
299 const char *ok_chars
, const char *cancel_chars
,
300 int flags
, char *result_buf
)
302 char *prompt_copy
= NULL
;
303 char *action_desc_copy
= NULL
;
304 char *ok_chars_copy
= NULL
;
305 char *cancel_chars_copy
= NULL
;
308 prompt_copy
= OPENSSL_strdup(prompt
);
309 if (prompt_copy
== NULL
) {
310 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN
, ERR_R_MALLOC_FAILURE
);
316 action_desc_copy
= OPENSSL_strdup(action_desc
);
317 if (action_desc_copy
== NULL
) {
318 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN
, ERR_R_MALLOC_FAILURE
);
324 ok_chars_copy
= OPENSSL_strdup(ok_chars
);
325 if (ok_chars_copy
== NULL
) {
326 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN
, ERR_R_MALLOC_FAILURE
);
332 cancel_chars_copy
= OPENSSL_strdup(cancel_chars
);
333 if (cancel_chars_copy
== NULL
) {
334 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN
, ERR_R_MALLOC_FAILURE
);
339 return general_allocate_boolean(ui
, prompt_copy
, action_desc_copy
,
340 ok_chars_copy
, cancel_chars_copy
, 1,
341 UIT_BOOLEAN
, flags
, result_buf
);
343 OPENSSL_free(prompt_copy
);
344 OPENSSL_free(action_desc_copy
);
345 OPENSSL_free(ok_chars_copy
);
346 OPENSSL_free(cancel_chars_copy
);
350 int UI_add_info_string(UI
*ui
, const char *text
)
352 return general_allocate_string(ui
, text
, 0, UIT_INFO
, 0, NULL
, 0, 0,
356 int UI_dup_info_string(UI
*ui
, const char *text
)
358 char *text_copy
= NULL
;
361 text_copy
= OPENSSL_strdup(text
);
362 if (text_copy
== NULL
) {
363 UIerr(UI_F_UI_DUP_INFO_STRING
, ERR_R_MALLOC_FAILURE
);
368 return general_allocate_string(ui
, text_copy
, 1, UIT_INFO
, 0, NULL
,
372 int UI_add_error_string(UI
*ui
, const char *text
)
374 return general_allocate_string(ui
, text
, 0, UIT_ERROR
, 0, NULL
, 0, 0,
378 int UI_dup_error_string(UI
*ui
, const char *text
)
380 char *text_copy
= NULL
;
383 text_copy
= OPENSSL_strdup(text
);
384 if (text_copy
== NULL
) {
385 UIerr(UI_F_UI_DUP_ERROR_STRING
, ERR_R_MALLOC_FAILURE
);
389 return general_allocate_string(ui
, text_copy
, 1, UIT_ERROR
, 0, NULL
,
393 char *UI_construct_prompt(UI
*ui
, const char *object_desc
,
394 const char *object_name
)
398 if (ui
->meth
->ui_construct_prompt
)
399 prompt
= ui
->meth
->ui_construct_prompt(ui
, object_desc
, object_name
);
401 char prompt1
[] = "Enter ";
402 char prompt2
[] = " for ";
403 char prompt3
[] = ":";
406 if (object_desc
== NULL
)
408 len
= sizeof(prompt1
) - 1 + strlen(object_desc
);
410 len
+= sizeof(prompt2
) - 1 + strlen(object_name
);
411 len
+= sizeof(prompt3
) - 1;
413 prompt
= OPENSSL_malloc(len
+ 1);
416 OPENSSL_strlcpy(prompt
, prompt1
, len
+ 1);
417 OPENSSL_strlcat(prompt
, object_desc
, len
+ 1);
419 OPENSSL_strlcat(prompt
, prompt2
, len
+ 1);
420 OPENSSL_strlcat(prompt
, object_name
, len
+ 1);
422 OPENSSL_strlcat(prompt
, prompt3
, len
+ 1);
427 void *UI_add_user_data(UI
*ui
, void *user_data
)
429 void *old_data
= ui
->user_data
;
430 ui
->user_data
= user_data
;
434 void *UI_get0_user_data(UI
*ui
)
436 return ui
->user_data
;
439 const char *UI_get0_result(UI
*ui
, int i
)
442 UIerr(UI_F_UI_GET0_RESULT
, UI_R_INDEX_TOO_SMALL
);
445 if (i
>= sk_UI_STRING_num(ui
->strings
)) {
446 UIerr(UI_F_UI_GET0_RESULT
, UI_R_INDEX_TOO_LARGE
);
449 return UI_get0_result_string(sk_UI_STRING_value(ui
->strings
, i
));
452 static int print_error(const char *str
, size_t len
, UI
*ui
)
456 memset(&uis
, 0, sizeof(uis
));
457 uis
.type
= UIT_ERROR
;
458 uis
.out_string
= str
;
460 if (ui
->meth
->ui_write_string
&& !ui
->meth
->ui_write_string(ui
, &uis
))
465 int UI_process(UI
*ui
)
469 if (ui
->meth
->ui_open_session
&& !ui
->meth
->ui_open_session(ui
))
472 if (ui
->flags
& UI_FLAG_PRINT_ERRORS
)
473 ERR_print_errors_cb((int (*)(const char *, size_t, void *))
474 print_error
, (void *)ui
);
476 for (i
= 0; i
< sk_UI_STRING_num(ui
->strings
); i
++) {
477 if (ui
->meth
->ui_write_string
478 && !ui
->meth
->ui_write_string(ui
,
479 sk_UI_STRING_value(ui
->strings
, i
)))
486 if (ui
->meth
->ui_flush
)
487 switch (ui
->meth
->ui_flush(ui
)) {
488 case -1: /* Interrupt/Cancel/something... */
494 default: /* Success */
499 for (i
= 0; i
< sk_UI_STRING_num(ui
->strings
); i
++) {
500 if (ui
->meth
->ui_read_string
) {
501 switch (ui
->meth
->ui_read_string(ui
,
502 sk_UI_STRING_value(ui
->strings
,
504 case -1: /* Interrupt/Cancel/something... */
510 default: /* Success */
517 if (ui
->meth
->ui_close_session
&& !ui
->meth
->ui_close_session(ui
))
522 int UI_ctrl(UI
*ui
, int cmd
, long i
, void *p
, void (*f
) (void))
525 UIerr(UI_F_UI_CTRL
, ERR_R_PASSED_NULL_PARAMETER
);
529 case UI_CTRL_PRINT_ERRORS
:
531 int save_flag
= ! !(ui
->flags
& UI_FLAG_PRINT_ERRORS
);
533 ui
->flags
|= UI_FLAG_PRINT_ERRORS
;
535 ui
->flags
&= ~UI_FLAG_PRINT_ERRORS
;
538 case UI_CTRL_IS_REDOABLE
:
539 return ! !(ui
->flags
& UI_FLAG_REDOABLE
);
543 UIerr(UI_F_UI_CTRL
, UI_R_UNKNOWN_CONTROL_COMMAND
);
547 int UI_set_ex_data(UI
*r
, int idx
, void *arg
)
549 return (CRYPTO_set_ex_data(&r
->ex_data
, idx
, arg
));
552 void *UI_get_ex_data(UI
*r
, int idx
)
554 return (CRYPTO_get_ex_data(&r
->ex_data
, idx
));
557 void UI_set_default_method(const UI_METHOD
*meth
)
559 default_UI_meth
= meth
;
562 const UI_METHOD
*UI_get_default_method(void)
564 if (default_UI_meth
== NULL
) {
565 default_UI_meth
= UI_OpenSSL();
567 return default_UI_meth
;
570 const UI_METHOD
*UI_get_method(UI
*ui
)
575 const UI_METHOD
*UI_set_method(UI
*ui
, const UI_METHOD
*meth
)
581 UI_METHOD
*UI_create_method(char *name
)
583 UI_METHOD
*ui_method
= OPENSSL_zalloc(sizeof(*ui_method
));
585 if (ui_method
!= NULL
)
586 ui_method
->name
= OPENSSL_strdup(name
);
591 * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
592 * (that is, it hasn't been allocated using UI_create_method(), you deserve
593 * anything Murphy can throw at you and more! You have been warned.
595 void UI_destroy_method(UI_METHOD
*ui_method
)
597 OPENSSL_free(ui_method
->name
);
598 ui_method
->name
= NULL
;
599 OPENSSL_free(ui_method
);
602 int UI_method_set_opener(UI_METHOD
*method
, int (*opener
) (UI
*ui
))
605 method
->ui_open_session
= opener
;
611 int UI_method_set_writer(UI_METHOD
*method
,
612 int (*writer
) (UI
*ui
, UI_STRING
*uis
))
615 method
->ui_write_string
= writer
;
621 int UI_method_set_flusher(UI_METHOD
*method
, int (*flusher
) (UI
*ui
))
624 method
->ui_flush
= flusher
;
630 int UI_method_set_reader(UI_METHOD
*method
,
631 int (*reader
) (UI
*ui
, UI_STRING
*uis
))
634 method
->ui_read_string
= reader
;
640 int UI_method_set_closer(UI_METHOD
*method
, int (*closer
) (UI
*ui
))
643 method
->ui_close_session
= closer
;
649 int UI_method_set_prompt_constructor(UI_METHOD
*method
,
650 char *(*prompt_constructor
) (UI
*ui
,
657 method
->ui_construct_prompt
= prompt_constructor
;
663 int (*UI_method_get_opener(UI_METHOD
*method
)) (UI
*) {
665 return method
->ui_open_session
;
670 int (*UI_method_get_writer(UI_METHOD
*method
)) (UI
*, UI_STRING
*) {
672 return method
->ui_write_string
;
677 int (*UI_method_get_flusher(UI_METHOD
*method
)) (UI
*) {
679 return method
->ui_flush
;
684 int (*UI_method_get_reader(UI_METHOD
*method
)) (UI
*, UI_STRING
*) {
686 return method
->ui_read_string
;
691 int (*UI_method_get_closer(UI_METHOD
*method
)) (UI
*) {
693 return method
->ui_close_session
;
698 char *(*UI_method_get_prompt_constructor(UI_METHOD
*method
)) (UI
*,
702 return method
->ui_construct_prompt
;
707 enum UI_string_types
UI_get_string_type(UI_STRING
*uis
)
714 int UI_get_input_flags(UI_STRING
*uis
)
718 return uis
->input_flags
;
721 const char *UI_get0_output_string(UI_STRING
*uis
)
725 return uis
->out_string
;
728 const char *UI_get0_action_string(UI_STRING
*uis
)
735 return uis
->_
.boolean_data
.action_desc
;
741 const char *UI_get0_result_string(UI_STRING
*uis
)
748 return uis
->result_buf
;
754 const char *UI_get0_test_string(UI_STRING
*uis
)
760 return uis
->_
.string_data
.test_buf
;
766 int UI_get_result_minsize(UI_STRING
*uis
)
773 return uis
->_
.string_data
.result_minsize
;
779 int UI_get_result_maxsize(UI_STRING
*uis
)
786 return uis
->_
.string_data
.result_maxsize
;
792 int UI_set_result(UI
*ui
, UI_STRING
*uis
, const char *result
)
794 int l
= strlen(result
);
796 ui
->flags
&= ~UI_FLAG_REDOABLE
;
804 char number1
[DECIMAL_SIZE(uis
->_
.string_data
.result_minsize
) + 1];
805 char number2
[DECIMAL_SIZE(uis
->_
.string_data
.result_maxsize
) + 1];
807 BIO_snprintf(number1
, sizeof(number1
), "%d",
808 uis
->_
.string_data
.result_minsize
);
809 BIO_snprintf(number2
, sizeof(number2
), "%d",
810 uis
->_
.string_data
.result_maxsize
);
812 if (l
< uis
->_
.string_data
.result_minsize
) {
813 ui
->flags
|= UI_FLAG_REDOABLE
;
814 UIerr(UI_F_UI_SET_RESULT
, UI_R_RESULT_TOO_SMALL
);
815 ERR_add_error_data(5, "You must type in ",
816 number1
, " to ", number2
, " characters");
819 if (l
> uis
->_
.string_data
.result_maxsize
) {
820 ui
->flags
|= UI_FLAG_REDOABLE
;
821 UIerr(UI_F_UI_SET_RESULT
, UI_R_RESULT_TOO_LARGE
);
822 ERR_add_error_data(5, "You must type in ",
823 number1
, " to ", number2
, " characters");
828 if (!uis
->result_buf
) {
829 UIerr(UI_F_UI_SET_RESULT
, UI_R_NO_RESULT_BUFFER
);
833 OPENSSL_strlcpy(uis
->result_buf
, result
,
834 uis
->_
.string_data
.result_maxsize
+ 1);
840 if (!uis
->result_buf
) {
841 UIerr(UI_F_UI_SET_RESULT
, UI_R_NO_RESULT_BUFFER
);
845 uis
->result_buf
[0] = '\0';
846 for (p
= result
; *p
; p
++) {
847 if (strchr(uis
->_
.boolean_data
.ok_chars
, *p
)) {
848 uis
->result_buf
[0] = uis
->_
.boolean_data
.ok_chars
[0];
851 if (strchr(uis
->_
.boolean_data
.cancel_chars
, *p
)) {
852 uis
->result_buf
[0] = uis
->_
.boolean_data
.cancel_chars
[0];