Enhance the user interface with better support for dialog box
[openssl.git] / crypto / ui / ui_lib.c
1 /* crypto/ui/ui_lib.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Written by Richard Levitte (levitte@stacken.kth.se) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58
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
64 #endif
65 #ifdef OPENSSL_SYS_UNIX
66 # define _XOPEN_SOURCE
67 # define _XOPEN_SOURCE_EXTENDED /* For Linux and probably anything GNU */
68 #endif
69 #include <string.h>
70
71 #include <openssl/ui.h>
72 #include <openssl/err.h>
73 #include "ui_locl.h"
74
75 IMPLEMENT_STACK_OF(UI_STRING_ST)
76
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;
80
81 UI *UI_new(void)
82         {
83         return(UI_new_method(NULL));
84         }
85
86 UI *UI_new_method(const UI_METHOD *method)
87         {
88         UI *ret;
89
90         ret=(UI *)OPENSSL_malloc(sizeof(UI));
91         if (ret == NULL)
92                 {
93                 UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
94                 return NULL;
95                 }
96         if (method == NULL)
97                 ret->meth=UI_get_default_method();
98         else
99                 ret->meth=method;
100
101         ret->strings=NULL;
102         ret->user_data=NULL;
103         CRYPTO_new_ex_data(ui_meth,ret,&ret->ex_data);
104         return ret;
105         }
106
107 static void free_string(UI_STRING *uis)
108         {
109         if (uis->flags & OUT_STRING_FREEABLE)
110                 OPENSSL_free((char *)uis->out_string);
111         OPENSSL_free(uis);
112         }
113
114 void UI_free(UI *ui)
115         {
116         sk_UI_STRING_pop_free(ui->strings,free_string);
117         OPENSSL_free(ui);
118         }
119
120 static int allocate_string_stack(UI *ui)
121         {
122         if (ui->strings == NULL)
123                 {
124                 ui->strings=sk_UI_STRING_new_null();
125                 if (ui->strings == NULL)
126                         {
127                         return -1;
128                         }
129                 }
130         return 0;
131         }
132
133 static int general_allocate_string(UI *ui, const char *prompt,
134         int prompt_freeable, enum UI_string_types type, int input_flags,
135         char *result_buf, int minsize, int maxsize, const char *test_buf)
136         {
137         int ret=-1;
138
139         if (prompt == NULL)
140                 {
141                 UIerr(UI_F_GENERAL_ALLOCATE_STRING,ERR_R_PASSED_NULL_PARAMETER);
142                 }
143         else if (allocate_string_stack(ui) >= 0)
144                 {
145                 UI_STRING *s=(UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING));
146                 s->out_string=prompt;
147                 s->flags=prompt_freeable ? OUT_STRING_FREEABLE : 0;
148                 s->input_flags=input_flags;
149                 s->type=type;
150                 s->result_buf=result_buf;
151                 s->result_minsize=minsize;
152                 s->result_maxsize=maxsize;
153                 s->test_buf=test_buf;
154                 ret=sk_UI_STRING_push(ui->strings, s);
155                 }
156         return ret;
157         }
158
159 /* Returns the index to the place in the stack or 0 for error.  Uses a
160    direct reference to the prompt.  */
161 int UI_add_input_string(UI *ui, const char *prompt, int flags,
162         char *result_buf, int minsize, int maxsize)
163         {
164         return general_allocate_string(ui, prompt, 0,
165                 UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL);
166         }
167
168 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
169 int UI_dup_input_string(UI *ui, const char *prompt, int flags,
170         char *result_buf, int minsize, int maxsize)
171         {
172         char *prompt_copy=NULL;
173
174         if (prompt)
175                 {
176                 prompt_copy=strdup(prompt);
177                 if (prompt_copy == NULL)
178                         {
179                         UIerr(UI_F_UI_DUP_INPUT_STRING,ERR_R_MALLOC_FAILURE);
180                         return 0;
181                         }
182                 }
183         
184         return general_allocate_string(ui, prompt, 1,
185                 UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL);
186         }
187
188 int UI_add_verify_string(UI *ui, const char *prompt, int flags,
189         char *result_buf, int minsize, int maxsize, const char *test_buf)
190         {
191         return general_allocate_string(ui, prompt, 0,
192                 UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf);
193         }
194
195 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
196         char *result_buf, int minsize, int maxsize, const char *test_buf)
197         {
198         char *prompt_copy=NULL;
199
200         if (prompt)
201                 {
202                 prompt_copy=strdup(prompt);
203                 if (prompt_copy == NULL)
204                         {
205                         UIerr(UI_F_UI_DUP_VERIFY_STRING,ERR_R_MALLOC_FAILURE);
206                         return -1;
207                         }
208                 }
209         
210         return general_allocate_string(ui, prompt, 1,
211                 UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf);
212         }
213
214 int UI_add_info_string(UI *ui, const char *text)
215         {
216         return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
217                 NULL);
218         }
219
220 int UI_dup_info_string(UI *ui, const char *text)
221         {
222         char *text_copy=NULL;
223
224         if (text)
225                 {
226                 text_copy=strdup(text);
227                 if (text_copy == NULL)
228                         {
229                         UIerr(UI_F_UI_DUP_INFO_STRING,ERR_R_MALLOC_FAILURE);
230                         return -1;
231                         }
232                 }
233
234         return general_allocate_string(ui, text, 1, UIT_INFO, 0, NULL, 0, 0,
235                 NULL);
236         }
237
238 int UI_add_error_string(UI *ui, const char *text)
239         {
240         return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
241                 NULL);
242         }
243
244 int UI_dup_error_string(UI *ui, const char *text)
245         {
246         char *text_copy=NULL;
247
248         if (text)
249                 {
250                 text_copy=strdup(text);
251                 if (text_copy == NULL)
252                         {
253                         UIerr(UI_F_UI_DUP_ERROR_STRING,ERR_R_MALLOC_FAILURE);
254                         return -1;
255                         }
256                 }
257         return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
258                 0, 0, NULL);
259         }
260
261 char *UI_construct_prompt(UI *ui, const char *object_desc,
262         const char *object_name)
263         {
264         char *prompt = NULL;
265
266         if (ui->meth->ui_construct_prompt)
267                 prompt = ui->meth->ui_construct_prompt(ui,
268                         object_desc, object_name);
269         else
270                 {
271                 char prompt1[] = "Enter ";
272                 char prompt2[] = " for ";
273                 char prompt3[] = ":";
274                 int len = 0;
275
276                 if (object_desc == NULL)
277                         return NULL;
278                 len = sizeof(prompt1) - 1 + strlen(object_desc);
279                 if (object_name)
280                         len += sizeof(prompt2) - 1 + strlen(object_name);
281                 len += sizeof(prompt3) - 1;
282
283                 prompt = (char *)OPENSSL_malloc(len + 1);
284                 strcpy(prompt, prompt1);
285                 strcat(prompt, object_desc);
286                 if (object_name)
287                         {
288                         strcat(prompt, prompt2);
289                         strcat(prompt, object_name);
290                         }
291                 strcat(prompt, prompt3);
292                 }
293         return prompt;
294         }
295
296 void *UI_add_user_data(UI *ui, void *user_data)
297         {
298         void *old_data = ui->user_data;
299         ui->user_data = user_data;
300         return old_data;
301         }
302
303 void *UI_get0_user_data(UI *ui)
304         {
305         return ui->user_data;
306         }
307
308 const char *UI_get0_result(UI *ui, int i)
309         {
310         if (i < 0)
311                 {
312                 UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_SMALL);
313                 return NULL;
314                 }
315         if (i >= sk_UI_STRING_num(ui->strings))
316                 {
317                 UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_LARGE);
318                 return NULL;
319                 }
320         return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
321         }
322
323 int UI_process(UI *ui)
324         {
325         int i, ok=0;
326
327         if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
328                 return -1;
329
330         for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
331                 {
332                 if (ui->meth->ui_write_string
333                         && !ui->meth->ui_write_string(ui,
334                                 sk_UI_STRING_value(ui->strings, i)))
335                         {
336                         ok=-1;
337                         goto err;
338                         }
339                 }
340
341         if (ui->meth->ui_flush)
342                 switch(ui->meth->ui_flush(ui))
343                         {
344                 case -1: /* Interrupt/Cancel/something... */
345                         ok = -2;
346                         goto err;
347                 case 0: /* Errors */
348                         ok = -1;
349                         goto err;
350                 default: /* Success */
351                         ok = 0;
352                         break;
353                         }
354
355         for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
356                 {
357                 if (ui->meth->ui_read_string)
358                         {
359                         switch(ui->meth->ui_read_string(ui,
360                                 sk_UI_STRING_value(ui->strings, i)))
361                                 {
362                         case -1: /* Interrupt/Cancel/something... */
363                                 ok = -2;
364                                 goto err;
365                         case 0: /* Errors */
366                                 ok = -1;
367                                 goto err;
368                         default: /* Success */
369                                 ok = 0;
370                                 break;
371                                 }
372                         }
373                 }
374  err:
375         if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
376                 return -1;
377         return ok;
378         }
379
380 int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
381              CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
382         {
383         ui_meth_num++;
384         return(CRYPTO_get_ex_new_index(ui_meth_num-1,
385                 &ui_meth,argl,argp,new_func,dup_func,free_func));
386         }
387
388 int UI_set_ex_data(UI *r, int idx, void *arg)
389         {
390         return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
391         }
392
393 void *UI_get_ex_data(UI *r, int idx)
394         {
395         return(CRYPTO_get_ex_data(&r->ex_data,idx));
396         }
397
398 void UI_set_default_method(const UI_METHOD *meth)
399         {
400         default_UI_meth=meth;
401         }
402
403 const UI_METHOD *UI_get_default_method(void)
404         {
405         if (default_UI_meth == NULL)
406                 {
407                 default_UI_meth=UI_OpenSSL();
408                 }
409         return default_UI_meth;
410         }
411
412 const UI_METHOD *UI_get_method(UI *ui)
413         {
414         return ui->meth;
415         }
416
417 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
418         {
419         ui->meth=meth;
420         return ui->meth;
421         }
422
423
424 UI_METHOD *UI_create_method(char *name)
425         {
426         UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
427
428         if (ui_method)
429                 memset(ui_method, 0, sizeof(*ui_method));
430         ui_method->name = strdup(name);
431         return ui_method;
432         }
433
434 int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui))
435         {
436         if (method)
437                 {
438                 method->ui_open_session = opener;
439                 return 0;
440                 }
441         else
442                 return -1;
443         }
444
445 int UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis))
446         {
447         if (method)
448                 {
449                 method->ui_write_string = writer;
450                 return 0;
451                 }
452         else
453                 return -1;
454         }
455
456 int UI_method_set_flusher(UI_METHOD *method, int (*flusher)(UI *ui))
457         {
458         if (method)
459                 {
460                 method->ui_flush = flusher;
461                 return 0;
462                 }
463         else
464                 return -1;
465         }
466
467 int UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis))
468         {
469         if (method)
470                 {
471                 method->ui_read_string = reader;
472                 return 0;
473                 }
474         else
475                 return -1;
476         }
477
478 int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
479         {
480         if (method)
481                 {
482                 method->ui_close_session = closer;
483                 return 0;
484                 }
485         else
486                 return -1;
487         }
488
489 int (*UI_method_get_opener(UI_METHOD *method))(UI*)
490         {
491         if (method)
492                 return method->ui_open_session;
493         else
494                 return NULL;
495         }
496
497 int (*UI_method_get_writer(UI_METHOD *method))(UI*,UI_STRING*)
498         {
499         if (method)
500                 return method->ui_write_string;
501         else
502                 return NULL;
503         }
504
505 int (*UI_method_get_reader(UI_METHOD *method))(UI*,UI_STRING*)
506         {
507         if (method)
508                 return method->ui_read_string;
509         else
510                 return NULL;
511         }
512
513 int (*UI_method_get_closer(UI_METHOD *method))(UI*)
514         {
515         if (method)
516                 return method->ui_close_session;
517         else
518                 return NULL;
519         }
520
521 enum UI_string_types UI_get_string_type(UI_STRING *uis)
522         {
523         if (!uis)
524                 return UIT_NONE;
525         return uis->type;
526         }
527
528 int UI_get_input_flags(UI_STRING *uis)
529         {
530         if (!uis)
531                 return 0;
532         return uis->input_flags;
533         }
534
535 const char *UI_get0_output_string(UI_STRING *uis)
536         {
537         if (!uis)
538                 return NULL;
539         return uis->out_string;
540         }
541
542 const char *UI_get0_result_string(UI_STRING *uis)
543         {
544         if (!uis)
545                 return NULL;
546         switch(uis->type)
547                 {
548         case UIT_PROMPT:
549         case UIT_VERIFY:
550                 return uis->result_buf;
551         default:
552                 return NULL;
553                 }
554         }
555
556 const char *UI_get0_test_string(UI_STRING *uis)
557         {
558         if (!uis)
559                 return NULL;
560         return uis->test_buf;
561         }
562
563 int UI_get_result_minsize(UI_STRING *uis)
564         {
565         if (!uis)
566                 return -1;
567         return uis->result_minsize;
568         }
569
570 int UI_get_result_maxsize(UI_STRING *uis)
571         {
572         if (!uis)
573                 return -1;
574         return uis->result_maxsize;
575         }
576
577 int UI_set_result(UI_STRING *uis, const char *result)
578         {
579         int l = strlen(result);
580
581         if (!uis)
582                 return -1;
583         if (l < uis->result_minsize)
584                 {
585                 UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_SMALL);
586                 return -1;
587                 }
588         if (l > uis->result_maxsize)
589                 {
590                 UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_LARGE);
591                 return -1;
592                 }
593
594         if (!uis->result_buf)
595                 {
596                 uis->result_buf = OPENSSL_malloc(uis->result_maxsize+1);
597                 }
598
599         if (!uis->result_buf)
600                 {
601                 UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
602                 return -1;
603                 }
604
605         strcpy(uis->result_buf, result);
606         return 0;
607         }