store_register.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright 2016-2021 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 <string.h>
  10. #include "crypto/ctype.h"
  11. #include <assert.h>
  12. #include <openssl/err.h>
  13. #include <openssl/lhash.h>
  14. #include "store_local.h"
  15. static CRYPTO_RWLOCK *registry_lock;
  16. static CRYPTO_ONCE registry_init = CRYPTO_ONCE_STATIC_INIT;
  17. DEFINE_RUN_ONCE_STATIC(do_registry_init)
  18. {
  19. registry_lock = CRYPTO_THREAD_lock_new();
  20. return registry_lock != NULL;
  21. }
  22. /*
  23. * Functions for manipulating OSSL_STORE_LOADERs
  24. */
  25. OSSL_STORE_LOADER *OSSL_STORE_LOADER_new(ENGINE *e, const char *scheme)
  26. {
  27. OSSL_STORE_LOADER *res = NULL;
  28. /*
  29. * We usually don't check NULL arguments. For loaders, though, the
  30. * scheme is crucial and must never be NULL, or the user will get
  31. * mysterious errors when trying to register the created loader
  32. * later on.
  33. */
  34. if (scheme == NULL) {
  35. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_INVALID_SCHEME);
  36. return NULL;
  37. }
  38. if ((res = OPENSSL_zalloc(sizeof(*res))) == NULL) {
  39. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
  40. return NULL;
  41. }
  42. res->engine = e;
  43. res->scheme = scheme;
  44. return res;
  45. }
  46. const ENGINE *OSSL_STORE_LOADER_get0_engine(const OSSL_STORE_LOADER *loader)
  47. {
  48. return loader->engine;
  49. }
  50. const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader)
  51. {
  52. return loader->scheme;
  53. }
  54. int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
  55. OSSL_STORE_open_fn open_function)
  56. {
  57. loader->open = open_function;
  58. return 1;
  59. }
  60. int OSSL_STORE_LOADER_set_open_ex
  61. (OSSL_STORE_LOADER *loader,
  62. OSSL_STORE_open_ex_fn open_ex_function)
  63. {
  64. loader->open_ex = open_ex_function;
  65. return 1;
  66. }
  67. int OSSL_STORE_LOADER_set_attach(OSSL_STORE_LOADER *loader,
  68. OSSL_STORE_attach_fn attach_function)
  69. {
  70. loader->attach = attach_function;
  71. return 1;
  72. }
  73. int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
  74. OSSL_STORE_ctrl_fn ctrl_function)
  75. {
  76. loader->ctrl = ctrl_function;
  77. return 1;
  78. }
  79. int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER *loader,
  80. OSSL_STORE_expect_fn expect_function)
  81. {
  82. loader->expect = expect_function;
  83. return 1;
  84. }
  85. int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER *loader,
  86. OSSL_STORE_find_fn find_function)
  87. {
  88. loader->find = find_function;
  89. return 1;
  90. }
  91. int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
  92. OSSL_STORE_load_fn load_function)
  93. {
  94. loader->load = load_function;
  95. return 1;
  96. }
  97. int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
  98. OSSL_STORE_eof_fn eof_function)
  99. {
  100. loader->eof = eof_function;
  101. return 1;
  102. }
  103. int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
  104. OSSL_STORE_error_fn error_function)
  105. {
  106. loader->error = error_function;
  107. return 1;
  108. }
  109. int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
  110. OSSL_STORE_close_fn close_function)
  111. {
  112. loader->close = close_function;
  113. return 1;
  114. }
  115. /*
  116. * Functions for registering OSSL_STORE_LOADERs
  117. */
  118. static unsigned long store_loader_hash(const OSSL_STORE_LOADER *v)
  119. {
  120. return OPENSSL_LH_strhash(v->scheme);
  121. }
  122. static int store_loader_cmp(const OSSL_STORE_LOADER *a,
  123. const OSSL_STORE_LOADER *b)
  124. {
  125. assert(a->scheme != NULL && b->scheme != NULL);
  126. return strcmp(a->scheme, b->scheme);
  127. }
  128. static LHASH_OF(OSSL_STORE_LOADER) *loader_register = NULL;
  129. static int ossl_store_register_init(void)
  130. {
  131. if (loader_register == NULL) {
  132. loader_register = lh_OSSL_STORE_LOADER_new(store_loader_hash,
  133. store_loader_cmp);
  134. }
  135. return loader_register != NULL;
  136. }
  137. int ossl_store_register_loader_int(OSSL_STORE_LOADER *loader)
  138. {
  139. const char *scheme = loader->scheme;
  140. int ok = 0;
  141. /*
  142. * Check that the given scheme conforms to correct scheme syntax as per
  143. * RFC 3986:
  144. *
  145. * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  146. */
  147. if (ossl_isalpha(*scheme))
  148. while (*scheme != '\0'
  149. && (ossl_isalpha(*scheme)
  150. || ossl_isdigit(*scheme)
  151. || strchr("+-.", *scheme) != NULL))
  152. scheme++;
  153. if (*scheme != '\0') {
  154. ERR_raise_data(ERR_LIB_OSSL_STORE, OSSL_STORE_R_INVALID_SCHEME,
  155. "scheme=%s", loader->scheme);
  156. return 0;
  157. }
  158. /* Check that functions we absolutely require are present */
  159. if (loader->open == NULL || loader->load == NULL || loader->eof == NULL
  160. || loader->error == NULL || loader->close == NULL) {
  161. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADER_INCOMPLETE);
  162. return 0;
  163. }
  164. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  165. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
  166. return 0;
  167. }
  168. if (!CRYPTO_THREAD_write_lock(registry_lock))
  169. return 0;
  170. if (ossl_store_register_init()
  171. && (lh_OSSL_STORE_LOADER_insert(loader_register, loader) != NULL
  172. || lh_OSSL_STORE_LOADER_error(loader_register) == 0))
  173. ok = 1;
  174. CRYPTO_THREAD_unlock(registry_lock);
  175. return ok;
  176. }
  177. int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader)
  178. {
  179. return ossl_store_register_loader_int(loader);
  180. }
  181. const OSSL_STORE_LOADER *ossl_store_get0_loader_int(const char *scheme)
  182. {
  183. OSSL_STORE_LOADER template;
  184. OSSL_STORE_LOADER *loader = NULL;
  185. template.scheme = scheme;
  186. template.open = NULL;
  187. template.load = NULL;
  188. template.eof = NULL;
  189. template.close = NULL;
  190. template.open_ex = NULL;
  191. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  192. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
  193. return NULL;
  194. }
  195. if (!CRYPTO_THREAD_write_lock(registry_lock))
  196. return NULL;
  197. if (!ossl_store_register_init())
  198. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_INTERNAL_ERROR);
  199. else if ((loader = lh_OSSL_STORE_LOADER_retrieve(loader_register,
  200. &template)) == NULL)
  201. ERR_raise_data(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNREGISTERED_SCHEME,
  202. "scheme=%s", scheme);
  203. CRYPTO_THREAD_unlock(registry_lock);
  204. return loader;
  205. }
  206. OSSL_STORE_LOADER *ossl_store_unregister_loader_int(const char *scheme)
  207. {
  208. OSSL_STORE_LOADER template;
  209. OSSL_STORE_LOADER *loader = NULL;
  210. template.scheme = scheme;
  211. template.open = NULL;
  212. template.load = NULL;
  213. template.eof = NULL;
  214. template.close = NULL;
  215. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  216. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_MALLOC_FAILURE);
  217. return NULL;
  218. }
  219. if (!CRYPTO_THREAD_write_lock(registry_lock))
  220. return NULL;
  221. if (!ossl_store_register_init())
  222. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_INTERNAL_ERROR);
  223. else if ((loader = lh_OSSL_STORE_LOADER_delete(loader_register,
  224. &template)) == NULL)
  225. ERR_raise_data(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNREGISTERED_SCHEME,
  226. "scheme=%s", scheme);
  227. CRYPTO_THREAD_unlock(registry_lock);
  228. return loader;
  229. }
  230. OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme)
  231. {
  232. return ossl_store_unregister_loader_int(scheme);
  233. }
  234. void ossl_store_destroy_loaders_int(void)
  235. {
  236. lh_OSSL_STORE_LOADER_free(loader_register);
  237. loader_register = NULL;
  238. CRYPTO_THREAD_lock_free(registry_lock);
  239. registry_lock = NULL;
  240. }
  241. /*
  242. * Functions to list OSSL_STORE loaders
  243. */
  244. IMPLEMENT_LHASH_DOALL_ARG_CONST(OSSL_STORE_LOADER, void);
  245. int OSSL_STORE_do_all_loaders(void (*do_function) (const OSSL_STORE_LOADER
  246. *loader, void *do_arg),
  247. void *do_arg)
  248. {
  249. if (ossl_store_register_init())
  250. lh_OSSL_STORE_LOADER_doall_void(loader_register, do_function, do_arg);
  251. return 1;
  252. }