eng_list.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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-2001 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 "eng_int.h"
  65. /*
  66. * The linked-list of pointers to engine types. engine_list_head incorporates
  67. * an implicit structural reference but engine_list_tail does not - the
  68. * latter is a computational niceity and only points to something that is
  69. * already pointed to by its predecessor in the list (or engine_list_head
  70. * itself). In the same way, the use of the "prev" pointer in each ENGINE is
  71. * to save excessive list iteration, it doesn't correspond to an extra
  72. * structural reference. Hence, engine_list_head, and each non-null "next"
  73. * pointer account for the list itself assuming exactly 1 structural
  74. * reference on each list member.
  75. */
  76. static ENGINE *engine_list_head = NULL;
  77. static ENGINE *engine_list_tail = NULL;
  78. /*
  79. * This cleanup function is only needed internally. If it should be called,
  80. * we register it with the "ENGINE_cleanup()" stack to be called during
  81. * cleanup.
  82. */
  83. static void engine_list_cleanup(void)
  84. {
  85. ENGINE *iterator = engine_list_head;
  86. while (iterator != NULL) {
  87. ENGINE_remove(iterator);
  88. iterator = engine_list_head;
  89. }
  90. return;
  91. }
  92. /*
  93. * These static functions starting with a lower case "engine_" always take
  94. * place when CRYPTO_LOCK_ENGINE has been locked up.
  95. */
  96. static int engine_list_add(ENGINE *e)
  97. {
  98. int conflict = 0;
  99. ENGINE *iterator = NULL;
  100. if (e == NULL) {
  101. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ERR_R_PASSED_NULL_PARAMETER);
  102. return 0;
  103. }
  104. iterator = engine_list_head;
  105. while (iterator && !conflict) {
  106. conflict = (strcmp(iterator->id, e->id) == 0);
  107. iterator = iterator->next;
  108. }
  109. if (conflict) {
  110. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_CONFLICTING_ENGINE_ID);
  111. return 0;
  112. }
  113. if (engine_list_head == NULL) {
  114. /* We are adding to an empty list. */
  115. if (engine_list_tail) {
  116. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
  117. return 0;
  118. }
  119. engine_list_head = e;
  120. e->prev = NULL;
  121. /*
  122. * The first time the list allocates, we should register the cleanup.
  123. */
  124. engine_cleanup_add_last(engine_list_cleanup);
  125. } else {
  126. /* We are adding to the tail of an existing list. */
  127. if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
  128. ENGINEerr(ENGINE_F_ENGINE_LIST_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
  129. return 0;
  130. }
  131. engine_list_tail->next = e;
  132. e->prev = engine_list_tail;
  133. }
  134. /*
  135. * Having the engine in the list assumes a structural reference.
  136. */
  137. e->struct_ref++;
  138. engine_ref_debug(e, 0, 1)
  139. /* However it came to be, e is the last item in the list. */
  140. engine_list_tail = e;
  141. e->next = NULL;
  142. return 1;
  143. }
  144. static int engine_list_remove(ENGINE *e)
  145. {
  146. ENGINE *iterator;
  147. if (e == NULL) {
  148. ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
  149. return 0;
  150. }
  151. /* We need to check that e is in our linked list! */
  152. iterator = engine_list_head;
  153. while (iterator && (iterator != e))
  154. iterator = iterator->next;
  155. if (iterator == NULL) {
  156. ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
  157. ENGINE_R_ENGINE_IS_NOT_IN_LIST);
  158. return 0;
  159. }
  160. /* un-link e from the chain. */
  161. if (e->next)
  162. e->next->prev = e->prev;
  163. if (e->prev)
  164. e->prev->next = e->next;
  165. /* Correct our head/tail if necessary. */
  166. if (engine_list_head == e)
  167. engine_list_head = e->next;
  168. if (engine_list_tail == e)
  169. engine_list_tail = e->prev;
  170. engine_free_util(e, 0);
  171. return 1;
  172. }
  173. /* Get the first/last "ENGINE" type available. */
  174. ENGINE *ENGINE_get_first(void)
  175. {
  176. ENGINE *ret;
  177. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  178. ret = engine_list_head;
  179. if (ret) {
  180. ret->struct_ref++;
  181. engine_ref_debug(ret, 0, 1)
  182. }
  183. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  184. return ret;
  185. }
  186. ENGINE *ENGINE_get_last(void)
  187. {
  188. ENGINE *ret;
  189. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  190. ret = engine_list_tail;
  191. if (ret) {
  192. ret->struct_ref++;
  193. engine_ref_debug(ret, 0, 1)
  194. }
  195. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  196. return ret;
  197. }
  198. /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
  199. ENGINE *ENGINE_get_next(ENGINE *e)
  200. {
  201. ENGINE *ret = NULL;
  202. if (e == NULL) {
  203. ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
  204. return 0;
  205. }
  206. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  207. ret = e->next;
  208. if (ret) {
  209. /* Return a valid structural refernce to the next ENGINE */
  210. ret->struct_ref++;
  211. engine_ref_debug(ret, 0, 1)
  212. }
  213. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  214. /* Release the structural reference to the previous ENGINE */
  215. ENGINE_free(e);
  216. return ret;
  217. }
  218. ENGINE *ENGINE_get_prev(ENGINE *e)
  219. {
  220. ENGINE *ret = NULL;
  221. if (e == NULL) {
  222. ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
  223. return 0;
  224. }
  225. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  226. ret = e->prev;
  227. if (ret) {
  228. /* Return a valid structural reference to the next ENGINE */
  229. ret->struct_ref++;
  230. engine_ref_debug(ret, 0, 1)
  231. }
  232. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  233. /* Release the structural reference to the previous ENGINE */
  234. ENGINE_free(e);
  235. return ret;
  236. }
  237. /* Add another "ENGINE" type into the list. */
  238. int ENGINE_add(ENGINE *e)
  239. {
  240. int to_return = 1;
  241. if (e == NULL) {
  242. ENGINEerr(ENGINE_F_ENGINE_ADD, ERR_R_PASSED_NULL_PARAMETER);
  243. return 0;
  244. }
  245. if ((e->id == NULL) || (e->name == NULL)) {
  246. ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
  247. return 0;
  248. }
  249. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  250. if (!engine_list_add(e)) {
  251. ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
  252. to_return = 0;
  253. }
  254. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  255. return to_return;
  256. }
  257. /* Remove an existing "ENGINE" type from the array. */
  258. int ENGINE_remove(ENGINE *e)
  259. {
  260. int to_return = 1;
  261. if (e == NULL) {
  262. ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
  263. return 0;
  264. }
  265. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  266. if (!engine_list_remove(e)) {
  267. ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
  268. to_return = 0;
  269. }
  270. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  271. return to_return;
  272. }
  273. static void engine_cpy(ENGINE *dest, const ENGINE *src)
  274. {
  275. dest->id = src->id;
  276. dest->name = src->name;
  277. #ifndef OPENSSL_NO_RSA
  278. dest->rsa_meth = src->rsa_meth;
  279. #endif
  280. #ifndef OPENSSL_NO_DSA
  281. dest->dsa_meth = src->dsa_meth;
  282. #endif
  283. #ifndef OPENSSL_NO_DH
  284. dest->dh_meth = src->dh_meth;
  285. #endif
  286. #ifndef OPENSSL_NO_ECDH
  287. dest->ecdh_meth = src->ecdh_meth;
  288. #endif
  289. #ifndef OPENSSL_NO_ECDSA
  290. dest->ecdsa_meth = src->ecdsa_meth;
  291. #endif
  292. dest->rand_meth = src->rand_meth;
  293. dest->store_meth = src->store_meth;
  294. dest->ciphers = src->ciphers;
  295. dest->digests = src->digests;
  296. dest->pkey_meths = src->pkey_meths;
  297. dest->destroy = src->destroy;
  298. dest->init = src->init;
  299. dest->finish = src->finish;
  300. dest->ctrl = src->ctrl;
  301. dest->load_privkey = src->load_privkey;
  302. dest->load_pubkey = src->load_pubkey;
  303. dest->cmd_defns = src->cmd_defns;
  304. dest->flags = src->flags;
  305. }
  306. ENGINE *ENGINE_by_id(const char *id)
  307. {
  308. ENGINE *iterator;
  309. char *load_dir = NULL;
  310. if (id == NULL) {
  311. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
  312. return NULL;
  313. }
  314. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  315. iterator = engine_list_head;
  316. while (iterator && (strcmp(id, iterator->id) != 0))
  317. iterator = iterator->next;
  318. if (iterator) {
  319. /*
  320. * We need to return a structural reference. If this is an ENGINE
  321. * type that returns copies, make a duplicate - otherwise increment
  322. * the existing ENGINE's reference count.
  323. */
  324. if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
  325. ENGINE *cp = ENGINE_new();
  326. if (!cp)
  327. iterator = NULL;
  328. else {
  329. engine_cpy(cp, iterator);
  330. iterator = cp;
  331. }
  332. } else {
  333. iterator->struct_ref++;
  334. engine_ref_debug(iterator, 0, 1)
  335. }
  336. }
  337. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  338. #if 0
  339. if (iterator == NULL) {
  340. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
  341. ERR_add_error_data(2, "id=", id);
  342. }
  343. return iterator;
  344. #else
  345. /* EEK! Experimental code starts */
  346. if (iterator)
  347. return iterator;
  348. /*
  349. * Prevent infinite recusrion if we're looking for the dynamic engine.
  350. */
  351. if (strcmp(id, "dynamic")) {
  352. # ifdef OPENSSL_SYS_VMS
  353. if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
  354. load_dir = "SSLROOT:[ENGINES]";
  355. # else
  356. if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
  357. load_dir = ENGINESDIR;
  358. # endif
  359. iterator = ENGINE_by_id("dynamic");
  360. if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
  361. !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
  362. !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
  363. load_dir, 0) ||
  364. !ENGINE_ctrl_cmd_string(iterator, "LIST_ADD", "1", 0) ||
  365. !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
  366. goto notfound;
  367. return iterator;
  368. }
  369. notfound:
  370. ENGINE_free(iterator);
  371. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
  372. ERR_add_error_data(2, "id=", id);
  373. return NULL;
  374. /* EEK! Experimental code ends */
  375. #endif
  376. }
  377. int ENGINE_up_ref(ENGINE *e)
  378. {
  379. if (e == NULL) {
  380. ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
  381. return 0;
  382. }
  383. CRYPTO_add(&e->struct_ref, 1, CRYPTO_LOCK_ENGINE);
  384. return 1;
  385. }