ui_lib.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/e_os2.h>
  12. #include <openssl/buffer.h>
  13. #include <openssl/ui.h>
  14. #include <openssl/err.h>
  15. #include "ui_local.h"
  16. DEFINE_STACK_OF(UI_STRING)
  17. UI *UI_new(void)
  18. {
  19. return UI_new_method(NULL);
  20. }
  21. UI *UI_new_method(const UI_METHOD *method)
  22. {
  23. UI *ret = OPENSSL_zalloc(sizeof(*ret));
  24. if (ret == NULL) {
  25. UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  26. return NULL;
  27. }
  28. ret->lock = CRYPTO_THREAD_lock_new();
  29. if (ret->lock == NULL) {
  30. UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  31. OPENSSL_free(ret);
  32. return NULL;
  33. }
  34. if (method == NULL)
  35. method = UI_get_default_method();
  36. if (method == NULL)
  37. method = UI_null();
  38. ret->meth = method;
  39. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data)) {
  40. OPENSSL_free(ret);
  41. return NULL;
  42. }
  43. return ret;
  44. }
  45. static void free_string(UI_STRING *uis)
  46. {
  47. if (uis->flags & OUT_STRING_FREEABLE) {
  48. OPENSSL_free((char *)uis->out_string);
  49. switch (uis->type) {
  50. case UIT_BOOLEAN:
  51. OPENSSL_free((char *)uis->_.boolean_data.action_desc);
  52. OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
  53. OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
  54. break;
  55. case UIT_NONE:
  56. case UIT_PROMPT:
  57. case UIT_VERIFY:
  58. case UIT_ERROR:
  59. case UIT_INFO:
  60. break;
  61. }
  62. }
  63. OPENSSL_free(uis);
  64. }
  65. void UI_free(UI *ui)
  66. {
  67. if (ui == NULL)
  68. return;
  69. if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
  70. ui->meth->ui_destroy_data(ui, ui->user_data);
  71. }
  72. sk_UI_STRING_pop_free(ui->strings, free_string);
  73. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
  74. CRYPTO_THREAD_lock_free(ui->lock);
  75. OPENSSL_free(ui);
  76. }
  77. static int allocate_string_stack(UI *ui)
  78. {
  79. if (ui->strings == NULL) {
  80. ui->strings = sk_UI_STRING_new_null();
  81. if (ui->strings == NULL) {
  82. return -1;
  83. }
  84. }
  85. return 0;
  86. }
  87. static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
  88. int prompt_freeable,
  89. enum UI_string_types type,
  90. int input_flags, char *result_buf)
  91. {
  92. UI_STRING *ret = NULL;
  93. if (prompt == NULL) {
  94. UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
  95. } else if ((type == UIT_PROMPT || type == UIT_VERIFY
  96. || type == UIT_BOOLEAN) && result_buf == NULL) {
  97. UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
  98. } else if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL) {
  99. ret->out_string = prompt;
  100. ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
  101. ret->input_flags = input_flags;
  102. ret->type = type;
  103. ret->result_buf = result_buf;
  104. }
  105. return ret;
  106. }
  107. static int general_allocate_string(UI *ui, const char *prompt,
  108. int prompt_freeable,
  109. enum UI_string_types type, int input_flags,
  110. char *result_buf, int minsize, int maxsize,
  111. const char *test_buf)
  112. {
  113. int ret = -1;
  114. UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
  115. type, input_flags, result_buf);
  116. if (s != NULL) {
  117. if (allocate_string_stack(ui) >= 0) {
  118. s->_.string_data.result_minsize = minsize;
  119. s->_.string_data.result_maxsize = maxsize;
  120. s->_.string_data.test_buf = test_buf;
  121. ret = sk_UI_STRING_push(ui->strings, s);
  122. /* sk_push() returns 0 on error. Let's adapt that */
  123. if (ret <= 0) {
  124. ret--;
  125. free_string(s);
  126. }
  127. } else
  128. free_string(s);
  129. }
  130. return ret;
  131. }
  132. static int general_allocate_boolean(UI *ui,
  133. const char *prompt,
  134. const char *action_desc,
  135. const char *ok_chars,
  136. const char *cancel_chars,
  137. int prompt_freeable,
  138. enum UI_string_types type,
  139. int input_flags, char *result_buf)
  140. {
  141. int ret = -1;
  142. UI_STRING *s;
  143. const char *p;
  144. if (ok_chars == NULL) {
  145. UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
  146. } else if (cancel_chars == NULL) {
  147. UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
  148. } else {
  149. for (p = ok_chars; *p != '\0'; p++) {
  150. if (strchr(cancel_chars, *p) != NULL) {
  151. UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
  152. UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
  153. }
  154. }
  155. s = general_allocate_prompt(ui, prompt, prompt_freeable,
  156. type, input_flags, result_buf);
  157. if (s != NULL) {
  158. if (allocate_string_stack(ui) >= 0) {
  159. s->_.boolean_data.action_desc = action_desc;
  160. s->_.boolean_data.ok_chars = ok_chars;
  161. s->_.boolean_data.cancel_chars = cancel_chars;
  162. ret = sk_UI_STRING_push(ui->strings, s);
  163. /*
  164. * sk_push() returns 0 on error. Let's adapt that
  165. */
  166. if (ret <= 0) {
  167. ret--;
  168. free_string(s);
  169. }
  170. } else
  171. free_string(s);
  172. }
  173. }
  174. return ret;
  175. }
  176. /*
  177. * Returns the index to the place in the stack or -1 for error. Uses a
  178. * direct reference to the prompt.
  179. */
  180. int UI_add_input_string(UI *ui, const char *prompt, int flags,
  181. char *result_buf, int minsize, int maxsize)
  182. {
  183. return general_allocate_string(ui, prompt, 0,
  184. UIT_PROMPT, flags, result_buf, minsize,
  185. maxsize, NULL);
  186. }
  187. /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
  188. int UI_dup_input_string(UI *ui, const char *prompt, int flags,
  189. char *result_buf, int minsize, int maxsize)
  190. {
  191. char *prompt_copy = NULL;
  192. if (prompt != NULL) {
  193. prompt_copy = OPENSSL_strdup(prompt);
  194. if (prompt_copy == NULL) {
  195. UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
  196. return 0;
  197. }
  198. }
  199. return general_allocate_string(ui, prompt_copy, 1,
  200. UIT_PROMPT, flags, result_buf, minsize,
  201. maxsize, NULL);
  202. }
  203. int UI_add_verify_string(UI *ui, const char *prompt, int flags,
  204. char *result_buf, int minsize, int maxsize,
  205. const char *test_buf)
  206. {
  207. return general_allocate_string(ui, prompt, 0,
  208. UIT_VERIFY, flags, result_buf, minsize,
  209. maxsize, test_buf);
  210. }
  211. int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
  212. char *result_buf, int minsize, int maxsize,
  213. const char *test_buf)
  214. {
  215. char *prompt_copy = NULL;
  216. if (prompt != NULL) {
  217. prompt_copy = OPENSSL_strdup(prompt);
  218. if (prompt_copy == NULL) {
  219. UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
  220. return -1;
  221. }
  222. }
  223. return general_allocate_string(ui, prompt_copy, 1,
  224. UIT_VERIFY, flags, result_buf, minsize,
  225. maxsize, test_buf);
  226. }
  227. int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
  228. const char *ok_chars, const char *cancel_chars,
  229. int flags, char *result_buf)
  230. {
  231. return general_allocate_boolean(ui, prompt, action_desc,
  232. ok_chars, cancel_chars, 0, UIT_BOOLEAN,
  233. flags, result_buf);
  234. }
  235. int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
  236. const char *ok_chars, const char *cancel_chars,
  237. int flags, char *result_buf)
  238. {
  239. char *prompt_copy = NULL;
  240. char *action_desc_copy = NULL;
  241. char *ok_chars_copy = NULL;
  242. char *cancel_chars_copy = NULL;
  243. if (prompt != NULL) {
  244. prompt_copy = OPENSSL_strdup(prompt);
  245. if (prompt_copy == NULL) {
  246. UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
  247. goto err;
  248. }
  249. }
  250. if (action_desc != NULL) {
  251. action_desc_copy = OPENSSL_strdup(action_desc);
  252. if (action_desc_copy == NULL) {
  253. UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
  254. goto err;
  255. }
  256. }
  257. if (ok_chars != NULL) {
  258. ok_chars_copy = OPENSSL_strdup(ok_chars);
  259. if (ok_chars_copy == NULL) {
  260. UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
  261. goto err;
  262. }
  263. }
  264. if (cancel_chars != NULL) {
  265. cancel_chars_copy = OPENSSL_strdup(cancel_chars);
  266. if (cancel_chars_copy == NULL) {
  267. UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
  268. goto err;
  269. }
  270. }
  271. return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
  272. ok_chars_copy, cancel_chars_copy, 1,
  273. UIT_BOOLEAN, flags, result_buf);
  274. err:
  275. OPENSSL_free(prompt_copy);
  276. OPENSSL_free(action_desc_copy);
  277. OPENSSL_free(ok_chars_copy);
  278. OPENSSL_free(cancel_chars_copy);
  279. return -1;
  280. }
  281. int UI_add_info_string(UI *ui, const char *text)
  282. {
  283. return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
  284. NULL);
  285. }
  286. int UI_dup_info_string(UI *ui, const char *text)
  287. {
  288. char *text_copy = NULL;
  289. if (text != NULL) {
  290. text_copy = OPENSSL_strdup(text);
  291. if (text_copy == NULL) {
  292. UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
  293. return -1;
  294. }
  295. }
  296. return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
  297. 0, 0, NULL);
  298. }
  299. int UI_add_error_string(UI *ui, const char *text)
  300. {
  301. return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
  302. NULL);
  303. }
  304. int UI_dup_error_string(UI *ui, const char *text)
  305. {
  306. char *text_copy = NULL;
  307. if (text != NULL) {
  308. text_copy = OPENSSL_strdup(text);
  309. if (text_copy == NULL) {
  310. UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
  311. return -1;
  312. }
  313. }
  314. return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
  315. 0, 0, NULL);
  316. }
  317. char *UI_construct_prompt(UI *ui, const char *object_desc,
  318. const char *object_name)
  319. {
  320. char *prompt = NULL;
  321. if (ui->meth->ui_construct_prompt != NULL)
  322. prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
  323. else {
  324. char prompt1[] = "Enter ";
  325. char prompt2[] = " for ";
  326. char prompt3[] = ":";
  327. int len = 0;
  328. if (object_desc == NULL)
  329. return NULL;
  330. len = sizeof(prompt1) - 1 + strlen(object_desc);
  331. if (object_name != NULL)
  332. len += sizeof(prompt2) - 1 + strlen(object_name);
  333. len += sizeof(prompt3) - 1;
  334. if ((prompt = OPENSSL_malloc(len + 1)) == NULL) {
  335. UIerr(UI_F_UI_CONSTRUCT_PROMPT, ERR_R_MALLOC_FAILURE);
  336. return NULL;
  337. }
  338. OPENSSL_strlcpy(prompt, prompt1, len + 1);
  339. OPENSSL_strlcat(prompt, object_desc, len + 1);
  340. if (object_name != NULL) {
  341. OPENSSL_strlcat(prompt, prompt2, len + 1);
  342. OPENSSL_strlcat(prompt, object_name, len + 1);
  343. }
  344. OPENSSL_strlcat(prompt, prompt3, len + 1);
  345. }
  346. return prompt;
  347. }
  348. void *UI_add_user_data(UI *ui, void *user_data)
  349. {
  350. void *old_data = ui->user_data;
  351. if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
  352. ui->meth->ui_destroy_data(ui, old_data);
  353. old_data = NULL;
  354. }
  355. ui->user_data = user_data;
  356. ui->flags &= ~UI_FLAG_DUPL_DATA;
  357. return old_data;
  358. }
  359. int UI_dup_user_data(UI *ui, void *user_data)
  360. {
  361. void *duplicate = NULL;
  362. if (ui->meth->ui_duplicate_data == NULL
  363. || ui->meth->ui_destroy_data == NULL) {
  364. UIerr(UI_F_UI_DUP_USER_DATA, UI_R_USER_DATA_DUPLICATION_UNSUPPORTED);
  365. return -1;
  366. }
  367. duplicate = ui->meth->ui_duplicate_data(ui, user_data);
  368. if (duplicate == NULL) {
  369. UIerr(UI_F_UI_DUP_USER_DATA, ERR_R_MALLOC_FAILURE);
  370. return -1;
  371. }
  372. (void)UI_add_user_data(ui, duplicate);
  373. ui->flags |= UI_FLAG_DUPL_DATA;
  374. return 0;
  375. }
  376. void *UI_get0_user_data(UI *ui)
  377. {
  378. return ui->user_data;
  379. }
  380. const char *UI_get0_result(UI *ui, int i)
  381. {
  382. if (i < 0) {
  383. UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL);
  384. return NULL;
  385. }
  386. if (i >= sk_UI_STRING_num(ui->strings)) {
  387. UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE);
  388. return NULL;
  389. }
  390. return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
  391. }
  392. int UI_get_result_length(UI *ui, int i)
  393. {
  394. if (i < 0) {
  395. UIerr(UI_F_UI_GET_RESULT_LENGTH, UI_R_INDEX_TOO_SMALL);
  396. return -1;
  397. }
  398. if (i >= sk_UI_STRING_num(ui->strings)) {
  399. UIerr(UI_F_UI_GET_RESULT_LENGTH, UI_R_INDEX_TOO_LARGE);
  400. return -1;
  401. }
  402. return UI_get_result_string_length(sk_UI_STRING_value(ui->strings, i));
  403. }
  404. static int print_error(const char *str, size_t len, UI *ui)
  405. {
  406. UI_STRING uis;
  407. memset(&uis, 0, sizeof(uis));
  408. uis.type = UIT_ERROR;
  409. uis.out_string = str;
  410. if (ui->meth->ui_write_string != NULL
  411. && ui->meth->ui_write_string(ui, &uis) <= 0)
  412. return -1;
  413. return 0;
  414. }
  415. int UI_process(UI *ui)
  416. {
  417. int i, ok = 0;
  418. const char *state = "processing";
  419. if (ui->meth->ui_open_session != NULL
  420. && ui->meth->ui_open_session(ui) <= 0) {
  421. state = "opening session";
  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. state = "writing strings";
  435. ok = -1;
  436. goto err;
  437. }
  438. }
  439. if (ui->meth->ui_flush != NULL)
  440. switch (ui->meth->ui_flush(ui)) {
  441. case -1: /* Interrupt/Cancel/something... */
  442. ui->flags &= ~UI_FLAG_REDOABLE;
  443. ok = -2;
  444. goto err;
  445. case 0: /* Errors */
  446. state = "flushing";
  447. ok = -1;
  448. goto err;
  449. default: /* Success */
  450. ok = 0;
  451. break;
  452. }
  453. for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
  454. if (ui->meth->ui_read_string != NULL) {
  455. switch (ui->meth->ui_read_string(ui,
  456. sk_UI_STRING_value(ui->strings,
  457. i))) {
  458. case -1: /* Interrupt/Cancel/something... */
  459. ui->flags &= ~UI_FLAG_REDOABLE;
  460. ok = -2;
  461. goto err;
  462. case 0: /* Errors */
  463. state = "reading strings";
  464. ok = -1;
  465. goto err;
  466. default: /* Success */
  467. ok = 0;
  468. break;
  469. }
  470. }
  471. }
  472. state = NULL;
  473. err:
  474. if (ui->meth->ui_close_session != NULL
  475. && ui->meth->ui_close_session(ui) <= 0) {
  476. if (state == NULL)
  477. state = "closing session";
  478. ok = -1;
  479. }
  480. if (ok == -1) {
  481. UIerr(UI_F_UI_PROCESS, UI_R_PROCESSING_ERROR);
  482. ERR_add_error_data(2, "while ", state);
  483. }
  484. return ok;
  485. }
  486. int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
  487. {
  488. if (ui == NULL) {
  489. UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  490. return -1;
  491. }
  492. switch (cmd) {
  493. case UI_CTRL_PRINT_ERRORS:
  494. {
  495. int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS);
  496. if (i)
  497. ui->flags |= UI_FLAG_PRINT_ERRORS;
  498. else
  499. ui->flags &= ~UI_FLAG_PRINT_ERRORS;
  500. return save_flag;
  501. }
  502. case UI_CTRL_IS_REDOABLE:
  503. return ! !(ui->flags & UI_FLAG_REDOABLE);
  504. default:
  505. break;
  506. }
  507. UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
  508. return -1;
  509. }
  510. int UI_set_ex_data(UI *r, int idx, void *arg)
  511. {
  512. return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
  513. }
  514. void *UI_get_ex_data(const UI *r, int idx)
  515. {
  516. return CRYPTO_get_ex_data(&r->ex_data, idx);
  517. }
  518. const UI_METHOD *UI_get_method(UI *ui)
  519. {
  520. return ui->meth;
  521. }
  522. const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
  523. {
  524. ui->meth = meth;
  525. return ui->meth;
  526. }
  527. UI_METHOD *UI_create_method(const char *name)
  528. {
  529. UI_METHOD *ui_method = NULL;
  530. if ((ui_method = OPENSSL_zalloc(sizeof(*ui_method))) == NULL
  531. || (ui_method->name = OPENSSL_strdup(name)) == NULL
  532. || !CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
  533. &ui_method->ex_data)) {
  534. if (ui_method)
  535. OPENSSL_free(ui_method->name);
  536. OPENSSL_free(ui_method);
  537. UIerr(UI_F_UI_CREATE_METHOD, ERR_R_MALLOC_FAILURE);
  538. return NULL;
  539. }
  540. return ui_method;
  541. }
  542. /*
  543. * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
  544. * (that is, it hasn't been allocated using UI_create_method(), you deserve
  545. * anything Murphy can throw at you and more! You have been warned.
  546. */
  547. void UI_destroy_method(UI_METHOD *ui_method)
  548. {
  549. if (ui_method == NULL)
  550. return;
  551. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
  552. &ui_method->ex_data);
  553. OPENSSL_free(ui_method->name);
  554. ui_method->name = NULL;
  555. OPENSSL_free(ui_method);
  556. }
  557. int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui))
  558. {
  559. if (method != NULL) {
  560. method->ui_open_session = opener;
  561. return 0;
  562. }
  563. return -1;
  564. }
  565. int UI_method_set_writer(UI_METHOD *method,
  566. int (*writer) (UI *ui, UI_STRING *uis))
  567. {
  568. if (method != NULL) {
  569. method->ui_write_string = writer;
  570. return 0;
  571. }
  572. return -1;
  573. }
  574. int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
  575. {
  576. if (method != NULL) {
  577. method->ui_flush = flusher;
  578. return 0;
  579. }
  580. return -1;
  581. }
  582. int UI_method_set_reader(UI_METHOD *method,
  583. int (*reader) (UI *ui, UI_STRING *uis))
  584. {
  585. if (method != NULL) {
  586. method->ui_read_string = reader;
  587. return 0;
  588. }
  589. return -1;
  590. }
  591. int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
  592. {
  593. if (method != NULL) {
  594. method->ui_close_session = closer;
  595. return 0;
  596. }
  597. return -1;
  598. }
  599. int UI_method_set_data_duplicator(UI_METHOD *method,
  600. void *(*duplicator) (UI *ui, void *ui_data),
  601. void (*destructor)(UI *ui, void *ui_data))
  602. {
  603. if (method != NULL) {
  604. method->ui_duplicate_data = duplicator;
  605. method->ui_destroy_data = destructor;
  606. return 0;
  607. }
  608. return -1;
  609. }
  610. int UI_method_set_prompt_constructor(UI_METHOD *method,
  611. char *(*prompt_constructor) (UI *ui,
  612. const char
  613. *object_desc,
  614. const char
  615. *object_name))
  616. {
  617. if (method != NULL) {
  618. method->ui_construct_prompt = prompt_constructor;
  619. return 0;
  620. }
  621. return -1;
  622. }
  623. int UI_method_set_ex_data(UI_METHOD *method, int idx, void *data)
  624. {
  625. return CRYPTO_set_ex_data(&method->ex_data, idx, data);
  626. }
  627. int (*UI_method_get_opener(const UI_METHOD *method)) (UI *)
  628. {
  629. if (method != NULL)
  630. return method->ui_open_session;
  631. return NULL;
  632. }
  633. int (*UI_method_get_writer(const UI_METHOD *method)) (UI *, UI_STRING *)
  634. {
  635. if (method != NULL)
  636. return method->ui_write_string;
  637. return NULL;
  638. }
  639. int (*UI_method_get_flusher(const UI_METHOD *method)) (UI *)
  640. {
  641. if (method != NULL)
  642. return method->ui_flush;
  643. return NULL;
  644. }
  645. int (*UI_method_get_reader(const UI_METHOD *method)) (UI *, UI_STRING *)
  646. {
  647. if (method != NULL)
  648. return method->ui_read_string;
  649. return NULL;
  650. }
  651. int (*UI_method_get_closer(const UI_METHOD *method)) (UI *)
  652. {
  653. if (method != NULL)
  654. return method->ui_close_session;
  655. return NULL;
  656. }
  657. char *(*UI_method_get_prompt_constructor(const UI_METHOD *method))
  658. (UI *, const char *, const char *)
  659. {
  660. if (method != NULL)
  661. return method->ui_construct_prompt;
  662. return NULL;
  663. }
  664. void *(*UI_method_get_data_duplicator(const UI_METHOD *method)) (UI *, void *)
  665. {
  666. if (method != NULL)
  667. return method->ui_duplicate_data;
  668. return NULL;
  669. }
  670. void (*UI_method_get_data_destructor(const UI_METHOD *method)) (UI *, void *)
  671. {
  672. if (method != NULL)
  673. return method->ui_destroy_data;
  674. return NULL;
  675. }
  676. const void *UI_method_get_ex_data(const UI_METHOD *method, int idx)
  677. {
  678. return CRYPTO_get_ex_data(&method->ex_data, idx);
  679. }
  680. enum UI_string_types UI_get_string_type(UI_STRING *uis)
  681. {
  682. return uis->type;
  683. }
  684. int UI_get_input_flags(UI_STRING *uis)
  685. {
  686. return uis->input_flags;
  687. }
  688. const char *UI_get0_output_string(UI_STRING *uis)
  689. {
  690. return uis->out_string;
  691. }
  692. const char *UI_get0_action_string(UI_STRING *uis)
  693. {
  694. switch (uis->type) {
  695. case UIT_BOOLEAN:
  696. return uis->_.boolean_data.action_desc;
  697. case UIT_PROMPT:
  698. case UIT_NONE:
  699. case UIT_VERIFY:
  700. case UIT_INFO:
  701. case UIT_ERROR:
  702. break;
  703. }
  704. return NULL;
  705. }
  706. const char *UI_get0_result_string(UI_STRING *uis)
  707. {
  708. switch (uis->type) {
  709. case UIT_PROMPT:
  710. case UIT_VERIFY:
  711. return uis->result_buf;
  712. case UIT_NONE:
  713. case UIT_BOOLEAN:
  714. case UIT_INFO:
  715. case UIT_ERROR:
  716. break;
  717. }
  718. return NULL;
  719. }
  720. int UI_get_result_string_length(UI_STRING *uis)
  721. {
  722. switch (uis->type) {
  723. case UIT_PROMPT:
  724. case UIT_VERIFY:
  725. return uis->result_len;
  726. case UIT_NONE:
  727. case UIT_BOOLEAN:
  728. case UIT_INFO:
  729. case UIT_ERROR:
  730. break;
  731. }
  732. return -1;
  733. }
  734. const char *UI_get0_test_string(UI_STRING *uis)
  735. {
  736. switch (uis->type) {
  737. case UIT_VERIFY:
  738. return uis->_.string_data.test_buf;
  739. case UIT_NONE:
  740. case UIT_BOOLEAN:
  741. case UIT_INFO:
  742. case UIT_ERROR:
  743. case UIT_PROMPT:
  744. break;
  745. }
  746. return NULL;
  747. }
  748. int UI_get_result_minsize(UI_STRING *uis)
  749. {
  750. switch (uis->type) {
  751. case UIT_PROMPT:
  752. case UIT_VERIFY:
  753. return uis->_.string_data.result_minsize;
  754. case UIT_NONE:
  755. case UIT_INFO:
  756. case UIT_ERROR:
  757. case UIT_BOOLEAN:
  758. break;
  759. }
  760. return -1;
  761. }
  762. int UI_get_result_maxsize(UI_STRING *uis)
  763. {
  764. switch (uis->type) {
  765. case UIT_PROMPT:
  766. case UIT_VERIFY:
  767. return uis->_.string_data.result_maxsize;
  768. case UIT_NONE:
  769. case UIT_INFO:
  770. case UIT_ERROR:
  771. case UIT_BOOLEAN:
  772. break;
  773. }
  774. return -1;
  775. }
  776. int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
  777. {
  778. return UI_set_result_ex(ui, uis, result, strlen(result));
  779. }
  780. int UI_set_result_ex(UI *ui, UI_STRING *uis, const char *result, int len)
  781. {
  782. ui->flags &= ~UI_FLAG_REDOABLE;
  783. switch (uis->type) {
  784. case UIT_PROMPT:
  785. case UIT_VERIFY:
  786. {
  787. char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
  788. char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
  789. BIO_snprintf(number1, sizeof(number1), "%d",
  790. uis->_.string_data.result_minsize);
  791. BIO_snprintf(number2, sizeof(number2), "%d",
  792. uis->_.string_data.result_maxsize);
  793. if (len < uis->_.string_data.result_minsize) {
  794. ui->flags |= UI_FLAG_REDOABLE;
  795. UIerr(UI_F_UI_SET_RESULT_EX, UI_R_RESULT_TOO_SMALL);
  796. ERR_add_error_data(5, "You must type in ",
  797. number1, " to ", number2, " characters");
  798. return -1;
  799. }
  800. if (len > uis->_.string_data.result_maxsize) {
  801. ui->flags |= UI_FLAG_REDOABLE;
  802. UIerr(UI_F_UI_SET_RESULT_EX, UI_R_RESULT_TOO_LARGE);
  803. ERR_add_error_data(5, "You must type in ",
  804. number1, " to ", number2, " characters");
  805. return -1;
  806. }
  807. }
  808. if (uis->result_buf == NULL) {
  809. UIerr(UI_F_UI_SET_RESULT_EX, UI_R_NO_RESULT_BUFFER);
  810. return -1;
  811. }
  812. memcpy(uis->result_buf, result, len);
  813. if (len <= uis->_.string_data.result_maxsize)
  814. uis->result_buf[len] = '\0';
  815. uis->result_len = len;
  816. break;
  817. case UIT_BOOLEAN:
  818. {
  819. const char *p;
  820. if (uis->result_buf == NULL) {
  821. UIerr(UI_F_UI_SET_RESULT_EX, UI_R_NO_RESULT_BUFFER);
  822. return -1;
  823. }
  824. uis->result_buf[0] = '\0';
  825. for (p = result; *p; p++) {
  826. if (strchr(uis->_.boolean_data.ok_chars, *p)) {
  827. uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
  828. break;
  829. }
  830. if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
  831. uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];
  832. break;
  833. }
  834. }
  835. }
  836. case UIT_NONE:
  837. case UIT_INFO:
  838. case UIT_ERROR:
  839. break;
  840. }
  841. return 0;
  842. }