Coverage Report

Created: 2025-08-26 06:04

/src/rauc/subprojects/openssl-3.0.8/crypto/ui/ui_util.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2002-2022 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 <openssl/pem.h>         /* PEM_def_callback() */
12
#include "internal/thread_once.h"
13
#include "ui_local.h"
14
15
#ifndef BUFSIZ
16
#define BUFSIZ 256
17
#endif
18
19
int UI_UTIL_read_pw_string(char *buf, int length, const char *prompt,
20
                           int verify)
21
0
{
22
0
    char buff[BUFSIZ];
23
0
    int ret;
24
25
0
    ret =
26
0
        UI_UTIL_read_pw(buf, buff, (length > BUFSIZ) ? BUFSIZ : length,
27
0
                        prompt, verify);
28
0
    OPENSSL_cleanse(buff, BUFSIZ);
29
0
    return ret;
30
0
}
31
32
int UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt,
33
                    int verify)
34
0
{
35
0
    int ok = 0;
36
0
    UI *ui;
37
38
0
    if (size < 1)
39
0
        return -1;
40
41
0
    ui = UI_new();
42
0
    if (ui != NULL) {
43
0
        ok = UI_add_input_string(ui, prompt, 0, buf, 0, size - 1);
44
0
        if (ok >= 0 && verify)
45
0
            ok = UI_add_verify_string(ui, prompt, 0, buff, 0, size - 1, buf);
46
0
        if (ok >= 0)
47
0
            ok = UI_process(ui);
48
0
        UI_free(ui);
49
0
    }
50
0
    if (ok > 0)
51
0
        ok = 0;
52
0
    return ok;
53
0
}
54
55
/*
56
 * Wrapper around pem_password_cb, a method to help older APIs use newer
57
 * ones.
58
 */
59
struct pem_password_cb_data {
60
    pem_password_cb *cb;
61
    int rwflag;
62
};
63
64
static void ui_new_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
65
                               int idx, long argl, void *argp)
66
0
{
67
    /*
68
     * Do nothing, the data is allocated externally and assigned later with
69
     * CRYPTO_set_ex_data()
70
     */
71
0
}
72
73
static int ui_dup_method_data(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
74
                              void **pptr, int idx, long argl, void *argp)
75
0
{
76
0
    if (*pptr != NULL) {
77
0
        *pptr = OPENSSL_memdup(*pptr, sizeof(struct pem_password_cb_data));
78
0
        if (*pptr != NULL)
79
0
            return 1;
80
0
    }
81
0
    return 0;
82
0
}
83
84
static void ui_free_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
85
                                int idx, long argl, void *argp)
86
0
{
87
0
    OPENSSL_free(ptr);
88
0
}
89
90
static CRYPTO_ONCE get_index_once = CRYPTO_ONCE_STATIC_INIT;
91
static int ui_method_data_index = -1;
92
DEFINE_RUN_ONCE_STATIC(ui_method_data_index_init)
93
0
{
94
0
    ui_method_data_index = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI_METHOD,
95
0
                                                   0, NULL, ui_new_method_data,
96
0
                                                   ui_dup_method_data,
97
0
                                                   ui_free_method_data);
98
0
    return 1;
99
0
}
100
101
static int ui_open(UI *ui)
102
0
{
103
0
    return 1;
104
0
}
105
static int ui_read(UI *ui, UI_STRING *uis)
106
0
{
107
0
    switch (UI_get_string_type(uis)) {
108
0
    case UIT_PROMPT:
109
0
        {
110
0
            char result[PEM_BUFSIZE + 1];
111
0
            const struct pem_password_cb_data *data =
112
0
                UI_method_get_ex_data(UI_get_method(ui), ui_method_data_index);
113
0
            int maxsize = UI_get_result_maxsize(uis);
114
0
            int len = data->cb(result,
115
0
                               maxsize > PEM_BUFSIZE ? PEM_BUFSIZE : maxsize,
116
0
                               data->rwflag, UI_get0_user_data(ui));
117
118
0
            if (len >= 0)
119
0
                result[len] = '\0';
120
0
            if (len < 0)
121
0
                return len;
122
0
            if (UI_set_result_ex(ui, uis, result, len) >= 0)
123
0
                return 1;
124
0
            return 0;
125
0
        }
126
0
    case UIT_VERIFY:
127
0
    case UIT_NONE:
128
0
    case UIT_BOOLEAN:
129
0
    case UIT_INFO:
130
0
    case UIT_ERROR:
131
0
        break;
132
0
    }
133
0
    return 1;
134
0
}
135
static int ui_write(UI *ui, UI_STRING *uis)
136
0
{
137
0
    return 1;
138
0
}
139
static int ui_close(UI *ui)
140
0
{
141
0
    return 1;
142
0
}
143
144
UI_METHOD *UI_UTIL_wrap_read_pem_callback(pem_password_cb *cb, int rwflag)
145
0
{
146
0
    struct pem_password_cb_data *data = NULL;
147
0
    UI_METHOD *ui_method = NULL;
148
149
0
    if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL
150
0
        || (ui_method = UI_create_method("PEM password callback wrapper")) == NULL
151
0
        || UI_method_set_opener(ui_method, ui_open) < 0
152
0
        || UI_method_set_reader(ui_method, ui_read) < 0
153
0
        || UI_method_set_writer(ui_method, ui_write) < 0
154
0
        || UI_method_set_closer(ui_method, ui_close) < 0
155
0
        || !RUN_ONCE(&get_index_once, ui_method_data_index_init)
156
0
        || !UI_method_set_ex_data(ui_method, ui_method_data_index, data)) {
157
0
        UI_destroy_method(ui_method);
158
0
        OPENSSL_free(data);
159
0
        return NULL;
160
0
    }
161
0
    data->rwflag = rwflag;
162
0
    data->cb = cb != NULL ? cb : PEM_def_callback;
163
164
0
    return ui_method;
165
0
}