1 /* crypto/ui/ui_lib.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Written by Richard Levitte (levitte@stacken.kth.se) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 #include <openssl/e_os2.h>
60 /* The following defines enable the declaration of strdup(), which is an
61 extended function according to X/Open. */
62 #ifdef OPENSSL_SYS_VMS_DECC
63 # define _XOPEN_SOURCE_EXTENDED
65 #ifdef OPENSSL_SYS_UNIX
66 # define _XOPEN_SOURCE
67 # define _XOPEN_SOURCE_EXTENDED /* For Linux and probably anything GNU */
71 #include <openssl/ui.h>
72 #include <openssl/err.h>
75 IMPLEMENT_STACK_OF(UI_STRING_ST)
77 static const UI_METHOD *default_UI_meth=NULL;
78 static int ui_meth_num=0;
79 static STACK_OF(CRYPTO_EX_DATA_FUNCS) *ui_meth=NULL;
83 return(UI_new_method(NULL));
86 UI *UI_new_method(const UI_METHOD *method)
90 ret=(UI *)OPENSSL_malloc(sizeof(UI));
93 UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
97 ret->meth=UI_get_default_method();
105 static void free_string(UI_STRING *uis)
107 if (uis->flags & OUT_STRING_FREEABLE)
108 OPENSSL_free((char *)uis->out_string);
114 sk_UI_STRING_pop_free(ui->strings,free_string);
118 static int allocate_string_stack(UI *ui)
120 if (ui->strings == NULL)
122 ui->strings=sk_UI_STRING_new_null();
123 if (ui->strings == NULL)
131 static int general_allocate_string(UI *ui, const char *prompt,
132 int prompt_freeable, enum UI_string_types type,
133 char *result_buf, int minsize, int maxsize, const char *test_buf)
139 UIerr(UI_F_GENERAL_ALLOCATE_STRING,ERR_R_PASSED_NULL_PARAMETER);
141 else if (allocate_string_stack(ui) >= 0)
143 UI_STRING *s=(UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING));
144 s->out_string=prompt;
145 s->flags=prompt_freeable ? OUT_STRING_FREEABLE : 0;
147 s->result_buf=result_buf;
148 s->result_minsize=minsize;
149 s->result_maxsize=maxsize;
150 s->test_buf=test_buf;
151 ret=sk_UI_STRING_push(ui->strings, s);
156 /* Returns the index to the place in the stack or 0 for error. Uses a
157 direct reference to the prompt. */
158 int UI_add_input_string(UI *ui, const char *prompt, int echo_p,
159 char *result_buf, int minsize, int maxsize)
161 return general_allocate_string(ui, prompt, 0,
162 echo_p?UI_STRING_ECHO:UI_STRING_NOECHO,
163 result_buf, minsize, maxsize, NULL);
166 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
167 int UI_dup_input_string(UI *ui, const char *prompt, int echo_p,
168 char *result_buf, int minsize, int maxsize)
170 char *prompt_copy=NULL;
174 prompt_copy=strdup(prompt);
175 if (prompt_copy == NULL)
177 UIerr(UI_F_UI_DUP_INPUT_STRING,ERR_R_MALLOC_FAILURE);
182 return general_allocate_string(ui, prompt, 1,
183 echo_p?UI_STRING_ECHO:UI_STRING_NOECHO,
184 result_buf, minsize, maxsize, NULL);
187 int UI_add_verify_string(UI *ui, const char *prompt, int echo_p,
188 char *result_buf, int minsize, int maxsize, const char *test_buf)
190 return general_allocate_string(ui, prompt, 0,
191 echo_p?UI_VERIFY_ECHO:UI_VERIFY_NOECHO,
192 result_buf, minsize, maxsize, test_buf);
195 int UI_dup_verify_string(UI *ui, const char *prompt, int echo_p,
196 char *result_buf, int minsize, int maxsize, const char *test_buf)
198 char *prompt_copy=NULL;
202 prompt_copy=strdup(prompt);
203 if (prompt_copy == NULL)
205 UIerr(UI_F_UI_DUP_VERIFY_STRING,ERR_R_MALLOC_FAILURE);
210 return general_allocate_string(ui, prompt, 1,
211 echo_p?UI_VERIFY_ECHO:UI_VERIFY_NOECHO,
212 result_buf, minsize, maxsize, test_buf);
215 int UI_add_info_string(UI *ui, const char *text)
217 return general_allocate_string(ui, text, 0, UI_INFO, NULL, 0, 0, NULL);
220 int UI_dup_info_string(UI *ui, const char *text)
222 char *text_copy=NULL;
226 text_copy=strdup(text);
227 if (text_copy == NULL)
229 UIerr(UI_F_UI_DUP_INFO_STRING,ERR_R_MALLOC_FAILURE);
234 return general_allocate_string(ui, text, 1, UI_INFO, NULL, 0, 0, NULL);
237 int UI_add_error_string(UI *ui, const char *text)
239 return general_allocate_string(ui, text, 0, UI_ERROR, NULL, 0, 0,
243 int UI_dup_error_string(UI *ui, const char *text)
245 char *text_copy=NULL;
249 text_copy=strdup(text);
250 if (text_copy == NULL)
252 UIerr(UI_F_UI_DUP_ERROR_STRING,ERR_R_MALLOC_FAILURE);
256 return general_allocate_string(ui, text_copy, 1, UI_ERROR, NULL, 0, 0,
260 void *UI_add_user_data(UI *ui, void *user_data)
262 void *old_data = ui->user_data;
263 ui->user_data = user_data;
267 void *UI_get0_user_data(UI *ui)
269 return ui->user_data;
272 const char *UI_get0_result(UI *ui, int i)
276 UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_SMALL);
279 if (i >= sk_UI_STRING_num(ui->strings))
281 UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_LARGE);
284 return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
287 int UI_process(UI *ui)
291 if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
294 for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
296 if (ui->meth->ui_write_string
297 && !ui->meth->ui_write_string(ui,
298 sk_UI_STRING_value(ui->strings, i)))
305 for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
307 if (ui->meth->ui_read_string
308 && !ui->meth->ui_read_string(ui,
309 sk_UI_STRING_value(ui->strings, i)))
316 if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
321 int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
322 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
325 return(CRYPTO_get_ex_new_index(ui_meth_num-1,
326 &ui_meth,argl,argp,new_func,dup_func,free_func));
329 int UI_set_ex_data(UI *r, int idx, void *arg)
331 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
334 void *UI_get_ex_data(UI *r, int idx)
336 return(CRYPTO_get_ex_data(&r->ex_data,idx));
339 void UI_set_default_method(const UI_METHOD *meth)
341 default_UI_meth=meth;
344 const UI_METHOD *UI_get_default_method(void)
346 if (default_UI_meth == NULL)
348 default_UI_meth=UI_OpenSSL();
350 return default_UI_meth;
353 const UI_METHOD *UI_get_method(UI *ui)
358 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
365 UI_METHOD *UI_create_method(void)
367 return (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
370 int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui))
374 method->ui_open_session = opener;
381 int UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis))
385 method->ui_write_string = writer;
392 int UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis))
396 method->ui_read_string = reader;
403 int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
407 method->ui_close_session = closer;
414 int (*UI_method_get_opener(UI_METHOD *method))(UI*)
417 return method->ui_open_session;
422 int (*UI_method_get_writer(UI_METHOD *method))(UI*,UI_STRING*)
425 return method->ui_write_string;
430 int (*UI_method_get_reader(UI_METHOD *method))(UI*,UI_STRING*)
433 return method->ui_read_string;
438 int (*UI_method_get_closer(UI_METHOD *method))(UI*)
441 return method->ui_close_session;
446 enum UI_string_types UI_get_string_type(UI_STRING *uis)
453 const char *UI_get0_output_string(UI_STRING *uis)
457 return uis->out_string;
460 const char *UI_get0_result_string(UI_STRING *uis)
467 case UI_STRING_NOECHO:
469 case UI_VERIFY_NOECHO:
470 return uis->result_buf;
476 const char *UI_get0_test_string(UI_STRING *uis)
480 return uis->test_buf;
483 int UI_get_result_minsize(UI_STRING *uis)
487 return uis->result_minsize;
490 int UI_get_result_maxsize(UI_STRING *uis)
494 return uis->result_maxsize;
497 int UI_set_result(UI_STRING *uis, char *result)
499 int l = strlen(result);
503 if (l < uis->result_minsize)
505 UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_SMALL);
508 if (l > uis->result_maxsize)
510 UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_LARGE);
514 if (!uis->result_buf)
516 uis->result_buf = OPENSSL_malloc(uis->result_maxsize+1);
519 if (!uis->result_buf)
521 UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
525 strcpy(uis->result_buf, result);