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