2
0

eng_list.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. }
  248. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  249. if (!engine_list_add(e)) {
  250. ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
  251. to_return = 0;
  252. }
  253. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  254. return to_return;
  255. }
  256. /* Remove an existing "ENGINE" type from the array. */
  257. int ENGINE_remove(ENGINE *e)
  258. {
  259. int to_return = 1;
  260. if (e == NULL) {
  261. ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
  262. return 0;
  263. }
  264. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  265. if (!engine_list_remove(e)) {
  266. ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
  267. to_return = 0;
  268. }
  269. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  270. return to_return;
  271. }
  272. static void engine_cpy(ENGINE *dest, const ENGINE *src)
  273. {
  274. dest->id = src->id;
  275. dest->name = src->name;
  276. #ifndef OPENSSL_NO_RSA
  277. dest->rsa_meth = src->rsa_meth;
  278. #endif
  279. #ifndef OPENSSL_NO_DSA
  280. dest->dsa_meth = src->dsa_meth;
  281. #endif
  282. #ifndef OPENSSL_NO_DH
  283. dest->dh_meth = src->dh_meth;
  284. #endif
  285. #ifndef OPENSSL_NO_ECDH
  286. dest->ecdh_meth = src->ecdh_meth;
  287. #endif
  288. #ifndef OPENSSL_NO_ECDSA
  289. dest->ecdsa_meth = src->ecdsa_meth;
  290. #endif
  291. dest->rand_meth = src->rand_meth;
  292. dest->store_meth = src->store_meth;
  293. dest->ciphers = src->ciphers;
  294. dest->digests = src->digests;
  295. dest->destroy = src->destroy;
  296. dest->init = src->init;
  297. dest->finish = src->finish;
  298. dest->ctrl = src->ctrl;
  299. dest->load_privkey = src->load_privkey;
  300. dest->load_pubkey = src->load_pubkey;
  301. dest->cmd_defns = src->cmd_defns;
  302. dest->flags = src->flags;
  303. }
  304. ENGINE *ENGINE_by_id(const char *id)
  305. {
  306. ENGINE *iterator;
  307. char *load_dir = NULL;
  308. if (id == NULL) {
  309. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
  310. return NULL;
  311. }
  312. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  313. iterator = engine_list_head;
  314. while (iterator && (strcmp(id, iterator->id) != 0))
  315. iterator = iterator->next;
  316. if (iterator) {
  317. /*
  318. * We need to return a structural reference. If this is an ENGINE
  319. * type that returns copies, make a duplicate - otherwise increment
  320. * the existing ENGINE's reference count.
  321. */
  322. if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
  323. ENGINE *cp = ENGINE_new();
  324. if (!cp)
  325. iterator = NULL;
  326. else {
  327. engine_cpy(cp, iterator);
  328. iterator = cp;
  329. }
  330. } else {
  331. iterator->struct_ref++;
  332. engine_ref_debug(iterator, 0, 1)
  333. }
  334. }
  335. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  336. #if 0
  337. if (iterator == NULL) {
  338. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
  339. ERR_add_error_data(2, "id=", id);
  340. }
  341. return iterator;
  342. #else
  343. /* EEK! Experimental code starts */
  344. if (iterator)
  345. return iterator;
  346. /*
  347. * Prevent infinite recusrion if we're looking for the dynamic engine.
  348. */
  349. if (strcmp(id, "dynamic")) {
  350. # ifdef OPENSSL_SYS_VMS
  351. if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
  352. load_dir = "SSLROOT:[ENGINES]";
  353. # else
  354. if ((load_dir = getenv("OPENSSL_ENGINES")) == 0)
  355. load_dir = ENGINESDIR;
  356. # endif
  357. iterator = ENGINE_by_id("dynamic");
  358. if (!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
  359. !ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
  360. !ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
  361. load_dir, 0) ||
  362. !ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
  363. goto notfound;
  364. return iterator;
  365. }
  366. notfound:
  367. ENGINE_free(iterator);
  368. ENGINEerr(ENGINE_F_ENGINE_BY_ID, ENGINE_R_NO_SUCH_ENGINE);
  369. ERR_add_error_data(2, "id=", id);
  370. return NULL;
  371. /* EEK! Experimental code ends */
  372. #endif
  373. }
  374. int ENGINE_up_ref(ENGINE *e)
  375. {
  376. if (e == NULL) {
  377. ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
  378. return 0;
  379. }
  380. CRYPTO_add(&e->struct_ref, 1, CRYPTO_LOCK_ENGINE);
  381. return 1;
  382. }