winstore_store.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright 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. #include <openssl/store.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/core_object.h>
  13. #include <openssl/bio.h>
  14. #include <openssl/err.h>
  15. #include <openssl/params.h>
  16. #include <openssl/decoder.h>
  17. #include <openssl/proverr.h>
  18. #include <openssl/store.h> /* The OSSL_STORE_INFO type numbers */
  19. #include "internal/cryptlib.h"
  20. #include "internal/o_dir.h"
  21. #include "crypto/decoder.h"
  22. #include "crypto/ctype.h" /* ossl_isdigit() */
  23. #include "prov/implementations.h"
  24. #include "prov/bio.h"
  25. #include "file_store_local.h"
  26. #include <wincrypt.h>
  27. enum {
  28. STATE_IDLE,
  29. STATE_READ,
  30. STATE_EOF,
  31. };
  32. struct winstore_ctx_st {
  33. void *provctx;
  34. char *propq;
  35. unsigned char *subject;
  36. size_t subject_len;
  37. HCERTSTORE win_store;
  38. const CERT_CONTEXT *win_ctx;
  39. int state;
  40. OSSL_DECODER_CTX *dctx;
  41. };
  42. static void winstore_win_reset(struct winstore_ctx_st *ctx)
  43. {
  44. if (ctx->win_ctx != NULL) {
  45. CertFreeCertificateContext(ctx->win_ctx);
  46. ctx->win_ctx = NULL;
  47. }
  48. ctx->state = STATE_IDLE;
  49. }
  50. static void winstore_win_advance(struct winstore_ctx_st *ctx)
  51. {
  52. CERT_NAME_BLOB name = {0};
  53. if (ctx->state == STATE_EOF)
  54. return;
  55. name.cbData = ctx->subject_len;
  56. name.pbData = ctx->subject;
  57. ctx->win_ctx = (name.cbData == 0 ? NULL :
  58. CertFindCertificateInStore(ctx->win_store,
  59. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  60. 0, CERT_FIND_SUBJECT_NAME,
  61. &name, ctx->win_ctx));
  62. ctx->state = (ctx->win_ctx == NULL) ? STATE_EOF : STATE_READ;
  63. }
  64. static void *winstore_open(void *provctx, const char *uri)
  65. {
  66. struct winstore_ctx_st *ctx = NULL;
  67. if (!HAS_CASE_PREFIX(uri, "org.openssl.winstore:"))
  68. return NULL;
  69. ctx = OPENSSL_zalloc(sizeof(*ctx));
  70. if (ctx == NULL)
  71. return NULL;
  72. ctx->provctx = provctx;
  73. ctx->win_store = CertOpenSystemStoreW(0, L"ROOT");
  74. if (ctx->win_store == NULL) {
  75. OPENSSL_free(ctx);
  76. return NULL;
  77. }
  78. winstore_win_reset(ctx);
  79. return ctx;
  80. }
  81. static void *winstore_attach(void *provctx, OSSL_CORE_BIO *cin)
  82. {
  83. return NULL; /* not supported */
  84. }
  85. static const OSSL_PARAM *winstore_settable_ctx_params(void *loaderctx, const OSSL_PARAM params[])
  86. {
  87. static const OSSL_PARAM known_settable_ctx_params[] = {
  88. OSSL_PARAM_octet_string(OSSL_STORE_PARAM_SUBJECT, NULL, 0),
  89. OSSL_PARAM_utf8_string(OSSL_STORE_PARAM_PROPERTIES, NULL, 0),
  90. OSSL_PARAM_END
  91. };
  92. return known_settable_ctx_params;
  93. }
  94. static int winstore_set_ctx_params(void *loaderctx, const OSSL_PARAM params[])
  95. {
  96. struct winstore_ctx_st *ctx = loaderctx;
  97. const OSSL_PARAM *p;
  98. int do_reset = 0;
  99. if (params == NULL)
  100. return 1;
  101. p = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_PROPERTIES);
  102. if (p != NULL) {
  103. do_reset = 1;
  104. OPENSSL_free(ctx->propq);
  105. ctx->propq = NULL;
  106. if (!OSSL_PARAM_get_utf8_string(p, &ctx->propq, 0))
  107. return 0;
  108. }
  109. p = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SUBJECT);
  110. if (p != NULL) {
  111. const unsigned char *der = NULL;
  112. size_t der_len = 0;
  113. if (!OSSL_PARAM_get_octet_string_ptr(p, (const void **)&der, &der_len))
  114. return 0;
  115. do_reset = 1;
  116. OPENSSL_free(ctx->subject);
  117. ctx->subject = OPENSSL_malloc(der_len);
  118. if (ctx->subject == NULL) {
  119. ctx->subject_len = 0;
  120. return 0;
  121. }
  122. ctx->subject_len = der_len;
  123. memcpy(ctx->subject, der, der_len);
  124. }
  125. if (do_reset) {
  126. winstore_win_reset(ctx);
  127. winstore_win_advance(ctx);
  128. }
  129. return 1;
  130. }
  131. struct load_data_st {
  132. OSSL_CALLBACK *object_cb;
  133. void *object_cbarg;
  134. };
  135. static int load_construct(OSSL_DECODER_INSTANCE *decoder_inst,
  136. const OSSL_PARAM *params, void *construct_data)
  137. {
  138. struct load_data_st *data = construct_data;
  139. return data->object_cb(params, data->object_cbarg);
  140. }
  141. static void load_cleanup(void *construct_data)
  142. {
  143. /* No-op. */
  144. }
  145. static int setup_decoder(struct winstore_ctx_st *ctx)
  146. {
  147. OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(ctx->provctx);
  148. const OSSL_ALGORITHM *to_algo = NULL;
  149. if (ctx->dctx != NULL)
  150. return 1;
  151. ctx->dctx = OSSL_DECODER_CTX_new();
  152. if (ctx->dctx == NULL) {
  153. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  154. return 0;
  155. }
  156. if (!OSSL_DECODER_CTX_set_input_type(ctx->dctx, "DER")) {
  157. ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
  158. goto err;
  159. }
  160. if (!OSSL_DECODER_CTX_set_input_structure(ctx->dctx, "Certificate")) {
  161. ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
  162. goto err;
  163. }
  164. for (to_algo = ossl_any_to_obj_algorithm;
  165. to_algo->algorithm_names != NULL;
  166. to_algo++) {
  167. OSSL_DECODER *to_obj = NULL;
  168. OSSL_DECODER_INSTANCE *to_obj_inst = NULL;
  169. /*
  170. * Create the internal last resort decoder implementation
  171. * together with a "decoder instance".
  172. * The decoder doesn't need any identification or to be
  173. * attached to any provider, since it's only used locally.
  174. */
  175. to_obj = ossl_decoder_from_algorithm(0, to_algo, NULL);
  176. if (to_obj != NULL)
  177. to_obj_inst = ossl_decoder_instance_new(to_obj, ctx->provctx);
  178. OSSL_DECODER_free(to_obj);
  179. if (to_obj_inst == NULL)
  180. goto err;
  181. if (!ossl_decoder_ctx_add_decoder_inst(ctx->dctx,
  182. to_obj_inst)) {
  183. ossl_decoder_instance_free(to_obj_inst);
  184. ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
  185. goto err;
  186. }
  187. }
  188. if (!OSSL_DECODER_CTX_add_extra(ctx->dctx, libctx, ctx->propq)) {
  189. ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
  190. goto err;
  191. }
  192. if (!OSSL_DECODER_CTX_set_construct(ctx->dctx, load_construct)) {
  193. ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
  194. goto err;
  195. }
  196. if (!OSSL_DECODER_CTX_set_cleanup(ctx->dctx, load_cleanup)) {
  197. ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
  198. goto err;
  199. }
  200. return 1;
  201. err:
  202. OSSL_DECODER_CTX_free(ctx->dctx);
  203. ctx->dctx = NULL;
  204. return 0;
  205. }
  206. static int winstore_load_using(struct winstore_ctx_st *ctx,
  207. OSSL_CALLBACK *object_cb, void *object_cbarg,
  208. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg,
  209. const void *der, size_t der_len)
  210. {
  211. struct load_data_st data;
  212. const unsigned char *der_ = der;
  213. size_t der_len_ = der_len;
  214. if (setup_decoder(ctx) == 0)
  215. return 0;
  216. data.object_cb = object_cb;
  217. data.object_cbarg = object_cbarg;
  218. OSSL_DECODER_CTX_set_construct_data(ctx->dctx, &data);
  219. OSSL_DECODER_CTX_set_passphrase_cb(ctx->dctx, pw_cb, pw_cbarg);
  220. if (OSSL_DECODER_from_data(ctx->dctx, &der_, &der_len_) == 0)
  221. return 0;
  222. return 1;
  223. }
  224. static int winstore_load(void *loaderctx,
  225. OSSL_CALLBACK *object_cb, void *object_cbarg,
  226. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  227. {
  228. int ret = 0;
  229. struct winstore_ctx_st *ctx = loaderctx;
  230. if (ctx->state != STATE_READ)
  231. return 0;
  232. ret = winstore_load_using(ctx, object_cb, object_cbarg, pw_cb, pw_cbarg,
  233. ctx->win_ctx->pbCertEncoded,
  234. ctx->win_ctx->cbCertEncoded);
  235. if (ret == 1)
  236. winstore_win_advance(ctx);
  237. return ret;
  238. }
  239. static int winstore_eof(void *loaderctx)
  240. {
  241. struct winstore_ctx_st *ctx = loaderctx;
  242. return ctx->state != STATE_READ;
  243. }
  244. static int winstore_close(void *loaderctx)
  245. {
  246. struct winstore_ctx_st *ctx = loaderctx;
  247. winstore_win_reset(ctx);
  248. CertCloseStore(ctx->win_store, 0);
  249. OSSL_DECODER_CTX_free(ctx->dctx);
  250. OPENSSL_free(ctx->propq);
  251. OPENSSL_free(ctx->subject);
  252. OPENSSL_free(ctx);
  253. return 1;
  254. }
  255. const OSSL_DISPATCH ossl_winstore_store_functions[] = {
  256. { OSSL_FUNC_STORE_OPEN, (void (*)(void))winstore_open },
  257. { OSSL_FUNC_STORE_ATTACH, (void (*)(void))winstore_attach },
  258. { OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS, (void (*)(void))winstore_settable_ctx_params },
  259. { OSSL_FUNC_STORE_SET_CTX_PARAMS, (void (*)(void))winstore_set_ctx_params },
  260. { OSSL_FUNC_STORE_LOAD, (void (*)(void))winstore_load },
  261. { OSSL_FUNC_STORE_EOF, (void (*)(void))winstore_eof },
  262. { OSSL_FUNC_STORE_CLOSE, (void (*)(void))winstore_close },
  263. { 0, NULL },
  264. };