os/ossrv/ssl/libcrypto/src/crypto/asn1/x_name.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/asn1/x_name.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,275 @@
     1.4 +/* crypto/asn1/x_name.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 "cryptlib.h"
    1.64 +#include <openssl/asn1t.h>
    1.65 +#include <openssl/x509.h>
    1.66 +
    1.67 +static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len, const ASN1_ITEM *it,
    1.68 +					int tag, int aclass, char opt, ASN1_TLC *ctx);
    1.69 +
    1.70 +static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
    1.71 +static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
    1.72 +static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
    1.73 +
    1.74 +static int x509_name_encode(X509_NAME *a);
    1.75 +
    1.76 +ASN1_SEQUENCE(X509_NAME_ENTRY) = {
    1.77 +	ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
    1.78 +	ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
    1.79 +} ASN1_SEQUENCE_END(X509_NAME_ENTRY)
    1.80 +
    1.81 +IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
    1.82 +IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
    1.83 +
    1.84 +/* For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY }
    1.85 + * so declare two template wrappers for this
    1.86 + */
    1.87 +
    1.88 +ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
    1.89 +	ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
    1.90 +ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
    1.91 +
    1.92 +ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
    1.93 +	ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
    1.94 +ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
    1.95 +
    1.96 +/* Normally that's where it would end: we'd have two nested STACK structures
    1.97 + * representing the ASN1. Unfortunately X509_NAME uses a completely different
    1.98 + * form and caches encodings so we have to process the internal form and convert
    1.99 + * to the external form.
   1.100 + */
   1.101 +
   1.102 +const ASN1_EXTERN_FUNCS x509_name_ff = {
   1.103 +	NULL,
   1.104 +	x509_name_ex_new,
   1.105 +	x509_name_ex_free,
   1.106 +	0,	/* Default clear behaviour is OK */
   1.107 +	x509_name_ex_d2i,
   1.108 +	x509_name_ex_i2d
   1.109 +};
   1.110 +
   1.111 +IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff) 
   1.112 +
   1.113 +IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
   1.114 +IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
   1.115 +
   1.116 +static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
   1.117 +{
   1.118 +	X509_NAME *ret = NULL;
   1.119 +	ret = OPENSSL_malloc(sizeof(X509_NAME));
   1.120 +	if(!ret) goto memerr;
   1.121 +	if ((ret->entries=sk_X509_NAME_ENTRY_new_null()) == NULL)
   1.122 +		goto memerr;
   1.123 +	if((ret->bytes = BUF_MEM_new()) == NULL) goto memerr;
   1.124 +	ret->modified=1;
   1.125 +	*val = (ASN1_VALUE *)ret;
   1.126 +	return 1;
   1.127 +
   1.128 + memerr:
   1.129 +	ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
   1.130 +	if (ret)
   1.131 +		{
   1.132 +		if (ret->entries)
   1.133 +			sk_X509_NAME_ENTRY_free(ret->entries);
   1.134 +		OPENSSL_free(ret);
   1.135 +		}
   1.136 +	return 0;
   1.137 +}
   1.138 +
   1.139 +static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
   1.140 +{
   1.141 +	X509_NAME *a;
   1.142 +	if(!pval || !*pval)
   1.143 +	    return;
   1.144 +	a = (X509_NAME *)*pval;
   1.145 +
   1.146 +	BUF_MEM_free(a->bytes);
   1.147 +	sk_X509_NAME_ENTRY_pop_free(a->entries,X509_NAME_ENTRY_free);
   1.148 +	OPENSSL_free(a);
   1.149 +	*pval = NULL;
   1.150 +}
   1.151 +
   1.152 +/* Used with sk_pop_free() to free up the internal representation.
   1.153 + * NB: we only free the STACK and not its contents because it is
   1.154 + * already present in the X509_NAME structure.
   1.155 + */
   1.156 +
   1.157 +static void sk_internal_free(void *a)
   1.158 +{
   1.159 +	sk_free(a);
   1.160 +}
   1.161 +
   1.162 +static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len, const ASN1_ITEM *it,
   1.163 +					int tag, int aclass, char opt, ASN1_TLC *ctx)
   1.164 +{
   1.165 +	const unsigned char *p = *in, *q;
   1.166 +	union { STACK *s; ASN1_VALUE *a; } intname = {NULL};
   1.167 +	union { X509_NAME *x; ASN1_VALUE *a; } nm = {NULL};
   1.168 +	int i, j, ret;
   1.169 +	STACK_OF(X509_NAME_ENTRY) *entries;
   1.170 +	X509_NAME_ENTRY *entry;
   1.171 +	q = p;
   1.172 +
   1.173 +	/* Get internal representation of Name */
   1.174 +	ret = ASN1_item_ex_d2i(&intname.a,
   1.175 +			       &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
   1.176 +			       tag, aclass, opt, ctx);
   1.177 +	
   1.178 +	if(ret <= 0) return ret;
   1.179 +
   1.180 +	if(*val) x509_name_ex_free(val, NULL);
   1.181 +	if(!x509_name_ex_new(&nm.a, NULL)) goto err;
   1.182 +	/* We've decoded it: now cache encoding */
   1.183 +	if(!BUF_MEM_grow(nm.x->bytes, p - q)) goto err;
   1.184 +	memcpy(nm.x->bytes->data, q, p - q);
   1.185 +
   1.186 +	/* Convert internal representation to X509_NAME structure */
   1.187 +	for(i = 0; i < sk_num(intname.s); i++) {
   1.188 +		entries = (STACK_OF(X509_NAME_ENTRY) *)sk_value(intname.s, i);
   1.189 +		for(j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
   1.190 +			entry = sk_X509_NAME_ENTRY_value(entries, j);
   1.191 +			entry->set = i;
   1.192 +			if(!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
   1.193 +				goto err;
   1.194 +		}
   1.195 +		sk_X509_NAME_ENTRY_free(entries);
   1.196 +	}
   1.197 +	sk_free(intname.s);
   1.198 +	nm.x->modified = 0;
   1.199 +	*val = nm.a;
   1.200 +	*in = p;
   1.201 +	return ret;
   1.202 +	err:
   1.203 +	ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
   1.204 +	return 0;
   1.205 +}
   1.206 +
   1.207 +static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass)
   1.208 +{
   1.209 +	int ret;
   1.210 +	X509_NAME *a = (X509_NAME *)*val;
   1.211 +	if(a->modified) {
   1.212 +		ret = x509_name_encode((X509_NAME *)a);
   1.213 +		if(ret < 0) return ret;
   1.214 +	}
   1.215 +	ret = a->bytes->length;
   1.216 +	if(out != NULL) {
   1.217 +		memcpy(*out,a->bytes->data,ret);
   1.218 +		*out+=ret;
   1.219 +	}
   1.220 +	return ret;
   1.221 +}
   1.222 +
   1.223 +static int x509_name_encode(X509_NAME *a)
   1.224 +{
   1.225 +	union { STACK *s; ASN1_VALUE *a; } intname = {NULL};
   1.226 +	int len;
   1.227 +	unsigned char *p;
   1.228 +	STACK_OF(X509_NAME_ENTRY) *entries = NULL;
   1.229 +	X509_NAME_ENTRY *entry;
   1.230 +	int i, set = -1;
   1.231 +	intname.s = sk_new_null();
   1.232 +	if(!intname.s) goto memerr;
   1.233 +	for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
   1.234 +		entry = sk_X509_NAME_ENTRY_value(a->entries, i);
   1.235 +		if(entry->set != set) {
   1.236 +			entries = sk_X509_NAME_ENTRY_new_null();
   1.237 +			if(!entries) goto memerr;
   1.238 +			if(!sk_push(intname.s, (char *)entries)) goto memerr;
   1.239 +			set = entry->set;
   1.240 +		}
   1.241 +		if(!sk_X509_NAME_ENTRY_push(entries, entry)) goto memerr;
   1.242 +	}
   1.243 +	len = ASN1_item_ex_i2d(&intname.a, NULL,
   1.244 +			       ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
   1.245 +	if (!BUF_MEM_grow(a->bytes,len)) goto memerr;
   1.246 +	p=(unsigned char *)a->bytes->data;
   1.247 +	ASN1_item_ex_i2d(&intname.a,
   1.248 +			 &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
   1.249 +	sk_pop_free(intname.s, sk_internal_free);
   1.250 +	a->modified = 0;
   1.251 +	return len;
   1.252 +	memerr:
   1.253 +	sk_pop_free(intname.s, sk_internal_free);
   1.254 +	ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
   1.255 +	return -1;
   1.256 +}
   1.257 +
   1.258 +
   1.259 +EXPORT_C int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
   1.260 +	{
   1.261 +	X509_NAME *in;
   1.262 +
   1.263 +	if (!xn || !name) return(0);
   1.264 +
   1.265 +	if (*xn != name)
   1.266 +		{
   1.267 +		in=X509_NAME_dup(name);
   1.268 +		if (in != NULL)
   1.269 +			{
   1.270 +			X509_NAME_free(*xn);
   1.271 +			*xn=in;
   1.272 +			}
   1.273 +		}
   1.274 +	return(*xn != NULL);
   1.275 +	}
   1.276 +	
   1.277 +IMPLEMENT_STACK_OF(X509_NAME_ENTRY)
   1.278 +IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)