ui_lib.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /* crypto/ui/ui_lib.c */
  2. /*
  3. * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
  4. * 2001.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * openssl-core@openssl.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <string.h>
  60. #include "cryptlib.h"
  61. #include <openssl/e_os2.h>
  62. #include <openssl/buffer.h>
  63. #include <openssl/ui.h>
  64. #include <openssl/err.h>
  65. #include "ui_locl.h"
  66. IMPLEMENT_STACK_OF(UI_STRING_ST)
  67. static const UI_METHOD *default_UI_meth = NULL;
  68. UI *UI_new(void)
  69. {
  70. return (UI_new_method(NULL));
  71. }
  72. UI *UI_new_method(const UI_METHOD *method)
  73. {
  74. UI *ret;
  75. ret = (UI *)OPENSSL_malloc(sizeof(UI));
  76. if (ret == NULL) {
  77. UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  78. return NULL;
  79. }
  80. if (method == NULL)
  81. ret->meth = UI_get_default_method();
  82. else
  83. ret->meth = method;
  84. ret->strings = NULL;
  85. ret->user_data = NULL;
  86. ret->flags = 0;
  87. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data);
  88. return ret;
  89. }
  90. static void free_string(UI_STRING *uis)
  91. {
  92. if (uis->flags & OUT_STRING_FREEABLE) {
  93. OPENSSL_free((char *)uis->out_string);
  94. switch (uis->type) {
  95. case UIT_BOOLEAN:
  96. OPENSSL_free((char *)uis->_.boolean_data.action_desc);
  97. OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
  98. OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. OPENSSL_free(uis);
  105. }
  106. void UI_free(UI *ui)
  107. {
  108. if (ui == NULL)
  109. return;
  110. sk_UI_STRING_pop_free(ui->strings, free_string);
  111. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
  112. OPENSSL_free(ui);
  113. }
  114. static int allocate_string_stack(UI *ui)
  115. {
  116. if (ui->strings == NULL) {
  117. ui->strings = sk_UI_STRING_new_null();
  118. if (ui->strings == NULL) {
  119. return -1;
  120. }
  121. }
  122. return 0;
  123. }
  124. static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
  125. int prompt_freeable,
  126. enum UI_string_types type,
  127. int input_flags, char *result_buf)
  128. {
  129. UI_STRING *ret = NULL;
  130. if (prompt == NULL) {
  131. UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
  132. } else if ((type == UIT_PROMPT || type == UIT_VERIFY
  133. || type == UIT_BOOLEAN) && result_buf == NULL) {
  134. UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
  135. } else if ((ret = (UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING)))) {
  136. ret->out_string = prompt;
  137. ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
  138. ret->input_flags = input_flags;
  139. ret->type = type;
  140. ret->result_buf = result_buf;
  141. }
  142. return ret;
  143. }
  144. static int general_allocate_string(UI *ui, const char *prompt,
  145. int prompt_freeable,
  146. enum UI_string_types type, int input_flags,
  147. char *result_buf, int minsize, int maxsize,
  148. const char *test_buf)
  149. {
  150. int ret = -1;
  151. UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
  152. type, input_flags, result_buf);
  153. if (s != NULL) {
  154. if (allocate_string_stack(ui) >= 0) {
  155. s->_.string_data.result_minsize = minsize;
  156. s->_.string_data.result_maxsize = maxsize;
  157. s->_.string_data.test_buf = test_buf;
  158. ret = sk_UI_STRING_push(ui->strings, s);
  159. /* sk_push() returns 0 on error. Let's addapt that */
  160. if (ret <= 0)
  161. ret--;
  162. } else
  163. free_string(s);
  164. }
  165. return ret;
  166. }
  167. static int general_allocate_boolean(UI *ui,
  168. const char *prompt,
  169. const char *action_desc,
  170. const char *ok_chars,
  171. const char *cancel_chars,
  172. int prompt_freeable,
  173. enum UI_string_types type,
  174. int input_flags, char *result_buf)
  175. {
  176. int ret = -1;
  177. UI_STRING *s;
  178. const char *p;
  179. if (ok_chars == NULL) {
  180. UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
  181. } else if (cancel_chars == NULL) {
  182. UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
  183. } else {
  184. for (p = ok_chars; *p != '\0'; p++) {
  185. if (strchr(cancel_chars, *p) != NULL) {
  186. UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
  187. UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
  188. }
  189. }
  190. s = general_allocate_prompt(ui, prompt, prompt_freeable,
  191. type, input_flags, result_buf);
  192. if (s != NULL) {
  193. if (allocate_string_stack(ui) >= 0) {
  194. s->_.boolean_data.action_desc = action_desc;
  195. s->_.boolean_data.ok_chars = ok_chars;
  196. s->_.boolean_data.cancel_chars = cancel_chars;
  197. ret = sk_UI_STRING_push(ui->strings, s);
  198. /*
  199. * sk_push() returns 0 on error. Let's addapt that
  200. */
  201. if (ret <= 0)
  202. ret--;
  203. } else
  204. free_string(s);
  205. }
  206. }
  207. return ret;
  208. }
  209. /*
  210. * Returns the index to the place in the stack or -1 for error. Uses a
  211. * direct reference to the prompt.
  212. */
  213. int UI_add_input_string(UI *ui, const char *prompt, int flags,
  214. char *result_buf, int minsize, int maxsize)
  215. {
  216. return general_allocate_string(ui, prompt, 0,
  217. UIT_PROMPT, flags, result_buf, minsize,
  218. maxsize, NULL);
  219. }
  220. /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
  221. int UI_dup_input_string(UI *ui, const char *prompt, int flags,
  222. char *result_buf, int minsize, int maxsize)
  223. {
  224. char *prompt_copy = NULL;
  225. if (prompt != NULL) {
  226. prompt_copy = BUF_strdup(prompt);
  227. if (prompt_copy == NULL) {
  228. UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
  229. return 0;
  230. }
  231. }
  232. return general_allocate_string(ui, prompt_copy, 1,
  233. UIT_PROMPT, flags, result_buf, minsize,
  234. maxsize, NULL);
  235. }
  236. int UI_add_verify_string(UI *ui, const char *prompt, int flags,
  237. char *result_buf, int minsize, int maxsize,
  238. const char *test_buf)
  239. {
  240. return general_allocate_string(ui, prompt, 0,
  241. UIT_VERIFY, flags, result_buf, minsize,
  242. maxsize, test_buf);
  243. }
  244. int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
  245. char *result_buf, int minsize, int maxsize,
  246. const char *test_buf)
  247. {
  248. char *prompt_copy = NULL;
  249. if (prompt != NULL) {
  250. prompt_copy = BUF_strdup(prompt);
  251. if (prompt_copy == NULL) {
  252. UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
  253. return -1;
  254. }
  255. }
  256. return general_allocate_string(ui, prompt_copy, 1,
  257. UIT_VERIFY, flags, result_buf, minsize,
  258. maxsize, test_buf);
  259. }
  260. int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
  261. const char *ok_chars, const char *cancel_chars,
  262. int flags, char *result_buf)
  263. {
  264. return general_allocate_boolean(ui, prompt, action_desc,
  265. ok_chars, cancel_chars, 0, UIT_BOOLEAN,
  266. flags, result_buf);
  267. }
  268. int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
  269. const char *ok_chars, const char *cancel_chars,
  270. int flags, char *result_buf)
  271. {
  272. char *prompt_copy = NULL;
  273. char *action_desc_copy = NULL;
  274. char *ok_chars_copy = NULL;
  275. char *cancel_chars_copy = NULL;
  276. if (prompt != NULL) {
  277. prompt_copy = BUF_strdup(prompt);
  278. if (prompt_copy == NULL) {
  279. UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
  280. goto err;
  281. }
  282. }
  283. if (action_desc != NULL) {
  284. action_desc_copy = BUF_strdup(action_desc);
  285. if (action_desc_copy == NULL) {
  286. UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
  287. goto err;
  288. }
  289. }
  290. if (ok_chars != NULL) {
  291. ok_chars_copy = BUF_strdup(ok_chars);
  292. if (ok_chars_copy == NULL) {
  293. UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
  294. goto err;
  295. }
  296. }
  297. if (cancel_chars != NULL) {
  298. cancel_chars_copy = BUF_strdup(cancel_chars);
  299. if (cancel_chars_copy == NULL) {
  300. UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
  301. goto err;
  302. }
  303. }
  304. return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
  305. ok_chars_copy, cancel_chars_copy, 1,
  306. UIT_BOOLEAN, flags, result_buf);
  307. err:
  308. if (prompt_copy)
  309. OPENSSL_free(prompt_copy);
  310. if (action_desc_copy)
  311. OPENSSL_free(action_desc_copy);
  312. if (ok_chars_copy)
  313. OPENSSL_free(ok_chars_copy);
  314. if (cancel_chars_copy)
  315. OPENSSL_free(cancel_chars_copy);
  316. return -1;
  317. }
  318. int UI_add_info_string(UI *ui, const char *text)
  319. {
  320. return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
  321. NULL);
  322. }
  323. int UI_dup_info_string(UI *ui, const char *text)
  324. {
  325. char *text_copy = NULL;
  326. if (text != NULL) {
  327. text_copy = BUF_strdup(text);
  328. if (text_copy == NULL) {
  329. UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
  330. return -1;
  331. }
  332. }
  333. return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
  334. 0, 0, NULL);
  335. }
  336. int UI_add_error_string(UI *ui, const char *text)
  337. {
  338. return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
  339. NULL);
  340. }
  341. int UI_dup_error_string(UI *ui, const char *text)
  342. {
  343. char *text_copy = NULL;
  344. if (text != NULL) {
  345. text_copy = BUF_strdup(text);
  346. if (text_copy == NULL) {
  347. UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
  348. return -1;
  349. }
  350. }
  351. return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
  352. 0, 0, NULL);
  353. }
  354. char *UI_construct_prompt(UI *ui, const char *object_desc,
  355. const char *object_name)
  356. {
  357. char *prompt = NULL;
  358. if (ui->meth->ui_construct_prompt != NULL)
  359. prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
  360. else {
  361. char prompt1[] = "Enter ";
  362. char prompt2[] = " for ";
  363. char prompt3[] = ":";
  364. int len = 0;
  365. if (object_desc == NULL)
  366. return NULL;
  367. len = sizeof(prompt1) - 1 + strlen(object_desc);
  368. if (object_name != NULL)
  369. len += sizeof(prompt2) - 1 + strlen(object_name);
  370. len += sizeof(prompt3) - 1;
  371. prompt = (char *)OPENSSL_malloc(len + 1);
  372. if (prompt == NULL)
  373. return NULL;
  374. BUF_strlcpy(prompt, prompt1, len + 1);
  375. BUF_strlcat(prompt, object_desc, len + 1);
  376. if (object_name != NULL) {
  377. BUF_strlcat(prompt, prompt2, len + 1);
  378. BUF_strlcat(prompt, object_name, len + 1);
  379. }
  380. BUF_strlcat(prompt, prompt3, len + 1);
  381. }
  382. return prompt;
  383. }
  384. void *UI_add_user_data(UI *ui, void *user_data)
  385. {
  386. void *old_data = ui->user_data;
  387. ui->user_data = user_data;
  388. return old_data;
  389. }
  390. void *UI_get0_user_data(UI *ui)
  391. {
  392. return ui->user_data;
  393. }
  394. const char *UI_get0_result(UI *ui, int i)
  395. {
  396. if (i < 0) {
  397. UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL);
  398. return NULL;
  399. }
  400. if (i >= sk_UI_STRING_num(ui->strings)) {
  401. UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE);
  402. return NULL;
  403. }
  404. return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
  405. }
  406. static int print_error(const char *str, size_t len, UI *ui)
  407. {
  408. UI_STRING uis;
  409. memset(&uis, 0, sizeof(uis));
  410. uis.type = UIT_ERROR;
  411. uis.out_string = str;
  412. if (ui->meth->ui_write_string != NULL
  413. && ui->meth->ui_write_string(ui, &uis) <= 0)
  414. return -1;
  415. return 0;
  416. }
  417. int UI_process(UI *ui)
  418. {
  419. int i, ok = 0;
  420. if (ui->meth->ui_open_session != NULL
  421. && ui->meth->ui_open_session(ui) <= 0) {
  422. ok = -1;
  423. goto err;
  424. }
  425. if (ui->flags & UI_FLAG_PRINT_ERRORS)
  426. ERR_print_errors_cb((int (*)(const char *, size_t, void *))
  427. print_error, (void *)ui);
  428. for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
  429. if (ui->meth->ui_write_string != NULL
  430. && (ui->meth->ui_write_string(ui,
  431. sk_UI_STRING_value(ui->strings, i))
  432. <= 0))
  433. {
  434. ok = -1;
  435. goto err;
  436. }
  437. }
  438. if (ui->meth->ui_flush != NULL)
  439. switch (ui->meth->ui_flush(ui)) {
  440. case -1: /* Interrupt/Cancel/something... */
  441. ok = -2;
  442. goto err;
  443. case 0: /* Errors */
  444. ok = -1;
  445. goto err;
  446. default: /* Success */
  447. ok = 0;
  448. break;
  449. }
  450. for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
  451. if (ui->meth->ui_read_string != NULL) {
  452. switch (ui->meth->ui_read_string(ui,
  453. sk_UI_STRING_value(ui->strings,
  454. i))) {
  455. case -1: /* Interrupt/Cancel/something... */
  456. ok = -2;
  457. goto err;
  458. case 0: /* Errors */
  459. ok = -1;
  460. goto err;
  461. default: /* Success */
  462. ok = 0;
  463. break;
  464. }
  465. }
  466. }
  467. err:
  468. if (ui->meth->ui_close_session != NULL
  469. && ui->meth->ui_close_session(ui) <= 0)
  470. return -1;
  471. return ok;
  472. }
  473. int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
  474. {
  475. if (ui == NULL) {
  476. UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  477. return -1;
  478. }
  479. switch (cmd) {
  480. case UI_CTRL_PRINT_ERRORS:
  481. {
  482. int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS);
  483. if (i)
  484. ui->flags |= UI_FLAG_PRINT_ERRORS;
  485. else
  486. ui->flags &= ~UI_FLAG_PRINT_ERRORS;
  487. return save_flag;
  488. }
  489. case UI_CTRL_IS_REDOABLE:
  490. return ! !(ui->flags & UI_FLAG_REDOABLE);
  491. default:
  492. break;
  493. }
  494. UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
  495. return -1;
  496. }
  497. int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  498. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  499. {
  500. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp,
  501. new_func, dup_func, free_func);
  502. }
  503. int UI_set_ex_data(UI *r, int idx, void *arg)
  504. {
  505. return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
  506. }
  507. void *UI_get_ex_data(UI *r, int idx)
  508. {
  509. return (CRYPTO_get_ex_data(&r->ex_data, idx));
  510. }
  511. void UI_set_default_method(const UI_METHOD *meth)
  512. {
  513. default_UI_meth = meth;
  514. }
  515. const UI_METHOD *UI_get_default_method(void)
  516. {
  517. if (default_UI_meth == NULL) {
  518. default_UI_meth = UI_OpenSSL();
  519. }
  520. return default_UI_meth;
  521. }
  522. const UI_METHOD *UI_get_method(UI *ui)
  523. {
  524. return ui->meth;
  525. }
  526. const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
  527. {
  528. ui->meth = meth;
  529. return ui->meth;
  530. }
  531. UI_METHOD *UI_create_method(char *name)
  532. {
  533. UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
  534. if (ui_method) {
  535. memset(ui_method, 0, sizeof(*ui_method));
  536. ui_method->name = BUF_strdup(name);
  537. }
  538. return ui_method;
  539. }
  540. /*
  541. * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
  542. * (that is, it hasn't been allocated using UI_create_method(), you deserve
  543. * anything Murphy can throw at you and more! You have been warned.
  544. */
  545. void UI_destroy_method(UI_METHOD *ui_method)
  546. {
  547. OPENSSL_free(ui_method->name);
  548. ui_method->name = NULL;
  549. OPENSSL_free(ui_method);
  550. }
  551. int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui))
  552. {
  553. if (method != NULL) {
  554. method->ui_open_session = opener;
  555. return 0;
  556. }
  557. return -1;
  558. }
  559. int UI_method_set_writer(UI_METHOD *method,
  560. int (*writer) (UI *ui, UI_STRING *uis))
  561. {
  562. if (method != NULL) {
  563. method->ui_write_string = writer;
  564. return 0;
  565. }
  566. return -1;
  567. }
  568. int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
  569. {
  570. if (method != NULL) {
  571. method->ui_flush = flusher;
  572. return 0;
  573. }
  574. return -1;
  575. }
  576. int UI_method_set_reader(UI_METHOD *method,
  577. int (*reader) (UI *ui, UI_STRING *uis))
  578. {
  579. if (method != NULL) {
  580. method->ui_read_string = reader;
  581. return 0;
  582. }
  583. return -1;
  584. }
  585. int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
  586. {
  587. if (method != NULL) {
  588. method->ui_close_session = closer;
  589. return 0;
  590. }
  591. return -1;
  592. }
  593. int UI_method_set_prompt_constructor(UI_METHOD *method,
  594. char *(*prompt_constructor) (UI *ui,
  595. const char
  596. *object_desc,
  597. const char
  598. *object_name))
  599. {
  600. if (method != NULL) {
  601. method->ui_construct_prompt = prompt_constructor;
  602. return 0;
  603. }
  604. return -1;
  605. }
  606. int (*UI_method_get_opener(UI_METHOD *method)) (UI *)
  607. {
  608. if (method != NULL)
  609. return method->ui_open_session;
  610. return NULL;
  611. }
  612. int (*UI_method_get_writer(UI_METHOD *method)) (UI *, UI_STRING *)
  613. {
  614. if (method != NULL)
  615. return method->ui_write_string;
  616. return NULL;
  617. }
  618. int (*UI_method_get_flusher(UI_METHOD *method)) (UI *)
  619. {
  620. if (method != NULL)
  621. return method->ui_flush;
  622. return NULL;
  623. }
  624. int (*UI_method_get_reader(UI_METHOD *method)) (UI *, UI_STRING *)
  625. {
  626. if (method != NULL)
  627. return method->ui_read_string;
  628. return NULL;
  629. }
  630. int (*UI_method_get_closer(UI_METHOD *method)) (UI *)
  631. {
  632. if (method != NULL)
  633. return method->ui_close_session;
  634. return NULL;
  635. }
  636. char *(*UI_method_get_prompt_constructor(UI_METHOD *method)) (UI *,
  637. const char *,
  638. const char *)
  639. {
  640. if (method != NULL)
  641. return method->ui_construct_prompt;
  642. return NULL;
  643. }
  644. enum UI_string_types UI_get_string_type(UI_STRING *uis)
  645. {
  646. if (!uis)
  647. return UIT_NONE;
  648. return uis->type;
  649. }
  650. int UI_get_input_flags(UI_STRING *uis)
  651. {
  652. if (!uis)
  653. return 0;
  654. return uis->input_flags;
  655. }
  656. const char *UI_get0_output_string(UI_STRING *uis)
  657. {
  658. if (!uis)
  659. return NULL;
  660. return uis->out_string;
  661. }
  662. const char *UI_get0_action_string(UI_STRING *uis)
  663. {
  664. if (!uis)
  665. return NULL;
  666. switch (uis->type) {
  667. case UIT_BOOLEAN:
  668. return uis->_.boolean_data.action_desc;
  669. default:
  670. return NULL;
  671. }
  672. }
  673. const char *UI_get0_result_string(UI_STRING *uis)
  674. {
  675. if (!uis)
  676. return NULL;
  677. switch (uis->type) {
  678. case UIT_PROMPT:
  679. case UIT_VERIFY:
  680. return uis->result_buf;
  681. default:
  682. return NULL;
  683. }
  684. }
  685. const char *UI_get0_test_string(UI_STRING *uis)
  686. {
  687. if (!uis)
  688. return NULL;
  689. switch (uis->type) {
  690. case UIT_VERIFY:
  691. return uis->_.string_data.test_buf;
  692. default:
  693. return NULL;
  694. }
  695. }
  696. int UI_get_result_minsize(UI_STRING *uis)
  697. {
  698. if (!uis)
  699. return -1;
  700. switch (uis->type) {
  701. case UIT_PROMPT:
  702. case UIT_VERIFY:
  703. return uis->_.string_data.result_minsize;
  704. default:
  705. return -1;
  706. }
  707. }
  708. int UI_get_result_maxsize(UI_STRING *uis)
  709. {
  710. if (!uis)
  711. return -1;
  712. switch (uis->type) {
  713. case UIT_PROMPT:
  714. case UIT_VERIFY:
  715. return uis->_.string_data.result_maxsize;
  716. default:
  717. return -1;
  718. }
  719. }
  720. int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
  721. {
  722. int l = strlen(result);
  723. ui->flags &= ~UI_FLAG_REDOABLE;
  724. if (!uis)
  725. return -1;
  726. switch (uis->type) {
  727. case UIT_PROMPT:
  728. case UIT_VERIFY:
  729. {
  730. char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
  731. char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
  732. BIO_snprintf(number1, sizeof(number1), "%d",
  733. uis->_.string_data.result_minsize);
  734. BIO_snprintf(number2, sizeof(number2), "%d",
  735. uis->_.string_data.result_maxsize);
  736. if (l < uis->_.string_data.result_minsize) {
  737. ui->flags |= UI_FLAG_REDOABLE;
  738. UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL);
  739. ERR_add_error_data(5, "You must type in ",
  740. number1, " to ", number2, " characters");
  741. return -1;
  742. }
  743. if (l > uis->_.string_data.result_maxsize) {
  744. ui->flags |= UI_FLAG_REDOABLE;
  745. UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE);
  746. ERR_add_error_data(5, "You must type in ",
  747. number1, " to ", number2, " characters");
  748. return -1;
  749. }
  750. }
  751. if (!uis->result_buf) {
  752. UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
  753. return -1;
  754. }
  755. BUF_strlcpy(uis->result_buf, result,
  756. uis->_.string_data.result_maxsize + 1);
  757. break;
  758. case UIT_BOOLEAN:
  759. {
  760. const char *p;
  761. if (!uis->result_buf) {
  762. UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
  763. return -1;
  764. }
  765. uis->result_buf[0] = '\0';
  766. for (p = result; *p; p++) {
  767. if (strchr(uis->_.boolean_data.ok_chars, *p)) {
  768. uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
  769. break;
  770. }
  771. if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
  772. uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];
  773. break;
  774. }
  775. }
  776. }
  777. default:
  778. break;
  779. }
  780. return 0;
  781. }