Coverage Report

Created: 2023-04-12 06:22

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