1 /* $OpenBSD: ui_lib.c,v 1.32 2017/01/29 17:49:23 beck Exp $ */
2 /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
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).
61 #include <openssl/opensslconf.h>
63 #include <openssl/buffer.h>
64 #include <openssl/err.h>
65 #include <openssl/ui.h>
69 static const UI_METHOD
*default_UI_meth
= NULL
;
74 return (UI_new_method(NULL
));
78 UI_new_method(const UI_METHOD
*method
)
82 ret
= malloc(sizeof(UI
));
84 UIerror(ERR_R_MALLOC_FAILURE
);
88 ret
->meth
= UI_get_default_method();
93 ret
->user_data
= NULL
;
95 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI
, ret
, &ret
->ex_data
);
100 free_string(UI_STRING
*uis
)
102 if (uis
->flags
& OUT_STRING_FREEABLE
) {
103 free((char *) uis
->out_string
);
106 free((char *)uis
->_
.boolean_data
.action_desc
);
107 free((char *)uis
->_
.boolean_data
.ok_chars
);
108 free((char *)uis
->_
.boolean_data
.cancel_chars
);
122 sk_UI_STRING_pop_free(ui
->strings
, free_string
);
123 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI
, ui
, &ui
->ex_data
);
128 allocate_string_stack(UI
*ui
)
130 if (ui
->strings
== NULL
) {
131 ui
->strings
= sk_UI_STRING_new_null();
132 if (ui
->strings
== NULL
) {
140 general_allocate_prompt(UI
*ui
, const char *prompt
, int prompt_freeable
,
141 enum UI_string_types type
, int input_flags
, char *result_buf
)
143 UI_STRING
*ret
= NULL
;
145 if (prompt
== NULL
) {
146 UIerror(ERR_R_PASSED_NULL_PARAMETER
);
147 } else if ((type
== UIT_PROMPT
|| type
== UIT_VERIFY
||
148 type
== UIT_BOOLEAN
) && result_buf
== NULL
) {
149 UIerror(UI_R_NO_RESULT_BUFFER
);
150 } else if ((ret
= malloc(sizeof(UI_STRING
)))) {
151 ret
->out_string
= prompt
;
152 ret
->flags
= prompt_freeable
? OUT_STRING_FREEABLE
: 0;
153 ret
->input_flags
= input_flags
;
155 ret
->result_buf
= result_buf
;
161 general_allocate_string(UI
*ui
, const char *prompt
, int prompt_freeable
,
162 enum UI_string_types type
, int input_flags
, char *result_buf
, int minsize
,
163 int maxsize
, 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 */
185 general_allocate_boolean(UI
*ui
, const char *prompt
, const char *action_desc
,
186 const char *ok_chars
, const char *cancel_chars
, int prompt_freeable
,
187 enum UI_string_types type
, int input_flags
, char *result_buf
)
193 if (ok_chars
== NULL
) {
194 UIerror(ERR_R_PASSED_NULL_PARAMETER
);
195 } else if (cancel_chars
== NULL
) {
196 UIerror(ERR_R_PASSED_NULL_PARAMETER
);
198 for (p
= ok_chars
; *p
; p
++) {
199 if (strchr(cancel_chars
, *p
)) {
200 UIerror(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 adapt
226 /* Returns the index to the place in the stack or -1 for error. Uses a
227 direct reference to the prompt. */
229 UI_add_input_string(UI
*ui
, const char *prompt
, int flags
, char *result_buf
,
230 int minsize
, int maxsize
)
232 return general_allocate_string(ui
, prompt
, 0, UIT_PROMPT
, flags
,
233 result_buf
, minsize
, maxsize
, NULL
);
236 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
238 UI_dup_input_string(UI
*ui
, const char *prompt
, int flags
, char *result_buf
,
239 int minsize
, int maxsize
)
241 char *prompt_copy
= NULL
;
244 prompt_copy
= strdup(prompt
);
245 if (prompt_copy
== NULL
) {
246 UIerror(ERR_R_MALLOC_FAILURE
);
250 return general_allocate_string(ui
, prompt_copy
, 1, UIT_PROMPT
, flags
,
251 result_buf
, minsize
, maxsize
, NULL
);
255 UI_add_verify_string(UI
*ui
, const char *prompt
, int flags
, char *result_buf
,
256 int minsize
, int maxsize
, const char *test_buf
)
258 return general_allocate_string(ui
, prompt
, 0, UIT_VERIFY
, flags
,
259 result_buf
, minsize
, maxsize
, test_buf
);
263 UI_dup_verify_string(UI
*ui
, const char *prompt
, int flags
,
264 char *result_buf
, int minsize
, int maxsize
, const char *test_buf
)
266 char *prompt_copy
= NULL
;
269 prompt_copy
= strdup(prompt
);
270 if (prompt_copy
== NULL
) {
271 UIerror(ERR_R_MALLOC_FAILURE
);
275 return general_allocate_string(ui
, prompt_copy
, 1, UIT_VERIFY
, flags
,
276 result_buf
, minsize
, maxsize
, test_buf
);
280 UI_add_input_boolean(UI
*ui
, const char *prompt
, const char *action_desc
,
281 const char *ok_chars
, const char *cancel_chars
, int flags
, char *result_buf
)
283 return general_allocate_boolean(ui
, prompt
, action_desc
, ok_chars
,
284 cancel_chars
, 0, UIT_BOOLEAN
, flags
, result_buf
);
288 UI_dup_input_boolean(UI
*ui
, const char *prompt
, const char *action_desc
,
289 const char *ok_chars
, const char *cancel_chars
, int flags
, char *result_buf
)
291 char *prompt_copy
= NULL
;
292 char *action_desc_copy
= NULL
;
293 char *ok_chars_copy
= NULL
;
294 char *cancel_chars_copy
= NULL
;
297 prompt_copy
= strdup(prompt
);
298 if (prompt_copy
== NULL
) {
299 UIerror(ERR_R_MALLOC_FAILURE
);
304 action_desc_copy
= strdup(action_desc
);
305 if (action_desc_copy
== NULL
) {
306 UIerror(ERR_R_MALLOC_FAILURE
);
311 ok_chars_copy
= strdup(ok_chars
);
312 if (ok_chars_copy
== NULL
) {
313 UIerror(ERR_R_MALLOC_FAILURE
);
318 cancel_chars_copy
= strdup(cancel_chars
);
319 if (cancel_chars_copy
== NULL
) {
320 UIerror(ERR_R_MALLOC_FAILURE
);
324 return general_allocate_boolean(ui
, prompt_copy
, action_desc_copy
,
325 ok_chars_copy
, cancel_chars_copy
, 1, UIT_BOOLEAN
, flags
,
330 free(action_desc_copy
);
332 free(cancel_chars_copy
);
337 UI_add_info_string(UI
*ui
, const char *text
)
339 return general_allocate_string(ui
, text
, 0, UIT_INFO
, 0, NULL
, 0, 0,
344 UI_dup_info_string(UI
*ui
, const char *text
)
346 char *text_copy
= NULL
;
349 text_copy
= strdup(text
);
350 if (text_copy
== NULL
) {
351 UIerror(ERR_R_MALLOC_FAILURE
);
355 return general_allocate_string(ui
, text_copy
, 1, UIT_INFO
, 0, NULL
,
360 UI_add_error_string(UI
*ui
, const char *text
)
362 return general_allocate_string(ui
, text
, 0, UIT_ERROR
, 0, NULL
, 0, 0,
367 UI_dup_error_string(UI
*ui
, const char *text
)
369 char *text_copy
= NULL
;
372 text_copy
= strdup(text
);
373 if (text_copy
== NULL
) {
374 UIerror(ERR_R_MALLOC_FAILURE
);
378 return general_allocate_string(ui
, text_copy
, 1, UIT_ERROR
, 0, NULL
,
383 UI_construct_prompt(UI
*ui
, const char *object_desc
, const char *object_name
)
387 if (ui
->meth
->ui_construct_prompt
)
388 return ui
->meth
->ui_construct_prompt(ui
, object_desc
,
391 if (object_desc
== NULL
)
394 if (object_name
== NULL
) {
395 if (asprintf(&prompt
, "Enter %s:", object_desc
) == -1)
398 if (asprintf(&prompt
, "Enter %s for %s:", object_desc
,
407 UI_add_user_data(UI
*ui
, void *user_data
)
409 void *old_data
= ui
->user_data
;
411 ui
->user_data
= user_data
;
416 UI_get0_user_data(UI
*ui
)
418 return ui
->user_data
;
422 UI_get0_result(UI
*ui
, int i
)
425 UIerror(UI_R_INDEX_TOO_SMALL
);
428 if (i
>= sk_UI_STRING_num(ui
->strings
)) {
429 UIerror(UI_R_INDEX_TOO_LARGE
);
432 return UI_get0_result_string(sk_UI_STRING_value(ui
->strings
, i
));
436 print_error(const char *str
, size_t len
, UI
*ui
)
440 memset(&uis
, 0, sizeof(uis
));
441 uis
.type
= UIT_ERROR
;
442 uis
.out_string
= str
;
444 if (ui
->meth
->ui_write_string
&&
445 !ui
->meth
->ui_write_string(ui
, &uis
))
455 if (ui
->meth
->ui_open_session
&& !ui
->meth
->ui_open_session(ui
))
458 if (ui
->flags
& UI_FLAG_PRINT_ERRORS
)
460 (int (*)(const char *, size_t, void *)) print_error
,
463 for (i
= 0; i
< sk_UI_STRING_num(ui
->strings
); i
++) {
464 if (ui
->meth
->ui_write_string
&&
465 !ui
->meth
->ui_write_string(ui
,
466 sk_UI_STRING_value(ui
->strings
, i
))) {
472 if (ui
->meth
->ui_flush
)
473 switch (ui
->meth
->ui_flush(ui
)) {
474 case -1: /* Interrupt/Cancel/something... */
480 default: /* Success */
485 for (i
= 0; i
< sk_UI_STRING_num(ui
->strings
); i
++) {
486 if (ui
->meth
->ui_read_string
) {
487 switch (ui
->meth
->ui_read_string(ui
,
488 sk_UI_STRING_value(ui
->strings
, i
))) {
489 case -1: /* Interrupt/Cancel/something... */
490 ui
->flags
&= ~UI_FLAG_REDOABLE
;
496 default: /* Success */
504 if (ui
->meth
->ui_close_session
&& !ui
->meth
->ui_close_session(ui
))
510 UI_ctrl(UI
*ui
, int cmd
, long i
, void *p
, void (*f
) (void))
513 UIerror(ERR_R_PASSED_NULL_PARAMETER
);
517 case UI_CTRL_PRINT_ERRORS
:
519 int save_flag
= !!(ui
->flags
& UI_FLAG_PRINT_ERRORS
);
521 ui
->flags
|= UI_FLAG_PRINT_ERRORS
;
523 ui
->flags
&= ~UI_FLAG_PRINT_ERRORS
;
526 case UI_CTRL_IS_REDOABLE
:
527 return !!(ui
->flags
& UI_FLAG_REDOABLE
);
531 UIerror(UI_R_UNKNOWN_CONTROL_COMMAND
);
536 UI_get_ex_new_index(long argl
, void *argp
, CRYPTO_EX_new
*new_func
,
537 CRYPTO_EX_dup
*dup_func
, CRYPTO_EX_free
*free_func
)
539 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI
, argl
, argp
,
540 new_func
, dup_func
, free_func
);
544 UI_set_ex_data(UI
*r
, int idx
, void *arg
)
546 return (CRYPTO_set_ex_data(&r
->ex_data
, idx
, arg
));
550 UI_get_ex_data(UI
*r
, int idx
)
552 return (CRYPTO_get_ex_data(&r
->ex_data
, idx
));
556 UI_set_default_method(const UI_METHOD
*meth
)
558 default_UI_meth
= meth
;
562 UI_get_default_method(void)
564 if (default_UI_meth
== NULL
) {
565 default_UI_meth
= UI_OpenSSL();
567 return default_UI_meth
;
571 UI_get_method(UI
*ui
)
577 UI_set_method(UI
*ui
, const UI_METHOD
*meth
)
585 UI_create_method(char *name
)
587 UI_METHOD
*ui_method
= calloc(1, sizeof(UI_METHOD
));
589 if (ui_method
&& name
)
590 ui_method
->name
= 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 UI_destroy_method(UI_METHOD
*ui_method
)
601 free(ui_method
->name
);
602 ui_method
->name
= NULL
;
607 UI_method_set_opener(UI_METHOD
*method
, int (*opener
)(UI
*ui
))
610 method
->ui_open_session
= opener
;
617 UI_method_set_writer(UI_METHOD
*method
, int (*writer
)(UI
*ui
, UI_STRING
*uis
))
620 method
->ui_write_string
= writer
;
627 UI_method_set_flusher(UI_METHOD
*method
, int (*flusher
)(UI
*ui
))
630 method
->ui_flush
= flusher
;
637 UI_method_set_reader(UI_METHOD
*method
, int (*reader
)(UI
*ui
, UI_STRING
*uis
))
640 method
->ui_read_string
= reader
;
647 UI_method_set_closer(UI_METHOD
*method
, int (*closer
)(UI
*ui
))
650 method
->ui_close_session
= closer
;
657 UI_method_set_prompt_constructor(UI_METHOD
*method
,
658 char *(*prompt_constructor
)(UI
*ui
, const char *object_desc
,
659 const char *object_name
))
662 method
->ui_construct_prompt
= prompt_constructor
;
669 (*UI_method_get_opener(UI_METHOD
* method
))(UI
*)
672 return method
->ui_open_session
;
678 (*UI_method_get_writer(UI_METHOD
*method
))(UI
*, UI_STRING
*)
681 return method
->ui_write_string
;
687 (*UI_method_get_flusher(UI_METHOD
*method
)) (UI
*)
690 return method
->ui_flush
;
696 (*UI_method_get_reader(UI_METHOD
*method
))(UI
*, UI_STRING
*)
699 return method
->ui_read_string
;
705 (*UI_method_get_closer(UI_METHOD
*method
))(UI
*)
708 return method
->ui_close_session
;
714 (*UI_method_get_prompt_constructor(UI_METHOD
*method
))(UI
*, const char *,
718 return method
->ui_construct_prompt
;
724 UI_get_string_type(UI_STRING
*uis
)
732 UI_get_input_flags(UI_STRING
*uis
)
736 return uis
->input_flags
;
740 UI_get0_output_string(UI_STRING
*uis
)
744 return uis
->out_string
;
748 UI_get0_action_string(UI_STRING
*uis
)
755 return uis
->_
.boolean_data
.action_desc
;
762 UI_get0_result_string(UI_STRING
*uis
)
769 return uis
->result_buf
;
776 UI_get0_test_string(UI_STRING
*uis
)
782 return uis
->_
.string_data
.test_buf
;
789 UI_get_result_minsize(UI_STRING
*uis
)
796 return uis
->_
.string_data
.result_minsize
;
803 UI_get_result_maxsize(UI_STRING
*uis
)
810 return uis
->_
.string_data
.result_maxsize
;
817 UI_set_result(UI
*ui
, UI_STRING
*uis
, const char *result
)
819 int l
= strlen(result
);
821 ui
->flags
&= ~UI_FLAG_REDOABLE
;
828 if (l
< uis
->_
.string_data
.result_minsize
) {
829 ui
->flags
|= UI_FLAG_REDOABLE
;
830 UIerror(UI_R_RESULT_TOO_SMALL
);
831 ERR_asprintf_error_data
832 ("You must type in %d to %d characters",
833 uis
->_
.string_data
.result_minsize
,
834 uis
->_
.string_data
.result_maxsize
);
837 if (l
> uis
->_
.string_data
.result_maxsize
) {
838 ui
->flags
|= UI_FLAG_REDOABLE
;
839 UIerror(UI_R_RESULT_TOO_LARGE
);
840 ERR_asprintf_error_data
841 ("You must type in %d to %d characters",
842 uis
->_
.string_data
.result_minsize
,
843 uis
->_
.string_data
.result_maxsize
);
846 if (!uis
->result_buf
) {
847 UIerror(UI_R_NO_RESULT_BUFFER
);
850 strlcpy(uis
->result_buf
, result
,
851 uis
->_
.string_data
.result_maxsize
+ 1);
857 if (!uis
->result_buf
) {
858 UIerror(UI_R_NO_RESULT_BUFFER
);
861 uis
->result_buf
[0] = '\0';
862 for (p
= result
; *p
; p
++) {
863 if (strchr(uis
->_
.boolean_data
.ok_chars
, *p
)) {
865 uis
->_
.boolean_data
.ok_chars
[0];
868 if (strchr(uis
->_
.boolean_data
.cancel_chars
, *p
)) {
870 uis
->_
.boolean_data
.cancel_chars
[0];