ui_lib.c 26 KB

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