store_register.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright 2016-2020 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. 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_attach(OSSL_STORE_LOADER *loader,
  62. OSSL_STORE_attach_fn attach_function)
  63. {
  64. loader->attach = attach_function;
  65. return 1;
  66. }
  67. int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
  68. OSSL_STORE_ctrl_fn ctrl_function)
  69. {
  70. loader->ctrl = ctrl_function;
  71. return 1;
  72. }
  73. int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER *loader,
  74. OSSL_STORE_expect_fn expect_function)
  75. {
  76. loader->expect = expect_function;
  77. return 1;
  78. }
  79. int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER *loader,
  80. OSSL_STORE_find_fn find_function)
  81. {
  82. loader->find = find_function;
  83. return 1;
  84. }
  85. int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
  86. OSSL_STORE_load_fn load_function)
  87. {
  88. loader->load = load_function;
  89. return 1;
  90. }
  91. int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
  92. OSSL_STORE_eof_fn eof_function)
  93. {
  94. loader->eof = eof_function;
  95. return 1;
  96. }
  97. int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
  98. OSSL_STORE_error_fn error_function)
  99. {
  100. loader->error = error_function;
  101. return 1;
  102. }
  103. int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
  104. OSSL_STORE_close_fn close_function)
  105. {
  106. loader->close = close_function;
  107. return 1;
  108. }
  109. void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader)
  110. {
  111. OPENSSL_free(loader);
  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. int ossl_store_register_loader_int(OSSL_STORE_LOADER *loader)
  128. {
  129. const char *scheme = loader->scheme;
  130. int ok = 0;
  131. /*
  132. * Check that the given scheme conforms to correct scheme syntax as per
  133. * RFC 3986:
  134. *
  135. * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  136. */
  137. if (ossl_isalpha(*scheme))
  138. while (*scheme != '\0'
  139. && (ossl_isalpha(*scheme)
  140. || ossl_isdigit(*scheme)
  141. || strchr("+-.", *scheme) != NULL))
  142. scheme++;
  143. if (*scheme != '\0') {
  144. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
  145. OSSL_STORE_R_INVALID_SCHEME);
  146. ERR_add_error_data(2, "scheme=", loader->scheme);
  147. return 0;
  148. }
  149. /* Check that functions we absolutely require are present */
  150. if (loader->open == NULL || loader->load == NULL || loader->eof == NULL
  151. || loader->error == NULL || loader->close == NULL) {
  152. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
  153. OSSL_STORE_R_LOADER_INCOMPLETE);
  154. return 0;
  155. }
  156. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  157. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
  158. ERR_R_MALLOC_FAILURE);
  159. return 0;
  160. }
  161. CRYPTO_THREAD_write_lock(registry_lock);
  162. if (loader_register == NULL) {
  163. loader_register = lh_OSSL_STORE_LOADER_new(store_loader_hash,
  164. store_loader_cmp);
  165. }
  166. if (loader_register != NULL
  167. && (lh_OSSL_STORE_LOADER_insert(loader_register, loader) != NULL
  168. || lh_OSSL_STORE_LOADER_error(loader_register) == 0))
  169. ok = 1;
  170. CRYPTO_THREAD_unlock(registry_lock);
  171. return ok;
  172. }
  173. int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader)
  174. {
  175. if (!ossl_store_init_once())
  176. return 0;
  177. return ossl_store_register_loader_int(loader);
  178. }
  179. const OSSL_STORE_LOADER *ossl_store_get0_loader_int(const char *scheme)
  180. {
  181. OSSL_STORE_LOADER template;
  182. OSSL_STORE_LOADER *loader = NULL;
  183. template.scheme = scheme;
  184. template.open = NULL;
  185. template.load = NULL;
  186. template.eof = NULL;
  187. template.close = NULL;
  188. if (!ossl_store_init_once())
  189. return NULL;
  190. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  191. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
  192. ERR_R_MALLOC_FAILURE);
  193. return NULL;
  194. }
  195. CRYPTO_THREAD_write_lock(registry_lock);
  196. loader = lh_OSSL_STORE_LOADER_retrieve(loader_register, &template);
  197. if (loader == NULL) {
  198. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
  199. OSSL_STORE_R_UNREGISTERED_SCHEME);
  200. ERR_add_error_data(2, "scheme=", scheme);
  201. }
  202. CRYPTO_THREAD_unlock(registry_lock);
  203. return loader;
  204. }
  205. OSSL_STORE_LOADER *ossl_store_unregister_loader_int(const char *scheme)
  206. {
  207. OSSL_STORE_LOADER template;
  208. OSSL_STORE_LOADER *loader = NULL;
  209. template.scheme = scheme;
  210. template.open = NULL;
  211. template.load = NULL;
  212. template.eof = NULL;
  213. template.close = NULL;
  214. if (!RUN_ONCE(&registry_init, do_registry_init)) {
  215. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
  216. ERR_R_MALLOC_FAILURE);
  217. return NULL;
  218. }
  219. CRYPTO_THREAD_write_lock(registry_lock);
  220. loader = lh_OSSL_STORE_LOADER_delete(loader_register, &template);
  221. if (loader == NULL) {
  222. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
  223. OSSL_STORE_R_UNREGISTERED_SCHEME);
  224. ERR_add_error_data(2, "scheme=", scheme);
  225. }
  226. CRYPTO_THREAD_unlock(registry_lock);
  227. return loader;
  228. }
  229. OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme)
  230. {
  231. if (!ossl_store_init_once())
  232. return 0;
  233. return ossl_store_unregister_loader_int(scheme);
  234. }
  235. void ossl_store_destroy_loaders_int(void)
  236. {
  237. assert(lh_OSSL_STORE_LOADER_num_items(loader_register) == 0);
  238. lh_OSSL_STORE_LOADER_free(loader_register);
  239. loader_register = NULL;
  240. CRYPTO_THREAD_lock_free(registry_lock);
  241. registry_lock = NULL;
  242. }
  243. /*
  244. * Functions to list OSSL_STORE loaders
  245. */
  246. IMPLEMENT_LHASH_DOALL_ARG_CONST(OSSL_STORE_LOADER, void);
  247. int OSSL_STORE_do_all_loaders(void (*do_function) (const OSSL_STORE_LOADER
  248. *loader, void *do_arg),
  249. void *do_arg)
  250. {
  251. lh_OSSL_STORE_LOADER_doall_void(loader_register, do_function, do_arg);
  252. return 1;
  253. }