ui_lib.c 25 KB

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