sl@0: /* conf_lib.c */ sl@0: /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL sl@0: * project 2000. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 2000 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: sl@0: const char CONF_version[]="CONF" OPENSSL_VERSION_PTEXT; sl@0: sl@0: #ifndef EMULATOR sl@0: static CONF_METHOD *default_CONF_method=NULL; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(default_CONF_method,conf_lib,const CONF_METHOD *) sl@0: #define default_CONF_method (*GET_WSD_VAR_NAME(default_CONF_method,conf_lib, s)()) sl@0: #endif sl@0: sl@0: /* Init a 'CONF' structure from an old LHASH */ sl@0: sl@0: EXPORT_C void CONF_set_nconf(CONF *conf, LHASH *hash) sl@0: { sl@0: if (default_CONF_method == NULL) sl@0: default_CONF_method = NCONF_default(); sl@0: sl@0: default_CONF_method->init(conf); sl@0: conf->data = hash; sl@0: } sl@0: sl@0: /* The following section contains the "CONF classic" functions, sl@0: rewritten in terms of the new CONF interface. */ sl@0: sl@0: EXPORT_C int CONF_set_default_method(CONF_METHOD *meth) sl@0: { sl@0: default_CONF_method = meth; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C LHASH *CONF_load(LHASH *conf, const char *file, long *eline) sl@0: { sl@0: LHASH *ltmp; sl@0: BIO *in=NULL; sl@0: sl@0: #ifdef OPENSSL_SYS_VMS sl@0: in=BIO_new_file(file, "r"); sl@0: #else sl@0: in=BIO_new_file(file, "rb"); sl@0: #endif sl@0: if (in == NULL) sl@0: { sl@0: CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB); sl@0: return NULL; sl@0: } sl@0: sl@0: ltmp = CONF_load_bio(conf, in, eline); sl@0: BIO_free(in); sl@0: sl@0: return ltmp; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_FP_API sl@0: EXPORT_C LHASH *CONF_load_fp(LHASH *conf, FILE *fp,long *eline) sl@0: { sl@0: BIO *btmp; sl@0: LHASH *ltmp; sl@0: if(!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) { sl@0: CONFerr(CONF_F_CONF_LOAD_FP,ERR_R_BUF_LIB); sl@0: return NULL; sl@0: } sl@0: ltmp = CONF_load_bio(conf, btmp, eline); sl@0: BIO_free(btmp); sl@0: return ltmp; sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C LHASH *CONF_load_bio(LHASH *conf, BIO *bp,long *eline) sl@0: { sl@0: CONF ctmp; sl@0: int ret; sl@0: sl@0: CONF_set_nconf(&ctmp, conf); sl@0: sl@0: ret = NCONF_load_bio(&ctmp, bp, eline); sl@0: if (ret) sl@0: return ctmp.data; sl@0: return NULL; sl@0: } sl@0: sl@0: EXPORT_C STACK_OF(CONF_VALUE) *CONF_get_section(LHASH *conf,const char *section) sl@0: { sl@0: if (conf == NULL) sl@0: { sl@0: return NULL; sl@0: } sl@0: else sl@0: { sl@0: CONF ctmp; sl@0: CONF_set_nconf(&ctmp, conf); sl@0: return NCONF_get_section(&ctmp, section); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C char *CONF_get_string(LHASH *conf,const char *group,const char *name) sl@0: { sl@0: if (conf == NULL) sl@0: { sl@0: return NCONF_get_string(NULL, group, name); sl@0: } sl@0: else sl@0: { sl@0: CONF ctmp; sl@0: CONF_set_nconf(&ctmp, conf); sl@0: return NCONF_get_string(&ctmp, group, name); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C long CONF_get_number(LHASH *conf,const char *group,const char *name) sl@0: { sl@0: int status; sl@0: long result = 0; sl@0: sl@0: if (conf == NULL) sl@0: { sl@0: status = NCONF_get_number_e(NULL, group, name, &result); sl@0: } sl@0: else sl@0: { sl@0: CONF ctmp; sl@0: CONF_set_nconf(&ctmp, conf); sl@0: status = NCONF_get_number_e(&ctmp, group, name, &result); sl@0: } sl@0: sl@0: if (status == 0) sl@0: { sl@0: /* This function does not believe in errors... */ sl@0: ERR_clear_error(); sl@0: } sl@0: return result; sl@0: } sl@0: sl@0: EXPORT_C void CONF_free(LHASH *conf) sl@0: { sl@0: CONF ctmp; sl@0: CONF_set_nconf(&ctmp, conf); sl@0: NCONF_free_data(&ctmp); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_FP_API sl@0: EXPORT_C int CONF_dump_fp(LHASH *conf, FILE *out) sl@0: { sl@0: BIO *btmp; sl@0: int ret; sl@0: sl@0: if(!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) { sl@0: CONFerr(CONF_F_CONF_DUMP_FP,ERR_R_BUF_LIB); sl@0: return 0; sl@0: } sl@0: ret = CONF_dump_bio(conf, btmp); sl@0: BIO_free(btmp); sl@0: return ret; sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int CONF_dump_bio(LHASH *conf, BIO *out) sl@0: { sl@0: CONF ctmp; sl@0: CONF_set_nconf(&ctmp, conf); sl@0: return NCONF_dump_bio(&ctmp, out); sl@0: } sl@0: sl@0: /* The following section contains the "New CONF" functions. They are sl@0: completely centralised around a new CONF structure that may contain sl@0: basically anything, but at least a method pointer and a table of data. sl@0: These functions are also written in terms of the bridge functions used sl@0: by the "CONF classic" functions, for consistency. */ sl@0: sl@0: EXPORT_C CONF *NCONF_new(CONF_METHOD *meth) sl@0: { sl@0: CONF *ret; sl@0: sl@0: if (meth == NULL) sl@0: meth = NCONF_default(); sl@0: sl@0: ret = meth->create(meth); sl@0: if (ret == NULL) sl@0: { sl@0: CONFerr(CONF_F_NCONF_NEW,ERR_R_MALLOC_FAILURE); sl@0: return(NULL); sl@0: } sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C void NCONF_free(CONF *conf) sl@0: { sl@0: if (conf == NULL) sl@0: return; sl@0: conf->meth->destroy(conf); sl@0: } sl@0: sl@0: EXPORT_C void NCONF_free_data(CONF *conf) sl@0: { sl@0: if (conf == NULL) sl@0: return; sl@0: conf->meth->destroy_data(conf); sl@0: } sl@0: sl@0: EXPORT_C int NCONF_load(CONF *conf, const char *file, long *eline) sl@0: { sl@0: if (conf == NULL) sl@0: { sl@0: CONFerr(CONF_F_NCONF_LOAD,CONF_R_NO_CONF); sl@0: return 0; sl@0: } sl@0: sl@0: return conf->meth->load(conf, file, eline); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_FP_API sl@0: EXPORT_C int NCONF_load_fp(CONF *conf, FILE *fp,long *eline) sl@0: { sl@0: BIO *btmp; sl@0: int ret; sl@0: if(!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) sl@0: { sl@0: CONFerr(CONF_F_NCONF_LOAD_FP,ERR_R_BUF_LIB); sl@0: return 0; sl@0: } sl@0: ret = NCONF_load_bio(conf, btmp, eline); sl@0: BIO_free(btmp); sl@0: return ret; sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int NCONF_load_bio(CONF *conf, BIO *bp,long *eline) sl@0: { sl@0: if (conf == NULL) sl@0: { sl@0: CONFerr(CONF_F_NCONF_LOAD_BIO,CONF_R_NO_CONF); sl@0: return 0; sl@0: } sl@0: sl@0: return conf->meth->load_bio(conf, bp, eline); sl@0: } sl@0: sl@0: EXPORT_C STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf,const char *section) sl@0: { sl@0: if (conf == NULL) sl@0: { sl@0: CONFerr(CONF_F_NCONF_GET_SECTION,CONF_R_NO_CONF); sl@0: return NULL; sl@0: } sl@0: sl@0: if (section == NULL) sl@0: { sl@0: CONFerr(CONF_F_NCONF_GET_SECTION,CONF_R_NO_SECTION); sl@0: return NULL; sl@0: } sl@0: sl@0: return _CONF_get_section_values(conf, section); sl@0: } sl@0: sl@0: EXPORT_C char *NCONF_get_string(const CONF *conf,const char *group,const char *name) sl@0: { sl@0: char *s = _CONF_get_string(conf, group, name); sl@0: sl@0: /* Since we may get a value from an environment variable even sl@0: if conf is NULL, let's check the value first */ sl@0: if (s) return s; sl@0: sl@0: if (conf == NULL) sl@0: { sl@0: CONFerr(CONF_F_NCONF_GET_STRING, sl@0: CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE); sl@0: return NULL; sl@0: } sl@0: CONFerr(CONF_F_NCONF_GET_STRING, sl@0: CONF_R_NO_VALUE); sl@0: ERR_add_error_data(4,"group=",group," name=",name); sl@0: return NULL; sl@0: } sl@0: sl@0: EXPORT_C int NCONF_get_number_e(const CONF *conf,const char *group,const char *name, sl@0: long *result) sl@0: { sl@0: char *str; sl@0: sl@0: if (result == NULL) sl@0: { sl@0: CONFerr(CONF_F_NCONF_GET_NUMBER_E,ERR_R_PASSED_NULL_PARAMETER); sl@0: return 0; sl@0: } sl@0: sl@0: str = NCONF_get_string(conf,group,name); sl@0: sl@0: if (str == NULL) sl@0: return 0; sl@0: sl@0: for (*result = 0;conf->meth->is_number(conf, *str);) sl@0: { sl@0: *result = (*result)*10 + conf->meth->to_int(conf, *str); sl@0: str++; sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_FP_API sl@0: EXPORT_C int NCONF_dump_fp(const CONF *conf, FILE *out) sl@0: { sl@0: BIO *btmp; sl@0: int ret; sl@0: if(!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) { sl@0: CONFerr(CONF_F_NCONF_DUMP_FP,ERR_R_BUF_LIB); sl@0: return 0; sl@0: } sl@0: ret = NCONF_dump_bio(conf, btmp); sl@0: BIO_free(btmp); sl@0: return ret; sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int NCONF_dump_bio(const CONF *conf, BIO *out) sl@0: { sl@0: if (conf == NULL) sl@0: { sl@0: CONFerr(CONF_F_NCONF_DUMP_BIO,CONF_R_NO_CONF); sl@0: return 0; sl@0: } sl@0: sl@0: return conf->meth->dump(conf, out); sl@0: } sl@0: sl@0: sl@0: /* This function should be avoided */ sl@0: #if 0 sl@0: long NCONF_get_number(CONF *conf,char *group,char *name) sl@0: { sl@0: int status; sl@0: long ret=0; sl@0: sl@0: status = NCONF_get_number_e(conf, group, name, &ret); sl@0: if (status == 0) sl@0: { sl@0: /* This function does not believe in errors... */ sl@0: ERR_get_error(); sl@0: } sl@0: return ret; sl@0: } sl@0: #endif