Initialize UI ex_data.
[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,
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->type=type;
149                 s->result_buf=result_buf;
150                 s->result_minsize=minsize;
151                 s->result_maxsize=maxsize;
152                 s->test_buf=test_buf;
153                 ret=sk_UI_STRING_push(ui->strings, s);
154                 }
155         return ret;
156         }
157
158 /* Returns the index to the place in the stack or 0 for error.  Uses a
159    direct reference to the prompt.  */
160 int UI_add_input_string(UI *ui, const char *prompt, int echo_p,
161         char *result_buf, int minsize, int maxsize)
162         {
163         return general_allocate_string(ui, prompt, 0,
164                 echo_p?UI_STRING_ECHO:UI_STRING_NOECHO,
165                 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 echo_p,
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                 echo_p?UI_STRING_ECHO:UI_STRING_NOECHO,
186                 result_buf, minsize, maxsize, NULL);
187         }
188
189 int UI_add_verify_string(UI *ui, const char *prompt, int echo_p,
190         char *result_buf, int minsize, int maxsize, const char *test_buf)
191         {
192         return general_allocate_string(ui, prompt, 0,
193                 echo_p?UI_VERIFY_ECHO:UI_VERIFY_NOECHO,
194                 result_buf, minsize, maxsize, test_buf);
195         }
196
197 int UI_dup_verify_string(UI *ui, const char *prompt, int echo_p,
198         char *result_buf, int minsize, int maxsize, const char *test_buf)
199         {
200         char *prompt_copy=NULL;
201
202         if (prompt)
203                 {
204                 prompt_copy=strdup(prompt);
205                 if (prompt_copy == NULL)
206                         {
207                         UIerr(UI_F_UI_DUP_VERIFY_STRING,ERR_R_MALLOC_FAILURE);
208                         return -1;
209                         }
210                 }
211         
212         return general_allocate_string(ui, prompt, 1,
213                 echo_p?UI_VERIFY_ECHO:UI_VERIFY_NOECHO,
214                 result_buf, minsize, maxsize, test_buf);
215         }
216
217 int UI_add_info_string(UI *ui, const char *text)
218         {
219         return general_allocate_string(ui, text, 0, UI_INFO, NULL, 0, 0, NULL);
220         }
221
222 int UI_dup_info_string(UI *ui, const char *text)
223         {
224         char *text_copy=NULL;
225
226         if (text)
227                 {
228                 text_copy=strdup(text);
229                 if (text_copy == NULL)
230                         {
231                         UIerr(UI_F_UI_DUP_INFO_STRING,ERR_R_MALLOC_FAILURE);
232                         return -1;
233                         }
234                 }
235
236         return general_allocate_string(ui, text, 1, UI_INFO, NULL, 0, 0, NULL);
237         }
238
239 int UI_add_error_string(UI *ui, const char *text)
240         {
241         return general_allocate_string(ui, text, 0, UI_ERROR, NULL, 0, 0,
242                 NULL);
243         }
244
245 int UI_dup_error_string(UI *ui, const char *text)
246         {
247         char *text_copy=NULL;
248
249         if (text)
250                 {
251                 text_copy=strdup(text);
252                 if (text_copy == NULL)
253                         {
254                         UIerr(UI_F_UI_DUP_ERROR_STRING,ERR_R_MALLOC_FAILURE);
255                         return -1;
256                         }
257                 }
258         return general_allocate_string(ui, text_copy, 1, UI_ERROR, NULL, 0, 0,
259                 NULL);
260         }
261
262 void *UI_add_user_data(UI *ui, void *user_data)
263         {
264         void *old_data = ui->user_data;
265         ui->user_data = user_data;
266         return old_data;
267         }
268
269 void *UI_get0_user_data(UI *ui)
270         {
271         return ui->user_data;
272         }
273
274 const char *UI_get0_result(UI *ui, int i)
275         {
276         if (i < 0)
277                 {
278                 UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_SMALL);
279                 return NULL;
280                 }
281         if (i >= sk_UI_STRING_num(ui->strings))
282                 {
283                 UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_LARGE);
284                 return NULL;
285                 }
286         return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
287         }
288
289 int UI_process(UI *ui)
290         {
291         int i, ok=0;
292
293         if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
294                 return -1;
295
296         for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
297                 {
298                 if (ui->meth->ui_write_string
299                         && !ui->meth->ui_write_string(ui,
300                                 sk_UI_STRING_value(ui->strings, i)))
301                         {
302                         ok=-1;
303                         goto err;
304                         }
305                 }
306
307         for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
308                 {
309                 if (ui->meth->ui_read_string
310                         && !ui->meth->ui_read_string(ui,
311                                 sk_UI_STRING_value(ui->strings, i)))
312                         {
313                         ok=-1;
314                         goto err;
315                         }
316                 }
317  err:
318         if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
319                 return -1;
320         return ok;
321         }
322
323 int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
324              CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
325         {
326         ui_meth_num++;
327         return(CRYPTO_get_ex_new_index(ui_meth_num-1,
328                 &ui_meth,argl,argp,new_func,dup_func,free_func));
329         }
330
331 int UI_set_ex_data(UI *r, int idx, void *arg)
332         {
333         return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
334         }
335
336 void *UI_get_ex_data(UI *r, int idx)
337         {
338         return(CRYPTO_get_ex_data(&r->ex_data,idx));
339         }
340
341 void UI_set_default_method(const UI_METHOD *meth)
342         {
343         default_UI_meth=meth;
344         }
345
346 const UI_METHOD *UI_get_default_method(void)
347         {
348         if (default_UI_meth == NULL)
349                 {
350                 default_UI_meth=UI_OpenSSL();
351                 }
352         return default_UI_meth;
353         }
354
355 const UI_METHOD *UI_get_method(UI *ui)
356         {
357         return ui->meth;
358         }
359
360 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
361         {
362         ui->meth=meth;
363         return ui->meth;
364         }
365
366
367 UI_METHOD *UI_create_method(void)
368         {
369         return (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
370         }
371
372 int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui))
373         {
374         if (method)
375                 {
376                 method->ui_open_session = opener;
377                 return 0;
378                 }
379         else
380                 return -1;
381         }
382
383 int UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis))
384         {
385         if (method)
386                 {
387                 method->ui_write_string = writer;
388                 return 0;
389                 }
390         else
391                 return -1;
392         }
393
394 int UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis))
395         {
396         if (method)
397                 {
398                 method->ui_read_string = reader;
399                 return 0;
400                 }
401         else
402                 return -1;
403         }
404
405 int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
406         {
407         if (method)
408                 {
409                 method->ui_close_session = closer;
410                 return 0;
411                 }
412         else
413                 return -1;
414         }
415
416 int (*UI_method_get_opener(UI_METHOD *method))(UI*)
417         {
418         if (method)
419                 return method->ui_open_session;
420         else
421                 return NULL;
422         }
423
424 int (*UI_method_get_writer(UI_METHOD *method))(UI*,UI_STRING*)
425         {
426         if (method)
427                 return method->ui_write_string;
428         else
429                 return NULL;
430         }
431
432 int (*UI_method_get_reader(UI_METHOD *method))(UI*,UI_STRING*)
433         {
434         if (method)
435                 return method->ui_read_string;
436         else
437                 return NULL;
438         }
439
440 int (*UI_method_get_closer(UI_METHOD *method))(UI*)
441         {
442         if (method)
443                 return method->ui_close_session;
444         else
445                 return NULL;
446         }
447
448 enum UI_string_types UI_get_string_type(UI_STRING *uis)
449         {
450         if (!uis)
451                 return UI_NONE;
452         return uis->type;
453         }
454
455 const char *UI_get0_output_string(UI_STRING *uis)
456         {
457         if (!uis)
458                 return NULL;
459         return uis->out_string;
460         }
461
462 const char *UI_get0_result_string(UI_STRING *uis)
463         {
464         if (!uis)
465                 return NULL;
466         switch(uis->type)
467                 {
468         case UI_STRING_ECHO:
469         case UI_STRING_NOECHO:
470         case UI_VERIFY_ECHO:
471         case UI_VERIFY_NOECHO:
472                 return uis->result_buf;
473         default:
474                 return NULL;
475                 }
476         }
477
478 const char *UI_get0_test_string(UI_STRING *uis)
479         {
480         if (!uis)
481                 return NULL;
482         return uis->test_buf;
483         }
484
485 int UI_get_result_minsize(UI_STRING *uis)
486         {
487         if (!uis)
488                 return -1;
489         return uis->result_minsize;
490         }
491
492 int UI_get_result_maxsize(UI_STRING *uis)
493         {
494         if (!uis)
495                 return -1;
496         return uis->result_maxsize;
497         }
498
499 int UI_set_result(UI_STRING *uis, char *result)
500         {
501         int l = strlen(result);
502
503         if (!uis)
504                 return -1;
505         if (l < uis->result_minsize)
506                 {
507                 UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_SMALL);
508                 return -1;
509                 }
510         if (l > uis->result_maxsize)
511                 {
512                 UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_LARGE);
513                 return -1;
514                 }
515
516         if (!uis->result_buf)
517                 {
518                 uis->result_buf = OPENSSL_malloc(uis->result_maxsize+1);
519                 }
520
521         if (!uis->result_buf)
522                 {
523                 UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
524                 return -1;
525                 }
526
527         strcpy(uis->result_buf, result);
528         return 0;
529         }