Fix warning with DEBUG_SAFESTACK
[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
61 #include <openssl/ui.h>
62 #include <openssl/err.h>
63 #include "ui_locl.h"
64
65 IMPLEMENT_STACK_OF(UI_STRING_ST)
66
67 static const UI_METHOD *default_UI_meth=NULL;
68 static int ui_meth_num=0;
69 static STACK_OF(CRYPTO_EX_DATA_FUNCS) *ui_meth=NULL;
70
71 UI *UI_new(void)
72         {
73         return(UI_new_method(NULL));
74         }
75
76 UI *UI_new_method(const UI_METHOD *method)
77         {
78         UI *ret;
79
80         ret=(UI *)OPENSSL_malloc(sizeof(UI));
81         if (ret == NULL)
82                 {
83                 UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
84                 return NULL;
85                 }
86         if (method == NULL)
87                 ret->meth=UI_get_default_method();
88         else
89                 ret->meth=method;
90
91         ret->strings=NULL;
92         return ret;
93         }
94
95 static void free_string(UI_STRING *uis)
96         {
97         if (uis->flags & OUT_STRING_FREEABLE)
98                 OPENSSL_free((char *)uis->out_string);
99         OPENSSL_free(uis);
100         }
101
102 void UI_free(UI *ui)
103         {
104         sk_UI_STRING_pop_free(ui->strings,free_string);
105         OPENSSL_free(ui);
106         }
107
108 static int allocate_string_stack(UI *ui)
109         {
110         if (ui->strings == NULL)
111                 {
112                 ui->strings=sk_UI_STRING_new_null();
113                 if (ui->strings == NULL)
114                         {
115                         return -1;
116                         }
117                 }
118         return 0;
119         }
120
121 static int general_allocate_string(UI *ui, const char *prompt,
122         int prompt_freeable, enum UI_string_types type,
123         char *result_buf, int minsize, int maxsize, const char *test_buf)
124         {
125         int ret=-1;
126
127         if (prompt == NULL)
128                 {
129                 UIerr(UI_F_GENERAL_ALLOCATE_STRING,ERR_R_PASSED_NULL_PARAMETER);
130                 }
131         else if (allocate_string_stack(ui) >= 0)
132                 {
133                 UI_STRING *s=(UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING));
134                 s->out_string=prompt;
135                 s->flags=prompt_freeable ? OUT_STRING_FREEABLE : 0;
136                 s->type=type;
137                 s->result_buf=result_buf;
138                 s->result_minsize=minsize;
139                 s->result_maxsize=maxsize;
140                 s->test_buf=test_buf;
141                 ret=sk_UI_STRING_push(ui->strings, s);
142                 }
143         return ret;
144         }
145
146 /* Returns the index to the place in the stack or 0 for error.  Uses a
147    direct reference to the prompt.  */
148 int UI_add_input_string(UI *ui, const char *prompt, int echo_p,
149         char *result_buf, int minsize, int maxsize)
150         {
151         return general_allocate_string(ui, prompt, 0,
152                 echo_p?UI_STRING_ECHO:UI_STRING_NOECHO,
153                 result_buf, minsize, maxsize, NULL);
154         }
155
156 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
157 int UI_dup_input_string(UI *ui, const char *prompt, int echo_p,
158         char *result_buf, int minsize, int maxsize)
159         {
160         char *prompt_copy=NULL;
161
162         if (prompt)
163                 {
164                 prompt_copy=strdup(prompt);
165                 if (prompt_copy == NULL)
166                         {
167                         UIerr(UI_F_UI_DUP_INPUT_STRING,ERR_R_MALLOC_FAILURE);
168                         return 0;
169                         }
170                 }
171         
172         return general_allocate_string(ui, prompt, 1,
173                 echo_p?UI_STRING_ECHO:UI_STRING_NOECHO,
174                 result_buf, minsize, maxsize, NULL);
175         }
176
177 int UI_add_verify_string(UI *ui, const char *prompt, int echo_p,
178         char *result_buf, int minsize, int maxsize, const char *test_buf)
179         {
180         return general_allocate_string(ui, prompt, 0,
181                 echo_p?UI_VERIFY_ECHO:UI_VERIFY_NOECHO,
182                 result_buf, minsize, maxsize, test_buf);
183         }
184
185 int UI_dup_verify_string(UI *ui, const char *prompt, int echo_p,
186         char *result_buf, int minsize, int maxsize, const char *test_buf)
187         {
188         char *prompt_copy=NULL;
189
190         if (prompt)
191                 {
192                 prompt_copy=strdup(prompt);
193                 if (prompt_copy == NULL)
194                         {
195                         UIerr(UI_F_UI_DUP_VERIFY_STRING,ERR_R_MALLOC_FAILURE);
196                         return -1;
197                         }
198                 }
199         
200         return general_allocate_string(ui, prompt, 1,
201                 echo_p?UI_VERIFY_ECHO:UI_VERIFY_NOECHO,
202                 result_buf, minsize, maxsize, test_buf);
203         }
204
205 int UI_add_info_string(UI *ui, const char *text)
206         {
207         return general_allocate_string(ui, text, 0, UI_INFO, NULL, 0, 0, NULL);
208         }
209
210 int UI_dup_info_string(UI *ui, const char *text)
211         {
212         char *text_copy=NULL;
213
214         if (text)
215                 {
216                 text_copy=strdup(text);
217                 if (text_copy == NULL)
218                         {
219                         UIerr(UI_F_UI_DUP_INFO_STRING,ERR_R_MALLOC_FAILURE);
220                         return -1;
221                         }
222                 }
223
224         return general_allocate_string(ui, text, 1, UI_INFO, NULL, 0, 0, NULL);
225         }
226
227 int UI_add_error_string(UI *ui, const char *text)
228         {
229         return general_allocate_string(ui, text, 0, UI_ERROR, NULL, 0, 0,
230                 NULL);
231         }
232
233 int UI_dup_error_string(UI *ui, const char *text)
234         {
235         char *text_copy=NULL;
236
237         if (text)
238                 {
239                 text_copy=strdup(text);
240                 if (text_copy == NULL)
241                         {
242                         UIerr(UI_F_UI_DUP_ERROR_STRING,ERR_R_MALLOC_FAILURE);
243                         return -1;
244                         }
245                 }
246         return general_allocate_string(ui, text_copy, 1, UI_ERROR, NULL, 0, 0,
247                 NULL);
248         }
249
250 const char *UI_get0_result(UI *ui, int i)
251         {
252         if (i < 0)
253                 {
254                 UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_SMALL);
255                 return NULL;
256                 }
257         if (i >= sk_UI_STRING_num(ui->strings))
258                 {
259                 UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_LARGE);
260                 return NULL;
261                 }
262         return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
263         }
264
265 int UI_process(UI *ui)
266         {
267         int i, ok=0;
268
269         if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
270                 return -1;
271
272         for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
273                 {
274                 if (ui->meth->ui_write_string
275                         && !ui->meth->ui_write_string(ui,
276                                 sk_UI_STRING_value(ui->strings, i)))
277                         {
278                         ok=-1;
279                         goto err;
280                         }
281                 }
282
283         for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
284                 {
285                 if (ui->meth->ui_read_string
286                         && !ui->meth->ui_read_string(ui,
287                                 sk_UI_STRING_value(ui->strings, i)))
288                         {
289                         ok=-1;
290                         goto err;
291                         }
292                 }
293  err:
294         if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
295                 return -1;
296         return ok;
297         }
298
299 int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
300              CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
301         {
302         ui_meth_num++;
303         return(CRYPTO_get_ex_new_index(ui_meth_num-1,
304                 &ui_meth,argl,argp,new_func,dup_func,free_func));
305         }
306
307 int UI_set_ex_data(UI *r, int idx, void *arg)
308         {
309         return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
310         }
311
312 void *UI_get_ex_data(UI *r, int idx)
313         {
314         return(CRYPTO_get_ex_data(&r->ex_data,idx));
315         }
316
317 void UI_set_default_method(const UI_METHOD *meth)
318         {
319         default_UI_meth=meth;
320         }
321
322 const UI_METHOD *UI_get_default_method(void)
323         {
324         if (default_UI_meth == NULL)
325                 {
326                 default_UI_meth=UI_OpenSSL();
327                 }
328         return default_UI_meth;
329         }
330
331 const UI_METHOD *UI_get_method(UI *ui)
332         {
333         return ui->meth;
334         }
335
336 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
337         {
338         ui->meth=meth;
339         return ui->meth;
340         }
341
342
343 UI_METHOD *UI_create_method(void)
344         {
345         return (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
346         }
347
348 int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui))
349         {
350         if (method)
351                 {
352                 method->ui_open_session = opener;
353                 return 0;
354                 }
355         else
356                 return -1;
357         }
358
359 int UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis))
360         {
361         if (method)
362                 {
363                 method->ui_write_string = writer;
364                 return 0;
365                 }
366         else
367                 return -1;
368         }
369
370 int UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis))
371         {
372         if (method)
373                 {
374                 method->ui_read_string = reader;
375                 return 0;
376                 }
377         else
378                 return -1;
379         }
380
381 int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
382         {
383         if (method)
384                 {
385                 method->ui_close_session = closer;
386                 return 0;
387                 }
388         else
389                 return -1;
390         }
391
392 int (*UI_method_get_opener(UI_METHOD *method))(UI*)
393         {
394         if (method)
395                 return method->ui_open_session;
396         else
397                 return NULL;
398         }
399
400 int (*UI_method_get_writer(UI_METHOD *method))(UI*,UI_STRING*)
401         {
402         if (method)
403                 return method->ui_write_string;
404         else
405                 return NULL;
406         }
407
408 int (*UI_method_get_reader(UI_METHOD *method))(UI*,UI_STRING*)
409         {
410         if (method)
411                 return method->ui_read_string;
412         else
413                 return NULL;
414         }
415
416 int (*UI_method_get_closer(UI_METHOD *method))(UI*)
417         {
418         if (method)
419                 return method->ui_close_session;
420         else
421                 return NULL;
422         }
423
424 enum UI_string_types UI_get_string_type(UI_STRING *uis)
425         {
426         if (!uis)
427                 return UI_NONE;
428         return uis->type;
429         }
430
431 const char *UI_get0_output_string(UI_STRING *uis)
432         {
433         if (!uis)
434                 return NULL;
435         return uis->out_string;
436         }
437
438 const char *UI_get0_result_string(UI_STRING *uis)
439         {
440         if (!uis)
441                 return NULL;
442         switch(uis->type)
443                 {
444         case UI_STRING_ECHO:
445         case UI_STRING_NOECHO:
446         case UI_VERIFY_ECHO:
447         case UI_VERIFY_NOECHO:
448                 return uis->result_buf;
449         default:
450                 return NULL;
451                 }
452         }
453
454 const char *UI_get0_test_string(UI_STRING *uis)
455         {
456         if (!uis)
457                 return NULL;
458         return uis->test_buf;
459         }
460
461 int UI_get_result_minsize(UI_STRING *uis)
462         {
463         if (!uis)
464                 return -1;
465         return uis->result_minsize;
466         }
467
468 int UI_get_result_maxsize(UI_STRING *uis)
469         {
470         if (!uis)
471                 return -1;
472         return uis->result_maxsize;
473         }
474
475 int UI_set_result(UI_STRING *uis, char *result)
476         {
477         int l = strlen(result);
478
479         if (!uis)
480                 return -1;
481         if (l < uis->result_minsize)
482                 {
483                 UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_SMALL);
484                 return -1;
485                 }
486         if (l > uis->result_maxsize)
487                 {
488                 UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_LARGE);
489                 return -1;
490                 }
491
492         if (!uis->result_buf)
493                 {
494                 uis->result_buf = OPENSSL_malloc(uis->result_maxsize+1);
495                 }
496
497         if (!uis->result_buf)
498                 {
499                 UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
500                 return -1;
501                 }
502
503         strcpy(uis->result_buf, result);
504         return 0;
505         }