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