apps_ui.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 1995-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 <openssl/err.h>
  11. #include <openssl/ui.h>
  12. #include "apps_ui.h"
  13. static UI_METHOD *ui_method = NULL;
  14. static const UI_METHOD *ui_base_method = NULL;
  15. static int ui_open(UI *ui)
  16. {
  17. int (*opener)(UI *ui) = UI_method_get_opener(ui_base_method);
  18. if (opener != NULL)
  19. return opener(ui);
  20. return 1;
  21. }
  22. static int ui_read(UI *ui, UI_STRING *uis)
  23. {
  24. int (*reader)(UI *ui, UI_STRING *uis) = NULL;
  25. if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
  26. && UI_get0_user_data(ui)) {
  27. switch (UI_get_string_type(uis)) {
  28. case UIT_PROMPT:
  29. case UIT_VERIFY:
  30. {
  31. const char *password =
  32. ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
  33. if (password != NULL) {
  34. UI_set_result(ui, uis, password);
  35. return 1;
  36. }
  37. }
  38. break;
  39. case UIT_NONE:
  40. case UIT_BOOLEAN:
  41. case UIT_INFO:
  42. case UIT_ERROR:
  43. break;
  44. }
  45. }
  46. reader = UI_method_get_reader(ui_base_method);
  47. if (reader != NULL)
  48. return reader(ui, uis);
  49. /* Default to the empty password if we've got nothing better */
  50. UI_set_result(ui, uis, "");
  51. return 1;
  52. }
  53. static int ui_write(UI *ui, UI_STRING *uis)
  54. {
  55. int (*writer)(UI *ui, UI_STRING *uis) = NULL;
  56. if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
  57. && UI_get0_user_data(ui)) {
  58. switch (UI_get_string_type(uis)) {
  59. case UIT_PROMPT:
  60. case UIT_VERIFY:
  61. {
  62. const char *password =
  63. ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
  64. if (password != NULL)
  65. return 1;
  66. }
  67. break;
  68. case UIT_NONE:
  69. case UIT_BOOLEAN:
  70. case UIT_INFO:
  71. case UIT_ERROR:
  72. break;
  73. }
  74. }
  75. writer = UI_method_get_writer(ui_base_method);
  76. if (writer != NULL)
  77. return writer(ui, uis);
  78. return 1;
  79. }
  80. static int ui_close(UI *ui)
  81. {
  82. int (*closer)(UI *ui) = UI_method_get_closer(ui_base_method);
  83. if (closer != NULL)
  84. return closer(ui);
  85. return 1;
  86. }
  87. /* object_name defaults to prompt_info from ui user data if present */
  88. static char *ui_prompt_construct(UI *ui, const char *phrase_desc,
  89. const char *object_name)
  90. {
  91. PW_CB_DATA *cb_data = (PW_CB_DATA *)UI_get0_user_data(ui);
  92. if (phrase_desc == NULL)
  93. phrase_desc = "pass phrase";
  94. if (object_name == NULL && cb_data != NULL)
  95. object_name = cb_data->prompt_info;
  96. return UI_construct_prompt(NULL, phrase_desc, object_name);
  97. }
  98. int set_base_ui_method(const UI_METHOD *ui_meth)
  99. {
  100. if (ui_meth == NULL)
  101. ui_meth = UI_null();
  102. ui_base_method = ui_meth;
  103. return 1;
  104. }
  105. int setup_ui_method(void)
  106. {
  107. ui_base_method = UI_null();
  108. #ifndef OPENSSL_NO_UI_CONSOLE
  109. ui_base_method = UI_OpenSSL();
  110. #endif
  111. ui_method = UI_create_method("OpenSSL application user interface");
  112. return ui_method != NULL
  113. && 0 == UI_method_set_opener(ui_method, ui_open)
  114. && 0 == UI_method_set_reader(ui_method, ui_read)
  115. && 0 == UI_method_set_writer(ui_method, ui_write)
  116. && 0 == UI_method_set_closer(ui_method, ui_close)
  117. && 0 == UI_method_set_prompt_constructor(ui_method,
  118. ui_prompt_construct);
  119. }
  120. void destroy_ui_method(void)
  121. {
  122. if (ui_method != NULL) {
  123. UI_destroy_method(ui_method);
  124. ui_method = NULL;
  125. }
  126. }
  127. const UI_METHOD *get_ui_method(void)
  128. {
  129. return ui_method;
  130. }
  131. static void *ui_malloc(int sz, const char *what)
  132. {
  133. void *vp = OPENSSL_malloc(sz);
  134. if (vp == NULL) {
  135. BIO_printf(bio_err, "Could not allocate %d bytes for %s\n", sz, what);
  136. ERR_print_errors(bio_err);
  137. exit(1);
  138. }
  139. return vp;
  140. }
  141. int password_callback(char *buf, int bufsiz, int verify, PW_CB_DATA *cb_data)
  142. {
  143. int res = 0;
  144. UI *ui;
  145. int ok = 0;
  146. char *buff = NULL;
  147. int ui_flags = 0;
  148. const char *prompt_info = NULL;
  149. char *prompt;
  150. if ((ui = UI_new_method(ui_method)) == NULL)
  151. return 0;
  152. if (cb_data != NULL && cb_data->prompt_info != NULL)
  153. prompt_info = cb_data->prompt_info;
  154. prompt = UI_construct_prompt(ui, "pass phrase", prompt_info);
  155. if (prompt == NULL) {
  156. BIO_printf(bio_err, "Out of memory\n");
  157. UI_free(ui);
  158. return 0;
  159. }
  160. ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD;
  161. UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0);
  162. /* We know that there is no previous user data to return to us */
  163. (void)UI_add_user_data(ui, cb_data);
  164. ok = UI_add_input_string(ui, prompt, ui_flags, buf,
  165. PW_MIN_LENGTH, bufsiz - 1);
  166. if (ok >= 0 && verify) {
  167. buff = ui_malloc(bufsiz, "password buffer");
  168. ok = UI_add_verify_string(ui, prompt, ui_flags, buff,
  169. PW_MIN_LENGTH, bufsiz - 1, buf);
  170. }
  171. if (ok >= 0)
  172. do {
  173. ok = UI_process(ui);
  174. } while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0));
  175. OPENSSL_clear_free(buff, (unsigned int)bufsiz);
  176. if (ok >= 0)
  177. res = strlen(buf);
  178. if (ok == -1) {
  179. BIO_printf(bio_err, "User interface error\n");
  180. ERR_print_errors(bio_err);
  181. OPENSSL_cleanse(buf, (unsigned int)bufsiz);
  182. res = 0;
  183. }
  184. if (ok == -2) {
  185. BIO_printf(bio_err, "aborted!\n");
  186. OPENSSL_cleanse(buf, (unsigned int)bufsiz);
  187. res = 0;
  188. }
  189. UI_free(ui);
  190. OPENSSL_free(prompt);
  191. return res;
  192. }