Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/passphrase.c
Line
Count
Source
1
/*
2
 * Copyright 2020-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 <openssl/err.h>
11
#include <openssl/ui.h>
12
#include <openssl/core_names.h>
13
#include "internal/cryptlib.h"
14
#include "internal/passphrase.h"
15
16
void ossl_pw_clear_passphrase_data(struct ossl_passphrase_data_st *data)
17
0
{
18
0
    if (data != NULL) {
19
0
        if (data->type == is_expl_passphrase)
20
0
            OPENSSL_clear_free(data->_.expl_passphrase.passphrase_copy,
21
0
                data->_.expl_passphrase.passphrase_len);
22
0
        ossl_pw_clear_passphrase_cache(data);
23
0
        memset(data, 0, sizeof(*data));
24
0
    }
25
0
}
26
27
void ossl_pw_clear_passphrase_cache(struct ossl_passphrase_data_st *data)
28
0
{
29
0
    OPENSSL_clear_free(data->cached_passphrase, data->cached_passphrase_len);
30
0
    data->cached_passphrase = NULL;
31
0
}
32
33
int ossl_pw_set_passphrase(struct ossl_passphrase_data_st *data,
34
    const unsigned char *passphrase,
35
    size_t passphrase_len)
36
0
{
37
0
    if (!ossl_assert(data != NULL && passphrase != NULL)) {
38
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
39
0
        return 0;
40
0
    }
41
0
    ossl_pw_clear_passphrase_data(data);
42
0
    data->type = is_expl_passphrase;
43
0
    data->_.expl_passphrase.passphrase_copy = passphrase_len != 0 ? OPENSSL_memdup(passphrase, passphrase_len)
44
0
                                                                  : OPENSSL_malloc(1);
45
0
    if (data->_.expl_passphrase.passphrase_copy == NULL)
46
0
        return 0;
47
0
    data->_.expl_passphrase.passphrase_len = passphrase_len;
48
0
    return 1;
49
0
}
50
51
int ossl_pw_set_pem_password_cb(struct ossl_passphrase_data_st *data,
52
    pem_password_cb *cb, void *cbarg)
53
0
{
54
0
    if (!ossl_assert(data != NULL && cb != NULL)) {
55
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
56
0
        return 0;
57
0
    }
58
0
    ossl_pw_clear_passphrase_data(data);
59
0
    data->type = is_pem_password;
60
0
    data->_.pem_password.password_cb = cb;
61
0
    data->_.pem_password.password_cbarg = cbarg;
62
0
    return 1;
63
0
}
64
65
int ossl_pw_set_ossl_passphrase_cb(struct ossl_passphrase_data_st *data,
66
    OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
67
0
{
68
0
    if (!ossl_assert(data != NULL && cb != NULL)) {
69
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
70
0
        return 0;
71
0
    }
72
0
    ossl_pw_clear_passphrase_data(data);
73
0
    data->type = is_ossl_passphrase;
74
0
    data->_.ossl_passphrase.passphrase_cb = cb;
75
0
    data->_.ossl_passphrase.passphrase_cbarg = cbarg;
76
0
    return 1;
77
0
}
78
79
int ossl_pw_set_ui_method(struct ossl_passphrase_data_st *data,
80
    const UI_METHOD *ui_method, void *ui_data)
81
0
{
82
0
    if (!ossl_assert(data != NULL && ui_method != NULL)) {
83
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
84
0
        return 0;
85
0
    }
86
0
    ossl_pw_clear_passphrase_data(data);
87
0
    data->type = is_ui_method;
88
0
    data->_.ui_method.ui_method = ui_method;
89
0
    data->_.ui_method.ui_method_data = ui_data;
90
0
    return 1;
91
0
}
92
93
int ossl_pw_enable_passphrase_caching(struct ossl_passphrase_data_st *data)
94
0
{
95
0
    data->flag_cache_passphrase = 1;
96
0
    return 1;
97
0
}
98
99
int ossl_pw_disable_passphrase_caching(struct ossl_passphrase_data_st *data)
100
0
{
101
0
    data->flag_cache_passphrase = 0;
102
0
    return 1;
103
0
}
104
105
/*-
106
 * UI_METHOD processor.  It differs from UI_UTIL_read_pw() like this:
107
 *
108
 * 1.  It constructs a prompt on its own, based on |prompt_info|.
109
 * 2.  It allocates a buffer for password and verification on its own
110
 *     to compensate for NUL terminator in UI password strings.
111
 * 3.  It raises errors.
112
 * 4.  It reports back the length of the prompted pass phrase.
113
 */
114
static int do_ui_passphrase(char *pass, size_t pass_size, size_t *pass_len,
115
    const char *prompt_info, int verify,
116
    const UI_METHOD *ui_method, void *ui_data)
117
0
{
118
0
    char *prompt = NULL, *ipass = NULL, *vpass = NULL;
119
0
    int prompt_idx = -1, verify_idx = -1, res;
120
0
    UI *ui = NULL;
121
0
    int ret = 0;
122
123
0
    if (!ossl_assert(pass != NULL && pass_size != 0 && pass_len != NULL)) {
124
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
125
0
        return 0;
126
0
    }
127
128
0
    if ((ui = UI_new()) == NULL) {
129
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
130
0
        return 0;
131
0
    }
132
133
0
    if (ui_method != NULL) {
134
0
        UI_set_method(ui, ui_method);
135
0
        if (ui_data != NULL)
136
0
            UI_add_user_data(ui, ui_data);
137
0
    }
138
139
    /* Get an application constructed prompt */
140
0
    prompt = UI_construct_prompt(ui, "pass phrase", prompt_info);
141
0
    if (prompt == NULL) {
142
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
143
0
        goto end;
144
0
    }
145
146
    /* Get a buffer for verification prompt */
147
0
    ipass = OPENSSL_zalloc(pass_size + 1);
148
0
    if (ipass == NULL)
149
0
        goto end;
150
151
0
    prompt_idx = UI_add_input_string(ui, prompt,
152
0
                     UI_INPUT_FLAG_DEFAULT_PWD,
153
0
                     ipass, 0, (int)pass_size)
154
0
        - 1;
155
0
    if (prompt_idx < 0) {
156
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
157
0
        goto end;
158
0
    }
159
160
0
    if (verify) {
161
        /* Get a buffer for verification prompt */
162
0
        vpass = OPENSSL_zalloc(pass_size + 1);
163
0
        if (vpass == NULL)
164
0
            goto end;
165
0
        verify_idx = UI_add_verify_string(ui, prompt,
166
0
                         UI_INPUT_FLAG_DEFAULT_PWD,
167
0
                         vpass, 0, (int)pass_size,
168
0
                         ipass)
169
0
            - 1;
170
0
        if (verify_idx < 0) {
171
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
172
0
            goto end;
173
0
        }
174
0
    }
175
176
0
    switch (UI_process(ui)) {
177
0
    case -2:
178
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERRUPTED_OR_CANCELLED);
179
0
        break;
180
0
    case -1:
181
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
182
0
        break;
183
0
    default:
184
0
        res = UI_get_result_length(ui, prompt_idx);
185
0
        if (res < 0) {
186
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
187
0
            break;
188
0
        }
189
0
        *pass_len = (size_t)res;
190
0
        memcpy(pass, ipass, *pass_len);
191
0
        ret = 1;
192
0
        break;
193
0
    }
194
195
0
end:
196
0
    OPENSSL_clear_free(vpass, pass_size + 1);
197
0
    OPENSSL_clear_free(ipass, pass_size + 1);
198
0
    OPENSSL_free(prompt);
199
0
    UI_free(ui);
200
0
    return ret;
201
0
}
202
203
/* Central pw prompting dispatcher */
204
int ossl_pw_get_passphrase(char *pass, size_t pass_size, size_t *pass_len,
205
    const OSSL_PARAM params[], int verify,
206
    struct ossl_passphrase_data_st *data)
207
0
{
208
0
    const char *source = NULL;
209
0
    size_t source_len = 0;
210
0
    const char *prompt_info = NULL;
211
0
    const UI_METHOD *ui_method = NULL;
212
0
    UI_METHOD *allocated_ui_method = NULL;
213
0
    void *ui_data = NULL;
214
0
    const OSSL_PARAM *p = NULL;
215
0
    int ret;
216
217
    /* Handle explicit and cached passphrases */
218
219
0
    if (data->type == is_expl_passphrase) {
220
0
        source = data->_.expl_passphrase.passphrase_copy;
221
0
        source_len = data->_.expl_passphrase.passphrase_len;
222
0
    } else if (data->flag_cache_passphrase && data->cached_passphrase != NULL) {
223
0
        source = data->cached_passphrase;
224
0
        source_len = data->cached_passphrase_len;
225
0
    }
226
227
0
    if (source != NULL) {
228
0
        if (source_len > pass_size)
229
0
            source_len = pass_size;
230
0
        memcpy(pass, source, source_len);
231
0
        *pass_len = source_len;
232
0
        return 1;
233
0
    }
234
235
    /* Handle the is_ossl_passphrase case...  that's pretty direct */
236
237
0
    if (data->type == is_ossl_passphrase) {
238
0
        OSSL_PASSPHRASE_CALLBACK *cb = data->_.ossl_passphrase.passphrase_cb;
239
0
        void *cbarg = data->_.ossl_passphrase.passphrase_cbarg;
240
241
0
        ret = cb(pass, pass_size, pass_len, params, cbarg);
242
0
        goto do_cache;
243
0
    }
244
245
    /* Handle the is_pem_password and is_ui_method cases */
246
247
0
    if ((p = OSSL_PARAM_locate_const(params,
248
0
             OSSL_PASSPHRASE_PARAM_INFO))
249
0
        != NULL) {
250
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING) {
251
0
            ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
252
0
                "Prompt info data type incorrect");
253
0
            return 0;
254
0
        }
255
0
        prompt_info = p->data;
256
0
    }
257
258
0
    if (data->type == is_pem_password) {
259
        /* We use a UI wrapper for PEM */
260
0
        pem_password_cb *cb = data->_.pem_password.password_cb;
261
262
0
        ui_method = allocated_ui_method = UI_UTIL_wrap_read_pem_callback(cb, verify);
263
0
        ui_data = data->_.pem_password.password_cbarg;
264
265
0
        if (ui_method == NULL) {
266
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
267
0
            return 0;
268
0
        }
269
0
    } else if (data->type == is_ui_method) {
270
0
        ui_method = data->_.ui_method.ui_method;
271
0
        ui_data = data->_.ui_method.ui_method_data;
272
0
    }
273
274
0
    if (ui_method == NULL) {
275
0
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
276
0
            "No password method specified");
277
0
        return 0;
278
0
    }
279
280
0
    ret = do_ui_passphrase(pass, pass_size, pass_len, prompt_info, verify,
281
0
        ui_method, ui_data);
282
283
0
    UI_destroy_method(allocated_ui_method);
284
285
0
do_cache:
286
0
    if (ret && data->flag_cache_passphrase) {
287
0
        if (data->cached_passphrase == NULL
288
0
            || *pass_len > data->cached_passphrase_len) {
289
0
            void *new_cache = OPENSSL_clear_realloc(data->cached_passphrase,
290
0
                data->cached_passphrase_len,
291
0
                *pass_len + 1);
292
293
0
            if (new_cache == NULL) {
294
0
                OPENSSL_cleanse(pass, *pass_len);
295
0
                return 0;
296
0
            }
297
0
            data->cached_passphrase = new_cache;
298
0
        }
299
0
        memcpy(data->cached_passphrase, pass, *pass_len);
300
0
        data->cached_passphrase[*pass_len] = '\0';
301
0
        data->cached_passphrase_len = *pass_len;
302
0
    }
303
304
0
    return ret;
305
0
}
306
307
static int ossl_pw_get_password(char *buf, int size, int rwflag,
308
    void *userdata, const char *info)
309
0
{
310
0
    size_t password_len = 0;
311
0
    OSSL_PARAM params[] = {
312
0
        OSSL_PARAM_utf8_string(OSSL_PASSPHRASE_PARAM_INFO, NULL, 0),
313
0
        OSSL_PARAM_END
314
0
    };
315
316
0
    params[0].data = (void *)info;
317
0
    if (ossl_pw_get_passphrase(buf, (size_t)size, &password_len, params,
318
0
            rwflag, userdata))
319
0
        return (int)password_len;
320
0
    return -1;
321
0
}
322
323
int ossl_pw_pem_password(char *buf, int size, int rwflag, void *userdata)
324
0
{
325
0
    return ossl_pw_get_password(buf, size, rwflag, userdata, "PEM");
326
0
}
327
328
int ossl_pw_pvk_password(char *buf, int size, int rwflag, void *userdata)
329
0
{
330
0
    return ossl_pw_get_password(buf, size, rwflag, userdata, "PVK");
331
0
}
332
333
int ossl_pw_passphrase_callback_enc(char *pass, size_t pass_size,
334
    size_t *pass_len,
335
    const OSSL_PARAM params[], void *arg)
336
0
{
337
0
    return ossl_pw_get_passphrase(pass, pass_size, pass_len, params, 1, arg);
338
0
}
339
340
int ossl_pw_passphrase_callback_dec(char *pass, size_t pass_size,
341
    size_t *pass_len,
342
    const OSSL_PARAM params[], void *arg)
343
0
{
344
0
    return ossl_pw_get_passphrase(pass, pass_size, pass_len, params, 0, arg);
345
0
}