decode_msblob2key.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. goto end;
  117. p = buf;
  118. if (BIO_read(in, buf, length) != (int)length) {
  119. ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
  120. goto next;
  121. }
  122. if ((selection == 0
  123. || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  124. && !ispub
  125. && ctx->desc->read_private_key != NULL) {
  126. struct ossl_passphrase_data_st pwdata;
  127. memset(&pwdata, 0, sizeof(pwdata));
  128. if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
  129. goto end;
  130. p = buf;
  131. key = ctx->desc->read_private_key(&p, bitlen, ispub);
  132. if (selection != 0 && key == NULL)
  133. goto next;
  134. }
  135. if (key == NULL && (selection == 0
  136. || (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  137. && ispub
  138. && ctx->desc->read_public_key != NULL) {
  139. p = buf;
  140. key = ctx->desc->read_public_key(&p, bitlen, ispub);
  141. if (selection != 0 && key == NULL)
  142. goto next;
  143. }
  144. if (key != NULL && ctx->desc->adjust_key != NULL)
  145. ctx->desc->adjust_key(key, ctx);
  146. next:
  147. /*
  148. * Indicated that we successfully decoded something, or not at all.
  149. * Ending up "empty handed" is not an error.
  150. */
  151. ok = 1;
  152. /*
  153. * We free resources here so it's not held up during the callback, because
  154. * we know the process is recursive and the allocated chunks of memory
  155. * add up.
  156. */
  157. OPENSSL_free(buf);
  158. BIO_free(in);
  159. buf = NULL;
  160. in = NULL;
  161. if (key != NULL) {
  162. OSSL_PARAM params[4];
  163. int object_type = OSSL_OBJECT_PKEY;
  164. params[0] =
  165. OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
  166. params[1] =
  167. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  168. (char *)ctx->desc->name, 0);
  169. /* The address of the key becomes the octet string */
  170. params[2] =
  171. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
  172. &key, sizeof(key));
  173. params[3] = OSSL_PARAM_construct_end();
  174. ok = data_cb(params, data_cbarg);
  175. }
  176. end:
  177. BIO_free(in);
  178. OPENSSL_free(buf);
  179. ctx->desc->free_key(key);
  180. return ok;
  181. }
  182. static int
  183. msblob2key_export_object(void *vctx,
  184. const void *reference, size_t reference_sz,
  185. OSSL_CALLBACK *export_cb, void *export_cbarg)
  186. {
  187. struct msblob2key_ctx_st *ctx = vctx;
  188. OSSL_FUNC_keymgmt_export_fn *export =
  189. ossl_prov_get_keymgmt_export(ctx->desc->fns);
  190. void *keydata;
  191. if (reference_sz == sizeof(keydata) && export != NULL) {
  192. int selection = ctx->selection;
  193. if (selection == 0)
  194. selection = OSSL_KEYMGMT_SELECT_ALL;
  195. /* The contents of the reference is the address to our object */
  196. keydata = *(void **)reference;
  197. return export(keydata, selection, export_cb, export_cbarg);
  198. }
  199. return 0;
  200. }
  201. /* ---------------------------------------------------------------------- */
  202. #define dsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header
  203. #define dsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header
  204. #define dsa_adjust NULL
  205. #define dsa_free (void (*)(void *))DSA_free
  206. /* ---------------------------------------------------------------------- */
  207. #define rsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header
  208. #define rsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header
  209. static void rsa_adjust(void *key, struct msblob2key_ctx_st *ctx)
  210. {
  211. ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
  212. }
  213. #define rsa_free (void (*)(void *))RSA_free
  214. /* ---------------------------------------------------------------------- */
  215. #define IMPLEMENT_MSBLOB(KEYTYPE, keytype) \
  216. static const struct keytype_desc_st mstype##2##keytype##_desc = { \
  217. EVP_PKEY_##KEYTYPE, #KEYTYPE, \
  218. ossl_##keytype##_keymgmt_functions, \
  219. keytype##_decode_private_key, \
  220. keytype##_decode_public_key, \
  221. keytype##_adjust, \
  222. keytype##_free \
  223. }; \
  224. static OSSL_FUNC_decoder_newctx_fn msblob2##keytype##_newctx; \
  225. static void *msblob2##keytype##_newctx(void *provctx) \
  226. { \
  227. return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \
  228. } \
  229. const OSSL_DISPATCH \
  230. ossl_msblob_to_##keytype##_decoder_functions[] = { \
  231. { OSSL_FUNC_DECODER_NEWCTX, \
  232. (void (*)(void))msblob2##keytype##_newctx }, \
  233. { OSSL_FUNC_DECODER_FREECTX, \
  234. (void (*)(void))msblob2key_freectx }, \
  235. { OSSL_FUNC_DECODER_DOES_SELECTION, \
  236. (void (*)(void))msblob2key_does_selection }, \
  237. { OSSL_FUNC_DECODER_DECODE, \
  238. (void (*)(void))msblob2key_decode }, \
  239. { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
  240. (void (*)(void))msblob2key_export_object }, \
  241. OSSL_DISPATCH_END \
  242. }
  243. #ifndef OPENSSL_NO_DSA
  244. IMPLEMENT_MSBLOB(DSA, dsa);
  245. #endif
  246. IMPLEMENT_MSBLOB(RSA, rsa);