encoder_pkey.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright 2019-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. #include <openssl/err.h>
  10. #include <openssl/ui.h>
  11. #include <openssl/params.h>
  12. #include <openssl/encoder.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/provider.h>
  15. #include <openssl/safestack.h>
  16. #include <openssl/trace.h>
  17. #include "internal/provider.h"
  18. #include "internal/property.h"
  19. #include "internal/namemap.h"
  20. #include "crypto/evp.h"
  21. #include "encoder_local.h"
  22. DEFINE_STACK_OF(OSSL_ENCODER)
  23. int OSSL_ENCODER_CTX_set_cipher(OSSL_ENCODER_CTX *ctx,
  24. const char *cipher_name,
  25. const char *propquery)
  26. {
  27. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
  28. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ENCODER_PARAM_CIPHER,
  29. (void *)cipher_name, 0);
  30. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ENCODER_PARAM_PROPERTIES,
  31. (void *)propquery, 0);
  32. return OSSL_ENCODER_CTX_set_params(ctx, params);
  33. }
  34. int OSSL_ENCODER_CTX_set_passphrase(OSSL_ENCODER_CTX *ctx,
  35. const unsigned char *kstr,
  36. size_t klen)
  37. {
  38. return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen);
  39. }
  40. int OSSL_ENCODER_CTX_set_passphrase_ui(OSSL_ENCODER_CTX *ctx,
  41. const UI_METHOD *ui_method,
  42. void *ui_data)
  43. {
  44. return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data);
  45. }
  46. int OSSL_ENCODER_CTX_set_pem_password_cb(OSSL_ENCODER_CTX *ctx,
  47. pem_password_cb *cb, void *cbarg)
  48. {
  49. return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg);
  50. }
  51. int OSSL_ENCODER_CTX_set_passphrase_cb(OSSL_ENCODER_CTX *ctx,
  52. OSSL_PASSPHRASE_CALLBACK *cb,
  53. void *cbarg)
  54. {
  55. return ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, cb, cbarg);
  56. }
  57. /*
  58. * Support for OSSL_ENCODER_CTX_new_for_type:
  59. * finding a suitable encoder
  60. */
  61. struct collected_encoder_st {
  62. STACK_OF(OPENSSL_CSTRING) *names;
  63. int *id_names;
  64. const char *output_structure;
  65. const char *output_type;
  66. const OSSL_PROVIDER *keymgmt_prov;
  67. OSSL_ENCODER_CTX *ctx;
  68. unsigned int flag_find_same_provider : 1;
  69. int error_occurred;
  70. };
  71. static void collect_encoder(OSSL_ENCODER *encoder, void *arg)
  72. {
  73. struct collected_encoder_st *data = arg;
  74. const OSSL_PROVIDER *prov;
  75. if (data->error_occurred)
  76. return;
  77. data->error_occurred = 1; /* Assume the worst */
  78. prov = OSSL_ENCODER_get0_provider(encoder);
  79. /*
  80. * collect_encoder() is called in two passes, one where the encoders
  81. * from the same provider as the keymgmt are looked up, and one where
  82. * the other encoders are looked up. |data->flag_find_same_provider|
  83. * tells us which pass we're in.
  84. */
  85. if ((data->keymgmt_prov == prov) == data->flag_find_same_provider) {
  86. void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
  87. int i, end_i = sk_OPENSSL_CSTRING_num(data->names);
  88. int match;
  89. for (i = 0; i < end_i; i++) {
  90. if (data->flag_find_same_provider)
  91. match = (data->id_names[i] == encoder->base.id);
  92. else
  93. match = OSSL_ENCODER_is_a(encoder,
  94. sk_OPENSSL_CSTRING_value(data->names, i));
  95. if (!match
  96. || (encoder->does_selection != NULL
  97. && !encoder->does_selection(provctx, data->ctx->selection))
  98. || (data->keymgmt_prov != prov
  99. && encoder->import_object == NULL))
  100. continue;
  101. /* Only add each encoder implementation once */
  102. if (OSSL_ENCODER_CTX_add_encoder(data->ctx, encoder))
  103. break;
  104. }
  105. }
  106. data->error_occurred = 0; /* All is good now */
  107. }
  108. struct collected_names_st {
  109. STACK_OF(OPENSSL_CSTRING) *names;
  110. unsigned int error_occurred : 1;
  111. };
  112. static void collect_name(const char *name, void *arg)
  113. {
  114. struct collected_names_st *data = arg;
  115. if (data->error_occurred)
  116. return;
  117. data->error_occurred = 1; /* Assume the worst */
  118. if (sk_OPENSSL_CSTRING_push(data->names, name) <= 0)
  119. return;
  120. data->error_occurred = 0; /* All is good now */
  121. }
  122. /*
  123. * Support for OSSL_ENCODER_to_bio:
  124. * writing callback for the OSSL_PARAM (the implementation doesn't have
  125. * intimate knowledge of the provider side object)
  126. */
  127. struct construct_data_st {
  128. const EVP_PKEY *pk;
  129. int selection;
  130. OSSL_ENCODER_INSTANCE *encoder_inst;
  131. const void *obj;
  132. void *constructed_obj;
  133. };
  134. static int encoder_import_cb(const OSSL_PARAM params[], void *arg)
  135. {
  136. struct construct_data_st *construct_data = arg;
  137. OSSL_ENCODER_INSTANCE *encoder_inst = construct_data->encoder_inst;
  138. OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst);
  139. void *encoderctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst);
  140. construct_data->constructed_obj = encoder->import_object(encoderctx, construct_data->selection, params);
  141. return (construct_data->constructed_obj != NULL);
  142. }
  143. static const void *
  144. encoder_construct_pkey(OSSL_ENCODER_INSTANCE *encoder_inst, void *arg)
  145. {
  146. struct construct_data_st *data = arg;
  147. if (data->obj == NULL) {
  148. OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst);
  149. const EVP_PKEY *pk = data->pk;
  150. const OSSL_PROVIDER *k_prov = EVP_KEYMGMT_get0_provider(pk->keymgmt);
  151. const OSSL_PROVIDER *e_prov = OSSL_ENCODER_get0_provider(encoder);
  152. if (k_prov != e_prov) {
  153. int selection = data->selection;
  154. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  155. selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  156. data->encoder_inst = encoder_inst;
  157. if (!evp_keymgmt_export(pk->keymgmt, pk->keydata, selection,
  158. &encoder_import_cb, data))
  159. return NULL;
  160. data->obj = data->constructed_obj;
  161. } else {
  162. data->obj = pk->keydata;
  163. }
  164. }
  165. return data->obj;
  166. }
  167. static void encoder_destruct_pkey(void *arg)
  168. {
  169. struct construct_data_st *data = arg;
  170. int match = (data->obj == data->constructed_obj);
  171. if (data->encoder_inst != NULL) {
  172. OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(data->encoder_inst);
  173. encoder->free_object(data->constructed_obj);
  174. }
  175. data->constructed_obj = NULL;
  176. if (match)
  177. data->obj = NULL;
  178. }
  179. /*
  180. * OSSL_ENCODER_CTX_new_for_pkey() returns a ctx with no encoder if
  181. * it couldn't find a suitable encoder. This allows a caller to detect if
  182. * a suitable encoder was found, with OSSL_ENCODER_CTX_get_num_encoder(),
  183. * and to use fallback methods if the result is NULL.
  184. */
  185. static int ossl_encoder_ctx_setup_for_pkey(OSSL_ENCODER_CTX *ctx,
  186. const EVP_PKEY *pkey,
  187. int selection,
  188. const char *propquery)
  189. {
  190. struct construct_data_st *data = NULL;
  191. const OSSL_PROVIDER *prov = NULL;
  192. OSSL_LIB_CTX *libctx = NULL;
  193. int ok = 0, i, end;
  194. OSSL_NAMEMAP *namemap;
  195. if (!ossl_assert(ctx != NULL) || !ossl_assert(pkey != NULL)) {
  196. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  197. return 0;
  198. }
  199. if (evp_pkey_is_provided(pkey)) {
  200. prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
  201. libctx = ossl_provider_libctx(prov);
  202. }
  203. if (pkey->keymgmt != NULL) {
  204. struct collected_encoder_st encoder_data;
  205. struct collected_names_st keymgmt_data;
  206. if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL)
  207. goto err;
  208. /*
  209. * Select the first encoder implementations in two steps.
  210. * First, collect the keymgmt names, then the encoders that match.
  211. */
  212. keymgmt_data.names = sk_OPENSSL_CSTRING_new_null();
  213. if (keymgmt_data.names == NULL) {
  214. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB);
  215. goto err;
  216. }
  217. keymgmt_data.error_occurred = 0;
  218. EVP_KEYMGMT_names_do_all(pkey->keymgmt, collect_name, &keymgmt_data);
  219. if (keymgmt_data.error_occurred) {
  220. sk_OPENSSL_CSTRING_free(keymgmt_data.names);
  221. goto err;
  222. }
  223. encoder_data.names = keymgmt_data.names;
  224. encoder_data.output_type = ctx->output_type;
  225. encoder_data.output_structure = ctx->output_structure;
  226. encoder_data.error_occurred = 0;
  227. encoder_data.keymgmt_prov = prov;
  228. encoder_data.ctx = ctx;
  229. encoder_data.id_names = NULL;
  230. /*
  231. * collect_encoder() is called many times, and for every call it converts all encoder_data.names
  232. * into namemap ids if it calls OSSL_ENCODER_is_a(). We cache the ids here instead,
  233. * and can use them for encoders with the same provider as the keymgmt.
  234. */
  235. namemap = ossl_namemap_stored(libctx);
  236. end = sk_OPENSSL_CSTRING_num(encoder_data.names);
  237. if (end > 0) {
  238. encoder_data.id_names = OPENSSL_malloc(end * sizeof(int));
  239. if (encoder_data.id_names == NULL) {
  240. sk_OPENSSL_CSTRING_free(keymgmt_data.names);
  241. goto err;
  242. }
  243. for (i = 0; i < end; ++i) {
  244. const char *name = sk_OPENSSL_CSTRING_value(keymgmt_data.names, i);
  245. encoder_data.id_names[i] = ossl_namemap_name2num(namemap, name);
  246. }
  247. }
  248. /*
  249. * Place the encoders with the a different provider as the keymgmt
  250. * last (the chain is processed in reverse order)
  251. */
  252. encoder_data.flag_find_same_provider = 0;
  253. OSSL_ENCODER_do_all_provided(libctx, collect_encoder, &encoder_data);
  254. /*
  255. * Place the encoders with the same provider as the keymgmt first
  256. * (the chain is processed in reverse order)
  257. */
  258. encoder_data.flag_find_same_provider = 1;
  259. OSSL_ENCODER_do_all_provided(libctx, collect_encoder, &encoder_data);
  260. OPENSSL_free(encoder_data.id_names);
  261. sk_OPENSSL_CSTRING_free(keymgmt_data.names);
  262. if (encoder_data.error_occurred) {
  263. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB);
  264. goto err;
  265. }
  266. }
  267. if (data != NULL && OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0) {
  268. if (!OSSL_ENCODER_CTX_set_construct(ctx, encoder_construct_pkey)
  269. || !OSSL_ENCODER_CTX_set_construct_data(ctx, data)
  270. || !OSSL_ENCODER_CTX_set_cleanup(ctx, encoder_destruct_pkey))
  271. goto err;
  272. data->pk = pkey;
  273. data->selection = selection;
  274. data = NULL; /* Avoid it being freed */
  275. }
  276. ok = 1;
  277. err:
  278. if (data != NULL) {
  279. OSSL_ENCODER_CTX_set_construct_data(ctx, NULL);
  280. OPENSSL_free(data);
  281. }
  282. return ok;
  283. }
  284. OSSL_ENCODER_CTX *OSSL_ENCODER_CTX_new_for_pkey(const EVP_PKEY *pkey,
  285. int selection,
  286. const char *output_type,
  287. const char *output_struct,
  288. const char *propquery)
  289. {
  290. OSSL_ENCODER_CTX *ctx = NULL;
  291. OSSL_LIB_CTX *libctx = NULL;
  292. if (pkey == NULL) {
  293. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  294. return NULL;
  295. }
  296. if (!evp_pkey_is_assigned(pkey)) {
  297. ERR_raise_data(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT,
  298. "The passed EVP_PKEY must be assigned a key");
  299. return NULL;
  300. }
  301. if ((ctx = OSSL_ENCODER_CTX_new()) == NULL) {
  302. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_OSSL_ENCODER_LIB);
  303. return NULL;
  304. }
  305. if (evp_pkey_is_provided(pkey)) {
  306. const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
  307. libctx = ossl_provider_libctx(prov);
  308. }
  309. OSSL_TRACE_BEGIN(ENCODER)
  310. {
  311. BIO_printf(trc_out,
  312. "(ctx %p) Looking for %s encoders with selection %d\n",
  313. (void *)ctx, EVP_PKEY_get0_type_name(pkey), selection);
  314. BIO_printf(trc_out, " output type: %s, output structure: %s\n",
  315. output_type, output_struct);
  316. }
  317. OSSL_TRACE_END(ENCODER);
  318. if (OSSL_ENCODER_CTX_set_output_type(ctx, output_type)
  319. && (output_struct == NULL
  320. || OSSL_ENCODER_CTX_set_output_structure(ctx, output_struct))
  321. && OSSL_ENCODER_CTX_set_selection(ctx, selection)
  322. && ossl_encoder_ctx_setup_for_pkey(ctx, pkey, selection, propquery)
  323. && OSSL_ENCODER_CTX_add_extra(ctx, libctx, propquery)) {
  324. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  325. int save_parameters = pkey->save_parameters;
  326. params[0] = OSSL_PARAM_construct_int(OSSL_ENCODER_PARAM_SAVE_PARAMETERS,
  327. &save_parameters);
  328. /* ignoring error as this is only auxiliary parameter */
  329. (void)OSSL_ENCODER_CTX_set_params(ctx, params);
  330. OSSL_TRACE_BEGIN(ENCODER)
  331. {
  332. BIO_printf(trc_out, "(ctx %p) Got %d encoders\n",
  333. (void *)ctx, OSSL_ENCODER_CTX_get_num_encoders(ctx));
  334. }
  335. OSSL_TRACE_END(ENCODER);
  336. return ctx;
  337. }
  338. OSSL_ENCODER_CTX_free(ctx);
  339. return NULL;
  340. }