1109998Smarkm/* crypto/ui/ui.h -*- mode:C; c-file-style: "eay" -*- */ 2296465Sdelphij/* 3296465Sdelphij * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project 4296465Sdelphij * 2001. 5109998Smarkm */ 6109998Smarkm/* ==================================================================== 7109998Smarkm * Copyright (c) 2001 The OpenSSL Project. All rights reserved. 8109998Smarkm * 9109998Smarkm * Redistribution and use in source and binary forms, with or without 10109998Smarkm * modification, are permitted provided that the following conditions 11109998Smarkm * are met: 12109998Smarkm * 13109998Smarkm * 1. Redistributions of source code must retain the above copyright 14296465Sdelphij * notice, this list of conditions and the following disclaimer. 15109998Smarkm * 16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 17109998Smarkm * notice, this list of conditions and the following disclaimer in 18109998Smarkm * the documentation and/or other materials provided with the 19109998Smarkm * distribution. 20109998Smarkm * 21109998Smarkm * 3. All advertising materials mentioning features or use of this 22109998Smarkm * software must display the following acknowledgment: 23109998Smarkm * "This product includes software developed by the OpenSSL Project 24109998Smarkm * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 25109998Smarkm * 26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27109998Smarkm * endorse or promote products derived from this software without 28109998Smarkm * prior written permission. For written permission, please contact 29109998Smarkm * openssl-core@openssl.org. 30109998Smarkm * 31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 32109998Smarkm * nor may "OpenSSL" appear in their names without prior written 33109998Smarkm * permission of the OpenSSL Project. 34109998Smarkm * 35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 36109998Smarkm * acknowledgment: 37109998Smarkm * "This product includes software developed by the OpenSSL Project 38109998Smarkm * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 39109998Smarkm * 40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 52109998Smarkm * ==================================================================== 53109998Smarkm * 54109998Smarkm * This product includes cryptographic software written by Eric Young 55109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 56109998Smarkm * Hudson (tjh@cryptsoft.com). 57109998Smarkm * 58109998Smarkm */ 59109998Smarkm 60109998Smarkm#ifndef HEADER_UI_H 61296465Sdelphij# define HEADER_UI_H 62109998Smarkm 63296465Sdelphij# ifndef OPENSSL_NO_DEPRECATED 64296465Sdelphij# include <openssl/crypto.h> 65296465Sdelphij# endif 66296465Sdelphij# include <openssl/safestack.h> 67296465Sdelphij# include <openssl/ossl_typ.h> 68109998Smarkm 69109998Smarkm#ifdef __cplusplus 70109998Smarkmextern "C" { 71109998Smarkm#endif 72109998Smarkm 73160814Ssimon/* Declared already in ossl_typ.h */ 74160814Ssimon/* typedef struct ui_st UI; */ 75160814Ssimon/* typedef struct ui_method_st UI_METHOD; */ 76109998Smarkm 77296465Sdelphij/* 78296465Sdelphij * All the following functions return -1 or NULL on error and in some cases 79296465Sdelphij * (UI_process()) -2 if interrupted or in some other way cancelled. When 80296465Sdelphij * everything is fine, they return 0, a positive value or a non-NULL pointer, 81296465Sdelphij * all depending on their purpose. 82296465Sdelphij */ 83109998Smarkm 84109998Smarkm/* Creators and destructor. */ 85109998SmarkmUI *UI_new(void); 86109998SmarkmUI *UI_new_method(const UI_METHOD *method); 87109998Smarkmvoid UI_free(UI *ui); 88109998Smarkm 89296465Sdelphij/*- 90296465Sdelphij The following functions are used to add strings to be printed and prompt 91109998Smarkm strings to prompt for data. The names are UI_{add,dup}_<function>_string 92109998Smarkm and UI_{add,dup}_input_boolean. 93109998Smarkm 94109998Smarkm UI_{add,dup}_<function>_string have the following meanings: 95296465Sdelphij add add a text or prompt string. The pointers given to these 96296465Sdelphij functions are used verbatim, no copying is done. 97296465Sdelphij dup make a copy of the text or prompt string, then add the copy 98296465Sdelphij to the collection of strings in the user interface. 99296465Sdelphij <function> 100296465Sdelphij The function is a name for the functionality that the given 101296465Sdelphij string shall be used for. It can be one of: 102296465Sdelphij input use the string as data prompt. 103296465Sdelphij verify use the string as verification prompt. This 104296465Sdelphij is used to verify a previous input. 105296465Sdelphij info use the string for informational output. 106296465Sdelphij error use the string for error output. 107109998Smarkm Honestly, there's currently no difference between info and error for the 108109998Smarkm moment. 109109998Smarkm 110109998Smarkm UI_{add,dup}_input_boolean have the same semantics for "add" and "dup", 111109998Smarkm and are typically used when one wants to prompt for a yes/no response. 112109998Smarkm 113109998Smarkm All of the functions in this group take a UI and a prompt string. 114109998Smarkm The string input and verify addition functions also take a flag argument, 115109998Smarkm a buffer for the result to end up with, a minimum input size and a maximum 116109998Smarkm input size (the result buffer MUST be large enough to be able to contain 117109998Smarkm the maximum number of characters). Additionally, the verify addition 118109998Smarkm functions takes another buffer to compare the result against. 119109998Smarkm The boolean input functions take an action description string (which should 120109998Smarkm be safe to ignore if the expected user action is obvious, for example with 121109998Smarkm a dialog box with an OK button and a Cancel button), a string of acceptable 122109998Smarkm characters to mean OK and to mean Cancel. The two last strings are checked 123109998Smarkm to make sure they don't have common characters. Additionally, the same 124109998Smarkm flag argument as for the string input is taken, as well as a result buffer. 125109998Smarkm The result buffer is required to be at least one byte long. Depending on 126109998Smarkm the answer, the first character from the OK or the Cancel character strings 127109998Smarkm will be stored in the first byte of the result buffer. No NUL will be 128109998Smarkm added, so the result is *not* a string. 129109998Smarkm 130109998Smarkm On success, the all return an index of the added information. That index 131109998Smarkm is usefull when retrieving results with UI_get0_result(). */ 132109998Smarkmint UI_add_input_string(UI *ui, const char *prompt, int flags, 133296465Sdelphij char *result_buf, int minsize, int maxsize); 134109998Smarkmint UI_dup_input_string(UI *ui, const char *prompt, int flags, 135296465Sdelphij char *result_buf, int minsize, int maxsize); 136109998Smarkmint UI_add_verify_string(UI *ui, const char *prompt, int flags, 137296465Sdelphij char *result_buf, int minsize, int maxsize, 138296465Sdelphij const char *test_buf); 139109998Smarkmint UI_dup_verify_string(UI *ui, const char *prompt, int flags, 140296465Sdelphij char *result_buf, int minsize, int maxsize, 141296465Sdelphij const char *test_buf); 142109998Smarkmint UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc, 143296465Sdelphij const char *ok_chars, const char *cancel_chars, 144296465Sdelphij int flags, char *result_buf); 145109998Smarkmint UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc, 146296465Sdelphij const char *ok_chars, const char *cancel_chars, 147296465Sdelphij int flags, char *result_buf); 148109998Smarkmint UI_add_info_string(UI *ui, const char *text); 149109998Smarkmint UI_dup_info_string(UI *ui, const char *text); 150109998Smarkmint UI_add_error_string(UI *ui, const char *text); 151109998Smarkmint UI_dup_error_string(UI *ui, const char *text); 152109998Smarkm 153109998Smarkm/* These are the possible flags. They can be or'ed together. */ 154109998Smarkm/* Use to have echoing of input */ 155296465Sdelphij# define UI_INPUT_FLAG_ECHO 0x01 156296465Sdelphij/* 157296465Sdelphij * Use a default password. Where that password is found is completely up to 158296465Sdelphij * the application, it might for example be in the user data set with 159296465Sdelphij * UI_add_user_data(). It is not recommended to have more than one input in 160296465Sdelphij * each UI being marked with this flag, or the application might get 161296465Sdelphij * confused. 162296465Sdelphij */ 163296465Sdelphij# define UI_INPUT_FLAG_DEFAULT_PWD 0x02 164109998Smarkm 165296465Sdelphij/*- 166296465Sdelphij * The user of these routines may want to define flags of their own. The core 167296465Sdelphij * UI won't look at those, but will pass them on to the method routines. They 168296465Sdelphij * must use higher bits so they don't get confused with the UI bits above. 169296465Sdelphij * UI_INPUT_FLAG_USER_BASE tells which is the lowest bit to use. A good 170296465Sdelphij * example of use is this: 171296465Sdelphij * 172296465Sdelphij * #define MY_UI_FLAG1 (0x01 << UI_INPUT_FLAG_USER_BASE) 173296465Sdelphij * 174109998Smarkm*/ 175296465Sdelphij# define UI_INPUT_FLAG_USER_BASE 16 176109998Smarkm 177296465Sdelphij/*- 178296465Sdelphij * The following function helps construct a prompt. object_desc is a 179296465Sdelphij * textual short description of the object, for example "pass phrase", 180296465Sdelphij * and object_name is the name of the object (might be a card name or 181296465Sdelphij * a file name. 182296465Sdelphij * The returned string shall always be allocated on the heap with 183296465Sdelphij * OPENSSL_malloc(), and need to be free'd with OPENSSL_free(). 184296465Sdelphij * 185296465Sdelphij * If the ui_method doesn't contain a pointer to a user-defined prompt 186296465Sdelphij * constructor, a default string is built, looking like this: 187296465Sdelphij * 188296465Sdelphij * "Enter {object_desc} for {object_name}:" 189296465Sdelphij * 190296465Sdelphij * So, if object_desc has the value "pass phrase" and object_name has 191296465Sdelphij * the value "foo.key", the resulting string is: 192296465Sdelphij * 193296465Sdelphij * "Enter pass phrase for foo.key:" 194109998Smarkm*/ 195109998Smarkmchar *UI_construct_prompt(UI *ui_method, 196296465Sdelphij const char *object_desc, const char *object_name); 197109998Smarkm 198296465Sdelphij/* 199296465Sdelphij * The following function is used to store a pointer to user-specific data. 200296465Sdelphij * Any previous such pointer will be returned and replaced. 201296465Sdelphij * 202296465Sdelphij * For callback purposes, this function makes a lot more sense than using 203296465Sdelphij * ex_data, since the latter requires that different parts of OpenSSL or 204296465Sdelphij * applications share the same ex_data index. 205296465Sdelphij * 206296465Sdelphij * Note that the UI_OpenSSL() method completely ignores the user data. Other 207296465Sdelphij * methods may not, however. 208296465Sdelphij */ 209109998Smarkmvoid *UI_add_user_data(UI *ui, void *user_data); 210109998Smarkm/* We need a user data retrieving function as well. */ 211109998Smarkmvoid *UI_get0_user_data(UI *ui); 212109998Smarkm 213109998Smarkm/* Return the result associated with a prompt given with the index i. */ 214109998Smarkmconst char *UI_get0_result(UI *ui, int i); 215109998Smarkm 216109998Smarkm/* When all strings have been added, process the whole thing. */ 217109998Smarkmint UI_process(UI *ui); 218109998Smarkm 219296465Sdelphij/* 220296465Sdelphij * Give a user interface parametrised control commands. This can be used to 221296465Sdelphij * send down an integer, a data pointer or a function pointer, as well as be 222296465Sdelphij * used to get information from a UI. 223296465Sdelphij */ 224296465Sdelphijint UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void)); 225109998Smarkm 226109998Smarkm/* The commands */ 227296465Sdelphij/* 228296465Sdelphij * Use UI_CONTROL_PRINT_ERRORS with the value 1 to have UI_process print the 229296465Sdelphij * OpenSSL error stack before printing any info or added error messages and 230296465Sdelphij * before any prompting. 231296465Sdelphij */ 232296465Sdelphij# define UI_CTRL_PRINT_ERRORS 1 233296465Sdelphij/* 234296465Sdelphij * Check if a UI_process() is possible to do again with the same instance of 235296465Sdelphij * a user interface. This makes UI_ctrl() return 1 if it is redoable, and 0 236296465Sdelphij * if not. 237296465Sdelphij */ 238296465Sdelphij# define UI_CTRL_IS_REDOABLE 2 239109998Smarkm 240109998Smarkm/* Some methods may use extra data */ 241296465Sdelphij# define UI_set_app_data(s,arg) UI_set_ex_data(s,0,arg) 242296465Sdelphij# define UI_get_app_data(s) UI_get_ex_data(s,0) 243109998Smarkmint UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 244296465Sdelphij CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); 245296465Sdelphijint UI_set_ex_data(UI *r, int idx, void *arg); 246109998Smarkmvoid *UI_get_ex_data(UI *r, int idx); 247109998Smarkm 248109998Smarkm/* Use specific methods instead of the built-in one */ 249109998Smarkmvoid UI_set_default_method(const UI_METHOD *meth); 250109998Smarkmconst UI_METHOD *UI_get_default_method(void); 251109998Smarkmconst UI_METHOD *UI_get_method(UI *ui); 252109998Smarkmconst UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth); 253109998Smarkm 254109998Smarkm/* The method with all the built-in thingies */ 255109998SmarkmUI_METHOD *UI_OpenSSL(void); 256109998Smarkm 257109998Smarkm/* ---------- For method writers ---------- */ 258296465Sdelphij/*- 259296465Sdelphij A method contains a number of functions that implement the low level 260109998Smarkm of the User Interface. The functions are: 261109998Smarkm 262296465Sdelphij an opener This function starts a session, maybe by opening 263296465Sdelphij a channel to a tty, or by opening a window. 264296465Sdelphij a writer This function is called to write a given string, 265296465Sdelphij maybe to the tty, maybe as a field label in a 266296465Sdelphij window. 267296465Sdelphij a flusher This function is called to flush everything that 268296465Sdelphij has been output so far. It can be used to actually 269296465Sdelphij display a dialog box after it has been built. 270296465Sdelphij a reader This function is called to read a given prompt, 271296465Sdelphij maybe from the tty, maybe from a field in a 272296465Sdelphij window. Note that it's called wth all string 273296465Sdelphij structures, not only the prompt ones, so it must 274296465Sdelphij check such things itself. 275296465Sdelphij a closer This function closes the session, maybe by closing 276296465Sdelphij the channel to the tty, or closing the window. 277109998Smarkm 278109998Smarkm All these functions are expected to return: 279109998Smarkm 280296465Sdelphij 0 on error. 281296465Sdelphij 1 on success. 282296465Sdelphij -1 on out-of-band events, for example if some prompting has 283296465Sdelphij been canceled (by pressing Ctrl-C, for example). This is 284296465Sdelphij only checked when returned by the flusher or the reader. 285109998Smarkm 286109998Smarkm The way this is used, the opener is first called, then the writer for all 287109998Smarkm strings, then the flusher, then the reader for all strings and finally the 288109998Smarkm closer. Note that if you want to prompt from a terminal or other command 289109998Smarkm line interface, the best is to have the reader also write the prompts 290109998Smarkm instead of having the writer do it. If you want to prompt from a dialog 291109998Smarkm box, the writer can be used to build up the contents of the box, and the 292109998Smarkm flusher to actually display the box and run the event loop until all data 293109998Smarkm has been given, after which the reader only grabs the given data and puts 294109998Smarkm them back into the UI strings. 295109998Smarkm 296109998Smarkm All method functions take a UI as argument. Additionally, the writer and 297109998Smarkm the reader take a UI_STRING. 298109998Smarkm*/ 299109998Smarkm 300296465Sdelphij/* 301296465Sdelphij * The UI_STRING type is the data structure that contains all the needed info 302296465Sdelphij * about a string or a prompt, including test data for a verification prompt. 303296465Sdelphij */ 304109998SmarkmDECLARE_STACK_OF(UI_STRING) 305109998Smarkmtypedef struct ui_string_st UI_STRING; 306109998Smarkm 307296465Sdelphij/* 308296465Sdelphij * The different types of strings that are currently supported. This is only 309296465Sdelphij * needed by method authors. 310296465Sdelphij */ 311296465Sdelphijenum UI_string_types { 312296465Sdelphij UIT_NONE = 0, 313296465Sdelphij UIT_PROMPT, /* Prompt for a string */ 314296465Sdelphij UIT_VERIFY, /* Prompt for a string and verify */ 315296465Sdelphij UIT_BOOLEAN, /* Prompt for a yes/no response */ 316296465Sdelphij UIT_INFO, /* Send info to the user */ 317296465Sdelphij UIT_ERROR /* Send an error message to the user */ 318296465Sdelphij}; 319109998Smarkm 320109998Smarkm/* Create and manipulate methods */ 321109998SmarkmUI_METHOD *UI_create_method(char *name); 322109998Smarkmvoid UI_destroy_method(UI_METHOD *ui_method); 323296465Sdelphijint UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui)); 324296465Sdelphijint UI_method_set_writer(UI_METHOD *method, 325296465Sdelphij int (*writer) (UI *ui, UI_STRING *uis)); 326296465Sdelphijint UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui)); 327296465Sdelphijint UI_method_set_reader(UI_METHOD *method, 328296465Sdelphij int (*reader) (UI *ui, UI_STRING *uis)); 329296465Sdelphijint UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui)); 330296465Sdelphijint (*UI_method_get_opener(UI_METHOD *method)) (UI *); 331296465Sdelphijint (*UI_method_get_writer(UI_METHOD *method)) (UI *, UI_STRING *); 332296465Sdelphijint (*UI_method_get_flusher(UI_METHOD *method)) (UI *); 333296465Sdelphijint (*UI_method_get_reader(UI_METHOD *method)) (UI *, UI_STRING *); 334296465Sdelphijint (*UI_method_get_closer(UI_METHOD *method)) (UI *); 335109998Smarkm 336296465Sdelphij/* 337296465Sdelphij * The following functions are helpers for method writers to access relevant 338296465Sdelphij * data from a UI_STRING. 339296465Sdelphij */ 340109998Smarkm 341109998Smarkm/* Return type of the UI_STRING */ 342109998Smarkmenum UI_string_types UI_get_string_type(UI_STRING *uis); 343109998Smarkm/* Return input flags of the UI_STRING */ 344109998Smarkmint UI_get_input_flags(UI_STRING *uis); 345109998Smarkm/* Return the actual string to output (the prompt, info or error) */ 346109998Smarkmconst char *UI_get0_output_string(UI_STRING *uis); 347296465Sdelphij/* 348296465Sdelphij * Return the optional action string to output (the boolean promtp 349296465Sdelphij * instruction) 350296465Sdelphij */ 351109998Smarkmconst char *UI_get0_action_string(UI_STRING *uis); 352109998Smarkm/* Return the result of a prompt */ 353109998Smarkmconst char *UI_get0_result_string(UI_STRING *uis); 354296465Sdelphij/* 355296465Sdelphij * Return the string to test the result against. Only useful with verifies. 356296465Sdelphij */ 357109998Smarkmconst char *UI_get0_test_string(UI_STRING *uis); 358109998Smarkm/* Return the required minimum size of the result */ 359109998Smarkmint UI_get_result_minsize(UI_STRING *uis); 360109998Smarkm/* Return the required maximum size of the result */ 361109998Smarkmint UI_get_result_maxsize(UI_STRING *uis); 362109998Smarkm/* Set the result of a UI_STRING. */ 363109998Smarkmint UI_set_result(UI *ui, UI_STRING *uis, const char *result); 364109998Smarkm 365109998Smarkm/* A couple of popular utility functions */ 366296465Sdelphijint UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, 367296465Sdelphij int verify); 368296465Sdelphijint UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt, 369296465Sdelphij int verify); 370109998Smarkm 371109998Smarkm/* BEGIN ERROR CODES */ 372296465Sdelphij/* 373296465Sdelphij * The following lines are auto generated by the script mkerr.pl. Any changes 374109998Smarkm * made after this point may be overwritten when the script is next run. 375109998Smarkm */ 376109998Smarkmvoid ERR_load_UI_strings(void); 377109998Smarkm 378109998Smarkm/* Error codes for the UI functions. */ 379109998Smarkm 380109998Smarkm/* Function codes. */ 381296465Sdelphij# define UI_F_GENERAL_ALLOCATE_BOOLEAN 108 382296465Sdelphij# define UI_F_GENERAL_ALLOCATE_PROMPT 109 383296465Sdelphij# define UI_F_GENERAL_ALLOCATE_STRING 100 384296465Sdelphij# define UI_F_UI_CTRL 111 385296465Sdelphij# define UI_F_UI_DUP_ERROR_STRING 101 386296465Sdelphij# define UI_F_UI_DUP_INFO_STRING 102 387296465Sdelphij# define UI_F_UI_DUP_INPUT_BOOLEAN 110 388296465Sdelphij# define UI_F_UI_DUP_INPUT_STRING 103 389296465Sdelphij# define UI_F_UI_DUP_VERIFY_STRING 106 390296465Sdelphij# define UI_F_UI_GET0_RESULT 107 391296465Sdelphij# define UI_F_UI_NEW_METHOD 104 392296465Sdelphij# define UI_F_UI_SET_RESULT 105 393109998Smarkm 394109998Smarkm/* Reason codes. */ 395296465Sdelphij# define UI_R_COMMON_OK_AND_CANCEL_CHARACTERS 104 396296465Sdelphij# define UI_R_INDEX_TOO_LARGE 102 397296465Sdelphij# define UI_R_INDEX_TOO_SMALL 103 398296465Sdelphij# define UI_R_NO_RESULT_BUFFER 105 399296465Sdelphij# define UI_R_RESULT_TOO_LARGE 100 400296465Sdelphij# define UI_R_RESULT_TOO_SMALL 101 401296465Sdelphij# define UI_R_UNKNOWN_CONTROL_COMMAND 106 402109998Smarkm 403109998Smarkm#ifdef __cplusplus 404109998Smarkm} 405109998Smarkm#endif 406109998Smarkm#endif 407