2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/e_os2.h>
13 #include <openssl/buffer.h>
14 #include <openssl/ui.h>
15 #include <openssl/err.h>
20 return (UI_new_method(NULL));
23 UI *UI_new_method(const UI_METHOD *method)
25 UI *ret = OPENSSL_zalloc(sizeof(*ret));
28 UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
32 ret->lock = CRYPTO_THREAD_lock_new();
33 if (ret->lock == NULL) {
34 UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
40 ret->meth = UI_get_default_method();
44 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data)) {
51 static void free_string(UI_STRING *uis)
53 if (uis->flags & OUT_STRING_FREEABLE) {
54 OPENSSL_free((char *)uis->out_string);
57 OPENSSL_free((char *)uis->_.boolean_data.action_desc);
58 OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
59 OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
76 if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
77 ui->meth->ui_destroy_data(ui, ui->user_data);
79 sk_UI_STRING_pop_free(ui->strings, free_string);
80 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
81 CRYPTO_THREAD_lock_free(ui->lock);
85 static int allocate_string_stack(UI *ui)
87 if (ui->strings == NULL) {
88 ui->strings = sk_UI_STRING_new_null();
89 if (ui->strings == NULL) {
96 static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
98 enum UI_string_types type,
99 int input_flags, char *result_buf)
101 UI_STRING *ret = NULL;
103 if (prompt == NULL) {
104 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
105 } else if ((type == UIT_PROMPT || type == UIT_VERIFY
106 || type == UIT_BOOLEAN) && result_buf == NULL) {
107 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
108 } else if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL) {
109 ret->out_string = prompt;
110 ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
111 ret->input_flags = input_flags;
113 ret->result_buf = result_buf;
118 static int general_allocate_string(UI *ui, const char *prompt,
120 enum UI_string_types type, int input_flags,
121 char *result_buf, int minsize, int maxsize,
122 const char *test_buf)
125 UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
126 type, input_flags, result_buf);
129 if (allocate_string_stack(ui) >= 0) {
130 s->_.string_data.result_minsize = minsize;
131 s->_.string_data.result_maxsize = maxsize;
132 s->_.string_data.test_buf = test_buf;
133 ret = sk_UI_STRING_push(ui->strings, s);
134 /* sk_push() returns 0 on error. Let's adapt that */
145 static int general_allocate_boolean(UI *ui,
147 const char *action_desc,
148 const char *ok_chars,
149 const char *cancel_chars,
151 enum UI_string_types type,
152 int input_flags, char *result_buf)
158 if (ok_chars == NULL) {
159 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
160 } else if (cancel_chars == NULL) {
161 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
163 for (p = ok_chars; *p != '\0'; p++) {
164 if (strchr(cancel_chars, *p) != NULL) {
165 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
166 UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
170 s = general_allocate_prompt(ui, prompt, prompt_freeable,
171 type, input_flags, result_buf);
174 if (allocate_string_stack(ui) >= 0) {
175 s->_.boolean_data.action_desc = action_desc;
176 s->_.boolean_data.ok_chars = ok_chars;
177 s->_.boolean_data.cancel_chars = cancel_chars;
178 ret = sk_UI_STRING_push(ui->strings, s);
180 * sk_push() returns 0 on error. Let's adapt that
194 * Returns the index to the place in the stack or -1 for error. Uses a
195 * direct reference to the prompt.
197 int UI_add_input_string(UI *ui, const char *prompt, int flags,
198 char *result_buf, int minsize, int maxsize)
200 return general_allocate_string(ui, prompt, 0,
201 UIT_PROMPT, flags, result_buf, minsize,
205 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
206 int UI_dup_input_string(UI *ui, const char *prompt, int flags,
207 char *result_buf, int minsize, int maxsize)
209 char *prompt_copy = NULL;
211 if (prompt != NULL) {
212 prompt_copy = OPENSSL_strdup(prompt);
213 if (prompt_copy == NULL) {
214 UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
219 return general_allocate_string(ui, prompt_copy, 1,
220 UIT_PROMPT, flags, result_buf, minsize,
224 int UI_add_verify_string(UI *ui, const char *prompt, int flags,
225 char *result_buf, int minsize, int maxsize,
226 const char *test_buf)
228 return general_allocate_string(ui, prompt, 0,
229 UIT_VERIFY, flags, result_buf, minsize,
233 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
234 char *result_buf, int minsize, int maxsize,
235 const char *test_buf)
237 char *prompt_copy = NULL;
239 if (prompt != NULL) {
240 prompt_copy = OPENSSL_strdup(prompt);
241 if (prompt_copy == NULL) {
242 UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
247 return general_allocate_string(ui, prompt_copy, 1,
248 UIT_VERIFY, flags, result_buf, minsize,
252 int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
253 const char *ok_chars, const char *cancel_chars,
254 int flags, char *result_buf)
256 return general_allocate_boolean(ui, prompt, action_desc,
257 ok_chars, cancel_chars, 0, UIT_BOOLEAN,
261 int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
262 const char *ok_chars, const char *cancel_chars,
263 int flags, char *result_buf)
265 char *prompt_copy = NULL;
266 char *action_desc_copy = NULL;
267 char *ok_chars_copy = NULL;
268 char *cancel_chars_copy = NULL;
270 if (prompt != NULL) {
271 prompt_copy = OPENSSL_strdup(prompt);
272 if (prompt_copy == NULL) {
273 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
278 if (action_desc != NULL) {
279 action_desc_copy = OPENSSL_strdup(action_desc);
280 if (action_desc_copy == NULL) {
281 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
286 if (ok_chars != NULL) {
287 ok_chars_copy = OPENSSL_strdup(ok_chars);
288 if (ok_chars_copy == NULL) {
289 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
294 if (cancel_chars != NULL) {
295 cancel_chars_copy = OPENSSL_strdup(cancel_chars);
296 if (cancel_chars_copy == NULL) {
297 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
302 return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
303 ok_chars_copy, cancel_chars_copy, 1,
304 UIT_BOOLEAN, flags, result_buf);
306 OPENSSL_free(prompt_copy);
307 OPENSSL_free(action_desc_copy);
308 OPENSSL_free(ok_chars_copy);
309 OPENSSL_free(cancel_chars_copy);
313 int UI_add_info_string(UI *ui, const char *text)
315 return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
319 int UI_dup_info_string(UI *ui, const char *text)
321 char *text_copy = NULL;
324 text_copy = OPENSSL_strdup(text);
325 if (text_copy == NULL) {
326 UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
331 return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
335 int UI_add_error_string(UI *ui, const char *text)
337 return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
341 int UI_dup_error_string(UI *ui, const char *text)
343 char *text_copy = NULL;
346 text_copy = OPENSSL_strdup(text);
347 if (text_copy == NULL) {
348 UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
352 return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
356 char *UI_construct_prompt(UI *ui, const char *object_desc,
357 const char *object_name)
361 if (ui->meth->ui_construct_prompt != NULL)
362 prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
364 char prompt1[] = "Enter ";
365 char prompt2[] = " for ";
366 char prompt3[] = ":";
369 if (object_desc == NULL)
371 len = sizeof(prompt1) - 1 + strlen(object_desc);
372 if (object_name != NULL)
373 len += sizeof(prompt2) - 1 + strlen(object_name);
374 len += sizeof(prompt3) - 1;
376 prompt = OPENSSL_malloc(len + 1);
379 OPENSSL_strlcpy(prompt, prompt1, len + 1);
380 OPENSSL_strlcat(prompt, object_desc, len + 1);
381 if (object_name != NULL) {
382 OPENSSL_strlcat(prompt, prompt2, len + 1);
383 OPENSSL_strlcat(prompt, object_name, len + 1);
385 OPENSSL_strlcat(prompt, prompt3, len + 1);
390 void *UI_add_user_data(UI *ui, void *user_data)
392 void *old_data = ui->user_data;
394 if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
395 ui->meth->ui_destroy_data(ui, old_data);
398 ui->user_data = user_data;
399 ui->flags &= ~UI_FLAG_DUPL_DATA;
403 int UI_dup_user_data(UI *ui, void *user_data)
405 void *duplicate = NULL;
407 if (ui->meth->ui_duplicate_data == NULL
408 || ui->meth->ui_destroy_data == NULL) {
409 UIerr(UI_F_UI_DUP_USER_DATA, UI_R_USER_DATA_DUPLICATION_UNSUPPORTED);
413 duplicate = ui->meth->ui_duplicate_data(ui, user_data);
414 if (duplicate == NULL) {
415 UIerr(UI_F_UI_DUP_USER_DATA, ERR_R_MALLOC_FAILURE);
419 (void)UI_add_user_data(ui, duplicate);
420 ui->flags |= UI_FLAG_DUPL_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 != NULL
452 && ui->meth->ui_write_string(ui, &uis) <= 0)
457 int UI_process(UI *ui)
460 const char *state = "processing";
462 if (ui->meth->ui_open_session != NULL
463 && ui->meth->ui_open_session(ui) <= 0) {
464 state = "opening session";
469 if (ui->flags & UI_FLAG_PRINT_ERRORS)
470 ERR_print_errors_cb((int (*)(const char *, size_t, void *))
471 print_error, (void *)ui);
473 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
474 if (ui->meth->ui_write_string != NULL
475 && (ui->meth->ui_write_string(ui,
476 sk_UI_STRING_value(ui->strings, i))
479 state = "writing strings";
485 if (ui->meth->ui_flush != NULL)
486 switch (ui->meth->ui_flush(ui)) {
487 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 != NULL) {
501 switch (ui->meth->ui_read_string(ui,
502 sk_UI_STRING_value(ui->strings,
504 case -1: /* Interrupt/Cancel/something... */
508 state = "reading strings";
511 default: /* Success */
518 if (ui->meth->ui_close_session != NULL
519 && ui->meth->ui_close_session(ui) <= 0) {
521 state = "closing session";
526 UIerr(UI_F_UI_PROCESS, UI_R_PROCESSING_ERROR);
527 ERR_add_error_data(2, "while ", state);
532 int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
535 UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
539 case UI_CTRL_PRINT_ERRORS:
541 int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS);
543 ui->flags |= UI_FLAG_PRINT_ERRORS;
545 ui->flags &= ~UI_FLAG_PRINT_ERRORS;
548 case UI_CTRL_IS_REDOABLE:
549 return ! !(ui->flags & UI_FLAG_REDOABLE);
553 UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
557 int UI_set_ex_data(UI *r, int idx, void *arg)
559 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
562 void *UI_get_ex_data(UI *r, int idx)
564 return (CRYPTO_get_ex_data(&r->ex_data, idx));
567 const UI_METHOD *UI_get_method(UI *ui)
572 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
578 UI_METHOD *UI_create_method(const char *name)
580 UI_METHOD *ui_method = NULL;
582 if ((ui_method = OPENSSL_zalloc(sizeof(*ui_method))) == NULL
583 || (ui_method->name = OPENSSL_strdup(name)) == NULL
584 || !CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
585 &ui_method->ex_data)) {
587 OPENSSL_free(ui_method->name);
588 OPENSSL_free(ui_method);
589 UIerr(UI_F_UI_CREATE_METHOD, ERR_R_MALLOC_FAILURE);
596 * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
597 * (that is, it hasn't been allocated using UI_create_method(), you deserve
598 * anything Murphy can throw at you and more! You have been warned.
600 void UI_destroy_method(UI_METHOD *ui_method)
602 if (ui_method == NULL)
604 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
605 &ui_method->ex_data);
606 OPENSSL_free(ui_method->name);
607 ui_method->name = NULL;
608 OPENSSL_free(ui_method);
611 int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui))
613 if (method != NULL) {
614 method->ui_open_session = opener;
620 int UI_method_set_writer(UI_METHOD *method,
621 int (*writer) (UI *ui, UI_STRING *uis))
623 if (method != NULL) {
624 method->ui_write_string = writer;
630 int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
632 if (method != NULL) {
633 method->ui_flush = flusher;
639 int UI_method_set_reader(UI_METHOD *method,
640 int (*reader) (UI *ui, UI_STRING *uis))
642 if (method != NULL) {
643 method->ui_read_string = reader;
649 int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
651 if (method != NULL) {
652 method->ui_close_session = closer;
658 int UI_method_set_data_duplicator(UI_METHOD *method,
659 void *(*duplicator) (UI *ui, void *ui_data),
660 void (*destructor)(UI *ui, void *ui_data))
662 if (method != NULL) {
663 method->ui_duplicate_data = duplicator;
664 method->ui_destroy_data = destructor;
670 int UI_method_set_prompt_constructor(UI_METHOD *method,
671 char *(*prompt_constructor) (UI *ui,
677 if (method != NULL) {
678 method->ui_construct_prompt = prompt_constructor;
684 int UI_method_set_ex_data(UI_METHOD *method, int idx, void *data)
686 return CRYPTO_set_ex_data(&method->ex_data, idx, data);
689 int (*UI_method_get_opener(const UI_METHOD *method)) (UI *)
692 return method->ui_open_session;
696 int (*UI_method_get_writer(const UI_METHOD *method)) (UI *, UI_STRING *)
699 return method->ui_write_string;
703 int (*UI_method_get_flusher(const UI_METHOD *method)) (UI *)
706 return method->ui_flush;
710 int (*UI_method_get_reader(const UI_METHOD *method)) (UI *, UI_STRING *)
713 return method->ui_read_string;
717 int (*UI_method_get_closer(const UI_METHOD *method)) (UI *)
720 return method->ui_close_session;
724 char *(*UI_method_get_prompt_constructor(const UI_METHOD *method))
725 (UI *, const char *, const char *)
728 return method->ui_construct_prompt;
732 void *(*UI_method_get_data_duplicator(const UI_METHOD *method)) (UI *, void *)
735 return method->ui_duplicate_data;
739 void (*UI_method_get_data_destructor(const UI_METHOD *method)) (UI *, void *)
742 return method->ui_destroy_data;
746 const void *UI_method_get_ex_data(const UI_METHOD *method, int idx)
748 return CRYPTO_get_ex_data(&method->ex_data, idx);
751 enum UI_string_types UI_get_string_type(UI_STRING *uis)
756 int UI_get_input_flags(UI_STRING *uis)
758 return uis->input_flags;
761 const char *UI_get0_output_string(UI_STRING *uis)
763 return uis->out_string;
766 const char *UI_get0_action_string(UI_STRING *uis)
770 return uis->_.boolean_data.action_desc;
781 const char *UI_get0_result_string(UI_STRING *uis)
786 return uis->result_buf;
796 const char *UI_get0_test_string(UI_STRING *uis)
800 return uis->_.string_data.test_buf;
811 int UI_get_result_minsize(UI_STRING *uis)
816 return uis->_.string_data.result_minsize;
826 int UI_get_result_maxsize(UI_STRING *uis)
831 return uis->_.string_data.result_maxsize;
841 int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
843 int l = strlen(result);
845 ui->flags &= ~UI_FLAG_REDOABLE;
851 char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
852 char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
854 BIO_snprintf(number1, sizeof(number1), "%d",
855 uis->_.string_data.result_minsize);
856 BIO_snprintf(number2, sizeof(number2), "%d",
857 uis->_.string_data.result_maxsize);
859 if (l < uis->_.string_data.result_minsize) {
860 ui->flags |= UI_FLAG_REDOABLE;
861 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL);
862 ERR_add_error_data(5, "You must type in ",
863 number1, " to ", number2, " characters");
866 if (l > uis->_.string_data.result_maxsize) {
867 ui->flags |= UI_FLAG_REDOABLE;
868 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE);
869 ERR_add_error_data(5, "You must type in ",
870 number1, " to ", number2, " characters");
875 if (uis->result_buf == NULL) {
876 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
880 OPENSSL_strlcpy(uis->result_buf, result,
881 uis->_.string_data.result_maxsize + 1);
887 if (uis->result_buf == NULL) {
888 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
892 uis->result_buf[0] = '\0';
893 for (p = result; *p; p++) {
894 if (strchr(uis->_.boolean_data.ok_chars, *p)) {
895 uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
898 if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
899 uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];