sl@0: /* x509_vpm.c */ sl@0: /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL sl@0: * project 2004. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 2004 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: #include sl@0: sl@0: #include "cryptlib.h" 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: /* X509_VERIFY_PARAM functions */ sl@0: sl@0: static void x509_verify_param_zero(X509_VERIFY_PARAM *param) sl@0: { sl@0: if (!param) sl@0: return; sl@0: param->name = NULL; sl@0: param->purpose = 0; sl@0: param->trust = 0; sl@0: param->inh_flags = X509_VP_FLAG_DEFAULT; sl@0: param->flags = 0; sl@0: param->depth = -1; sl@0: if (param->policies) sl@0: { sl@0: sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free); sl@0: param->policies = NULL; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void) sl@0: { sl@0: X509_VERIFY_PARAM *param; sl@0: param = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM)); sl@0: #ifdef SYMBIAN sl@0: if(param==NULL) sl@0: return param; sl@0: #endif sl@0: memset(param, 0, sizeof(X509_VERIFY_PARAM)); sl@0: x509_verify_param_zero(param); sl@0: return param; sl@0: } sl@0: sl@0: EXPORT_C void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param) sl@0: { sl@0: x509_verify_param_zero(param); sl@0: OPENSSL_free(param); sl@0: } sl@0: sl@0: /* This function determines how parameters are "inherited" from one structure sl@0: * to another. There are several different ways this can happen. sl@0: * sl@0: * 1. If a child structure needs to have its values initialized from a parent sl@0: * they are simply copied across. For example SSL_CTX copied to SSL. sl@0: * 2. If the structure should take on values only if they are currently unset. sl@0: * For example the values in an SSL structure will take appropriate value sl@0: * for SSL servers or clients but only if the application has not set new sl@0: * ones. sl@0: * sl@0: * The "inh_flags" field determines how this function behaves. sl@0: * sl@0: * Normally any values which are set in the default are not copied from the sl@0: * destination and verify flags are ORed together. sl@0: * sl@0: * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied sl@0: * to the destination. Effectively the values in "to" become default values sl@0: * which will be used only if nothing new is set in "from". sl@0: * sl@0: * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether sl@0: * they are set or not. Flags is still Ored though. sl@0: * sl@0: * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead sl@0: * of ORed. sl@0: * sl@0: * If X509_VP_FLAG_LOCKED is set then no values are copied. sl@0: * sl@0: * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed sl@0: * after the next call. sl@0: */ sl@0: sl@0: /* Macro to test if a field should be copied from src to dest */ sl@0: sl@0: #define test_x509_verify_param_copy(field, def) \ sl@0: (to_overwrite || \ sl@0: ((src->field != def) && (to_default || (dest->field == def)))) sl@0: sl@0: /* Macro to test and copy a field if necessary */ sl@0: sl@0: #define x509_verify_param_copy(field, def) \ sl@0: if (test_x509_verify_param_copy(field, def)) \ sl@0: dest->field = src->field sl@0: sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest, sl@0: const X509_VERIFY_PARAM *src) sl@0: { sl@0: unsigned long inh_flags; sl@0: int to_default, to_overwrite; sl@0: if (!src) sl@0: return 1; sl@0: inh_flags = dest->inh_flags | src->inh_flags; sl@0: sl@0: if (inh_flags & X509_VP_FLAG_ONCE) sl@0: dest->inh_flags = 0; sl@0: sl@0: if (inh_flags & X509_VP_FLAG_LOCKED) sl@0: return 1; sl@0: sl@0: if (inh_flags & X509_VP_FLAG_DEFAULT) sl@0: to_default = 1; sl@0: else sl@0: to_default = 0; sl@0: sl@0: if (inh_flags & X509_VP_FLAG_OVERWRITE) sl@0: to_overwrite = 1; sl@0: else sl@0: to_overwrite = 0; sl@0: sl@0: x509_verify_param_copy(purpose, 0); sl@0: x509_verify_param_copy(trust, 0); sl@0: x509_verify_param_copy(depth, -1); sl@0: /* If overwrite or check time not set, copy across */ sl@0: sl@0: if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) sl@0: { sl@0: dest->check_time = src->check_time; sl@0: dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME; sl@0: /* Don't need to copy flag: that is done below */ sl@0: } sl@0: sl@0: sl@0: if (inh_flags & X509_VP_FLAG_RESET_FLAGS) sl@0: dest->flags = 0; sl@0: sl@0: dest->flags |= src->flags; sl@0: sl@0: if (test_x509_verify_param_copy(policies, NULL)) sl@0: { sl@0: if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies)) sl@0: return 0; sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, sl@0: const X509_VERIFY_PARAM *from) sl@0: { sl@0: to->inh_flags |= X509_VP_FLAG_DEFAULT; sl@0: return X509_VERIFY_PARAM_inherit(to, from); sl@0: } sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name) sl@0: { sl@0: if (param->name) sl@0: OPENSSL_free(param->name); sl@0: param->name = BUF_strdup(name); sl@0: if (param->name) sl@0: return 1; sl@0: return 0; sl@0: } sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags) sl@0: { sl@0: param->flags |= flags; sl@0: if (flags & X509_V_FLAG_POLICY_MASK) sl@0: param->flags |= X509_V_FLAG_POLICY_CHECK; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags) sl@0: { sl@0: param->flags &= ~flags; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param) sl@0: { sl@0: return param->flags; sl@0: } sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose) sl@0: { sl@0: return X509_PURPOSE_set(¶m->purpose, purpose); sl@0: } sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust) sl@0: { sl@0: return X509_TRUST_set(¶m->trust, trust); sl@0: } sl@0: sl@0: EXPORT_C void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth) sl@0: { sl@0: param->depth = depth; sl@0: } sl@0: sl@0: EXPORT_C void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t) sl@0: { sl@0: param->check_time = t; sl@0: param->flags |= X509_V_FLAG_USE_CHECK_TIME; sl@0: } sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy) sl@0: { sl@0: if (!param->policies) sl@0: { sl@0: param->policies = sk_ASN1_OBJECT_new_null(); sl@0: if (!param->policies) sl@0: return 0; sl@0: } sl@0: if (!sk_ASN1_OBJECT_push(param->policies, policy)) sl@0: return 0; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param, sl@0: STACK_OF(ASN1_OBJECT) *policies) sl@0: { sl@0: int i; sl@0: ASN1_OBJECT *oid, *doid; sl@0: if (!param) sl@0: return 0; sl@0: if (param->policies) sl@0: sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free); sl@0: sl@0: if (!policies) sl@0: { sl@0: param->policies = NULL; sl@0: return 1; sl@0: } sl@0: sl@0: param->policies = sk_ASN1_OBJECT_new_null(); sl@0: if (!param->policies) sl@0: return 0; sl@0: sl@0: for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) sl@0: { sl@0: oid = sk_ASN1_OBJECT_value(policies, i); sl@0: doid = OBJ_dup(oid); sl@0: if (!doid) sl@0: return 0; sl@0: if (!sk_ASN1_OBJECT_push(param->policies, doid)) sl@0: { sl@0: ASN1_OBJECT_free(doid); sl@0: return 0; sl@0: } sl@0: } sl@0: param->flags |= X509_V_FLAG_POLICY_CHECK; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param) sl@0: { sl@0: return param->depth; sl@0: } sl@0: sl@0: /* Default verify parameters: these are used for various sl@0: * applications and can be overridden by the user specified table. sl@0: * NB: the 'name' field *must* be in alphabetical order because it sl@0: * will be searched using OBJ_search. sl@0: */ sl@0: sl@0: static const X509_VERIFY_PARAM default_table[] = { sl@0: { sl@0: "default", /* X509 default parameters */ sl@0: 0, /* Check time */ sl@0: 0, /* internal flags */ sl@0: 0, /* flags */ sl@0: 0, /* purpose */ sl@0: 0, /* trust */ sl@0: 9, /* depth */ sl@0: NULL /* policies */ sl@0: }, sl@0: { sl@0: "pkcs7", /* SSL/TLS client parameters */ sl@0: 0, /* Check time */ sl@0: 0, /* internal flags */ sl@0: 0, /* flags */ sl@0: X509_PURPOSE_SMIME_SIGN, /* purpose */ sl@0: X509_TRUST_EMAIL, /* trust */ sl@0: -1, /* depth */ sl@0: NULL /* policies */ sl@0: }, sl@0: { sl@0: "ssl_client", /* SSL/TLS client parameters */ sl@0: 0, /* Check time */ sl@0: 0, /* internal flags */ sl@0: 0, /* flags */ sl@0: X509_PURPOSE_SSL_CLIENT, /* purpose */ sl@0: X509_TRUST_SSL_CLIENT, /* trust */ sl@0: -1, /* depth */ sl@0: NULL /* policies */ sl@0: }, sl@0: { sl@0: "ssl_server", /* SSL/TLS server parameters */ sl@0: 0, /* Check time */ sl@0: 0, /* internal flags */ sl@0: 0, /* flags */ sl@0: X509_PURPOSE_SSL_SERVER, /* purpose */ sl@0: X509_TRUST_SSL_SERVER, /* trust */ sl@0: -1, /* depth */ sl@0: NULL /* policies */ sl@0: }}; sl@0: sl@0: #ifndef EMULATOR sl@0: static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(param_table,x509_vpm,STACK_OF(X509_VERIFY_PARAM) *) sl@0: #define param_table (*GET_WSD_VAR_NAME(param_table,x509_vpm, s)()) sl@0: #endif sl@0: sl@0: static int table_cmp(const void *pa, const void *pb) sl@0: { sl@0: const X509_VERIFY_PARAM *a = pa, *b = pb; sl@0: return strcmp(a->name, b->name); sl@0: } sl@0: sl@0: static int param_cmp(const X509_VERIFY_PARAM * const *a, sl@0: const X509_VERIFY_PARAM * const *b) sl@0: { sl@0: return strcmp((*a)->name, (*b)->name); sl@0: } sl@0: sl@0: EXPORT_C int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param) sl@0: { sl@0: int idx; sl@0: X509_VERIFY_PARAM *ptmp; sl@0: if (!param_table) sl@0: { sl@0: param_table = sk_X509_VERIFY_PARAM_new(param_cmp); sl@0: if (!param_table) sl@0: return 0; sl@0: } sl@0: else sl@0: { sl@0: idx = sk_X509_VERIFY_PARAM_find(param_table, param); sl@0: if (idx != -1) sl@0: { sl@0: ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx); sl@0: X509_VERIFY_PARAM_free(ptmp); sl@0: (void)sk_X509_VERIFY_PARAM_delete(param_table, idx); sl@0: } sl@0: } sl@0: if (!sk_X509_VERIFY_PARAM_push(param_table, param)) sl@0: return 0; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name) sl@0: { sl@0: int idx; sl@0: X509_VERIFY_PARAM pm; sl@0: pm.name = (char *)name; sl@0: if (param_table) sl@0: { sl@0: idx = sk_X509_VERIFY_PARAM_find(param_table, &pm); sl@0: if (idx != -1) sl@0: return sk_X509_VERIFY_PARAM_value(param_table, idx); sl@0: } sl@0: return (const X509_VERIFY_PARAM *) OBJ_bsearch((char *)&pm, sl@0: (char *)&default_table, sl@0: sizeof(default_table)/sizeof(X509_VERIFY_PARAM), sl@0: sizeof(X509_VERIFY_PARAM), sl@0: table_cmp); sl@0: } sl@0: sl@0: EXPORT_C void X509_VERIFY_PARAM_table_cleanup(void) sl@0: { sl@0: if (param_table) sl@0: sk_X509_VERIFY_PARAM_pop_free(param_table, sl@0: X509_VERIFY_PARAM_free); sl@0: param_table = NULL; sl@0: }