os/ossrv/ssl/libcrypto/src/crypto/x509/x509_v3.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/x509/x509_v3.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,274 @@
     1.4 +/* crypto/x509/x509_v3.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +
    1.62 +#include <stdio.h>
    1.63 +#include <openssl/stack.h>
    1.64 +#include "cryptlib.h"
    1.65 +#include <openssl/asn1.h>
    1.66 +#include <openssl/objects.h>
    1.67 +#include <openssl/evp.h>
    1.68 +#include <openssl/x509.h>
    1.69 +#include <openssl/x509v3.h>
    1.70 +
    1.71 +EXPORT_C int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
    1.72 +	{
    1.73 +	if (x == NULL) return(0);
    1.74 +	return(sk_X509_EXTENSION_num(x));
    1.75 +	}
    1.76 +
    1.77 +EXPORT_C int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
    1.78 +			  int lastpos)
    1.79 +	{
    1.80 +	ASN1_OBJECT *obj;
    1.81 +
    1.82 +	obj=OBJ_nid2obj(nid);
    1.83 +	if (obj == NULL) return(-2);
    1.84 +	return(X509v3_get_ext_by_OBJ(x,obj,lastpos));
    1.85 +	}
    1.86 +
    1.87 +EXPORT_C int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk, ASN1_OBJECT *obj,
    1.88 +			  int lastpos)
    1.89 +	{
    1.90 +	int n;
    1.91 +	X509_EXTENSION *ex;
    1.92 +
    1.93 +	if (sk == NULL) return(-1);
    1.94 +	lastpos++;
    1.95 +	if (lastpos < 0)
    1.96 +		lastpos=0;
    1.97 +	n=sk_X509_EXTENSION_num(sk);
    1.98 +	for ( ; lastpos < n; lastpos++)
    1.99 +		{
   1.100 +		ex=sk_X509_EXTENSION_value(sk,lastpos);
   1.101 +		if (OBJ_cmp(ex->object,obj) == 0)
   1.102 +			return(lastpos);
   1.103 +		}
   1.104 +	return(-1);
   1.105 +	}
   1.106 +
   1.107 +EXPORT_C int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
   1.108 +			       int lastpos)
   1.109 +	{
   1.110 +	int n;
   1.111 +	X509_EXTENSION *ex;
   1.112 +
   1.113 +	if (sk == NULL) return(-1);
   1.114 +	lastpos++;
   1.115 +	if (lastpos < 0)
   1.116 +		lastpos=0;
   1.117 +	n=sk_X509_EXTENSION_num(sk);
   1.118 +	for ( ; lastpos < n; lastpos++)
   1.119 +		{
   1.120 +		ex=sk_X509_EXTENSION_value(sk,lastpos);
   1.121 +		if (	((ex->critical > 0) && crit) ||
   1.122 +			((ex->critical <= 0) && !crit))
   1.123 +			return(lastpos);
   1.124 +		}
   1.125 +	return(-1);
   1.126 +	}
   1.127 +
   1.128 +EXPORT_C X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
   1.129 +	{
   1.130 +	if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
   1.131 +		return NULL;
   1.132 +	else
   1.133 +		return sk_X509_EXTENSION_value(x,loc);
   1.134 +	}
   1.135 +
   1.136 +EXPORT_C X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
   1.137 +	{
   1.138 +	X509_EXTENSION *ret;
   1.139 +
   1.140 +	if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
   1.141 +		return(NULL);
   1.142 +	ret=sk_X509_EXTENSION_delete(x,loc);
   1.143 +	return(ret);
   1.144 +	}
   1.145 +
   1.146 +EXPORT_C STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
   1.147 +					 X509_EXTENSION *ex, int loc)
   1.148 +	{
   1.149 +	X509_EXTENSION *new_ex=NULL;
   1.150 +	int n;
   1.151 +	STACK_OF(X509_EXTENSION) *sk=NULL;
   1.152 +
   1.153 +	if (x == NULL)
   1.154 +		{
   1.155 +		X509err(X509_F_X509V3_ADD_EXT,ERR_R_PASSED_NULL_PARAMETER);
   1.156 +		goto err2;
   1.157 +		}
   1.158 +
   1.159 +	if (*x == NULL)
   1.160 +		{
   1.161 +		if ((sk=sk_X509_EXTENSION_new_null()) == NULL)
   1.162 +			goto err;
   1.163 +		}
   1.164 +	else
   1.165 +		sk= *x;
   1.166 +
   1.167 +	n=sk_X509_EXTENSION_num(sk);
   1.168 +	if (loc > n) loc=n;
   1.169 +	else if (loc < 0) loc=n;
   1.170 +
   1.171 +	if ((new_ex=X509_EXTENSION_dup(ex)) == NULL)
   1.172 +		goto err2;
   1.173 +	if (!sk_X509_EXTENSION_insert(sk,new_ex,loc))
   1.174 +		goto err;
   1.175 +	if (*x == NULL)
   1.176 +		*x=sk;
   1.177 +	return(sk);
   1.178 +err:
   1.179 +	X509err(X509_F_X509V3_ADD_EXT,ERR_R_MALLOC_FAILURE);
   1.180 +err2:
   1.181 +	if (new_ex != NULL) X509_EXTENSION_free(new_ex);
   1.182 +	if (sk != NULL) sk_X509_EXTENSION_free(sk);
   1.183 +	return(NULL);
   1.184 +	}
   1.185 +
   1.186 +EXPORT_C X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
   1.187 +	     int crit, ASN1_OCTET_STRING *data)
   1.188 +	{
   1.189 +	ASN1_OBJECT *obj;
   1.190 +	X509_EXTENSION *ret;
   1.191 +
   1.192 +	obj=OBJ_nid2obj(nid);
   1.193 +	if (obj == NULL)
   1.194 +		{
   1.195 +		X509err(X509_F_X509_EXTENSION_CREATE_BY_NID,X509_R_UNKNOWN_NID);
   1.196 +		return(NULL);
   1.197 +		}
   1.198 +	ret=X509_EXTENSION_create_by_OBJ(ex,obj,crit,data);
   1.199 +	if (ret == NULL) ASN1_OBJECT_free(obj);
   1.200 +	return(ret);
   1.201 +	}
   1.202 +
   1.203 +EXPORT_C X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
   1.204 +	     ASN1_OBJECT *obj, int crit, ASN1_OCTET_STRING *data)
   1.205 +	{
   1.206 +	X509_EXTENSION *ret;
   1.207 +
   1.208 +	if ((ex == NULL) || (*ex == NULL))
   1.209 +		{
   1.210 +		if ((ret=X509_EXTENSION_new()) == NULL)
   1.211 +			{
   1.212 +			X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,ERR_R_MALLOC_FAILURE);
   1.213 +			return(NULL);
   1.214 +			}
   1.215 +		}
   1.216 +	else
   1.217 +		ret= *ex;
   1.218 +
   1.219 +	if (!X509_EXTENSION_set_object(ret,obj))
   1.220 +		goto err;
   1.221 +	if (!X509_EXTENSION_set_critical(ret,crit))
   1.222 +		goto err;
   1.223 +	if (!X509_EXTENSION_set_data(ret,data))
   1.224 +		goto err;
   1.225 +	
   1.226 +	if ((ex != NULL) && (*ex == NULL)) *ex=ret;
   1.227 +	return(ret);
   1.228 +err:
   1.229 +	if ((ex == NULL) || (ret != *ex))
   1.230 +		X509_EXTENSION_free(ret);
   1.231 +	return(NULL);
   1.232 +	}
   1.233 +
   1.234 +EXPORT_C int X509_EXTENSION_set_object(X509_EXTENSION *ex, ASN1_OBJECT *obj)
   1.235 +	{
   1.236 +	if ((ex == NULL) || (obj == NULL))
   1.237 +		return(0);
   1.238 +	ASN1_OBJECT_free(ex->object);
   1.239 +	ex->object=OBJ_dup(obj);
   1.240 +	return(1);
   1.241 +	}
   1.242 +
   1.243 +EXPORT_C int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
   1.244 +	{
   1.245 +	if (ex == NULL) return(0);
   1.246 +	ex->critical=(crit)?0xFF:-1;
   1.247 +	return(1);
   1.248 +	}
   1.249 +
   1.250 +EXPORT_C int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
   1.251 +	{
   1.252 +	int i;
   1.253 +
   1.254 +	if (ex == NULL) return(0);
   1.255 +	i=M_ASN1_OCTET_STRING_set(ex->value,data->data,data->length);
   1.256 +	if (!i) return(0);
   1.257 +	return(1);
   1.258 +	}
   1.259 +
   1.260 +EXPORT_C ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
   1.261 +	{
   1.262 +	if (ex == NULL) return(NULL);
   1.263 +	return(ex->object);
   1.264 +	}
   1.265 +
   1.266 +EXPORT_C ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
   1.267 +	{
   1.268 +	if (ex == NULL) return(NULL);
   1.269 +	return(ex->value);
   1.270 +	}
   1.271 +
   1.272 +EXPORT_C int X509_EXTENSION_get_critical(X509_EXTENSION *ex)
   1.273 +	{
   1.274 +	if (ex == NULL) return(0);
   1.275 +	if(ex->critical > 0) return 1;
   1.276 +	return 0;
   1.277 +	}