sl@0: /* x509_trs.c */ sl@0: /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL sl@0: * project 1999. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1999 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: #include "cryptlib.h" 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: static int tr_cmp(const X509_TRUST * const *a, sl@0: const X509_TRUST * const *b); sl@0: static void trtable_free(X509_TRUST *p); sl@0: sl@0: static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags); sl@0: static int trust_1oid(X509_TRUST *trust, X509 *x, int flags); sl@0: static int trust_compat(X509_TRUST *trust, X509 *x, int flags); sl@0: sl@0: static int obj_trust(int id, X509 *x, int flags); sl@0: static int (*default_trust)(int id, X509 *x, int flags) = obj_trust; sl@0: sl@0: sl@0: #ifndef EMULATOR sl@0: /* WARNING: the following table should be kept in order of trust sl@0: * and without any gaps so we can just subtract the minimum trust sl@0: * value to get an index into the table sl@0: */ sl@0: sl@0: static X509_TRUST trstandard[] = { sl@0: {X509_TRUST_COMPAT, 0, trust_compat, "compatible", 0, NULL}, sl@0: {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, "SSL Client", NID_client_auth, NULL}, sl@0: {X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth, NULL}, sl@0: {X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect, NULL}, sl@0: {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign, NULL}, sl@0: {X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign, NULL}, sl@0: {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP, NULL} sl@0: }; sl@0: #else sl@0: static const X509_TRUST trstandard[] = { sl@0: {X509_TRUST_COMPAT, 0, trust_compat, "compatible", 0, NULL}, sl@0: {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, "SSL Client", NID_client_auth, NULL}, sl@0: {X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth, NULL}, sl@0: {X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect, NULL}, sl@0: {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign, NULL}, sl@0: {X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign, NULL}, sl@0: {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP, NULL} sl@0: }; sl@0: #endif sl@0: #define X509_TRUST_COUNT (sizeof(trstandard)/sizeof(X509_TRUST)) sl@0: sl@0: IMPLEMENT_STACK_OF(X509_TRUST) sl@0: #ifndef EMULATOR sl@0: static STACK_OF(X509_TRUST) *trtable = NULL; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(trtable,x509_trs,STACK_OF(X509_TRUST) *) sl@0: #define trtable (*GET_WSD_VAR_NAME(trtable,x509_trs, s)()) sl@0: #endif sl@0: sl@0: static int tr_cmp(const X509_TRUST * const *a, sl@0: const X509_TRUST * const *b) sl@0: { sl@0: return (*a)->trust - (*b)->trust; sl@0: } sl@0: sl@0: EXPORT_C int (*X509_TRUST_set_default(int (*trust)(int , X509 *, int)))(int, X509 *, int) sl@0: { sl@0: int (*oldtrust)(int , X509 *, int); sl@0: oldtrust = default_trust; sl@0: default_trust = trust; sl@0: return oldtrust; sl@0: } sl@0: sl@0: sl@0: EXPORT_C int X509_check_trust(X509 *x, int id, int flags) sl@0: { sl@0: X509_TRUST *pt; sl@0: int idx; sl@0: if(id == -1) return 1; sl@0: idx = X509_TRUST_get_by_id(id); sl@0: if(idx == -1) return default_trust(id, x, flags); sl@0: pt = X509_TRUST_get0(idx); sl@0: return pt->check_trust(pt, x, flags); sl@0: } sl@0: sl@0: EXPORT_C int X509_TRUST_get_count(void) sl@0: { sl@0: if(!trtable) return X509_TRUST_COUNT; sl@0: return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT; sl@0: } sl@0: sl@0: EXPORT_C X509_TRUST * X509_TRUST_get0(int idx) sl@0: { sl@0: if(idx < 0) return NULL; sl@0: if(idx < (int)X509_TRUST_COUNT) return ((X509_TRUST *)trstandard + idx); sl@0: return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT); sl@0: } sl@0: sl@0: EXPORT_C int X509_TRUST_get_by_id(int id) sl@0: { sl@0: X509_TRUST tmp; sl@0: int idx; sl@0: if((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX)) sl@0: return id - X509_TRUST_MIN; sl@0: tmp.trust = id; sl@0: if(!trtable) return -1; sl@0: idx = sk_X509_TRUST_find(trtable, &tmp); sl@0: if(idx == -1) return -1; sl@0: return idx + X509_TRUST_COUNT; sl@0: } sl@0: sl@0: EXPORT_C int X509_TRUST_set(int *t, int trust) sl@0: { sl@0: if(X509_TRUST_get_by_id(trust) == -1) { sl@0: X509err(X509_F_X509_TRUST_SET, X509_R_INVALID_TRUST); sl@0: return 0; sl@0: } sl@0: *t = trust; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int X509_TRUST_add(int id, int flags, int (*ck)(X509_TRUST *, X509 *, int), sl@0: char *name, int arg1, void *arg2) sl@0: { sl@0: int idx; sl@0: X509_TRUST *trtmp; sl@0: /* This is set according to what we change: application can't set it */ sl@0: flags &= ~X509_TRUST_DYNAMIC; sl@0: /* This will always be set for application modified trust entries */ sl@0: flags |= X509_TRUST_DYNAMIC_NAME; sl@0: /* Get existing entry if any */ sl@0: idx = X509_TRUST_get_by_id(id); sl@0: /* Need a new entry */ sl@0: if(idx == -1) { sl@0: if(!(trtmp = OPENSSL_malloc(sizeof(X509_TRUST)))) { sl@0: X509err(X509_F_X509_TRUST_ADD,ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: trtmp->flags = X509_TRUST_DYNAMIC; sl@0: } else trtmp = X509_TRUST_get0(idx); sl@0: sl@0: /* OPENSSL_free existing name if dynamic */ sl@0: if(trtmp->flags & X509_TRUST_DYNAMIC_NAME) OPENSSL_free(trtmp->name); sl@0: /* dup supplied name */ sl@0: if(!(trtmp->name = BUF_strdup(name))) { sl@0: X509err(X509_F_X509_TRUST_ADD,ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: /* Keep the dynamic flag of existing entry */ sl@0: trtmp->flags &= X509_TRUST_DYNAMIC; sl@0: /* Set all other flags */ sl@0: trtmp->flags |= flags; sl@0: sl@0: trtmp->trust = id; sl@0: trtmp->check_trust = ck; sl@0: trtmp->arg1 = arg1; sl@0: trtmp->arg2 = arg2; sl@0: sl@0: /* If its a new entry manage the dynamic table */ sl@0: if(idx == -1) { sl@0: if(!trtable && !(trtable = sk_X509_TRUST_new(tr_cmp))) { sl@0: X509err(X509_F_X509_TRUST_ADD,ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: if (!sk_X509_TRUST_push(trtable, trtmp)) { sl@0: X509err(X509_F_X509_TRUST_ADD,ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: static void trtable_free(X509_TRUST *p) sl@0: { sl@0: if(!p) return; sl@0: if (p->flags & X509_TRUST_DYNAMIC) sl@0: { sl@0: if (p->flags & X509_TRUST_DYNAMIC_NAME) sl@0: OPENSSL_free(p->name); sl@0: OPENSSL_free(p); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void X509_TRUST_cleanup(void) sl@0: { sl@0: unsigned int i; sl@0: for(i = 0; i < X509_TRUST_COUNT; i++) trtable_free((X509_TRUST *)trstandard + i); sl@0: sk_X509_TRUST_pop_free(trtable, trtable_free); sl@0: trtable = NULL; sl@0: } sl@0: sl@0: EXPORT_C int X509_TRUST_get_flags(X509_TRUST *xp) sl@0: { sl@0: return xp->flags; sl@0: } sl@0: sl@0: EXPORT_C char *X509_TRUST_get0_name(X509_TRUST *xp) sl@0: { sl@0: return xp->name; sl@0: } sl@0: sl@0: EXPORT_C int X509_TRUST_get_trust(X509_TRUST *xp) sl@0: { sl@0: return xp->trust; sl@0: } sl@0: sl@0: static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags) sl@0: { sl@0: if(x->aux && (x->aux->trust || x->aux->reject)) sl@0: return obj_trust(trust->arg1, x, flags); sl@0: /* we don't have any trust settings: for compatibility sl@0: * we return trusted if it is self signed sl@0: */ sl@0: return trust_compat(trust, x, flags); sl@0: } sl@0: sl@0: static int trust_1oid(X509_TRUST *trust, X509 *x, int flags) sl@0: { sl@0: if(x->aux) return obj_trust(trust->arg1, x, flags); sl@0: return X509_TRUST_UNTRUSTED; sl@0: } sl@0: sl@0: static int trust_compat(X509_TRUST *trust, X509 *x, int flags) sl@0: { sl@0: X509_check_purpose(x, -1, 0); sl@0: if(x->ex_flags & EXFLAG_SS) return X509_TRUST_TRUSTED; sl@0: else return X509_TRUST_UNTRUSTED; sl@0: } sl@0: sl@0: static int obj_trust(int id, X509 *x, int flags) sl@0: { sl@0: ASN1_OBJECT *obj; sl@0: int i; sl@0: X509_CERT_AUX *ax; sl@0: ax = x->aux; sl@0: if(!ax) return X509_TRUST_UNTRUSTED; sl@0: if(ax->reject) { sl@0: for(i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) { sl@0: obj = sk_ASN1_OBJECT_value(ax->reject, i); sl@0: if(OBJ_obj2nid(obj) == id) return X509_TRUST_REJECTED; sl@0: } sl@0: } sl@0: if(ax->trust) { sl@0: for(i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) { sl@0: obj = sk_ASN1_OBJECT_value(ax->trust, i); sl@0: if(OBJ_obj2nid(obj) == id) return X509_TRUST_TRUSTED; sl@0: } sl@0: } sl@0: return X509_TRUST_UNTRUSTED; sl@0: } sl@0: