dso_lib.c 13 KB

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