Coverage Report

Created: 2026-10-16 07:31

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
        int rv;
372
373
0
        if (phrase_desc == NULL)
374
0
            return NULL;
375
0
        if (object_name != NULL)
376
0
            rv = ossl_asprintf(&prompt, "Enter %s for %s:",
377
0
                phrase_desc, object_name);
378
0
        else
379
0
            rv = ossl_asprintf(&prompt, "Enter %s:", phrase_desc);
380
0
        if (rv < 0)
381
0
            return NULL;
382
0
    }
383
0
    return prompt;
384
0
}
385
386
void *UI_add_user_data(UI *ui, void *user_data)
387
0
{
388
0
    void *old_data = ui->user_data;
389
390
0
    if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) {
391
0
        ui->meth->ui_destroy_data(ui, old_data);
392
0
        old_data = NULL;
393
0
    }
394
0
    ui->user_data = user_data;
395
0
    ui->flags &= ~UI_FLAG_DUPL_DATA;
396
0
    return old_data;
397
0
}
398
399
int UI_dup_user_data(UI *ui, void *user_data)
400
0
{
401
0
    void *duplicate = NULL;
402
403
0
    if (ui->meth->ui_duplicate_data == NULL
404
0
        || ui->meth->ui_destroy_data == NULL) {
405
0
        ERR_raise(ERR_LIB_UI, UI_R_USER_DATA_DUPLICATION_UNSUPPORTED);
406
0
        return -1;
407
0
    }
408
409
0
    duplicate = ui->meth->ui_duplicate_data(ui, user_data);
410
0
    if (duplicate == NULL) {
411
0
        ERR_raise(ERR_LIB_UI, ERR_R_UI_LIB);
412
0
        return -1;
413
0
    }
414
415
0
    (void)UI_add_user_data(ui, duplicate);
416
0
    ui->flags |= UI_FLAG_DUPL_DATA;
417
418
0
    return 0;
419
0
}
420
421
void *UI_get0_user_data(UI *ui)
422
0
{
423
0
    return ui->user_data;
424
0
}
425
426
const char *UI_get0_result(UI *ui, int i)
427
0
{
428
0
    if (i < 0) {
429
0
        ERR_raise(ERR_LIB_UI, UI_R_INDEX_TOO_SMALL);
430
0
        return NULL;
431
0
    }
432
0
    if (i >= sk_UI_STRING_num(ui->strings)) {
433
0
        ERR_raise(ERR_LIB_UI, UI_R_INDEX_TOO_LARGE);
434
0
        return NULL;
435
0
    }
436
0
    return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
437
0
}
438
439
int UI_get_result_length(UI *ui, int i)
440
0
{
441
0
    if (i < 0) {
442
0
        ERR_raise(ERR_LIB_UI, UI_R_INDEX_TOO_SMALL);
443
0
        return -1;
444
0
    }
445
0
    if (i >= sk_UI_STRING_num(ui->strings)) {
446
0
        ERR_raise(ERR_LIB_UI, UI_R_INDEX_TOO_LARGE);
447
0
        return -1;
448
0
    }
449
0
    return UI_get_result_string_length(sk_UI_STRING_value(ui->strings, i));
450
0
}
451
452
static int print_error(const char *str, size_t len, UI *ui)
453
0
{
454
0
    UI_STRING uis;
455
456
0
    memset(&uis, 0, sizeof(uis));
457
0
    uis.type = UIT_ERROR;
458
0
    uis.out_string = str;
459
460
0
    if (ui->meth->ui_write_string != NULL
461
0
        && ui->meth->ui_write_string(ui, &uis) <= 0)
462
0
        return -1;
463
0
    return 0;
464
0
}
465
466
int UI_process(UI *ui)
467
0
{
468
0
    int i, ok = 0;
469
0
    const char *state = "processing";
470
471
0
    if (ui->meth->ui_open_session != NULL
472
0
        && ui->meth->ui_open_session(ui) <= 0) {
473
0
        state = "opening session";
474
0
        ok = -1;
475
0
        goto err;
476
0
    }
477
478
0
    if (ui->flags & UI_FLAG_PRINT_ERRORS)
479
0
        ERR_print_errors_cb((int (*)(const char *, size_t, void *))
480
0
                                print_error,
481
0
            (void *)ui);
482
483
0
    for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
484
0
        if (ui->meth->ui_write_string != NULL
485
0
            && (ui->meth->ui_write_string(ui,
486
0
                    sk_UI_STRING_value(ui->strings, i))
487
0
                <= 0)) {
488
0
            state = "writing strings";
489
0
            ok = -1;
490
0
            goto err;
491
0
        }
492
0
    }
493
494
0
    if (ui->meth->ui_flush != NULL)
495
0
        switch (ui->meth->ui_flush(ui)) {
496
0
        case -1: /* Interrupt/Cancel/something... */
497
0
            ui->flags &= ~UI_FLAG_REDOABLE;
498
0
            ok = -2;
499
0
            goto err;
500
0
        case 0: /* Errors */
501
0
            state = "flushing";
502
0
            ok = -1;
503
0
            goto err;
504
0
        default: /* Success */
505
0
            ok = 0;
506
0
            break;
507
0
        }
508
509
0
    for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
510
0
        if (ui->meth->ui_read_string != NULL) {
511
0
            switch (ui->meth->ui_read_string(ui,
512
0
                sk_UI_STRING_value(ui->strings,
513
0
                    i))) {
514
0
            case -1: /* Interrupt/Cancel/something... */
515
0
                ui->flags &= ~UI_FLAG_REDOABLE;
516
0
                ok = -2;
517
0
                goto err;
518
0
            case 0: /* Errors */
519
0
                state = "reading strings";
520
0
                ok = -1;
521
0
                goto err;
522
0
            default: /* Success */
523
0
                ok = 0;
524
0
                break;
525
0
            }
526
0
        } else {
527
0
            ui->flags &= ~UI_FLAG_REDOABLE;
528
0
            ok = -2;
529
0
            goto err;
530
0
        }
531
0
    }
532
533
0
    state = NULL;
534
0
err:
535
0
    if (ui->meth->ui_close_session != NULL
536
0
        && ui->meth->ui_close_session(ui) <= 0) {
537
0
        if (state == NULL)
538
0
            state = "closing session";
539
0
        ok = -1;
540
0
    }
541
542
0
    if (ok == -1)
543
0
        ERR_raise_data(ERR_LIB_UI, UI_R_PROCESSING_ERROR, "while %s", state);
544
0
    return ok;
545
0
}
546
547
int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f)(void))
548
0
{
549
0
    if (ui == NULL) {
550
0
        ERR_raise(ERR_LIB_UI, ERR_R_PASSED_NULL_PARAMETER);
551
0
        return -1;
552
0
    }
553
0
    switch (cmd) {
554
0
    case UI_CTRL_PRINT_ERRORS: {
555
0
        int save_flag = !!(ui->flags & UI_FLAG_PRINT_ERRORS);
556
0
        if (i)
557
0
            ui->flags |= UI_FLAG_PRINT_ERRORS;
558
0
        else
559
0
            ui->flags &= ~UI_FLAG_PRINT_ERRORS;
560
0
        return save_flag;
561
0
    }
562
0
    case UI_CTRL_IS_REDOABLE:
563
0
        return !!(ui->flags & UI_FLAG_REDOABLE);
564
0
    default:
565
0
        break;
566
0
    }
567
0
    ERR_raise(ERR_LIB_UI, UI_R_UNKNOWN_CONTROL_COMMAND);
568
0
    return -1;
569
0
}
570
571
int UI_set_ex_data(UI *r, int idx, void *arg)
572
0
{
573
0
    return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
574
0
}
575
576
void *UI_get_ex_data(const UI *r, int idx)
577
0
{
578
0
    return CRYPTO_get_ex_data(&r->ex_data, idx);
579
0
}
580
581
const UI_METHOD *UI_get_method(UI *ui)
582
0
{
583
0
    return ui->meth;
584
0
}
585
586
const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
587
0
{
588
0
    ui->meth = meth;
589
0
    return ui->meth;
590
0
}
591
592
UI_METHOD *UI_create_method(const char *name)
593
0
{
594
0
    UI_METHOD *ui_method = NULL;
595
596
0
    if ((ui_method = OPENSSL_zalloc(sizeof(*ui_method))) == NULL
597
0
        || (ui_method->name = OPENSSL_strdup(name)) == NULL
598
0
        || !CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
599
0
            &ui_method->ex_data)) {
600
601
0
        if (ui_method != NULL) {
602
0
            if (ui_method->name != NULL)
603
                /*
604
                 * These conditions indicate that the CRYPTO_new_ex_data()
605
                 * call failed.
606
                 */
607
0
                ERR_raise(ERR_LIB_UI, ERR_R_CRYPTO_LIB);
608
0
            OPENSSL_free(ui_method->name);
609
0
        }
610
0
        OPENSSL_free(ui_method);
611
0
        return NULL;
612
0
    }
613
0
    return ui_method;
614
0
}
615
616
/*
617
 * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
618
 * (that is, it hasn't been allocated using UI_create_method(), you deserve
619
 * anything Murphy can throw at you and more! You have been warned.
620
 */
621
void UI_destroy_method(UI_METHOD *ui_method)
622
0
{
623
0
    if (ui_method == NULL)
624
0
        return;
625
0
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method,
626
0
        &ui_method->ex_data);
627
0
    OPENSSL_free(ui_method->name);
628
0
    ui_method->name = NULL;
629
0
    OPENSSL_free(ui_method);
630
0
}
631
632
int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui))
633
0
{
634
0
    if (method != NULL) {
635
0
        method->ui_open_session = opener;
636
0
        return 0;
637
0
    }
638
0
    return -1;
639
0
}
640
641
int UI_method_set_writer(UI_METHOD *method,
642
    int (*writer)(UI *ui, UI_STRING *uis))
643
0
{
644
0
    if (method != NULL) {
645
0
        method->ui_write_string = writer;
646
0
        return 0;
647
0
    }
648
0
    return -1;
649
0
}
650
651
int UI_method_set_flusher(UI_METHOD *method, int (*flusher)(UI *ui))
652
0
{
653
0
    if (method != NULL) {
654
0
        method->ui_flush = flusher;
655
0
        return 0;
656
0
    }
657
0
    return -1;
658
0
}
659
660
int UI_method_set_reader(UI_METHOD *method,
661
    int (*reader)(UI *ui, UI_STRING *uis))
662
0
{
663
0
    if (method != NULL) {
664
0
        method->ui_read_string = reader;
665
0
        return 0;
666
0
    }
667
0
    return -1;
668
0
}
669
670
int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
671
0
{
672
0
    if (method != NULL) {
673
0
        method->ui_close_session = closer;
674
0
        return 0;
675
0
    }
676
0
    return -1;
677
0
}
678
679
int UI_method_set_data_duplicator(UI_METHOD *method,
680
    void *(*duplicator)(UI *ui, void *ui_data),
681
    void (*destructor)(UI *ui, void *ui_data))
682
0
{
683
0
    if (method != NULL) {
684
0
        method->ui_duplicate_data = duplicator;
685
0
        method->ui_destroy_data = destructor;
686
0
        return 0;
687
0
    }
688
0
    return -1;
689
0
}
690
691
int UI_method_set_prompt_constructor(UI_METHOD *method,
692
    char *(*prompt_constructor)(UI *ui,
693
        const char *,
694
        const char *))
695
0
{
696
0
    if (method != NULL) {
697
0
        method->ui_construct_prompt = prompt_constructor;
698
0
        return 0;
699
0
    }
700
0
    return -1;
701
0
}
702
703
int UI_method_set_ex_data(UI_METHOD *method, int idx, void *data)
704
0
{
705
0
    return CRYPTO_set_ex_data(&method->ex_data, idx, data);
706
0
}
707
708
int (*UI_method_get_opener(const UI_METHOD *method))(UI *)
709
0
{
710
0
    if (method != NULL)
711
0
        return method->ui_open_session;
712
0
    return NULL;
713
0
}
714
715
int (*UI_method_get_writer(const UI_METHOD *method))(UI *, UI_STRING *)
716
0
{
717
0
    if (method != NULL)
718
0
        return method->ui_write_string;
719
0
    return NULL;
720
0
}
721
722
int (*UI_method_get_flusher(const UI_METHOD *method))(UI *)
723
0
{
724
0
    if (method != NULL)
725
0
        return method->ui_flush;
726
0
    return NULL;
727
0
}
728
729
int (*UI_method_get_reader(const UI_METHOD *method))(UI *, UI_STRING *)
730
0
{
731
0
    if (method != NULL)
732
0
        return method->ui_read_string;
733
0
    return NULL;
734
0
}
735
736
int (*UI_method_get_closer(const UI_METHOD *method))(UI *)
737
0
{
738
0
    if (method != NULL)
739
0
        return method->ui_close_session;
740
0
    return NULL;
741
0
}
742
743
char *(*UI_method_get_prompt_constructor(const UI_METHOD *method))(UI *, const char *, const char *)
744
0
{
745
0
    if (method != NULL)
746
0
        return method->ui_construct_prompt;
747
0
    return NULL;
748
0
}
749
750
void *(*UI_method_get_data_duplicator(const UI_METHOD *method))(UI *, void *)
751
0
{
752
0
    if (method != NULL)
753
0
        return method->ui_duplicate_data;
754
0
    return NULL;
755
0
}
756
757
void (*UI_method_get_data_destructor(const UI_METHOD *method))(UI *, void *)
758
0
{
759
0
    if (method != NULL)
760
0
        return method->ui_destroy_data;
761
0
    return NULL;
762
0
}
763
764
const void *UI_method_get_ex_data(const UI_METHOD *method, int idx)
765
0
{
766
0
    return CRYPTO_get_ex_data(&method->ex_data, idx);
767
0
}
768
769
enum UI_string_types UI_get_string_type(UI_STRING *uis)
770
0
{
771
0
    return uis->type;
772
0
}
773
774
int UI_get_input_flags(UI_STRING *uis)
775
0
{
776
0
    return uis->input_flags;
777
0
}
778
779
const char *UI_get0_output_string(UI_STRING *uis)
780
0
{
781
0
    return uis->out_string;
782
0
}
783
784
const char *UI_get0_action_string(UI_STRING *uis)
785
0
{
786
0
    switch (uis->type) {
787
0
    case UIT_BOOLEAN:
788
0
        return uis->_.boolean_data.action_desc;
789
0
    case UIT_PROMPT:
790
0
    case UIT_NONE:
791
0
    case UIT_VERIFY:
792
0
    case UIT_INFO:
793
0
    case UIT_ERROR:
794
0
        break;
795
0
    }
796
0
    return NULL;
797
0
}
798
799
const char *UI_get0_result_string(UI_STRING *uis)
800
0
{
801
0
    switch (uis->type) {
802
0
    case UIT_PROMPT:
803
0
    case UIT_VERIFY:
804
0
        return uis->result_buf;
805
0
    case UIT_NONE:
806
0
    case UIT_BOOLEAN:
807
0
    case UIT_INFO:
808
0
    case UIT_ERROR:
809
0
        break;
810
0
    }
811
0
    return NULL;
812
0
}
813
814
int UI_get_result_string_length(UI_STRING *uis)
815
0
{
816
0
    switch (uis->type) {
817
0
    case UIT_PROMPT:
818
0
    case UIT_VERIFY:
819
0
        return (int)uis->result_len;
820
0
    case UIT_NONE:
821
0
    case UIT_BOOLEAN:
822
0
    case UIT_INFO:
823
0
    case UIT_ERROR:
824
0
        break;
825
0
    }
826
0
    return -1;
827
0
}
828
829
const char *UI_get0_test_string(UI_STRING *uis)
830
0
{
831
0
    switch (uis->type) {
832
0
    case UIT_VERIFY:
833
0
        return uis->_.string_data.test_buf;
834
0
    case UIT_NONE:
835
0
    case UIT_BOOLEAN:
836
0
    case UIT_INFO:
837
0
    case UIT_ERROR:
838
0
    case UIT_PROMPT:
839
0
        break;
840
0
    }
841
0
    return NULL;
842
0
}
843
844
int UI_get_result_minsize(UI_STRING *uis)
845
0
{
846
0
    switch (uis->type) {
847
0
    case UIT_PROMPT:
848
0
    case UIT_VERIFY:
849
0
        return uis->_.string_data.result_minsize;
850
0
    case UIT_NONE:
851
0
    case UIT_INFO:
852
0
    case UIT_ERROR:
853
0
    case UIT_BOOLEAN:
854
0
        break;
855
0
    }
856
0
    return -1;
857
0
}
858
859
int UI_get_result_maxsize(UI_STRING *uis)
860
0
{
861
0
    switch (uis->type) {
862
0
    case UIT_PROMPT:
863
0
    case UIT_VERIFY:
864
0
        return uis->_.string_data.result_maxsize;
865
0
    case UIT_NONE:
866
0
    case UIT_INFO:
867
0
    case UIT_ERROR:
868
0
    case UIT_BOOLEAN:
869
0
        break;
870
0
    }
871
0
    return -1;
872
0
}
873
874
int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
875
0
{
876
0
    return UI_set_result_ex(ui, uis, result, (int)strlen(result));
877
0
}
878
879
int UI_set_result_ex(UI *ui, UI_STRING *uis, const char *result, int len)
880
0
{
881
0
    ui->flags &= ~UI_FLAG_REDOABLE;
882
883
0
    switch (uis->type) {
884
0
    case UIT_PROMPT:
885
0
    case UIT_VERIFY:
886
0
        if (len < uis->_.string_data.result_minsize) {
887
0
            ui->flags |= UI_FLAG_REDOABLE;
888
0
            ERR_raise_data(ERR_LIB_UI, UI_R_RESULT_TOO_SMALL,
889
0
                "You must type in %d to %d characters",
890
0
                uis->_.string_data.result_minsize,
891
0
                uis->_.string_data.result_maxsize);
892
0
            return -1;
893
0
        }
894
0
        if (len > uis->_.string_data.result_maxsize) {
895
0
            ui->flags |= UI_FLAG_REDOABLE;
896
0
            ERR_raise_data(ERR_LIB_UI, UI_R_RESULT_TOO_LARGE,
897
0
                "You must type in %d to %d characters",
898
0
                uis->_.string_data.result_minsize,
899
0
                uis->_.string_data.result_maxsize);
900
0
            return -1;
901
0
        }
902
903
0
        if (uis->result_buf == NULL) {
904
0
            ERR_raise(ERR_LIB_UI, UI_R_NO_RESULT_BUFFER);
905
0
            return -1;
906
0
        }
907
908
0
        memcpy(uis->result_buf, result, len);
909
0
        if (len <= uis->_.string_data.result_maxsize)
910
0
            uis->result_buf[len] = '\0';
911
0
        uis->result_len = len;
912
0
        break;
913
0
    case UIT_BOOLEAN: {
914
0
        const char *p;
915
916
0
        if (uis->result_buf == NULL) {
917
0
            ERR_raise(ERR_LIB_UI, UI_R_NO_RESULT_BUFFER);
918
0
            return -1;
919
0
        }
920
921
0
        uis->result_buf[0] = '\0';
922
0
        for (p = result; *p; p++) {
923
0
            if (strchr(uis->_.boolean_data.ok_chars, *p)) {
924
0
                uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
925
0
                break;
926
0
            }
927
0
            if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
928
0
                uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];
929
0
                break;
930
0
            }
931
0
        }
932
0
    }
933
0
    case UIT_NONE:
934
0
    case UIT_INFO:
935
0
    case UIT_ERROR:
936
0
        break;
937
0
    }
938
0
    return 0;
939
0
}