xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/x_name.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/asn1/x_name.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/asn1t.h>
620Sstevel@tonic-gate #include <openssl/x509.h>
630Sstevel@tonic-gate 
64*2139Sjp161948 static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len, const ASN1_ITEM *it,
650Sstevel@tonic-gate 					int tag, int aclass, char opt, ASN1_TLC *ctx);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
680Sstevel@tonic-gate static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
690Sstevel@tonic-gate static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static int x509_name_encode(X509_NAME *a);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate ASN1_SEQUENCE(X509_NAME_ENTRY) = {
740Sstevel@tonic-gate 	ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
750Sstevel@tonic-gate 	ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
760Sstevel@tonic-gate } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
770Sstevel@tonic-gate 
780Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
790Sstevel@tonic-gate IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /* For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY }
820Sstevel@tonic-gate  * so declare two template wrappers for this
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
860Sstevel@tonic-gate 	ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
870Sstevel@tonic-gate ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
880Sstevel@tonic-gate 
890Sstevel@tonic-gate ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
900Sstevel@tonic-gate 	ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
910Sstevel@tonic-gate ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /* Normally that's where it would end: we'd have two nested STACK structures
940Sstevel@tonic-gate  * representing the ASN1. Unfortunately X509_NAME uses a completely different
950Sstevel@tonic-gate  * form and caches encodings so we have to process the internal form and convert
960Sstevel@tonic-gate  * to the external form.
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate const ASN1_EXTERN_FUNCS x509_name_ff = {
1000Sstevel@tonic-gate 	NULL,
1010Sstevel@tonic-gate 	x509_name_ex_new,
1020Sstevel@tonic-gate 	x509_name_ex_free,
1030Sstevel@tonic-gate 	0,	/* Default clear behaviour is OK */
1040Sstevel@tonic-gate 	x509_name_ex_d2i,
1050Sstevel@tonic-gate 	x509_name_ex_i2d
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate 
IMPLEMENT_EXTERN_ASN1(X509_NAME,V_ASN1_SEQUENCE,x509_name_ff)1080Sstevel@tonic-gate IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
1110Sstevel@tonic-gate IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	X509_NAME *ret = NULL;
1160Sstevel@tonic-gate 	ret = OPENSSL_malloc(sizeof(X509_NAME));
1170Sstevel@tonic-gate 	if(!ret) goto memerr;
1180Sstevel@tonic-gate 	if ((ret->entries=sk_X509_NAME_ENTRY_new_null()) == NULL)
1190Sstevel@tonic-gate 		goto memerr;
1200Sstevel@tonic-gate 	if((ret->bytes = BUF_MEM_new()) == NULL) goto memerr;
1210Sstevel@tonic-gate 	ret->modified=1;
1220Sstevel@tonic-gate 	*val = (ASN1_VALUE *)ret;
1230Sstevel@tonic-gate 	return 1;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate  memerr:
126*2139Sjp161948 	ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
1270Sstevel@tonic-gate 	if (ret)
1280Sstevel@tonic-gate 		{
1290Sstevel@tonic-gate 		if (ret->entries)
1300Sstevel@tonic-gate 			sk_X509_NAME_ENTRY_free(ret->entries);
1310Sstevel@tonic-gate 		OPENSSL_free(ret);
1320Sstevel@tonic-gate 		}
1330Sstevel@tonic-gate 	return 0;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
x509_name_ex_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)1360Sstevel@tonic-gate static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	X509_NAME *a;
1390Sstevel@tonic-gate 	if(!pval || !*pval)
1400Sstevel@tonic-gate 	    return;
1410Sstevel@tonic-gate 	a = (X509_NAME *)*pval;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	BUF_MEM_free(a->bytes);
1440Sstevel@tonic-gate 	sk_X509_NAME_ENTRY_pop_free(a->entries,X509_NAME_ENTRY_free);
1450Sstevel@tonic-gate 	OPENSSL_free(a);
1460Sstevel@tonic-gate 	*pval = NULL;
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /* Used with sk_pop_free() to free up the internal representation.
1500Sstevel@tonic-gate  * NB: we only free the STACK and not its contents because it is
1510Sstevel@tonic-gate  * already present in the X509_NAME structure.
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate 
sk_internal_free(void * a)1540Sstevel@tonic-gate static void sk_internal_free(void *a)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	sk_free(a);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
x509_name_ex_d2i(ASN1_VALUE ** val,const unsigned char ** in,long len,const ASN1_ITEM * it,int tag,int aclass,char opt,ASN1_TLC * ctx)159*2139Sjp161948 static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len, const ASN1_ITEM *it,
1600Sstevel@tonic-gate 					int tag, int aclass, char opt, ASN1_TLC *ctx)
1610Sstevel@tonic-gate {
162*2139Sjp161948 	const unsigned char *p = *in, *q;
163*2139Sjp161948 	STACK *intname = NULL, **intname_pp = &intname;
1640Sstevel@tonic-gate 	int i, j, ret;
165*2139Sjp161948 	X509_NAME *nm = NULL, **nm_pp = &nm;
1660Sstevel@tonic-gate 	STACK_OF(X509_NAME_ENTRY) *entries;
1670Sstevel@tonic-gate 	X509_NAME_ENTRY *entry;
1680Sstevel@tonic-gate 	q = p;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/* Get internal representation of Name */
171*2139Sjp161948 	ret = ASN1_item_ex_d2i((ASN1_VALUE **)intname_pp,
172*2139Sjp161948 			       &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
173*2139Sjp161948 			       tag, aclass, opt, ctx);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if(ret <= 0) return ret;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if(*val) x509_name_ex_free(val, NULL);
178*2139Sjp161948 	if(!x509_name_ex_new((ASN1_VALUE **)nm_pp, NULL)) goto err;
1790Sstevel@tonic-gate 	/* We've decoded it: now cache encoding */
1800Sstevel@tonic-gate 	if(!BUF_MEM_grow(nm->bytes, p - q)) goto err;
1810Sstevel@tonic-gate 	memcpy(nm->bytes->data, q, p - q);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	/* Convert internal representation to X509_NAME structure */
1840Sstevel@tonic-gate 	for(i = 0; i < sk_num(intname); i++) {
1850Sstevel@tonic-gate 		entries = (STACK_OF(X509_NAME_ENTRY) *)sk_value(intname, i);
1860Sstevel@tonic-gate 		for(j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
1870Sstevel@tonic-gate 			entry = sk_X509_NAME_ENTRY_value(entries, j);
1880Sstevel@tonic-gate 			entry->set = i;
1890Sstevel@tonic-gate 			if(!sk_X509_NAME_ENTRY_push(nm->entries, entry))
1900Sstevel@tonic-gate 				goto err;
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 		sk_X509_NAME_ENTRY_free(entries);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 	sk_free(intname);
1950Sstevel@tonic-gate 	nm->modified = 0;
1960Sstevel@tonic-gate 	*val = (ASN1_VALUE *)nm;
1970Sstevel@tonic-gate 	*in = p;
1980Sstevel@tonic-gate 	return ret;
1990Sstevel@tonic-gate 	err:
200*2139Sjp161948 	ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
2010Sstevel@tonic-gate 	return 0;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
x509_name_ex_i2d(ASN1_VALUE ** val,unsigned char ** out,const ASN1_ITEM * it,int tag,int aclass)2040Sstevel@tonic-gate static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate 	int ret;
2070Sstevel@tonic-gate 	X509_NAME *a = (X509_NAME *)*val;
2080Sstevel@tonic-gate 	if(a->modified) {
2090Sstevel@tonic-gate 		ret = x509_name_encode((X509_NAME *)a);
2100Sstevel@tonic-gate 		if(ret < 0) return ret;
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 	ret = a->bytes->length;
2130Sstevel@tonic-gate 	if(out != NULL) {
2140Sstevel@tonic-gate 		memcpy(*out,a->bytes->data,ret);
2150Sstevel@tonic-gate 		*out+=ret;
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 	return ret;
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
x509_name_encode(X509_NAME * a)2200Sstevel@tonic-gate static int x509_name_encode(X509_NAME *a)
2210Sstevel@tonic-gate {
222*2139Sjp161948 	STACK *intname = NULL, **intname_pp = &intname;
2230Sstevel@tonic-gate 	int len;
2240Sstevel@tonic-gate 	unsigned char *p;
2250Sstevel@tonic-gate 	STACK_OF(X509_NAME_ENTRY) *entries = NULL;
2260Sstevel@tonic-gate 	X509_NAME_ENTRY *entry;
2270Sstevel@tonic-gate 	int i, set = -1;
2280Sstevel@tonic-gate 	intname = sk_new_null();
2290Sstevel@tonic-gate 	if(!intname) goto memerr;
2300Sstevel@tonic-gate 	for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
2310Sstevel@tonic-gate 		entry = sk_X509_NAME_ENTRY_value(a->entries, i);
2320Sstevel@tonic-gate 		if(entry->set != set) {
2330Sstevel@tonic-gate 			entries = sk_X509_NAME_ENTRY_new_null();
2340Sstevel@tonic-gate 			if(!entries) goto memerr;
2350Sstevel@tonic-gate 			if(!sk_push(intname, (char *)entries)) goto memerr;
2360Sstevel@tonic-gate 			set = entry->set;
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 		if(!sk_X509_NAME_ENTRY_push(entries, entry)) goto memerr;
2390Sstevel@tonic-gate 	}
240*2139Sjp161948 	len = ASN1_item_ex_i2d((ASN1_VALUE **)intname_pp, NULL,
241*2139Sjp161948 			       ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
2420Sstevel@tonic-gate 	if (!BUF_MEM_grow(a->bytes,len)) goto memerr;
2430Sstevel@tonic-gate 	p=(unsigned char *)a->bytes->data;
244*2139Sjp161948 	ASN1_item_ex_i2d((ASN1_VALUE **)intname_pp,
245*2139Sjp161948 			 &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
2460Sstevel@tonic-gate 	sk_pop_free(intname, sk_internal_free);
2470Sstevel@tonic-gate 	a->modified = 0;
2480Sstevel@tonic-gate 	return len;
2490Sstevel@tonic-gate 	memerr:
2500Sstevel@tonic-gate 	sk_pop_free(intname, sk_internal_free);
251*2139Sjp161948 	ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
2520Sstevel@tonic-gate 	return -1;
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 
X509_NAME_set(X509_NAME ** xn,X509_NAME * name)2560Sstevel@tonic-gate int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
2570Sstevel@tonic-gate 	{
2580Sstevel@tonic-gate 	X509_NAME *in;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (!xn || !name) return(0);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	if (*xn != name)
2630Sstevel@tonic-gate 		{
2640Sstevel@tonic-gate 		in=X509_NAME_dup(name);
2650Sstevel@tonic-gate 		if (in != NULL)
2660Sstevel@tonic-gate 			{
2670Sstevel@tonic-gate 			X509_NAME_free(*xn);
2680Sstevel@tonic-gate 			*xn=in;
2690Sstevel@tonic-gate 			}
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 	return(*xn != NULL);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate IMPLEMENT_STACK_OF(X509_NAME_ENTRY)
2750Sstevel@tonic-gate IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)
276