serializer_pkey.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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,
  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, 1);
  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_to_bio:
  93. * writing callback for the OSSL_PARAM (the implementation doesn't have
  94. * intimate knowledge of the provider side object)
  95. */
  96. struct serializer_write_data_st {
  97. OSSL_SERIALIZER_CTX *ctx;
  98. BIO *out;
  99. };
  100. static int serializer_write_cb(const OSSL_PARAM params[], void *arg)
  101. {
  102. struct serializer_write_data_st *write_data = arg;
  103. OSSL_SERIALIZER_CTX *ctx = write_data->ctx;
  104. BIO *out = write_data->out;
  105. return ctx->ser->serialize_data(ctx->serctx, params, (OSSL_CORE_BIO *)out,
  106. ossl_serializer_passphrase_out_cb, ctx);
  107. }
  108. /*
  109. * Support for OSSL_SERIALIZER_to_bio:
  110. * Perform the actual output.
  111. */
  112. static int serializer_EVP_PKEY_to_bio(OSSL_SERIALIZER_CTX *ctx, BIO *out)
  113. {
  114. const EVP_PKEY *pkey = ctx->object;
  115. void *keydata = pkey->keydata;
  116. EVP_KEYMGMT *keymgmt = pkey->keymgmt;
  117. /*
  118. * OSSL_SERIALIZER_CTX_new() creates a context, even when the
  119. * serializer it's given is NULL. Callers can detect the lack
  120. * of serializer with OSSL_SERIALIZER_CTX_get_serializer() and
  121. * should take precautions, possibly call a fallback instead of
  122. * OSSL_SERIALIZER_to_bio() / OSSL_SERIALIZER_to_fp(). If it's
  123. * come this far, we return an error.
  124. */
  125. if (ctx->ser == NULL)
  126. return 0;
  127. if (ctx->ser->serialize_object == NULL
  128. || OSSL_SERIALIZER_provider(ctx->ser) != EVP_KEYMGMT_provider(keymgmt)) {
  129. struct serializer_write_data_st write_data;
  130. write_data.ctx = ctx;
  131. write_data.out = out;
  132. return evp_keymgmt_export(keymgmt, keydata, ctx->selection,
  133. &serializer_write_cb, &write_data);
  134. }
  135. return ctx->ser->serialize_object(ctx->serctx, keydata,
  136. (OSSL_CORE_BIO *)out,
  137. ossl_serializer_passphrase_out_cb, ctx);
  138. }
  139. /*
  140. * OSSL_SERIALIZER_CTX_new_by_EVP_PKEY() returns a ctx with no serializer if
  141. * it couldn't find a suitable serializer. This allows a caller to detect if
  142. * a suitable serializer was found, with OSSL_SERIALIZER_CTX_get_serializer(),
  143. * and to use fallback methods if the result is NULL.
  144. */
  145. OSSL_SERIALIZER_CTX *OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(const EVP_PKEY *pkey,
  146. const char *propquery)
  147. {
  148. OSSL_SERIALIZER_CTX *ctx = NULL;
  149. OSSL_SERIALIZER *ser = NULL;
  150. EVP_KEYMGMT *keymgmt = pkey->keymgmt;
  151. int selection = OSSL_KEYMGMT_SELECT_ALL;
  152. if (!ossl_assert(pkey != NULL && propquery != NULL)) {
  153. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
  154. return NULL;
  155. }
  156. if (keymgmt != NULL) {
  157. const OSSL_PROVIDER *desired_prov = EVP_KEYMGMT_provider(keymgmt);
  158. OPENSSL_CTX *libctx = ossl_provider_library_context(desired_prov);
  159. struct selected_serializer_st sel_data;
  160. OSSL_SERIALIZER *first = NULL;
  161. const char *name;
  162. int i;
  163. /*
  164. * Select the serializer in two steps. First, get the names of all of
  165. * the serializers. Then determine which is the best one to use.
  166. * This has to be broken because it isn't possible to fetch the
  167. * serialisers inside EVP_KEYMGMT_names_do_all() due to locking
  168. * order inversions with the store lock.
  169. */
  170. sel_data.error = 0;
  171. sel_data.names = sk_OPENSSL_CSTRING_new_null();
  172. if (sel_data.names == NULL)
  173. return NULL;
  174. EVP_KEYMGMT_names_do_all(keymgmt, cache_serializers, &sel_data);
  175. /*
  176. * Ignore memory allocation errors that are indicated in sel_data.error
  177. * in case a suitable provider does get found regardless.
  178. */
  179. /*
  180. * Serializers offer two functions, one that handles object data in
  181. * the form of a OSSL_PARAM array, and one that directly handles a
  182. * provider side object. The latter requires that the serializer
  183. * is offered by the same provider that holds that object, but is
  184. * more desirable because it usually provides faster serialization.
  185. *
  186. * When looking up possible serializers, we save the first that can
  187. * handle an OSSL_PARAM array in |first| and use that if nothing
  188. * better turns up.
  189. */
  190. for (i = 0; i < sk_OPENSSL_CSTRING_num(sel_data.names); i++) {
  191. name = sk_OPENSSL_CSTRING_value(sel_data.names, i);
  192. ser = OSSL_SERIALIZER_fetch(libctx, name, propquery);
  193. if (ser != NULL) {
  194. if (OSSL_SERIALIZER_provider(ser) == desired_prov
  195. && ser->serialize_object != NULL) {
  196. OSSL_SERIALIZER_free(first);
  197. break;
  198. }
  199. if (first == NULL && ser->serialize_data != NULL)
  200. first = ser;
  201. else
  202. OSSL_SERIALIZER_free(ser);
  203. ser = NULL;
  204. }
  205. }
  206. sk_OPENSSL_CSTRING_free(sel_data.names);
  207. if (ser == NULL)
  208. ser = first;
  209. if (ser != NULL) {
  210. OSSL_PROPERTY_LIST *check = NULL, *current_props = NULL;
  211. check = ossl_parse_query(libctx, "type=parameters");
  212. current_props =
  213. ossl_parse_property(libctx, OSSL_SERIALIZER_properties(ser));
  214. if (ossl_property_match_count(check, current_props) > 0)
  215. selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  216. ossl_property_free(current_props);
  217. ossl_property_free(check);
  218. } else {
  219. if (sel_data.error)
  220. ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_MALLOC_FAILURE);
  221. else
  222. ERR_raise(ERR_LIB_OSSL_SERIALIZER,
  223. OSSL_SERIALIZER_R_SERIALIZER_NOT_FOUND);
  224. }
  225. }
  226. ctx = OSSL_SERIALIZER_CTX_new(ser); /* refcnt(ser)++ */
  227. OSSL_SERIALIZER_free(ser); /* refcnt(ser)-- */
  228. if (ctx != NULL) {
  229. /* Setup for OSSL_SERIALIZE_to_bio() */
  230. ctx->selection = selection;
  231. ctx->object = pkey;
  232. ctx->do_output = serializer_EVP_PKEY_to_bio;
  233. }
  234. return ctx;
  235. }