sl@0: /* crypto/ui/ui_lib.c -*- mode:C; c-file-style: "eay" -*- */ sl@0: /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL sl@0: * project 2001. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "ui_locl.h" sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: IMPLEMENT_STACK_OF(UI_STRING_ST) sl@0: sl@0: #ifndef EMULATOR sl@0: static const UI_METHOD *default_UI_meth=NULL; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(default_UI_meth,ui_lib,const UI_METHOD *) sl@0: #define default_UI_meth (*GET_WSD_VAR_NAME(default_UI_meth,ui_lib, s)()) sl@0: #endif sl@0: sl@0: EXPORT_C UI *UI_new(void) sl@0: { sl@0: return(UI_new_method(NULL)); sl@0: } sl@0: sl@0: EXPORT_C UI *UI_new_method(const UI_METHOD *method) sl@0: { sl@0: UI *ret; sl@0: sl@0: ret=(UI *)OPENSSL_malloc(sizeof(UI)); sl@0: if (ret == NULL) sl@0: { sl@0: UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE); sl@0: return NULL; sl@0: } sl@0: if (method == NULL) sl@0: ret->meth=UI_get_default_method(); sl@0: else sl@0: ret->meth=method; sl@0: sl@0: ret->strings=NULL; sl@0: ret->user_data=NULL; sl@0: CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data); sl@0: return ret; sl@0: } sl@0: sl@0: static void free_string(UI_STRING *uis) sl@0: { sl@0: if (uis->flags & OUT_STRING_FREEABLE) sl@0: { sl@0: OPENSSL_free((char *)uis->out_string); sl@0: switch(uis->type) sl@0: { sl@0: case UIT_BOOLEAN: sl@0: OPENSSL_free((char *)uis->_.boolean_data.action_desc); sl@0: OPENSSL_free((char *)uis->_.boolean_data.ok_chars); sl@0: OPENSSL_free((char *)uis->_.boolean_data.cancel_chars); sl@0: break; sl@0: default: sl@0: break; sl@0: } sl@0: } sl@0: OPENSSL_free(uis); sl@0: } sl@0: sl@0: EXPORT_C void UI_free(UI *ui) sl@0: { sl@0: if (ui == NULL) sl@0: return; sl@0: sk_UI_STRING_pop_free(ui->strings,free_string); sl@0: CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data); sl@0: OPENSSL_free(ui); sl@0: } sl@0: sl@0: static int allocate_string_stack(UI *ui) sl@0: { sl@0: if (ui->strings == NULL) sl@0: { sl@0: ui->strings=sk_UI_STRING_new_null(); sl@0: if (ui->strings == NULL) sl@0: { sl@0: return -1; sl@0: } sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt, sl@0: int prompt_freeable, enum UI_string_types type, int input_flags, sl@0: char *result_buf) sl@0: { sl@0: UI_STRING *ret = NULL; sl@0: sl@0: if (prompt == NULL) sl@0: { sl@0: UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,ERR_R_PASSED_NULL_PARAMETER); sl@0: } sl@0: else if ((type == UIT_PROMPT || type == UIT_VERIFY sl@0: || type == UIT_BOOLEAN) && result_buf == NULL) sl@0: { sl@0: UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,UI_R_NO_RESULT_BUFFER); sl@0: } sl@0: else if ((ret = (UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING)))) sl@0: { sl@0: ret->out_string=prompt; sl@0: ret->flags=prompt_freeable ? OUT_STRING_FREEABLE : 0; sl@0: ret->input_flags=input_flags; sl@0: ret->type=type; sl@0: ret->result_buf=result_buf; sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: static int general_allocate_string(UI *ui, const char *prompt, sl@0: int prompt_freeable, enum UI_string_types type, int input_flags, sl@0: char *result_buf, int minsize, int maxsize, const char *test_buf) sl@0: { sl@0: int ret = -1; sl@0: UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable, sl@0: type, input_flags, result_buf); sl@0: sl@0: if (s) sl@0: { sl@0: if (allocate_string_stack(ui) >= 0) sl@0: { sl@0: s->_.string_data.result_minsize=minsize; sl@0: s->_.string_data.result_maxsize=maxsize; sl@0: s->_.string_data.test_buf=test_buf; sl@0: ret=sk_UI_STRING_push(ui->strings, s); sl@0: /* sk_push() returns 0 on error. Let's addapt that */ sl@0: if (ret <= 0) ret--; sl@0: } sl@0: else sl@0: free_string(s); sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: static int general_allocate_boolean(UI *ui, sl@0: const char *prompt, const char *action_desc, sl@0: const char *ok_chars, const char *cancel_chars, sl@0: int prompt_freeable, enum UI_string_types type, int input_flags, sl@0: char *result_buf) sl@0: { sl@0: int ret = -1; sl@0: UI_STRING *s; sl@0: const char *p; sl@0: sl@0: if (ok_chars == NULL) sl@0: { sl@0: UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,ERR_R_PASSED_NULL_PARAMETER); sl@0: } sl@0: else if (cancel_chars == NULL) sl@0: { sl@0: UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,ERR_R_PASSED_NULL_PARAMETER); sl@0: } sl@0: else sl@0: { sl@0: for(p = ok_chars; *p; p++) sl@0: { sl@0: if (strchr(cancel_chars, *p)) sl@0: { sl@0: UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, sl@0: UI_R_COMMON_OK_AND_CANCEL_CHARACTERS); sl@0: } sl@0: } sl@0: sl@0: s = general_allocate_prompt(ui, prompt, prompt_freeable, sl@0: type, input_flags, result_buf); sl@0: sl@0: if (s) sl@0: { sl@0: if (allocate_string_stack(ui) >= 0) sl@0: { sl@0: s->_.boolean_data.action_desc = action_desc; sl@0: s->_.boolean_data.ok_chars = ok_chars; sl@0: s->_.boolean_data.cancel_chars = cancel_chars; sl@0: ret=sk_UI_STRING_push(ui->strings, s); sl@0: /* sk_push() returns 0 on error. sl@0: Let's addapt that */ sl@0: if (ret <= 0) ret--; sl@0: } sl@0: else sl@0: free_string(s); sl@0: } sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: /* Returns the index to the place in the stack or -1 for error. Uses a sl@0: direct reference to the prompt. */ sl@0: EXPORT_C int UI_add_input_string(UI *ui, const char *prompt, int flags, sl@0: char *result_buf, int minsize, int maxsize) sl@0: { sl@0: return general_allocate_string(ui, prompt, 0, sl@0: UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL); sl@0: } sl@0: sl@0: /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */ sl@0: EXPORT_C int UI_dup_input_string(UI *ui, const char *prompt, int flags, sl@0: char *result_buf, int minsize, int maxsize) sl@0: { sl@0: char *prompt_copy=NULL; sl@0: sl@0: if (prompt) sl@0: { sl@0: prompt_copy=BUF_strdup(prompt); sl@0: if (prompt_copy == NULL) sl@0: { sl@0: UIerr(UI_F_UI_DUP_INPUT_STRING,ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: } sl@0: sl@0: return general_allocate_string(ui, prompt_copy, 1, sl@0: UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL); sl@0: } sl@0: sl@0: EXPORT_C int UI_add_verify_string(UI *ui, const char *prompt, int flags, sl@0: char *result_buf, int minsize, int maxsize, const char *test_buf) sl@0: { sl@0: return general_allocate_string(ui, prompt, 0, sl@0: UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf); sl@0: } sl@0: sl@0: EXPORT_C int UI_dup_verify_string(UI *ui, const char *prompt, int flags, sl@0: char *result_buf, int minsize, int maxsize, const char *test_buf) sl@0: { sl@0: char *prompt_copy=NULL; sl@0: sl@0: if (prompt) sl@0: { sl@0: prompt_copy=BUF_strdup(prompt); sl@0: if (prompt_copy == NULL) sl@0: { sl@0: UIerr(UI_F_UI_DUP_VERIFY_STRING,ERR_R_MALLOC_FAILURE); sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: return general_allocate_string(ui, prompt_copy, 1, sl@0: UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf); sl@0: } sl@0: sl@0: EXPORT_C int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc, sl@0: const char *ok_chars, const char *cancel_chars, sl@0: int flags, char *result_buf) sl@0: { sl@0: return general_allocate_boolean(ui, prompt, action_desc, sl@0: ok_chars, cancel_chars, 0, UIT_BOOLEAN, flags, result_buf); sl@0: } sl@0: sl@0: EXPORT_C int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc, sl@0: const char *ok_chars, const char *cancel_chars, sl@0: int flags, char *result_buf) sl@0: { sl@0: char *prompt_copy = NULL; sl@0: char *action_desc_copy = NULL; sl@0: char *ok_chars_copy = NULL; sl@0: char *cancel_chars_copy = NULL; sl@0: sl@0: if (prompt) sl@0: { sl@0: prompt_copy=BUF_strdup(prompt); sl@0: if (prompt_copy == NULL) sl@0: { sl@0: UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: if (action_desc) sl@0: { sl@0: action_desc_copy=BUF_strdup(action_desc); sl@0: if (action_desc_copy == NULL) sl@0: { sl@0: UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: if (ok_chars) sl@0: { sl@0: ok_chars_copy=BUF_strdup(ok_chars); sl@0: if (ok_chars_copy == NULL) sl@0: { sl@0: UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: if (cancel_chars) sl@0: { sl@0: cancel_chars_copy=BUF_strdup(cancel_chars); sl@0: if (cancel_chars_copy == NULL) sl@0: { sl@0: UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: return general_allocate_boolean(ui, prompt_copy, action_desc_copy, sl@0: ok_chars_copy, cancel_chars_copy, 1, UIT_BOOLEAN, flags, sl@0: result_buf); sl@0: err: sl@0: if (prompt_copy) OPENSSL_free(prompt_copy); sl@0: if (action_desc_copy) OPENSSL_free(action_desc_copy); sl@0: if (ok_chars_copy) OPENSSL_free(ok_chars_copy); sl@0: if (cancel_chars_copy) OPENSSL_free(cancel_chars_copy); sl@0: return -1; sl@0: } sl@0: sl@0: EXPORT_C int UI_add_info_string(UI *ui, const char *text) sl@0: { sl@0: return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0, sl@0: NULL); sl@0: } sl@0: sl@0: EXPORT_C int UI_dup_info_string(UI *ui, const char *text) sl@0: { sl@0: char *text_copy=NULL; sl@0: sl@0: if (text) sl@0: { sl@0: text_copy=BUF_strdup(text); sl@0: if (text_copy == NULL) sl@0: { sl@0: UIerr(UI_F_UI_DUP_INFO_STRING,ERR_R_MALLOC_FAILURE); sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL, sl@0: 0, 0, NULL); sl@0: } sl@0: sl@0: EXPORT_C int UI_add_error_string(UI *ui, const char *text) sl@0: { sl@0: return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0, sl@0: NULL); sl@0: } sl@0: sl@0: EXPORT_C int UI_dup_error_string(UI *ui, const char *text) sl@0: { sl@0: char *text_copy=NULL; sl@0: sl@0: if (text) sl@0: { sl@0: text_copy=BUF_strdup(text); sl@0: if (text_copy == NULL) sl@0: { sl@0: UIerr(UI_F_UI_DUP_ERROR_STRING,ERR_R_MALLOC_FAILURE); sl@0: return -1; sl@0: } sl@0: } sl@0: return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL, sl@0: 0, 0, NULL); sl@0: } sl@0: sl@0: EXPORT_C char *UI_construct_prompt(UI *ui, const char *object_desc, sl@0: const char *object_name) sl@0: { sl@0: char *prompt = NULL; sl@0: sl@0: if (ui->meth->ui_construct_prompt) sl@0: prompt = ui->meth->ui_construct_prompt(ui, sl@0: object_desc, object_name); sl@0: else sl@0: { sl@0: char prompt1[] = "Enter "; sl@0: char prompt2[] = " for "; sl@0: char prompt3[] = ":"; sl@0: int len = 0; sl@0: sl@0: if (object_desc == NULL) sl@0: return NULL; sl@0: len = sizeof(prompt1) - 1 + strlen(object_desc); sl@0: if (object_name) sl@0: len += sizeof(prompt2) - 1 + strlen(object_name); sl@0: len += sizeof(prompt3) - 1; sl@0: sl@0: prompt = (char *)OPENSSL_malloc(len + 1); sl@0: #ifdef SYMBIAN sl@0: if(prompt==NULL) sl@0: return NULL; sl@0: #endif sl@0: BUF_strlcpy(prompt, prompt1, len + 1); sl@0: BUF_strlcat(prompt, object_desc, len + 1); sl@0: if (object_name) sl@0: { sl@0: BUF_strlcat(prompt, prompt2, len + 1); sl@0: BUF_strlcat(prompt, object_name, len + 1); sl@0: } sl@0: BUF_strlcat(prompt, prompt3, len + 1); sl@0: } sl@0: return prompt; sl@0: } sl@0: sl@0: EXPORT_C void *UI_add_user_data(UI *ui, void *user_data) sl@0: { sl@0: void *old_data = ui->user_data; sl@0: ui->user_data = user_data; sl@0: return old_data; sl@0: } sl@0: sl@0: EXPORT_C void *UI_get0_user_data(UI *ui) sl@0: { sl@0: return ui->user_data; sl@0: } sl@0: sl@0: EXPORT_C const char *UI_get0_result(UI *ui, int i) sl@0: { sl@0: if (i < 0) sl@0: { sl@0: UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_SMALL); sl@0: return NULL; sl@0: } sl@0: if (i >= sk_UI_STRING_num(ui->strings)) sl@0: { sl@0: UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_LARGE); sl@0: return NULL; sl@0: } sl@0: return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i)); sl@0: } sl@0: sl@0: static int print_error(const char *str, size_t len, UI *ui) sl@0: { sl@0: UI_STRING uis; sl@0: sl@0: memset(&uis, 0, sizeof(uis)); sl@0: uis.type = UIT_ERROR; sl@0: uis.out_string = str; sl@0: sl@0: if (ui->meth->ui_write_string sl@0: && !ui->meth->ui_write_string(ui, &uis)) sl@0: return -1; sl@0: return 0; sl@0: } sl@0: sl@0: EXPORT_C int UI_process(UI *ui) sl@0: { sl@0: int i, ok=0; sl@0: sl@0: if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui)) sl@0: return -1; sl@0: sl@0: if (ui->flags & UI_FLAG_PRINT_ERRORS) sl@0: ERR_print_errors_cb( sl@0: (int (*)(const char *, size_t, void *))print_error, sl@0: (void *)ui); sl@0: sl@0: for(i=0; istrings); i++) sl@0: { sl@0: if (ui->meth->ui_write_string sl@0: && !ui->meth->ui_write_string(ui, sl@0: sk_UI_STRING_value(ui->strings, i))) sl@0: { sl@0: ok=-1; sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: if (ui->meth->ui_flush) sl@0: switch(ui->meth->ui_flush(ui)) sl@0: { sl@0: case -1: /* Interrupt/Cancel/something... */ sl@0: ok = -2; sl@0: goto err; sl@0: case 0: /* Errors */ sl@0: ok = -1; sl@0: goto err; sl@0: default: /* Success */ sl@0: ok = 0; sl@0: break; sl@0: } sl@0: sl@0: for(i=0; istrings); i++) sl@0: { sl@0: if (ui->meth->ui_read_string) sl@0: { sl@0: switch(ui->meth->ui_read_string(ui, sl@0: sk_UI_STRING_value(ui->strings, i))) sl@0: { sl@0: case -1: /* Interrupt/Cancel/something... */ sl@0: ok = -2; sl@0: goto err; sl@0: case 0: /* Errors */ sl@0: ok = -1; sl@0: goto err; sl@0: default: /* Success */ sl@0: ok = 0; sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: err: sl@0: if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui)) sl@0: return -1; sl@0: return ok; sl@0: } sl@0: sl@0: EXPORT_C int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f)(void)) sl@0: { sl@0: if (ui == NULL) sl@0: { sl@0: UIerr(UI_F_UI_CTRL,ERR_R_PASSED_NULL_PARAMETER); sl@0: return -1; sl@0: } sl@0: switch(cmd) sl@0: { sl@0: case UI_CTRL_PRINT_ERRORS: sl@0: { sl@0: int save_flag = !!(ui->flags & UI_FLAG_PRINT_ERRORS); sl@0: if (i) sl@0: ui->flags |= UI_FLAG_PRINT_ERRORS; sl@0: else sl@0: ui->flags &= ~UI_FLAG_PRINT_ERRORS; sl@0: return save_flag; sl@0: } sl@0: case UI_CTRL_IS_REDOABLE: sl@0: return !!(ui->flags & UI_FLAG_REDOABLE); sl@0: default: sl@0: break; sl@0: } sl@0: UIerr(UI_F_UI_CTRL,UI_R_UNKNOWN_CONTROL_COMMAND); sl@0: return -1; sl@0: } sl@0: sl@0: EXPORT_C int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, sl@0: CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) sl@0: { sl@0: return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp, sl@0: new_func, dup_func, free_func); sl@0: } sl@0: sl@0: EXPORT_C int UI_set_ex_data(UI *r, int idx, void *arg) sl@0: { sl@0: return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); sl@0: } sl@0: sl@0: EXPORT_C void *UI_get_ex_data(UI *r, int idx) sl@0: { sl@0: return(CRYPTO_get_ex_data(&r->ex_data,idx)); sl@0: } sl@0: sl@0: EXPORT_C void UI_set_default_method(const UI_METHOD *meth) sl@0: { sl@0: default_UI_meth=meth; sl@0: } sl@0: sl@0: EXPORT_C const UI_METHOD *UI_get_default_method(void) sl@0: { sl@0: if (default_UI_meth == NULL) sl@0: { sl@0: default_UI_meth=UI_OpenSSL(); sl@0: } sl@0: return default_UI_meth; sl@0: } sl@0: sl@0: EXPORT_C const UI_METHOD *UI_get_method(UI *ui) sl@0: { sl@0: return ui->meth; sl@0: } sl@0: sl@0: EXPORT_C const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth) sl@0: { sl@0: ui->meth=meth; sl@0: return ui->meth; sl@0: } sl@0: sl@0: sl@0: EXPORT_C UI_METHOD *UI_create_method(char *name) sl@0: { sl@0: UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD)); sl@0: sl@0: if (ui_method) sl@0: { sl@0: memset(ui_method, 0, sizeof(*ui_method)); sl@0: ui_method->name = BUF_strdup(name); sl@0: } sl@0: return ui_method; sl@0: } sl@0: sl@0: /* BIG FSCKING WARNING!!!! If you use this on a statically allocated method sl@0: (that is, it hasn't been allocated using UI_create_method(), you deserve sl@0: anything Murphy can throw at you and more! You have been warned. */ sl@0: EXPORT_C void UI_destroy_method(UI_METHOD *ui_method) sl@0: { sl@0: OPENSSL_free(ui_method->name); sl@0: ui_method->name = NULL; sl@0: OPENSSL_free(ui_method); sl@0: } sl@0: sl@0: EXPORT_C int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui)) sl@0: { sl@0: if (method) sl@0: { sl@0: method->ui_open_session = opener; sl@0: return 0; sl@0: } sl@0: else sl@0: return -1; sl@0: } sl@0: sl@0: EXPORT_C int UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis)) sl@0: { sl@0: if (method) sl@0: { sl@0: method->ui_write_string = writer; sl@0: return 0; sl@0: } sl@0: else sl@0: return -1; sl@0: } sl@0: sl@0: EXPORT_C int UI_method_set_flusher(UI_METHOD *method, int (*flusher)(UI *ui)) sl@0: { sl@0: if (method) sl@0: { sl@0: method->ui_flush = flusher; sl@0: return 0; sl@0: } sl@0: else sl@0: return -1; sl@0: } sl@0: sl@0: EXPORT_C int UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis)) sl@0: { sl@0: if (method) sl@0: { sl@0: method->ui_read_string = reader; sl@0: return 0; sl@0: } sl@0: else sl@0: return -1; sl@0: } sl@0: sl@0: EXPORT_C int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui)) sl@0: { sl@0: if (method) sl@0: { sl@0: method->ui_close_session = closer; sl@0: return 0; sl@0: } sl@0: else sl@0: return -1; sl@0: } sl@0: sl@0: EXPORT_C int (*UI_method_get_opener(UI_METHOD *method))(UI*) sl@0: { sl@0: if (method) sl@0: return method->ui_open_session; sl@0: else sl@0: return NULL; sl@0: } sl@0: sl@0: EXPORT_C int (*UI_method_get_writer(UI_METHOD *method))(UI*,UI_STRING*) sl@0: { sl@0: if (method) sl@0: return method->ui_write_string; sl@0: else sl@0: return NULL; sl@0: } sl@0: sl@0: EXPORT_C int (*UI_method_get_flusher(UI_METHOD *method))(UI*) sl@0: { sl@0: if (method) sl@0: return method->ui_flush; sl@0: else sl@0: return NULL; sl@0: } sl@0: sl@0: EXPORT_C int (*UI_method_get_reader(UI_METHOD *method))(UI*,UI_STRING*) sl@0: { sl@0: if (method) sl@0: return method->ui_read_string; sl@0: else sl@0: return NULL; sl@0: } sl@0: sl@0: EXPORT_C int (*UI_method_get_closer(UI_METHOD *method))(UI*) sl@0: { sl@0: if (method) sl@0: return method->ui_close_session; sl@0: else sl@0: return NULL; sl@0: } sl@0: sl@0: EXPORT_C enum UI_string_types UI_get_string_type(UI_STRING *uis) sl@0: { sl@0: if (!uis) sl@0: return UIT_NONE; sl@0: return uis->type; sl@0: } sl@0: sl@0: EXPORT_C int UI_get_input_flags(UI_STRING *uis) sl@0: { sl@0: if (!uis) sl@0: return 0; sl@0: return uis->input_flags; sl@0: } sl@0: sl@0: EXPORT_C const char *UI_get0_output_string(UI_STRING *uis) sl@0: { sl@0: if (!uis) sl@0: return NULL; sl@0: return uis->out_string; sl@0: } sl@0: sl@0: EXPORT_C const char *UI_get0_action_string(UI_STRING *uis) sl@0: { sl@0: if (!uis) sl@0: return NULL; sl@0: switch(uis->type) sl@0: { sl@0: case UIT_PROMPT: sl@0: case UIT_BOOLEAN: sl@0: return uis->_.boolean_data.action_desc; sl@0: default: sl@0: return NULL; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C const char *UI_get0_result_string(UI_STRING *uis) sl@0: { sl@0: if (!uis) sl@0: return NULL; sl@0: switch(uis->type) sl@0: { sl@0: case UIT_PROMPT: sl@0: case UIT_VERIFY: sl@0: return uis->result_buf; sl@0: default: sl@0: return NULL; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C const char *UI_get0_test_string(UI_STRING *uis) sl@0: { sl@0: if (!uis) sl@0: return NULL; sl@0: switch(uis->type) sl@0: { sl@0: case UIT_VERIFY: sl@0: return uis->_.string_data.test_buf; sl@0: default: sl@0: return NULL; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C int UI_get_result_minsize(UI_STRING *uis) sl@0: { sl@0: if (!uis) sl@0: return -1; sl@0: switch(uis->type) sl@0: { sl@0: case UIT_PROMPT: sl@0: case UIT_VERIFY: sl@0: return uis->_.string_data.result_minsize; sl@0: default: sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C int UI_get_result_maxsize(UI_STRING *uis) sl@0: { sl@0: if (!uis) sl@0: return -1; sl@0: switch(uis->type) sl@0: { sl@0: case UIT_PROMPT: sl@0: case UIT_VERIFY: sl@0: return uis->_.string_data.result_maxsize; sl@0: default: sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C int UI_set_result(UI *ui, UI_STRING *uis, const char *result) sl@0: { sl@0: int l = strlen(result); sl@0: sl@0: ui->flags &= ~UI_FLAG_REDOABLE; sl@0: sl@0: if (!uis) sl@0: return -1; sl@0: switch (uis->type) sl@0: { sl@0: case UIT_PROMPT: sl@0: case UIT_VERIFY: sl@0: { sl@0: char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize)+1]; sl@0: char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize)+1]; sl@0: sl@0: BIO_snprintf(number1, sizeof(number1), "%d", sl@0: uis->_.string_data.result_minsize); sl@0: BIO_snprintf(number2, sizeof(number2), "%d", sl@0: uis->_.string_data.result_maxsize); sl@0: sl@0: if (l < uis->_.string_data.result_minsize) sl@0: { sl@0: ui->flags |= UI_FLAG_REDOABLE; sl@0: UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_SMALL); sl@0: ERR_add_error_data(5,"You must type in ", sl@0: number1," to ",number2," characters"); sl@0: return -1; sl@0: } sl@0: if (l > uis->_.string_data.result_maxsize) sl@0: { sl@0: ui->flags |= UI_FLAG_REDOABLE; sl@0: UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_LARGE); sl@0: ERR_add_error_data(5,"You must type in ", sl@0: number1," to ",number2," characters"); sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: if (!uis->result_buf) sl@0: { sl@0: UIerr(UI_F_UI_SET_RESULT,UI_R_NO_RESULT_BUFFER); sl@0: return -1; sl@0: } sl@0: sl@0: BUF_strlcpy(uis->result_buf, result, sl@0: uis->_.string_data.result_maxsize + 1); sl@0: break; sl@0: case UIT_BOOLEAN: sl@0: { sl@0: const char *p; sl@0: sl@0: if (!uis->result_buf) sl@0: { sl@0: UIerr(UI_F_UI_SET_RESULT,UI_R_NO_RESULT_BUFFER); sl@0: return -1; sl@0: } sl@0: sl@0: uis->result_buf[0] = '\0'; sl@0: for(p = result; *p; p++) sl@0: { sl@0: if (strchr(uis->_.boolean_data.ok_chars, *p)) sl@0: { sl@0: uis->result_buf[0] = sl@0: uis->_.boolean_data.ok_chars[0]; sl@0: break; sl@0: } sl@0: if (strchr(uis->_.boolean_data.cancel_chars, *p)) sl@0: { sl@0: uis->result_buf[0] = sl@0: uis->_.boolean_data.cancel_chars[0]; sl@0: break; sl@0: } sl@0: } sl@0: default: sl@0: break; sl@0: } sl@0: } sl@0: return 0; sl@0: }