2
0

ui_lib.c 25 KB

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