Coverage Report

Created: 2025-12-10 06:24

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