LCOV - code coverage report
Current view: top level - third_party/openssl/crypto/dso - dso_lib.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 0 202 0.0 %
Date: 2015-10-10 Functions: 0 21 0.0 %

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

Generated by: LCOV version 1.10