Coverage Report

Created: 2025-12-31 06:58

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