eng_list.c 11 KB

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