1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/conf/conf_api.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,308 @@
1.4 +/* conf_api.c */
1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1.6 + * All rights reserved.
1.7 + *
1.8 + * This package is an SSL implementation written
1.9 + * by Eric Young (eay@cryptsoft.com).
1.10 + * The implementation was written so as to conform with Netscapes SSL.
1.11 + *
1.12 + * This library is free for commercial and non-commercial use as long as
1.13 + * the following conditions are aheared to. The following conditions
1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
1.15 + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1.16 + * included with this distribution is covered by the same copyright terms
1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1.18 + *
1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
1.20 + * the code are not to be removed.
1.21 + * If this package is used in a product, Eric Young should be given attribution
1.22 + * as the author of the parts of the library used.
1.23 + * This can be in the form of a textual message at program startup or
1.24 + * in documentation (online or textual) provided with the package.
1.25 + *
1.26 + * Redistribution and use in source and binary forms, with or without
1.27 + * modification, are permitted provided that the following conditions
1.28 + * are met:
1.29 + * 1. Redistributions of source code must retain the copyright
1.30 + * notice, this list of conditions and the following disclaimer.
1.31 + * 2. Redistributions in binary form must reproduce the above copyright
1.32 + * notice, this list of conditions and the following disclaimer in the
1.33 + * documentation and/or other materials provided with the distribution.
1.34 + * 3. All advertising materials mentioning features or use of this software
1.35 + * must display the following acknowledgement:
1.36 + * "This product includes cryptographic software written by
1.37 + * Eric Young (eay@cryptsoft.com)"
1.38 + * The word 'cryptographic' can be left out if the rouines from the library
1.39 + * being used are not cryptographic related :-).
1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from
1.41 + * the apps directory (application code) you must include an acknowledgement:
1.42 + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1.43 + *
1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.47 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.54 + * SUCH DAMAGE.
1.55 + *
1.56 + * The licence and distribution terms for any publically available version or
1.57 + * derivative of this code cannot be changed. i.e. this code cannot simply be
1.58 + * copied and put under another distribution licence
1.59 + * [including the GNU Public Licence.]
1.60 + */
1.61 +
1.62 +/* Part of the code in here was originally in conf.c, which is now removed */
1.63 +
1.64 +#ifndef CONF_DEBUG
1.65 +# undef NDEBUG /* avoid conflicting definitions */
1.66 +# define NDEBUG
1.67 +#endif
1.68 +
1.69 +#include <assert.h>
1.70 +#include <string.h>
1.71 +#include <openssl/conf.h>
1.72 +#include <openssl/conf_api.h>
1.73 +#include "e_os.h"
1.74 +
1.75 +static void value_free_hash(CONF_VALUE *a, LHASH *conf);
1.76 +static void value_free_stack(CONF_VALUE *a,LHASH *conf);
1.77 +static IMPLEMENT_LHASH_DOALL_ARG_FN(value_free_hash, CONF_VALUE *, LHASH *)
1.78 +static IMPLEMENT_LHASH_DOALL_ARG_FN(value_free_stack, CONF_VALUE *, LHASH *)
1.79 +/* We don't use function pointer casting or wrapper functions - but cast each
1.80 + * callback parameter inside the callback functions. */
1.81 +/* static unsigned long hash(CONF_VALUE *v); */
1.82 +static unsigned long hash(const void *v_void);
1.83 +/* static int cmp_conf(CONF_VALUE *a,CONF_VALUE *b); */
1.84 +static int cmp_conf(const void *a_void,const void *b_void);
1.85 +
1.86 +/* Up until OpenSSL 0.9.5a, this was get_section */
1.87 +EXPORT_C CONF_VALUE *_CONF_get_section(const CONF *conf, const char *section)
1.88 + {
1.89 + CONF_VALUE *v,vv;
1.90 +
1.91 + if ((conf == NULL) || (section == NULL)) return(NULL);
1.92 + vv.name=NULL;
1.93 + vv.section=(char *)section;
1.94 + v=(CONF_VALUE *)lh_retrieve(conf->data,&vv);
1.95 + return(v);
1.96 + }
1.97 +
1.98 +/* Up until OpenSSL 0.9.5a, this was CONF_get_section */
1.99 +EXPORT_C STACK_OF(CONF_VALUE) *_CONF_get_section_values(const CONF *conf,
1.100 + const char *section)
1.101 + {
1.102 + CONF_VALUE *v;
1.103 +
1.104 + v=_CONF_get_section(conf,section);
1.105 + if (v != NULL)
1.106 + return((STACK_OF(CONF_VALUE) *)v->value);
1.107 + else
1.108 + return(NULL);
1.109 + }
1.110 +
1.111 +EXPORT_C int _CONF_add_string(CONF *conf, CONF_VALUE *section, CONF_VALUE *value)
1.112 + {
1.113 + CONF_VALUE *v = NULL;
1.114 + STACK_OF(CONF_VALUE) *ts;
1.115 +
1.116 + ts = (STACK_OF(CONF_VALUE) *)section->value;
1.117 +
1.118 + value->section=section->section;
1.119 + if (!sk_CONF_VALUE_push(ts,value))
1.120 + {
1.121 + return 0;
1.122 + }
1.123 +
1.124 + v = (CONF_VALUE *)lh_insert(conf->data, value);
1.125 + if (v != NULL)
1.126 + {
1.127 + (void)sk_CONF_VALUE_delete_ptr(ts,v);
1.128 + OPENSSL_free(v->name);
1.129 + OPENSSL_free(v->value);
1.130 + OPENSSL_free(v);
1.131 + }
1.132 + return 1;
1.133 + }
1.134 +
1.135 +EXPORT_C char *_CONF_get_string(const CONF *conf, const char *section, const char *name)
1.136 + {
1.137 + CONF_VALUE *v,vv;
1.138 + char *p;
1.139 +
1.140 + if (name == NULL) return(NULL);
1.141 + if (conf != NULL)
1.142 + {
1.143 + if (section != NULL)
1.144 + {
1.145 + vv.name=(char *)name;
1.146 + vv.section=(char *)section;
1.147 + v=(CONF_VALUE *)lh_retrieve(conf->data,&vv);
1.148 + if (v != NULL) return(v->value);
1.149 + if (strcmp(section,"ENV") == 0)
1.150 + {
1.151 + p=Getenv(name);
1.152 + if (p != NULL) return(p);
1.153 + }
1.154 + }
1.155 + vv.section="default";
1.156 + vv.name=(char *)name;
1.157 + v=(CONF_VALUE *)lh_retrieve(conf->data,&vv);
1.158 + if (v != NULL)
1.159 + return(v->value);
1.160 + else
1.161 + return(NULL);
1.162 + }
1.163 + else
1.164 + return(Getenv(name));
1.165 + }
1.166 +
1.167 +#if 0 /* There's no way to provide error checking with this function, so
1.168 + force implementors of the higher levels to get a string and read
1.169 + the number themselves. */
1.170 +EXPORT_C long _CONF_get_number(CONF *conf, char *section, char *name)
1.171 + {
1.172 + char *str;
1.173 + long ret=0;
1.174 +
1.175 + str=_CONF_get_string(conf,section,name);
1.176 + if (str == NULL) return(0);
1.177 + for (;;)
1.178 + {
1.179 + if (conf->meth->is_number(conf, *str))
1.180 + ret=ret*10+conf->meth->to_int(conf, *str);
1.181 + else
1.182 + return(ret);
1.183 + str++;
1.184 + }
1.185 + }
1.186 +#endif
1.187 +
1.188 +EXPORT_C int _CONF_new_data(CONF *conf)
1.189 + {
1.190 + if (conf == NULL)
1.191 + {
1.192 + return 0;
1.193 + }
1.194 + if (conf->data == NULL)
1.195 + if ((conf->data = lh_new(hash, cmp_conf)) == NULL)
1.196 + {
1.197 + return 0;
1.198 + }
1.199 + return 1;
1.200 + }
1.201 +
1.202 +EXPORT_C void _CONF_free_data(CONF *conf)
1.203 + {
1.204 + if (conf == NULL || conf->data == NULL) return;
1.205 +
1.206 + conf->data->down_load=0; /* evil thing to make sure the 'OPENSSL_free()'
1.207 + * works as expected */
1.208 + lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(value_free_hash),
1.209 + conf->data);
1.210 +
1.211 + /* We now have only 'section' entries in the hash table.
1.212 + * Due to problems with */
1.213 +
1.214 + lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(value_free_stack),
1.215 + conf->data);
1.216 + lh_free(conf->data);
1.217 + }
1.218 +
1.219 +static void value_free_hash(CONF_VALUE *a, LHASH *conf)
1.220 + {
1.221 + if (a->name != NULL)
1.222 + {
1.223 + a=(CONF_VALUE *)lh_delete(conf,a);
1.224 + }
1.225 + }
1.226 +
1.227 +static void value_free_stack(CONF_VALUE *a, LHASH *conf)
1.228 + {
1.229 + CONF_VALUE *vv;
1.230 + STACK *sk;
1.231 + int i;
1.232 +
1.233 + if (a->name != NULL) return;
1.234 +
1.235 + sk=(STACK *)a->value;
1.236 + for (i=sk_num(sk)-1; i>=0; i--)
1.237 + {
1.238 + vv=(CONF_VALUE *)sk_value(sk,i);
1.239 + OPENSSL_free(vv->value);
1.240 + OPENSSL_free(vv->name);
1.241 + OPENSSL_free(vv);
1.242 + }
1.243 + if (sk != NULL) sk_free(sk);
1.244 + OPENSSL_free(a->section);
1.245 + OPENSSL_free(a);
1.246 + }
1.247 +
1.248 +/* static unsigned long hash(CONF_VALUE *v) */
1.249 +static unsigned long hash(const void *v_void)
1.250 + {
1.251 + CONF_VALUE *v = (CONF_VALUE *)v_void;
1.252 + return((lh_strhash(v->section)<<2)^lh_strhash(v->name));
1.253 + }
1.254 +
1.255 +/* static int cmp_conf(CONF_VALUE *a, CONF_VALUE *b) */
1.256 +static int cmp_conf(const void *a_void,const void *b_void)
1.257 + {
1.258 + int i;
1.259 + CONF_VALUE *a = (CONF_VALUE *)a_void;
1.260 + CONF_VALUE *b = (CONF_VALUE *)b_void;
1.261 +
1.262 + if (a->section != b->section)
1.263 + {
1.264 + i=strcmp(a->section,b->section);
1.265 + if (i) return(i);
1.266 + }
1.267 +
1.268 + if ((a->name != NULL) && (b->name != NULL))
1.269 + {
1.270 + i=strcmp(a->name,b->name);
1.271 + return(i);
1.272 + }
1.273 + else if (a->name == b->name)
1.274 + return(0);
1.275 + else
1.276 + return((a->name == NULL)?-1:1);
1.277 + }
1.278 +
1.279 +/* Up until OpenSSL 0.9.5a, this was new_section */
1.280 +EXPORT_C CONF_VALUE *_CONF_new_section(CONF *conf, const char *section)
1.281 + {
1.282 + STACK *sk=NULL;
1.283 + int ok=0,i;
1.284 + CONF_VALUE *v=NULL,*vv;
1.285 +
1.286 + if ((sk=sk_new_null()) == NULL)
1.287 + goto err;
1.288 + if ((v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))) == NULL)
1.289 + goto err;
1.290 + i=strlen(section)+1;
1.291 + if ((v->section=(char *)OPENSSL_malloc(i)) == NULL)
1.292 + goto err;
1.293 +
1.294 + memcpy(v->section,section,i);
1.295 + v->name=NULL;
1.296 + v->value=(char *)sk;
1.297 +
1.298 + vv=(CONF_VALUE *)lh_insert(conf->data,v);
1.299 + assert(vv == NULL);
1.300 + ok=1;
1.301 +err:
1.302 + if (!ok)
1.303 + {
1.304 + if (sk != NULL) sk_free(sk);
1.305 + if (v != NULL) OPENSSL_free(v);
1.306 + v=NULL;
1.307 + }
1.308 + return(v);
1.309 + }
1.310 +
1.311 +IMPLEMENT_STACK_OF(CONF_VALUE)