store_result.c 23 KB

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