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 method = UI_get_default_method();
45 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data)) {
52 static void free_string(UI_STRING *uis)
54 if (uis->flags & OUT_STRING_FREEABLE) {
55 OPENSSL_free((char *)uis->out_string);
58 OPENSSL_free((char *)uis->_.boolean_data.action_desc);
59 OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
60 OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
77 if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
78 ui->meth->ui_destroy_data(ui, ui->user_data);
80 sk_UI_STRING_pop_free(ui->strings, free_string);
81 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
82 CRYPTO_THREAD_lock_free(ui->lock);
86 static int allocate_string_stack(UI *ui)
88 if (ui->strings == NULL) {
89 ui->strings = sk_UI_STRING_new_null();
90 if (ui->strings == NULL) {
97 static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
99 enum UI_string_types type,
100 int input_flags, char *result_buf)
102 UI_STRING *ret = NULL;
104 if (prompt == NULL) {
105 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
106 } else if ((type == UIT_PROMPT || type == UIT_VERIFY
107 || type == UIT_BOOLEAN) && result_buf == NULL) {
108 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
109 } else if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL) {
110 ret->out_string = prompt;
111 ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
112 ret->input_flags = input_flags;
114 ret->result_buf = result_buf;
119 static int general_allocate_string(UI *ui, const char *prompt,
121 enum UI_string_types type, int input_flags,
122 char *result_buf, int minsize, int maxsize,
123 const char *test_buf)
126 UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
127 type, input_flags, result_buf);
130 if (allocate_string_stack(ui) >= 0) {
131 s->_.string_data.result_minsize = minsize;
132 s->_.string_data.result_maxsize = maxsize;
133 s->_.string_data.test_buf = test_buf;
134 ret = sk_UI_STRING_push(ui->strings, s);
135 /* sk_push() returns 0 on error. Let's adapt that */
146 static int general_allocate_boolean(UI *ui,
148 const char *action_desc,
149 const char *ok_chars,
150 const char *cancel_chars,
152 enum UI_string_types type,
153 int input_flags, char *result_buf)
159 if (ok_chars == NULL) {
160 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
161 } else if (cancel_chars == NULL) {
162 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
164 for (p = ok_chars; *p != '\0'; p++) {
165 if (strchr(cancel_chars, *p) != NULL) {
166 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
167 UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
171 s = general_allocate_prompt(ui, prompt, prompt_freeable,
172 type, input_flags, result_buf);
175 if (allocate_string_stack(ui) >= 0) {
176 s->_.boolean_data.action_desc = action_desc;
177 s->_.boolean_data.ok_chars = ok_chars;
178 s->_.boolean_data.cancel_chars = cancel_chars;
179 ret = sk_UI_STRING_push(ui->strings, s);
181 * sk_push() returns 0 on error. Let's adapt that
195 * Returns the index to the place in the stack or -1 for error. Uses a
196 * direct reference to the prompt.
198 int UI_add_input_string(UI *ui, const char *prompt, int flags,
199 char *result_buf, int minsize, int maxsize)
201 return general_allocate_string(ui, prompt, 0,
202 UIT_PROMPT, flags, result_buf, minsize,
206 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
207 int UI_dup_input_string(UI *ui, const char *prompt, int flags,
208 char *result_buf, int minsize, int maxsize)
210 char *prompt_copy = NULL;
212 if (prompt != NULL) {
213 prompt_copy = OPENSSL_strdup(prompt);
214 if (prompt_copy == NULL) {
215 UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
220 return general_allocate_string(ui, prompt_copy, 1,
221 UIT_PROMPT, flags, result_buf, minsize,
225 int UI_add_verify_string(UI *ui, const char *prompt, int flags,
226 char *result_buf, int minsize, int maxsize,
227 const char *test_buf)
229 return general_allocate_string(ui, prompt, 0,
230 UIT_VERIFY, flags, result_buf, minsize,
234 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
235 char *result_buf, int minsize, int maxsize,
236 const char *test_buf)
238 char *prompt_copy = NULL;
240 if (prompt != NULL) {
241 prompt_copy = OPENSSL_strdup(prompt);
242 if (prompt_copy == NULL) {
243 UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
248 return general_allocate_string(ui, prompt_copy, 1,
249 UIT_VERIFY, flags, result_buf, minsize,
253 int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
254 const char *ok_chars, const char *cancel_chars,
255 int flags, char *result_buf)
257 return general_allocate_boolean(ui, prompt, action_desc,
258 ok_chars, cancel_chars, 0, UIT_BOOLEAN,
262 int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
263 const char *ok_chars, const char *cancel_chars,
264 int flags, char *result_buf)
266 char *prompt_copy = NULL;
267 char *action_desc_copy = NULL;
268 char *ok_chars_copy = NULL;
269 char *cancel_chars_copy = NULL;
271 if (prompt != NULL) {
272 prompt_copy = OPENSSL_strdup(prompt);
273 if (prompt_copy == NULL) {
274 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
279 if (action_desc != NULL) {
280 action_desc_copy = OPENSSL_strdup(action_desc);
281 if (action_desc_copy == NULL) {
282 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
287 if (ok_chars != NULL) {
288 ok_chars_copy = OPENSSL_strdup(ok_chars);
289 if (ok_chars_copy == NULL) {
290 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
295 if (cancel_chars != NULL) {
296 cancel_chars_copy = OPENSSL_strdup(cancel_chars);
297 if (cancel_chars_copy == NULL) {
298 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
303 return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
304 ok_chars_copy, cancel_chars_copy, 1,
305 UIT_BOOLEAN, flags, result_buf);
307 OPENSSL_free(prompt_copy);
308 OPENSSL_free(action_desc_copy);
309 OPENSSL_free(ok_chars_copy);
310 OPENSSL_free(cancel_chars_copy);
314 int UI_add_info_string(UI *ui, const char *text)
316 return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
320 int UI_dup_info_string(UI *ui, const char *text)
322 char *text_copy = NULL;
325 text_copy = OPENSSL_strdup(text);
326 if (text_copy == NULL) {
327 UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
332 return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
336 int UI_add_error_string(UI *ui, const char *text)
338 return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
342 int UI_dup_error_string(UI *ui, const char *text)
344 char *text_copy = NULL;
347 text_copy = OPENSSL_strdup(text);
348 if (text_copy == NULL) {
349 UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
353 return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
357 char *UI_construct_prompt(UI *ui, const char *object_desc,
358 const char *object_name)
362 if (ui->meth->ui_construct_prompt != NULL)
363 prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
365 char prompt1[] = "Enter ";
366 char prompt2[] = " for ";
367 char prompt3[] = ":";
370 if (object_desc == NULL)
372 len = sizeof(prompt1) - 1 + strlen(object_desc);
373 if (object_name != NULL)
374 len += sizeof(prompt2) - 1 + strlen(object_name);
375 len += sizeof(prompt3) - 1;
377 prompt = OPENSSL_malloc(len + 1);
380 OPENSSL_strlcpy(prompt, prompt1, len + 1);
381 OPENSSL_strlcat(prompt, object_desc, len + 1);
382 if (object_name != NULL) {
383 OPENSSL_strlcat(prompt, prompt2, len + 1);
384 OPENSSL_strlcat(prompt, object_name, len + 1);
386 OPENSSL_strlcat(prompt, prompt3, len + 1);
391 void *UI_add_user_data(UI *ui, void *user_data)
393 void *old_data = ui->user_data;
395 if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
396 ui->meth->ui_destroy_data(ui, old_data);
399 ui->user_data = user_data;
400 ui->flags &= ~UI_FLAG_DUPL_DATA;
404 int UI_dup_user_data(UI *ui, void *user_data)
406 void *duplicate = NULL;
408 if (ui->meth->ui_duplicate_data == NULL
409 || ui->meth->ui_destroy_data == NULL) {
410 UIerr(UI_F_UI_DUP_USER_DATA, UI_R_USER_DATA_DUPLICATION_UNSUPPORTED);
414 duplicate = ui->meth->ui_duplicate_data(ui, user_data);
415 if (duplicate == NULL) {
416 UIerr(UI_F_UI_DUP_USER_DATA, ERR_R_MALLOC_FAILURE);
420 (void)UI_add_user_data(ui, duplicate);
421 ui->flags |= UI_FLAG_DUPL_DATA;
426 void *UI_get0_user_data(UI *ui)
428 return ui->user_data;
431 const char *UI_get0_result(UI *ui, int i)
434 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL);
437 if (i >= sk_UI_STRING_num(ui->strings)) {
438 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE);
441 return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
444 static int print_error(const char *str, size_t len, UI *ui)
448 memset(&uis, 0, sizeof(uis));
449 uis.type = UIT_ERROR;
450 uis.out_string = str;
452 if (ui->meth->ui_write_string != NULL
453 && ui->meth->ui_write_string(ui, &uis) <= 0)
458 int UI_process(UI *ui)
461 const char *state = "processing";
463 if (ui->meth->ui_open_session != NULL
464 && ui->meth->ui_open_session(ui) <= 0) {
465 state = "opening session";
470 if (ui->flags & UI_FLAG_PRINT_ERRORS)
471 ERR_print_errors_cb((int (*)(const char *, size_t, void *))
472 print_error, (void *)ui);
474 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
475 if (ui->meth->ui_write_string != NULL
476 && (ui->meth->ui_write_string(ui,
477 sk_UI_STRING_value(ui->strings, i))
480 state = "writing strings";
486 if (ui->meth->ui_flush != NULL)
487 switch (ui->meth->ui_flush(ui)) {
488 case -1: /* Interrupt/Cancel/something... */
495 default: /* Success */
500 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
501 if (ui->meth->ui_read_string != NULL) {
502 switch (ui->meth->ui_read_string(ui,
503 sk_UI_STRING_value(ui->strings,
505 case -1: /* Interrupt/Cancel/something... */
509 state = "reading strings";
512 default: /* Success */
521 if (ui->meth->ui_close_session != NULL
522 && ui->meth->ui_close_session(ui) <= 0) {
524 state = "closing session";
529 UIerr(UI_F_UI_PROCESS, UI_R_PROCESSING_ERROR);
530 ERR_add_error_data(2, "while ", state);
535 int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
538 UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
542 case UI_CTRL_PRINT_ERRORS:
544 int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS);
546 ui->flags |= UI_FLAG_PRINT_ERRORS;
548 ui->flags &= ~UI_FLAG_PRINT_ERRORS;
551 case UI_CTRL_IS_REDOABLE:
552 return ! !(ui->flags & UI_FLAG_REDOABLE);
556 UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
560 int UI_set_ex_data(UI *r, int idx, void *arg)
562 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
565 void *UI_get_ex_data(UI *r, int idx)
567 return (CRYPTO_get_ex_data(&r->ex_data, idx));
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(const char *name)
583 UI_METHOD *ui_method = NULL;
585 if ((ui_method = OPENSSL_zalloc(sizeof(*ui_method))) == NULL
586 || (ui_method->name = OPENSSL_strdup(name)) == NULL
587 || !CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
588 &ui_method->ex_data)) {
590 OPENSSL_free(ui_method->name);
591 OPENSSL_free(ui_method);
592 UIerr(UI_F_UI_CREATE_METHOD, ERR_R_MALLOC_FAILURE);
599 * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
600 * (that is, it hasn't been allocated using UI_create_method(), you deserve
601 * anything Murphy can throw at you and more! You have been warned.
603 void UI_destroy_method(UI_METHOD *ui_method)
605 if (ui_method == NULL)
607 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
608 &ui_method->ex_data);
609 OPENSSL_free(ui_method->name);
610 ui_method->name = NULL;
611 OPENSSL_free(ui_method);
614 int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui))
616 if (method != NULL) {
617 method->ui_open_session = opener;
623 int UI_method_set_writer(UI_METHOD *method,
624 int (*writer) (UI *ui, UI_STRING *uis))
626 if (method != NULL) {
627 method->ui_write_string = writer;
633 int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
635 if (method != NULL) {
636 method->ui_flush = flusher;
642 int UI_method_set_reader(UI_METHOD *method,
643 int (*reader) (UI *ui, UI_STRING *uis))
645 if (method != NULL) {
646 method->ui_read_string = reader;
652 int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
654 if (method != NULL) {
655 method->ui_close_session = closer;
661 int UI_method_set_data_duplicator(UI_METHOD *method,
662 void *(*duplicator) (UI *ui, void *ui_data),
663 void (*destructor)(UI *ui, void *ui_data))
665 if (method != NULL) {
666 method->ui_duplicate_data = duplicator;
667 method->ui_destroy_data = destructor;
673 int UI_method_set_prompt_constructor(UI_METHOD *method,
674 char *(*prompt_constructor) (UI *ui,
680 if (method != NULL) {
681 method->ui_construct_prompt = prompt_constructor;
687 int UI_method_set_ex_data(UI_METHOD *method, int idx, void *data)
689 return CRYPTO_set_ex_data(&method->ex_data, idx, data);
692 int (*UI_method_get_opener(const UI_METHOD *method)) (UI *)
695 return method->ui_open_session;
699 int (*UI_method_get_writer(const UI_METHOD *method)) (UI *, UI_STRING *)
702 return method->ui_write_string;
706 int (*UI_method_get_flusher(const UI_METHOD *method)) (UI *)
709 return method->ui_flush;
713 int (*UI_method_get_reader(const UI_METHOD *method)) (UI *, UI_STRING *)
716 return method->ui_read_string;
720 int (*UI_method_get_closer(const UI_METHOD *method)) (UI *)
723 return method->ui_close_session;
727 char *(*UI_method_get_prompt_constructor(const UI_METHOD *method))
728 (UI *, const char *, const char *)
731 return method->ui_construct_prompt;
735 void *(*UI_method_get_data_duplicator(const UI_METHOD *method)) (UI *, void *)
738 return method->ui_duplicate_data;
742 void (*UI_method_get_data_destructor(const UI_METHOD *method)) (UI *, void *)
745 return method->ui_destroy_data;
749 const void *UI_method_get_ex_data(const UI_METHOD *method, int idx)
751 return CRYPTO_get_ex_data(&method->ex_data, idx);
754 enum UI_string_types UI_get_string_type(UI_STRING *uis)
759 int UI_get_input_flags(UI_STRING *uis)
761 return uis->input_flags;
764 const char *UI_get0_output_string(UI_STRING *uis)
766 return uis->out_string;
769 const char *UI_get0_action_string(UI_STRING *uis)
773 return uis->_.boolean_data.action_desc;
784 const char *UI_get0_result_string(UI_STRING *uis)
789 return uis->result_buf;
799 const char *UI_get0_test_string(UI_STRING *uis)
803 return uis->_.string_data.test_buf;
814 int UI_get_result_minsize(UI_STRING *uis)
819 return uis->_.string_data.result_minsize;
829 int UI_get_result_maxsize(UI_STRING *uis)
834 return uis->_.string_data.result_maxsize;
844 int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
846 int l = strlen(result);
848 ui->flags &= ~UI_FLAG_REDOABLE;
854 char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
855 char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
857 BIO_snprintf(number1, sizeof(number1), "%d",
858 uis->_.string_data.result_minsize);
859 BIO_snprintf(number2, sizeof(number2), "%d",
860 uis->_.string_data.result_maxsize);
862 if (l < uis->_.string_data.result_minsize) {
863 ui->flags |= UI_FLAG_REDOABLE;
864 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL);
865 ERR_add_error_data(5, "You must type in ",
866 number1, " to ", number2, " characters");
869 if (l > uis->_.string_data.result_maxsize) {
870 ui->flags |= UI_FLAG_REDOABLE;
871 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE);
872 ERR_add_error_data(5, "You must type in ",
873 number1, " to ", number2, " characters");
878 if (uis->result_buf == NULL) {
879 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
883 OPENSSL_strlcpy(uis->result_buf, result,
884 uis->_.string_data.result_maxsize + 1);
890 if (uis->result_buf == NULL) {
891 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
895 uis->result_buf[0] = '\0';
896 for (p = result; *p; p++) {
897 if (strchr(uis->_.boolean_data.ok_chars, *p)) {
898 uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
901 if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
902 uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];