OSDN Git Service

c2c39860dc502accf61437f445ea2abc21d0dd11
[ffftp/ffftp.git] / contrib / openssl / include / openssl / ui.h
1 /* crypto/ui/ui.h -*- mode:C; c-file-style: "eay" -*- */\r
2 /*\r
3  * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project\r
4  * 2001.\r
5  */\r
6 /* ====================================================================\r
7  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.\r
8  *\r
9  * Redistribution and use in source and binary forms, with or without\r
10  * modification, are permitted provided that the following conditions\r
11  * are met:\r
12  *\r
13  * 1. Redistributions of source code must retain the above copyright\r
14  *    notice, this list of conditions and the following disclaimer.\r
15  *\r
16  * 2. Redistributions in binary form must reproduce the above copyright\r
17  *    notice, this list of conditions and the following disclaimer in\r
18  *    the documentation and/or other materials provided with the\r
19  *    distribution.\r
20  *\r
21  * 3. All advertising materials mentioning features or use of this\r
22  *    software must display the following acknowledgment:\r
23  *    "This product includes software developed by the OpenSSL Project\r
24  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"\r
25  *\r
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to\r
27  *    endorse or promote products derived from this software without\r
28  *    prior written permission. For written permission, please contact\r
29  *    openssl-core@openssl.org.\r
30  *\r
31  * 5. Products derived from this software may not be called "OpenSSL"\r
32  *    nor may "OpenSSL" appear in their names without prior written\r
33  *    permission of the OpenSSL Project.\r
34  *\r
35  * 6. Redistributions of any form whatsoever must retain the following\r
36  *    acknowledgment:\r
37  *    "This product includes software developed by the OpenSSL Project\r
38  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"\r
39  *\r
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\r
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR\r
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\r
51  * OF THE POSSIBILITY OF SUCH DAMAGE.\r
52  * ====================================================================\r
53  *\r
54  * This product includes cryptographic software written by Eric Young\r
55  * (eay@cryptsoft.com).  This product includes software written by Tim\r
56  * Hudson (tjh@cryptsoft.com).\r
57  *\r
58  */\r
59 \r
60 #ifndef HEADER_UI_H\r
61 # define HEADER_UI_H\r
62 \r
63 # ifndef OPENSSL_NO_DEPRECATED\r
64 #  include <openssl/crypto.h>\r
65 # endif\r
66 # include <openssl/safestack.h>\r
67 # include <openssl/ossl_typ.h>\r
68 \r
69 #ifdef  __cplusplus\r
70 extern "C" {\r
71 #endif\r
72 \r
73 /* Declared already in ossl_typ.h */\r
74 /* typedef struct ui_st UI; */\r
75 /* typedef struct ui_method_st UI_METHOD; */\r
76 \r
77 /*\r
78  * All the following functions return -1 or NULL on error and in some cases\r
79  * (UI_process()) -2 if interrupted or in some other way cancelled. When\r
80  * everything is fine, they return 0, a positive value or a non-NULL pointer,\r
81  * all depending on their purpose.\r
82  */\r
83 \r
84 /* Creators and destructor.   */\r
85 UI *UI_new(void);\r
86 UI *UI_new_method(const UI_METHOD *method);\r
87 void UI_free(UI *ui);\r
88 \r
89 /*-\r
90    The following functions are used to add strings to be printed and prompt\r
91    strings to prompt for data.  The names are UI_{add,dup}_<function>_string\r
92    and UI_{add,dup}_input_boolean.\r
93 \r
94    UI_{add,dup}_<function>_string have the following meanings:\r
95         add     add a text or prompt string.  The pointers given to these\r
96                 functions are used verbatim, no copying is done.\r
97         dup     make a copy of the text or prompt string, then add the copy\r
98                 to the collection of strings in the user interface.\r
99         <function>\r
100                 The function is a name for the functionality that the given\r
101                 string shall be used for.  It can be one of:\r
102                         input   use the string as data prompt.\r
103                         verify  use the string as verification prompt.  This\r
104                                 is used to verify a previous input.\r
105                         info    use the string for informational output.\r
106                         error   use the string for error output.\r
107    Honestly, there's currently no difference between info and error for the\r
108    moment.\r
109 \r
110    UI_{add,dup}_input_boolean have the same semantics for "add" and "dup",\r
111    and are typically used when one wants to prompt for a yes/no response.\r
112 \r
113    All of the functions in this group take a UI and a prompt string.\r
114    The string input and verify addition functions also take a flag argument,\r
115    a buffer for the result to end up with, a minimum input size and a maximum\r
116    input size (the result buffer MUST be large enough to be able to contain\r
117    the maximum number of characters).  Additionally, the verify addition\r
118    functions takes another buffer to compare the result against.\r
119    The boolean input functions take an action description string (which should\r
120    be safe to ignore if the expected user action is obvious, for example with\r
121    a dialog box with an OK button and a Cancel button), a string of acceptable\r
122    characters to mean OK and to mean Cancel.  The two last strings are checked\r
123    to make sure they don't have common characters.  Additionally, the same\r
124    flag argument as for the string input is taken, as well as a result buffer.\r
125    The result buffer is required to be at least one byte long.  Depending on\r
126    the answer, the first character from the OK or the Cancel character strings\r
127    will be stored in the first byte of the result buffer.  No NUL will be\r
128    added, so the result is *not* a string.\r
129 \r
130    On success, the all return an index of the added information.  That index\r
131    is usefull when retrieving results with UI_get0_result(). */\r
132 int UI_add_input_string(UI *ui, const char *prompt, int flags,\r
133                         char *result_buf, int minsize, int maxsize);\r
134 int UI_dup_input_string(UI *ui, const char *prompt, int flags,\r
135                         char *result_buf, int minsize, int maxsize);\r
136 int UI_add_verify_string(UI *ui, const char *prompt, int flags,\r
137                          char *result_buf, int minsize, int maxsize,\r
138                          const char *test_buf);\r
139 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,\r
140                          char *result_buf, int minsize, int maxsize,\r
141                          const char *test_buf);\r
142 int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,\r
143                          const char *ok_chars, const char *cancel_chars,\r
144                          int flags, char *result_buf);\r
145 int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,\r
146                          const char *ok_chars, const char *cancel_chars,\r
147                          int flags, char *result_buf);\r
148 int UI_add_info_string(UI *ui, const char *text);\r
149 int UI_dup_info_string(UI *ui, const char *text);\r
150 int UI_add_error_string(UI *ui, const char *text);\r
151 int UI_dup_error_string(UI *ui, const char *text);\r
152 \r
153 /* These are the possible flags.  They can be or'ed together. */\r
154 /* Use to have echoing of input */\r
155 # define UI_INPUT_FLAG_ECHO              0x01\r
156 /*\r
157  * Use a default password.  Where that password is found is completely up to\r
158  * the application, it might for example be in the user data set with\r
159  * UI_add_user_data().  It is not recommended to have more than one input in\r
160  * each UI being marked with this flag, or the application might get\r
161  * confused.\r
162  */\r
163 # define UI_INPUT_FLAG_DEFAULT_PWD       0x02\r
164 \r
165 /*-\r
166  * The user of these routines may want to define flags of their own.  The core\r
167  * UI won't look at those, but will pass them on to the method routines.  They\r
168  * must use higher bits so they don't get confused with the UI bits above.\r
169  * UI_INPUT_FLAG_USER_BASE tells which is the lowest bit to use.  A good\r
170  * example of use is this:\r
171  *\r
172  *    #define MY_UI_FLAG1       (0x01 << UI_INPUT_FLAG_USER_BASE)\r
173  *\r
174 */\r
175 # define UI_INPUT_FLAG_USER_BASE 16\r
176 \r
177 /*-\r
178  * The following function helps construct a prompt.  object_desc is a\r
179  * textual short description of the object, for example "pass phrase",\r
180  * and object_name is the name of the object (might be a card name or\r
181  * a file name.\r
182  * The returned string shall always be allocated on the heap with\r
183  * OPENSSL_malloc(), and need to be free'd with OPENSSL_free().\r
184  *\r
185  * If the ui_method doesn't contain a pointer to a user-defined prompt\r
186  * constructor, a default string is built, looking like this:\r
187  *\r
188  *       "Enter {object_desc} for {object_name}:"\r
189  *\r
190  * So, if object_desc has the value "pass phrase" and object_name has\r
191  * the value "foo.key", the resulting string is:\r
192  *\r
193  *       "Enter pass phrase for foo.key:"\r
194 */\r
195 char *UI_construct_prompt(UI *ui_method,\r
196                           const char *object_desc, const char *object_name);\r
197 \r
198 /*\r
199  * The following function is used to store a pointer to user-specific data.\r
200  * Any previous such pointer will be returned and replaced.\r
201  *\r
202  * For callback purposes, this function makes a lot more sense than using\r
203  * ex_data, since the latter requires that different parts of OpenSSL or\r
204  * applications share the same ex_data index.\r
205  *\r
206  * Note that the UI_OpenSSL() method completely ignores the user data. Other\r
207  * methods may not, however.\r
208  */\r
209 void *UI_add_user_data(UI *ui, void *user_data);\r
210 /* We need a user data retrieving function as well.  */\r
211 void *UI_get0_user_data(UI *ui);\r
212 \r
213 /* Return the result associated with a prompt given with the index i. */\r
214 const char *UI_get0_result(UI *ui, int i);\r
215 \r
216 /* When all strings have been added, process the whole thing. */\r
217 int UI_process(UI *ui);\r
218 \r
219 /*\r
220  * Give a user interface parametrised control commands.  This can be used to\r
221  * send down an integer, a data pointer or a function pointer, as well as be\r
222  * used to get information from a UI.\r
223  */\r
224 int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void));\r
225 \r
226 /* The commands */\r
227 /*\r
228  * Use UI_CONTROL_PRINT_ERRORS with the value 1 to have UI_process print the\r
229  * OpenSSL error stack before printing any info or added error messages and\r
230  * before any prompting.\r
231  */\r
232 # define UI_CTRL_PRINT_ERRORS            1\r
233 /*\r
234  * Check if a UI_process() is possible to do again with the same instance of\r
235  * a user interface.  This makes UI_ctrl() return 1 if it is redoable, and 0\r
236  * if not.\r
237  */\r
238 # define UI_CTRL_IS_REDOABLE             2\r
239 \r
240 /* Some methods may use extra data */\r
241 # define UI_set_app_data(s,arg)         UI_set_ex_data(s,0,arg)\r
242 # define UI_get_app_data(s)             UI_get_ex_data(s,0)\r
243 int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,\r
244                         CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);\r
245 int UI_set_ex_data(UI *r, int idx, void *arg);\r
246 void *UI_get_ex_data(UI *r, int idx);\r
247 \r
248 /* Use specific methods instead of the built-in one */\r
249 void UI_set_default_method(const UI_METHOD *meth);\r
250 const UI_METHOD *UI_get_default_method(void);\r
251 const UI_METHOD *UI_get_method(UI *ui);\r
252 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth);\r
253 \r
254 /* The method with all the built-in thingies */\r
255 UI_METHOD *UI_OpenSSL(void);\r
256 \r
257 /* ---------- For method writers ---------- */\r
258 /*-\r
259    A method contains a number of functions that implement the low level\r
260    of the User Interface.  The functions are:\r
261 \r
262         an opener       This function starts a session, maybe by opening\r
263                         a channel to a tty, or by opening a window.\r
264         a writer        This function is called to write a given string,\r
265                         maybe to the tty, maybe as a field label in a\r
266                         window.\r
267         a flusher       This function is called to flush everything that\r
268                         has been output so far.  It can be used to actually\r
269                         display a dialog box after it has been built.\r
270         a reader        This function is called to read a given prompt,\r
271                         maybe from the tty, maybe from a field in a\r
272                         window.  Note that it's called wth all string\r
273                         structures, not only the prompt ones, so it must\r
274                         check such things itself.\r
275         a closer        This function closes the session, maybe by closing\r
276                         the channel to the tty, or closing the window.\r
277 \r
278    All these functions are expected to return:\r
279 \r
280         0       on error.\r
281         1       on success.\r
282         -1      on out-of-band events, for example if some prompting has\r
283                 been canceled (by pressing Ctrl-C, for example).  This is\r
284                 only checked when returned by the flusher or the reader.\r
285 \r
286    The way this is used, the opener is first called, then the writer for all\r
287    strings, then the flusher, then the reader for all strings and finally the\r
288    closer.  Note that if you want to prompt from a terminal or other command\r
289    line interface, the best is to have the reader also write the prompts\r
290    instead of having the writer do it.  If you want to prompt from a dialog\r
291    box, the writer can be used to build up the contents of the box, and the\r
292    flusher to actually display the box and run the event loop until all data\r
293    has been given, after which the reader only grabs the given data and puts\r
294    them back into the UI strings.\r
295 \r
296    All method functions take a UI as argument.  Additionally, the writer and\r
297    the reader take a UI_STRING.\r
298 */\r
299 \r
300 /*\r
301  * The UI_STRING type is the data structure that contains all the needed info\r
302  * about a string or a prompt, including test data for a verification prompt.\r
303  */\r
304 typedef struct ui_string_st UI_STRING;\r
305 DECLARE_STACK_OF(UI_STRING)\r
306 \r
307 /*\r
308  * The different types of strings that are currently supported. This is only\r
309  * needed by method authors.\r
310  */\r
311 enum UI_string_types {\r
312     UIT_NONE = 0,\r
313     UIT_PROMPT,                 /* Prompt for a string */\r
314     UIT_VERIFY,                 /* Prompt for a string and verify */\r
315     UIT_BOOLEAN,                /* Prompt for a yes/no response */\r
316     UIT_INFO,                   /* Send info to the user */\r
317     UIT_ERROR                   /* Send an error message to the user */\r
318 };\r
319 \r
320 /* Create and manipulate methods */\r
321 UI_METHOD *UI_create_method(char *name);\r
322 void UI_destroy_method(UI_METHOD *ui_method);\r
323 int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui));\r
324 int UI_method_set_writer(UI_METHOD *method,\r
325                          int (*writer) (UI *ui, UI_STRING *uis));\r
326 int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui));\r
327 int UI_method_set_reader(UI_METHOD *method,\r
328                          int (*reader) (UI *ui, UI_STRING *uis));\r
329 int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui));\r
330 int UI_method_set_prompt_constructor(UI_METHOD *method,\r
331                                      char *(*prompt_constructor) (UI *ui,\r
332                                                                   const char\r
333                                                                   *object_desc,\r
334                                                                   const char\r
335                                                                   *object_name));\r
336 int (*UI_method_get_opener(UI_METHOD *method)) (UI *);\r
337 int (*UI_method_get_writer(UI_METHOD *method)) (UI *, UI_STRING *);\r
338 int (*UI_method_get_flusher(UI_METHOD *method)) (UI *);\r
339 int (*UI_method_get_reader(UI_METHOD *method)) (UI *, UI_STRING *);\r
340 int (*UI_method_get_closer(UI_METHOD *method)) (UI *);\r
341 char *(*UI_method_get_prompt_constructor(UI_METHOD *method)) (UI *,\r
342                                                               const char *,\r
343                                                               const char *);\r
344 \r
345 /*\r
346  * The following functions are helpers for method writers to access relevant\r
347  * data from a UI_STRING.\r
348  */\r
349 \r
350 /* Return type of the UI_STRING */\r
351 enum UI_string_types UI_get_string_type(UI_STRING *uis);\r
352 /* Return input flags of the UI_STRING */\r
353 int UI_get_input_flags(UI_STRING *uis);\r
354 /* Return the actual string to output (the prompt, info or error) */\r
355 const char *UI_get0_output_string(UI_STRING *uis);\r
356 /*\r
357  * Return the optional action string to output (the boolean promtp\r
358  * instruction)\r
359  */\r
360 const char *UI_get0_action_string(UI_STRING *uis);\r
361 /* Return the result of a prompt */\r
362 const char *UI_get0_result_string(UI_STRING *uis);\r
363 /*\r
364  * Return the string to test the result against.  Only useful with verifies.\r
365  */\r
366 const char *UI_get0_test_string(UI_STRING *uis);\r
367 /* Return the required minimum size of the result */\r
368 int UI_get_result_minsize(UI_STRING *uis);\r
369 /* Return the required maximum size of the result */\r
370 int UI_get_result_maxsize(UI_STRING *uis);\r
371 /* Set the result of a UI_STRING. */\r
372 int UI_set_result(UI *ui, UI_STRING *uis, const char *result);\r
373 \r
374 /* A couple of popular utility functions */\r
375 int UI_UTIL_read_pw_string(char *buf, int length, const char *prompt,\r
376                            int verify);\r
377 int UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt,\r
378                     int verify);\r
379 \r
380 /* BEGIN ERROR CODES */\r
381 /*\r
382  * The following lines are auto generated by the script mkerr.pl. Any changes\r
383  * made after this point may be overwritten when the script is next run.\r
384  */\r
385 void ERR_load_UI_strings(void);\r
386 \r
387 /* Error codes for the UI functions. */\r
388 \r
389 /* Function codes. */\r
390 # define UI_F_GENERAL_ALLOCATE_BOOLEAN                    108\r
391 # define UI_F_GENERAL_ALLOCATE_PROMPT                     109\r
392 # define UI_F_GENERAL_ALLOCATE_STRING                     100\r
393 # define UI_F_UI_CTRL                                     111\r
394 # define UI_F_UI_DUP_ERROR_STRING                         101\r
395 # define UI_F_UI_DUP_INFO_STRING                          102\r
396 # define UI_F_UI_DUP_INPUT_BOOLEAN                        110\r
397 # define UI_F_UI_DUP_INPUT_STRING                         103\r
398 # define UI_F_UI_DUP_VERIFY_STRING                        106\r
399 # define UI_F_UI_GET0_RESULT                              107\r
400 # define UI_F_UI_NEW_METHOD                               104\r
401 # define UI_F_UI_SET_RESULT                               105\r
402 \r
403 /* Reason codes. */\r
404 # define UI_R_COMMON_OK_AND_CANCEL_CHARACTERS             104\r
405 # define UI_R_INDEX_TOO_LARGE                             102\r
406 # define UI_R_INDEX_TOO_SMALL                             103\r
407 # define UI_R_NO_RESULT_BUFFER                            105\r
408 # define UI_R_RESULT_TOO_LARGE                            100\r
409 # define UI_R_RESULT_TOO_SMALL                            101\r
410 # define UI_R_UNKNOWN_CONTROL_COMMAND                     106\r
411 \r
412 #ifdef  __cplusplus\r
413 }\r
414 #endif\r
415 #endif\r