Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/store/store_result.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2023 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 "internal/e_os.h"
11
#include <string.h>
12
13
#include <openssl/core.h>
14
#include <openssl/core_names.h>
15
#include <openssl/core_object.h>
16
#include <openssl/err.h>
17
#include <openssl/pkcs12.h>
18
#include <openssl/provider.h>
19
#include <openssl/decoder.h>
20
#include <openssl/store.h>
21
#include "internal/provider.h"
22
#include "internal/passphrase.h"
23
#include "crypto/evp.h"
24
#include "crypto/x509.h"
25
#include "store_local.h"
26
27
#ifndef OSSL_OBJECT_PKCS12
28
/*
29
 * The object abstraction doesn't know PKCS#12, but we want to indicate
30
 * it anyway, so we create our own.  Since the public macros use positive
31
 * numbers, negative ones should be fine.  They must never slip out from
32
 * this translation unit anyway.
33
 */
34
0
#define OSSL_OBJECT_PKCS12 -1
35
#endif
36
37
/*
38
 * ossl_store_handle_load_result() is initially written to be a companion
39
 * to our 'file:' scheme provider implementation, but has been made generic
40
 * to serve others as well.
41
 *
42
 * This result handler takes any object abstraction (see provider-object(7))
43
 * and does the best it can with it.  If the object is passed by value (not
44
 * by reference), the contents are currently expected to be DER encoded.
45
 * If an object type is specified, that will be respected; otherwise, this
46
 * handler will guess the contents, by trying the following in order:
47
 *
48
 * 1.  Decode it into an EVP_PKEY, using OSSL_DECODER.
49
 * 2.  Decode it into an X.509 certificate, using d2i_X509 / d2i_X509_AUX.
50
 * 3.  Decode it into an X.509 CRL, using d2i_X509_CRL.
51
 * 4.  Decode it into a PKCS#12 structure, using d2i_PKCS12 (*).
52
 *
53
 * For the 'file:' scheme implementation, this is division of labor.  Since
54
 * the libcrypto <-> provider interface currently doesn't support certain
55
 * structures as first class objects, they must be unpacked from DER here
56
 * rather than in the provider.  The current exception is asymmetric keys,
57
 * which can reside within the provider boundary, most of all thanks to
58
 * OSSL_FUNC_keymgmt_load(), which allows loading the key material by
59
 * reference.
60
 */
61
62
struct extracted_param_data_st {
63
    int object_type;
64
    const char *data_type;
65
    const char *data_structure;
66
    const char *utf8_data;
67
    const void *octet_data;
68
    size_t octet_data_size;
69
    const void *ref;
70
    size_t ref_size;
71
    const char *desc;
72
};
73
74
static int try_name(struct extracted_param_data_st *, OSSL_STORE_INFO **);
75
static int try_key(struct extracted_param_data_st *, OSSL_STORE_INFO **,
76
    OSSL_STORE_CTX *, const OSSL_PROVIDER *,
77
    OSSL_LIB_CTX *, const char *);
78
static int try_cert(struct extracted_param_data_st *, OSSL_STORE_INFO **,
79
    OSSL_LIB_CTX *, const char *);
80
static int try_crl(struct extracted_param_data_st *, OSSL_STORE_INFO **,
81
    OSSL_LIB_CTX *, const char *);
82
static int try_pkcs12(struct extracted_param_data_st *, OSSL_STORE_INFO **,
83
    OSSL_STORE_CTX *, OSSL_LIB_CTX *, const char *);
84
85
int ossl_store_handle_load_result(const OSSL_PARAM params[], void *arg)
86
0
{
87
0
    struct ossl_load_result_data_st *cbdata = arg;
88
0
    OSSL_STORE_INFO **v = &cbdata->v;
89
0
    OSSL_STORE_CTX *ctx = cbdata->ctx;
90
0
    const OSSL_PROVIDER *provider = OSSL_STORE_LOADER_get0_provider(ctx->fetched_loader);
91
0
    OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
92
0
    const char *propq = ctx->properties;
93
0
    const OSSL_PARAM *p;
94
0
    struct extracted_param_data_st helper_data;
95
96
0
    memset(&helper_data, 0, sizeof(helper_data));
97
0
    helper_data.object_type = OSSL_OBJECT_UNKNOWN;
98
99
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_TYPE)) != NULL
100
0
        && !OSSL_PARAM_get_int(p, &helper_data.object_type))
101
0
        return 0;
102
0
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
103
0
    if (p != NULL
104
0
        && !OSSL_PARAM_get_utf8_string_ptr(p, &helper_data.data_type))
105
0
        return 0;
106
0
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA);
107
0
    if (p != NULL
108
0
        && !OSSL_PARAM_get_octet_string_ptr(p, &helper_data.octet_data,
109
0
            &helper_data.octet_data_size)
110
0
        && !OSSL_PARAM_get_utf8_string_ptr(p, &helper_data.utf8_data))
111
0
        return 0;
112
0
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE);
113
0
    if (p != NULL
114
0
        && !OSSL_PARAM_get_utf8_string_ptr(p, &helper_data.data_structure))
115
0
        return 0;
116
0
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_REFERENCE);
117
0
    if (p != NULL && !OSSL_PARAM_get_octet_string_ptr(p, &helper_data.ref, &helper_data.ref_size))
118
0
        return 0;
119
0
    p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DESC);
120
0
    if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &helper_data.desc))
121
0
        return 0;
122
123
    /*
124
     * The helper functions return 0 on actual errors, otherwise 1, even if
125
     * they didn't fill out |*v|.
126
     */
127
0
    ERR_set_mark();
128
0
    if (*v == NULL && !try_name(&helper_data, v))
129
0
        goto err;
130
0
    ERR_pop_to_mark();
131
0
    ERR_set_mark();
132
0
    if (*v == NULL && !try_key(&helper_data, v, ctx, provider, libctx, propq))
133
0
        goto err;
134
0
    ERR_pop_to_mark();
135
0
    ERR_set_mark();
136
0
    if (*v == NULL && !try_cert(&helper_data, v, libctx, propq))
137
0
        goto err;
138
0
    ERR_pop_to_mark();
139
0
    ERR_set_mark();
140
0
    if (*v == NULL && !try_crl(&helper_data, v, libctx, propq))
141
0
        goto err;
142
0
    ERR_pop_to_mark();
143
0
    ERR_set_mark();
144
0
    if (*v == NULL && !try_pkcs12(&helper_data, v, ctx, libctx, propq))
145
0
        goto err;
146
0
    ERR_pop_to_mark();
147
148
0
    if (*v == NULL)
149
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_UNSUPPORTED);
150
151
0
    return (*v != NULL);
152
0
err:
153
0
    ERR_clear_last_mark();
154
0
    return 0;
155
0
}
156
157
static int try_name(struct extracted_param_data_st *data, OSSL_STORE_INFO **v)
158
0
{
159
0
    if (data->object_type == OSSL_OBJECT_NAME) {
160
0
        char *newname = NULL, *newdesc = NULL;
161
162
0
        if (data->utf8_data == NULL)
163
0
            return 0;
164
0
        if ((newname = OPENSSL_strdup(data->utf8_data)) == NULL
165
0
            || (data->desc != NULL
166
0
                && (newdesc = OPENSSL_strdup(data->desc)) == NULL)
167
0
            || (*v = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
168
0
            OPENSSL_free(newname);
169
0
            OPENSSL_free(newdesc);
170
0
            return 0;
171
0
        }
172
0
        OSSL_STORE_INFO_set0_NAME_description(*v, newdesc);
173
0
    }
174
0
    return 1;
175
0
}
176
177
/*
178
 * For the rest of the object types, the provider code may not know what
179
 * type of data it gave us, so we may need to figure that out on our own.
180
 * Therefore, we do check for OSSL_OBJECT_UNKNOWN everywhere below, and
181
 * only return 0 on error if the object type is known.
182
 */
183
184
static EVP_PKEY *try_key_ref(struct extracted_param_data_st *data,
185
    OSSL_STORE_CTX *ctx,
186
    const OSSL_PROVIDER *provider,
187
    OSSL_LIB_CTX *libctx, const char *propq)
188
0
{
189
0
    EVP_PKEY *pk = NULL;
190
0
    EVP_KEYMGMT *keymgmt = NULL;
191
0
    void *keydata = NULL;
192
0
    int try_fallback = 2;
193
194
    /* If we have an object reference, we must have a data type */
195
0
    if (data->data_type == NULL)
196
0
        return 0;
197
198
0
    keymgmt = EVP_KEYMGMT_fetch(libctx, data->data_type, propq);
199
0
    ERR_set_mark();
200
0
    while (keymgmt != NULL && keydata == NULL && try_fallback-- > 0) {
201
        /*
202
         * There are two possible cases
203
         *
204
         * 1.  The keymgmt is from the same provider as the loader,
205
         *     so we can use evp_keymgmt_load()
206
         * 2.  The keymgmt is from another provider, then we must
207
         *     do the export/import dance.
208
         */
209
0
        if (EVP_KEYMGMT_get0_provider(keymgmt) == provider) {
210
            /* no point trying fallback here */
211
0
            try_fallback = 0;
212
0
            keydata = evp_keymgmt_load(keymgmt, data->ref, data->ref_size);
213
0
        } else {
214
0
            struct evp_keymgmt_util_try_import_data_st import_data;
215
0
            OSSL_FUNC_store_export_object_fn *export_object = ctx->fetched_loader->p_export_object;
216
217
0
            import_data.keymgmt = keymgmt;
218
0
            import_data.keydata = NULL;
219
0
            import_data.selection = OSSL_KEYMGMT_SELECT_ALL;
220
221
0
            if (export_object != NULL) {
222
                /*
223
                 * No need to check for errors here, the value of
224
                 * |import_data.keydata| is as much an indicator.
225
                 */
226
0
                (void)export_object(ctx->loader_ctx,
227
0
                    data->ref, data->ref_size,
228
0
                    &evp_keymgmt_util_try_import,
229
0
                    &import_data);
230
0
            }
231
232
0
            keydata = import_data.keydata;
233
0
        }
234
235
0
        if (keydata == NULL && try_fallback > 0) {
236
0
            EVP_KEYMGMT_free(keymgmt);
237
0
            keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)provider,
238
0
                data->data_type, propq);
239
0
            if (keymgmt != NULL) {
240
0
                ERR_pop_to_mark();
241
0
                ERR_set_mark();
242
0
            }
243
0
        }
244
0
    }
245
0
    if (keydata != NULL) {
246
0
        ERR_pop_to_mark();
247
0
        pk = evp_keymgmt_util_make_pkey(keymgmt, keydata);
248
0
    } else {
249
0
        ERR_clear_last_mark();
250
0
    }
251
0
    EVP_KEYMGMT_free(keymgmt);
252
253
0
    return pk;
254
0
}
255
256
static EVP_PKEY *try_key_value(struct extracted_param_data_st *data,
257
    OSSL_STORE_CTX *ctx,
258
    OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg,
259
    OSSL_LIB_CTX *libctx, const char *propq)
260
0
{
261
0
    EVP_PKEY *pk = NULL;
262
0
    OSSL_DECODER_CTX *decoderctx = NULL;
263
0
    const unsigned char *pdata = data->octet_data;
264
0
    size_t pdatalen = data->octet_data_size;
265
0
    int selection = 0;
266
267
0
    switch (ctx->expected_type) {
268
0
    case 0:
269
0
        break;
270
0
    case OSSL_STORE_INFO_PARAMS:
271
0
        selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
272
0
        break;
273
0
    case OSSL_STORE_INFO_PUBKEY:
274
0
        selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY
275
0
            | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
276
0
        break;
277
0
    case OSSL_STORE_INFO_PKEY:
278
0
        selection = OSSL_KEYMGMT_SELECT_ALL;
279
0
        break;
280
0
    default:
281
0
        return NULL;
282
0
    }
283
284
0
    decoderctx = OSSL_DECODER_CTX_new_for_pkey(&pk, NULL, data->data_structure,
285
0
        data->data_type, selection, libctx,
286
0
        propq);
287
0
    (void)OSSL_DECODER_CTX_set_passphrase_cb(decoderctx, cb, cbarg);
288
289
    /* No error if this couldn't be decoded */
290
0
    (void)OSSL_DECODER_from_data(decoderctx, &pdata, &pdatalen);
291
292
0
    OSSL_DECODER_CTX_free(decoderctx);
293
294
0
    return pk;
295
0
}
296
297
typedef OSSL_STORE_INFO *store_info_new_fn(EVP_PKEY *);
298
299
static EVP_PKEY *try_key_value_legacy(struct extracted_param_data_st *data,
300
    store_info_new_fn **store_info_new,
301
    OSSL_STORE_CTX *ctx,
302
    OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg,
303
    OSSL_LIB_CTX *libctx, const char *propq)
304
0
{
305
0
    EVP_PKEY *pk = NULL;
306
0
    const unsigned char *der = data->octet_data, *derp;
307
0
    long der_len = (long)data->octet_data_size;
308
309
    /* Try PUBKEY first, that's a real easy target */
310
0
    if (ctx->expected_type == 0
311
0
        || ctx->expected_type == OSSL_STORE_INFO_PUBKEY) {
312
0
        derp = der;
313
0
        pk = d2i_PUBKEY_ex(NULL, &derp, der_len, libctx, propq);
314
315
0
        if (pk != NULL)
316
0
            *store_info_new = OSSL_STORE_INFO_new_PUBKEY;
317
0
    }
318
319
    /* Try private keys next */
320
0
    if (pk == NULL
321
0
        && (ctx->expected_type == 0
322
0
            || ctx->expected_type == OSSL_STORE_INFO_PKEY)) {
323
0
        unsigned char *new_der = NULL;
324
0
        X509_SIG *p8 = NULL;
325
0
        PKCS8_PRIV_KEY_INFO *p8info = NULL;
326
327
        /* See if it's an encrypted PKCS#8 and decrypt it. */
328
0
        derp = der;
329
0
        p8 = d2i_X509_SIG(NULL, &derp, der_len);
330
331
0
        if (p8 != NULL) {
332
0
            char pbuf[PEM_BUFSIZE];
333
0
            size_t plen = 0;
334
335
0
            if (!cb(pbuf, sizeof(pbuf), &plen, NULL, cbarg)) {
336
0
                ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_BAD_PASSWORD_READ);
337
0
            } else {
338
0
                const X509_ALGOR *alg = NULL;
339
0
                const ASN1_OCTET_STRING *oct = NULL;
340
0
                int len = 0;
341
342
0
                X509_SIG_get0(p8, &alg, &oct);
343
344
                /*
345
                 * No need to check the returned value, |new_der|
346
                 * will be NULL on error anyway.
347
                 */
348
0
                PKCS12_pbe_crypt(alg, pbuf, plen,
349
0
                    oct->data, oct->length,
350
0
                    &new_der, &len, 0);
351
0
                der_len = len;
352
0
                der = new_der;
353
0
            }
354
0
            X509_SIG_free(p8);
355
0
        }
356
357
        /*
358
         * If the encrypted PKCS#8 couldn't be decrypted,
359
         * |der| is NULL
360
         */
361
0
        if (der != NULL) {
362
            /* Try to unpack an unencrypted PKCS#8, that's easy */
363
0
            derp = der;
364
0
            p8info = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp, der_len);
365
366
0
            if (p8info != NULL) {
367
0
                pk = EVP_PKCS82PKEY_ex(p8info, libctx, propq);
368
0
                PKCS8_PRIV_KEY_INFO_free(p8info);
369
0
            }
370
0
        }
371
372
0
        if (pk != NULL)
373
0
            *store_info_new = OSSL_STORE_INFO_new_PKEY;
374
375
0
        OPENSSL_free(new_der);
376
0
    }
377
378
0
    return pk;
379
0
}
380
381
static int try_key(struct extracted_param_data_st *data, OSSL_STORE_INFO **v,
382
    OSSL_STORE_CTX *ctx, const OSSL_PROVIDER *provider,
383
    OSSL_LIB_CTX *libctx, const char *propq)
384
0
{
385
0
    store_info_new_fn *store_info_new = NULL;
386
387
0
    if (data->object_type == OSSL_OBJECT_UNKNOWN
388
0
        || data->object_type == OSSL_OBJECT_PKEY) {
389
0
        EVP_PKEY *pk = NULL;
390
391
        /* Prefer key by reference than key by value */
392
0
        if (data->object_type == OSSL_OBJECT_PKEY && data->ref != NULL) {
393
0
            pk = try_key_ref(data, ctx, provider, libctx, propq);
394
395
            /*
396
             * If for some reason we couldn't get a key, it's an error.
397
             * It indicates that while decoders could make a key reference,
398
             * the keymgmt somehow couldn't handle it, or doesn't have a
399
             * OSSL_FUNC_keymgmt_load function.
400
             */
401
0
            if (pk == NULL)
402
0
                return 0;
403
0
        } else if (data->octet_data != NULL) {
404
0
            OSSL_PASSPHRASE_CALLBACK *cb = ossl_pw_passphrase_callback_dec;
405
0
            void *cbarg = &ctx->pwdata;
406
407
0
            pk = try_key_value(data, ctx, cb, cbarg, libctx, propq);
408
409
            /*
410
             * Desperate last maneuver, in case the decoders don't support
411
             * the data we have, then we try on our own to at least get an
412
             * engine provided legacy key.
413
             * This is the same as der2key_decode() does, but in a limited
414
             * way and within the walls of libcrypto.
415
             */
416
0
            if (pk == NULL)
417
0
                pk = try_key_value_legacy(data, &store_info_new, ctx,
418
0
                    cb, cbarg, libctx, propq);
419
0
        }
420
421
0
        if (pk != NULL) {
422
0
            data->object_type = OSSL_OBJECT_PKEY;
423
424
0
            if (store_info_new == NULL) {
425
                /*
426
                 * We determined the object type for OSSL_STORE_INFO, which
427
                 * makes an explicit difference between an EVP_PKEY with just
428
                 * (domain) parameters and an EVP_PKEY with actual key
429
                 * material.
430
                 * The logic is that an EVP_PKEY with actual key material
431
                 * always has the public half.
432
                 */
433
0
                if (evp_keymgmt_util_has(pk, OSSL_KEYMGMT_SELECT_PRIVATE_KEY))
434
0
                    store_info_new = OSSL_STORE_INFO_new_PKEY;
435
0
                else if (evp_keymgmt_util_has(pk,
436
0
                             OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
437
0
                    store_info_new = OSSL_STORE_INFO_new_PUBKEY;
438
0
                else
439
0
                    store_info_new = OSSL_STORE_INFO_new_PARAMS;
440
0
            }
441
0
            *v = store_info_new(pk);
442
0
        }
443
444
0
        if (*v == NULL)
445
0
            EVP_PKEY_free(pk);
446
0
    }
447
448
0
    return 1;
449
0
}
450
451
static int try_cert(struct extracted_param_data_st *data, OSSL_STORE_INFO **v,
452
    OSSL_LIB_CTX *libctx, const char *propq)
453
0
{
454
0
    if (data->object_type == OSSL_OBJECT_UNKNOWN
455
0
        || data->object_type == OSSL_OBJECT_CERT) {
456
        /*
457
         * In most cases, we can try to interpret the serialized
458
         * data as a trusted cert (X509 + X509_AUX) and fall back
459
         * to reading it as a normal cert (just X509), but if
460
         * |data_type| (the PEM name) specifically declares it as a
461
         * trusted cert, then no fallback should be engaged.
462
         * |ignore_trusted| tells if the fallback can be used (1)
463
         * or not (0).
464
         */
465
0
        int ignore_trusted = 1;
466
0
        X509 *cert = X509_new_ex(libctx, propq);
467
468
0
        if (cert == NULL)
469
0
            return 0;
470
471
        /* If we have a data type, it should be a PEM name */
472
0
        if (data->data_type != NULL
473
0
            && (OPENSSL_strcasecmp(data->data_type, PEM_STRING_X509_TRUSTED) == 0))
474
0
            ignore_trusted = 0;
475
476
0
        if (d2i_X509_AUX(&cert, (const unsigned char **)&data->octet_data,
477
0
                data->octet_data_size)
478
0
                == NULL
479
0
            && (!ignore_trusted
480
0
                || d2i_X509(&cert, (const unsigned char **)&data->octet_data,
481
0
                       data->octet_data_size)
482
0
                    == NULL)) {
483
0
            X509_free(cert);
484
0
            cert = NULL;
485
0
        }
486
487
0
        if (cert != NULL) {
488
            /* We determined the object type */
489
0
            data->object_type = OSSL_OBJECT_CERT;
490
0
            *v = OSSL_STORE_INFO_new_CERT(cert);
491
0
            if (*v == NULL)
492
0
                X509_free(cert);
493
0
        }
494
0
    }
495
496
0
    return 1;
497
0
}
498
499
static int try_crl(struct extracted_param_data_st *data, OSSL_STORE_INFO **v,
500
    OSSL_LIB_CTX *libctx, const char *propq)
501
0
{
502
0
    if (data->object_type == OSSL_OBJECT_UNKNOWN
503
0
        || data->object_type == OSSL_OBJECT_CRL) {
504
0
        X509_CRL *crl;
505
506
0
        crl = d2i_X509_CRL(NULL, (const unsigned char **)&data->octet_data,
507
0
            data->octet_data_size);
508
509
0
        if (crl != NULL)
510
            /* We determined the object type */
511
0
            data->object_type = OSSL_OBJECT_CRL;
512
513
0
        if (crl != NULL && !ossl_x509_crl_set0_libctx(crl, libctx, propq)) {
514
0
            X509_CRL_free(crl);
515
0
            crl = NULL;
516
0
        }
517
518
0
        if (crl != NULL)
519
0
            *v = OSSL_STORE_INFO_new_CRL(crl);
520
0
        if (*v == NULL)
521
0
            X509_CRL_free(crl);
522
0
    }
523
524
0
    return 1;
525
0
}
526
527
static int try_pkcs12(struct extracted_param_data_st *data, OSSL_STORE_INFO **v,
528
    OSSL_STORE_CTX *ctx,
529
    OSSL_LIB_CTX *libctx, const char *propq)
530
0
{
531
0
    int ok = 1;
532
533
    /* There is no specific object type for PKCS12 */
534
0
    if (data->object_type == OSSL_OBJECT_UNKNOWN) {
535
        /* Initial parsing */
536
0
        PKCS12 *p12;
537
538
0
        p12 = d2i_PKCS12(NULL, (const unsigned char **)&data->octet_data,
539
0
            data->octet_data_size);
540
541
0
        if (p12 != NULL) {
542
0
            char *pass = NULL;
543
0
            char tpass[PEM_BUFSIZE + 1];
544
0
            size_t tpass_len;
545
0
            EVP_PKEY *pkey = NULL;
546
0
            X509 *cert = NULL;
547
0
            STACK_OF(X509) *chain = NULL;
548
549
0
            data->object_type = OSSL_OBJECT_PKCS12;
550
551
0
            ok = 0; /* Assume decryption or parse error */
552
553
0
            if (!PKCS12_mac_present(p12)
554
0
                || PKCS12_verify_mac(p12, NULL, 0)) {
555
0
                pass = NULL;
556
0
            } else if (PKCS12_verify_mac(p12, "", 0)) {
557
0
                pass = "";
558
0
            } else {
559
0
                static char prompt_info[] = "PKCS12 import pass phrase";
560
0
                OSSL_PARAM pw_params[] = {
561
0
                    OSSL_PARAM_utf8_string(OSSL_PASSPHRASE_PARAM_INFO,
562
0
                        prompt_info,
563
0
                        sizeof(prompt_info) - 1),
564
0
                    OSSL_PARAM_END
565
0
                };
566
567
0
                if (!ossl_pw_get_passphrase(tpass, sizeof(tpass) - 1,
568
0
                        &tpass_len,
569
0
                        pw_params, 0, &ctx->pwdata)) {
570
0
                    ERR_raise(ERR_LIB_OSSL_STORE,
571
0
                        OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR);
572
0
                    goto p12_end;
573
0
                }
574
0
                pass = tpass;
575
                /*
576
                 * ossl_pw_get_passphrase() does not NUL terminate but
577
                 * we must do it for PKCS12_parse()
578
                 */
579
0
                pass[tpass_len] = '\0';
580
0
                if (!PKCS12_verify_mac(p12, pass, tpass_len)) {
581
0
                    ERR_raise_data(ERR_LIB_OSSL_STORE,
582
0
                        OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC,
583
0
                        tpass_len == 0 ? "empty password" : "maybe wrong password");
584
0
                    goto p12_end;
585
0
                }
586
0
            }
587
588
0
            if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
589
0
                STACK_OF(OSSL_STORE_INFO) *infos = NULL;
590
0
                OSSL_STORE_INFO *osi_pkey = NULL;
591
0
                OSSL_STORE_INFO *osi_cert = NULL;
592
0
                OSSL_STORE_INFO *osi_ca = NULL;
593
594
0
                ok = 1; /* Parsing went through correctly! */
595
596
0
                if ((infos = sk_OSSL_STORE_INFO_new_null()) != NULL) {
597
0
                    if (pkey != NULL) {
598
0
                        if ((osi_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
599
                            /* clearing pkey here avoids case distinctions */
600
0
                            && (pkey = NULL) == NULL
601
0
                            && sk_OSSL_STORE_INFO_push(infos, osi_pkey) != 0)
602
0
                            osi_pkey = NULL;
603
0
                        else
604
0
                            ok = 0;
605
0
                    }
606
0
                    if (ok && cert != NULL) {
607
0
                        if ((osi_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
608
                            /* clearing cert here avoids case distinctions */
609
0
                            && (cert = NULL) == NULL
610
0
                            && sk_OSSL_STORE_INFO_push(infos, osi_cert) != 0)
611
0
                            osi_cert = NULL;
612
0
                        else
613
0
                            ok = 0;
614
0
                    }
615
0
                    while (ok && sk_X509_num(chain) > 0) {
616
0
                        X509 *ca = sk_X509_value(chain, 0);
617
618
0
                        if ((osi_ca = OSSL_STORE_INFO_new_CERT(ca)) != NULL
619
0
                            && sk_X509_shift(chain) != NULL
620
0
                            && sk_OSSL_STORE_INFO_push(infos, osi_ca) != 0)
621
0
                            osi_ca = NULL;
622
0
                        else
623
0
                            ok = 0;
624
0
                    }
625
0
                }
626
0
                EVP_PKEY_free(pkey);
627
0
                X509_free(cert);
628
0
                OSSL_STACK_OF_X509_free(chain);
629
0
                OSSL_STORE_INFO_free(osi_pkey);
630
0
                OSSL_STORE_INFO_free(osi_cert);
631
0
                OSSL_STORE_INFO_free(osi_ca);
632
0
                if (!ok) {
633
0
                    sk_OSSL_STORE_INFO_pop_free(infos, OSSL_STORE_INFO_free);
634
0
                    infos = NULL;
635
0
                }
636
0
                ctx->cached_info = infos;
637
0
            }
638
0
        p12_end:
639
0
            OPENSSL_cleanse(tpass, sizeof(tpass));
640
0
            PKCS12_free(p12);
641
0
        }
642
0
        *v = sk_OSSL_STORE_INFO_shift(ctx->cached_info);
643
0
    }
644
645
0
    return ok;
646
0
}