decode_msblob2key.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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
  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_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
  71. OSSL_CALLBACK *data_cb, void *data_cbarg,
  72. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  73. {
  74. struct msblob2key_ctx_st *ctx = vctx;
  75. BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
  76. const unsigned char *p;
  77. unsigned char hdr_buf[16], *buf = NULL;
  78. unsigned int bitlen, magic, length;
  79. int isdss = -1;
  80. int ispub = -1;
  81. void *key = NULL;
  82. int ok = 0;
  83. if (in == NULL)
  84. return 0;
  85. if (BIO_read(in, hdr_buf, 16) != 16) {
  86. ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
  87. goto next;
  88. }
  89. ERR_set_mark();
  90. p = hdr_buf;
  91. ok = ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) > 0;
  92. ERR_pop_to_mark();
  93. if (!ok)
  94. goto next;
  95. ctx->selection = selection;
  96. ok = 0; /* Assume that we fail */
  97. if ((isdss && ctx->desc->type != EVP_PKEY_DSA)
  98. || (!isdss && ctx->desc->type != EVP_PKEY_RSA))
  99. goto next;
  100. length = ossl_blob_length(bitlen, isdss, ispub);
  101. if (length > BLOB_MAX_LENGTH) {
  102. ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
  103. goto next;
  104. }
  105. buf = OPENSSL_malloc(length);
  106. if (buf == NULL) {
  107. ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
  108. goto end;
  109. }
  110. p = buf;
  111. if (BIO_read(in, buf, length) != (int)length) {
  112. ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
  113. goto next;
  114. }
  115. if ((selection == 0
  116. || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  117. && !ispub
  118. && ctx->desc->read_private_key != NULL) {
  119. struct ossl_passphrase_data_st pwdata;
  120. memset(&pwdata, 0, sizeof(pwdata));
  121. if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
  122. goto end;
  123. p = buf;
  124. key = ctx->desc->read_private_key(&p, bitlen, ispub);
  125. if (selection != 0 && key == NULL)
  126. goto next;
  127. }
  128. if (key == NULL && (selection == 0
  129. || (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  130. && ispub
  131. && ctx->desc->read_public_key != NULL) {
  132. p = buf;
  133. key = ctx->desc->read_public_key(&p, bitlen, ispub);
  134. if (selection != 0 && key == NULL)
  135. goto next;
  136. }
  137. if (key != NULL && ctx->desc->adjust_key != NULL)
  138. ctx->desc->adjust_key(key, ctx);
  139. next:
  140. /*
  141. * Indicated that we successfully decoded something, or not at all.
  142. * Ending up "empty handed" is not an error.
  143. */
  144. ok = 1;
  145. /*
  146. * We free resources here so it's not held up during the callback, because
  147. * we know the process is recursive and the allocated chunks of memory
  148. * add up.
  149. */
  150. OPENSSL_free(buf);
  151. BIO_free(in);
  152. buf = NULL;
  153. in = NULL;
  154. if (key != NULL) {
  155. OSSL_PARAM params[4];
  156. int object_type = OSSL_OBJECT_PKEY;
  157. params[0] =
  158. OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
  159. params[1] =
  160. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  161. (char *)ctx->desc->name, 0);
  162. /* The address of the key becomes the octet string */
  163. params[2] =
  164. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
  165. &key, sizeof(key));
  166. params[3] = OSSL_PARAM_construct_end();
  167. ok = data_cb(params, data_cbarg);
  168. }
  169. end:
  170. BIO_free(in);
  171. OPENSSL_free(buf);
  172. ctx->desc->free_key(key);
  173. return ok;
  174. }
  175. static int
  176. msblob2key_export_object(void *vctx,
  177. const void *reference, size_t reference_sz,
  178. OSSL_CALLBACK *export_cb, void *export_cbarg)
  179. {
  180. struct msblob2key_ctx_st *ctx = vctx;
  181. OSSL_FUNC_keymgmt_export_fn *export =
  182. ossl_prov_get_keymgmt_export(ctx->desc->fns);
  183. void *keydata;
  184. if (reference_sz == sizeof(keydata) && export != NULL) {
  185. /* The contents of the reference is the address to our object */
  186. keydata = *(void **)reference;
  187. return export(keydata, ctx->selection, export_cb, export_cbarg);
  188. }
  189. return 0;
  190. }
  191. /* ---------------------------------------------------------------------- */
  192. #define dsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header
  193. #define dsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header
  194. #define dsa_adjust NULL
  195. #define dsa_free (void (*)(void *))DSA_free
  196. /* ---------------------------------------------------------------------- */
  197. #define rsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header
  198. #define rsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header
  199. static void rsa_adjust(void *key, struct msblob2key_ctx_st *ctx)
  200. {
  201. ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
  202. }
  203. #define rsa_free (void (*)(void *))RSA_free
  204. /* ---------------------------------------------------------------------- */
  205. #define IMPLEMENT_MSBLOB(KEYTYPE, keytype) \
  206. static const struct keytype_desc_st mstype##2##keytype##_desc = { \
  207. EVP_PKEY_##KEYTYPE, #KEYTYPE, \
  208. ossl_##keytype##_keymgmt_functions, \
  209. keytype##_decode_private_key, \
  210. keytype##_decode_public_key, \
  211. keytype##_adjust, \
  212. keytype##_free \
  213. }; \
  214. static OSSL_FUNC_decoder_newctx_fn msblob2##keytype##_newctx; \
  215. static void *msblob2##keytype##_newctx(void *provctx) \
  216. { \
  217. return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \
  218. } \
  219. const OSSL_DISPATCH \
  220. ossl_msblob_to_##keytype##_decoder_functions[] = { \
  221. { OSSL_FUNC_DECODER_NEWCTX, \
  222. (void (*)(void))msblob2##keytype##_newctx }, \
  223. { OSSL_FUNC_DECODER_FREECTX, \
  224. (void (*)(void))msblob2key_freectx }, \
  225. { OSSL_FUNC_DECODER_DECODE, \
  226. (void (*)(void))msblob2key_decode }, \
  227. { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
  228. (void (*)(void))msblob2key_export_object }, \
  229. { 0, NULL } \
  230. }
  231. #ifndef OPENSSL_NO_DSA
  232. IMPLEMENT_MSBLOB(DSA, dsa);
  233. #endif
  234. IMPLEMENT_MSBLOB(RSA, rsa);