winstore_store.c 9.0 KB

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