decode_msblob2key.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 2020-2023 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
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/core_dispatch.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/core_object.h>
  18. #include <openssl/crypto.h>
  19. #include <openssl/params.h>
  20. #include <openssl/pem.h> /* For public PVK functions */
  21. #include <openssl/x509.h>
  22. #include <openssl/err.h>
  23. #include "internal/passphrase.h"
  24. #include "crypto/pem.h" /* For internal PVK and "blob" headers */
  25. #include "crypto/rsa.h"
  26. #include "prov/bio.h"
  27. #include "prov/implementations.h"
  28. #include "endecoder_local.h"
  29. struct msblob2key_ctx_st; /* Forward declaration */
  30. typedef void *b2i_of_void_fn(const unsigned char **in, unsigned int bitlen,
  31. int ispub);
  32. typedef void adjust_key_fn(void *, struct msblob2key_ctx_st *ctx);
  33. typedef void free_key_fn(void *);
  34. struct keytype_desc_st {
  35. int type; /* EVP key type */
  36. const char *name; /* Keytype */
  37. const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
  38. b2i_of_void_fn *read_private_key;
  39. b2i_of_void_fn *read_public_key;
  40. adjust_key_fn *adjust_key;
  41. free_key_fn *free_key;
  42. };
  43. static OSSL_FUNC_decoder_freectx_fn msblob2key_freectx;
  44. static OSSL_FUNC_decoder_decode_fn msblob2key_decode;
  45. static OSSL_FUNC_decoder_export_object_fn msblob2key_export_object;
  46. /*
  47. * Context used for DER to key decoding.
  48. */
  49. struct msblob2key_ctx_st {
  50. PROV_CTX *provctx;
  51. const struct keytype_desc_st *desc;
  52. /* The selection that is passed to msblob2key_decode() */
  53. int selection;
  54. };
  55. static struct msblob2key_ctx_st *
  56. msblob2key_newctx(void *provctx, const struct keytype_desc_st *desc)
  57. {
  58. struct msblob2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  59. if (ctx != NULL) {
  60. ctx->provctx = provctx;
  61. ctx->desc = desc;
  62. }
  63. return ctx;
  64. }
  65. static void msblob2key_freectx(void *vctx)
  66. {
  67. struct msblob2key_ctx_st *ctx = vctx;
  68. OPENSSL_free(ctx);
  69. }
  70. static int msblob2key_does_selection(void *provctx, int selection)
  71. {
  72. if (selection == 0)
  73. return 1;
  74. if ((selection & (OSSL_KEYMGMT_SELECT_PRIVATE_KEY
  75. | OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) != 0)
  76. return 1;
  77. return 0;
  78. }
  79. static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
  80. OSSL_CALLBACK *data_cb, void *data_cbarg,
  81. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  82. {
  83. struct msblob2key_ctx_st *ctx = vctx;
  84. BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
  85. const unsigned char *p;
  86. unsigned char hdr_buf[16], *buf = NULL;
  87. unsigned int bitlen, magic, length;
  88. int isdss = -1;
  89. int ispub = -1;
  90. void *key = NULL;
  91. int ok = 0;
  92. if (in == NULL)
  93. return 0;
  94. if (BIO_read(in, hdr_buf, 16) != 16) {
  95. ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
  96. goto next;
  97. }
  98. ERR_set_mark();
  99. p = hdr_buf;
  100. ok = ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) > 0;
  101. ERR_pop_to_mark();
  102. if (!ok)
  103. goto next;
  104. ctx->selection = selection;
  105. ok = 0; /* Assume that we fail */
  106. if ((isdss && ctx->desc->type != EVP_PKEY_DSA)
  107. || (!isdss && ctx->desc->type != EVP_PKEY_RSA))
  108. goto next;
  109. length = ossl_blob_length(bitlen, isdss, ispub);
  110. if (length > BLOB_MAX_LENGTH) {
  111. ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
  112. goto next;
  113. }
  114. buf = OPENSSL_malloc(length);
  115. if (buf == NULL) {
  116. ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
  117. goto end;
  118. }
  119. p = buf;
  120. if (BIO_read(in, buf, length) != (int)length) {
  121. ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
  122. goto next;
  123. }
  124. if ((selection == 0
  125. || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  126. && !ispub
  127. && ctx->desc->read_private_key != NULL) {
  128. struct ossl_passphrase_data_st pwdata;
  129. memset(&pwdata, 0, sizeof(pwdata));
  130. if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
  131. goto end;
  132. p = buf;
  133. key = ctx->desc->read_private_key(&p, bitlen, ispub);
  134. if (selection != 0 && key == NULL)
  135. goto next;
  136. }
  137. if (key == NULL && (selection == 0
  138. || (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  139. && ispub
  140. && ctx->desc->read_public_key != NULL) {
  141. p = buf;
  142. key = ctx->desc->read_public_key(&p, bitlen, ispub);
  143. if (selection != 0 && key == NULL)
  144. goto next;
  145. }
  146. if (key != NULL && ctx->desc->adjust_key != NULL)
  147. ctx->desc->adjust_key(key, ctx);
  148. next:
  149. /*
  150. * Indicated that we successfully decoded something, or not at all.
  151. * Ending up "empty handed" is not an error.
  152. */
  153. ok = 1;
  154. /*
  155. * We free resources here so it's not held up during the callback, because
  156. * we know the process is recursive and the allocated chunks of memory
  157. * add up.
  158. */
  159. OPENSSL_free(buf);
  160. BIO_free(in);
  161. buf = NULL;
  162. in = NULL;
  163. if (key != NULL) {
  164. OSSL_PARAM params[4];
  165. int object_type = OSSL_OBJECT_PKEY;
  166. params[0] =
  167. OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
  168. params[1] =
  169. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  170. (char *)ctx->desc->name, 0);
  171. /* The address of the key becomes the octet string */
  172. params[2] =
  173. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
  174. &key, sizeof(key));
  175. params[3] = OSSL_PARAM_construct_end();
  176. ok = data_cb(params, data_cbarg);
  177. }
  178. end:
  179. BIO_free(in);
  180. OPENSSL_free(buf);
  181. ctx->desc->free_key(key);
  182. return ok;
  183. }
  184. static int
  185. msblob2key_export_object(void *vctx,
  186. const void *reference, size_t reference_sz,
  187. OSSL_CALLBACK *export_cb, void *export_cbarg)
  188. {
  189. struct msblob2key_ctx_st *ctx = vctx;
  190. OSSL_FUNC_keymgmt_export_fn *export =
  191. ossl_prov_get_keymgmt_export(ctx->desc->fns);
  192. void *keydata;
  193. if (reference_sz == sizeof(keydata) && export != NULL) {
  194. int selection = ctx->selection;
  195. if (selection == 0)
  196. selection = OSSL_KEYMGMT_SELECT_ALL;
  197. /* The contents of the reference is the address to our object */
  198. keydata = *(void **)reference;
  199. return export(keydata, selection, export_cb, export_cbarg);
  200. }
  201. return 0;
  202. }
  203. /* ---------------------------------------------------------------------- */
  204. #define dsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header
  205. #define dsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header
  206. #define dsa_adjust NULL
  207. #define dsa_free (void (*)(void *))DSA_free
  208. /* ---------------------------------------------------------------------- */
  209. #define rsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header
  210. #define rsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header
  211. static void rsa_adjust(void *key, struct msblob2key_ctx_st *ctx)
  212. {
  213. ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
  214. }
  215. #define rsa_free (void (*)(void *))RSA_free
  216. /* ---------------------------------------------------------------------- */
  217. #define IMPLEMENT_MSBLOB(KEYTYPE, keytype) \
  218. static const struct keytype_desc_st mstype##2##keytype##_desc = { \
  219. EVP_PKEY_##KEYTYPE, #KEYTYPE, \
  220. ossl_##keytype##_keymgmt_functions, \
  221. keytype##_decode_private_key, \
  222. keytype##_decode_public_key, \
  223. keytype##_adjust, \
  224. keytype##_free \
  225. }; \
  226. static OSSL_FUNC_decoder_newctx_fn msblob2##keytype##_newctx; \
  227. static void *msblob2##keytype##_newctx(void *provctx) \
  228. { \
  229. return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \
  230. } \
  231. const OSSL_DISPATCH \
  232. ossl_msblob_to_##keytype##_decoder_functions[] = { \
  233. { OSSL_FUNC_DECODER_NEWCTX, \
  234. (void (*)(void))msblob2##keytype##_newctx }, \
  235. { OSSL_FUNC_DECODER_FREECTX, \
  236. (void (*)(void))msblob2key_freectx }, \
  237. { OSSL_FUNC_DECODER_DOES_SELECTION, \
  238. (void (*)(void))msblob2key_does_selection }, \
  239. { OSSL_FUNC_DECODER_DECODE, \
  240. (void (*)(void))msblob2key_decode }, \
  241. { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
  242. (void (*)(void))msblob2key_export_object }, \
  243. { 0, NULL } \
  244. }
  245. #ifndef OPENSSL_NO_DSA
  246. IMPLEMENT_MSBLOB(DSA, dsa);
  247. #endif
  248. IMPLEMENT_MSBLOB(RSA, rsa);