1295016Sjkim/* crypto/ui/ui.h */ 2280304Sjkim/* 3280304Sjkim * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project 4280304Sjkim * 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 14280304Sjkim * 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 61280304Sjkim# define HEADER_UI_H 62109998Smarkm 63280304Sjkim# ifndef OPENSSL_NO_DEPRECATED 64280304Sjkim# include <openssl/crypto.h> 65280304Sjkim# endif 66280304Sjkim# include <openssl/safestack.h> 67280304Sjkim# 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 77280304Sjkim/* 78280304Sjkim * All the following functions return -1 or NULL on error and in some cases 79280304Sjkim * (UI_process()) -2 if interrupted or in some other way cancelled. When 80280304Sjkim * everything is fine, they return 0, a positive value or a non-NULL pointer, 81280304Sjkim * all depending on their purpose. 82280304Sjkim */ 83109998Smarkm 84109998Smarkm/* Creators and destructor. */ 85109998SmarkmUI *UI_new(void); 86109998SmarkmUI *UI_new_method(const UI_METHOD *method); 87109998Smarkmvoid UI_free(UI *ui); 88109998Smarkm 89280304Sjkim/*- 90280304Sjkim 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: 95280304Sjkim add add a text or prompt string. The pointers given to these 96280304Sjkim functions are used verbatim, no copying is done. 97280304Sjkim dup make a copy of the text or prompt string, then add the copy 98280304Sjkim to the collection of strings in the user interface. 99280304Sjkim <function> 100280304Sjkim The function is a name for the functionality that the given 101280304Sjkim string shall be used for. It can be one of: 102280304Sjkim input use the string as data prompt. 103280304Sjkim verify use the string as verification prompt. This 104280304Sjkim is used to verify a previous input. 105280304Sjkim info use the string for informational output. 106280304Sjkim 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, 133280304Sjkim char *result_buf, int minsize, int maxsize); 134109998Smarkmint UI_dup_input_string(UI *ui, const char *prompt, int flags, 135280304Sjkim char *result_buf, int minsize, int maxsize); 136109998Smarkmint UI_add_verify_string(UI *ui, const char *prompt, int flags, 137280304Sjkim char *result_buf, int minsize, int maxsize, 138280304Sjkim const char *test_buf); 139109998Smarkmint UI_dup_verify_string(UI *ui, const char *prompt, int flags, 140280304Sjkim char *result_buf, int minsize, int maxsize, 141280304Sjkim const char *test_buf); 142109998Smarkmint UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc, 143280304Sjkim const char *ok_chars, const char *cancel_chars, 144280304Sjkim int flags, char *result_buf); 145109998Smarkmint UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc, 146280304Sjkim const char *ok_chars, const char *cancel_chars, 147280304Sjkim 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 */ 155280304Sjkim# define UI_INPUT_FLAG_ECHO 0x01 156280304Sjkim/* 157280304Sjkim * Use a default password. Where that password is found is completely up to 158280304Sjkim * the application, it might for example be in the user data set with 159280304Sjkim * UI_add_user_data(). It is not recommended to have more than one input in 160280304Sjkim * each UI being marked with this flag, or the application might get 161280304Sjkim * confused. 162280304Sjkim */ 163280304Sjkim# define UI_INPUT_FLAG_DEFAULT_PWD 0x02 164109998Smarkm 165280304Sjkim/*- 166280304Sjkim * The user of these routines may want to define flags of their own. The core 167280304Sjkim * UI won't look at those, but will pass them on to the method routines. They 168280304Sjkim * must use higher bits so they don't get confused with the UI bits above. 169280304Sjkim * UI_INPUT_FLAG_USER_BASE tells which is the lowest bit to use. A good 170280304Sjkim * example of use is this: 171280304Sjkim * 172280304Sjkim * #define MY_UI_FLAG1 (0x01 << UI_INPUT_FLAG_USER_BASE) 173280304Sjkim * 174109998Smarkm*/ 175280304Sjkim# define UI_INPUT_FLAG_USER_BASE 16 176109998Smarkm 177280304Sjkim/*- 178280304Sjkim * The following function helps construct a prompt. object_desc is a 179280304Sjkim * textual short description of the object, for example "pass phrase", 180280304Sjkim * and object_name is the name of the object (might be a card name or 181280304Sjkim * a file name. 182280304Sjkim * The returned string shall always be allocated on the heap with 183280304Sjkim * OPENSSL_malloc(), and need to be free'd with OPENSSL_free(). 184280304Sjkim * 185280304Sjkim * If the ui_method doesn't contain a pointer to a user-defined prompt 186280304Sjkim * constructor, a default string is built, looking like this: 187280304Sjkim * 188280304Sjkim * "Enter {object_desc} for {object_name}:" 189280304Sjkim * 190280304Sjkim * So, if object_desc has the value "pass phrase" and object_name has 191280304Sjkim * the value "foo.key", the resulting string is: 192280304Sjkim * 193280304Sjkim * "Enter pass phrase for foo.key:" 194109998Smarkm*/ 195109998Smarkmchar *UI_construct_prompt(UI *ui_method, 196280304Sjkim const char *object_desc, const char *object_name); 197109998Smarkm 198280304Sjkim/* 199280304Sjkim * The following function is used to store a pointer to user-specific data. 200280304Sjkim * Any previous such pointer will be returned and replaced. 201280304Sjkim * 202280304Sjkim * For callback purposes, this function makes a lot more sense than using 203280304Sjkim * ex_data, since the latter requires that different parts of OpenSSL or 204280304Sjkim * applications share the same ex_data index. 205280304Sjkim * 206280304Sjkim * Note that the UI_OpenSSL() method completely ignores the user data. Other 207280304Sjkim * methods may not, however. 208280304Sjkim */ 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 219280304Sjkim/* 220280304Sjkim * Give a user interface parametrised control commands. This can be used to 221280304Sjkim * send down an integer, a data pointer or a function pointer, as well as be 222280304Sjkim * used to get information from a UI. 223280304Sjkim */ 224280304Sjkimint UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void)); 225109998Smarkm 226109998Smarkm/* The commands */ 227280304Sjkim/* 228280304Sjkim * Use UI_CONTROL_PRINT_ERRORS with the value 1 to have UI_process print the 229280304Sjkim * OpenSSL error stack before printing any info or added error messages and 230280304Sjkim * before any prompting. 231280304Sjkim */ 232280304Sjkim# define UI_CTRL_PRINT_ERRORS 1 233280304Sjkim/* 234280304Sjkim * Check if a UI_process() is possible to do again with the same instance of 235280304Sjkim * a user interface. This makes UI_ctrl() return 1 if it is redoable, and 0 236280304Sjkim * if not. 237280304Sjkim */ 238280304Sjkim# define UI_CTRL_IS_REDOABLE 2 239109998Smarkm 240109998Smarkm/* Some methods may use extra data */ 241280304Sjkim# define UI_set_app_data(s,arg) UI_set_ex_data(s,0,arg) 242280304Sjkim# 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, 244280304Sjkim CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); 245280304Sjkimint 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 ---------- */ 258280304Sjkim/*- 259280304Sjkim A method contains a number of functions that implement the low level 260109998Smarkm of the User Interface. The functions are: 261109998Smarkm 262280304Sjkim an opener This function starts a session, maybe by opening 263280304Sjkim a channel to a tty, or by opening a window. 264280304Sjkim a writer This function is called to write a given string, 265280304Sjkim maybe to the tty, maybe as a field label in a 266280304Sjkim window. 267280304Sjkim a flusher This function is called to flush everything that 268280304Sjkim has been output so far. It can be used to actually 269280304Sjkim display a dialog box after it has been built. 270280304Sjkim a reader This function is called to read a given prompt, 271280304Sjkim maybe from the tty, maybe from a field in a 272280304Sjkim window. Note that it's called wth all string 273280304Sjkim structures, not only the prompt ones, so it must 274280304Sjkim check such things itself. 275280304Sjkim a closer This function closes the session, maybe by closing 276280304Sjkim the channel to the tty, or closing the window. 277109998Smarkm 278109998Smarkm All these functions are expected to return: 279109998Smarkm 280280304Sjkim 0 on error. 281280304Sjkim 1 on success. 282280304Sjkim -1 on out-of-band events, for example if some prompting has 283280304Sjkim been canceled (by pressing Ctrl-C, for example). This is 284280304Sjkim 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 300280304Sjkim/* 301280304Sjkim * The UI_STRING type is the data structure that contains all the needed info 302280304Sjkim * about a string or a prompt, including test data for a verification prompt. 303280304Sjkim */ 304238405Sjkimtypedef struct ui_string_st UI_STRING; 305109998SmarkmDECLARE_STACK_OF(UI_STRING) 306109998Smarkm 307280304Sjkim/* 308280304Sjkim * The different types of strings that are currently supported. This is only 309280304Sjkim * needed by method authors. 310280304Sjkim */ 311280304Sjkimenum UI_string_types { 312280304Sjkim UIT_NONE = 0, 313280304Sjkim UIT_PROMPT, /* Prompt for a string */ 314280304Sjkim UIT_VERIFY, /* Prompt for a string and verify */ 315280304Sjkim UIT_BOOLEAN, /* Prompt for a yes/no response */ 316280304Sjkim UIT_INFO, /* Send info to the user */ 317280304Sjkim UIT_ERROR /* Send an error message to the user */ 318280304Sjkim}; 319109998Smarkm 320109998Smarkm/* Create and manipulate methods */ 321109998SmarkmUI_METHOD *UI_create_method(char *name); 322109998Smarkmvoid UI_destroy_method(UI_METHOD *ui_method); 323280304Sjkimint UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui)); 324280304Sjkimint UI_method_set_writer(UI_METHOD *method, 325280304Sjkim int (*writer) (UI *ui, UI_STRING *uis)); 326280304Sjkimint UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui)); 327280304Sjkimint UI_method_set_reader(UI_METHOD *method, 328280304Sjkim int (*reader) (UI *ui, UI_STRING *uis)); 329280304Sjkimint UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui)); 330280304Sjkimint UI_method_set_prompt_constructor(UI_METHOD *method, 331280304Sjkim char *(*prompt_constructor) (UI *ui, 332280304Sjkim const char 333280304Sjkim *object_desc, 334280304Sjkim const char 335280304Sjkim *object_name)); 336280304Sjkimint (*UI_method_get_opener(UI_METHOD *method)) (UI *); 337280304Sjkimint (*UI_method_get_writer(UI_METHOD *method)) (UI *, UI_STRING *); 338280304Sjkimint (*UI_method_get_flusher(UI_METHOD *method)) (UI *); 339280304Sjkimint (*UI_method_get_reader(UI_METHOD *method)) (UI *, UI_STRING *); 340280304Sjkimint (*UI_method_get_closer(UI_METHOD *method)) (UI *); 341280304Sjkimchar *(*UI_method_get_prompt_constructor(UI_METHOD *method)) (UI *, 342280304Sjkim const char *, 343280304Sjkim const char *); 344109998Smarkm 345280304Sjkim/* 346280304Sjkim * The following functions are helpers for method writers to access relevant 347280304Sjkim * data from a UI_STRING. 348280304Sjkim */ 349109998Smarkm 350109998Smarkm/* Return type of the UI_STRING */ 351109998Smarkmenum UI_string_types UI_get_string_type(UI_STRING *uis); 352109998Smarkm/* Return input flags of the UI_STRING */ 353109998Smarkmint UI_get_input_flags(UI_STRING *uis); 354109998Smarkm/* Return the actual string to output (the prompt, info or error) */ 355109998Smarkmconst char *UI_get0_output_string(UI_STRING *uis); 356280304Sjkim/* 357280304Sjkim * Return the optional action string to output (the boolean promtp 358280304Sjkim * instruction) 359280304Sjkim */ 360109998Smarkmconst char *UI_get0_action_string(UI_STRING *uis); 361109998Smarkm/* Return the result of a prompt */ 362109998Smarkmconst char *UI_get0_result_string(UI_STRING *uis); 363280304Sjkim/* 364280304Sjkim * Return the string to test the result against. Only useful with verifies. 365280304Sjkim */ 366109998Smarkmconst char *UI_get0_test_string(UI_STRING *uis); 367109998Smarkm/* Return the required minimum size of the result */ 368109998Smarkmint UI_get_result_minsize(UI_STRING *uis); 369109998Smarkm/* Return the required maximum size of the result */ 370109998Smarkmint UI_get_result_maxsize(UI_STRING *uis); 371109998Smarkm/* Set the result of a UI_STRING. */ 372109998Smarkmint UI_set_result(UI *ui, UI_STRING *uis, const char *result); 373109998Smarkm 374109998Smarkm/* A couple of popular utility functions */ 375280304Sjkimint UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, 376280304Sjkim int verify); 377280304Sjkimint UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt, 378280304Sjkim int verify); 379109998Smarkm 380109998Smarkm/* BEGIN ERROR CODES */ 381280304Sjkim/* 382280304Sjkim * The following lines are auto generated by the script mkerr.pl. Any changes 383109998Smarkm * made after this point may be overwritten when the script is next run. 384109998Smarkm */ 385109998Smarkmvoid ERR_load_UI_strings(void); 386109998Smarkm 387109998Smarkm/* Error codes for the UI functions. */ 388109998Smarkm 389109998Smarkm/* Function codes. */ 390280304Sjkim# define UI_F_GENERAL_ALLOCATE_BOOLEAN 108 391280304Sjkim# define UI_F_GENERAL_ALLOCATE_PROMPT 109 392280304Sjkim# define UI_F_GENERAL_ALLOCATE_STRING 100 393280304Sjkim# define UI_F_UI_CTRL 111 394280304Sjkim# define UI_F_UI_DUP_ERROR_STRING 101 395280304Sjkim# define UI_F_UI_DUP_INFO_STRING 102 396280304Sjkim# define UI_F_UI_DUP_INPUT_BOOLEAN 110 397280304Sjkim# define UI_F_UI_DUP_INPUT_STRING 103 398280304Sjkim# define UI_F_UI_DUP_VERIFY_STRING 106 399280304Sjkim# define UI_F_UI_GET0_RESULT 107 400280304Sjkim# define UI_F_UI_NEW_METHOD 104 401280304Sjkim# define UI_F_UI_SET_RESULT 105 402109998Smarkm 403109998Smarkm/* Reason codes. */ 404280304Sjkim# define UI_R_COMMON_OK_AND_CANCEL_CHARACTERS 104 405280304Sjkim# define UI_R_INDEX_TOO_LARGE 102 406280304Sjkim# define UI_R_INDEX_TOO_SMALL 103 407280304Sjkim# define UI_R_NO_RESULT_BUFFER 105 408280304Sjkim# define UI_R_RESULT_TOO_LARGE 100 409280304Sjkim# define UI_R_RESULT_TOO_SMALL 101 410280304Sjkim# define UI_R_UNKNOWN_CONTROL_COMMAND 106 411109998Smarkm 412109998Smarkm#ifdef __cplusplus 413109998Smarkm} 414109998Smarkm#endif 415109998Smarkm#endif 416