Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/passphrase.c
Line
Count
Source (jump to first uncovered line)
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
1.53M
{
18
1.53M
    if (data != NULL) {
19
1.53M
        if (data->type == is_expl_passphrase)
20
0
            OPENSSL_clear_free(data->_.expl_passphrase.passphrase_copy,
21
1.53M
                               data->_.expl_passphrase.passphrase_len);
22
1.53M
        ossl_pw_clear_passphrase_cache(data);
23
1.53M
        memset(data, 0, sizeof(*data));
24
1.53M
    }
25
1.53M
}
26
27
void ossl_pw_clear_passphrase_cache(struct ossl_passphrase_data_st *data)
28
2.45M
{
29
2.45M
    OPENSSL_clear_free(data->cached_passphrase, data->cached_passphrase_len);
30
2.45M
    data->cached_passphrase = NULL;
31
2.45M
}
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 =
44
0
        passphrase_len != 0 ? OPENSSL_memdup(passphrase, passphrase_len)
45
0
                            : OPENSSL_malloc(1);
46
0
    if (data->_.expl_passphrase.passphrase_copy == NULL) {
47
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
48
0
        return 0;
49
0
    }
50
0
    data->_.expl_passphrase.passphrase_len = passphrase_len;
51
0
    return 1;
52
0
}
53
54
int ossl_pw_set_pem_password_cb(struct ossl_passphrase_data_st *data,
55
                                pem_password_cb *cb, void *cbarg)
56
110k
{
57
110k
    if (!ossl_assert(data != NULL && cb != NULL)) {
58
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
59
0
        return 0;
60
0
    }
61
110k
    ossl_pw_clear_passphrase_data(data);
62
110k
    data->type = is_pem_password;
63
110k
    data->_.pem_password.password_cb = cb;
64
110k
    data->_.pem_password.password_cbarg = cbarg;
65
110k
    return 1;
66
110k
}
67
68
int ossl_pw_set_ossl_passphrase_cb(struct ossl_passphrase_data_st *data,
69
                                   OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
70
24.1k
{
71
24.1k
    if (!ossl_assert(data != NULL && cb != NULL)) {
72
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
73
0
        return 0;
74
0
    }
75
24.1k
    ossl_pw_clear_passphrase_data(data);
76
24.1k
    data->type = is_ossl_passphrase;
77
24.1k
    data->_.ossl_passphrase.passphrase_cb = cb;
78
24.1k
    data->_.ossl_passphrase.passphrase_cbarg = cbarg;
79
24.1k
    return 1;
80
24.1k
}
81
82
int ossl_pw_set_ui_method(struct ossl_passphrase_data_st *data,
83
                          const UI_METHOD *ui_method, void *ui_data)
84
0
{
85
0
    if (!ossl_assert(data != NULL && ui_method != NULL)) {
86
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
87
0
        return 0;
88
0
    }
89
0
    ossl_pw_clear_passphrase_data(data);
90
0
    data->type = is_ui_method;
91
0
    data->_.ui_method.ui_method = ui_method;
92
0
    data->_.ui_method.ui_method_data = ui_data;
93
0
    return 1;
94
0
}
95
96
int ossl_pw_enable_passphrase_caching(struct ossl_passphrase_data_st *data)
97
972k
{
98
972k
    data->flag_cache_passphrase = 1;
99
972k
    return 1;
100
972k
}
101
102
int ossl_pw_disable_passphrase_caching(struct ossl_passphrase_data_st *data)
103
0
{
104
0
    data->flag_cache_passphrase = 0;
105
0
    return 1;
106
0
}
107
108
109
/*-
110
 * UI_METHOD processor.  It differs from UI_UTIL_read_pw() like this:
111
 *
112
 * 1.  It constructs a prompt on its own, based on |prompt_info|.
113
 * 2.  It allocates a buffer for password and verification on its own
114
 *     to compensate for NUL terminator in UI password strings.
115
 * 3.  It raises errors.
116
 * 4.  It reports back the length of the prompted pass phrase.
117
 */
118
static int do_ui_passphrase(char *pass, size_t pass_size, size_t *pass_len,
119
                            const char *prompt_info, int verify,
120
                            const UI_METHOD *ui_method, void *ui_data)
121
0
{
122
0
    char *prompt = NULL, *ipass = NULL, *vpass = NULL;
123
0
    int prompt_idx = -1, verify_idx = -1, res;
124
0
    UI *ui = NULL;
125
0
    int ret = 0;
126
127
0
    if (!ossl_assert(pass != NULL && pass_size != 0 && pass_len != NULL)) {
128
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
129
0
        return 0;
130
0
    }
131
132
0
    if ((ui = UI_new()) == NULL) {
133
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
134
0
        return 0;
135
0
    }
136
137
0
    if (ui_method != NULL) {
138
0
        UI_set_method(ui, ui_method);
139
0
        if (ui_data != NULL)
140
0
            UI_add_user_data(ui, ui_data);
141
0
    }
142
143
    /* Get an application constructed prompt */
144
0
    prompt = UI_construct_prompt(ui, "pass phrase", prompt_info);
145
0
    if (prompt == NULL) {
146
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
147
0
        goto end;
148
0
    }
149
150
    /* Get a buffer for verification prompt */
151
0
    ipass = OPENSSL_zalloc(pass_size + 1);
152
0
    if (ipass == NULL) {
153
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
154
0
        goto end;
155
0
    }
156
157
0
    prompt_idx = UI_add_input_string(ui, prompt,
158
0
                                     UI_INPUT_FLAG_DEFAULT_PWD,
159
0
                                     ipass, 0, pass_size) - 1;
160
0
    if (prompt_idx < 0) {
161
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
162
0
        goto end;
163
0
    }
164
165
0
    if (verify) {
166
        /* Get a buffer for verification prompt */
167
0
        vpass = OPENSSL_zalloc(pass_size + 1);
168
0
        if (vpass == NULL) {
169
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
170
0
            goto end;
171
0
        }
172
0
        verify_idx = UI_add_verify_string(ui, prompt,
173
0
                                          UI_INPUT_FLAG_DEFAULT_PWD,
174
0
                                          vpass, 0, pass_size,
175
0
                                          ipass) - 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
125
{
214
125
    const char *source = NULL;
215
125
    size_t source_len = 0;
216
125
    const char *prompt_info = NULL;
217
125
    const UI_METHOD *ui_method = NULL;
218
125
    UI_METHOD *allocated_ui_method = NULL;
219
125
    void *ui_data = NULL;
220
125
    const OSSL_PARAM *p = NULL;
221
125
    int ret;
222
223
    /* Handle explicit and cached passphrases */
224
225
125
    if (data->type == is_expl_passphrase) {
226
0
        source = data->_.expl_passphrase.passphrase_copy;
227
0
        source_len = data->_.expl_passphrase.passphrase_len;
228
125
    } 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
125
    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
125
    if (data->type == is_ossl_passphrase) {
244
10
        OSSL_PASSPHRASE_CALLBACK *cb = data->_.ossl_passphrase.passphrase_cb;
245
10
        void *cbarg = data->_.ossl_passphrase.passphrase_cbarg;
246
247
10
        ret = cb(pass, pass_size, pass_len, params, cbarg);
248
10
        goto do_cache;
249
10
    }
250
251
    /* Handle the is_pem_password and is_ui_method cases */
252
253
115
    if ((p = OSSL_PARAM_locate_const(params,
254
115
                                     OSSL_PASSPHRASE_PARAM_INFO)) != NULL) {
255
10
        if (p->data_type != OSSL_PARAM_UTF8_STRING) {
256
0
            ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
257
0
                           "Prompt info data type incorrect");
258
0
            return 0;
259
0
        }
260
10
        prompt_info = p->data;
261
10
    }
262
263
115
    if (data->type == is_pem_password) {
264
        /* We use a UI wrapper for PEM */
265
0
        pem_password_cb *cb = data->_.pem_password.password_cb;
266
267
0
        ui_method = allocated_ui_method =
268
0
            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
115
    } 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
115
    if (ui_method == NULL) {
281
115
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
282
115
                       "No password method specified");
283
115
        return 0;
284
115
    }
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
10
 do_cache:
292
10
    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 =
296
0
                OPENSSL_clear_realloc(data->cached_passphrase,
297
0
                                      data->cached_passphrase_len,
298
0
                                      *pass_len + 1);
299
300
0
            if (new_cache == NULL) {
301
0
                OPENSSL_cleanse(pass, *pass_len);
302
0
                ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
303
0
                return 0;
304
0
            }
305
0
            data->cached_passphrase = new_cache;
306
0
        }
307
0
        memcpy(data->cached_passphrase, pass, *pass_len);
308
0
        data->cached_passphrase[*pass_len] = '\0';
309
0
        data->cached_passphrase_len = *pass_len;
310
0
    }
311
312
10
    return ret;
313
10
}
314
315
static int ossl_pw_get_password(char *buf, int size, int rwflag,
316
                                void *userdata, const char *info)
317
10
{
318
10
    size_t password_len = 0;
319
10
    OSSL_PARAM params[] = {
320
10
        OSSL_PARAM_utf8_string(OSSL_PASSPHRASE_PARAM_INFO, NULL, 0),
321
10
        OSSL_PARAM_END
322
10
    };
323
324
10
    params[0].data = (void *)info;
325
10
    if (ossl_pw_get_passphrase(buf, (size_t)size, &password_len, params,
326
10
                               rwflag, userdata))
327
0
        return (int)password_len;
328
10
    return -1;
329
10
}
330
331
int ossl_pw_pem_password(char *buf, int size, int rwflag, void *userdata)
332
0
{
333
0
    return ossl_pw_get_password(buf, size, rwflag, userdata, "PEM");
334
0
}
335
336
int ossl_pw_pvk_password(char *buf, int size, int rwflag, void *userdata)
337
10
{
338
10
    return ossl_pw_get_password(buf, size, rwflag, userdata, "PVK");
339
10
}
340
341
int ossl_pw_passphrase_callback_enc(char *pass, size_t pass_size,
342
                                    size_t *pass_len,
343
                                    const OSSL_PARAM params[], void *arg)
344
0
{
345
0
    return ossl_pw_get_passphrase(pass, pass_size, pass_len, params, 1, arg);
346
0
}
347
348
int ossl_pw_passphrase_callback_dec(char *pass, size_t pass_size,
349
                                    size_t *pass_len,
350
                                    const OSSL_PARAM params[], void *arg)
351
115
{
352
115
    return ossl_pw_get_passphrase(pass, pass_size, pass_len, params, 0, arg);
353
115
}