2
0

dso_lib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /* dso_lib.c -*- mode:C; c-file-style: "eay" -*- */
  2. /*
  3. * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 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. #include <stdio.h>
  60. #include <openssl/crypto.h>
  61. #include "cryptlib.h"
  62. #include <openssl/dso.h>
  63. static DSO_METHOD *default_DSO_meth = NULL;
  64. DSO *DSO_new(void)
  65. {
  66. return (DSO_new_method(NULL));
  67. }
  68. void DSO_set_default_method(DSO_METHOD *meth)
  69. {
  70. default_DSO_meth = meth;
  71. }
  72. DSO_METHOD *DSO_get_default_method(void)
  73. {
  74. return (default_DSO_meth);
  75. }
  76. DSO_METHOD *DSO_get_method(DSO *dso)
  77. {
  78. return (dso->meth);
  79. }
  80. DSO_METHOD *DSO_set_method(DSO *dso, DSO_METHOD *meth)
  81. {
  82. DSO_METHOD *mtmp;
  83. mtmp = dso->meth;
  84. dso->meth = meth;
  85. return (mtmp);
  86. }
  87. DSO *DSO_new_method(DSO_METHOD *meth)
  88. {
  89. DSO *ret;
  90. if (default_DSO_meth == NULL)
  91. /*
  92. * We default to DSO_METH_openssl() which in turn defaults to
  93. * stealing the "best available" method. Will fallback to
  94. * DSO_METH_null() in the worst case.
  95. */
  96. default_DSO_meth = DSO_METHOD_openssl();
  97. ret = (DSO *)OPENSSL_malloc(sizeof(DSO));
  98. if (ret == NULL) {
  99. DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  100. return (NULL);
  101. }
  102. memset(ret, 0, sizeof(DSO));
  103. ret->meth_data = sk_new_null();
  104. if (ret->meth_data == NULL) {
  105. /* sk_new doesn't generate any errors so we do */
  106. DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  107. OPENSSL_free(ret);
  108. return (NULL);
  109. }
  110. if (meth == NULL)
  111. ret->meth = default_DSO_meth;
  112. else
  113. ret->meth = meth;
  114. ret->references = 1;
  115. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  116. OPENSSL_free(ret);
  117. ret = NULL;
  118. }
  119. return (ret);
  120. }
  121. int DSO_free(DSO *dso)
  122. {
  123. int i;
  124. if (dso == NULL) {
  125. DSOerr(DSO_F_DSO_FREE, ERR_R_PASSED_NULL_PARAMETER);
  126. return (0);
  127. }
  128. i = CRYPTO_add(&dso->references, -1, CRYPTO_LOCK_DSO);
  129. #ifdef REF_PRINT
  130. REF_PRINT("DSO", dso);
  131. #endif
  132. if (i > 0)
  133. return (1);
  134. #ifdef REF_CHECK
  135. if (i < 0) {
  136. fprintf(stderr, "DSO_free, bad reference count\n");
  137. abort();
  138. }
  139. #endif
  140. if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
  141. DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED);
  142. return (0);
  143. }
  144. if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
  145. DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED);
  146. return (0);
  147. }
  148. sk_free(dso->meth_data);
  149. if (dso->filename != NULL)
  150. OPENSSL_free(dso->filename);
  151. if (dso->loaded_filename != NULL)
  152. OPENSSL_free(dso->loaded_filename);
  153. OPENSSL_free(dso);
  154. return (1);
  155. }
  156. int DSO_flags(DSO *dso)
  157. {
  158. return ((dso == NULL) ? 0 : dso->flags);
  159. }
  160. int DSO_up_ref(DSO *dso)
  161. {
  162. if (dso == NULL) {
  163. DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
  164. return (0);
  165. }
  166. CRYPTO_add(&dso->references, 1, CRYPTO_LOCK_DSO);
  167. return (1);
  168. }
  169. DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
  170. {
  171. DSO *ret;
  172. int allocated = 0;
  173. if (dso == NULL) {
  174. ret = DSO_new_method(meth);
  175. if (ret == NULL) {
  176. DSOerr(DSO_F_DSO_LOAD, ERR_R_MALLOC_FAILURE);
  177. goto err;
  178. }
  179. allocated = 1;
  180. /* Pass the provided flags to the new DSO object */
  181. if (DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0) {
  182. DSOerr(DSO_F_DSO_LOAD, DSO_R_CTRL_FAILED);
  183. goto err;
  184. }
  185. } else
  186. ret = dso;
  187. /* Don't load if we're currently already loaded */
  188. if (ret->filename != NULL) {
  189. DSOerr(DSO_F_DSO_LOAD, DSO_R_DSO_ALREADY_LOADED);
  190. goto err;
  191. }
  192. /*
  193. * filename can only be NULL if we were passed a dso that already has one
  194. * set.
  195. */
  196. if (filename != NULL)
  197. if (!DSO_set_filename(ret, filename)) {
  198. DSOerr(DSO_F_DSO_LOAD, DSO_R_SET_FILENAME_FAILED);
  199. goto err;
  200. }
  201. filename = ret->filename;
  202. if (filename == NULL) {
  203. DSOerr(DSO_F_DSO_LOAD, DSO_R_NO_FILENAME);
  204. goto err;
  205. }
  206. if (ret->meth->dso_load == NULL) {
  207. DSOerr(DSO_F_DSO_LOAD, DSO_R_UNSUPPORTED);
  208. goto err;
  209. }
  210. if (!ret->meth->dso_load(ret)) {
  211. DSOerr(DSO_F_DSO_LOAD, DSO_R_LOAD_FAILED);
  212. goto err;
  213. }
  214. /* Load succeeded */
  215. return (ret);
  216. err:
  217. if (allocated)
  218. DSO_free(ret);
  219. return (NULL);
  220. }
  221. void *DSO_bind_var(DSO *dso, const char *symname)
  222. {
  223. void *ret = NULL;
  224. if ((dso == NULL) || (symname == NULL)) {
  225. DSOerr(DSO_F_DSO_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
  226. return (NULL);
  227. }
  228. if (dso->meth->dso_bind_var == NULL) {
  229. DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_UNSUPPORTED);
  230. return (NULL);
  231. }
  232. if ((ret = dso->meth->dso_bind_var(dso, symname)) == NULL) {
  233. DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_SYM_FAILURE);
  234. return (NULL);
  235. }
  236. /* Success */
  237. return (ret);
  238. }
  239. DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
  240. {
  241. DSO_FUNC_TYPE ret = NULL;
  242. if ((dso == NULL) || (symname == NULL)) {
  243. DSOerr(DSO_F_DSO_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  244. return (NULL);
  245. }
  246. if (dso->meth->dso_bind_func == NULL) {
  247. DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_UNSUPPORTED);
  248. return (NULL);
  249. }
  250. if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
  251. DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_SYM_FAILURE);
  252. return (NULL);
  253. }
  254. /* Success */
  255. return (ret);
  256. }
  257. /*
  258. * I don't really like these *_ctrl functions very much to be perfectly
  259. * honest. For one thing, I think I have to return a negative value for any
  260. * error because possible DSO_ctrl() commands may return values such as
  261. * "size"s that can legitimately be zero (making the standard
  262. * "if(DSO_cmd(...))" form that works almost everywhere else fail at odd
  263. * times. I'd prefer "output" values to be passed by reference and the return
  264. * value as success/failure like usual ... but we conform when we must... :-)
  265. */
  266. long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
  267. {
  268. if (dso == NULL) {
  269. DSOerr(DSO_F_DSO_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  270. return (-1);
  271. }
  272. /*
  273. * We should intercept certain generic commands and only pass control to
  274. * the method-specific ctrl() function if it's something we don't handle.
  275. */
  276. switch (cmd) {
  277. case DSO_CTRL_GET_FLAGS:
  278. return dso->flags;
  279. case DSO_CTRL_SET_FLAGS:
  280. dso->flags = (int)larg;
  281. return (0);
  282. case DSO_CTRL_OR_FLAGS:
  283. dso->flags |= (int)larg;
  284. return (0);
  285. default:
  286. break;
  287. }
  288. if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
  289. DSOerr(DSO_F_DSO_CTRL, DSO_R_UNSUPPORTED);
  290. return (-1);
  291. }
  292. return (dso->meth->dso_ctrl(dso, cmd, larg, parg));
  293. }
  294. int DSO_set_name_converter(DSO *dso, DSO_NAME_CONVERTER_FUNC cb,
  295. DSO_NAME_CONVERTER_FUNC *oldcb)
  296. {
  297. if (dso == NULL) {
  298. DSOerr(DSO_F_DSO_SET_NAME_CONVERTER, ERR_R_PASSED_NULL_PARAMETER);
  299. return (0);
  300. }
  301. if (oldcb)
  302. *oldcb = dso->name_converter;
  303. dso->name_converter = cb;
  304. return (1);
  305. }
  306. const char *DSO_get_filename(DSO *dso)
  307. {
  308. if (dso == NULL) {
  309. DSOerr(DSO_F_DSO_GET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  310. return (NULL);
  311. }
  312. return (dso->filename);
  313. }
  314. int DSO_set_filename(DSO *dso, const char *filename)
  315. {
  316. char *copied;
  317. if ((dso == NULL) || (filename == NULL)) {
  318. DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  319. return (0);
  320. }
  321. if (dso->loaded_filename) {
  322. DSOerr(DSO_F_DSO_SET_FILENAME, DSO_R_DSO_ALREADY_LOADED);
  323. return (0);
  324. }
  325. /* We'll duplicate filename */
  326. copied = OPENSSL_malloc(strlen(filename) + 1);
  327. if (copied == NULL) {
  328. DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_MALLOC_FAILURE);
  329. return (0);
  330. }
  331. BUF_strlcpy(copied, filename, strlen(filename) + 1);
  332. if (dso->filename)
  333. OPENSSL_free(dso->filename);
  334. dso->filename = copied;
  335. return (1);
  336. }
  337. char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
  338. {
  339. char *result = NULL;
  340. if (dso == NULL || filespec1 == NULL) {
  341. DSOerr(DSO_F_DSO_MERGE, ERR_R_PASSED_NULL_PARAMETER);
  342. return (NULL);
  343. }
  344. if (filespec1 == NULL)
  345. filespec1 = dso->filename;
  346. if (filespec1 == NULL) {
  347. DSOerr(DSO_F_DSO_MERGE, DSO_R_NO_FILE_SPECIFICATION);
  348. return (NULL);
  349. }
  350. if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
  351. if (dso->merger != NULL)
  352. result = dso->merger(dso, filespec1, filespec2);
  353. else if (dso->meth->dso_merger != NULL)
  354. result = dso->meth->dso_merger(dso, filespec1, filespec2);
  355. }
  356. return (result);
  357. }
  358. char *DSO_convert_filename(DSO *dso, const char *filename)
  359. {
  360. char *result = NULL;
  361. if (dso == NULL) {
  362. DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  363. return (NULL);
  364. }
  365. if (filename == NULL)
  366. filename = dso->filename;
  367. if (filename == NULL) {
  368. DSOerr(DSO_F_DSO_CONVERT_FILENAME, DSO_R_NO_FILENAME);
  369. return (NULL);
  370. }
  371. if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
  372. if (dso->name_converter != NULL)
  373. result = dso->name_converter(dso, filename);
  374. else if (dso->meth->dso_name_converter != NULL)
  375. result = dso->meth->dso_name_converter(dso, filename);
  376. }
  377. if (result == NULL) {
  378. result = OPENSSL_malloc(strlen(filename) + 1);
  379. if (result == NULL) {
  380. DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_MALLOC_FAILURE);
  381. return (NULL);
  382. }
  383. BUF_strlcpy(result, filename, strlen(filename) + 1);
  384. }
  385. return (result);
  386. }
  387. const char *DSO_get_loaded_filename(DSO *dso)
  388. {
  389. if (dso == NULL) {
  390. DSOerr(DSO_F_DSO_GET_LOADED_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  391. return (NULL);
  392. }
  393. return (dso->loaded_filename);
  394. }