eng_list.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include "eng_local.h"
  11. /*
  12. * The linked-list of pointers to engine types. engine_list_head incorporates
  13. * an implicit structural reference but engine_list_tail does not - the
  14. * latter is a computational optimization and only points to something that
  15. * is already pointed to by its predecessor in the list (or engine_list_head
  16. * itself). In the same way, the use of the "prev" pointer in each ENGINE is
  17. * to save excessive list iteration, it doesn't correspond to an extra
  18. * structural reference. Hence, engine_list_head, and each non-null "next"
  19. * pointer account for the list itself assuming exactly 1 structural
  20. * reference on each list member.
  21. */
  22. static ENGINE *engine_list_head = NULL;
  23. static ENGINE *engine_list_tail = NULL;
  24. /*
  25. * The linked list of currently loaded dynamic engines.
  26. */
  27. static ENGINE *engine_dyn_list_head = NULL;
  28. static ENGINE *engine_dyn_list_tail = NULL;
  29. /*
  30. * This cleanup function is only needed internally. If it should be called,
  31. * we register it with the "engine_cleanup_int()" stack to be called during
  32. * cleanup.
  33. */
  34. static void engine_list_cleanup(void)
  35. {
  36. ENGINE *iterator = engine_list_head;
  37. while (iterator != NULL) {
  38. ENGINE_remove(iterator);
  39. iterator = engine_list_head;
  40. }
  41. return;
  42. }
  43. /*
  44. * These static functions starting with a lower case "engine_" always take
  45. * place when global_engine_lock has been locked up.
  46. */
  47. static int engine_list_add(ENGINE *e)
  48. {
  49. int conflict = 0;
  50. ENGINE *iterator = NULL;
  51. if (e == NULL) {
  52. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER);
  53. return 0;
  54. }
  55. iterator = engine_list_head;
  56. while (iterator && !conflict) {
  57. conflict = (strcmp(iterator->id, e->id) == 0);
  58. iterator = iterator->next;
  59. }
  60. if (conflict) {
  61. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID);
  62. return 0;
  63. }
  64. if (engine_list_head == NULL) {
  65. /* We are adding to an empty list. */
  66. if (engine_list_tail) {
  67. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
  68. return 0;
  69. }
  70. engine_list_head = e;
  71. e->prev = NULL;
  72. /*
  73. * The first time the list allocates, we should register the cleanup.
  74. */
  75. engine_cleanup_add_last(engine_list_cleanup);
  76. } else {
  77. /* We are adding to the tail of an existing list. */
  78. if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
  79. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
  80. return 0;
  81. }
  82. engine_list_tail->next = e;
  83. e->prev = engine_list_tail;
  84. }
  85. /*
  86. * Having the engine in the list assumes a structural reference.
  87. */
  88. e->struct_ref++;
  89. engine_ref_debug(e, 0, 1);
  90. /* However it came to be, e is the last item in the list. */
  91. engine_list_tail = e;
  92. e->next = NULL;
  93. return 1;
  94. }
  95. static int engine_list_remove(ENGINE *e)
  96. {
  97. ENGINE *iterator;
  98. if (e == NULL) {
  99. ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
  100. return 0;
  101. }
  102. /* We need to check that e is in our linked list! */
  103. iterator = engine_list_head;
  104. while (iterator && (iterator != e))
  105. iterator = iterator->next;
  106. if (iterator == NULL) {
  107. ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
  108. ENGINE_R_ENGINE_IS_NOT_IN_LIST);
  109. return 0;
  110. }
  111. /* un-link e from the chain. */
  112. if (e->next)
  113. e->next->prev = e->prev;
  114. if (e->prev)
  115. e->prev->next = e->next;
  116. /* Correct our head/tail if necessary. */
  117. if (engine_list_head == e)
  118. engine_list_head = e->next;
  119. if (engine_list_tail == e)
  120. engine_list_tail = e->prev;
  121. engine_free_util(e, 0);
  122. return 1;
  123. }
  124. /* Add engine to dynamic engine list. */
  125. int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id,
  126. int not_locked)
  127. {
  128. int result = 0;
  129. ENGINE *iterator = NULL;
  130. if (e == NULL)
  131. return 0;
  132. if (e->dynamic_id == NULL && dynamic_id == NULL)
  133. return 0;
  134. if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
  135. return 0;
  136. if (dynamic_id != NULL) {
  137. iterator = engine_dyn_list_head;
  138. while (iterator != NULL) {
  139. if (iterator->dynamic_id == dynamic_id)
  140. goto err;
  141. iterator = iterator->next;
  142. }
  143. if (e->dynamic_id != NULL)
  144. goto err;
  145. e->dynamic_id = dynamic_id;
  146. }
  147. if (engine_dyn_list_head == NULL) {
  148. /* We are adding to an empty list. */
  149. if (engine_dyn_list_tail != NULL)
  150. goto err;
  151. engine_dyn_list_head = e;
  152. e->prev_dyn = NULL;
  153. } else {
  154. /* We are adding to the tail of an existing list. */
  155. if (engine_dyn_list_tail == NULL
  156. || engine_dyn_list_tail->next_dyn != NULL)
  157. goto err;
  158. engine_dyn_list_tail->next_dyn = e;
  159. e->prev_dyn = engine_dyn_list_tail;
  160. }
  161. engine_dyn_list_tail = e;
  162. e->next_dyn = NULL;
  163. result = 1;
  164. err:
  165. if (not_locked)
  166. CRYPTO_THREAD_unlock(global_engine_lock);
  167. return result;
  168. }
  169. /* Remove engine from dynamic engine list. */
  170. void engine_remove_dynamic_id(ENGINE *e, int not_locked)
  171. {
  172. if (e == NULL || e->dynamic_id == NULL)
  173. return;
  174. if (not_locked && !CRYPTO_THREAD_write_lock(global_engine_lock))
  175. return;
  176. e->dynamic_id = NULL;
  177. /* un-link e from the chain. */
  178. if (e->next_dyn != NULL)
  179. e->next_dyn->prev_dyn = e->prev_dyn;
  180. if (e->prev_dyn != NULL)
  181. e->prev_dyn->next_dyn = e->next_dyn;
  182. /* Correct our head/tail if necessary. */
  183. if (engine_dyn_list_head == e)
  184. engine_dyn_list_head = e->next_dyn;
  185. if (engine_dyn_list_tail == e)
  186. engine_dyn_list_tail = e->prev_dyn;
  187. if (not_locked)
  188. CRYPTO_THREAD_unlock(global_engine_lock);
  189. }
  190. /* Get the first/last "ENGINE" type available. */
  191. ENGINE *ENGINE_get_first(void)
  192. {
  193. ENGINE *ret;
  194. if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
  195. ENGINEerr(ENGINE_F_ENGINE_GET_FIRST, ERR_R_MALLOC_FAILURE);
  196. return NULL;
  197. }
  198. CRYPTO_THREAD_write_lock(global_engine_lock);
  199. ret = engine_list_head;
  200. if (ret) {
  201. ret->struct_ref++;
  202. engine_ref_debug(ret, 0, 1);
  203. }
  204. CRYPTO_THREAD_unlock(global_engine_lock);
  205. return ret;
  206. }
  207. ENGINE *ENGINE_get_last(void)
  208. {
  209. ENGINE *ret;
  210. if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
  211. ENGINEerr(ENGINE_F_ENGINE_GET_LAST, ERR_R_MALLOC_FAILURE);
  212. return NULL;
  213. }
  214. CRYPTO_THREAD_write_lock(global_engine_lock);
  215. ret = engine_list_tail;
  216. if (ret) {
  217. ret->struct_ref++;
  218. engine_ref_debug(ret, 0, 1);
  219. }
  220. CRYPTO_THREAD_unlock(global_engine_lock);
  221. return ret;
  222. }
  223. /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
  224. ENGINE *ENGINE_get_next(ENGINE *e)
  225. {
  226. ENGINE *ret = NULL;
  227. if (e == NULL) {
  228. ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
  229. return 0;
  230. }
  231. CRYPTO_THREAD_write_lock(global_engine_lock);
  232. ret = e->next;
  233. if (ret) {
  234. /* Return a valid structural reference to the next ENGINE */
  235. ret->struct_ref++;
  236. engine_ref_debug(ret, 0, 1);
  237. }
  238. CRYPTO_THREAD_unlock(global_engine_lock);
  239. /* Release the structural reference to the previous ENGINE */
  240. ENGINE_free(e);
  241. return ret;
  242. }
  243. ENGINE *ENGINE_get_prev(ENGINE *e)
  244. {
  245. ENGINE *ret = NULL;
  246. if (e == NULL) {
  247. ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
  248. return 0;
  249. }
  250. CRYPTO_THREAD_write_lock(global_engine_lock);
  251. ret = e->prev;
  252. if (ret) {
  253. /* Return a valid structural reference to the next ENGINE */
  254. ret->struct_ref++;
  255. engine_ref_debug(ret, 0, 1);
  256. }
  257. CRYPTO_THREAD_unlock(global_engine_lock);
  258. /* Release the structural reference to the previous ENGINE */
  259. ENGINE_free(e);
  260. return ret;
  261. }
  262. /* Add another "ENGINE" type into the list. */
  263. int ENGINE_add(ENGINE *e)
  264. {
  265. int to_return = 1;
  266. if (e == NULL) {
  267. ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
  268. return 0;
  269. }
  270. if ((e->id == NULL) || (e->name == NULL)) {
  271. ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
  272. return 0;
  273. }
  274. CRYPTO_THREAD_write_lock(global_engine_lock);
  275. if (!engine_list_add(e)) {
  276. ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
  277. to_return = 0;
  278. }
  279. CRYPTO_THREAD_unlock(global_engine_lock);
  280. return to_return;
  281. }
  282. /* Remove an existing "ENGINE" type from the array. */
  283. int ENGINE_remove(ENGINE *e)
  284. {
  285. int to_return = 1;
  286. if (e == NULL) {
  287. ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
  288. return 0;
  289. }
  290. CRYPTO_THREAD_write_lock(global_engine_lock);
  291. if (!engine_list_remove(e)) {
  292. ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
  293. to_return = 0;
  294. }
  295. CRYPTO_THREAD_unlock(global_engine_lock);
  296. return to_return;
  297. }
  298. static void engine_cpy(ENGINE *dest, const ENGINE *src)
  299. {
  300. dest->id = src->id;
  301. dest->name = src->name;
  302. #ifndef OPENSSL_NO_RSA
  303. dest->rsa_meth = src->rsa_meth;
  304. #endif
  305. #ifndef OPENSSL_NO_DSA
  306. dest->dsa_meth = src->dsa_meth;
  307. #endif
  308. #ifndef OPENSSL_NO_DH
  309. dest->dh_meth = src->dh_meth;
  310. #endif
  311. #ifndef OPENSSL_NO_EC
  312. dest->ec_meth = src->ec_meth;
  313. #endif
  314. dest->rand_meth = src->rand_meth;
  315. dest->ciphers = src->ciphers;
  316. dest->digests = src->digests;
  317. dest->pkey_meths = src->pkey_meths;
  318. dest->destroy = src->destroy;
  319. dest->init = src->init;
  320. dest->finish = src->finish;
  321. dest->ctrl = src->ctrl;
  322. dest->load_privkey = src->load_privkey;
  323. dest->load_pubkey = src->load_pubkey;
  324. dest->cmd_defns = src->cmd_defns;
  325. dest->flags = src->flags;
  326. dest->dynamic_id = src->dynamic_id;
  327. engine_add_dynamic_id(dest, NULL, 0);
  328. }
  329. ENGINE *ENGINE_by_id(const char *id)
  330. {
  331. ENGINE *iterator;
  332. char *load_dir = NULL;
  333. if (id == NULL) {
  334. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
  335. return NULL;
  336. }
  337. if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
  338. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_MALLOC_FAILURE);
  339. return NULL;
  340. }
  341. CRYPTO_THREAD_write_lock(global_engine_lock);
  342. iterator = engine_list_head;
  343. while (iterator && (strcmp(id, iterator->id) != 0))
  344. iterator = iterator->next;
  345. if (iterator != NULL) {
  346. /*
  347. * We need to return a structural reference. If this is an ENGINE
  348. * type that returns copies, make a duplicate - otherwise increment
  349. * the existing ENGINE's reference count.
  350. */
  351. if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
  352. ENGINE *cp = ENGINE_new();
  353. if (cp == NULL)
  354. iterator = NULL;
  355. else {
  356. engine_cpy(cp, iterator);
  357. iterator = cp;
  358. }
  359. } else {
  360. iterator->struct_ref++;
  361. engine_ref_debug(iterator, 0, 1);
  362. }
  363. }
  364. CRYPTO_THREAD_unlock(global_engine_lock);
  365. if (iterator != NULL)
  366. return iterator;
  367. /*
  368. * Prevent infinite recursion if we're looking for the dynamic engine.
  369. */
  370. if (strcmp(id, "dynamic")) {
  371. if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == NULL)
  372. load_dir = ENGINESDIR;
  373. iterator = ENGINE_by_id("dynamic");
  374. if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
  375. !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
  376. !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
  377. load_dir, 0) ||
  378. !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
  379. !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
  380. goto notfound;
  381. return iterator;
  382. }
  383. notfound:
  384. ENGINE_free(iterator);
  385. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
  386. ERR_add_error_data(2, "id=", id);
  387. return NULL;
  388. /* EEK! Experimental code ends */
  389. }
  390. int ENGINE_up_ref(ENGINE *e)
  391. {
  392. int i;
  393. if (e == NULL) {
  394. ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
  395. return 0;
  396. }
  397. CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock);
  398. return 1;
  399. }