serializer_pkey.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright 2019-2020 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/serializer.h>
  13. #include <openssl/core_names.h>
  14. #include "internal/provider.h"
  15. #include "internal/property.h"
  16. #include "crypto/evp.h"
  17. #include "serializer_local.h"
  18. int OSSL_SERIALIZER_CTX_set_cipher(OSSL_SERIALIZER_CTX *ctx,
  19. const char *cipher_name,
  20. const char *propquery)
  21. {
  22. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
  23. params[0] =
  24. OSSL_PARAM_construct_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER,
  25. (void *)cipher_name, 0);
  26. params[1] =
  27. OSSL_PARAM_construct_utf8_string(OSSL_SERIALIZER_PARAM_PROPERTIES,
  28. (void *)propquery, 0);
  29. return OSSL_SERIALIZER_CTX_set_params(ctx, params);
  30. }
  31. int OSSL_SERIALIZER_CTX_set_passphrase(OSSL_SERIALIZER_CTX *ctx,
  32. const unsigned char *kstr,
  33. size_t klen)
  34. {
  35. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  36. params[0] = OSSL_PARAM_construct_octet_string(OSSL_SERIALIZER_PARAM_PASS,
  37. (void *)kstr, klen);
  38. return OSSL_SERIALIZER_CTX_set_params(ctx, params);
  39. }
  40. static void serializer_ctx_reset_passphrase_ui(OSSL_SERIALIZER_CTX *ctx)
  41. {
  42. UI_destroy_method(ctx->allocated_ui_method);
  43. ctx->allocated_ui_method = NULL;
  44. ctx->ui_method = NULL;
  45. ctx->ui_data = NULL;
  46. }
  47. int OSSL_SERIALIZER_CTX_set_passphrase_ui(OSSL_SERIALIZER_CTX *ctx,
  48. const UI_METHOD *ui_method,
  49. void *ui_data)
  50. {
  51. if (!ossl_assert(ctx != NULL)) {
  52. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
  53. return 0;
  54. }
  55. serializer_ctx_reset_passphrase_ui(ctx);
  56. ctx->ui_method = ui_method;
  57. ctx->ui_data = ui_data;
  58. return 1;
  59. }
  60. int OSSL_SERIALIZER_CTX_set_passphrase_cb(OSSL_SERIALIZER_CTX *ctx, int enc,
  61. pem_password_cb *cb, void *cbarg)
  62. {
  63. if (!ossl_assert(ctx != NULL)) {
  64. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
  65. return 0;
  66. }
  67. serializer_ctx_reset_passphrase_ui(ctx);
  68. if (cb == NULL)
  69. return 1;
  70. ctx->ui_method =
  71. ctx->allocated_ui_method = UI_UTIL_wrap_read_pem_callback(cb, enc);
  72. ctx->ui_data = cbarg;
  73. return ctx->ui_method != NULL;
  74. }
  75. /*
  76. * Support for OSSL_SERIALIZER_CTX_new_by_TYPE:
  77. * finding a suitable serializer
  78. */
  79. struct selected_serializer_st {
  80. OPENSSL_CTX *libctx;
  81. const OSSL_PROVIDER *desired_provider;
  82. const char *propquery;
  83. /*
  84. * Serializers offer two functions, one that handles object data in
  85. * the form of a OSSL_PARAM array, and one that directly handles a
  86. * provider side object. The latter requires that the serializer
  87. * is offered by the same provider that holds that object, but is
  88. * more desirable because it usually provides faster serialization.
  89. *
  90. * When looking up possible serializers, we save the first that can
  91. * handle an OSSL_PARAM array in |first|, and the first that can
  92. * handle a provider side object in |desired|.
  93. */
  94. OSSL_SERIALIZER *first;
  95. OSSL_SERIALIZER *desired;
  96. };
  97. static void select_serializer(const char *name, void *data)
  98. {
  99. struct selected_serializer_st *d = data;
  100. OSSL_SERIALIZER *s = NULL;
  101. /* No need to look further if we already have the more desirable option */
  102. if (d->desired != NULL)
  103. return;
  104. if ((s = OSSL_SERIALIZER_fetch(d->libctx, name, d->propquery)) != NULL) {
  105. if (OSSL_SERIALIZER_provider(s) == d->desired_provider
  106. && s->serialize_object != NULL) {
  107. OSSL_SERIALIZER_free(d->first);
  108. d->first = NULL;
  109. d->desired = s;
  110. } else if (d->first == NULL && s->serialize_data != NULL) {
  111. d->first = s;
  112. } else {
  113. OSSL_SERIALIZER_free(s);
  114. }
  115. }
  116. }
  117. /*
  118. * Support for OSSL_SERIALIZER_CTX_new_by_TYPE and OSSL_SERIALIZER_to_bio:
  119. * Passphrase callbacks
  120. */
  121. /*
  122. * First, we define the generic passphrase function that supports both
  123. * outgoing (with passphrase verify) and incoming (without passphrase verify)
  124. * passphrase reading.
  125. */
  126. static int serializer_passphrase(char *pass, size_t pass_size,
  127. size_t *pass_len, int verify,
  128. const OSSL_PARAM params[], void *arg)
  129. {
  130. OSSL_SERIALIZER_CTX *ctx = arg;
  131. const OSSL_PARAM *p;
  132. const char *prompt_info = NULL;
  133. char *prompt = NULL, *vpass = NULL;
  134. int prompt_idx = -1, verify_idx = -1;
  135. UI *ui = NULL;
  136. int ret = 0;
  137. if (!ossl_assert(ctx != NULL && pass != NULL
  138. && pass_size != 0 && pass_len != NULL)) {
  139. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
  140. return 0;
  141. }
  142. if ((p = OSSL_PARAM_locate_const(params,
  143. OSSL_PASSPHRASE_PARAM_INFO)) != NULL) {
  144. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  145. return 0;
  146. prompt_info = p->data;
  147. }
  148. if ((ui = UI_new()) == NULL) {
  149. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_MALLOC_FAILURE);
  150. return 0;
  151. }
  152. UI_set_method(ui, ctx->ui_method);
  153. UI_add_user_data(ui, ctx->ui_data);
  154. /* Get an application constructed prompt */
  155. prompt = UI_construct_prompt(ui, "pass phrase", prompt_info);
  156. if (prompt == NULL) {
  157. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_MALLOC_FAILURE);
  158. goto end;
  159. }
  160. prompt_idx = UI_add_input_string(ui, prompt,
  161. UI_INPUT_FLAG_DEFAULT_PWD,
  162. pass, 0, pass_size - 1) - 1;
  163. if (prompt_idx < 0) {
  164. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_UI_LIB);
  165. goto end;
  166. }
  167. if (verify) {
  168. /* Get a buffer for verification prompt */
  169. vpass = OPENSSL_zalloc(pass_size);
  170. if (vpass == NULL) {
  171. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_MALLOC_FAILURE);
  172. goto end;
  173. }
  174. verify_idx = UI_add_verify_string(ui, prompt,
  175. UI_INPUT_FLAG_DEFAULT_PWD,
  176. vpass, 0, pass_size - 1,
  177. pass) - 1;
  178. if (verify_idx < 0) {
  179. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_UI_LIB);
  180. goto end;
  181. }
  182. }
  183. switch (UI_process(ui)) {
  184. case -2:
  185. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_INTERRUPTED_OR_CANCELLED);
  186. break;
  187. case -1:
  188. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_UI_LIB);
  189. break;
  190. default:
  191. *pass_len = (size_t)UI_get_result_length(ui, prompt_idx);
  192. ret = 1;
  193. break;
  194. }
  195. end:
  196. OPENSSL_free(vpass);
  197. OPENSSL_free(prompt);
  198. UI_free(ui);
  199. return ret;
  200. }
  201. /* Ensure correct function definition for outgoing passphrase reader */
  202. static OSSL_PASSPHRASE_CALLBACK serializer_passphrase_out_cb;
  203. static int serializer_passphrase_out_cb(char *pass, size_t pass_size,
  204. size_t *pass_len,
  205. const OSSL_PARAM params[], void *arg)
  206. {
  207. return serializer_passphrase(pass, pass_size, pass_len, 1, params, arg);
  208. }
  209. /*
  210. * Support for OSSL_SERIALIZER_to_bio:
  211. * writing callback for the OSSL_PARAM (the implementation doesn't have
  212. * intimate knowledge of the provider side object)
  213. */
  214. struct serializer_write_data_st {
  215. OSSL_SERIALIZER_CTX *ctx;
  216. BIO *out;
  217. };
  218. static int serializer_write_cb(const OSSL_PARAM params[], void *arg)
  219. {
  220. struct serializer_write_data_st *write_data = arg;
  221. OSSL_SERIALIZER_CTX *ctx = write_data->ctx;
  222. BIO *out = write_data->out;
  223. return ctx->ser->serialize_data(ctx->serctx, params, out,
  224. serializer_passphrase_out_cb, ctx);
  225. }
  226. /*
  227. * Support for OSSL_SERIALIZER_to_bio:
  228. * Perform the actual output.
  229. */
  230. static int serializer_EVP_PKEY_to_bio(OSSL_SERIALIZER_CTX *ctx, BIO *out)
  231. {
  232. const EVP_PKEY *pkey = ctx->object;
  233. void *keydata = pkey->keydata;
  234. EVP_KEYMGMT *keymgmt = pkey->keymgmt;
  235. /*
  236. * OSSL_SERIALIZER_CTX_new() creates a context, even when the
  237. * serializer it's given is NULL. Callers can detect the lack
  238. * of serializer with OSSL_SERIALIZER_CTX_get_serializer() and
  239. * should take precautions, possibly call a fallback instead of
  240. * OSSL_SERIALIZER_to_bio() / OSSL_SERIALIZER_to_fp(). If it's
  241. * come this far, we return an error.
  242. */
  243. if (ctx->ser == NULL)
  244. return 0;
  245. if (ctx->ser->serialize_object == NULL) {
  246. struct serializer_write_data_st write_data;
  247. write_data.ctx = ctx;
  248. write_data.out = out;
  249. return evp_keymgmt_export(keymgmt, keydata, ctx->selection,
  250. &serializer_write_cb, &write_data);
  251. }
  252. return ctx->ser->serialize_object(ctx->serctx, keydata, out,
  253. serializer_passphrase_out_cb, ctx);
  254. }
  255. /*
  256. * OSSL_SERIALIZER_CTX_new_by_EVP_PKEY() returns a ctx with no serializer if
  257. * it couldn't find a suitable serializer. This allows a caller to detect if
  258. * a suitable serializer was found, with OSSL_SERIALIZER_CTX_get_serializer(),
  259. * and to use fallback methods if the result is NULL.
  260. */
  261. OSSL_SERIALIZER_CTX *OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(const EVP_PKEY *pkey,
  262. const char *propquery)
  263. {
  264. OSSL_SERIALIZER_CTX *ctx = NULL;
  265. OSSL_SERIALIZER *ser = NULL;
  266. EVP_KEYMGMT *keymgmt = pkey->keymgmt;
  267. int selection = OSSL_KEYMGMT_SELECT_ALL;
  268. if (!ossl_assert(pkey != NULL && propquery != NULL)) {
  269. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
  270. return NULL;
  271. }
  272. if (keymgmt != NULL) {
  273. const OSSL_PROVIDER *desired_prov = EVP_KEYMGMT_provider(keymgmt);
  274. OPENSSL_CTX *libctx = ossl_provider_library_context(desired_prov);
  275. struct selected_serializer_st sel_data;
  276. OSSL_PROPERTY_LIST *check =
  277. ossl_parse_query(libctx, "type=parameters");
  278. OSSL_PROPERTY_LIST *current_props = NULL;
  279. memset(&sel_data, 0, sizeof(sel_data));
  280. sel_data.libctx = libctx;
  281. sel_data.desired_provider = desired_prov;
  282. sel_data.propquery = propquery;
  283. EVP_KEYMGMT_names_do_all(keymgmt, select_serializer, &sel_data);
  284. if (sel_data.desired != NULL) {
  285. ser = sel_data.desired;
  286. sel_data.desired = NULL;
  287. } else if (sel_data.first != NULL) {
  288. ser = sel_data.first;
  289. sel_data.first = NULL;
  290. }
  291. OSSL_SERIALIZER_free(sel_data.first);
  292. OSSL_SERIALIZER_free(sel_data.desired);
  293. if (ser != NULL) {
  294. current_props =
  295. ossl_parse_property(libctx, OSSL_SERIALIZER_properties(ser));
  296. if (ossl_property_match_count(check, current_props) > 0)
  297. selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  298. ossl_property_free(current_props);
  299. }
  300. ossl_property_free(check);
  301. }
  302. ctx = OSSL_SERIALIZER_CTX_new(ser); /* refcnt(ser)++ */
  303. OSSL_SERIALIZER_free(ser); /* refcnt(ser)-- */
  304. if (ctx != NULL) {
  305. /* Setup for OSSL_SERIALIZE_to_bio() */
  306. ctx->selection = selection;
  307. ctx->object = pkey;
  308. ctx->do_output = serializer_EVP_PKEY_to_bio;
  309. }
  310. return ctx;
  311. }