os/ossrv/ssl/libcrypto/src/crypto/x509/x509_att.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_att.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,332 @@
     1.4 +/* crypto/x509/x509_att.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 X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
    1.72 +{
    1.73 +	if (!x) return 0;
    1.74 +	return(sk_X509_ATTRIBUTE_num(x));
    1.75 +}
    1.76 +
    1.77 +EXPORT_C int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *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(X509at_get_attr_by_OBJ(x,obj,lastpos));
    1.85 +}
    1.86 +
    1.87 +EXPORT_C int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk, ASN1_OBJECT *obj,
    1.88 +			  int lastpos)
    1.89 +{
    1.90 +	int n;
    1.91 +	X509_ATTRIBUTE *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_ATTRIBUTE_num(sk);
    1.98 +	for ( ; lastpos < n; lastpos++)
    1.99 +		{
   1.100 +		ex=sk_X509_ATTRIBUTE_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 X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
   1.108 +{
   1.109 +	if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
   1.110 +		return NULL;
   1.111 +	else
   1.112 +		return sk_X509_ATTRIBUTE_value(x,loc);
   1.113 +}
   1.114 +
   1.115 +EXPORT_C X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
   1.116 +{
   1.117 +	X509_ATTRIBUTE *ret;
   1.118 +
   1.119 +	if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
   1.120 +		return(NULL);
   1.121 +	ret=sk_X509_ATTRIBUTE_delete(x,loc);
   1.122 +	return(ret);
   1.123 +}
   1.124 +
   1.125 +EXPORT_C STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
   1.126 +					 X509_ATTRIBUTE *attr)
   1.127 +{
   1.128 +	X509_ATTRIBUTE *new_attr=NULL;
   1.129 +	STACK_OF(X509_ATTRIBUTE) *sk=NULL;
   1.130 +
   1.131 +	if (x == NULL)
   1.132 +		{
   1.133 +		X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_PASSED_NULL_PARAMETER);
   1.134 +		goto err2;
   1.135 +		} 
   1.136 +
   1.137 +	if (*x == NULL)
   1.138 +		{
   1.139 +		if ((sk=sk_X509_ATTRIBUTE_new_null()) == NULL)
   1.140 +			goto err;
   1.141 +		}
   1.142 +	else
   1.143 +		sk= *x;
   1.144 +
   1.145 +	if ((new_attr=X509_ATTRIBUTE_dup(attr)) == NULL)
   1.146 +		goto err2;
   1.147 +	if (!sk_X509_ATTRIBUTE_push(sk,new_attr))
   1.148 +		goto err;
   1.149 +	if (*x == NULL)
   1.150 +		*x=sk;
   1.151 +	return(sk);
   1.152 +err:
   1.153 +	X509err(X509_F_X509AT_ADD1_ATTR,ERR_R_MALLOC_FAILURE);
   1.154 +err2:
   1.155 +	if (new_attr != NULL) X509_ATTRIBUTE_free(new_attr);
   1.156 +	if (sk != NULL) sk_X509_ATTRIBUTE_free(sk);
   1.157 +	return(NULL);
   1.158 +}
   1.159 +
   1.160 +EXPORT_C STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x,
   1.161 +			const ASN1_OBJECT *obj, int type,
   1.162 +			const unsigned char *bytes, int len)
   1.163 +{
   1.164 +	X509_ATTRIBUTE *attr;
   1.165 +	STACK_OF(X509_ATTRIBUTE) *ret;
   1.166 +	attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
   1.167 +	if(!attr) return 0;
   1.168 +	ret = X509at_add1_attr(x, attr);
   1.169 +	X509_ATTRIBUTE_free(attr);
   1.170 +	return ret;
   1.171 +}
   1.172 +
   1.173 +EXPORT_C STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x,
   1.174 +			int nid, int type,
   1.175 +			const unsigned char *bytes, int len)
   1.176 +{
   1.177 +	X509_ATTRIBUTE *attr;
   1.178 +	STACK_OF(X509_ATTRIBUTE) *ret;
   1.179 +	attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
   1.180 +	if(!attr) return 0;
   1.181 +	ret = X509at_add1_attr(x, attr);
   1.182 +	X509_ATTRIBUTE_free(attr);
   1.183 +	return ret;
   1.184 +}
   1.185 +
   1.186 +EXPORT_C STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x,
   1.187 +			const char *attrname, int type,
   1.188 +			const unsigned char *bytes, int len)
   1.189 +{
   1.190 +	X509_ATTRIBUTE *attr;
   1.191 +	STACK_OF(X509_ATTRIBUTE) *ret;
   1.192 +	attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
   1.193 +	if(!attr) return 0;
   1.194 +	ret = X509at_add1_attr(x, attr);
   1.195 +	X509_ATTRIBUTE_free(attr);
   1.196 +	return ret;
   1.197 +}
   1.198 +
   1.199 +EXPORT_C X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
   1.200 +	     int atrtype, const void *data, int len)
   1.201 +{
   1.202 +	ASN1_OBJECT *obj;
   1.203 +	X509_ATTRIBUTE *ret;
   1.204 +
   1.205 +	obj=OBJ_nid2obj(nid);
   1.206 +	if (obj == NULL)
   1.207 +		{
   1.208 +		X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID,X509_R_UNKNOWN_NID);
   1.209 +		return(NULL);
   1.210 +		}
   1.211 +	ret=X509_ATTRIBUTE_create_by_OBJ(attr,obj,atrtype,data,len);
   1.212 +	if (ret == NULL) ASN1_OBJECT_free(obj);
   1.213 +	return(ret);
   1.214 +}
   1.215 +
   1.216 +EXPORT_C X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
   1.217 +	     const ASN1_OBJECT *obj, int atrtype, const void *data, int len)
   1.218 +{
   1.219 +	X509_ATTRIBUTE *ret;
   1.220 +
   1.221 +	if ((attr == NULL) || (*attr == NULL))
   1.222 +		{
   1.223 +		if ((ret=X509_ATTRIBUTE_new()) == NULL)
   1.224 +			{
   1.225 +			X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ,ERR_R_MALLOC_FAILURE);
   1.226 +			return(NULL);
   1.227 +			}
   1.228 +		}
   1.229 +	else
   1.230 +		ret= *attr;
   1.231 +
   1.232 +	if (!X509_ATTRIBUTE_set1_object(ret,obj))
   1.233 +		goto err;
   1.234 +	if (!X509_ATTRIBUTE_set1_data(ret,atrtype,data,len))
   1.235 +		goto err;
   1.236 +	
   1.237 +	if ((attr != NULL) && (*attr == NULL)) *attr=ret;
   1.238 +	return(ret);
   1.239 +err:
   1.240 +	if ((attr == NULL) || (ret != *attr))
   1.241 +		X509_ATTRIBUTE_free(ret);
   1.242 +	return(NULL);
   1.243 +}
   1.244 +
   1.245 +EXPORT_C X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
   1.246 +		const char *atrname, int type, const unsigned char *bytes, int len)
   1.247 +	{
   1.248 +	ASN1_OBJECT *obj;
   1.249 +	X509_ATTRIBUTE *nattr;
   1.250 +
   1.251 +	obj=OBJ_txt2obj(atrname, 0);
   1.252 +	if (obj == NULL)
   1.253 +		{
   1.254 +		X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT,
   1.255 +						X509_R_INVALID_FIELD_NAME);
   1.256 +		ERR_add_error_data(2, "name=", atrname);
   1.257 +		return(NULL);
   1.258 +		}
   1.259 +	nattr = X509_ATTRIBUTE_create_by_OBJ(attr,obj,type,bytes,len);
   1.260 +	ASN1_OBJECT_free(obj);
   1.261 +	return nattr;
   1.262 +	}
   1.263 +
   1.264 +EXPORT_C int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
   1.265 +{
   1.266 +	if ((attr == NULL) || (obj == NULL))
   1.267 +		return(0);
   1.268 +	ASN1_OBJECT_free(attr->object);
   1.269 +	attr->object=OBJ_dup(obj);
   1.270 +	return(1);
   1.271 +}
   1.272 +
   1.273 +EXPORT_C int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, const void *data, int len)
   1.274 +{
   1.275 +	ASN1_TYPE *ttmp;
   1.276 +	ASN1_STRING *stmp;
   1.277 +	int atype;
   1.278 +	if (!attr) return 0;
   1.279 +	if(attrtype & MBSTRING_FLAG) {
   1.280 +		stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
   1.281 +						OBJ_obj2nid(attr->object));
   1.282 +		if(!stmp) {
   1.283 +			X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB);
   1.284 +			return 0;
   1.285 +		}
   1.286 +		atype = stmp->type;
   1.287 +	} else {
   1.288 +		if(!(stmp = ASN1_STRING_type_new(attrtype))) goto err;
   1.289 +		if(!ASN1_STRING_set(stmp, data, len)) goto err;
   1.290 +		atype = attrtype;
   1.291 +	}
   1.292 +	if(!(attr->value.set = sk_ASN1_TYPE_new_null())) goto err;
   1.293 +	if(!(ttmp = ASN1_TYPE_new())) goto err;
   1.294 +	if(!sk_ASN1_TYPE_push(attr->value.set, ttmp)) goto err;
   1.295 +	attr->single = 0;
   1.296 +	ASN1_TYPE_set(ttmp, atype, stmp);
   1.297 +	return 1;
   1.298 +	err:
   1.299 +	X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);
   1.300 +	return 0;
   1.301 +}
   1.302 +
   1.303 +EXPORT_C int X509_ATTRIBUTE_count(X509_ATTRIBUTE *attr)
   1.304 +{
   1.305 +	if(!attr->single) return sk_ASN1_TYPE_num(attr->value.set);
   1.306 +	if(attr->value.single) return 1;
   1.307 +	return 0;
   1.308 +}
   1.309 +
   1.310 +EXPORT_C ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
   1.311 +{
   1.312 +	if (attr == NULL) return(NULL);
   1.313 +	return(attr->object);
   1.314 +}
   1.315 +
   1.316 +EXPORT_C void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
   1.317 +					int atrtype, void *data)
   1.318 +{
   1.319 +	ASN1_TYPE *ttmp;
   1.320 +	ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
   1.321 +	if(!ttmp) return NULL;
   1.322 +	if(atrtype != ASN1_TYPE_get(ttmp)){
   1.323 +		X509err(X509_F_X509_ATTRIBUTE_GET0_DATA, X509_R_WRONG_TYPE);
   1.324 +		return NULL;
   1.325 +	}
   1.326 +	return ttmp->value.ptr;
   1.327 +}
   1.328 +
   1.329 +EXPORT_C ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
   1.330 +{
   1.331 +	if (attr == NULL) return(NULL);
   1.332 +	if(idx >= X509_ATTRIBUTE_count(attr)) return NULL;
   1.333 +	if(!attr->single) return sk_ASN1_TYPE_value(attr->value.set, idx);
   1.334 +	else return attr->value.single;
   1.335 +}