1295016Sjkim/* crypto/ui/ui_lib.c */ 2280304Sjkim/* 3280304Sjkim * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project 4280304Sjkim * 2001. 5109998Smarkm */ 6109998Smarkm/* ==================================================================== 7109998Smarkm * Copyright (c) 2001 The OpenSSL Project. All rights reserved. 8109998Smarkm * 9109998Smarkm * Redistribution and use in source and binary forms, with or without 10109998Smarkm * modification, are permitted provided that the following conditions 11109998Smarkm * are met: 12109998Smarkm * 13109998Smarkm * 1. Redistributions of source code must retain the above copyright 14280304Sjkim * notice, this list of conditions and the following disclaimer. 15109998Smarkm * 16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 17109998Smarkm * notice, this list of conditions and the following disclaimer in 18109998Smarkm * the documentation and/or other materials provided with the 19109998Smarkm * distribution. 20109998Smarkm * 21109998Smarkm * 3. All advertising materials mentioning features or use of this 22109998Smarkm * software must display the following acknowledgment: 23109998Smarkm * "This product includes software developed by the OpenSSL Project 24109998Smarkm * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 25109998Smarkm * 26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27109998Smarkm * endorse or promote products derived from this software without 28109998Smarkm * prior written permission. For written permission, please contact 29109998Smarkm * openssl-core@openssl.org. 30109998Smarkm * 31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 32109998Smarkm * nor may "OpenSSL" appear in their names without prior written 33109998Smarkm * permission of the OpenSSL Project. 34109998Smarkm * 35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 36109998Smarkm * acknowledgment: 37109998Smarkm * "This product includes software developed by the OpenSSL Project 38109998Smarkm * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 39109998Smarkm * 40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 52109998Smarkm * ==================================================================== 53109998Smarkm * 54109998Smarkm * This product includes cryptographic software written by Eric Young 55109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 56109998Smarkm * Hudson (tjh@cryptsoft.com). 57109998Smarkm * 58109998Smarkm */ 59109998Smarkm 60109998Smarkm#include <string.h> 61160814Ssimon#include "cryptlib.h" 62109998Smarkm#include <openssl/e_os2.h> 63109998Smarkm#include <openssl/buffer.h> 64109998Smarkm#include <openssl/ui.h> 65109998Smarkm#include <openssl/err.h> 66109998Smarkm#include "ui_locl.h" 67109998Smarkm 68109998SmarkmIMPLEMENT_STACK_OF(UI_STRING_ST) 69109998Smarkm 70280304Sjkimstatic const UI_METHOD *default_UI_meth = NULL; 71109998Smarkm 72109998SmarkmUI *UI_new(void) 73280304Sjkim{ 74280304Sjkim return (UI_new_method(NULL)); 75280304Sjkim} 76109998Smarkm 77109998SmarkmUI *UI_new_method(const UI_METHOD *method) 78280304Sjkim{ 79280304Sjkim UI *ret; 80109998Smarkm 81280304Sjkim ret = (UI *)OPENSSL_malloc(sizeof(UI)); 82280304Sjkim if (ret == NULL) { 83280304Sjkim UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE); 84280304Sjkim return NULL; 85280304Sjkim } 86280304Sjkim if (method == NULL) 87280304Sjkim ret->meth = UI_get_default_method(); 88280304Sjkim else 89280304Sjkim ret->meth = method; 90109998Smarkm 91280304Sjkim ret->strings = NULL; 92280304Sjkim ret->user_data = NULL; 93280304Sjkim ret->flags = 0; 94280304Sjkim CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data); 95280304Sjkim return ret; 96280304Sjkim} 97109998Smarkm 98109998Smarkmstatic void free_string(UI_STRING *uis) 99280304Sjkim{ 100280304Sjkim if (uis->flags & OUT_STRING_FREEABLE) { 101280304Sjkim OPENSSL_free((char *)uis->out_string); 102280304Sjkim switch (uis->type) { 103280304Sjkim case UIT_BOOLEAN: 104280304Sjkim OPENSSL_free((char *)uis->_.boolean_data.action_desc); 105280304Sjkim OPENSSL_free((char *)uis->_.boolean_data.ok_chars); 106280304Sjkim OPENSSL_free((char *)uis->_.boolean_data.cancel_chars); 107280304Sjkim break; 108280304Sjkim default: 109280304Sjkim break; 110280304Sjkim } 111280304Sjkim } 112280304Sjkim OPENSSL_free(uis); 113280304Sjkim} 114109998Smarkm 115109998Smarkmvoid UI_free(UI *ui) 116280304Sjkim{ 117280304Sjkim if (ui == NULL) 118280304Sjkim return; 119280304Sjkim sk_UI_STRING_pop_free(ui->strings, free_string); 120280304Sjkim CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data); 121280304Sjkim OPENSSL_free(ui); 122280304Sjkim} 123109998Smarkm 124109998Smarkmstatic int allocate_string_stack(UI *ui) 125280304Sjkim{ 126280304Sjkim if (ui->strings == NULL) { 127280304Sjkim ui->strings = sk_UI_STRING_new_null(); 128280304Sjkim if (ui->strings == NULL) { 129280304Sjkim return -1; 130280304Sjkim } 131280304Sjkim } 132280304Sjkim return 0; 133280304Sjkim} 134109998Smarkm 135109998Smarkmstatic UI_STRING *general_allocate_prompt(UI *ui, const char *prompt, 136280304Sjkim int prompt_freeable, 137280304Sjkim enum UI_string_types type, 138280304Sjkim int input_flags, char *result_buf) 139280304Sjkim{ 140280304Sjkim UI_STRING *ret = NULL; 141109998Smarkm 142280304Sjkim if (prompt == NULL) { 143280304Sjkim UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER); 144280304Sjkim } else if ((type == UIT_PROMPT || type == UIT_VERIFY 145280304Sjkim || type == UIT_BOOLEAN) && result_buf == NULL) { 146280304Sjkim UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER); 147280304Sjkim } else if ((ret = (UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING)))) { 148280304Sjkim ret->out_string = prompt; 149280304Sjkim ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0; 150280304Sjkim ret->input_flags = input_flags; 151280304Sjkim ret->type = type; 152280304Sjkim ret->result_buf = result_buf; 153280304Sjkim } 154280304Sjkim return ret; 155280304Sjkim} 156109998Smarkm 157109998Smarkmstatic int general_allocate_string(UI *ui, const char *prompt, 158280304Sjkim int prompt_freeable, 159280304Sjkim enum UI_string_types type, int input_flags, 160280304Sjkim char *result_buf, int minsize, int maxsize, 161280304Sjkim const char *test_buf) 162280304Sjkim{ 163280304Sjkim int ret = -1; 164280304Sjkim UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable, 165280304Sjkim type, input_flags, result_buf); 166109998Smarkm 167280304Sjkim if (s) { 168280304Sjkim if (allocate_string_stack(ui) >= 0) { 169280304Sjkim s->_.string_data.result_minsize = minsize; 170280304Sjkim s->_.string_data.result_maxsize = maxsize; 171280304Sjkim s->_.string_data.test_buf = test_buf; 172280304Sjkim ret = sk_UI_STRING_push(ui->strings, s); 173280304Sjkim /* sk_push() returns 0 on error. Let's addapt that */ 174280304Sjkim if (ret <= 0) 175280304Sjkim ret--; 176280304Sjkim } else 177280304Sjkim free_string(s); 178280304Sjkim } 179280304Sjkim return ret; 180280304Sjkim} 181109998Smarkm 182109998Smarkmstatic int general_allocate_boolean(UI *ui, 183280304Sjkim const char *prompt, 184280304Sjkim const char *action_desc, 185280304Sjkim const char *ok_chars, 186280304Sjkim const char *cancel_chars, 187280304Sjkim int prompt_freeable, 188280304Sjkim enum UI_string_types type, 189280304Sjkim int input_flags, char *result_buf) 190280304Sjkim{ 191280304Sjkim int ret = -1; 192280304Sjkim UI_STRING *s; 193280304Sjkim const char *p; 194109998Smarkm 195280304Sjkim if (ok_chars == NULL) { 196280304Sjkim UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER); 197280304Sjkim } else if (cancel_chars == NULL) { 198280304Sjkim UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER); 199280304Sjkim } else { 200280304Sjkim for (p = ok_chars; *p; p++) { 201280304Sjkim if (strchr(cancel_chars, *p)) { 202280304Sjkim UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, 203280304Sjkim UI_R_COMMON_OK_AND_CANCEL_CHARACTERS); 204280304Sjkim } 205280304Sjkim } 206109998Smarkm 207280304Sjkim s = general_allocate_prompt(ui, prompt, prompt_freeable, 208280304Sjkim type, input_flags, result_buf); 209109998Smarkm 210280304Sjkim if (s) { 211280304Sjkim if (allocate_string_stack(ui) >= 0) { 212280304Sjkim s->_.boolean_data.action_desc = action_desc; 213280304Sjkim s->_.boolean_data.ok_chars = ok_chars; 214280304Sjkim s->_.boolean_data.cancel_chars = cancel_chars; 215280304Sjkim ret = sk_UI_STRING_push(ui->strings, s); 216280304Sjkim /* 217280304Sjkim * sk_push() returns 0 on error. Let's addapt that 218280304Sjkim */ 219280304Sjkim if (ret <= 0) 220280304Sjkim ret--; 221280304Sjkim } else 222280304Sjkim free_string(s); 223280304Sjkim } 224280304Sjkim } 225280304Sjkim return ret; 226280304Sjkim} 227109998Smarkm 228280304Sjkim/* 229280304Sjkim * Returns the index to the place in the stack or -1 for error. Uses a 230280304Sjkim * direct reference to the prompt. 231280304Sjkim */ 232109998Smarkmint UI_add_input_string(UI *ui, const char *prompt, int flags, 233280304Sjkim char *result_buf, int minsize, int maxsize) 234280304Sjkim{ 235280304Sjkim return general_allocate_string(ui, prompt, 0, 236280304Sjkim UIT_PROMPT, flags, result_buf, minsize, 237280304Sjkim maxsize, NULL); 238280304Sjkim} 239109998Smarkm 240109998Smarkm/* Same as UI_add_input_string(), excepts it takes a copy of the prompt */ 241109998Smarkmint UI_dup_input_string(UI *ui, const char *prompt, int flags, 242280304Sjkim char *result_buf, int minsize, int maxsize) 243280304Sjkim{ 244280304Sjkim char *prompt_copy = NULL; 245109998Smarkm 246280304Sjkim if (prompt) { 247280304Sjkim prompt_copy = BUF_strdup(prompt); 248280304Sjkim if (prompt_copy == NULL) { 249280304Sjkim UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE); 250280304Sjkim return 0; 251280304Sjkim } 252280304Sjkim } 253109998Smarkm 254280304Sjkim return general_allocate_string(ui, prompt_copy, 1, 255280304Sjkim UIT_PROMPT, flags, result_buf, minsize, 256280304Sjkim maxsize, NULL); 257280304Sjkim} 258280304Sjkim 259109998Smarkmint UI_add_verify_string(UI *ui, const char *prompt, int flags, 260280304Sjkim char *result_buf, int minsize, int maxsize, 261280304Sjkim const char *test_buf) 262280304Sjkim{ 263280304Sjkim return general_allocate_string(ui, prompt, 0, 264280304Sjkim UIT_VERIFY, flags, result_buf, minsize, 265280304Sjkim maxsize, test_buf); 266280304Sjkim} 267109998Smarkm 268109998Smarkmint UI_dup_verify_string(UI *ui, const char *prompt, int flags, 269280304Sjkim char *result_buf, int minsize, int maxsize, 270280304Sjkim const char *test_buf) 271280304Sjkim{ 272280304Sjkim char *prompt_copy = NULL; 273109998Smarkm 274280304Sjkim if (prompt) { 275280304Sjkim prompt_copy = BUF_strdup(prompt); 276280304Sjkim if (prompt_copy == NULL) { 277280304Sjkim UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE); 278280304Sjkim return -1; 279280304Sjkim } 280280304Sjkim } 281109998Smarkm 282280304Sjkim return general_allocate_string(ui, prompt_copy, 1, 283280304Sjkim UIT_VERIFY, flags, result_buf, minsize, 284280304Sjkim maxsize, test_buf); 285280304Sjkim} 286280304Sjkim 287109998Smarkmint UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc, 288280304Sjkim const char *ok_chars, const char *cancel_chars, 289280304Sjkim int flags, char *result_buf) 290280304Sjkim{ 291280304Sjkim return general_allocate_boolean(ui, prompt, action_desc, 292280304Sjkim ok_chars, cancel_chars, 0, UIT_BOOLEAN, 293280304Sjkim flags, result_buf); 294280304Sjkim} 295109998Smarkm 296109998Smarkmint UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc, 297280304Sjkim const char *ok_chars, const char *cancel_chars, 298280304Sjkim int flags, char *result_buf) 299280304Sjkim{ 300280304Sjkim char *prompt_copy = NULL; 301280304Sjkim char *action_desc_copy = NULL; 302280304Sjkim char *ok_chars_copy = NULL; 303280304Sjkim char *cancel_chars_copy = NULL; 304109998Smarkm 305280304Sjkim if (prompt) { 306280304Sjkim prompt_copy = BUF_strdup(prompt); 307280304Sjkim if (prompt_copy == NULL) { 308280304Sjkim UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE); 309280304Sjkim goto err; 310280304Sjkim } 311280304Sjkim } 312280304Sjkim 313280304Sjkim if (action_desc) { 314280304Sjkim action_desc_copy = BUF_strdup(action_desc); 315280304Sjkim if (action_desc_copy == NULL) { 316280304Sjkim UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE); 317280304Sjkim goto err; 318280304Sjkim } 319280304Sjkim } 320280304Sjkim 321280304Sjkim if (ok_chars) { 322280304Sjkim ok_chars_copy = BUF_strdup(ok_chars); 323280304Sjkim if (ok_chars_copy == NULL) { 324280304Sjkim UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE); 325280304Sjkim goto err; 326280304Sjkim } 327280304Sjkim } 328280304Sjkim 329280304Sjkim if (cancel_chars) { 330280304Sjkim cancel_chars_copy = BUF_strdup(cancel_chars); 331280304Sjkim if (cancel_chars_copy == NULL) { 332280304Sjkim UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE); 333280304Sjkim goto err; 334280304Sjkim } 335280304Sjkim } 336280304Sjkim 337280304Sjkim return general_allocate_boolean(ui, prompt_copy, action_desc_copy, 338280304Sjkim ok_chars_copy, cancel_chars_copy, 1, 339280304Sjkim UIT_BOOLEAN, flags, result_buf); 340109998Smarkm err: 341280304Sjkim if (prompt_copy) 342280304Sjkim OPENSSL_free(prompt_copy); 343280304Sjkim if (action_desc_copy) 344280304Sjkim OPENSSL_free(action_desc_copy); 345280304Sjkim if (ok_chars_copy) 346280304Sjkim OPENSSL_free(ok_chars_copy); 347280304Sjkim if (cancel_chars_copy) 348280304Sjkim OPENSSL_free(cancel_chars_copy); 349280304Sjkim return -1; 350280304Sjkim} 351109998Smarkm 352109998Smarkmint UI_add_info_string(UI *ui, const char *text) 353280304Sjkim{ 354280304Sjkim return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0, 355280304Sjkim NULL); 356280304Sjkim} 357109998Smarkm 358109998Smarkmint UI_dup_info_string(UI *ui, const char *text) 359280304Sjkim{ 360280304Sjkim char *text_copy = NULL; 361109998Smarkm 362280304Sjkim if (text) { 363280304Sjkim text_copy = BUF_strdup(text); 364280304Sjkim if (text_copy == NULL) { 365280304Sjkim UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE); 366280304Sjkim return -1; 367280304Sjkim } 368280304Sjkim } 369109998Smarkm 370280304Sjkim return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL, 371280304Sjkim 0, 0, NULL); 372280304Sjkim} 373109998Smarkm 374109998Smarkmint UI_add_error_string(UI *ui, const char *text) 375280304Sjkim{ 376280304Sjkim return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0, 377280304Sjkim NULL); 378280304Sjkim} 379109998Smarkm 380109998Smarkmint UI_dup_error_string(UI *ui, const char *text) 381280304Sjkim{ 382280304Sjkim char *text_copy = NULL; 383109998Smarkm 384280304Sjkim if (text) { 385280304Sjkim text_copy = BUF_strdup(text); 386280304Sjkim if (text_copy == NULL) { 387280304Sjkim UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE); 388280304Sjkim return -1; 389280304Sjkim } 390280304Sjkim } 391280304Sjkim return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL, 392280304Sjkim 0, 0, NULL); 393280304Sjkim} 394109998Smarkm 395109998Smarkmchar *UI_construct_prompt(UI *ui, const char *object_desc, 396280304Sjkim const char *object_name) 397280304Sjkim{ 398280304Sjkim char *prompt = NULL; 399109998Smarkm 400280304Sjkim if (ui->meth->ui_construct_prompt) 401280304Sjkim prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name); 402280304Sjkim else { 403280304Sjkim char prompt1[] = "Enter "; 404280304Sjkim char prompt2[] = " for "; 405280304Sjkim char prompt3[] = ":"; 406280304Sjkim int len = 0; 407109998Smarkm 408280304Sjkim if (object_desc == NULL) 409280304Sjkim return NULL; 410280304Sjkim len = sizeof(prompt1) - 1 + strlen(object_desc); 411280304Sjkim if (object_name) 412280304Sjkim len += sizeof(prompt2) - 1 + strlen(object_name); 413280304Sjkim len += sizeof(prompt3) - 1; 414109998Smarkm 415280304Sjkim prompt = (char *)OPENSSL_malloc(len + 1); 416280304Sjkim BUF_strlcpy(prompt, prompt1, len + 1); 417280304Sjkim BUF_strlcat(prompt, object_desc, len + 1); 418280304Sjkim if (object_name) { 419280304Sjkim BUF_strlcat(prompt, prompt2, len + 1); 420280304Sjkim BUF_strlcat(prompt, object_name, len + 1); 421280304Sjkim } 422280304Sjkim BUF_strlcat(prompt, prompt3, len + 1); 423280304Sjkim } 424280304Sjkim return prompt; 425280304Sjkim} 426109998Smarkm 427109998Smarkmvoid *UI_add_user_data(UI *ui, void *user_data) 428280304Sjkim{ 429280304Sjkim void *old_data = ui->user_data; 430280304Sjkim ui->user_data = user_data; 431280304Sjkim return old_data; 432280304Sjkim} 433109998Smarkm 434109998Smarkmvoid *UI_get0_user_data(UI *ui) 435280304Sjkim{ 436280304Sjkim return ui->user_data; 437280304Sjkim} 438109998Smarkm 439109998Smarkmconst char *UI_get0_result(UI *ui, int i) 440280304Sjkim{ 441280304Sjkim if (i < 0) { 442280304Sjkim UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL); 443280304Sjkim return NULL; 444280304Sjkim } 445280304Sjkim if (i >= sk_UI_STRING_num(ui->strings)) { 446280304Sjkim UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE); 447280304Sjkim return NULL; 448280304Sjkim } 449280304Sjkim return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i)); 450280304Sjkim} 451109998Smarkm 452109998Smarkmstatic int print_error(const char *str, size_t len, UI *ui) 453280304Sjkim{ 454280304Sjkim UI_STRING uis; 455109998Smarkm 456280304Sjkim memset(&uis, 0, sizeof(uis)); 457280304Sjkim uis.type = UIT_ERROR; 458280304Sjkim uis.out_string = str; 459109998Smarkm 460280304Sjkim if (ui->meth->ui_write_string && !ui->meth->ui_write_string(ui, &uis)) 461280304Sjkim return -1; 462280304Sjkim return 0; 463280304Sjkim} 464109998Smarkm 465109998Smarkmint UI_process(UI *ui) 466280304Sjkim{ 467280304Sjkim int i, ok = 0; 468109998Smarkm 469280304Sjkim if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui)) 470280304Sjkim return -1; 471109998Smarkm 472280304Sjkim if (ui->flags & UI_FLAG_PRINT_ERRORS) 473280304Sjkim ERR_print_errors_cb((int (*)(const char *, size_t, void *)) 474280304Sjkim print_error, (void *)ui); 475109998Smarkm 476280304Sjkim for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) { 477280304Sjkim if (ui->meth->ui_write_string 478280304Sjkim && !ui->meth->ui_write_string(ui, 479280304Sjkim sk_UI_STRING_value(ui->strings, i))) 480280304Sjkim { 481280304Sjkim ok = -1; 482280304Sjkim goto err; 483280304Sjkim } 484280304Sjkim } 485109998Smarkm 486280304Sjkim if (ui->meth->ui_flush) 487280304Sjkim switch (ui->meth->ui_flush(ui)) { 488280304Sjkim case -1: /* Interrupt/Cancel/something... */ 489280304Sjkim ok = -2; 490280304Sjkim goto err; 491280304Sjkim case 0: /* Errors */ 492280304Sjkim ok = -1; 493280304Sjkim goto err; 494280304Sjkim default: /* Success */ 495280304Sjkim ok = 0; 496280304Sjkim break; 497280304Sjkim } 498109998Smarkm 499280304Sjkim for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) { 500280304Sjkim if (ui->meth->ui_read_string) { 501280304Sjkim switch (ui->meth->ui_read_string(ui, 502280304Sjkim sk_UI_STRING_value(ui->strings, 503280304Sjkim i))) { 504280304Sjkim case -1: /* Interrupt/Cancel/something... */ 505280304Sjkim ok = -2; 506280304Sjkim goto err; 507280304Sjkim case 0: /* Errors */ 508280304Sjkim ok = -1; 509280304Sjkim goto err; 510280304Sjkim default: /* Success */ 511280304Sjkim ok = 0; 512280304Sjkim break; 513280304Sjkim } 514280304Sjkim } 515280304Sjkim } 516109998Smarkm err: 517280304Sjkim if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui)) 518280304Sjkim return -1; 519280304Sjkim return ok; 520280304Sjkim} 521109998Smarkm 522280304Sjkimint UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void)) 523280304Sjkim{ 524280304Sjkim if (ui == NULL) { 525280304Sjkim UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER); 526280304Sjkim return -1; 527280304Sjkim } 528280304Sjkim switch (cmd) { 529280304Sjkim case UI_CTRL_PRINT_ERRORS: 530109998Smarkm { 531280304Sjkim int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS); 532280304Sjkim if (i) 533280304Sjkim ui->flags |= UI_FLAG_PRINT_ERRORS; 534280304Sjkim else 535280304Sjkim ui->flags &= ~UI_FLAG_PRINT_ERRORS; 536280304Sjkim return save_flag; 537109998Smarkm } 538280304Sjkim case UI_CTRL_IS_REDOABLE: 539280304Sjkim return ! !(ui->flags & UI_FLAG_REDOABLE); 540280304Sjkim default: 541280304Sjkim break; 542280304Sjkim } 543280304Sjkim UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND); 544280304Sjkim return -1; 545280304Sjkim} 546109998Smarkm 547280304Sjkimint UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 548280304Sjkim CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 549280304Sjkim{ 550280304Sjkim return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp, 551280304Sjkim new_func, dup_func, free_func); 552280304Sjkim} 553280304Sjkim 554109998Smarkmint UI_set_ex_data(UI *r, int idx, void *arg) 555280304Sjkim{ 556280304Sjkim return (CRYPTO_set_ex_data(&r->ex_data, idx, arg)); 557280304Sjkim} 558109998Smarkm 559109998Smarkmvoid *UI_get_ex_data(UI *r, int idx) 560280304Sjkim{ 561280304Sjkim return (CRYPTO_get_ex_data(&r->ex_data, idx)); 562280304Sjkim} 563109998Smarkm 564109998Smarkmvoid UI_set_default_method(const UI_METHOD *meth) 565280304Sjkim{ 566280304Sjkim default_UI_meth = meth; 567280304Sjkim} 568109998Smarkm 569109998Smarkmconst UI_METHOD *UI_get_default_method(void) 570280304Sjkim{ 571280304Sjkim if (default_UI_meth == NULL) { 572280304Sjkim default_UI_meth = UI_OpenSSL(); 573280304Sjkim } 574280304Sjkim return default_UI_meth; 575280304Sjkim} 576109998Smarkm 577109998Smarkmconst UI_METHOD *UI_get_method(UI *ui) 578280304Sjkim{ 579280304Sjkim return ui->meth; 580280304Sjkim} 581109998Smarkm 582109998Smarkmconst UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth) 583280304Sjkim{ 584280304Sjkim ui->meth = meth; 585280304Sjkim return ui->meth; 586280304Sjkim} 587109998Smarkm 588109998SmarkmUI_METHOD *UI_create_method(char *name) 589280304Sjkim{ 590280304Sjkim UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD)); 591109998Smarkm 592280304Sjkim if (ui_method) { 593280304Sjkim memset(ui_method, 0, sizeof(*ui_method)); 594280304Sjkim ui_method->name = BUF_strdup(name); 595280304Sjkim } 596280304Sjkim return ui_method; 597280304Sjkim} 598109998Smarkm 599280304Sjkim/* 600280304Sjkim * BIG FSCKING WARNING!!!! If you use this on a statically allocated method 601280304Sjkim * (that is, it hasn't been allocated using UI_create_method(), you deserve 602280304Sjkim * anything Murphy can throw at you and more! You have been warned. 603280304Sjkim */ 604109998Smarkmvoid UI_destroy_method(UI_METHOD *ui_method) 605280304Sjkim{ 606280304Sjkim OPENSSL_free(ui_method->name); 607280304Sjkim ui_method->name = NULL; 608280304Sjkim OPENSSL_free(ui_method); 609280304Sjkim} 610109998Smarkm 611280304Sjkimint UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui)) 612280304Sjkim{ 613280304Sjkim if (method) { 614280304Sjkim method->ui_open_session = opener; 615280304Sjkim return 0; 616280304Sjkim } else 617280304Sjkim return -1; 618280304Sjkim} 619109998Smarkm 620280304Sjkimint UI_method_set_writer(UI_METHOD *method, 621280304Sjkim int (*writer) (UI *ui, UI_STRING *uis)) 622280304Sjkim{ 623280304Sjkim if (method) { 624280304Sjkim method->ui_write_string = writer; 625280304Sjkim return 0; 626280304Sjkim } else 627280304Sjkim return -1; 628280304Sjkim} 629109998Smarkm 630280304Sjkimint UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui)) 631280304Sjkim{ 632280304Sjkim if (method) { 633280304Sjkim method->ui_flush = flusher; 634280304Sjkim return 0; 635280304Sjkim } else 636280304Sjkim return -1; 637280304Sjkim} 638109998Smarkm 639280304Sjkimint UI_method_set_reader(UI_METHOD *method, 640280304Sjkim int (*reader) (UI *ui, UI_STRING *uis)) 641280304Sjkim{ 642280304Sjkim if (method) { 643280304Sjkim method->ui_read_string = reader; 644280304Sjkim return 0; 645280304Sjkim } else 646280304Sjkim return -1; 647280304Sjkim} 648109998Smarkm 649280304Sjkimint UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui)) 650280304Sjkim{ 651280304Sjkim if (method) { 652280304Sjkim method->ui_close_session = closer; 653280304Sjkim return 0; 654280304Sjkim } else 655280304Sjkim return -1; 656280304Sjkim} 657109998Smarkm 658280304Sjkimint UI_method_set_prompt_constructor(UI_METHOD *method, 659280304Sjkim char *(*prompt_constructor) (UI *ui, 660280304Sjkim const char 661280304Sjkim *object_desc, 662280304Sjkim const char 663280304Sjkim *object_name)) 664280304Sjkim{ 665280304Sjkim if (method) { 666280304Sjkim method->ui_construct_prompt = prompt_constructor; 667280304Sjkim return 0; 668280304Sjkim } else 669280304Sjkim return -1; 670280304Sjkim} 671238405Sjkim 672280304Sjkimint (*UI_method_get_opener(UI_METHOD *method)) (UI *) { 673280304Sjkim if (method) 674280304Sjkim return method->ui_open_session; 675280304Sjkim else 676280304Sjkim return NULL; 677280304Sjkim} 678109998Smarkm 679280304Sjkimint (*UI_method_get_writer(UI_METHOD *method)) (UI *, UI_STRING *) { 680280304Sjkim if (method) 681280304Sjkim return method->ui_write_string; 682280304Sjkim else 683280304Sjkim return NULL; 684280304Sjkim} 685109998Smarkm 686280304Sjkimint (*UI_method_get_flusher(UI_METHOD *method)) (UI *) { 687280304Sjkim if (method) 688280304Sjkim return method->ui_flush; 689280304Sjkim else 690280304Sjkim return NULL; 691280304Sjkim} 692109998Smarkm 693280304Sjkimint (*UI_method_get_reader(UI_METHOD *method)) (UI *, UI_STRING *) { 694280304Sjkim if (method) 695280304Sjkim return method->ui_read_string; 696280304Sjkim else 697280304Sjkim return NULL; 698280304Sjkim} 699109998Smarkm 700280304Sjkimint (*UI_method_get_closer(UI_METHOD *method)) (UI *) { 701280304Sjkim if (method) 702280304Sjkim return method->ui_close_session; 703280304Sjkim else 704280304Sjkim return NULL; 705280304Sjkim} 706109998Smarkm 707280304Sjkimchar *(*UI_method_get_prompt_constructor(UI_METHOD *method)) (UI *, 708280304Sjkim const char *, 709280304Sjkim const char *) { 710280304Sjkim if (method) 711280304Sjkim return method->ui_construct_prompt; 712280304Sjkim else 713280304Sjkim return NULL; 714280304Sjkim} 715238405Sjkim 716109998Smarkmenum UI_string_types UI_get_string_type(UI_STRING *uis) 717280304Sjkim{ 718280304Sjkim if (!uis) 719280304Sjkim return UIT_NONE; 720280304Sjkim return uis->type; 721280304Sjkim} 722109998Smarkm 723109998Smarkmint UI_get_input_flags(UI_STRING *uis) 724280304Sjkim{ 725280304Sjkim if (!uis) 726280304Sjkim return 0; 727280304Sjkim return uis->input_flags; 728280304Sjkim} 729109998Smarkm 730109998Smarkmconst char *UI_get0_output_string(UI_STRING *uis) 731280304Sjkim{ 732280304Sjkim if (!uis) 733280304Sjkim return NULL; 734280304Sjkim return uis->out_string; 735280304Sjkim} 736109998Smarkm 737109998Smarkmconst char *UI_get0_action_string(UI_STRING *uis) 738280304Sjkim{ 739280304Sjkim if (!uis) 740280304Sjkim return NULL; 741280304Sjkim switch (uis->type) { 742280304Sjkim case UIT_PROMPT: 743280304Sjkim case UIT_BOOLEAN: 744280304Sjkim return uis->_.boolean_data.action_desc; 745280304Sjkim default: 746280304Sjkim return NULL; 747280304Sjkim } 748280304Sjkim} 749109998Smarkm 750109998Smarkmconst char *UI_get0_result_string(UI_STRING *uis) 751280304Sjkim{ 752280304Sjkim if (!uis) 753280304Sjkim return NULL; 754280304Sjkim switch (uis->type) { 755280304Sjkim case UIT_PROMPT: 756280304Sjkim case UIT_VERIFY: 757280304Sjkim return uis->result_buf; 758280304Sjkim default: 759280304Sjkim return NULL; 760280304Sjkim } 761280304Sjkim} 762109998Smarkm 763109998Smarkmconst char *UI_get0_test_string(UI_STRING *uis) 764280304Sjkim{ 765280304Sjkim if (!uis) 766280304Sjkim return NULL; 767280304Sjkim switch (uis->type) { 768280304Sjkim case UIT_VERIFY: 769280304Sjkim return uis->_.string_data.test_buf; 770280304Sjkim default: 771280304Sjkim return NULL; 772280304Sjkim } 773280304Sjkim} 774109998Smarkm 775109998Smarkmint UI_get_result_minsize(UI_STRING *uis) 776280304Sjkim{ 777280304Sjkim if (!uis) 778280304Sjkim return -1; 779280304Sjkim switch (uis->type) { 780280304Sjkim case UIT_PROMPT: 781280304Sjkim case UIT_VERIFY: 782280304Sjkim return uis->_.string_data.result_minsize; 783280304Sjkim default: 784280304Sjkim return -1; 785280304Sjkim } 786280304Sjkim} 787109998Smarkm 788109998Smarkmint UI_get_result_maxsize(UI_STRING *uis) 789280304Sjkim{ 790280304Sjkim if (!uis) 791280304Sjkim return -1; 792280304Sjkim switch (uis->type) { 793280304Sjkim case UIT_PROMPT: 794280304Sjkim case UIT_VERIFY: 795280304Sjkim return uis->_.string_data.result_maxsize; 796280304Sjkim default: 797280304Sjkim return -1; 798280304Sjkim } 799280304Sjkim} 800109998Smarkm 801109998Smarkmint UI_set_result(UI *ui, UI_STRING *uis, const char *result) 802280304Sjkim{ 803280304Sjkim int l = strlen(result); 804109998Smarkm 805280304Sjkim ui->flags &= ~UI_FLAG_REDOABLE; 806109998Smarkm 807280304Sjkim if (!uis) 808280304Sjkim return -1; 809280304Sjkim switch (uis->type) { 810280304Sjkim case UIT_PROMPT: 811280304Sjkim case UIT_VERIFY: 812280304Sjkim { 813280304Sjkim char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1]; 814280304Sjkim char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1]; 815109998Smarkm 816280304Sjkim BIO_snprintf(number1, sizeof(number1), "%d", 817280304Sjkim uis->_.string_data.result_minsize); 818280304Sjkim BIO_snprintf(number2, sizeof(number2), "%d", 819280304Sjkim uis->_.string_data.result_maxsize); 820109998Smarkm 821280304Sjkim if (l < uis->_.string_data.result_minsize) { 822280304Sjkim ui->flags |= UI_FLAG_REDOABLE; 823280304Sjkim UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL); 824280304Sjkim ERR_add_error_data(5, "You must type in ", 825280304Sjkim number1, " to ", number2, " characters"); 826280304Sjkim return -1; 827280304Sjkim } 828280304Sjkim if (l > uis->_.string_data.result_maxsize) { 829280304Sjkim ui->flags |= UI_FLAG_REDOABLE; 830280304Sjkim UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE); 831280304Sjkim ERR_add_error_data(5, "You must type in ", 832280304Sjkim number1, " to ", number2, " characters"); 833280304Sjkim return -1; 834280304Sjkim } 835280304Sjkim } 836109998Smarkm 837280304Sjkim if (!uis->result_buf) { 838280304Sjkim UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER); 839280304Sjkim return -1; 840280304Sjkim } 841109998Smarkm 842280304Sjkim BUF_strlcpy(uis->result_buf, result, 843280304Sjkim uis->_.string_data.result_maxsize + 1); 844280304Sjkim break; 845280304Sjkim case UIT_BOOLEAN: 846280304Sjkim { 847280304Sjkim const char *p; 848109998Smarkm 849280304Sjkim if (!uis->result_buf) { 850280304Sjkim UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER); 851280304Sjkim return -1; 852280304Sjkim } 853109998Smarkm 854280304Sjkim uis->result_buf[0] = '\0'; 855280304Sjkim for (p = result; *p; p++) { 856280304Sjkim if (strchr(uis->_.boolean_data.ok_chars, *p)) { 857280304Sjkim uis->result_buf[0] = uis->_.boolean_data.ok_chars[0]; 858280304Sjkim break; 859280304Sjkim } 860280304Sjkim if (strchr(uis->_.boolean_data.cancel_chars, *p)) { 861280304Sjkim uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0]; 862280304Sjkim break; 863280304Sjkim } 864280304Sjkim } 865280304Sjkim } 866280304Sjkim default: 867280304Sjkim break; 868280304Sjkim } 869280304Sjkim return 0; 870280304Sjkim} 871