1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/x509v3/v3_lib.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,316 @@
1.4 +/* v3_lib.c */
1.5 +/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
1.6 + * project 1999.
1.7 + */
1.8 +/* ====================================================================
1.9 + * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
1.10 + *
1.11 + * Redistribution and use in source and binary forms, with or without
1.12 + * modification, are permitted provided that the following conditions
1.13 + * are met:
1.14 + *
1.15 + * 1. Redistributions of source code must retain the above copyright
1.16 + * notice, this list of conditions and the following disclaimer.
1.17 + *
1.18 + * 2. Redistributions in binary form must reproduce the above copyright
1.19 + * notice, this list of conditions and the following disclaimer in
1.20 + * the documentation and/or other materials provided with the
1.21 + * distribution.
1.22 + *
1.23 + * 3. All advertising materials mentioning features or use of this
1.24 + * software must display the following acknowledgment:
1.25 + * "This product includes software developed by the OpenSSL Project
1.26 + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
1.27 + *
1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.29 + * endorse or promote products derived from this software without
1.30 + * prior written permission. For written permission, please contact
1.31 + * licensing@OpenSSL.org.
1.32 + *
1.33 + * 5. Products derived from this software may not be called "OpenSSL"
1.34 + * nor may "OpenSSL" appear in their names without prior written
1.35 + * permission of the OpenSSL Project.
1.36 + *
1.37 + * 6. Redistributions of any form whatsoever must retain the following
1.38 + * acknowledgment:
1.39 + * "This product includes software developed by the OpenSSL Project
1.40 + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
1.41 + *
1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.45 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.54 + * ====================================================================
1.55 + *
1.56 + * This product includes cryptographic software written by Eric Young
1.57 + * (eay@cryptsoft.com). This product includes software written by Tim
1.58 + * Hudson (tjh@cryptsoft.com).
1.59 + *
1.60 + */
1.61 + /*
1.62 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.63 + */
1.64 +
1.65 +/* X509 v3 extension utilities */
1.66 +
1.67 +#include <stdio.h>
1.68 +#include "cryptlib.h"
1.69 +#include <openssl/conf.h>
1.70 +#include <openssl/x509v3.h>
1.71 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
1.72 +#include "libcrypto_wsd_macros.h"
1.73 +#include "libcrypto_wsd.h"
1.74 +#endif
1.75 +
1.76 +#include "ext_dat.h"
1.77 +
1.78 +#ifndef EMULATOR
1.79 +static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
1.80 +#else
1.81 +GET_STATIC_VAR_FROM_TLS(ext_list,v3_lib,STACK_OF(X509V3_EXT_METHOD) *)
1.82 +#define ext_list (*GET_WSD_VAR_NAME(ext_list,v3_lib, s)())
1.83 +#endif
1.84 +
1.85 +static int ext_cmp(const X509V3_EXT_METHOD * const *a,
1.86 + const X509V3_EXT_METHOD * const *b);
1.87 +static void ext_list_free(X509V3_EXT_METHOD *ext);
1.88 +
1.89 +EXPORT_C int X509V3_EXT_add(X509V3_EXT_METHOD *ext)
1.90 +{
1.91 + if(!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp))) {
1.92 + X509V3err(X509V3_F_X509V3_EXT_ADD,ERR_R_MALLOC_FAILURE);
1.93 + return 0;
1.94 + }
1.95 + if(!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
1.96 + X509V3err(X509V3_F_X509V3_EXT_ADD,ERR_R_MALLOC_FAILURE);
1.97 + return 0;
1.98 + }
1.99 + return 1;
1.100 +}
1.101 +
1.102 +static int ext_cmp(const X509V3_EXT_METHOD * const *a,
1.103 + const X509V3_EXT_METHOD * const *b)
1.104 +{
1.105 + return ((*a)->ext_nid - (*b)->ext_nid);
1.106 +}
1.107 +
1.108 +EXPORT_C X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
1.109 +{
1.110 + X509V3_EXT_METHOD tmp, *t = &tmp, **ret;
1.111 + int idx;
1.112 + if(nid < 0) return NULL;
1.113 + tmp.ext_nid = nid;
1.114 + ret = (X509V3_EXT_METHOD **) OBJ_bsearch((char *)&t,
1.115 + (char *)standard_exts, STANDARD_EXTENSION_COUNT,
1.116 + sizeof(X509V3_EXT_METHOD *), (int (*)(const void *, const void *))ext_cmp);
1.117 + if(ret) return *ret;
1.118 + if(!ext_list) return NULL;
1.119 + idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
1.120 + if(idx == -1) return NULL;
1.121 + return sk_X509V3_EXT_METHOD_value(ext_list, idx);
1.122 +}
1.123 +
1.124 +EXPORT_C X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext)
1.125 +{
1.126 + int nid;
1.127 + if((nid = OBJ_obj2nid(ext->object)) == NID_undef) return NULL;
1.128 + return X509V3_EXT_get_nid(nid);
1.129 +}
1.130 +
1.131 +
1.132 +EXPORT_C int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
1.133 +{
1.134 + for(;extlist->ext_nid!=-1;extlist++)
1.135 + if(!X509V3_EXT_add(extlist)) return 0;
1.136 + return 1;
1.137 +}
1.138 +
1.139 +EXPORT_C int X509V3_EXT_add_alias(int nid_to, int nid_from)
1.140 +{
1.141 + X509V3_EXT_METHOD *ext, *tmpext;
1.142 + if(!(ext = X509V3_EXT_get_nid(nid_from))) {
1.143 + X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,X509V3_R_EXTENSION_NOT_FOUND);
1.144 + return 0;
1.145 + }
1.146 + if(!(tmpext = (X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD)))) {
1.147 + X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,ERR_R_MALLOC_FAILURE);
1.148 + return 0;
1.149 + }
1.150 + *tmpext = *ext;
1.151 + tmpext->ext_nid = nid_to;
1.152 + tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
1.153 + return X509V3_EXT_add(tmpext);
1.154 +}
1.155 +
1.156 +EXPORT_C void X509V3_EXT_cleanup(void)
1.157 +{
1.158 + sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
1.159 + ext_list = NULL;
1.160 +}
1.161 +
1.162 +static void ext_list_free(X509V3_EXT_METHOD *ext)
1.163 +{
1.164 + if(ext->ext_flags & X509V3_EXT_DYNAMIC) OPENSSL_free(ext);
1.165 +}
1.166 +
1.167 +/* Legacy function: we don't need to add standard extensions
1.168 + * any more because they are now kept in ext_dat.h.
1.169 + */
1.170 +
1.171 +EXPORT_C int X509V3_add_standard_extensions(void)
1.172 +{
1.173 + return 1;
1.174 +}
1.175 +
1.176 +/* Return an extension internal structure */
1.177 +
1.178 +EXPORT_C void *X509V3_EXT_d2i(X509_EXTENSION *ext)
1.179 +{
1.180 + X509V3_EXT_METHOD *method;
1.181 + const unsigned char *p;
1.182 +
1.183 + if(!(method = X509V3_EXT_get(ext))) return NULL;
1.184 + p = ext->value->data;
1.185 + if(method->it) return ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it));
1.186 + return method->d2i(NULL, &p, ext->value->length);
1.187 +}
1.188 +
1.189 +/* Get critical flag and decoded version of extension from a NID.
1.190 + * The "idx" variable returns the last found extension and can
1.191 + * be used to retrieve multiple extensions of the same NID.
1.192 + * However multiple extensions with the same NID is usually
1.193 + * due to a badly encoded certificate so if idx is NULL we
1.194 + * choke if multiple extensions exist.
1.195 + * The "crit" variable is set to the critical value.
1.196 + * The return value is the decoded extension or NULL on
1.197 + * error. The actual error can have several different causes,
1.198 + * the value of *crit reflects the cause:
1.199 + * >= 0, extension found but not decoded (reflects critical value).
1.200 + * -1 extension not found.
1.201 + * -2 extension occurs more than once.
1.202 + */
1.203 +
1.204 +EXPORT_C void *X509V3_get_d2i(STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx)
1.205 +{
1.206 + int lastpos, i;
1.207 + X509_EXTENSION *ex, *found_ex = NULL;
1.208 + if(!x) {
1.209 + if(idx) *idx = -1;
1.210 + if(crit) *crit = -1;
1.211 + return NULL;
1.212 + }
1.213 + if(idx) lastpos = *idx + 1;
1.214 + else lastpos = 0;
1.215 + if(lastpos < 0) lastpos = 0;
1.216 + for(i = lastpos; i < sk_X509_EXTENSION_num(x); i++)
1.217 + {
1.218 + ex = sk_X509_EXTENSION_value(x, i);
1.219 + if(OBJ_obj2nid(ex->object) == nid) {
1.220 + if(idx) {
1.221 + *idx = i;
1.222 + found_ex = ex;
1.223 + break;
1.224 + } else if(found_ex) {
1.225 + /* Found more than one */
1.226 + if(crit) *crit = -2;
1.227 + return NULL;
1.228 + }
1.229 + found_ex = ex;
1.230 + }
1.231 + }
1.232 + if(found_ex) {
1.233 + /* Found it */
1.234 + if(crit) *crit = X509_EXTENSION_get_critical(found_ex);
1.235 + return X509V3_EXT_d2i(found_ex);
1.236 + }
1.237 +
1.238 + /* Extension not found */
1.239 + if(idx) *idx = -1;
1.240 + if(crit) *crit = -1;
1.241 + return NULL;
1.242 +}
1.243 +
1.244 +/* This function is a general extension append, replace and delete utility.
1.245 + * The precise operation is governed by the 'flags' value. The 'crit' and
1.246 + * 'value' arguments (if relevant) are the extensions internal structure.
1.247 + */
1.248 +
1.249 +EXPORT_C int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
1.250 + int crit, unsigned long flags)
1.251 +{
1.252 + int extidx = -1;
1.253 + int errcode;
1.254 + X509_EXTENSION *ext, *extmp;
1.255 + unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
1.256 +
1.257 + /* If appending we don't care if it exists, otherwise
1.258 + * look for existing extension.
1.259 + */
1.260 + if(ext_op != X509V3_ADD_APPEND)
1.261 + extidx = X509v3_get_ext_by_NID(*x, nid, -1);
1.262 +
1.263 + /* See if extension exists */
1.264 + if(extidx >= 0) {
1.265 + /* If keep existing, nothing to do */
1.266 + if(ext_op == X509V3_ADD_KEEP_EXISTING)
1.267 + return 1;
1.268 + /* If default then its an error */
1.269 + if(ext_op == X509V3_ADD_DEFAULT) {
1.270 + errcode = X509V3_R_EXTENSION_EXISTS;
1.271 + goto err;
1.272 + }
1.273 + /* If delete, just delete it */
1.274 + if(ext_op == X509V3_ADD_DELETE) {
1.275 + if(!sk_X509_EXTENSION_delete(*x, extidx)) return -1;
1.276 + return 1;
1.277 + }
1.278 + } else {
1.279 + /* If replace existing or delete, error since
1.280 + * extension must exist
1.281 + */
1.282 + if((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
1.283 + (ext_op == X509V3_ADD_DELETE)) {
1.284 + errcode = X509V3_R_EXTENSION_NOT_FOUND;
1.285 + goto err;
1.286 + }
1.287 + }
1.288 +
1.289 + /* If we get this far then we have to create an extension:
1.290 + * could have some flags for alternative encoding schemes...
1.291 + */
1.292 +
1.293 + ext = X509V3_EXT_i2d(nid, crit, value);
1.294 +
1.295 + if(!ext) {
1.296 + X509V3err(X509V3_F_X509V3_ADD1_I2D, X509V3_R_ERROR_CREATING_EXTENSION);
1.297 + return 0;
1.298 + }
1.299 +
1.300 + /* If extension exists replace it.. */
1.301 + if(extidx >= 0) {
1.302 + extmp = sk_X509_EXTENSION_value(*x, extidx);
1.303 + X509_EXTENSION_free(extmp);
1.304 + if(!sk_X509_EXTENSION_set(*x, extidx, ext)) return -1;
1.305 + return 1;
1.306 + }
1.307 +
1.308 + if(!*x && !(*x = sk_X509_EXTENSION_new_null())) return -1;
1.309 + if(!sk_X509_EXTENSION_push(*x, ext)) return -1;
1.310 +
1.311 + return 1;
1.312 +
1.313 + err:
1.314 + if(!(flags & X509V3_ADD_SILENT))
1.315 + X509V3err(X509V3_F_X509V3_ADD1_I2D, errcode);
1.316 + return 0;
1.317 +}
1.318 +
1.319 +IMPLEMENT_STACK_OF(X509V3_EXT_METHOD)