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