eng_list.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* crypto/engine/eng_list.c */
  2. /*
  3. * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2018 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* ====================================================================
  60. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  61. * ECDH support in OpenSSL originally developed by
  62. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  63. */
  64. #include "cryptlib.h"
  65. #include "eng_int.h"
  66. /*
  67. * The linked-list of pointers to engine types. engine_list_head incorporates
  68. * an implicit structural reference but engine_list_tail does not - the
  69. * latter is a computational niceity and only points to something that is
  70. * already pointed to by its predecessor in the list (or engine_list_head
  71. * itself). In the same way, the use of the "prev" pointer in each ENGINE is
  72. * to save excessive list iteration, it doesn't correspond to an extra
  73. * structural reference. Hence, engine_list_head, and each non-null "next"
  74. * pointer account for the list itself assuming exactly 1 structural
  75. * reference on each list member.
  76. */
  77. static ENGINE *engine_list_head = NULL;
  78. static ENGINE *engine_list_tail = NULL;
  79. /*
  80. * This cleanup function is only needed internally. If it should be called,
  81. * we register it with the "ENGINE_cleanup()" stack to be called during
  82. * cleanup.
  83. */
  84. static void engine_list_cleanup(void)
  85. {
  86. ENGINE *iterator = engine_list_head;
  87. while (iterator != NULL) {
  88. ENGINE_remove(iterator);
  89. iterator = engine_list_head;
  90. }
  91. return;
  92. }
  93. /*
  94. * These static functions starting with a lower case "engine_" always take
  95. * place when CRYPTO_LOCK_ENGINE has been locked up.
  96. */
  97. static int engine_list_add(ENGINE *e)
  98. {
  99. int conflict = 0;
  100. ENGINE *iterator = NULL;
  101. if (e == NULL) {
  102. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER);
  103. return 0;
  104. }
  105. iterator = engine_list_head;
  106. while (iterator && !conflict) {
  107. conflict = (strcmp(iterator->id, e->id) == 0);
  108. iterator = iterator->next;
  109. }
  110. if (conflict) {
  111. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID);
  112. return 0;
  113. }
  114. if (engine_list_head == NULL) {
  115. /* We are adding to an empty list. */
  116. if (engine_list_tail) {
  117. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
  118. return 0;
  119. }
  120. engine_list_head = e;
  121. e->prev = NULL;
  122. /*
  123. * The first time the list allocates, we should register the cleanup.
  124. */
  125. engine_cleanup_add_last(engine_list_cleanup);
  126. } else {
  127. /* We are adding to the tail of an existing list. */
  128. if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
  129. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
  130. return 0;
  131. }
  132. engine_list_tail->next = e;
  133. e->prev = engine_list_tail;
  134. }
  135. /*
  136. * Having the engine in the list assumes a structural reference.
  137. */
  138. e->struct_ref++;
  139. engine_ref_debug(e, 0, 1)
  140. /* However it came to be, e is the last item in the list. */
  141. engine_list_tail = e;
  142. e->next = NULL;
  143. return 1;
  144. }
  145. static int engine_list_remove(ENGINE *e)
  146. {
  147. ENGINE *iterator;
  148. if (e == NULL) {
  149. ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
  150. return 0;
  151. }
  152. /* We need to check that e is in our linked list! */
  153. iterator = engine_list_head;
  154. while (iterator && (iterator != e))
  155. iterator = iterator->next;
  156. if (iterator == NULL) {
  157. ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
  158. ENGINE_R_ENGINE_IS_NOT_IN_LIST);
  159. return 0;
  160. }
  161. /* un-link e from the chain. */
  162. if (e->next)
  163. e->next->prev = e->prev;
  164. if (e->prev)
  165. e->prev->next = e->next;
  166. /* Correct our head/tail if necessary. */
  167. if (engine_list_head == e)
  168. engine_list_head = e->next;
  169. if (engine_list_tail == e)
  170. engine_list_tail = e->prev;
  171. engine_free_util(e, 0);
  172. return 1;
  173. }
  174. /* Get the first/last "ENGINE" type available. */
  175. ENGINE *ENGINE_get_first(void)
  176. {
  177. ENGINE *ret;
  178. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  179. ret = engine_list_head;
  180. if (ret) {
  181. ret->struct_ref++;
  182. engine_ref_debug(ret, 0, 1)
  183. }
  184. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  185. return ret;
  186. }
  187. ENGINE *ENGINE_get_last(void)
  188. {
  189. ENGINE *ret;
  190. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  191. ret = engine_list_tail;
  192. if (ret) {
  193. ret->struct_ref++;
  194. engine_ref_debug(ret, 0, 1)
  195. }
  196. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  197. return ret;
  198. }
  199. /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
  200. ENGINE *ENGINE_get_next(ENGINE *e)
  201. {
  202. ENGINE *ret = NULL;
  203. if (e == NULL) {
  204. ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
  205. return 0;
  206. }
  207. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  208. ret = e->next;
  209. if (ret) {
  210. /* Return a valid structural refernce to the next ENGINE */
  211. ret->struct_ref++;
  212. engine_ref_debug(ret, 0, 1)
  213. }
  214. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  215. /* Release the structural reference to the previous ENGINE */
  216. ENGINE_free(e);
  217. return ret;
  218. }
  219. ENGINE *ENGINE_get_prev(ENGINE *e)
  220. {
  221. ENGINE *ret = NULL;
  222. if (e == NULL) {
  223. ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
  224. return 0;
  225. }
  226. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  227. ret = e->prev;
  228. if (ret) {
  229. /* Return a valid structural reference to the next ENGINE */
  230. ret->struct_ref++;
  231. engine_ref_debug(ret, 0, 1)
  232. }
  233. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  234. /* Release the structural reference to the previous ENGINE */
  235. ENGINE_free(e);
  236. return ret;
  237. }
  238. /* Add another "ENGINE" type into the list. */
  239. int ENGINE_add(ENGINE *e)
  240. {
  241. int to_return = 1;
  242. if (e == NULL) {
  243. ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
  244. return 0;
  245. }
  246. if ((e->id == NULL) || (e->name == NULL)) {
  247. ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
  248. return 0;
  249. }
  250. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  251. if (!engine_list_add(e)) {
  252. ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
  253. to_return = 0;
  254. }
  255. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  256. return to_return;
  257. }
  258. /* Remove an existing "ENGINE" type from the array. */
  259. int ENGINE_remove(ENGINE *e)
  260. {
  261. int to_return = 1;
  262. if (e == NULL) {
  263. ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
  264. return 0;
  265. }
  266. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  267. if (!engine_list_remove(e)) {
  268. ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
  269. to_return = 0;
  270. }
  271. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  272. return to_return;
  273. }
  274. static void engine_cpy(ENGINE *dest, const ENGINE *src)
  275. {
  276. dest->id = src->id;
  277. dest->name = src->name;
  278. #ifndef OPENSSL_NO_RSA
  279. dest->rsa_meth = src->rsa_meth;
  280. #endif
  281. #ifndef OPENSSL_NO_DSA
  282. dest->dsa_meth = src->dsa_meth;
  283. #endif
  284. #ifndef OPENSSL_NO_DH
  285. dest->dh_meth = src->dh_meth;
  286. #endif
  287. #ifndef OPENSSL_NO_ECDH
  288. dest->ecdh_meth = src->ecdh_meth;
  289. #endif
  290. #ifndef OPENSSL_NO_ECDSA
  291. dest->ecdsa_meth = src->ecdsa_meth;
  292. #endif
  293. dest->rand_meth = src->rand_meth;
  294. dest->store_meth = src->store_meth;
  295. dest->ciphers = src->ciphers;
  296. dest->digests = src->digests;
  297. dest->pkey_meths = src->pkey_meths;
  298. dest->destroy = src->destroy;
  299. dest->init = src->init;
  300. dest->finish = src->finish;
  301. dest->ctrl = src->ctrl;
  302. dest->load_privkey = src->load_privkey;
  303. dest->load_pubkey = src->load_pubkey;
  304. dest->cmd_defns = src->cmd_defns;
  305. dest->flags = src->flags;
  306. }
  307. ENGINE *ENGINE_by_id(const char *id)
  308. {
  309. ENGINE *iterator;
  310. char *load_dir = NULL;
  311. if (id == NULL) {
  312. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
  313. return NULL;
  314. }
  315. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  316. iterator = engine_list_head;
  317. while (iterator && (strcmp(id, iterator->id) != 0))
  318. iterator = iterator->next;
  319. if (iterator) {
  320. /*
  321. * We need to return a structural reference. If this is an ENGINE
  322. * type that returns copies, make a duplicate - otherwise increment
  323. * the existing ENGINE's reference count.
  324. */
  325. if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
  326. ENGINE *cp = ENGINE_new();
  327. if (!cp)
  328. iterator = NULL;
  329. else {
  330. engine_cpy(cp, iterator);
  331. iterator = cp;
  332. }
  333. } else {
  334. iterator->struct_ref++;
  335. engine_ref_debug(iterator, 0, 1)
  336. }
  337. }
  338. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  339. #if 0
  340. if (iterator == NULL) {
  341. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
  342. ERR_add_error_data(2, "id=", id);
  343. }
  344. return iterator;
  345. #else
  346. /* EEK! Experimental code starts */
  347. if (iterator)
  348. return iterator;
  349. /*
  350. * Prevent infinite recusrion if we're looking for the dynamic engine.
  351. */
  352. if (strcmp(id, "dynamic")) {
  353. # ifdef OPENSSL_SYS_VMS
  354. if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == 0)
  355. load_dir = "SSLROOT:[ENGINES]";
  356. # else
  357. if ((load_dir = ossl_safe_getenv("OPENSSL_ENGINES")) == 0)
  358. load_dir = ENGINESDIR;
  359. # endif
  360. iterator = ENGINE_by_id("dynamic");
  361. if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
  362. !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
  363. !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
  364. load_dir, 0) ||
  365. !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
  366. !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
  367. goto notfound;
  368. return iterator;
  369. }
  370. notfound:
  371. ENGINE_free(iterator);
  372. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
  373. ERR_add_error_data(2, "id=", id);
  374. return NULL;
  375. /* EEK! Experimental code ends */
  376. #endif
  377. }
  378. int ENGINE_up_ref(ENGINE *e)
  379. {
  380. if (e == NULL) {
  381. ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
  382. return 0;
  383. }
  384. CRYPTO_add(&e->struct_ref, 1, CRYPTO_LOCK_ENGINE);
  385. return 1;
  386. }