Add ossl_ ecx symbols
[openssl.git] / crypto / store / store_result.c
1 /*
2  * Copyright 2020-2021 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 "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 # 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 #define SET_ERR_MARK() ERR_set_mark()
86 #define CLEAR_ERR_MARK()                                                \
87     do {                                                                \
88         int err = ERR_peek_last_error();                                \
89                                                                         \
90         if (ERR_GET_LIB(err) == ERR_LIB_ASN1                            \
91             && (ERR_GET_REASON(err) == ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE   \
92                 || ERR_GET_REASON(err) == ASN1_R_NO_MATCHING_CHOICE_TYPE \
93                 || ERR_GET_REASON(err) == ERR_R_NESTED_ASN1_ERROR))     \
94             ERR_pop_to_mark();                                          \
95         else                                                            \
96             ERR_clear_last_mark();                                      \
97     } while(0)
98 #define RESET_ERR_MARK()                                                \
99     do {                                                                \
100         CLEAR_ERR_MARK();                                               \
101         SET_ERR_MARK();                                                 \
102     } while(0)
103
104 int ossl_store_handle_load_result(const OSSL_PARAM params[], void *arg)
105 {
106     struct ossl_load_result_data_st *cbdata = arg;
107     OSSL_STORE_INFO **v = &cbdata->v;
108     OSSL_STORE_CTX *ctx = cbdata->ctx;
109     const OSSL_PROVIDER *provider =
110         OSSL_STORE_LOADER_provider(ctx->fetched_loader);
111     OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
112     const char *propq = ctx->properties;
113     const OSSL_PARAM *p;
114     struct extracted_param_data_st helper_data;
115
116     memset(&helper_data, 0, sizeof(helper_data));
117     helper_data.object_type = OSSL_OBJECT_UNKNOWN;
118
119     if ((p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_TYPE)) != NULL
120         && !OSSL_PARAM_get_int(p, &helper_data.object_type))
121         return 0;
122     p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
123     if (p != NULL
124         && !OSSL_PARAM_get_utf8_string_ptr(p, &helper_data.data_type))
125         return 0;
126     p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA);
127     if (p != NULL
128         && !OSSL_PARAM_get_octet_string_ptr(p, &helper_data.octet_data,
129                                             &helper_data.octet_data_size)
130         && !OSSL_PARAM_get_utf8_string_ptr(p, &helper_data.utf8_data))
131         return 0;
132     p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE);
133     if (p != NULL
134         && !OSSL_PARAM_get_utf8_string_ptr(p, &helper_data.data_structure))
135         return 0;
136     p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_REFERENCE);
137     if (p != NULL && !OSSL_PARAM_get_octet_string_ptr(p, &helper_data.ref,
138                                                       &helper_data.ref_size))
139         return 0;
140     p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DESC);
141     if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &helper_data.desc))
142         return 0;
143
144     /*
145      * The helper functions return 0 on actual errors, otherwise 1, even if
146      * they didn't fill out |*v|.
147      */
148     SET_ERR_MARK();
149     if (!try_name(&helper_data, v))
150         goto err;
151     RESET_ERR_MARK();
152     if (!try_key(&helper_data, v, ctx, provider, libctx, propq))
153         goto err;
154     RESET_ERR_MARK();
155     if (!try_cert(&helper_data, v, libctx, propq))
156         goto err;
157     RESET_ERR_MARK();
158     if (!try_crl(&helper_data, v, libctx, propq))
159         goto err;
160     RESET_ERR_MARK();
161     if (!try_pkcs12(&helper_data, v, ctx, libctx, propq))
162         goto err;
163     CLEAR_ERR_MARK();
164
165     return (*v != NULL);
166  err:
167     return 0;
168 }
169
170 static int try_name(struct extracted_param_data_st *data, OSSL_STORE_INFO **v)
171 {
172     if (data->object_type == OSSL_OBJECT_NAME) {
173         char *newname = NULL, *newdesc = NULL;
174
175         if (data->utf8_data == NULL)
176             return 0;
177         if ((newname = OPENSSL_strdup(data->utf8_data)) == NULL
178             || (data->desc != NULL
179                 && (newdesc = OPENSSL_strdup(data->desc)) == NULL)
180             || (*v = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
181             OPENSSL_free(newname);
182             OPENSSL_free(newdesc);
183             return 0;
184         }
185         OSSL_STORE_INFO_set0_NAME_description(*v, newdesc);
186     }
187     return 1;
188 }
189
190 /*
191  * For the rest of the object types, the provider code may not know what
192  * type of data it gave us, so we may need to figure that out on our own.
193  * Therefore, we do check for OSSL_OBJECT_UNKNOWN everywhere below, and
194  * only return 0 on error if the object type is known.
195  */
196
197 static EVP_PKEY *try_key_ref(struct extracted_param_data_st *data,
198                              OSSL_STORE_CTX *ctx,
199                              const OSSL_PROVIDER *provider,
200                              OSSL_LIB_CTX *libctx, const char *propq)
201 {
202     EVP_PKEY *pk = NULL;
203     EVP_KEYMGMT *keymgmt = NULL;
204     void *keydata = NULL;
205
206     /* If we have an object reference, we must have a data type */
207     if (data->data_type == NULL)
208         return 0;
209
210     keymgmt = EVP_KEYMGMT_fetch(libctx, data->data_type, propq);
211     if (keymgmt != NULL) {
212         /*
213          * There are two possible cases
214          *
215          * 1.  The keymgmt is from the same provider as the loader,
216          *     so we can use evp_keymgmt_load()
217          * 2.  The keymgmt is from another provider, then we must
218          *     do the export/import dance.
219          */
220         if (EVP_KEYMGMT_provider(keymgmt) == provider) {
221             keydata = evp_keymgmt_load(keymgmt, data->ref, data->ref_size);
222         } else {
223             struct evp_keymgmt_util_try_import_data_st import_data;
224             OSSL_FUNC_store_export_object_fn *export_object =
225                 ctx->fetched_loader->p_export_object;
226
227             import_data.keymgmt = keymgmt;
228             import_data.keydata = NULL;
229             import_data.selection = OSSL_KEYMGMT_SELECT_ALL;
230
231             if (export_object != NULL) {
232                 /*
233                  * No need to check for errors here, the value of
234                  * |import_data.keydata| is as much an indicator.
235                  */
236                 (void)export_object(ctx->loader_ctx,
237                                     data->ref, data->ref_size,
238                                     &evp_keymgmt_util_try_import,
239                                     &import_data);
240             }
241
242             keydata = import_data.keydata;
243         }
244     }
245     if (keydata != NULL)
246         pk = evp_keymgmt_util_make_pkey(keymgmt, keydata);
247     EVP_KEYMGMT_free(keymgmt);
248
249     return pk;
250 }
251
252 static EVP_PKEY *try_key_value(struct extracted_param_data_st *data,
253                                OSSL_STORE_CTX *ctx,
254                                OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg,
255                                OSSL_LIB_CTX *libctx, const char *propq)
256 {
257     EVP_PKEY *pk = NULL;
258     OSSL_DECODER_CTX *decoderctx = NULL;
259     const unsigned char *pdata = data->octet_data;
260     size_t pdatalen = data->octet_data_size;
261     int selection = 0;
262
263     switch (ctx->expected_type) {
264     case 0:
265         break;
266     case OSSL_STORE_INFO_PARAMS:
267         selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
268         break;
269     case OSSL_STORE_INFO_PUBKEY:
270         selection =
271             OSSL_KEYMGMT_SELECT_PUBLIC_KEY
272             | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
273         break;
274     case OSSL_STORE_INFO_PKEY:
275         selection = OSSL_KEYMGMT_SELECT_ALL;
276         break;
277     default:
278         return NULL;
279     }
280
281     decoderctx =
282         OSSL_DECODER_CTX_new_for_pkey(&pk, "DER", data->data_structure,
283                                       data->data_type, selection, libctx,
284                                       propq);
285     (void)OSSL_DECODER_CTX_set_passphrase_cb(decoderctx, cb, cbarg);
286
287     /* No error if this couldn't be decoded */
288     (void)OSSL_DECODER_from_data(decoderctx, &pdata, &pdatalen);
289
290     OSSL_DECODER_CTX_free(decoderctx);
291
292     return pk;
293 }
294
295 typedef OSSL_STORE_INFO *store_info_new_fn(EVP_PKEY *);
296
297 static EVP_PKEY *try_key_value_legacy(struct extracted_param_data_st *data,
298                                       store_info_new_fn **store_info_new,
299                                       OSSL_STORE_CTX *ctx,
300                                       OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg,
301                                       OSSL_LIB_CTX *libctx, const char *propq)
302 {
303     EVP_PKEY *pk = NULL;
304     const unsigned char *der = data->octet_data, *derp;
305     long der_len = (long)data->octet_data_size;
306
307     SET_ERR_MARK();
308     /* Try PUBKEY first, that's a real easy target */
309     if (ctx->expected_type == 0
310         || ctx->expected_type == OSSL_STORE_INFO_PUBKEY) {
311         derp = der;
312         pk = d2i_PUBKEY_ex(NULL, &derp, der_len, libctx, propq);
313         if (pk != NULL)
314             *store_info_new = OSSL_STORE_INFO_new_PUBKEY;
315
316         RESET_ERR_MARK();
317     }
318
319     /* Try private keys next */
320     if (pk == NULL
321         && (ctx->expected_type == 0
322             || ctx->expected_type == OSSL_STORE_INFO_PKEY)) {
323         unsigned char *new_der = NULL;
324         X509_SIG *p8 = NULL;
325         PKCS8_PRIV_KEY_INFO *p8info = NULL;
326
327         /* See if it's an encrypted PKCS#8 and decrypt it */
328         derp = der;
329         if ((p8 = d2i_X509_SIG(NULL, &derp, der_len)) != NULL) {
330             char pbuf[PEM_BUFSIZE];
331             size_t plen = 0;
332
333             if (!cb(pbuf, sizeof(pbuf), &plen, NULL, cbarg)) {
334                 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_BAD_PASSWORD_READ);
335             } else {
336                 const X509_ALGOR *alg = NULL;
337                 const ASN1_OCTET_STRING *oct = NULL;
338                 int len = 0;
339
340                 X509_SIG_get0(p8, &alg, &oct);
341
342                 /*
343                  * No need to check the returned value, |new_der|
344                  * will be NULL on error anyway.
345                  */
346                 PKCS12_pbe_crypt(alg, pbuf, plen,
347                                  oct->data, oct->length,
348                                  &new_der, &len, 0);
349                 der_len = len;
350                 der = new_der;
351             }
352             X509_SIG_free(p8);
353         }
354         RESET_ERR_MARK();
355
356         /*
357          * If the encrypted PKCS#8 couldn't be decrypted,
358          * |der| is NULL
359          */
360         if (der != NULL) {
361             /* Try to unpack an unencrypted PKCS#8, that's easy */
362             derp = der;
363             p8info = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp, der_len);
364             RESET_ERR_MARK();
365             if (p8info != NULL) {
366                 pk = EVP_PKCS82PKEY_ex(p8info, libctx, propq);
367                 PKCS8_PRIV_KEY_INFO_free(p8info);
368             }
369         }
370
371         if (pk != NULL)
372             *store_info_new = OSSL_STORE_INFO_new_PKEY;
373
374         OPENSSL_free(new_der);
375     }
376     CLEAR_ERR_MARK();
377
378     return pk;
379 }
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 {
385     store_info_new_fn *store_info_new = NULL;
386
387     if (data->object_type == OSSL_OBJECT_UNKNOWN
388         || data->object_type == OSSL_OBJECT_PKEY) {
389         EVP_PKEY *pk = NULL;
390
391         /* Prefer key by reference than key by value */
392         if (data->object_type == OSSL_OBJECT_PKEY && data->ref != NULL) {
393             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             if (pk == NULL)
402                 return 0;
403         } else if (data->octet_data != NULL) {
404             OSSL_PASSPHRASE_CALLBACK *cb = ossl_pw_passphrase_callback_dec;
405             void *cbarg = &ctx->pwdata;
406
407             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 a
412              * 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              * TODO Remove this when #legacy keys are gone
417              */
418             if (pk == NULL)
419                 pk = try_key_value_legacy(data, &store_info_new, ctx,
420                                           cb, cbarg, libctx, propq);
421         }
422
423         if (pk != NULL) {
424             data->object_type = OSSL_OBJECT_PKEY;
425
426             if (store_info_new == NULL) {
427                 /*
428                  * We determined the object type for OSSL_STORE_INFO, which
429                  * makes an explicit difference between an EVP_PKEY with just
430                  * (domain) parameters and an EVP_PKEY with actual key
431                  * material.
432                  * The logic is that an EVP_PKEY with actual key material
433                  * always has the public half.
434                  */
435                 if (evp_keymgmt_util_has(pk, OSSL_KEYMGMT_SELECT_PRIVATE_KEY))
436                     store_info_new = OSSL_STORE_INFO_new_PKEY;
437                 else if (evp_keymgmt_util_has(pk,
438                                               OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
439                     store_info_new = OSSL_STORE_INFO_new_PUBKEY;
440                 else
441                     store_info_new = OSSL_STORE_INFO_new_PARAMS;
442             }
443             *v = store_info_new(pk);
444         }
445
446         if (*v == NULL)
447             EVP_PKEY_free(pk);
448     }
449
450     return 1;
451 }
452
453 static int try_cert(struct extracted_param_data_st *data, OSSL_STORE_INFO **v,
454                     OSSL_LIB_CTX *libctx, const char *propq)
455 {
456     if (data->object_type == OSSL_OBJECT_UNKNOWN
457         || data->object_type == OSSL_OBJECT_CERT) {
458         X509 *cert;
459
460         /*
461          * In most cases, we can try to interpret the serialized
462          * data as a trusted cert (X509 + X509_AUX) and fall back
463          * to reading it as a normal cert (just X509), but if
464          * |data_type| (the PEM name) specifically declares it as a
465          * trusted cert, then no fallback should be engaged.
466          * |ignore_trusted| tells if the fallback can be used (1)
467          * or not (0).
468          */
469         int ignore_trusted = 1;
470
471         /* If we have a data type, it should be a PEM name */
472         if (data->data_type != NULL
473             && (strcasecmp(data->data_type, PEM_STRING_X509_TRUSTED) == 0))
474             ignore_trusted = 0;
475
476         cert = d2i_X509_AUX(NULL, (const unsigned char **)&data->octet_data,
477                             data->octet_data_size);
478         if (cert == NULL && ignore_trusted)
479             cert = d2i_X509(NULL, (const unsigned char **)&data->octet_data,
480                             data->octet_data_size);
481
482         if (cert != NULL)
483             /* We determined the object type */
484             data->object_type = OSSL_OBJECT_CERT;
485
486         if (cert != NULL && !x509_set0_libctx(cert, libctx, propq)) {
487             X509_free(cert);
488             cert = NULL;
489         }
490
491         if (cert != NULL)
492             *v = OSSL_STORE_INFO_new_CERT(cert);
493         if (*v == NULL)
494             X509_free(cert);
495     }
496
497     return 1;
498 }
499
500 static int try_crl(struct extracted_param_data_st *data, OSSL_STORE_INFO **v,
501                    OSSL_LIB_CTX *libctx, const char *propq)
502 {
503     if (data->object_type == OSSL_OBJECT_UNKNOWN
504         || data->object_type == OSSL_OBJECT_CRL) {
505         X509_CRL *crl;
506
507         crl = d2i_X509_CRL(NULL, (const unsigned char **)&data->octet_data,
508                            data->octet_data_size);
509         if (crl != NULL)
510             /* We determined the object type */
511             data->object_type = OSSL_OBJECT_CRL;
512
513         if (crl != NULL && !x509_crl_set0_libctx(crl, libctx, propq)) {
514             X509_CRL_free(crl);
515             crl = NULL;
516         }
517
518         if (crl != NULL)
519             *v = OSSL_STORE_INFO_new_CRL(crl);
520         if (*v == NULL)
521             X509_CRL_free(crl);
522     }
523
524     return 1;
525 }
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 {
531     /* There is no specific object type for PKCS12 */
532     if (data->object_type == OSSL_OBJECT_UNKNOWN) {
533         /* Initial parsing */
534         PKCS12 *p12;
535
536         if ((p12 = d2i_PKCS12(NULL, (const unsigned char **)&data->octet_data,
537                               data->octet_data_size)) != NULL) {
538             char *pass = NULL;
539             char tpass[PEM_BUFSIZE];
540             size_t tpass_len;
541             EVP_PKEY *pkey = NULL;
542             X509 *cert = NULL;
543             STACK_OF(X509) *chain = NULL;
544
545             data->object_type = OSSL_OBJECT_PKCS12;
546
547             if (PKCS12_verify_mac(p12, "", 0)
548                 || PKCS12_verify_mac(p12, NULL, 0)) {
549                 pass = "";
550             } else {
551                 static char prompt_info[] = "PKCS12 import pass phrase";
552                 OSSL_PARAM pw_params[] = {
553                     OSSL_PARAM_utf8_string(OSSL_PASSPHRASE_PARAM_INFO,
554                                            prompt_info,
555                                            sizeof(prompt_info) - 1),
556                     OSSL_PARAM_END
557                 };
558
559                 if (!ossl_pw_get_passphrase(tpass, sizeof(tpass), &tpass_len,
560                                             pw_params, 0, &ctx->pwdata)) {
561                     ERR_raise(ERR_LIB_OSSL_STORE,
562                               OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR);
563                     goto p12_end;
564                 }
565                 pass = tpass;
566                 if (!PKCS12_verify_mac(p12, pass, strlen(pass))) {
567                     ERR_raise_data(ERR_LIB_OSSL_STORE,
568                                    OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC,
569                                    strlen(pass) == 0 ? "empty password" :
570                                    "maybe wrong password");
571                     goto p12_end;
572                 }
573             }
574
575             if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
576                 STACK_OF(OSSL_STORE_INFO) *infos = NULL;
577                 OSSL_STORE_INFO *osi_pkey = NULL;
578                 OSSL_STORE_INFO *osi_cert = NULL;
579                 OSSL_STORE_INFO *osi_ca = NULL;
580                 int ok = 1;
581
582                 if ((infos = sk_OSSL_STORE_INFO_new_null()) != NULL) {
583                     if (pkey != NULL) {
584                         if ((osi_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
585                             /* clearing pkey here avoids case distinctions */
586                             && (pkey = NULL) == NULL
587                             && sk_OSSL_STORE_INFO_push(infos, osi_pkey) != 0)
588                             osi_pkey = NULL;
589                         else
590                             ok = 0;
591                     }
592                     if (ok && cert != NULL) {
593                         if ((osi_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
594                             /* clearing cert here avoids case distinctions */
595                             && (cert = NULL) == NULL
596                             && sk_OSSL_STORE_INFO_push(infos, osi_cert) != 0)
597                             osi_cert = NULL;
598                         else
599                             ok = 0;
600                     }
601                     while (ok && sk_X509_num(chain) > 0) {
602                         X509 *ca = sk_X509_value(chain, 0);
603
604                         if ((osi_ca = OSSL_STORE_INFO_new_CERT(ca)) != NULL
605                             && sk_X509_shift(chain) != NULL
606                             && sk_OSSL_STORE_INFO_push(infos, osi_ca) != 0)
607                             osi_ca = NULL;
608                         else
609                             ok = 0;
610                     }
611                 }
612                 EVP_PKEY_free(pkey);
613                 X509_free(cert);
614                 sk_X509_pop_free(chain, X509_free);
615                 OSSL_STORE_INFO_free(osi_pkey);
616                 OSSL_STORE_INFO_free(osi_cert);
617                 OSSL_STORE_INFO_free(osi_ca);
618                 if (!ok) {
619                     sk_OSSL_STORE_INFO_pop_free(infos, OSSL_STORE_INFO_free);
620                     infos = NULL;
621                 }
622                 ctx->cached_info = infos;
623             }
624         }
625      p12_end:
626         PKCS12_free(p12);
627         *v = sk_OSSL_STORE_INFO_shift(ctx->cached_info);
628     }
629
630     return 1;
631 }