serializer_pkey.c 12 KB

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