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).
60 #include <openssl/e_os2.h>
61 #include <openssl/buffer.h>
62 #include <openssl/ui.h>
63 #include <openssl/err.h>
66 IMPLEMENT_STACK_OF(UI_STRING_ST)
68 static const UI_METHOD *default_UI_meth=NULL;
69 static int ui_meth_num=0;
70 static STACK_OF(CRYPTO_EX_DATA_FUNCS) *ui_meth=NULL;
74 return(UI_new_method(NULL));
77 UI *UI_new_method(const UI_METHOD *method)
81 ret=(UI *)OPENSSL_malloc(sizeof(UI));
84 UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
88 ret->meth=UI_get_default_method();
94 CRYPTO_new_ex_data(ui_meth,ret,&ret->ex_data);
98 static void free_string(UI_STRING *uis)
100 if (uis->flags & OUT_STRING_FREEABLE)
101 OPENSSL_free((char *)uis->out_string);
107 sk_UI_STRING_pop_free(ui->strings,free_string);
111 static int allocate_string_stack(UI *ui)
113 if (ui->strings == NULL)
115 ui->strings=sk_UI_STRING_new_null();
116 if (ui->strings == NULL)
124 static int general_allocate_string(UI *ui, const char *prompt,
125 int prompt_freeable, enum UI_string_types type, int input_flags,
126 char *result_buf, int minsize, int maxsize, const char *test_buf)
132 UIerr(UI_F_GENERAL_ALLOCATE_STRING,ERR_R_PASSED_NULL_PARAMETER);
134 else if (allocate_string_stack(ui) >= 0)
136 UI_STRING *s=(UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING));
137 s->out_string=prompt;
138 s->flags=prompt_freeable ? OUT_STRING_FREEABLE : 0;
139 s->input_flags=input_flags;
141 s->result_buf=result_buf;
142 s->result_minsize=minsize;
143 s->result_maxsize=maxsize;
144 s->test_buf=test_buf;
145 ret=sk_UI_STRING_push(ui->strings, s);
150 /* Returns the index to the place in the stack or 0 for error. Uses a
151 direct reference to the prompt. */
152 int UI_add_input_string(UI *ui, const char *prompt, int flags,
153 char *result_buf, int minsize, int maxsize)
155 return general_allocate_string(ui, prompt, 0,
156 UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL);
159 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
160 int UI_dup_input_string(UI *ui, const char *prompt, int flags,
161 char *result_buf, int minsize, int maxsize)
163 char *prompt_copy=NULL;
167 prompt_copy=BUF_strdup(prompt);
168 if (prompt_copy == NULL)
170 UIerr(UI_F_UI_DUP_INPUT_STRING,ERR_R_MALLOC_FAILURE);
175 return general_allocate_string(ui, prompt, 1,
176 UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL);
179 int UI_add_verify_string(UI *ui, const char *prompt, int flags,
180 char *result_buf, int minsize, int maxsize, const char *test_buf)
182 return general_allocate_string(ui, prompt, 0,
183 UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf);
186 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
187 char *result_buf, int minsize, int maxsize, const char *test_buf)
189 char *prompt_copy=NULL;
193 prompt_copy=BUF_strdup(prompt);
194 if (prompt_copy == NULL)
196 UIerr(UI_F_UI_DUP_VERIFY_STRING,ERR_R_MALLOC_FAILURE);
201 return general_allocate_string(ui, prompt, 1,
202 UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf);
205 int UI_add_info_string(UI *ui, const char *text)
207 return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
211 int UI_dup_info_string(UI *ui, const char *text)
213 char *text_copy=NULL;
217 text_copy=BUF_strdup(text);
218 if (text_copy == NULL)
220 UIerr(UI_F_UI_DUP_INFO_STRING,ERR_R_MALLOC_FAILURE);
225 return general_allocate_string(ui, text, 1, UIT_INFO, 0, NULL, 0, 0,
229 int UI_add_error_string(UI *ui, const char *text)
231 return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
235 int UI_dup_error_string(UI *ui, const char *text)
237 char *text_copy=NULL;
241 text_copy=BUF_strdup(text);
242 if (text_copy == NULL)
244 UIerr(UI_F_UI_DUP_ERROR_STRING,ERR_R_MALLOC_FAILURE);
248 return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
252 char *UI_construct_prompt(UI *ui, const char *object_desc,
253 const char *object_name)
257 if (ui->meth->ui_construct_prompt)
258 prompt = ui->meth->ui_construct_prompt(ui,
259 object_desc, object_name);
262 char prompt1[] = "Enter ";
263 char prompt2[] = " for ";
264 char prompt3[] = ":";
267 if (object_desc == NULL)
269 len = sizeof(prompt1) - 1 + strlen(object_desc);
271 len += sizeof(prompt2) - 1 + strlen(object_name);
272 len += sizeof(prompt3) - 1;
274 prompt = (char *)OPENSSL_malloc(len + 1);
275 strcpy(prompt, prompt1);
276 strcat(prompt, object_desc);
279 strcat(prompt, prompt2);
280 strcat(prompt, object_name);
282 strcat(prompt, prompt3);
287 void *UI_add_user_data(UI *ui, void *user_data)
289 void *old_data = ui->user_data;
290 ui->user_data = user_data;
294 void *UI_get0_user_data(UI *ui)
296 return ui->user_data;
299 const char *UI_get0_result(UI *ui, int i)
303 UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_SMALL);
306 if (i >= sk_UI_STRING_num(ui->strings))
308 UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_LARGE);
311 return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
314 int UI_process(UI *ui)
318 if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
321 for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
323 if (ui->meth->ui_write_string
324 && !ui->meth->ui_write_string(ui,
325 sk_UI_STRING_value(ui->strings, i)))
332 if (ui->meth->ui_flush)
333 switch(ui->meth->ui_flush(ui))
335 case -1: /* Interrupt/Cancel/something... */
341 default: /* Success */
346 for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
348 if (ui->meth->ui_read_string)
350 switch(ui->meth->ui_read_string(ui,
351 sk_UI_STRING_value(ui->strings, i)))
353 case -1: /* Interrupt/Cancel/something... */
359 default: /* Success */
366 if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
371 int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
372 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
375 return(CRYPTO_get_ex_new_index(ui_meth_num-1,
376 &ui_meth,argl,argp,new_func,dup_func,free_func));
379 int UI_set_ex_data(UI *r, int idx, void *arg)
381 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
384 void *UI_get_ex_data(UI *r, int idx)
386 return(CRYPTO_get_ex_data(&r->ex_data,idx));
389 void UI_set_default_method(const UI_METHOD *meth)
391 default_UI_meth=meth;
394 const UI_METHOD *UI_get_default_method(void)
396 if (default_UI_meth == NULL)
398 default_UI_meth=UI_OpenSSL();
400 return default_UI_meth;
403 const UI_METHOD *UI_get_method(UI *ui)
408 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
415 UI_METHOD *UI_create_method(char *name)
417 UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
420 memset(ui_method, 0, sizeof(*ui_method));
421 ui_method->name = BUF_strdup(name);
425 /* BIG FSCKING WARNING!!!! If you use this on a statically allocated method
426 (that is, it hasn't been allocated using UI_create_method(), you deserve
427 anything Murphy can throw at you and more! You have been warned. */
428 void UI_destroy_method(UI_METHOD *ui_method)
430 OPENSSL_free(ui_method->name);
431 ui_method->name = NULL;
432 OPENSSL_free(ui_method);
435 int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui))
439 method->ui_open_session = opener;
446 int UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis))
450 method->ui_write_string = writer;
457 int UI_method_set_flusher(UI_METHOD *method, int (*flusher)(UI *ui))
461 method->ui_flush = flusher;
468 int UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis))
472 method->ui_read_string = reader;
479 int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
483 method->ui_close_session = closer;
490 int (*UI_method_get_opener(UI_METHOD *method))(UI*)
493 return method->ui_open_session;
498 int (*UI_method_get_writer(UI_METHOD *method))(UI*,UI_STRING*)
501 return method->ui_write_string;
506 int (*UI_method_get_reader(UI_METHOD *method))(UI*,UI_STRING*)
509 return method->ui_read_string;
514 int (*UI_method_get_closer(UI_METHOD *method))(UI*)
517 return method->ui_close_session;
522 enum UI_string_types UI_get_string_type(UI_STRING *uis)
529 int UI_get_input_flags(UI_STRING *uis)
533 return uis->input_flags;
536 const char *UI_get0_output_string(UI_STRING *uis)
540 return uis->out_string;
543 const char *UI_get0_result_string(UI_STRING *uis)
551 return uis->result_buf;
557 const char *UI_get0_test_string(UI_STRING *uis)
561 return uis->test_buf;
564 int UI_get_result_minsize(UI_STRING *uis)
568 return uis->result_minsize;
571 int UI_get_result_maxsize(UI_STRING *uis)
575 return uis->result_maxsize;
578 int UI_set_result(UI_STRING *uis, const char *result)
580 int l = strlen(result);
584 if (l < uis->result_minsize)
586 UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_SMALL);
589 if (l > uis->result_maxsize)
591 UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_LARGE);
595 if (!uis->result_buf)
597 uis->result_buf = OPENSSL_malloc(uis->result_maxsize+1);
600 if (!uis->result_buf)
602 UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
606 strcpy(uis->result_buf, result);