encode_key2ms.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 2020-2022 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. if (out == NULL)
  38. return 0;
  39. ret = ispub ? i2b_PublicKey_bio(out, pkey) : i2b_PrivateKey_bio(out, pkey);
  40. BIO_free(out);
  41. return ret;
  42. }
  43. static int write_pvk(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
  44. EVP_PKEY *pkey)
  45. {
  46. BIO *out = NULL;
  47. int ret;
  48. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  49. out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
  50. if (out == NULL)
  51. return 0;
  52. ret = i2b_PVK_bio_ex(out, pkey, ctx->pvk_encr_level,
  53. ossl_pw_pvk_password, &ctx->pwdata, libctx, NULL);
  54. BIO_free(out);
  55. return ret;
  56. }
  57. static OSSL_FUNC_encoder_freectx_fn key2ms_freectx;
  58. static OSSL_FUNC_encoder_does_selection_fn key2ms_does_selection;
  59. static struct key2ms_ctx_st *key2ms_newctx(void *provctx)
  60. {
  61. struct key2ms_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  62. if (ctx != NULL) {
  63. ctx->provctx = provctx;
  64. /* This is the strongest encryption level */
  65. ctx->pvk_encr_level = 2;
  66. }
  67. return ctx;
  68. }
  69. static void key2ms_freectx(void *vctx)
  70. {
  71. struct key2ms_ctx_st *ctx = vctx;
  72. ossl_pw_clear_passphrase_data(&ctx->pwdata);
  73. OPENSSL_free(ctx);
  74. }
  75. static const OSSL_PARAM *key2pvk_settable_ctx_params(ossl_unused void *provctx)
  76. {
  77. static const OSSL_PARAM settables[] = {
  78. OSSL_PARAM_int(OSSL_ENCODER_PARAM_ENCRYPT_LEVEL, NULL),
  79. OSSL_PARAM_END,
  80. };
  81. return settables;
  82. }
  83. static int key2pvk_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  84. {
  85. struct key2ms_ctx_st *ctx = vctx;
  86. const OSSL_PARAM *p;
  87. p = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_ENCRYPT_LEVEL);
  88. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->pvk_encr_level))
  89. return 0;
  90. return 1;
  91. }
  92. static int key2ms_does_selection(void *vctx, int selection)
  93. {
  94. return (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0;
  95. }
  96. /*
  97. * The real EVP_PKEY_set1_TYPE() functions take a non-const key, while the key
  98. * pointer in the encode function is a const pointer. We violate the constness
  99. * knowingly, since we know that the key comes from the same provider, is never
  100. * actually const, and the implied reference count change is safe.
  101. *
  102. * EVP_PKEY_assign() can't be used, because there's no way to clear the internal
  103. * key using that function without freeing the existing internal key.
  104. */
  105. typedef int evp_pkey_set1_fn(EVP_PKEY *, const void *key);
  106. static int key2msblob_encode(void *vctx, const void *key, int selection,
  107. OSSL_CORE_BIO *cout, evp_pkey_set1_fn *set1_key,
  108. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  109. {
  110. struct key2ms_ctx_st *ctx = vctx;
  111. int ispub = -1;
  112. EVP_PKEY *pkey = NULL;
  113. int ok = 0;
  114. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  115. ispub = 0;
  116. else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  117. ispub = 1;
  118. else
  119. return 0; /* Error */
  120. if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key))
  121. ok = write_msblob(ctx, cout, pkey, ispub);
  122. EVP_PKEY_free(pkey);
  123. return ok;
  124. }
  125. static int key2pvk_encode(void *vctx, const void *key, int selection,
  126. OSSL_CORE_BIO *cout, evp_pkey_set1_fn *set1_key,
  127. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  128. {
  129. struct key2ms_ctx_st *ctx = vctx;
  130. EVP_PKEY *pkey = NULL;
  131. int ok = 0;
  132. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
  133. return 0; /* Error */
  134. if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key)
  135. && (pw_cb == NULL
  136. || ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, pw_cb, pw_cbarg)))
  137. ok = write_pvk(ctx, cout, pkey);
  138. EVP_PKEY_free(pkey);
  139. return ok;
  140. }
  141. #define dsa_set1 (evp_pkey_set1_fn *)EVP_PKEY_set1_DSA
  142. #define rsa_set1 (evp_pkey_set1_fn *)EVP_PKEY_set1_RSA
  143. #define msblob_set_params
  144. #define pvk_set_params \
  145. { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \
  146. (void (*)(void))key2pvk_settable_ctx_params }, \
  147. { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \
  148. (void (*)(void))key2pvk_set_ctx_params },
  149. #define MAKE_MS_ENCODER(impl, output, type) \
  150. static OSSL_FUNC_encoder_import_object_fn \
  151. impl##2##output##_import_object; \
  152. static OSSL_FUNC_encoder_free_object_fn impl##2##output##_free_object; \
  153. static OSSL_FUNC_encoder_encode_fn impl##2##output##_encode; \
  154. \
  155. static void * \
  156. impl##2##output##_import_object(void *ctx, int selection, \
  157. const OSSL_PARAM params[]) \
  158. { \
  159. return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
  160. ctx, selection, params); \
  161. } \
  162. static void impl##2##output##_free_object(void *key) \
  163. { \
  164. ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
  165. } \
  166. static int impl##2##output##_encode(void *vctx, OSSL_CORE_BIO *cout, \
  167. const void *key, \
  168. const OSSL_PARAM key_abstract[], \
  169. int selection, \
  170. OSSL_PASSPHRASE_CALLBACK *cb, \
  171. void *cbarg) \
  172. { \
  173. /* We don't deal with abstract objects */ \
  174. if (key_abstract != NULL) { \
  175. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
  176. return 0; \
  177. } \
  178. return key2##output##_encode(vctx, key, selection, cout, type##_set1, \
  179. cb, cbarg); \
  180. } \
  181. const OSSL_DISPATCH ossl_##impl##_to_##output##_encoder_functions[] = { \
  182. { OSSL_FUNC_ENCODER_NEWCTX, \
  183. (void (*)(void))key2ms_newctx }, \
  184. { OSSL_FUNC_ENCODER_FREECTX, \
  185. (void (*)(void))key2ms_freectx }, \
  186. output##_set_params \
  187. { OSSL_FUNC_ENCODER_DOES_SELECTION, \
  188. (void (*)(void))key2ms_does_selection }, \
  189. { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
  190. (void (*)(void))impl##2##output##_import_object }, \
  191. { OSSL_FUNC_ENCODER_FREE_OBJECT, \
  192. (void (*)(void))impl##2##output##_free_object }, \
  193. { OSSL_FUNC_ENCODER_ENCODE, \
  194. (void (*)(void))impl##2##output##_encode }, \
  195. { 0, NULL } \
  196. }
  197. #ifndef OPENSSL_NO_DSA
  198. MAKE_MS_ENCODER(dsa, pvk, dsa);
  199. MAKE_MS_ENCODER(dsa, msblob, dsa);
  200. #endif
  201. MAKE_MS_ENCODER(rsa, pvk, rsa);
  202. MAKE_MS_ENCODER(rsa, msblob, rsa);