2
0

store_register.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. return NULL;
  40. res->engine = e;
  41. res->scheme = scheme;
  42. return res;
  43. }
  44. const ENGINE *OSSL_STORE_LOADER_get0_engine(const OSSL_STORE_LOADER *loader)
  45. {
  46. return loader->engine;
  47. }
  48. const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader)
  49. {
  50. return loader->scheme;
  51. }
  52. int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
  53. OSSL_STORE_open_fn open_function)
  54. {
  55. loader->open = open_function;
  56. return 1;
  57. }
  58. int OSSL_STORE_LOADER_set_open_ex
  59. (OSSL_STORE_LOADER *loader,
  60. OSSL_STORE_open_ex_fn open_ex_function)
  61. {
  62. loader->open_ex = open_ex_function;
  63. return 1;
  64. }
  65. int OSSL_STORE_LOADER_set_attach(OSSL_STORE_LOADER *loader,
  66. OSSL_STORE_attach_fn attach_function)
  67. {
  68. loader->attach = attach_function;
  69. return 1;
  70. }
  71. int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
  72. OSSL_STORE_ctrl_fn ctrl_function)
  73. {
  74. loader->ctrl = ctrl_function;
  75. return 1;
  76. }
  77. int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER *loader,
  78. OSSL_STORE_expect_fn expect_function)
  79. {
  80. loader->expect = expect_function;
  81. return 1;
  82. }
  83. int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER *loader,
  84. OSSL_STORE_find_fn find_function)
  85. {
  86. loader->find = find_function;
  87. return 1;
  88. }
  89. int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
  90. OSSL_STORE_load_fn load_function)
  91. {
  92. loader->load = load_function;
  93. return 1;
  94. }
  95. int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
  96. OSSL_STORE_eof_fn eof_function)
  97. {
  98. loader->eof = eof_function;
  99. return 1;
  100. }
  101. int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
  102. OSSL_STORE_error_fn error_function)
  103. {
  104. loader->error = error_function;
  105. return 1;
  106. }
  107. int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
  108. OSSL_STORE_close_fn close_function)
  109. {
  110. loader->closefn = close_function;
  111. return 1;
  112. }
  113. /*
  114. * Functions for registering OSSL_STORE_LOADERs
  115. */
  116. static unsigned long store_loader_hash(const OSSL_STORE_LOADER *v)
  117. {
  118. return OPENSSL_LH_strhash(v->scheme);
  119. }
  120. static int store_loader_cmp(const OSSL_STORE_LOADER *a,
  121. const OSSL_STORE_LOADER *b)
  122. {
  123. assert(a->scheme != NULL && b->scheme != NULL);
  124. return strcmp(a->scheme, b->scheme);
  125. }
  126. static LHASH_OF(OSSL_STORE_LOADER) *loader_register = NULL;
  127. static int ossl_store_register_init(void)
  128. {
  129. if (loader_register == NULL) {
  130. loader_register = lh_OSSL_STORE_LOADER_new(store_loader_hash,
  131. store_loader_cmp);
  132. }
  133. return loader_register != NULL;
  134. }
  135. int ossl_store_register_loader_int(OSSL_STORE_LOADER *loader)
  136. {
  137. const char *scheme = loader->scheme;
  138. int ok = 0;
  139. /*
  140. * Check that the given scheme conforms to correct scheme syntax as per
  141. * RFC 3986:
  142. *
  143. * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  144. */
  145. if (ossl_isalpha(*scheme))
  146. while (*scheme != '\0'
  147. && (ossl_isalpha(*scheme)
  148. || ossl_isdigit(*scheme)
  149. || strchr("+-.", *scheme) != NULL))
  150. scheme++;
  151. if (*scheme != '\0') {
  152. ERR_raise_data(ERR_LIB_OSSL_STORE, OSSL_STORE_R_INVALID_SCHEME,
  153. "scheme=%s", loader->scheme);
  154. return 0;
  155. }
  156. /* Check that functions we absolutely require are present */
  157. if (loader->open == NULL || loader->load == NULL || loader->eof == NULL
  158. || loader->error == NULL || loader->closefn == NULL) {
  159. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADER_INCOMPLETE);
  160. return 0;
  161. }
  162. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  163. /* Should this error be raised in do_registry_init()? */
  164. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
  165. return 0;
  166. }
  167. if (!CRYPTO_THREAD_write_lock(registry_lock))
  168. return 0;
  169. if (ossl_store_register_init()
  170. && (lh_OSSL_STORE_LOADER_insert(loader_register, loader) != NULL
  171. || lh_OSSL_STORE_LOADER_error(loader_register) == 0))
  172. ok = 1;
  173. CRYPTO_THREAD_unlock(registry_lock);
  174. return ok;
  175. }
  176. int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader)
  177. {
  178. return ossl_store_register_loader_int(loader);
  179. }
  180. const OSSL_STORE_LOADER *ossl_store_get0_loader_int(const char *scheme)
  181. {
  182. OSSL_STORE_LOADER template;
  183. OSSL_STORE_LOADER *loader = NULL;
  184. template.scheme = scheme;
  185. template.open = NULL;
  186. template.load = NULL;
  187. template.eof = NULL;
  188. template.closefn = NULL;
  189. template.open_ex = NULL;
  190. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  191. /* Should this error be raised in do_registry_init()? */
  192. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
  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.closefn = NULL;
  215. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  216. /* Should this error be raised in do_registry_init()? */
  217. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
  218. return NULL;
  219. }
  220. if (!CRYPTO_THREAD_write_lock(registry_lock))
  221. return NULL;
  222. if (!ossl_store_register_init())
  223. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_INTERNAL_ERROR);
  224. else if ((loader = lh_OSSL_STORE_LOADER_delete(loader_register,
  225. &template)) == NULL)
  226. ERR_raise_data(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNREGISTERED_SCHEME,
  227. "scheme=%s", scheme);
  228. CRYPTO_THREAD_unlock(registry_lock);
  229. return loader;
  230. }
  231. OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme)
  232. {
  233. return ossl_store_unregister_loader_int(scheme);
  234. }
  235. void ossl_store_destroy_loaders_int(void)
  236. {
  237. lh_OSSL_STORE_LOADER_free(loader_register);
  238. loader_register = NULL;
  239. CRYPTO_THREAD_lock_free(registry_lock);
  240. registry_lock = NULL;
  241. }
  242. /*
  243. * Functions to list OSSL_STORE loaders
  244. */
  245. IMPLEMENT_LHASH_DOALL_ARG_CONST(OSSL_STORE_LOADER, void);
  246. int OSSL_STORE_do_all_loaders(void (*do_function) (const OSSL_STORE_LOADER
  247. *loader, void *do_arg),
  248. void *do_arg)
  249. {
  250. if (ossl_store_register_init())
  251. lh_OSSL_STORE_LOADER_doall_void(loader_register, do_function, do_arg);
  252. return 1;
  253. }