ui.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright 2001-2018 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. #ifndef HEADER_UI_H
  10. # define HEADER_UI_H
  11. # include <openssl/opensslconf.h>
  12. # if OPENSSL_API_COMPAT < 0x10100000L
  13. # include <openssl/crypto.h>
  14. # endif
  15. # include <openssl/safestack.h>
  16. # include <openssl/pem.h>
  17. # include <openssl/ossl_typ.h>
  18. # include <openssl/uierr.h>
  19. /* For compatibility reasons, the macro OPENSSL_NO_UI is currently retained */
  20. # if OPENSSL_API_COMPAT < 0x10200000L
  21. # ifdef OPENSSL_NO_UI_CONSOLE
  22. # define OPENSSL_NO_UI
  23. # endif
  24. # endif
  25. # ifdef __cplusplus
  26. extern "C" {
  27. # endif
  28. /*
  29. * All the following functions return -1 or NULL on error and in some cases
  30. * (UI_process()) -2 if interrupted or in some other way cancelled. When
  31. * everything is fine, they return 0, a positive value or a non-NULL pointer,
  32. * all depending on their purpose.
  33. */
  34. /* Creators and destructor. */
  35. UI *UI_new(void);
  36. UI *UI_new_method(const UI_METHOD *method);
  37. void UI_free(UI *ui);
  38. /*-
  39. The following functions are used to add strings to be printed and prompt
  40. strings to prompt for data. The names are UI_{add,dup}_<function>_string
  41. and UI_{add,dup}_input_boolean.
  42. UI_{add,dup}_<function>_string have the following meanings:
  43. add add a text or prompt string. The pointers given to these
  44. functions are used verbatim, no copying is done.
  45. dup make a copy of the text or prompt string, then add the copy
  46. to the collection of strings in the user interface.
  47. <function>
  48. The function is a name for the functionality that the given
  49. string shall be used for. It can be one of:
  50. input use the string as data prompt.
  51. verify use the string as verification prompt. This
  52. is used to verify a previous input.
  53. info use the string for informational output.
  54. error use the string for error output.
  55. Honestly, there's currently no difference between info and error for the
  56. moment.
  57. UI_{add,dup}_input_boolean have the same semantics for "add" and "dup",
  58. and are typically used when one wants to prompt for a yes/no response.
  59. All of the functions in this group take a UI and a prompt string.
  60. The string input and verify addition functions also take a flag argument,
  61. a buffer for the result to end up with, a minimum input size and a maximum
  62. input size (the result buffer MUST be large enough to be able to contain
  63. the maximum number of characters). Additionally, the verify addition
  64. functions takes another buffer to compare the result against.
  65. The boolean input functions take an action description string (which should
  66. be safe to ignore if the expected user action is obvious, for example with
  67. a dialog box with an OK button and a Cancel button), a string of acceptable
  68. characters to mean OK and to mean Cancel. The two last strings are checked
  69. to make sure they don't have common characters. Additionally, the same
  70. flag argument as for the string input is taken, as well as a result buffer.
  71. The result buffer is required to be at least one byte long. Depending on
  72. the answer, the first character from the OK or the Cancel character strings
  73. will be stored in the first byte of the result buffer. No NUL will be
  74. added, so the result is *not* a string.
  75. On success, the all return an index of the added information. That index
  76. is useful when retrieving results with UI_get0_result(). */
  77. int UI_add_input_string(UI *ui, const char *prompt, int flags,
  78. char *result_buf, int minsize, int maxsize);
  79. int UI_dup_input_string(UI *ui, const char *prompt, int flags,
  80. char *result_buf, int minsize, int maxsize);
  81. int UI_add_verify_string(UI *ui, const char *prompt, int flags,
  82. char *result_buf, int minsize, int maxsize,
  83. const char *test_buf);
  84. int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
  85. char *result_buf, int minsize, int maxsize,
  86. const char *test_buf);
  87. int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
  88. const char *ok_chars, const char *cancel_chars,
  89. int flags, char *result_buf);
  90. int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
  91. const char *ok_chars, const char *cancel_chars,
  92. int flags, char *result_buf);
  93. int UI_add_info_string(UI *ui, const char *text);
  94. int UI_dup_info_string(UI *ui, const char *text);
  95. int UI_add_error_string(UI *ui, const char *text);
  96. int UI_dup_error_string(UI *ui, const char *text);
  97. /* These are the possible flags. They can be or'ed together. */
  98. /* Use to have echoing of input */
  99. # define UI_INPUT_FLAG_ECHO 0x01
  100. /*
  101. * Use a default password. Where that password is found is completely up to
  102. * the application, it might for example be in the user data set with
  103. * UI_add_user_data(). It is not recommended to have more than one input in
  104. * each UI being marked with this flag, or the application might get
  105. * confused.
  106. */
  107. # define UI_INPUT_FLAG_DEFAULT_PWD 0x02
  108. /*-
  109. * The user of these routines may want to define flags of their own. The core
  110. * UI won't look at those, but will pass them on to the method routines. They
  111. * must use higher bits so they don't get confused with the UI bits above.
  112. * UI_INPUT_FLAG_USER_BASE tells which is the lowest bit to use. A good
  113. * example of use is this:
  114. *
  115. * #define MY_UI_FLAG1 (0x01 << UI_INPUT_FLAG_USER_BASE)
  116. *
  117. */
  118. # define UI_INPUT_FLAG_USER_BASE 16
  119. /*-
  120. * The following function helps construct a prompt. object_desc is a
  121. * textual short description of the object, for example "pass phrase",
  122. * and object_name is the name of the object (might be a card name or
  123. * a file name.
  124. * The returned string shall always be allocated on the heap with
  125. * OPENSSL_malloc(), and need to be free'd with OPENSSL_free().
  126. *
  127. * If the ui_method doesn't contain a pointer to a user-defined prompt
  128. * constructor, a default string is built, looking like this:
  129. *
  130. * "Enter {object_desc} for {object_name}:"
  131. *
  132. * So, if object_desc has the value "pass phrase" and object_name has
  133. * the value "foo.key", the resulting string is:
  134. *
  135. * "Enter pass phrase for foo.key:"
  136. */
  137. char *UI_construct_prompt(UI *ui_method,
  138. const char *object_desc, const char *object_name);
  139. /*
  140. * The following function is used to store a pointer to user-specific data.
  141. * Any previous such pointer will be returned and replaced.
  142. *
  143. * For callback purposes, this function makes a lot more sense than using
  144. * ex_data, since the latter requires that different parts of OpenSSL or
  145. * applications share the same ex_data index.
  146. *
  147. * Note that the UI_OpenSSL() method completely ignores the user data. Other
  148. * methods may not, however.
  149. */
  150. void *UI_add_user_data(UI *ui, void *user_data);
  151. /*
  152. * Alternatively, this function is used to duplicate the user data.
  153. * This uses the duplicator method function. The destroy function will
  154. * be used to free the user data in this case.
  155. */
  156. int UI_dup_user_data(UI *ui, void *user_data);
  157. /* We need a user data retrieving function as well. */
  158. void *UI_get0_user_data(UI *ui);
  159. /* Return the result associated with a prompt given with the index i. */
  160. const char *UI_get0_result(UI *ui, int i);
  161. int UI_get_result_length(UI *ui, int i);
  162. /* When all strings have been added, process the whole thing. */
  163. int UI_process(UI *ui);
  164. /*
  165. * Give a user interface parameterised control commands. This can be used to
  166. * send down an integer, a data pointer or a function pointer, as well as be
  167. * used to get information from a UI.
  168. */
  169. int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void));
  170. /* The commands */
  171. /*
  172. * Use UI_CONTROL_PRINT_ERRORS with the value 1 to have UI_process print the
  173. * OpenSSL error stack before printing any info or added error messages and
  174. * before any prompting.
  175. */
  176. # define UI_CTRL_PRINT_ERRORS 1
  177. /*
  178. * Check if a UI_process() is possible to do again with the same instance of
  179. * a user interface. This makes UI_ctrl() return 1 if it is redoable, and 0
  180. * if not.
  181. */
  182. # define UI_CTRL_IS_REDOABLE 2
  183. /* Some methods may use extra data */
  184. # define UI_set_app_data(s,arg) UI_set_ex_data(s,0,arg)
  185. # define UI_get_app_data(s) UI_get_ex_data(s,0)
  186. # define UI_get_ex_new_index(l, p, newf, dupf, freef) \
  187. CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, l, p, newf, dupf, freef)
  188. int UI_set_ex_data(UI *r, int idx, void *arg);
  189. void *UI_get_ex_data(UI *r, int idx);
  190. /* Use specific methods instead of the built-in one */
  191. void UI_set_default_method(const UI_METHOD *meth);
  192. const UI_METHOD *UI_get_default_method(void);
  193. const UI_METHOD *UI_get_method(UI *ui);
  194. const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth);
  195. # ifndef OPENSSL_NO_UI_CONSOLE
  196. /* The method with all the built-in thingies */
  197. UI_METHOD *UI_OpenSSL(void);
  198. # endif
  199. /*
  200. * NULL method. Literally does nothing, but may serve as a placeholder
  201. * to avoid internal default.
  202. */
  203. const UI_METHOD *UI_null(void);
  204. /* ---------- For method writers ---------- */
  205. /*-
  206. A method contains a number of functions that implement the low level
  207. of the User Interface. The functions are:
  208. an opener This function starts a session, maybe by opening
  209. a channel to a tty, or by opening a window.
  210. a writer This function is called to write a given string,
  211. maybe to the tty, maybe as a field label in a
  212. window.
  213. a flusher This function is called to flush everything that
  214. has been output so far. It can be used to actually
  215. display a dialog box after it has been built.
  216. a reader This function is called to read a given prompt,
  217. maybe from the tty, maybe from a field in a
  218. window. Note that it's called with all string
  219. structures, not only the prompt ones, so it must
  220. check such things itself.
  221. a closer This function closes the session, maybe by closing
  222. the channel to the tty, or closing the window.
  223. All these functions are expected to return:
  224. 0 on error.
  225. 1 on success.
  226. -1 on out-of-band events, for example if some prompting has
  227. been canceled (by pressing Ctrl-C, for example). This is
  228. only checked when returned by the flusher or the reader.
  229. The way this is used, the opener is first called, then the writer for all
  230. strings, then the flusher, then the reader for all strings and finally the
  231. closer. Note that if you want to prompt from a terminal or other command
  232. line interface, the best is to have the reader also write the prompts
  233. instead of having the writer do it. If you want to prompt from a dialog
  234. box, the writer can be used to build up the contents of the box, and the
  235. flusher to actually display the box and run the event loop until all data
  236. has been given, after which the reader only grabs the given data and puts
  237. them back into the UI strings.
  238. All method functions take a UI as argument. Additionally, the writer and
  239. the reader take a UI_STRING.
  240. */
  241. /*
  242. * The UI_STRING type is the data structure that contains all the needed info
  243. * about a string or a prompt, including test data for a verification prompt.
  244. */
  245. typedef struct ui_string_st UI_STRING;
  246. DEFINE_STACK_OF(UI_STRING)
  247. /*
  248. * The different types of strings that are currently supported. This is only
  249. * needed by method authors.
  250. */
  251. enum UI_string_types {
  252. UIT_NONE = 0,
  253. UIT_PROMPT, /* Prompt for a string */
  254. UIT_VERIFY, /* Prompt for a string and verify */
  255. UIT_BOOLEAN, /* Prompt for a yes/no response */
  256. UIT_INFO, /* Send info to the user */
  257. UIT_ERROR /* Send an error message to the user */
  258. };
  259. /* Create and manipulate methods */
  260. UI_METHOD *UI_create_method(const char *name);
  261. void UI_destroy_method(UI_METHOD *ui_method);
  262. int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui));
  263. int UI_method_set_writer(UI_METHOD *method,
  264. int (*writer) (UI *ui, UI_STRING *uis));
  265. int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui));
  266. int UI_method_set_reader(UI_METHOD *method,
  267. int (*reader) (UI *ui, UI_STRING *uis));
  268. int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui));
  269. int UI_method_set_data_duplicator(UI_METHOD *method,
  270. void *(*duplicator) (UI *ui, void *ui_data),
  271. void (*destructor)(UI *ui, void *ui_data));
  272. int UI_method_set_prompt_constructor(UI_METHOD *method,
  273. char *(*prompt_constructor) (UI *ui,
  274. const char
  275. *object_desc,
  276. const char
  277. *object_name));
  278. int UI_method_set_ex_data(UI_METHOD *method, int idx, void *data);
  279. int (*UI_method_get_opener(const UI_METHOD *method)) (UI *);
  280. int (*UI_method_get_writer(const UI_METHOD *method)) (UI *, UI_STRING *);
  281. int (*UI_method_get_flusher(const UI_METHOD *method)) (UI *);
  282. int (*UI_method_get_reader(const UI_METHOD *method)) (UI *, UI_STRING *);
  283. int (*UI_method_get_closer(const UI_METHOD *method)) (UI *);
  284. char *(*UI_method_get_prompt_constructor(const UI_METHOD *method))
  285. (UI *, const char *, const char *);
  286. void *(*UI_method_get_data_duplicator(const UI_METHOD *method)) (UI *, void *);
  287. void (*UI_method_get_data_destructor(const UI_METHOD *method)) (UI *, void *);
  288. const void *UI_method_get_ex_data(const UI_METHOD *method, int idx);
  289. /*
  290. * The following functions are helpers for method writers to access relevant
  291. * data from a UI_STRING.
  292. */
  293. /* Return type of the UI_STRING */
  294. enum UI_string_types UI_get_string_type(UI_STRING *uis);
  295. /* Return input flags of the UI_STRING */
  296. int UI_get_input_flags(UI_STRING *uis);
  297. /* Return the actual string to output (the prompt, info or error) */
  298. const char *UI_get0_output_string(UI_STRING *uis);
  299. /*
  300. * Return the optional action string to output (the boolean prompt
  301. * instruction)
  302. */
  303. const char *UI_get0_action_string(UI_STRING *uis);
  304. /* Return the result of a prompt */
  305. const char *UI_get0_result_string(UI_STRING *uis);
  306. int UI_get_result_string_length(UI_STRING *uis);
  307. /*
  308. * Return the string to test the result against. Only useful with verifies.
  309. */
  310. const char *UI_get0_test_string(UI_STRING *uis);
  311. /* Return the required minimum size of the result */
  312. int UI_get_result_minsize(UI_STRING *uis);
  313. /* Return the required maximum size of the result */
  314. int UI_get_result_maxsize(UI_STRING *uis);
  315. /* Set the result of a UI_STRING. */
  316. int UI_set_result(UI *ui, UI_STRING *uis, const char *result);
  317. int UI_set_result_ex(UI *ui, UI_STRING *uis, const char *result, int len);
  318. /* A couple of popular utility functions */
  319. int UI_UTIL_read_pw_string(char *buf, int length, const char *prompt,
  320. int verify);
  321. int UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt,
  322. int verify);
  323. UI_METHOD *UI_UTIL_wrap_read_pem_callback(pem_password_cb *cb, int rwflag);
  324. # ifdef __cplusplus
  325. }
  326. # endif
  327. #endif