dso_lib.c 12 KB

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