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