store_register.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "internal/ctype.h"
  11. #include <assert.h>
  12. #include <openssl/err.h>
  13. #include <openssl/lhash.h>
  14. #include "store_locl.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. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW,
  36. OSSL_STORE_R_INVALID_SCHEME);
  37. return NULL;
  38. }
  39. if ((res = OPENSSL_zalloc(sizeof(*res))) == NULL) {
  40. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW, ERR_R_MALLOC_FAILURE);
  41. return NULL;
  42. }
  43. res->engine = e;
  44. res->scheme = scheme;
  45. return res;
  46. }
  47. const ENGINE *OSSL_STORE_LOADER_get0_engine(const OSSL_STORE_LOADER *loader)
  48. {
  49. return loader->engine;
  50. }
  51. const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader)
  52. {
  53. return loader->scheme;
  54. }
  55. int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
  56. OSSL_STORE_open_fn open_function)
  57. {
  58. loader->open = open_function;
  59. return 1;
  60. }
  61. int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
  62. OSSL_STORE_ctrl_fn ctrl_function)
  63. {
  64. loader->ctrl = ctrl_function;
  65. return 1;
  66. }
  67. int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER *loader,
  68. OSSL_STORE_expect_fn expect_function)
  69. {
  70. loader->expect = expect_function;
  71. return 1;
  72. }
  73. int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER *loader,
  74. OSSL_STORE_find_fn find_function)
  75. {
  76. loader->find = find_function;
  77. return 1;
  78. }
  79. int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
  80. OSSL_STORE_load_fn load_function)
  81. {
  82. loader->load = load_function;
  83. return 1;
  84. }
  85. int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
  86. OSSL_STORE_eof_fn eof_function)
  87. {
  88. loader->eof = eof_function;
  89. return 1;
  90. }
  91. int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
  92. OSSL_STORE_error_fn error_function)
  93. {
  94. loader->error = error_function;
  95. return 1;
  96. }
  97. int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
  98. OSSL_STORE_close_fn close_function)
  99. {
  100. loader->close = close_function;
  101. return 1;
  102. }
  103. void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader)
  104. {
  105. OPENSSL_free(loader);
  106. }
  107. /*
  108. * Functions for registering OSSL_STORE_LOADERs
  109. */
  110. static unsigned long store_loader_hash(const OSSL_STORE_LOADER *v)
  111. {
  112. return OPENSSL_LH_strhash(v->scheme);
  113. }
  114. static int store_loader_cmp(const OSSL_STORE_LOADER *a,
  115. const OSSL_STORE_LOADER *b)
  116. {
  117. assert(a->scheme != NULL && b->scheme != NULL);
  118. return strcmp(a->scheme, b->scheme);
  119. }
  120. static LHASH_OF(OSSL_STORE_LOADER) *loader_register = NULL;
  121. int ossl_store_register_loader_int(OSSL_STORE_LOADER *loader)
  122. {
  123. const char *scheme = loader->scheme;
  124. int ok = 0;
  125. /*
  126. * Check that the given scheme conforms to correct scheme syntax as per
  127. * RFC 3986:
  128. *
  129. * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  130. */
  131. if (ossl_isalpha(*scheme))
  132. while (*scheme != '\0'
  133. && (ossl_isalpha(*scheme)
  134. || ossl_isdigit(*scheme)
  135. || strchr("+-.", *scheme) != NULL))
  136. scheme++;
  137. if (*scheme != '\0') {
  138. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
  139. OSSL_STORE_R_INVALID_SCHEME);
  140. ERR_add_error_data(2, "scheme=", loader->scheme);
  141. return 0;
  142. }
  143. /* Check that functions we absolutely require are present */
  144. if (loader->open == NULL || loader->load == NULL || loader->eof == NULL
  145. || loader->error == NULL || loader->close == NULL) {
  146. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
  147. OSSL_STORE_R_LOADER_INCOMPLETE);
  148. return 0;
  149. }
  150. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  151. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
  152. ERR_R_MALLOC_FAILURE);
  153. return 0;
  154. }
  155. CRYPTO_THREAD_write_lock(registry_lock);
  156. if (loader_register == NULL) {
  157. loader_register = lh_OSSL_STORE_LOADER_new(store_loader_hash,
  158. store_loader_cmp);
  159. }
  160. if (loader_register != NULL
  161. && (lh_OSSL_STORE_LOADER_insert(loader_register, loader) != NULL
  162. || lh_OSSL_STORE_LOADER_error(loader_register) == 0))
  163. ok = 1;
  164. CRYPTO_THREAD_unlock(registry_lock);
  165. return ok;
  166. }
  167. int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader)
  168. {
  169. if (!ossl_store_init_once())
  170. return 0;
  171. return ossl_store_register_loader_int(loader);
  172. }
  173. const OSSL_STORE_LOADER *ossl_store_get0_loader_int(const char *scheme)
  174. {
  175. OSSL_STORE_LOADER template;
  176. OSSL_STORE_LOADER *loader = NULL;
  177. template.scheme = scheme;
  178. template.open = NULL;
  179. template.load = NULL;
  180. template.eof = NULL;
  181. template.close = NULL;
  182. if (!ossl_store_init_once())
  183. return NULL;
  184. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  185. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
  186. ERR_R_MALLOC_FAILURE);
  187. return NULL;
  188. }
  189. CRYPTO_THREAD_write_lock(registry_lock);
  190. loader = lh_OSSL_STORE_LOADER_retrieve(loader_register, &template);
  191. if (loader == NULL) {
  192. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
  193. OSSL_STORE_R_UNREGISTERED_SCHEME);
  194. ERR_add_error_data(2, "scheme=", scheme);
  195. }
  196. CRYPTO_THREAD_unlock(registry_lock);
  197. return loader;
  198. }
  199. OSSL_STORE_LOADER *ossl_store_unregister_loader_int(const char *scheme)
  200. {
  201. OSSL_STORE_LOADER template;
  202. OSSL_STORE_LOADER *loader = NULL;
  203. template.scheme = scheme;
  204. template.open = NULL;
  205. template.load = NULL;
  206. template.eof = NULL;
  207. template.close = NULL;
  208. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  209. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
  210. ERR_R_MALLOC_FAILURE);
  211. return NULL;
  212. }
  213. CRYPTO_THREAD_write_lock(registry_lock);
  214. loader = lh_OSSL_STORE_LOADER_delete(loader_register, &template);
  215. if (loader == NULL) {
  216. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
  217. OSSL_STORE_R_UNREGISTERED_SCHEME);
  218. ERR_add_error_data(2, "scheme=", scheme);
  219. }
  220. CRYPTO_THREAD_unlock(registry_lock);
  221. return loader;
  222. }
  223. OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme)
  224. {
  225. if (!ossl_store_init_once())
  226. return 0;
  227. return ossl_store_unregister_loader_int(scheme);
  228. }
  229. void ossl_store_destroy_loaders_int(void)
  230. {
  231. assert(lh_OSSL_STORE_LOADER_num_items(loader_register) == 0);
  232. lh_OSSL_STORE_LOADER_free(loader_register);
  233. loader_register = NULL;
  234. CRYPTO_THREAD_lock_free(registry_lock);
  235. registry_lock = NULL;
  236. }
  237. /*
  238. * Functions to list OSSL_STORE loaders
  239. */
  240. IMPLEMENT_LHASH_DOALL_ARG_CONST(OSSL_STORE_LOADER, void);
  241. int OSSL_STORE_do_all_loaders(void (*do_function) (const OSSL_STORE_LOADER
  242. *loader, void *do_arg),
  243. void *do_arg)
  244. {
  245. lh_OSSL_STORE_LOADER_doall_void(loader_register, do_function, do_arg);
  246. return 1;
  247. }