Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/ui/ui_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <string.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/e_os2.h>
13
#include <openssl/buffer.h>
14
#include <openssl/ui.h>
15
#include <openssl/err.h>
16
#include "ui_local.h"
17
18
UI *UI_new(void)
19
0
{
20
0
    return UI_new_method(NULL);
21
0
}
22
23
UI *UI_new_method(const UI_METHOD *method)
24
0
{
25
0
    UI *ret = OPENSSL_zalloc(sizeof(*ret));
26
27
0
    if (ret == NULL) {
28
0
        UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
29
0
        return NULL;
30
0
    }
31
32
0
    ret->lock = CRYPTO_THREAD_lock_new();
33
0
    if (ret->lock == NULL) {
34
0
        UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
35
0
        OPENSSL_free(ret);
36
0
        return NULL;
37
0
    }
38
39
0
    if (method == NULL)
40
0
        method = UI_get_default_method();
41
0
    if (method == NULL)
42
0
        method = UI_null();
43
0
    ret->meth = method;
44
45
0
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data)) {
46
0
        OPENSSL_free(ret);
47
0
        return NULL;
48
0
    }
49
0
    return ret;
50
0
}
51
52
static void free_string(UI_STRING *uis)
53
0
{
54
0
    if (uis->flags & OUT_STRING_FREEABLE) {
55
0
        OPENSSL_free((char *)uis->out_string);
56
0
        switch (uis->type) {
57
0
        case UIT_BOOLEAN:
58
0
            OPENSSL_free((char *)uis->_.boolean_data.action_desc);
59
0
            OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
60
0
            OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
61
0
            break;
62
0
        case UIT_NONE:
63
0
        case UIT_PROMPT:
64
0
        case UIT_VERIFY:
65
0
        case UIT_ERROR:
66
0
        case UIT_INFO:
67
0
            break;
68
0
        }
69
0
    }
70
0
    OPENSSL_free(uis);
71
0
}
72
73
void UI_free(UI *ui)
74
0
{
75
0
    if (ui == NULL)
76
0
        return;
77
0
    if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
78
0
        ui->meth->ui_destroy_data(ui, ui->user_data);
79
0
    }
80
0
    sk_UI_STRING_pop_free(ui->strings, free_string);
81
0
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
82
0
    CRYPTO_THREAD_lock_free(ui->lock);
83
0
    OPENSSL_free(ui);
84
0
}
85
86
static int allocate_string_stack(UI *ui)
87
0
{
88
0
    if (ui->strings == NULL) {
89
0
        ui->strings = sk_UI_STRING_new_null();
90
0
        if (ui->strings == NULL) {
91
0
            return -1;
92
0
        }
93
0
    }
94
0
    return 0;
95
0
}
96
97
static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
98
                                          int prompt_freeable,
99
                                          enum UI_string_types type,
100
                                          int input_flags, char *result_buf)
101
0
{
102
0
    UI_STRING *ret = NULL;
103
104
0
    if (prompt == NULL) {
105
0
        UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
106
0
    } else if ((type == UIT_PROMPT || type == UIT_VERIFY
107
0
                || type == UIT_BOOLEAN) && result_buf == NULL) {
108
0
        UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
109
0
    } else if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL) {
110
0
        ret->out_string = prompt;
111
0
        ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
112
0
        ret->input_flags = input_flags;
113
0
        ret->type = type;
114
0
        ret->result_buf = result_buf;
115
0
    }
116
0
    return ret;
117
0
}
118
119
static int general_allocate_string(UI *ui, const char *prompt,
120
                                   int prompt_freeable,
121
                                   enum UI_string_types type, int input_flags,
122
                                   char *result_buf, int minsize, int maxsize,
123
                                   const char *test_buf)
124
0
{
125
0
    int ret = -1;
126
0
    UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
127
0
                                           type, input_flags, result_buf);
128
129
0
    if (s != NULL) {
130
0
        if (allocate_string_stack(ui) >= 0) {
131
0
            s->_.string_data.result_minsize = minsize;
132
0
            s->_.string_data.result_maxsize = maxsize;
133
0
            s->_.string_data.test_buf = test_buf;
134
0
            ret = sk_UI_STRING_push(ui->strings, s);
135
            /* sk_push() returns 0 on error.  Let's adapt that */
136
0
            if (ret <= 0) {
137
0
                ret--;
138
0
                free_string(s);
139
0
            }
140
0
        } else
141
0
            free_string(s);
142
0
    }
143
0
    return ret;
144
0
}
145
146
static int general_allocate_boolean(UI *ui,
147
                                    const char *prompt,
148
                                    const char *action_desc,
149
                                    const char *ok_chars,
150
                                    const char *cancel_chars,
151
                                    int prompt_freeable,
152
                                    enum UI_string_types type,
153
                                    int input_flags, char *result_buf)
154
0
{
155
0
    int ret = -1;
156
0
    UI_STRING *s;
157
0
    const char *p;
158
159
0
    if (ok_chars == NULL) {
160
0
        UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
161
0
    } else if (cancel_chars == NULL) {
162
0
        UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
163
0
    } else {
164
0
        for (p = ok_chars; *p != '\0'; p++) {
165
0
            if (strchr(cancel_chars, *p) != NULL) {
166
0
                UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
167
0
                      UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
168
0
            }
169
0
        }
170
171
0
        s = general_allocate_prompt(ui, prompt, prompt_freeable,
172
0
                                    type, input_flags, result_buf);
173
174
0
        if (s != NULL) {
175
0
            if (allocate_string_stack(ui) >= 0) {
176
0
                s->_.boolean_data.action_desc = action_desc;
177
0
                s->_.boolean_data.ok_chars = ok_chars;
178
0
                s->_.boolean_data.cancel_chars = cancel_chars;
179
0
                ret = sk_UI_STRING_push(ui->strings, s);
180
                /*
181
                 * sk_push() returns 0 on error. Let's adapt that
182
                 */
183
0
                if (ret <= 0) {
184
0
                    ret--;
185
0
                    free_string(s);
186
0
                }
187
0
            } else
188
0
                free_string(s);
189
0
        }
190
0
    }
191
0
    return ret;
192
0
}
193
194
/*
195
 * Returns the index to the place in the stack or -1 for error.  Uses a
196
 * direct reference to the prompt.
197
 */
198
int UI_add_input_string(UI *ui, const char *prompt, int flags,
199
                        char *result_buf, int minsize, int maxsize)
200
0
{
201
0
    return general_allocate_string(ui, prompt, 0,
202
0
                                   UIT_PROMPT, flags, result_buf, minsize,
203
0
                                   maxsize, NULL);
204
0
}
205
206
/* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
207
int UI_dup_input_string(UI *ui, const char *prompt, int flags,
208
                        char *result_buf, int minsize, int maxsize)
209
0
{
210
0
    char *prompt_copy = NULL;
211
212
0
    if (prompt != NULL) {
213
0
        prompt_copy = OPENSSL_strdup(prompt);
214
0
        if (prompt_copy == NULL) {
215
0
            UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
216
0
            return 0;
217
0
        }
218
0
    }
219
220
0
    return general_allocate_string(ui, prompt_copy, 1,
221
0
                                   UIT_PROMPT, flags, result_buf, minsize,
222
0
                                   maxsize, NULL);
223
0
}
224
225
int UI_add_verify_string(UI *ui, const char *prompt, int flags,
226
                         char *result_buf, int minsize, int maxsize,
227
                         const char *test_buf)
228
0
{
229
0
    return general_allocate_string(ui, prompt, 0,
230
0
                                   UIT_VERIFY, flags, result_buf, minsize,
231
0
                                   maxsize, test_buf);
232
0
}
233
234
int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
235
                         char *result_buf, int minsize, int maxsize,
236
                         const char *test_buf)
237
0
{
238
0
    char *prompt_copy = NULL;
239
240
0
    if (prompt != NULL) {
241
0
        prompt_copy = OPENSSL_strdup(prompt);
242
0
        if (prompt_copy == NULL) {
243
0
            UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
244
0
            return -1;
245
0
        }
246
0
    }
247
248
0
    return general_allocate_string(ui, prompt_copy, 1,
249
0
                                   UIT_VERIFY, flags, result_buf, minsize,
250
0
                                   maxsize, test_buf);
251
0
}
252
253
int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
254
                         const char *ok_chars, const char *cancel_chars,
255
                         int flags, char *result_buf)
256
0
{
257
0
    return general_allocate_boolean(ui, prompt, action_desc,
258
0
                                    ok_chars, cancel_chars, 0, UIT_BOOLEAN,
259
0
                                    flags, result_buf);
260
0
}
261
262
int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
263
                         const char *ok_chars, const char *cancel_chars,
264
                         int flags, char *result_buf)
265
0
{
266
0
    char *prompt_copy = NULL;
267
0
    char *action_desc_copy = NULL;
268
0
    char *ok_chars_copy = NULL;
269
0
    char *cancel_chars_copy = NULL;
270
271
0
    if (prompt != NULL) {
272
0
        prompt_copy = OPENSSL_strdup(prompt);
273
0
        if (prompt_copy == NULL) {
274
0
            UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
275
0
            goto err;
276
0
        }
277
0
    }
278
279
0
    if (action_desc != NULL) {
280
0
        action_desc_copy = OPENSSL_strdup(action_desc);
281
0
        if (action_desc_copy == NULL) {
282
0
            UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
283
0
            goto err;
284
0
        }
285
0
    }
286
287
0
    if (ok_chars != NULL) {
288
0
        ok_chars_copy = OPENSSL_strdup(ok_chars);
289
0
        if (ok_chars_copy == NULL) {
290
0
            UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
291
0
            goto err;
292
0
        }
293
0
    }
294
295
0
    if (cancel_chars != NULL) {
296
0
        cancel_chars_copy = OPENSSL_strdup(cancel_chars);
297
0
        if (cancel_chars_copy == NULL) {
298
0
            UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
299
0
            goto err;
300
0
        }
301
0
    }
302
303
0
    return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
304
0
                                    ok_chars_copy, cancel_chars_copy, 1,
305
0
                                    UIT_BOOLEAN, flags, result_buf);
306
0
 err:
307
0
    OPENSSL_free(prompt_copy);
308
0
    OPENSSL_free(action_desc_copy);
309
0
    OPENSSL_free(ok_chars_copy);
310
0
    OPENSSL_free(cancel_chars_copy);
311
0
    return -1;
312
0
}
313
314
int UI_add_info_string(UI *ui, const char *text)
315
0
{
316
0
    return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
317
0
                                   NULL);
318
0
}
319
320
int UI_dup_info_string(UI *ui, const char *text)
321
0
{
322
0
    char *text_copy = NULL;
323
324
0
    if (text != NULL) {
325
0
        text_copy = OPENSSL_strdup(text);
326
0
        if (text_copy == NULL) {
327
0
            UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
328
0
            return -1;
329
0
        }
330
0
    }
331
332
0
    return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
333
0
                                   0, 0, NULL);
334
0
}
335
336
int UI_add_error_string(UI *ui, const char *text)
337
0
{
338
0
    return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
339
0
                                   NULL);
340
0
}
341
342
int UI_dup_error_string(UI *ui, const char *text)
343
0
{
344
0
    char *text_copy = NULL;
345
346
0
    if (text != NULL) {
347
0
        text_copy = OPENSSL_strdup(text);
348
0
        if (text_copy == NULL) {
349
0
            UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
350
0
            return -1;
351
0
        }
352
0
    }
353
0
    return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
354
0
                                   0, 0, NULL);
355
0
}
356
357
char *UI_construct_prompt(UI *ui, const char *object_desc,
358
                          const char *object_name)
359
0
{
360
0
    char *prompt = NULL;
361
362
0
    if (ui->meth->ui_construct_prompt != NULL)
363
0
        prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
364
0
    else {
365
0
        char prompt1[] = "Enter ";
366
0
        char prompt2[] = " for ";
367
0
        char prompt3[] = ":";
368
0
        int len = 0;
369
370
0
        if (object_desc == NULL)
371
0
            return NULL;
372
0
        len = sizeof(prompt1) - 1 + strlen(object_desc);
373
0
        if (object_name != NULL)
374
0
            len += sizeof(prompt2) - 1 + strlen(object_name);
375
0
        len += sizeof(prompt3) - 1;
376
377
0
        if ((prompt = OPENSSL_malloc(len + 1)) == NULL) {
378
0
            UIerr(UI_F_UI_CONSTRUCT_PROMPT, ERR_R_MALLOC_FAILURE);
379
0
            return NULL;
380
0
        }
381
0
        OPENSSL_strlcpy(prompt, prompt1, len + 1);
382
0
        OPENSSL_strlcat(prompt, object_desc, len + 1);
383
0
        if (object_name != NULL) {
384
0
            OPENSSL_strlcat(prompt, prompt2, len + 1);
385
0
            OPENSSL_strlcat(prompt, object_name, len + 1);
386
0
        }
387
0
        OPENSSL_strlcat(prompt, prompt3, len + 1);
388
0
    }
389
0
    return prompt;
390
0
}
391
392
void *UI_add_user_data(UI *ui, void *user_data)
393
0
{
394
0
    void *old_data = ui->user_data;
395
396
0
    if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
397
0
        ui->meth->ui_destroy_data(ui, old_data);
398
0
        old_data = NULL;
399
0
    }
400
0
    ui->user_data = user_data;
401
0
    ui->flags &= ~UI_FLAG_DUPL_DATA;
402
0
    return old_data;
403
0
}
404
405
int UI_dup_user_data(UI *ui, void *user_data)
406
0
{
407
0
    void *duplicate = NULL;
408
409
0
    if (ui->meth->ui_duplicate_data == NULL
410
0
        || ui->meth->ui_destroy_data == NULL) {
411
0
        UIerr(UI_F_UI_DUP_USER_DATA, UI_R_USER_DATA_DUPLICATION_UNSUPPORTED);
412
0
        return -1;
413
0
    }
414
415
0
    duplicate = ui->meth->ui_duplicate_data(ui, user_data);
416
0
    if (duplicate == NULL) {
417
0
        UIerr(UI_F_UI_DUP_USER_DATA, ERR_R_MALLOC_FAILURE);
418
0
        return -1;
419
0
    }
420
421
0
    (void)UI_add_user_data(ui, duplicate);
422
0
    ui->flags |= UI_FLAG_DUPL_DATA;
423
424
0
    return 0;
425
0
}
426
427
void *UI_get0_user_data(UI *ui)
428
0
{
429
0
    return ui->user_data;
430
0
}
431
432
const char *UI_get0_result(UI *ui, int i)
433
0
{
434
0
    if (i < 0) {
435
0
        UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL);
436
0
        return NULL;
437
0
    }
438
0
    if (i >= sk_UI_STRING_num(ui->strings)) {
439
0
        UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE);
440
0
        return NULL;
441
0
    }
442
0
    return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
443
0
}
444
445
int UI_get_result_length(UI *ui, int i)
446
0
{
447
0
    if (i < 0) {
448
0
        UIerr(UI_F_UI_GET_RESULT_LENGTH, UI_R_INDEX_TOO_SMALL);
449
0
        return -1;
450
0
    }
451
0
    if (i >= sk_UI_STRING_num(ui->strings)) {
452
0
        UIerr(UI_F_UI_GET_RESULT_LENGTH, UI_R_INDEX_TOO_LARGE);
453
0
        return -1;
454
0
    }
455
0
    return UI_get_result_string_length(sk_UI_STRING_value(ui->strings, i));
456
0
}
457
458
static int print_error(const char *str, size_t len, UI *ui)
459
0
{
460
0
    UI_STRING uis;
461
462
0
    memset(&uis, 0, sizeof(uis));
463
0
    uis.type = UIT_ERROR;
464
0
    uis.out_string = str;
465
466
0
    if (ui->meth->ui_write_string != NULL
467
0
        && ui->meth->ui_write_string(ui, &uis) <= 0)
468
0
        return -1;
469
0
    return 0;
470
0
}
471
472
int UI_process(UI *ui)
473
0
{
474
0
    int i, ok = 0;
475
0
    const char *state = "processing";
476
477
0
    if (ui->meth->ui_open_session != NULL
478
0
        && ui->meth->ui_open_session(ui) <= 0) {
479
0
        state = "opening session";
480
0
        ok = -1;
481
0
        goto err;
482
0
    }
483
484
0
    if (ui->flags & UI_FLAG_PRINT_ERRORS)
485
0
        ERR_print_errors_cb((int (*)(const char *, size_t, void *))
486
0
                            print_error, (void *)ui);
487
488
0
    for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
489
0
        if (ui->meth->ui_write_string != NULL
490
0
            && (ui->meth->ui_write_string(ui,
491
0
                                          sk_UI_STRING_value(ui->strings, i))
492
0
                <= 0))
493
0
        {
494
0
            state = "writing strings";
495
0
            ok = -1;
496
0
            goto err;
497
0
        }
498
0
    }
499
500
0
    if (ui->meth->ui_flush != NULL)
501
0
        switch (ui->meth->ui_flush(ui)) {
502
0
        case -1:               /* Interrupt/Cancel/something... */
503
0
            ui->flags &= ~UI_FLAG_REDOABLE;
504
0
            ok = -2;
505
0
            goto err;
506
0
        case 0:                /* Errors */
507
0
            state = "flushing";
508
0
            ok = -1;
509
0
            goto err;
510
0
        default:               /* Success */
511
0
            ok = 0;
512
0
            break;
513
0
        }
514
515
0
    for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
516
0
        if (ui->meth->ui_read_string != NULL) {
517
0
            switch (ui->meth->ui_read_string(ui,
518
0
                                             sk_UI_STRING_value(ui->strings,
519
0
                                                                i))) {
520
0
            case -1:           /* Interrupt/Cancel/something... */
521
0
                ui->flags &= ~UI_FLAG_REDOABLE;
522
0
                ok = -2;
523
0
                goto err;
524
0
            case 0:            /* Errors */
525
0
                state = "reading strings";
526
0
                ok = -1;
527
0
                goto err;
528
0
            default:           /* Success */
529
0
                ok = 0;
530
0
                break;
531
0
            }
532
0
        } else {
533
0
            ui->flags &= ~UI_FLAG_REDOABLE;
534
0
            ok = -2;
535
0
            goto err;
536
0
        }
537
0
    }
538
539
0
    state = NULL;
540
0
 err:
541
0
    if (ui->meth->ui_close_session != NULL
542
0
        && ui->meth->ui_close_session(ui) <= 0) {
543
0
        if (state == NULL)
544
0
            state = "closing session";
545
0
        ok = -1;
546
0
    }
547
548
0
    if (ok == -1) {
549
0
        UIerr(UI_F_UI_PROCESS, UI_R_PROCESSING_ERROR);
550
0
        ERR_add_error_data(2, "while ", state);
551
0
    }
552
0
    return ok;
553
0
}
554
555
int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
556
0
{
557
0
    if (ui == NULL) {
558
0
        UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
559
0
        return -1;
560
0
    }
561
0
    switch (cmd) {
562
0
    case UI_CTRL_PRINT_ERRORS:
563
0
        {
564
0
            int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS);
565
0
            if (i)
566
0
                ui->flags |= UI_FLAG_PRINT_ERRORS;
567
0
            else
568
0
                ui->flags &= ~UI_FLAG_PRINT_ERRORS;
569
0
            return save_flag;
570
0
        }
571
0
    case UI_CTRL_IS_REDOABLE:
572
0
        return ! !(ui->flags & UI_FLAG_REDOABLE);
573
0
    default:
574
0
        break;
575
0
    }
576
0
    UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
577
0
    return -1;
578
0
}
579
580
int UI_set_ex_data(UI *r, int idx, void *arg)
581
0
{
582
0
    return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
583
0
}
584
585
void *UI_get_ex_data(UI *r, int idx)
586
0
{
587
0
    return CRYPTO_get_ex_data(&r->ex_data, idx);
588
0
}
589
590
const UI_METHOD *UI_get_method(UI *ui)
591
0
{
592
0
    return ui->meth;
593
0
}
594
595
const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
596
0
{
597
0
    ui->meth = meth;
598
0
    return ui->meth;
599
0
}
600
601
UI_METHOD *UI_create_method(const char *name)
602
0
{
603
0
    UI_METHOD *ui_method = NULL;
604
605
0
    if ((ui_method = OPENSSL_zalloc(sizeof(*ui_method))) == NULL
606
0
        || (ui_method->name = OPENSSL_strdup(name)) == NULL
607
0
        || !CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
608
0
                               &ui_method->ex_data)) {
609
0
        if (ui_method)
610
0
            OPENSSL_free(ui_method->name);
611
0
        OPENSSL_free(ui_method);
612
0
        UIerr(UI_F_UI_CREATE_METHOD, ERR_R_MALLOC_FAILURE);
613
0
        return NULL;
614
0
    }
615
0
    return ui_method;
616
0
}
617
618
/*
619
 * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
620
 * (that is, it hasn't been allocated using UI_create_method(), you deserve
621
 * anything Murphy can throw at you and more! You have been warned.
622
 */
623
void UI_destroy_method(UI_METHOD *ui_method)
624
0
{
625
0
    if (ui_method == NULL)
626
0
        return;
627
0
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
628
0
                        &ui_method->ex_data);
629
0
    OPENSSL_free(ui_method->name);
630
0
    ui_method->name = NULL;
631
0
    OPENSSL_free(ui_method);
632
0
}
633
634
int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui))
635
0
{
636
0
    if (method != NULL) {
637
0
        method->ui_open_session = opener;
638
0
        return 0;
639
0
    }
640
0
    return -1;
641
0
}
642
643
int UI_method_set_writer(UI_METHOD *method,
644
                         int (*writer) (UI *ui, UI_STRING *uis))
645
0
{
646
0
    if (method != NULL) {
647
0
        method->ui_write_string = writer;
648
0
        return 0;
649
0
    }
650
0
    return -1;
651
0
}
652
653
int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
654
0
{
655
0
    if (method != NULL) {
656
0
        method->ui_flush = flusher;
657
0
        return 0;
658
0
    }
659
0
    return -1;
660
0
}
661
662
int UI_method_set_reader(UI_METHOD *method,
663
                         int (*reader) (UI *ui, UI_STRING *uis))
664
0
{
665
0
    if (method != NULL) {
666
0
        method->ui_read_string = reader;
667
0
        return 0;
668
0
    }
669
0
    return -1;
670
0
}
671
672
int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
673
0
{
674
0
    if (method != NULL) {
675
0
        method->ui_close_session = closer;
676
0
        return 0;
677
0
    }
678
0
    return -1;
679
0
}
680
681
int UI_method_set_data_duplicator(UI_METHOD *method,
682
                                  void *(*duplicator) (UI *ui, void *ui_data),
683
                                  void (*destructor)(UI *ui, void *ui_data))
684
0
{
685
0
    if (method != NULL) {
686
0
        method->ui_duplicate_data = duplicator;
687
0
        method->ui_destroy_data = destructor;
688
0
        return 0;
689
0
    }
690
0
    return -1;
691
0
}
692
693
int UI_method_set_prompt_constructor(UI_METHOD *method,
694
                                     char *(*prompt_constructor) (UI *ui,
695
                                                                  const char
696
                                                                  *object_desc,
697
                                                                  const char
698
                                                                  *object_name))
699
0
{
700
0
    if (method != NULL) {
701
0
        method->ui_construct_prompt = prompt_constructor;
702
0
        return 0;
703
0
    }
704
0
    return -1;
705
0
}
706
707
int UI_method_set_ex_data(UI_METHOD *method, int idx, void *data)
708
0
{
709
0
    return CRYPTO_set_ex_data(&method->ex_data, idx, data);
710
0
}
711
712
int (*UI_method_get_opener(const UI_METHOD *method)) (UI *)
713
0
{
714
0
    if (method != NULL)
715
0
        return method->ui_open_session;
716
0
    return NULL;
717
0
}
718
719
int (*UI_method_get_writer(const UI_METHOD *method)) (UI *, UI_STRING *)
720
0
{
721
0
    if (method != NULL)
722
0
        return method->ui_write_string;
723
0
    return NULL;
724
0
}
725
726
int (*UI_method_get_flusher(const UI_METHOD *method)) (UI *)
727
0
{
728
0
    if (method != NULL)
729
0
        return method->ui_flush;
730
0
    return NULL;
731
0
}
732
733
int (*UI_method_get_reader(const UI_METHOD *method)) (UI *, UI_STRING *)
734
0
{
735
0
    if (method != NULL)
736
0
        return method->ui_read_string;
737
0
    return NULL;
738
0
}
739
740
int (*UI_method_get_closer(const UI_METHOD *method)) (UI *)
741
0
{
742
0
    if (method != NULL)
743
0
        return method->ui_close_session;
744
0
    return NULL;
745
0
}
746
747
char *(*UI_method_get_prompt_constructor(const UI_METHOD *method))
748
    (UI *, const char *, const char *)
749
0
{
750
0
    if (method != NULL)
751
0
        return method->ui_construct_prompt;
752
0
    return NULL;
753
0
}
754
755
void *(*UI_method_get_data_duplicator(const UI_METHOD *method)) (UI *, void *)
756
0
{
757
0
    if (method != NULL)
758
0
        return method->ui_duplicate_data;
759
0
    return NULL;
760
0
}
761
762
void (*UI_method_get_data_destructor(const UI_METHOD *method)) (UI *, void *)
763
0
{
764
0
    if (method != NULL)
765
0
        return method->ui_destroy_data;
766
0
    return NULL;
767
0
}
768
769
const void *UI_method_get_ex_data(const UI_METHOD *method, int idx)
770
0
{
771
0
    return CRYPTO_get_ex_data(&method->ex_data, idx);
772
0
}
773
774
enum UI_string_types UI_get_string_type(UI_STRING *uis)
775
0
{
776
0
    return uis->type;
777
0
}
778
779
int UI_get_input_flags(UI_STRING *uis)
780
0
{
781
0
    return uis->input_flags;
782
0
}
783
784
const char *UI_get0_output_string(UI_STRING *uis)
785
0
{
786
0
    return uis->out_string;
787
0
}
788
789
const char *UI_get0_action_string(UI_STRING *uis)
790
0
{
791
0
    switch (uis->type) {
792
0
    case UIT_BOOLEAN:
793
0
        return uis->_.boolean_data.action_desc;
794
0
    case UIT_PROMPT:
795
0
    case UIT_NONE:
796
0
    case UIT_VERIFY:
797
0
    case UIT_INFO:
798
0
    case UIT_ERROR:
799
0
        break;
800
0
    }
801
0
    return NULL;
802
0
}
803
804
const char *UI_get0_result_string(UI_STRING *uis)
805
0
{
806
0
    switch (uis->type) {
807
0
    case UIT_PROMPT:
808
0
    case UIT_VERIFY:
809
0
        return uis->result_buf;
810
0
    case UIT_NONE:
811
0
    case UIT_BOOLEAN:
812
0
    case UIT_INFO:
813
0
    case UIT_ERROR:
814
0
        break;
815
0
    }
816
0
    return NULL;
817
0
}
818
819
int UI_get_result_string_length(UI_STRING *uis)
820
0
{
821
0
    switch (uis->type) {
822
0
    case UIT_PROMPT:
823
0
    case UIT_VERIFY:
824
0
        return uis->result_len;
825
0
    case UIT_NONE:
826
0
    case UIT_BOOLEAN:
827
0
    case UIT_INFO:
828
0
    case UIT_ERROR:
829
0
        break;
830
0
    }
831
0
    return -1;
832
0
}
833
834
const char *UI_get0_test_string(UI_STRING *uis)
835
0
{
836
0
    switch (uis->type) {
837
0
    case UIT_VERIFY:
838
0
        return uis->_.string_data.test_buf;
839
0
    case UIT_NONE:
840
0
    case UIT_BOOLEAN:
841
0
    case UIT_INFO:
842
0
    case UIT_ERROR:
843
0
    case UIT_PROMPT:
844
0
        break;
845
0
    }
846
0
    return NULL;
847
0
}
848
849
int UI_get_result_minsize(UI_STRING *uis)
850
0
{
851
0
    switch (uis->type) {
852
0
    case UIT_PROMPT:
853
0
    case UIT_VERIFY:
854
0
        return uis->_.string_data.result_minsize;
855
0
    case UIT_NONE:
856
0
    case UIT_INFO:
857
0
    case UIT_ERROR:
858
0
    case UIT_BOOLEAN:
859
0
        break;
860
0
    }
861
0
    return -1;
862
0
}
863
864
int UI_get_result_maxsize(UI_STRING *uis)
865
0
{
866
0
    switch (uis->type) {
867
0
    case UIT_PROMPT:
868
0
    case UIT_VERIFY:
869
0
        return uis->_.string_data.result_maxsize;
870
0
    case UIT_NONE:
871
0
    case UIT_INFO:
872
0
    case UIT_ERROR:
873
0
    case UIT_BOOLEAN:
874
0
        break;
875
0
    }
876
0
    return -1;
877
0
}
878
879
int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
880
0
{
881
#if 0
882
    /*
883
     * This is placed here solely to preserve UI_F_UI_SET_RESULT
884
     * To be removed for OpenSSL 1.2.0
885
     */
886
    UIerr(UI_F_UI_SET_RESULT, ERR_R_DISABLED);
887
#endif
888
0
    return UI_set_result_ex(ui, uis, result, strlen(result));
889
0
}
890
891
int UI_set_result_ex(UI *ui, UI_STRING *uis, const char *result, int len)
892
0
{
893
0
    ui->flags &= ~UI_FLAG_REDOABLE;
894
895
0
    switch (uis->type) {
896
0
    case UIT_PROMPT:
897
0
    case UIT_VERIFY:
898
0
        {
899
0
            char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
900
0
            char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
901
902
0
            BIO_snprintf(number1, sizeof(number1), "%d",
903
0
                         uis->_.string_data.result_minsize);
904
0
            BIO_snprintf(number2, sizeof(number2), "%d",
905
0
                         uis->_.string_data.result_maxsize);
906
907
0
            if (len < uis->_.string_data.result_minsize) {
908
0
                ui->flags |= UI_FLAG_REDOABLE;
909
0
                UIerr(UI_F_UI_SET_RESULT_EX, UI_R_RESULT_TOO_SMALL);
910
0
                ERR_add_error_data(5, "You must type in ",
911
0
                                   number1, " to ", number2, " characters");
912
0
                return -1;
913
0
            }
914
0
            if (len > uis->_.string_data.result_maxsize) {
915
0
                ui->flags |= UI_FLAG_REDOABLE;
916
0
                UIerr(UI_F_UI_SET_RESULT_EX, UI_R_RESULT_TOO_LARGE);
917
0
                ERR_add_error_data(5, "You must type in ",
918
0
                                   number1, " to ", number2, " characters");
919
0
                return -1;
920
0
            }
921
0
        }
922
923
0
        if (uis->result_buf == NULL) {
924
0
            UIerr(UI_F_UI_SET_RESULT_EX, UI_R_NO_RESULT_BUFFER);
925
0
            return -1;
926
0
        }
927
928
0
        memcpy(uis->result_buf, result, len);
929
0
        if (len <= uis->_.string_data.result_maxsize)
930
0
            uis->result_buf[len] = '\0';
931
0
        uis->result_len = len;
932
0
        break;
933
0
    case UIT_BOOLEAN:
934
0
        {
935
0
            const char *p;
936
937
0
            if (uis->result_buf == NULL) {
938
0
                UIerr(UI_F_UI_SET_RESULT_EX, UI_R_NO_RESULT_BUFFER);
939
0
                return -1;
940
0
            }
941
942
0
            uis->result_buf[0] = '\0';
943
0
            for (p = result; *p; p++) {
944
0
                if (strchr(uis->_.boolean_data.ok_chars, *p)) {
945
0
                    uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
946
0
                    break;
947
0
                }
948
0
                if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
949
0
                    uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];
950
0
                    break;
951
0
                }
952
0
            }
953
0
        }
954
0
    case UIT_NONE:
955
0
    case UIT_INFO:
956
0
    case UIT_ERROR:
957
0
        break;
958
0
    }
959
0
    return 0;
960
0
}