ssl/libcrypto/src/crypto/x509v3/v3_alt.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* v3_alt.c */
       
     2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
       
     3  * project.
       
     4  */
       
     5 /* ====================================================================
       
     6  * Copyright (c) 1999-2003 The OpenSSL Project.  All rights reserved.
       
     7  *
       
     8  * Redistribution and use in source and binary forms, with or without
       
     9  * modification, are permitted provided that the following conditions
       
    10  * are met:
       
    11  *
       
    12  * 1. Redistributions of source code must retain the above copyright
       
    13  *    notice, this list of conditions and the following disclaimer. 
       
    14  *
       
    15  * 2. Redistributions in binary form must reproduce the above copyright
       
    16  *    notice, this list of conditions and the following disclaimer in
       
    17  *    the documentation and/or other materials provided with the
       
    18  *    distribution.
       
    19  *
       
    20  * 3. All advertising materials mentioning features or use of this
       
    21  *    software must display the following acknowledgment:
       
    22  *    "This product includes software developed by the OpenSSL Project
       
    23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
       
    24  *
       
    25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
       
    26  *    endorse or promote products derived from this software without
       
    27  *    prior written permission. For written permission, please contact
       
    28  *    licensing@OpenSSL.org.
       
    29  *
       
    30  * 5. Products derived from this software may not be called "OpenSSL"
       
    31  *    nor may "OpenSSL" appear in their names without prior written
       
    32  *    permission of the OpenSSL Project.
       
    33  *
       
    34  * 6. Redistributions of any form whatsoever must retain the following
       
    35  *    acknowledgment:
       
    36  *    "This product includes software developed by the OpenSSL Project
       
    37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
       
    38  *
       
    39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
       
    40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
       
    43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
       
    45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
       
    48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
       
    50  * OF THE POSSIBILITY OF SUCH DAMAGE.
       
    51  * ====================================================================
       
    52  *
       
    53  * This product includes cryptographic software written by Eric Young
       
    54  * (eay@cryptsoft.com).  This product includes software written by Tim
       
    55  * Hudson (tjh@cryptsoft.com).
       
    56  *
       
    57  */
       
    58  /*
       
    59  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
    60  */
       
    61 
       
    62 
       
    63 #include <stdio.h>
       
    64 #include "cryptlib.h"
       
    65 #include <openssl/conf.h>
       
    66 #include <openssl/x509v3.h>
       
    67 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
       
    68 #include "libcrypto_wsd_macros.h"
       
    69 #include "libcrypto_wsd.h"
       
    70 #endif
       
    71 
       
    72 
       
    73 static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
       
    74 static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
       
    75 static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p);
       
    76 static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
       
    77 static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx);
       
    78 static int do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx);
       
    79 
       
    80 #ifndef EMULATOR
       
    81 X509V3_EXT_METHOD v3_alt[] = {
       
    82 { NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
       
    83 0,0,0,0,
       
    84 0,0,
       
    85 (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
       
    86 (X509V3_EXT_V2I)v2i_subject_alt,
       
    87 NULL, NULL, NULL},
       
    88 
       
    89 { NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
       
    90 0,0,0,0,
       
    91 0,0,
       
    92 (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
       
    93 (X509V3_EXT_V2I)v2i_issuer_alt,
       
    94 NULL, NULL, NULL},
       
    95 };
       
    96 #else
       
    97 const X509V3_EXT_METHOD v3_alt[] = {
       
    98 { NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
       
    99 0,0,0,0,
       
   100 0,0,
       
   101 (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
       
   102 (X509V3_EXT_V2I)v2i_subject_alt,
       
   103 NULL, NULL, NULL},
       
   104 
       
   105 { NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
       
   106 0,0,0,0,
       
   107 0,0,
       
   108 (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
       
   109 (X509V3_EXT_V2I)v2i_issuer_alt,
       
   110 NULL, NULL, NULL},
       
   111 };
       
   112 
       
   113 #endif
       
   114 EXPORT_C STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method,
       
   115 		GENERAL_NAMES *gens, STACK_OF(CONF_VALUE) *ret)
       
   116 {
       
   117 	int i;
       
   118 	GENERAL_NAME *gen;
       
   119 	for(i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
       
   120 		gen = sk_GENERAL_NAME_value(gens, i);
       
   121 		ret = i2v_GENERAL_NAME(method, gen, ret);
       
   122 	}
       
   123 	if(!ret) return sk_CONF_VALUE_new_null();
       
   124 	return ret;
       
   125 }
       
   126 
       
   127 EXPORT_C STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
       
   128 				GENERAL_NAME *gen, STACK_OF(CONF_VALUE) *ret)
       
   129 {
       
   130 	unsigned char *p;
       
   131 	char oline[256], htmp[5];
       
   132 	int i;
       
   133 	switch (gen->type)
       
   134 	{
       
   135 		case GEN_OTHERNAME:
       
   136 		X509V3_add_value("othername","<unsupported>", &ret);
       
   137 		break;
       
   138 
       
   139 		case GEN_X400:
       
   140 		X509V3_add_value("X400Name","<unsupported>", &ret);
       
   141 		break;
       
   142 
       
   143 		case GEN_EDIPARTY:
       
   144 		X509V3_add_value("EdiPartyName","<unsupported>", &ret);
       
   145 		break;
       
   146 
       
   147 		case GEN_EMAIL:
       
   148 		X509V3_add_value_uchar("email",gen->d.ia5->data, &ret);
       
   149 		break;
       
   150 
       
   151 		case GEN_DNS:
       
   152 		X509V3_add_value_uchar("DNS",gen->d.ia5->data, &ret);
       
   153 		break;
       
   154 
       
   155 		case GEN_URI:
       
   156 		X509V3_add_value_uchar("URI",gen->d.ia5->data, &ret);
       
   157 		break;
       
   158 
       
   159 		case GEN_DIRNAME:
       
   160 		X509_NAME_oneline(gen->d.dirn, oline, 256);
       
   161 		X509V3_add_value("DirName",oline, &ret);
       
   162 		break;
       
   163 
       
   164 		case GEN_IPADD:
       
   165 		p = gen->d.ip->data;
       
   166 		if(gen->d.ip->length == 4)
       
   167 			BIO_snprintf(oline, sizeof oline,
       
   168 				     "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
       
   169 		else if(gen->d.ip->length == 16)
       
   170 			{
       
   171 			oline[0] = 0;
       
   172 			for (i = 0; i < 8; i++)
       
   173 				{
       
   174 				BIO_snprintf(htmp, sizeof htmp,
       
   175 					     "%X", p[0] << 8 | p[1]);
       
   176 				p += 2;
       
   177 				strcat(oline, htmp);
       
   178 				if (i != 7)
       
   179 					strcat(oline, ":");
       
   180 				}
       
   181 			}
       
   182 		else
       
   183 			{
       
   184 			X509V3_add_value("IP Address","<invalid>", &ret);
       
   185 			break;
       
   186 			}
       
   187 		X509V3_add_value("IP Address",oline, &ret);
       
   188 		break;
       
   189 
       
   190 		case GEN_RID:
       
   191 		i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
       
   192 		X509V3_add_value("Registered ID",oline, &ret);
       
   193 		break;
       
   194 	}
       
   195 	return ret;
       
   196 }
       
   197 
       
   198 EXPORT_C int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
       
   199 {
       
   200 	unsigned char *p;
       
   201 	int i;
       
   202 	switch (gen->type)
       
   203 	{
       
   204 		case GEN_OTHERNAME:
       
   205 		BIO_printf(out, "othername:<unsupported>");
       
   206 		break;
       
   207 
       
   208 		case GEN_X400:
       
   209 		BIO_printf(out, "X400Name:<unsupported>");
       
   210 		break;
       
   211 
       
   212 		case GEN_EDIPARTY:
       
   213 		/* Maybe fix this: it is supported now */
       
   214 		BIO_printf(out, "EdiPartyName:<unsupported>");
       
   215 		break;
       
   216 
       
   217 		case GEN_EMAIL:
       
   218 		BIO_printf(out, "email:%s",gen->d.ia5->data);
       
   219 		break;
       
   220 
       
   221 		case GEN_DNS:
       
   222 		BIO_printf(out, "DNS:%s",gen->d.ia5->data);
       
   223 		break;
       
   224 
       
   225 		case GEN_URI:
       
   226 		BIO_printf(out, "URI:%s",gen->d.ia5->data);
       
   227 		break;
       
   228 
       
   229 		case GEN_DIRNAME:
       
   230 		BIO_printf(out, "DirName: ");
       
   231 		X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
       
   232 		break;
       
   233 
       
   234 		case GEN_IPADD:
       
   235 		p = gen->d.ip->data;
       
   236 		if(gen->d.ip->length == 4)
       
   237 			BIO_printf(out, "IP Address:%d.%d.%d.%d",
       
   238 						p[0], p[1], p[2], p[3]);
       
   239 		else if(gen->d.ip->length == 16)
       
   240 			{
       
   241 			BIO_printf(out, "IP Address");
       
   242 			for (i = 0; i < 8; i++)
       
   243 				{
       
   244 				BIO_printf(out, ":%X", p[0] << 8 | p[1]);
       
   245 				p += 2;
       
   246 				}
       
   247 			BIO_puts(out, "\n");
       
   248 			}
       
   249 		else
       
   250 			{
       
   251 			BIO_printf(out,"IP Address:<invalid>");
       
   252 			break;
       
   253 			}
       
   254 		break;
       
   255 
       
   256 		case GEN_RID:
       
   257 		BIO_printf(out, "Registered ID");
       
   258 		i2a_ASN1_OBJECT(out, gen->d.rid);
       
   259 		break;
       
   260 	}
       
   261 	return 1;
       
   262 }
       
   263 
       
   264 static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
       
   265 				 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
       
   266 {
       
   267 	GENERAL_NAMES *gens = NULL;
       
   268 	CONF_VALUE *cnf;
       
   269 	int i;
       
   270 	if(!(gens = sk_GENERAL_NAME_new_null())) {
       
   271 		X509V3err(X509V3_F_V2I_ISSUER_ALT,ERR_R_MALLOC_FAILURE);
       
   272 		return NULL;
       
   273 	}
       
   274 	for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
       
   275 		cnf = sk_CONF_VALUE_value(nval, i);
       
   276 		if(!name_cmp(cnf->name, "issuer") && cnf->value &&
       
   277 						!strcmp(cnf->value, "copy")) {
       
   278 			if(!copy_issuer(ctx, gens)) goto err;
       
   279 		} else {
       
   280 			GENERAL_NAME *gen;
       
   281 			if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
       
   282 								 goto err; 
       
   283 			sk_GENERAL_NAME_push(gens, gen);
       
   284 		}
       
   285 	}
       
   286 	return gens;
       
   287 	err:
       
   288 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
       
   289 	return NULL;
       
   290 }
       
   291 
       
   292 /* Append subject altname of issuer to issuer alt name of subject */
       
   293 
       
   294 static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
       
   295 {
       
   296 	GENERAL_NAMES *ialt;
       
   297 	GENERAL_NAME *gen;
       
   298 	X509_EXTENSION *ext;
       
   299 	int i;
       
   300 	if(ctx && (ctx->flags == CTX_TEST)) return 1;
       
   301 	if(!ctx || !ctx->issuer_cert) {
       
   302 		X509V3err(X509V3_F_COPY_ISSUER,X509V3_R_NO_ISSUER_DETAILS);
       
   303 		goto err;
       
   304 	}
       
   305         i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
       
   306 	if(i < 0) return 1;
       
   307         if(!(ext = X509_get_ext(ctx->issuer_cert, i)) ||
       
   308                         !(ialt = X509V3_EXT_d2i(ext)) ) {
       
   309 		X509V3err(X509V3_F_COPY_ISSUER,X509V3_R_ISSUER_DECODE_ERROR);
       
   310 		goto err;
       
   311 	}
       
   312 
       
   313 	for(i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
       
   314 		gen = sk_GENERAL_NAME_value(ialt, i);
       
   315 		if(!sk_GENERAL_NAME_push(gens, gen)) {
       
   316 			X509V3err(X509V3_F_COPY_ISSUER,ERR_R_MALLOC_FAILURE);
       
   317 			goto err;
       
   318 		}
       
   319 	}
       
   320 	sk_GENERAL_NAME_free(ialt);
       
   321 
       
   322 	return 1;
       
   323 		
       
   324 	err:
       
   325 	return 0;
       
   326 	
       
   327 }
       
   328 
       
   329 static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
       
   330 				 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
       
   331 {
       
   332 	GENERAL_NAMES *gens = NULL;
       
   333 	CONF_VALUE *cnf;
       
   334 	int i;
       
   335 	if(!(gens = sk_GENERAL_NAME_new_null())) {
       
   336 		X509V3err(X509V3_F_V2I_SUBJECT_ALT,ERR_R_MALLOC_FAILURE);
       
   337 		return NULL;
       
   338 	}
       
   339 	for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
       
   340 		cnf = sk_CONF_VALUE_value(nval, i);
       
   341 		if(!name_cmp(cnf->name, "email") && cnf->value &&
       
   342 						!strcmp(cnf->value, "copy")) {
       
   343 			if(!copy_email(ctx, gens, 0)) goto err;
       
   344 		} else if(!name_cmp(cnf->name, "email") && cnf->value &&
       
   345 						!strcmp(cnf->value, "move")) {
       
   346 			if(!copy_email(ctx, gens, 1)) goto err;
       
   347 		} else {
       
   348 			GENERAL_NAME *gen;
       
   349 			if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
       
   350 								 goto err; 
       
   351 			sk_GENERAL_NAME_push(gens, gen);
       
   352 		}
       
   353 	}
       
   354 	return gens;
       
   355 	err:
       
   356 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
       
   357 	return NULL;
       
   358 }
       
   359 
       
   360 /* Copy any email addresses in a certificate or request to 
       
   361  * GENERAL_NAMES
       
   362  */
       
   363 
       
   364 static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
       
   365 {
       
   366 	X509_NAME *nm;
       
   367 	ASN1_IA5STRING *email = NULL;
       
   368 	X509_NAME_ENTRY *ne;
       
   369 	GENERAL_NAME *gen = NULL;
       
   370 	int i;
       
   371 	if(ctx != NULL && ctx->flags == CTX_TEST)
       
   372 		return 1;
       
   373 	if(!ctx || (!ctx->subject_cert && !ctx->subject_req)) {
       
   374 		X509V3err(X509V3_F_COPY_EMAIL,X509V3_R_NO_SUBJECT_DETAILS);
       
   375 		goto err;
       
   376 	}
       
   377 	/* Find the subject name */
       
   378 	if(ctx->subject_cert) nm = X509_get_subject_name(ctx->subject_cert);
       
   379 	else nm = X509_REQ_get_subject_name(ctx->subject_req);
       
   380 
       
   381 	/* Now add any email address(es) to STACK */
       
   382 	i = -1;
       
   383 	while((i = X509_NAME_get_index_by_NID(nm,
       
   384 					 NID_pkcs9_emailAddress, i)) >= 0) {
       
   385 		ne = X509_NAME_get_entry(nm, i);
       
   386 		email = M_ASN1_IA5STRING_dup(X509_NAME_ENTRY_get_data(ne));
       
   387                 if (move_p)
       
   388                         {
       
   389                         X509_NAME_delete_entry(nm, i);
       
   390                         i--;
       
   391                         }
       
   392 		if(!email || !(gen = GENERAL_NAME_new())) {
       
   393 			X509V3err(X509V3_F_COPY_EMAIL,ERR_R_MALLOC_FAILURE);
       
   394 			goto err;
       
   395 		}
       
   396 		gen->d.ia5 = email;
       
   397 		email = NULL;
       
   398 		gen->type = GEN_EMAIL;
       
   399 		if(!sk_GENERAL_NAME_push(gens, gen)) {
       
   400 			X509V3err(X509V3_F_COPY_EMAIL,ERR_R_MALLOC_FAILURE);
       
   401 			goto err;
       
   402 		}
       
   403 		gen = NULL;
       
   404 	}
       
   405 
       
   406 	
       
   407 	return 1;
       
   408 		
       
   409 	err:
       
   410 	GENERAL_NAME_free(gen);
       
   411 	M_ASN1_IA5STRING_free(email);
       
   412 	return 0;
       
   413 	
       
   414 }
       
   415 
       
   416 EXPORT_C GENERAL_NAMES *v2i_GENERAL_NAMES(X509V3_EXT_METHOD *method,
       
   417 				X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
       
   418 {
       
   419 	GENERAL_NAME *gen;
       
   420 	GENERAL_NAMES *gens = NULL;
       
   421 	CONF_VALUE *cnf;
       
   422 	int i;
       
   423 	if(!(gens = sk_GENERAL_NAME_new_null())) {
       
   424 		X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
       
   425 		return NULL;
       
   426 	}
       
   427 	for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
       
   428 		cnf = sk_CONF_VALUE_value(nval, i);
       
   429 		if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) goto err; 
       
   430 		sk_GENERAL_NAME_push(gens, gen);
       
   431 	}
       
   432 	return gens;
       
   433 	err:
       
   434 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
       
   435 	return NULL;
       
   436 }
       
   437 
       
   438 EXPORT_C GENERAL_NAME *v2i_GENERAL_NAME(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
       
   439 							 CONF_VALUE *cnf)
       
   440 	{
       
   441 	return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
       
   442 	}
       
   443 
       
   444 EXPORT_C GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
       
   445 				X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
       
   446 						 CONF_VALUE *cnf, int is_nc)
       
   447 	{
       
   448 	char is_string = 0;
       
   449 	int type;
       
   450 	GENERAL_NAME *gen = NULL;
       
   451 
       
   452 	char *name, *value;
       
   453 
       
   454 	name = cnf->name;
       
   455 	value = cnf->value;
       
   456 
       
   457 	if(!value)
       
   458 		{
       
   459 		X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_MISSING_VALUE);
       
   460 		return NULL;
       
   461 		}
       
   462 
       
   463 	if (out)
       
   464 		gen = out;
       
   465 	else
       
   466 		{
       
   467 		gen = GENERAL_NAME_new();
       
   468 		if(gen == NULL)
       
   469 			{
       
   470 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,ERR_R_MALLOC_FAILURE);
       
   471 			return NULL;
       
   472 			}
       
   473 		}
       
   474 
       
   475 	if(!name_cmp(name, "email"))
       
   476 		{
       
   477 		is_string = 1;
       
   478 		type = GEN_EMAIL;
       
   479 		}
       
   480 	else if(!name_cmp(name, "URI"))
       
   481 		{
       
   482 		is_string = 1;
       
   483 		type = GEN_URI;
       
   484 		}
       
   485 	else if(!name_cmp(name, "DNS"))
       
   486 		{
       
   487 		is_string = 1;
       
   488 		type = GEN_DNS;
       
   489 		}
       
   490 	else if(!name_cmp(name, "RID"))
       
   491 		{
       
   492 		ASN1_OBJECT *obj;
       
   493 		if(!(obj = OBJ_txt2obj(value,0)))
       
   494 			{
       
   495 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_BAD_OBJECT);
       
   496 			ERR_add_error_data(2, "value=", value);
       
   497 			goto err;
       
   498 			}
       
   499 		gen->d.rid = obj;
       
   500 		type = GEN_RID;
       
   501 		}
       
   502 	else if(!name_cmp(name, "IP"))
       
   503 		{
       
   504 		if (is_nc)
       
   505 			gen->d.ip = a2i_IPADDRESS_NC(value);
       
   506 		else
       
   507 			gen->d.ip = a2i_IPADDRESS(value);
       
   508 		if(gen->d.ip == NULL)
       
   509 			{
       
   510 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_BAD_IP_ADDRESS);
       
   511 			ERR_add_error_data(2, "value=", value);
       
   512 			goto err;
       
   513 			}
       
   514 		type = GEN_IPADD;
       
   515 		}
       
   516 	else if(!name_cmp(name, "dirName"))
       
   517 		{
       
   518 		type = GEN_DIRNAME;
       
   519 		if (!do_dirname(gen, value, ctx))
       
   520 			{
       
   521 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_DIRNAME_ERROR);
       
   522 			goto err;
       
   523 			}
       
   524 		}
       
   525 	else if(!name_cmp(name, "otherName"))
       
   526 		{
       
   527 		if (!do_othername(gen, value, ctx))
       
   528 			{
       
   529 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_OTHERNAME_ERROR);
       
   530 			goto err;
       
   531 			}
       
   532 		type = GEN_OTHERNAME;
       
   533 		}
       
   534 	else
       
   535 		{
       
   536 		X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_UNSUPPORTED_OPTION);
       
   537 		ERR_add_error_data(2, "name=", name);
       
   538 		goto err;
       
   539 		}
       
   540 
       
   541 	if(is_string)
       
   542 		{
       
   543 		if(!(gen->d.ia5 = M_ASN1_IA5STRING_new()) ||
       
   544 			      !ASN1_STRING_set(gen->d.ia5, (unsigned char*)value,
       
   545 					       strlen(value)))
       
   546 			{
       
   547 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,ERR_R_MALLOC_FAILURE);
       
   548 			goto err;
       
   549 			}
       
   550 		}
       
   551 
       
   552 	gen->type = type;
       
   553 
       
   554 	return gen;
       
   555 
       
   556 	err:
       
   557 	GENERAL_NAME_free(gen);
       
   558 	return NULL;
       
   559 	}
       
   560 
       
   561 static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
       
   562 	{
       
   563 	char *objtmp = NULL, *p;
       
   564 	int objlen;
       
   565 	if (!(p = strchr(value, ';')))
       
   566 		return 0;
       
   567 	if (!(gen->d.otherName = OTHERNAME_new()))
       
   568 		return 0;
       
   569 	/* Free this up because we will overwrite it.
       
   570 	 * no need to free type_id because it is static
       
   571 	 */
       
   572 	ASN1_TYPE_free(gen->d.otherName->value);
       
   573 	if (!(gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)))
       
   574 		return 0;
       
   575 	objlen = p - value;
       
   576 	objtmp = OPENSSL_malloc(objlen + 1);
       
   577 #ifdef SYMBIAN
       
   578 	if(objtmp==NULL)
       
   579 	return 0;
       
   580 #endif
       
   581 	strncpy(objtmp, value, objlen);
       
   582 	objtmp[objlen] = 0;
       
   583 	gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
       
   584 	OPENSSL_free(objtmp);	
       
   585 	if (!gen->d.otherName->type_id)
       
   586 		return 0;
       
   587 	return 1;
       
   588 	}
       
   589 
       
   590 static int do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
       
   591 	{
       
   592 	int ret;
       
   593 	STACK_OF(CONF_VALUE) *sk;
       
   594 	X509_NAME *nm;
       
   595 	if (!(nm = X509_NAME_new()))
       
   596 		return 0;
       
   597 	sk = X509V3_get_section(ctx, value);
       
   598 	if (!sk)
       
   599 		{
       
   600 		X509V3err(X509V3_F_DO_DIRNAME,X509V3_R_SECTION_NOT_FOUND);
       
   601 		ERR_add_error_data(2, "section=", value);
       
   602 		X509_NAME_free(nm);
       
   603 		return 0;
       
   604 		}
       
   605 	/* FIXME: should allow other character types... */
       
   606 	ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
       
   607 	if (!ret)
       
   608 		X509_NAME_free(nm);
       
   609 	gen->d.dirn = nm;
       
   610 		
       
   611 	return ret;
       
   612 	}