1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/ui/ui_lib.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,920 @@
1.4 +/* crypto/ui/ui_lib.c -*- mode:C; c-file-style: "eay" -*- */
1.5 +/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
1.6 + * project 2001.
1.7 + */
1.8 +/* ====================================================================
1.9 + * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
1.10 + *
1.11 + * Redistribution and use in source and binary forms, with or without
1.12 + * modification, are permitted provided that the following conditions
1.13 + * are met:
1.14 + *
1.15 + * 1. Redistributions of source code must retain the above copyright
1.16 + * notice, this list of conditions and the following disclaimer.
1.17 + *
1.18 + * 2. Redistributions in binary form must reproduce the above copyright
1.19 + * notice, this list of conditions and the following disclaimer in
1.20 + * the documentation and/or other materials provided with the
1.21 + * distribution.
1.22 + *
1.23 + * 3. All advertising materials mentioning features or use of this
1.24 + * software must display the following acknowledgment:
1.25 + * "This product includes software developed by the OpenSSL Project
1.26 + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
1.27 + *
1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.29 + * endorse or promote products derived from this software without
1.30 + * prior written permission. For written permission, please contact
1.31 + * openssl-core@openssl.org.
1.32 + *
1.33 + * 5. Products derived from this software may not be called "OpenSSL"
1.34 + * nor may "OpenSSL" appear in their names without prior written
1.35 + * permission of the OpenSSL Project.
1.36 + *
1.37 + * 6. Redistributions of any form whatsoever must retain the following
1.38 + * acknowledgment:
1.39 + * "This product includes software developed by the OpenSSL Project
1.40 + * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
1.41 + *
1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.45 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.54 + * ====================================================================
1.55 + *
1.56 + * This product includes cryptographic software written by Eric Young
1.57 + * (eay@cryptsoft.com). This product includes software written by Tim
1.58 + * Hudson (tjh@cryptsoft.com).
1.59 + *
1.60 + */
1.61 + /*
1.62 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.63 + */
1.64 +
1.65 +#include <string.h>
1.66 +#include "cryptlib.h"
1.67 +#include <openssl/e_os2.h>
1.68 +#include <openssl/buffer.h>
1.69 +#include <openssl/ui.h>
1.70 +#include <openssl/err.h>
1.71 +#include "ui_locl.h"
1.72 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
1.73 +#include "libcrypto_wsd_macros.h"
1.74 +#include "libcrypto_wsd.h"
1.75 +#endif
1.76 +
1.77 +IMPLEMENT_STACK_OF(UI_STRING_ST)
1.78 +
1.79 +#ifndef EMULATOR
1.80 +static const UI_METHOD *default_UI_meth=NULL;
1.81 +#else
1.82 +GET_STATIC_VAR_FROM_TLS(default_UI_meth,ui_lib,const UI_METHOD *)
1.83 +#define default_UI_meth (*GET_WSD_VAR_NAME(default_UI_meth,ui_lib, s)())
1.84 +#endif
1.85 +
1.86 +EXPORT_C UI *UI_new(void)
1.87 + {
1.88 + return(UI_new_method(NULL));
1.89 + }
1.90 +
1.91 +EXPORT_C UI *UI_new_method(const UI_METHOD *method)
1.92 + {
1.93 + UI *ret;
1.94 +
1.95 + ret=(UI *)OPENSSL_malloc(sizeof(UI));
1.96 + if (ret == NULL)
1.97 + {
1.98 + UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
1.99 + return NULL;
1.100 + }
1.101 + if (method == NULL)
1.102 + ret->meth=UI_get_default_method();
1.103 + else
1.104 + ret->meth=method;
1.105 +
1.106 + ret->strings=NULL;
1.107 + ret->user_data=NULL;
1.108 + CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data);
1.109 + return ret;
1.110 + }
1.111 +
1.112 +static void free_string(UI_STRING *uis)
1.113 + {
1.114 + if (uis->flags & OUT_STRING_FREEABLE)
1.115 + {
1.116 + OPENSSL_free((char *)uis->out_string);
1.117 + switch(uis->type)
1.118 + {
1.119 + case UIT_BOOLEAN:
1.120 + OPENSSL_free((char *)uis->_.boolean_data.action_desc);
1.121 + OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
1.122 + OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
1.123 + break;
1.124 + default:
1.125 + break;
1.126 + }
1.127 + }
1.128 + OPENSSL_free(uis);
1.129 + }
1.130 +
1.131 +EXPORT_C void UI_free(UI *ui)
1.132 + {
1.133 + if (ui == NULL)
1.134 + return;
1.135 + sk_UI_STRING_pop_free(ui->strings,free_string);
1.136 + CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
1.137 + OPENSSL_free(ui);
1.138 + }
1.139 +
1.140 +static int allocate_string_stack(UI *ui)
1.141 + {
1.142 + if (ui->strings == NULL)
1.143 + {
1.144 + ui->strings=sk_UI_STRING_new_null();
1.145 + if (ui->strings == NULL)
1.146 + {
1.147 + return -1;
1.148 + }
1.149 + }
1.150 + return 0;
1.151 + }
1.152 +
1.153 +static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
1.154 + int prompt_freeable, enum UI_string_types type, int input_flags,
1.155 + char *result_buf)
1.156 + {
1.157 + UI_STRING *ret = NULL;
1.158 +
1.159 + if (prompt == NULL)
1.160 + {
1.161 + UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,ERR_R_PASSED_NULL_PARAMETER);
1.162 + }
1.163 + else if ((type == UIT_PROMPT || type == UIT_VERIFY
1.164 + || type == UIT_BOOLEAN) && result_buf == NULL)
1.165 + {
1.166 + UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,UI_R_NO_RESULT_BUFFER);
1.167 + }
1.168 + else if ((ret = (UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING))))
1.169 + {
1.170 + ret->out_string=prompt;
1.171 + ret->flags=prompt_freeable ? OUT_STRING_FREEABLE : 0;
1.172 + ret->input_flags=input_flags;
1.173 + ret->type=type;
1.174 + ret->result_buf=result_buf;
1.175 + }
1.176 + return ret;
1.177 + }
1.178 +
1.179 +static int general_allocate_string(UI *ui, const char *prompt,
1.180 + int prompt_freeable, enum UI_string_types type, int input_flags,
1.181 + char *result_buf, int minsize, int maxsize, const char *test_buf)
1.182 + {
1.183 + int ret = -1;
1.184 + UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
1.185 + type, input_flags, result_buf);
1.186 +
1.187 + if (s)
1.188 + {
1.189 + if (allocate_string_stack(ui) >= 0)
1.190 + {
1.191 + s->_.string_data.result_minsize=minsize;
1.192 + s->_.string_data.result_maxsize=maxsize;
1.193 + s->_.string_data.test_buf=test_buf;
1.194 + ret=sk_UI_STRING_push(ui->strings, s);
1.195 + /* sk_push() returns 0 on error. Let's addapt that */
1.196 + if (ret <= 0) ret--;
1.197 + }
1.198 + else
1.199 + free_string(s);
1.200 + }
1.201 + return ret;
1.202 + }
1.203 +
1.204 +static int general_allocate_boolean(UI *ui,
1.205 + const char *prompt, const char *action_desc,
1.206 + const char *ok_chars, const char *cancel_chars,
1.207 + int prompt_freeable, enum UI_string_types type, int input_flags,
1.208 + char *result_buf)
1.209 + {
1.210 + int ret = -1;
1.211 + UI_STRING *s;
1.212 + const char *p;
1.213 +
1.214 + if (ok_chars == NULL)
1.215 + {
1.216 + UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,ERR_R_PASSED_NULL_PARAMETER);
1.217 + }
1.218 + else if (cancel_chars == NULL)
1.219 + {
1.220 + UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,ERR_R_PASSED_NULL_PARAMETER);
1.221 + }
1.222 + else
1.223 + {
1.224 + for(p = ok_chars; *p; p++)
1.225 + {
1.226 + if (strchr(cancel_chars, *p))
1.227 + {
1.228 + UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
1.229 + UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
1.230 + }
1.231 + }
1.232 +
1.233 + s = general_allocate_prompt(ui, prompt, prompt_freeable,
1.234 + type, input_flags, result_buf);
1.235 +
1.236 + if (s)
1.237 + {
1.238 + if (allocate_string_stack(ui) >= 0)
1.239 + {
1.240 + s->_.boolean_data.action_desc = action_desc;
1.241 + s->_.boolean_data.ok_chars = ok_chars;
1.242 + s->_.boolean_data.cancel_chars = cancel_chars;
1.243 + ret=sk_UI_STRING_push(ui->strings, s);
1.244 + /* sk_push() returns 0 on error.
1.245 + Let's addapt that */
1.246 + if (ret <= 0) ret--;
1.247 + }
1.248 + else
1.249 + free_string(s);
1.250 + }
1.251 + }
1.252 + return ret;
1.253 + }
1.254 +
1.255 +/* Returns the index to the place in the stack or -1 for error. Uses a
1.256 + direct reference to the prompt. */
1.257 +EXPORT_C int UI_add_input_string(UI *ui, const char *prompt, int flags,
1.258 + char *result_buf, int minsize, int maxsize)
1.259 + {
1.260 + return general_allocate_string(ui, prompt, 0,
1.261 + UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL);
1.262 + }
1.263 +
1.264 +/* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
1.265 +EXPORT_C int UI_dup_input_string(UI *ui, const char *prompt, int flags,
1.266 + char *result_buf, int minsize, int maxsize)
1.267 + {
1.268 + char *prompt_copy=NULL;
1.269 +
1.270 + if (prompt)
1.271 + {
1.272 + prompt_copy=BUF_strdup(prompt);
1.273 + if (prompt_copy == NULL)
1.274 + {
1.275 + UIerr(UI_F_UI_DUP_INPUT_STRING,ERR_R_MALLOC_FAILURE);
1.276 + return 0;
1.277 + }
1.278 + }
1.279 +
1.280 + return general_allocate_string(ui, prompt_copy, 1,
1.281 + UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL);
1.282 + }
1.283 +
1.284 +EXPORT_C int UI_add_verify_string(UI *ui, const char *prompt, int flags,
1.285 + char *result_buf, int minsize, int maxsize, const char *test_buf)
1.286 + {
1.287 + return general_allocate_string(ui, prompt, 0,
1.288 + UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf);
1.289 + }
1.290 +
1.291 +EXPORT_C int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
1.292 + char *result_buf, int minsize, int maxsize, const char *test_buf)
1.293 + {
1.294 + char *prompt_copy=NULL;
1.295 +
1.296 + if (prompt)
1.297 + {
1.298 + prompt_copy=BUF_strdup(prompt);
1.299 + if (prompt_copy == NULL)
1.300 + {
1.301 + UIerr(UI_F_UI_DUP_VERIFY_STRING,ERR_R_MALLOC_FAILURE);
1.302 + return -1;
1.303 + }
1.304 + }
1.305 +
1.306 + return general_allocate_string(ui, prompt_copy, 1,
1.307 + UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf);
1.308 + }
1.309 +
1.310 +EXPORT_C int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
1.311 + const char *ok_chars, const char *cancel_chars,
1.312 + int flags, char *result_buf)
1.313 + {
1.314 + return general_allocate_boolean(ui, prompt, action_desc,
1.315 + ok_chars, cancel_chars, 0, UIT_BOOLEAN, flags, result_buf);
1.316 + }
1.317 +
1.318 +EXPORT_C int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
1.319 + const char *ok_chars, const char *cancel_chars,
1.320 + int flags, char *result_buf)
1.321 + {
1.322 + char *prompt_copy = NULL;
1.323 + char *action_desc_copy = NULL;
1.324 + char *ok_chars_copy = NULL;
1.325 + char *cancel_chars_copy = NULL;
1.326 +
1.327 + if (prompt)
1.328 + {
1.329 + prompt_copy=BUF_strdup(prompt);
1.330 + if (prompt_copy == NULL)
1.331 + {
1.332 + UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE);
1.333 + goto err;
1.334 + }
1.335 + }
1.336 +
1.337 + if (action_desc)
1.338 + {
1.339 + action_desc_copy=BUF_strdup(action_desc);
1.340 + if (action_desc_copy == NULL)
1.341 + {
1.342 + UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE);
1.343 + goto err;
1.344 + }
1.345 + }
1.346 +
1.347 + if (ok_chars)
1.348 + {
1.349 + ok_chars_copy=BUF_strdup(ok_chars);
1.350 + if (ok_chars_copy == NULL)
1.351 + {
1.352 + UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE);
1.353 + goto err;
1.354 + }
1.355 + }
1.356 +
1.357 + if (cancel_chars)
1.358 + {
1.359 + cancel_chars_copy=BUF_strdup(cancel_chars);
1.360 + if (cancel_chars_copy == NULL)
1.361 + {
1.362 + UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE);
1.363 + goto err;
1.364 + }
1.365 + }
1.366 +
1.367 + return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
1.368 + ok_chars_copy, cancel_chars_copy, 1, UIT_BOOLEAN, flags,
1.369 + result_buf);
1.370 + err:
1.371 + if (prompt_copy) OPENSSL_free(prompt_copy);
1.372 + if (action_desc_copy) OPENSSL_free(action_desc_copy);
1.373 + if (ok_chars_copy) OPENSSL_free(ok_chars_copy);
1.374 + if (cancel_chars_copy) OPENSSL_free(cancel_chars_copy);
1.375 + return -1;
1.376 + }
1.377 +
1.378 +EXPORT_C int UI_add_info_string(UI *ui, const char *text)
1.379 + {
1.380 + return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
1.381 + NULL);
1.382 + }
1.383 +
1.384 +EXPORT_C int UI_dup_info_string(UI *ui, const char *text)
1.385 + {
1.386 + char *text_copy=NULL;
1.387 +
1.388 + if (text)
1.389 + {
1.390 + text_copy=BUF_strdup(text);
1.391 + if (text_copy == NULL)
1.392 + {
1.393 + UIerr(UI_F_UI_DUP_INFO_STRING,ERR_R_MALLOC_FAILURE);
1.394 + return -1;
1.395 + }
1.396 + }
1.397 +
1.398 + return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
1.399 + 0, 0, NULL);
1.400 + }
1.401 +
1.402 +EXPORT_C int UI_add_error_string(UI *ui, const char *text)
1.403 + {
1.404 + return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
1.405 + NULL);
1.406 + }
1.407 +
1.408 +EXPORT_C int UI_dup_error_string(UI *ui, const char *text)
1.409 + {
1.410 + char *text_copy=NULL;
1.411 +
1.412 + if (text)
1.413 + {
1.414 + text_copy=BUF_strdup(text);
1.415 + if (text_copy == NULL)
1.416 + {
1.417 + UIerr(UI_F_UI_DUP_ERROR_STRING,ERR_R_MALLOC_FAILURE);
1.418 + return -1;
1.419 + }
1.420 + }
1.421 + return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
1.422 + 0, 0, NULL);
1.423 + }
1.424 +
1.425 +EXPORT_C char *UI_construct_prompt(UI *ui, const char *object_desc,
1.426 + const char *object_name)
1.427 + {
1.428 + char *prompt = NULL;
1.429 +
1.430 + if (ui->meth->ui_construct_prompt)
1.431 + prompt = ui->meth->ui_construct_prompt(ui,
1.432 + object_desc, object_name);
1.433 + else
1.434 + {
1.435 + char prompt1[] = "Enter ";
1.436 + char prompt2[] = " for ";
1.437 + char prompt3[] = ":";
1.438 + int len = 0;
1.439 +
1.440 + if (object_desc == NULL)
1.441 + return NULL;
1.442 + len = sizeof(prompt1) - 1 + strlen(object_desc);
1.443 + if (object_name)
1.444 + len += sizeof(prompt2) - 1 + strlen(object_name);
1.445 + len += sizeof(prompt3) - 1;
1.446 +
1.447 + prompt = (char *)OPENSSL_malloc(len + 1);
1.448 +#ifdef SYMBIAN
1.449 + if(prompt==NULL)
1.450 + return NULL;
1.451 +#endif
1.452 + BUF_strlcpy(prompt, prompt1, len + 1);
1.453 + BUF_strlcat(prompt, object_desc, len + 1);
1.454 + if (object_name)
1.455 + {
1.456 + BUF_strlcat(prompt, prompt2, len + 1);
1.457 + BUF_strlcat(prompt, object_name, len + 1);
1.458 + }
1.459 + BUF_strlcat(prompt, prompt3, len + 1);
1.460 + }
1.461 + return prompt;
1.462 + }
1.463 +
1.464 +EXPORT_C void *UI_add_user_data(UI *ui, void *user_data)
1.465 + {
1.466 + void *old_data = ui->user_data;
1.467 + ui->user_data = user_data;
1.468 + return old_data;
1.469 + }
1.470 +
1.471 +EXPORT_C void *UI_get0_user_data(UI *ui)
1.472 + {
1.473 + return ui->user_data;
1.474 + }
1.475 +
1.476 +EXPORT_C const char *UI_get0_result(UI *ui, int i)
1.477 + {
1.478 + if (i < 0)
1.479 + {
1.480 + UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_SMALL);
1.481 + return NULL;
1.482 + }
1.483 + if (i >= sk_UI_STRING_num(ui->strings))
1.484 + {
1.485 + UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_LARGE);
1.486 + return NULL;
1.487 + }
1.488 + return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
1.489 + }
1.490 +
1.491 +static int print_error(const char *str, size_t len, UI *ui)
1.492 + {
1.493 + UI_STRING uis;
1.494 +
1.495 + memset(&uis, 0, sizeof(uis));
1.496 + uis.type = UIT_ERROR;
1.497 + uis.out_string = str;
1.498 +
1.499 + if (ui->meth->ui_write_string
1.500 + && !ui->meth->ui_write_string(ui, &uis))
1.501 + return -1;
1.502 + return 0;
1.503 + }
1.504 +
1.505 +EXPORT_C int UI_process(UI *ui)
1.506 + {
1.507 + int i, ok=0;
1.508 +
1.509 + if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
1.510 + return -1;
1.511 +
1.512 + if (ui->flags & UI_FLAG_PRINT_ERRORS)
1.513 + ERR_print_errors_cb(
1.514 + (int (*)(const char *, size_t, void *))print_error,
1.515 + (void *)ui);
1.516 +
1.517 + for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
1.518 + {
1.519 + if (ui->meth->ui_write_string
1.520 + && !ui->meth->ui_write_string(ui,
1.521 + sk_UI_STRING_value(ui->strings, i)))
1.522 + {
1.523 + ok=-1;
1.524 + goto err;
1.525 + }
1.526 + }
1.527 +
1.528 + if (ui->meth->ui_flush)
1.529 + switch(ui->meth->ui_flush(ui))
1.530 + {
1.531 + case -1: /* Interrupt/Cancel/something... */
1.532 + ok = -2;
1.533 + goto err;
1.534 + case 0: /* Errors */
1.535 + ok = -1;
1.536 + goto err;
1.537 + default: /* Success */
1.538 + ok = 0;
1.539 + break;
1.540 + }
1.541 +
1.542 + for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
1.543 + {
1.544 + if (ui->meth->ui_read_string)
1.545 + {
1.546 + switch(ui->meth->ui_read_string(ui,
1.547 + sk_UI_STRING_value(ui->strings, i)))
1.548 + {
1.549 + case -1: /* Interrupt/Cancel/something... */
1.550 + ok = -2;
1.551 + goto err;
1.552 + case 0: /* Errors */
1.553 + ok = -1;
1.554 + goto err;
1.555 + default: /* Success */
1.556 + ok = 0;
1.557 + break;
1.558 + }
1.559 + }
1.560 + }
1.561 + err:
1.562 + if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
1.563 + return -1;
1.564 + return ok;
1.565 + }
1.566 +
1.567 +EXPORT_C int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f)(void))
1.568 + {
1.569 + if (ui == NULL)
1.570 + {
1.571 + UIerr(UI_F_UI_CTRL,ERR_R_PASSED_NULL_PARAMETER);
1.572 + return -1;
1.573 + }
1.574 + switch(cmd)
1.575 + {
1.576 + case UI_CTRL_PRINT_ERRORS:
1.577 + {
1.578 + int save_flag = !!(ui->flags & UI_FLAG_PRINT_ERRORS);
1.579 + if (i)
1.580 + ui->flags |= UI_FLAG_PRINT_ERRORS;
1.581 + else
1.582 + ui->flags &= ~UI_FLAG_PRINT_ERRORS;
1.583 + return save_flag;
1.584 + }
1.585 + case UI_CTRL_IS_REDOABLE:
1.586 + return !!(ui->flags & UI_FLAG_REDOABLE);
1.587 + default:
1.588 + break;
1.589 + }
1.590 + UIerr(UI_F_UI_CTRL,UI_R_UNKNOWN_CONTROL_COMMAND);
1.591 + return -1;
1.592 + }
1.593 +
1.594 +EXPORT_C int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
1.595 + CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
1.596 + {
1.597 + return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp,
1.598 + new_func, dup_func, free_func);
1.599 + }
1.600 +
1.601 +EXPORT_C int UI_set_ex_data(UI *r, int idx, void *arg)
1.602 + {
1.603 + return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
1.604 + }
1.605 +
1.606 +EXPORT_C void *UI_get_ex_data(UI *r, int idx)
1.607 + {
1.608 + return(CRYPTO_get_ex_data(&r->ex_data,idx));
1.609 + }
1.610 +
1.611 +EXPORT_C void UI_set_default_method(const UI_METHOD *meth)
1.612 + {
1.613 + default_UI_meth=meth;
1.614 + }
1.615 +
1.616 +EXPORT_C const UI_METHOD *UI_get_default_method(void)
1.617 + {
1.618 + if (default_UI_meth == NULL)
1.619 + {
1.620 + default_UI_meth=UI_OpenSSL();
1.621 + }
1.622 + return default_UI_meth;
1.623 + }
1.624 +
1.625 +EXPORT_C const UI_METHOD *UI_get_method(UI *ui)
1.626 + {
1.627 + return ui->meth;
1.628 + }
1.629 +
1.630 +EXPORT_C const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
1.631 + {
1.632 + ui->meth=meth;
1.633 + return ui->meth;
1.634 + }
1.635 +
1.636 +
1.637 +EXPORT_C UI_METHOD *UI_create_method(char *name)
1.638 + {
1.639 + UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
1.640 +
1.641 + if (ui_method)
1.642 + {
1.643 + memset(ui_method, 0, sizeof(*ui_method));
1.644 + ui_method->name = BUF_strdup(name);
1.645 + }
1.646 + return ui_method;
1.647 + }
1.648 +
1.649 +/* BIG FSCKING WARNING!!!! If you use this on a statically allocated method
1.650 + (that is, it hasn't been allocated using UI_create_method(), you deserve
1.651 + anything Murphy can throw at you and more! You have been warned. */
1.652 +EXPORT_C void UI_destroy_method(UI_METHOD *ui_method)
1.653 + {
1.654 + OPENSSL_free(ui_method->name);
1.655 + ui_method->name = NULL;
1.656 + OPENSSL_free(ui_method);
1.657 + }
1.658 +
1.659 +EXPORT_C int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui))
1.660 + {
1.661 + if (method)
1.662 + {
1.663 + method->ui_open_session = opener;
1.664 + return 0;
1.665 + }
1.666 + else
1.667 + return -1;
1.668 + }
1.669 +
1.670 +EXPORT_C int UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis))
1.671 + {
1.672 + if (method)
1.673 + {
1.674 + method->ui_write_string = writer;
1.675 + return 0;
1.676 + }
1.677 + else
1.678 + return -1;
1.679 + }
1.680 +
1.681 +EXPORT_C int UI_method_set_flusher(UI_METHOD *method, int (*flusher)(UI *ui))
1.682 + {
1.683 + if (method)
1.684 + {
1.685 + method->ui_flush = flusher;
1.686 + return 0;
1.687 + }
1.688 + else
1.689 + return -1;
1.690 + }
1.691 +
1.692 +EXPORT_C int UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis))
1.693 + {
1.694 + if (method)
1.695 + {
1.696 + method->ui_read_string = reader;
1.697 + return 0;
1.698 + }
1.699 + else
1.700 + return -1;
1.701 + }
1.702 +
1.703 +EXPORT_C int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
1.704 + {
1.705 + if (method)
1.706 + {
1.707 + method->ui_close_session = closer;
1.708 + return 0;
1.709 + }
1.710 + else
1.711 + return -1;
1.712 + }
1.713 +
1.714 +EXPORT_C int (*UI_method_get_opener(UI_METHOD *method))(UI*)
1.715 + {
1.716 + if (method)
1.717 + return method->ui_open_session;
1.718 + else
1.719 + return NULL;
1.720 + }
1.721 +
1.722 +EXPORT_C int (*UI_method_get_writer(UI_METHOD *method))(UI*,UI_STRING*)
1.723 + {
1.724 + if (method)
1.725 + return method->ui_write_string;
1.726 + else
1.727 + return NULL;
1.728 + }
1.729 +
1.730 +EXPORT_C int (*UI_method_get_flusher(UI_METHOD *method))(UI*)
1.731 + {
1.732 + if (method)
1.733 + return method->ui_flush;
1.734 + else
1.735 + return NULL;
1.736 + }
1.737 +
1.738 +EXPORT_C int (*UI_method_get_reader(UI_METHOD *method))(UI*,UI_STRING*)
1.739 + {
1.740 + if (method)
1.741 + return method->ui_read_string;
1.742 + else
1.743 + return NULL;
1.744 + }
1.745 +
1.746 +EXPORT_C int (*UI_method_get_closer(UI_METHOD *method))(UI*)
1.747 + {
1.748 + if (method)
1.749 + return method->ui_close_session;
1.750 + else
1.751 + return NULL;
1.752 + }
1.753 +
1.754 +EXPORT_C enum UI_string_types UI_get_string_type(UI_STRING *uis)
1.755 + {
1.756 + if (!uis)
1.757 + return UIT_NONE;
1.758 + return uis->type;
1.759 + }
1.760 +
1.761 +EXPORT_C int UI_get_input_flags(UI_STRING *uis)
1.762 + {
1.763 + if (!uis)
1.764 + return 0;
1.765 + return uis->input_flags;
1.766 + }
1.767 +
1.768 +EXPORT_C const char *UI_get0_output_string(UI_STRING *uis)
1.769 + {
1.770 + if (!uis)
1.771 + return NULL;
1.772 + return uis->out_string;
1.773 + }
1.774 +
1.775 +EXPORT_C const char *UI_get0_action_string(UI_STRING *uis)
1.776 + {
1.777 + if (!uis)
1.778 + return NULL;
1.779 + switch(uis->type)
1.780 + {
1.781 + case UIT_PROMPT:
1.782 + case UIT_BOOLEAN:
1.783 + return uis->_.boolean_data.action_desc;
1.784 + default:
1.785 + return NULL;
1.786 + }
1.787 + }
1.788 +
1.789 +EXPORT_C const char *UI_get0_result_string(UI_STRING *uis)
1.790 + {
1.791 + if (!uis)
1.792 + return NULL;
1.793 + switch(uis->type)
1.794 + {
1.795 + case UIT_PROMPT:
1.796 + case UIT_VERIFY:
1.797 + return uis->result_buf;
1.798 + default:
1.799 + return NULL;
1.800 + }
1.801 + }
1.802 +
1.803 +EXPORT_C const char *UI_get0_test_string(UI_STRING *uis)
1.804 + {
1.805 + if (!uis)
1.806 + return NULL;
1.807 + switch(uis->type)
1.808 + {
1.809 + case UIT_VERIFY:
1.810 + return uis->_.string_data.test_buf;
1.811 + default:
1.812 + return NULL;
1.813 + }
1.814 + }
1.815 +
1.816 +EXPORT_C int UI_get_result_minsize(UI_STRING *uis)
1.817 + {
1.818 + if (!uis)
1.819 + return -1;
1.820 + switch(uis->type)
1.821 + {
1.822 + case UIT_PROMPT:
1.823 + case UIT_VERIFY:
1.824 + return uis->_.string_data.result_minsize;
1.825 + default:
1.826 + return -1;
1.827 + }
1.828 + }
1.829 +
1.830 +EXPORT_C int UI_get_result_maxsize(UI_STRING *uis)
1.831 + {
1.832 + if (!uis)
1.833 + return -1;
1.834 + switch(uis->type)
1.835 + {
1.836 + case UIT_PROMPT:
1.837 + case UIT_VERIFY:
1.838 + return uis->_.string_data.result_maxsize;
1.839 + default:
1.840 + return -1;
1.841 + }
1.842 + }
1.843 +
1.844 +EXPORT_C int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
1.845 + {
1.846 + int l = strlen(result);
1.847 +
1.848 + ui->flags &= ~UI_FLAG_REDOABLE;
1.849 +
1.850 + if (!uis)
1.851 + return -1;
1.852 + switch (uis->type)
1.853 + {
1.854 + case UIT_PROMPT:
1.855 + case UIT_VERIFY:
1.856 + {
1.857 + char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize)+1];
1.858 + char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize)+1];
1.859 +
1.860 + BIO_snprintf(number1, sizeof(number1), "%d",
1.861 + uis->_.string_data.result_minsize);
1.862 + BIO_snprintf(number2, sizeof(number2), "%d",
1.863 + uis->_.string_data.result_maxsize);
1.864 +
1.865 + if (l < uis->_.string_data.result_minsize)
1.866 + {
1.867 + ui->flags |= UI_FLAG_REDOABLE;
1.868 + UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_SMALL);
1.869 + ERR_add_error_data(5,"You must type in ",
1.870 + number1," to ",number2," characters");
1.871 + return -1;
1.872 + }
1.873 + if (l > uis->_.string_data.result_maxsize)
1.874 + {
1.875 + ui->flags |= UI_FLAG_REDOABLE;
1.876 + UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_LARGE);
1.877 + ERR_add_error_data(5,"You must type in ",
1.878 + number1," to ",number2," characters");
1.879 + return -1;
1.880 + }
1.881 + }
1.882 +
1.883 + if (!uis->result_buf)
1.884 + {
1.885 + UIerr(UI_F_UI_SET_RESULT,UI_R_NO_RESULT_BUFFER);
1.886 + return -1;
1.887 + }
1.888 +
1.889 + BUF_strlcpy(uis->result_buf, result,
1.890 + uis->_.string_data.result_maxsize + 1);
1.891 + break;
1.892 + case UIT_BOOLEAN:
1.893 + {
1.894 + const char *p;
1.895 +
1.896 + if (!uis->result_buf)
1.897 + {
1.898 + UIerr(UI_F_UI_SET_RESULT,UI_R_NO_RESULT_BUFFER);
1.899 + return -1;
1.900 + }
1.901 +
1.902 + uis->result_buf[0] = '\0';
1.903 + for(p = result; *p; p++)
1.904 + {
1.905 + if (strchr(uis->_.boolean_data.ok_chars, *p))
1.906 + {
1.907 + uis->result_buf[0] =
1.908 + uis->_.boolean_data.ok_chars[0];
1.909 + break;
1.910 + }
1.911 + if (strchr(uis->_.boolean_data.cancel_chars, *p))
1.912 + {
1.913 + uis->result_buf[0] =
1.914 + uis->_.boolean_data.cancel_chars[0];
1.915 + break;
1.916 + }
1.917 + }
1.918 + default:
1.919 + break;
1.920 + }
1.921 + }
1.922 + return 0;
1.923 + }