dso_lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* dso_lib.c */
  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_void_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. sk_void_free(ret->meth_data);
  117. OPENSSL_free(ret);
  118. ret = NULL;
  119. }
  120. return (ret);
  121. }
  122. int DSO_free(DSO *dso)
  123. {
  124. int i;
  125. if (dso == NULL) {
  126. DSOerr(DSO_F_DSO_FREE, ERR_R_PASSED_NULL_PARAMETER);
  127. return (0);
  128. }
  129. i = CRYPTO_add(&dso->references, -1, CRYPTO_LOCK_DSO);
  130. #ifdef REF_PRINT
  131. REF_PRINT("DSO", dso);
  132. #endif
  133. if (i > 0)
  134. return (1);
  135. #ifdef REF_CHECK
  136. if (i < 0) {
  137. fprintf(stderr, "DSO_free, bad reference count\n");
  138. abort();
  139. }
  140. #endif
  141. if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
  142. DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED);
  143. return (0);
  144. }
  145. if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
  146. DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED);
  147. return (0);
  148. }
  149. sk_void_free(dso->meth_data);
  150. if (dso->filename != NULL)
  151. OPENSSL_free(dso->filename);
  152. if (dso->loaded_filename != NULL)
  153. OPENSSL_free(dso->loaded_filename);
  154. OPENSSL_free(dso);
  155. return (1);
  156. }
  157. int DSO_flags(DSO *dso)
  158. {
  159. return ((dso == NULL) ? 0 : dso->flags);
  160. }
  161. int DSO_up_ref(DSO *dso)
  162. {
  163. if (dso == NULL) {
  164. DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
  165. return (0);
  166. }
  167. CRYPTO_add(&dso->references, 1, CRYPTO_LOCK_DSO);
  168. return (1);
  169. }
  170. DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
  171. {
  172. DSO *ret;
  173. int allocated = 0;
  174. if (dso == NULL) {
  175. ret = DSO_new_method(meth);
  176. if (ret == NULL) {
  177. DSOerr(DSO_F_DSO_LOAD, ERR_R_MALLOC_FAILURE);
  178. goto err;
  179. }
  180. allocated = 1;
  181. /* Pass the provided flags to the new DSO object */
  182. if (DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0) {
  183. DSOerr(DSO_F_DSO_LOAD, DSO_R_CTRL_FAILED);
  184. goto err;
  185. }
  186. } else
  187. ret = dso;
  188. /* Don't load if we're currently already loaded */
  189. if (ret->filename != NULL) {
  190. DSOerr(DSO_F_DSO_LOAD, DSO_R_DSO_ALREADY_LOADED);
  191. goto err;
  192. }
  193. /*
  194. * filename can only be NULL if we were passed a dso that already has one
  195. * set.
  196. */
  197. if (filename != NULL)
  198. if (!DSO_set_filename(ret, filename)) {
  199. DSOerr(DSO_F_DSO_LOAD, DSO_R_SET_FILENAME_FAILED);
  200. goto err;
  201. }
  202. filename = ret->filename;
  203. if (filename == NULL) {
  204. DSOerr(DSO_F_DSO_LOAD, DSO_R_NO_FILENAME);
  205. goto err;
  206. }
  207. if (ret->meth->dso_load == NULL) {
  208. DSOerr(DSO_F_DSO_LOAD, DSO_R_UNSUPPORTED);
  209. goto err;
  210. }
  211. if (!ret->meth->dso_load(ret)) {
  212. DSOerr(DSO_F_DSO_LOAD, DSO_R_LOAD_FAILED);
  213. goto err;
  214. }
  215. /* Load succeeded */
  216. return (ret);
  217. err:
  218. if (allocated)
  219. DSO_free(ret);
  220. return (NULL);
  221. }
  222. void *DSO_bind_var(DSO *dso, const char *symname)
  223. {
  224. void *ret = NULL;
  225. if ((dso == NULL) || (symname == NULL)) {
  226. DSOerr(DSO_F_DSO_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
  227. return (NULL);
  228. }
  229. if (dso->meth->dso_bind_var == NULL) {
  230. DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_UNSUPPORTED);
  231. return (NULL);
  232. }
  233. if ((ret = dso->meth->dso_bind_var(dso, symname)) == NULL) {
  234. DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_SYM_FAILURE);
  235. return (NULL);
  236. }
  237. /* Success */
  238. return (ret);
  239. }
  240. DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
  241. {
  242. DSO_FUNC_TYPE ret = NULL;
  243. if ((dso == NULL) || (symname == NULL)) {
  244. DSOerr(DSO_F_DSO_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  245. return (NULL);
  246. }
  247. if (dso->meth->dso_bind_func == NULL) {
  248. DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_UNSUPPORTED);
  249. return (NULL);
  250. }
  251. if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
  252. DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_SYM_FAILURE);
  253. return (NULL);
  254. }
  255. /* Success */
  256. return (ret);
  257. }
  258. /*
  259. * I don't really like these *_ctrl functions very much to be perfectly
  260. * honest. For one thing, I think I have to return a negative value for any
  261. * error because possible DSO_ctrl() commands may return values such as
  262. * "size"s that can legitimately be zero (making the standard
  263. * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
  264. * times. I'd prefer "output" values to be passed by reference and the return
  265. * value as success/failure like usual ... but we conform when we must... :-)
  266. */
  267. long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
  268. {
  269. if (dso == NULL) {
  270. DSOerr(DSO_F_DSO_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  271. return (-1);
  272. }
  273. /*
  274. * We should intercept certain generic commands and only pass control to
  275. * the method-specific ctrl() function if it's something we don't handle.
  276. */
  277. switch (cmd) {
  278. case DSO_CTRL_GET_FLAGS:
  279. return dso->flags;
  280. case DSO_CTRL_SET_FLAGS:
  281. dso->flags = (int)larg;
  282. return (0);
  283. case DSO_CTRL_OR_FLAGS:
  284. dso->flags |= (int)larg;
  285. return (0);
  286. default:
  287. break;
  288. }
  289. if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
  290. DSOerr(DSO_F_DSO_CTRL, DSO_R_UNSUPPORTED);
  291. return (-1);
  292. }
  293. return (dso->meth->dso_ctrl(dso, cmd, larg, parg));
  294. }
  295. int DSO_set_name_converter(DSO *dso, DSO_NAME_CONVERTER_FUNC cb,
  296. DSO_NAME_CONVERTER_FUNC *oldcb)
  297. {
  298. if (dso == NULL) {
  299. DSOerr(DSO_F_DSO_SET_NAME_CONVERTER, ERR_R_PASSED_NULL_PARAMETER);
  300. return (0);
  301. }
  302. if (oldcb)
  303. *oldcb = dso->name_converter;
  304. dso->name_converter = cb;
  305. return (1);
  306. }
  307. const char *DSO_get_filename(DSO *dso)
  308. {
  309. if (dso == NULL) {
  310. DSOerr(DSO_F_DSO_GET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  311. return (NULL);
  312. }
  313. return (dso->filename);
  314. }
  315. int DSO_set_filename(DSO *dso, const char *filename)
  316. {
  317. char *copied;
  318. if ((dso == NULL) || (filename == NULL)) {
  319. DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  320. return (0);
  321. }
  322. if (dso->loaded_filename) {
  323. DSOerr(DSO_F_DSO_SET_FILENAME, DSO_R_DSO_ALREADY_LOADED);
  324. return (0);
  325. }
  326. /* We'll duplicate filename */
  327. copied = OPENSSL_malloc(strlen(filename) + 1);
  328. if (copied == NULL) {
  329. DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_MALLOC_FAILURE);
  330. return (0);
  331. }
  332. BUF_strlcpy(copied, filename, strlen(filename) + 1);
  333. if (dso->filename)
  334. OPENSSL_free(dso->filename);
  335. dso->filename = copied;
  336. return (1);
  337. }
  338. char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
  339. {
  340. char *result = NULL;
  341. if (dso == NULL || filespec1 == NULL) {
  342. DSOerr(DSO_F_DSO_MERGE, ERR_R_PASSED_NULL_PARAMETER);
  343. return (NULL);
  344. }
  345. if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
  346. if (dso->merger != NULL)
  347. result = dso->merger(dso, filespec1, filespec2);
  348. else if (dso->meth->dso_merger != NULL)
  349. result = dso->meth->dso_merger(dso, filespec1, filespec2);
  350. }
  351. return (result);
  352. }
  353. char *DSO_convert_filename(DSO *dso, const char *filename)
  354. {
  355. char *result = NULL;
  356. if (dso == NULL) {
  357. DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  358. return (NULL);
  359. }
  360. if (filename == NULL)
  361. filename = dso->filename;
  362. if (filename == NULL) {
  363. DSOerr(DSO_F_DSO_CONVERT_FILENAME, DSO_R_NO_FILENAME);
  364. return (NULL);
  365. }
  366. if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
  367. if (dso->name_converter != NULL)
  368. result = dso->name_converter(dso, filename);
  369. else if (dso->meth->dso_name_converter != NULL)
  370. result = dso->meth->dso_name_converter(dso, filename);
  371. }
  372. if (result == NULL) {
  373. result = OPENSSL_malloc(strlen(filename) + 1);
  374. if (result == NULL) {
  375. DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_MALLOC_FAILURE);
  376. return (NULL);
  377. }
  378. BUF_strlcpy(result, filename, strlen(filename) + 1);
  379. }
  380. return (result);
  381. }
  382. const char *DSO_get_loaded_filename(DSO *dso)
  383. {
  384. if (dso == NULL) {
  385. DSOerr(DSO_F_DSO_GET_LOADED_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
  386. return (NULL);
  387. }
  388. return (dso->loaded_filename);
  389. }
  390. int DSO_pathbyaddr(void *addr, char *path, int sz)
  391. {
  392. DSO_METHOD *meth = default_DSO_meth;
  393. if (meth == NULL)
  394. meth = DSO_METHOD_openssl();
  395. if (meth->pathbyaddr == NULL) {
  396. DSOerr(DSO_F_DSO_PATHBYADDR, DSO_R_UNSUPPORTED);
  397. return -1;
  398. }
  399. return (*meth->pathbyaddr) (addr, path, sz);
  400. }
  401. void *DSO_global_lookup(const char *name)
  402. {
  403. DSO_METHOD *meth = default_DSO_meth;
  404. if (meth == NULL)
  405. meth = DSO_METHOD_openssl();
  406. if (meth->globallookup == NULL) {
  407. DSOerr(DSO_F_DSO_GLOBAL_LOOKUP, DSO_R_UNSUPPORTED);
  408. return NULL;
  409. }
  410. return (*meth->globallookup) (name);
  411. }