encode_key2ms.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright 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. /*
  10. * Low level APIs are deprecated for public use, but still ok for internal use.
  11. */
  12. #include "internal/deprecated.h"
  13. #include <string.h>
  14. #include <openssl/core.h>
  15. #include <openssl/core_dispatch.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/params.h>
  18. #include <openssl/err.h>
  19. #include <openssl/pem.h> /* Functions for writing MSBLOB and PVK */
  20. #include <openssl/dsa.h>
  21. #include "internal/passphrase.h"
  22. #include "crypto/rsa.h"
  23. #include "prov/implementations.h"
  24. #include "prov/bio.h"
  25. #include "prov/provider_ctx.h"
  26. #include "endecoder_local.h"
  27. struct key2ms_ctx_st {
  28. PROV_CTX *provctx;
  29. int pvk_encr_level;
  30. struct ossl_passphrase_data_st pwdata;
  31. };
  32. static int write_msblob(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
  33. EVP_PKEY *pkey, int ispub)
  34. {
  35. BIO *out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
  36. int ret =
  37. ispub ? i2b_PublicKey_bio(out, pkey) : i2b_PrivateKey_bio(out, pkey);
  38. BIO_free(out);
  39. return ret;
  40. }
  41. static int write_pvk(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
  42. EVP_PKEY *pkey,
  43. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  44. {
  45. BIO *out = NULL;
  46. int ret = 0;
  47. out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
  48. ret = i2b_PVK_bio(out, pkey, ctx->pvk_encr_level,
  49. ossl_pw_pem_password, &ctx->pwdata);
  50. BIO_free(out);
  51. return ret;
  52. }
  53. static OSSL_FUNC_encoder_freectx_fn key2ms_freectx;
  54. static OSSL_FUNC_encoder_gettable_params_fn key2ms_gettable_params;
  55. static OSSL_FUNC_encoder_does_selection_fn key2ms_does_selection;
  56. static struct key2ms_ctx_st *key2ms_newctx(void *provctx)
  57. {
  58. struct key2ms_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  59. if (ctx != NULL) {
  60. ctx->provctx = provctx;
  61. /* This is the strongest encryption level */
  62. ctx->pvk_encr_level = 2;
  63. }
  64. return ctx;
  65. }
  66. static void key2ms_freectx(void *vctx)
  67. {
  68. struct key2ms_ctx_st *ctx = vctx;
  69. OPENSSL_free(ctx);
  70. }
  71. static const OSSL_PARAM *key2ms_gettable_params(ossl_unused void *provctx)
  72. {
  73. static const OSSL_PARAM gettables[] = {
  74. { OSSL_ENCODER_PARAM_OUTPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
  75. OSSL_PARAM_END,
  76. };
  77. return gettables;
  78. }
  79. static int key2msblob_get_params(OSSL_PARAM params[])
  80. {
  81. OSSL_PARAM *p;
  82. p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_OUTPUT_TYPE);
  83. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "MSBLOB"))
  84. return 0;
  85. return 1;
  86. }
  87. static int key2pvk_get_params(OSSL_PARAM params[])
  88. {
  89. OSSL_PARAM *p;
  90. p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_OUTPUT_TYPE);
  91. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "PVK"))
  92. return 0;
  93. return 1;
  94. }
  95. static const OSSL_PARAM *key2pvk_settable_ctx_params(ossl_unused void *provctx)
  96. {
  97. static const OSSL_PARAM settables[] = {
  98. OSSL_PARAM_int(OSSL_ENCODER_PARAM_ENCRYPT_LEVEL, NULL),
  99. OSSL_PARAM_END,
  100. };
  101. return settables;
  102. }
  103. static int key2pvk_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  104. {
  105. struct key2ms_ctx_st *ctx = vctx;
  106. const OSSL_PARAM *p;
  107. p = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_ENCRYPT_LEVEL);
  108. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->pvk_encr_level))
  109. return 0;
  110. return 1;
  111. }
  112. static int key2ms_does_selection(void *vctx, int selection)
  113. {
  114. return (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0;
  115. }
  116. /*
  117. * The real EVP_PKEY_set1_TYPE() functions take a non-const key, while the key
  118. * pointer in the encode function is a const pointer. We violate the constness
  119. * knowingly, since we know that the key comes from the same provider, is never
  120. * actually const, and the implied reference count change is safe.
  121. *
  122. * EVP_PKEY_assign() can't be used, because there's no way to clear the internal
  123. * key using that function without freeing the existing internal key.
  124. */
  125. typedef int evp_pkey_set1_fn(EVP_PKEY *, const void *key);
  126. static int key2msblob_encode(void *vctx, const void *key, int selection,
  127. OSSL_CORE_BIO *cout, evp_pkey_set1_fn *set1_key,
  128. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  129. {
  130. struct key2ms_ctx_st *ctx = vctx;
  131. int ispub = -1;
  132. EVP_PKEY *pkey = NULL;
  133. int ok = 0;
  134. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  135. ispub = 0;
  136. else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  137. ispub = 1;
  138. else
  139. return 0; /* Error */
  140. if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key))
  141. ok = write_msblob(ctx, cout, pkey, ispub);
  142. EVP_PKEY_free(pkey);
  143. return ok;
  144. }
  145. static int key2pvk_encode(void *vctx, const void *key, int selection,
  146. OSSL_CORE_BIO *cout, evp_pkey_set1_fn *set1_key,
  147. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  148. {
  149. struct key2ms_ctx_st *ctx = vctx;
  150. EVP_PKEY *pkey = NULL;
  151. int ok = 0;
  152. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
  153. return 0; /* Error */
  154. if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key))
  155. ok = write_pvk(ctx, cout, pkey, pw_cb, pw_cbarg);
  156. EVP_PKEY_free(pkey);
  157. return ok;
  158. }
  159. #define dsa_set1 (evp_pkey_set1_fn *)EVP_PKEY_set1_DSA
  160. #define rsa_set1 (evp_pkey_set1_fn *)EVP_PKEY_set1_RSA
  161. #define msblob_set_params
  162. #define pvk_set_params \
  163. { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \
  164. (void (*)(void))key2pvk_settable_ctx_params }, \
  165. { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \
  166. (void (*)(void))key2pvk_set_ctx_params },
  167. #define MAKE_MS_ENCODER(impl, output, type) \
  168. static OSSL_FUNC_encoder_import_object_fn \
  169. impl##2##output##_import_object; \
  170. static OSSL_FUNC_encoder_free_object_fn impl##2##output##_free_object; \
  171. static OSSL_FUNC_encoder_encode_fn impl##2##output##_encode; \
  172. \
  173. static void * \
  174. impl##2##output##_import_object(void *ctx, int selection, \
  175. const OSSL_PARAM params[]) \
  176. { \
  177. return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
  178. ctx, selection, params); \
  179. } \
  180. static void impl##2##output##_free_object(void *key) \
  181. { \
  182. ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
  183. } \
  184. static int impl##2##output##_encode(void *vctx, OSSL_CORE_BIO *cout, \
  185. const void *key, \
  186. const OSSL_PARAM key_abstract[], \
  187. int selection, \
  188. OSSL_PASSPHRASE_CALLBACK *cb, \
  189. void *cbarg) \
  190. { \
  191. /* We don't deal with abstract objects */ \
  192. if (key_abstract != NULL) { \
  193. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
  194. return 0; \
  195. } \
  196. return key2##output##_encode(vctx, key, selection, cout, type##_set1, \
  197. cb, cbarg); \
  198. } \
  199. const OSSL_DISPATCH ossl_##impl##_to_##output##_encoder_functions[] = { \
  200. { OSSL_FUNC_ENCODER_NEWCTX, \
  201. (void (*)(void))key2ms_newctx }, \
  202. { OSSL_FUNC_ENCODER_FREECTX, \
  203. (void (*)(void))key2ms_freectx }, \
  204. { OSSL_FUNC_ENCODER_GETTABLE_PARAMS, \
  205. (void (*)(void))key2ms_gettable_params }, \
  206. { OSSL_FUNC_ENCODER_GET_PARAMS, \
  207. (void (*)(void))key2##output##_get_params }, \
  208. output##_set_params \
  209. { OSSL_FUNC_ENCODER_DOES_SELECTION, \
  210. (void (*)(void))key2ms_does_selection }, \
  211. { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
  212. (void (*)(void))impl##2##output##_import_object }, \
  213. { OSSL_FUNC_ENCODER_FREE_OBJECT, \
  214. (void (*)(void))impl##2##output##_free_object }, \
  215. { OSSL_FUNC_ENCODER_ENCODE, \
  216. (void (*)(void))impl##2##output##_encode }, \
  217. { 0, NULL } \
  218. }
  219. #ifndef OPENSSL_NO_DSA
  220. MAKE_MS_ENCODER(dsa, pvk, dsa);
  221. MAKE_MS_ENCODER(dsa, msblob, dsa);
  222. #endif
  223. MAKE_MS_ENCODER(rsa, pvk, rsa);
  224. MAKE_MS_ENCODER(rsa, msblob, rsa);