ssl/libcrypto/src/crypto/conf/conf_lib.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* conf_lib.c */
       
     2 /* Written by Richard Levitte (richard@levitte.org) 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  /*
       
    59  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
    60  */
       
    61 
       
    62 
       
    63 #include <stdio.h>
       
    64 #include <openssl/crypto.h>
       
    65 #include <openssl/err.h>
       
    66 #include <openssl/conf.h>
       
    67 #include <openssl/conf_api.h>
       
    68 #include <openssl/lhash.h>
       
    69 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
       
    70 #include "libcrypto_wsd_macros.h"
       
    71 #include "libcrypto_wsd.h"
       
    72 #endif
       
    73 
       
    74 
       
    75 const char CONF_version[]="CONF" OPENSSL_VERSION_PTEXT;
       
    76 
       
    77 #ifndef EMULATOR
       
    78 static CONF_METHOD *default_CONF_method=NULL;
       
    79 #else
       
    80 GET_STATIC_VAR_FROM_TLS(default_CONF_method,conf_lib,const CONF_METHOD *)
       
    81 #define default_CONF_method (*GET_WSD_VAR_NAME(default_CONF_method,conf_lib, s)())
       
    82 #endif
       
    83 
       
    84 /* Init a 'CONF' structure from an old LHASH */
       
    85 
       
    86 EXPORT_C void CONF_set_nconf(CONF *conf, LHASH *hash)
       
    87 	{
       
    88 	if (default_CONF_method == NULL)
       
    89 		default_CONF_method = NCONF_default();
       
    90 
       
    91 	default_CONF_method->init(conf);
       
    92 	conf->data = hash;
       
    93 	}
       
    94 
       
    95 /* The following section contains the "CONF classic" functions,
       
    96    rewritten in terms of the new CONF interface. */
       
    97 
       
    98 EXPORT_C int CONF_set_default_method(CONF_METHOD *meth)
       
    99 	{
       
   100 	default_CONF_method = meth;
       
   101 	return 1;
       
   102 	}
       
   103 
       
   104 EXPORT_C LHASH *CONF_load(LHASH *conf, const char *file, long *eline)
       
   105 	{
       
   106 	LHASH *ltmp;
       
   107 	BIO *in=NULL;
       
   108 
       
   109 #ifdef OPENSSL_SYS_VMS
       
   110 	in=BIO_new_file(file, "r");
       
   111 #else
       
   112 	in=BIO_new_file(file, "rb");
       
   113 #endif
       
   114 	if (in == NULL)
       
   115 		{
       
   116 		CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB);
       
   117 		return NULL;
       
   118 		}
       
   119 
       
   120 	ltmp = CONF_load_bio(conf, in, eline);
       
   121 	BIO_free(in);
       
   122 
       
   123 	return ltmp;
       
   124 	}
       
   125 
       
   126 #ifndef OPENSSL_NO_FP_API
       
   127 EXPORT_C LHASH *CONF_load_fp(LHASH *conf, FILE *fp,long *eline)
       
   128 	{
       
   129 	BIO *btmp;
       
   130 	LHASH *ltmp;
       
   131 	if(!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) {
       
   132 		CONFerr(CONF_F_CONF_LOAD_FP,ERR_R_BUF_LIB);
       
   133 		return NULL;
       
   134 	}
       
   135 	ltmp = CONF_load_bio(conf, btmp, eline);
       
   136 	BIO_free(btmp);
       
   137 	return ltmp;
       
   138 	}
       
   139 #endif
       
   140 
       
   141 EXPORT_C LHASH *CONF_load_bio(LHASH *conf, BIO *bp,long *eline)
       
   142 	{
       
   143 	CONF ctmp;
       
   144 	int ret;
       
   145 
       
   146 	CONF_set_nconf(&ctmp, conf);
       
   147 
       
   148 	ret = NCONF_load_bio(&ctmp, bp, eline);
       
   149 	if (ret)
       
   150 		return ctmp.data;
       
   151 	return NULL;
       
   152 	}
       
   153 
       
   154 EXPORT_C STACK_OF(CONF_VALUE) *CONF_get_section(LHASH *conf,const char *section)
       
   155 	{
       
   156 	if (conf == NULL)
       
   157 		{
       
   158 		return NULL;
       
   159 		}
       
   160 	else
       
   161 		{
       
   162 		CONF ctmp;
       
   163 		CONF_set_nconf(&ctmp, conf);
       
   164 		return NCONF_get_section(&ctmp, section);
       
   165 		}
       
   166 	}
       
   167 
       
   168 EXPORT_C char *CONF_get_string(LHASH *conf,const char *group,const char *name)
       
   169 	{
       
   170 	if (conf == NULL)
       
   171 		{
       
   172 		return NCONF_get_string(NULL, group, name);
       
   173 		}
       
   174 	else
       
   175 		{
       
   176 		CONF ctmp;
       
   177 		CONF_set_nconf(&ctmp, conf);
       
   178 		return NCONF_get_string(&ctmp, group, name);
       
   179 		}
       
   180 	}
       
   181 
       
   182 EXPORT_C long CONF_get_number(LHASH *conf,const char *group,const char *name)
       
   183 	{
       
   184 	int status;
       
   185 	long result = 0;
       
   186 
       
   187 	if (conf == NULL)
       
   188 		{
       
   189 		status = NCONF_get_number_e(NULL, group, name, &result);
       
   190 		}
       
   191 	else
       
   192 		{
       
   193 		CONF ctmp;
       
   194 		CONF_set_nconf(&ctmp, conf);
       
   195 		status = NCONF_get_number_e(&ctmp, group, name, &result);
       
   196 		}
       
   197 
       
   198 	if (status == 0)
       
   199 		{
       
   200 		/* This function does not believe in errors... */
       
   201 		ERR_clear_error();
       
   202 		}
       
   203 	return result;
       
   204 	}
       
   205 
       
   206 EXPORT_C void CONF_free(LHASH *conf)
       
   207 	{
       
   208 	CONF ctmp;
       
   209 	CONF_set_nconf(&ctmp, conf);
       
   210 	NCONF_free_data(&ctmp);
       
   211 	}
       
   212 
       
   213 #ifndef OPENSSL_NO_FP_API
       
   214 EXPORT_C int CONF_dump_fp(LHASH *conf, FILE *out)
       
   215 	{
       
   216 	BIO *btmp;
       
   217 	int ret;
       
   218 
       
   219 	if(!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) {
       
   220 		CONFerr(CONF_F_CONF_DUMP_FP,ERR_R_BUF_LIB);
       
   221 		return 0;
       
   222 	}
       
   223 	ret = CONF_dump_bio(conf, btmp);
       
   224 	BIO_free(btmp);
       
   225 	return ret;
       
   226 	}
       
   227 #endif
       
   228 
       
   229 EXPORT_C int CONF_dump_bio(LHASH *conf, BIO *out)
       
   230 	{
       
   231 	CONF ctmp;
       
   232 	CONF_set_nconf(&ctmp, conf);
       
   233 	return NCONF_dump_bio(&ctmp, out);
       
   234 	}
       
   235 
       
   236 /* The following section contains the "New CONF" functions.  They are
       
   237    completely centralised around a new CONF structure that may contain
       
   238    basically anything, but at least a method pointer and a table of data.
       
   239    These functions are also written in terms of the bridge functions used
       
   240    by the "CONF classic" functions, for consistency.  */
       
   241 
       
   242 EXPORT_C CONF *NCONF_new(CONF_METHOD *meth)
       
   243 	{
       
   244 	CONF *ret;
       
   245 
       
   246 	if (meth == NULL)
       
   247 		meth = NCONF_default();
       
   248 
       
   249 	ret = meth->create(meth);
       
   250 	if (ret == NULL)
       
   251 		{
       
   252 		CONFerr(CONF_F_NCONF_NEW,ERR_R_MALLOC_FAILURE);
       
   253 		return(NULL);
       
   254 		}
       
   255 
       
   256 	return ret;
       
   257 	}
       
   258 
       
   259 EXPORT_C void NCONF_free(CONF *conf)
       
   260 	{
       
   261 	if (conf == NULL)
       
   262 		return;
       
   263 	conf->meth->destroy(conf);
       
   264 	}
       
   265 
       
   266 EXPORT_C void NCONF_free_data(CONF *conf)
       
   267 	{
       
   268 	if (conf == NULL)
       
   269 		return;
       
   270 	conf->meth->destroy_data(conf);
       
   271 	}
       
   272 
       
   273 EXPORT_C int NCONF_load(CONF *conf, const char *file, long *eline)
       
   274 	{
       
   275 	if (conf == NULL)
       
   276 		{
       
   277 		CONFerr(CONF_F_NCONF_LOAD,CONF_R_NO_CONF);
       
   278 		return 0;
       
   279 		}
       
   280 
       
   281 	return conf->meth->load(conf, file, eline);
       
   282 	}
       
   283 
       
   284 #ifndef OPENSSL_NO_FP_API
       
   285 EXPORT_C int NCONF_load_fp(CONF *conf, FILE *fp,long *eline)
       
   286 	{
       
   287 	BIO *btmp;
       
   288 	int ret;
       
   289 	if(!(btmp = BIO_new_fp(fp, BIO_NOCLOSE)))
       
   290 		{
       
   291 		CONFerr(CONF_F_NCONF_LOAD_FP,ERR_R_BUF_LIB);
       
   292 		return 0;
       
   293 		}
       
   294 	ret = NCONF_load_bio(conf, btmp, eline);
       
   295 	BIO_free(btmp);
       
   296 	return ret;
       
   297 	}
       
   298 #endif
       
   299 
       
   300 EXPORT_C int NCONF_load_bio(CONF *conf, BIO *bp,long *eline)
       
   301 	{
       
   302 	if (conf == NULL)
       
   303 		{
       
   304 		CONFerr(CONF_F_NCONF_LOAD_BIO,CONF_R_NO_CONF);
       
   305 		return 0;
       
   306 		}
       
   307 
       
   308 	return conf->meth->load_bio(conf, bp, eline);
       
   309 	}
       
   310 
       
   311 EXPORT_C STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf,const char *section)
       
   312 	{
       
   313 	if (conf == NULL)
       
   314 		{
       
   315 		CONFerr(CONF_F_NCONF_GET_SECTION,CONF_R_NO_CONF);
       
   316 		return NULL;
       
   317 		}
       
   318 
       
   319 	if (section == NULL)
       
   320 		{
       
   321 		CONFerr(CONF_F_NCONF_GET_SECTION,CONF_R_NO_SECTION);
       
   322 		return NULL;
       
   323 		}
       
   324 
       
   325 	return _CONF_get_section_values(conf, section);
       
   326 	}
       
   327 
       
   328 EXPORT_C char *NCONF_get_string(const CONF *conf,const char *group,const char *name)
       
   329 	{
       
   330 	char *s = _CONF_get_string(conf, group, name);
       
   331 
       
   332         /* Since we may get a value from an environment variable even
       
   333            if conf is NULL, let's check the value first */
       
   334         if (s) return s;
       
   335 
       
   336 	if (conf == NULL)
       
   337 		{
       
   338 		CONFerr(CONF_F_NCONF_GET_STRING,
       
   339                         CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
       
   340 		return NULL;
       
   341 		}
       
   342 	CONFerr(CONF_F_NCONF_GET_STRING,
       
   343 		CONF_R_NO_VALUE);
       
   344 	ERR_add_error_data(4,"group=",group," name=",name);
       
   345 	return NULL;
       
   346 	}
       
   347 
       
   348 EXPORT_C int NCONF_get_number_e(const CONF *conf,const char *group,const char *name,
       
   349 		       long *result)
       
   350 	{
       
   351 	char *str;
       
   352 
       
   353 	if (result == NULL)
       
   354 		{
       
   355 		CONFerr(CONF_F_NCONF_GET_NUMBER_E,ERR_R_PASSED_NULL_PARAMETER);
       
   356 		return 0;
       
   357 		}
       
   358 
       
   359 	str = NCONF_get_string(conf,group,name);
       
   360 
       
   361 	if (str == NULL)
       
   362 		return 0;
       
   363 
       
   364 	for (*result = 0;conf->meth->is_number(conf, *str);)
       
   365 		{
       
   366 		*result = (*result)*10 + conf->meth->to_int(conf, *str);
       
   367 		str++;
       
   368 		}
       
   369 
       
   370 	return 1;
       
   371 	}
       
   372 
       
   373 #ifndef OPENSSL_NO_FP_API
       
   374 EXPORT_C int NCONF_dump_fp(const CONF *conf, FILE *out)
       
   375 	{
       
   376 	BIO *btmp;
       
   377 	int ret;
       
   378 	if(!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) {
       
   379 		CONFerr(CONF_F_NCONF_DUMP_FP,ERR_R_BUF_LIB);
       
   380 		return 0;
       
   381 	}
       
   382 	ret = NCONF_dump_bio(conf, btmp);
       
   383 	BIO_free(btmp);
       
   384 	return ret;
       
   385 	}
       
   386 #endif
       
   387 
       
   388 EXPORT_C int NCONF_dump_bio(const CONF *conf, BIO *out)
       
   389 	{
       
   390 	if (conf == NULL)
       
   391 		{
       
   392 		CONFerr(CONF_F_NCONF_DUMP_BIO,CONF_R_NO_CONF);
       
   393 		return 0;
       
   394 		}
       
   395 
       
   396 	return conf->meth->dump(conf, out);
       
   397 	}
       
   398 
       
   399 
       
   400 /* This function should be avoided */
       
   401 #if 0
       
   402 long NCONF_get_number(CONF *conf,char *group,char *name)
       
   403 	{
       
   404 	int status;
       
   405 	long ret=0;
       
   406 
       
   407 	status = NCONF_get_number_e(conf, group, name, &ret);
       
   408 	if (status == 0)
       
   409 		{
       
   410 		/* This function does not believe in errors... */
       
   411 		ERR_get_error();
       
   412 		}
       
   413 	return ret;
       
   414 	}
       
   415 #endif