os/ossrv/ssl/libcrypto/src/crypto/x509v3/v3_cpols.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* v3_cpols.c */
     2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
     3  * project 1999.
     4  */
     5 /* ====================================================================
     6  * Copyright (c) 1999-2004 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/asn1.h>
    67 #include <openssl/asn1t.h>
    68 #include <openssl/x509v3.h>
    69 
    70 #include "pcy_int.h"
    71 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    72 #include "libcrypto_wsd_macros.h"
    73 #include "libcrypto_wsd.h"
    74 #endif
    75 
    76 /* Certificate policies extension support: this one is a bit complex... */
    77 
    78 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol, BIO *out, int indent);
    79 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *value);
    80 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals, int indent);
    81 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
    82 static POLICYINFO *policy_section(X509V3_CTX *ctx,
    83 				 STACK_OF(CONF_VALUE) *polstrs, int ia5org);
    84 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
    85 					STACK_OF(CONF_VALUE) *unot, int ia5org);
    86 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
    87 
    88 #ifndef EMULATOR
    89 X509V3_EXT_METHOD v3_cpols = {
    90 NID_certificate_policies, 0,ASN1_ITEM_ref(CERTIFICATEPOLICIES),
    91 0,0,0,0,
    92 0,0,
    93 0,0,
    94 (X509V3_EXT_I2R)i2r_certpol,
    95 (X509V3_EXT_R2I)r2i_certpol,
    96 NULL
    97 };
    98 #else
    99 const X509V3_EXT_METHOD v3_cpols = {
   100 NID_certificate_policies, 0,ASN1_ITEM_ref(CERTIFICATEPOLICIES),
   101 0,0,0,0,
   102 0,0,
   103 0,0,
   104 (X509V3_EXT_I2R)i2r_certpol,
   105 (X509V3_EXT_R2I)r2i_certpol,
   106 NULL
   107 };
   108 #endif
   109 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) = 
   110 	ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
   111 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
   112 
   113 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
   114 
   115 ASN1_SEQUENCE(POLICYINFO) = {
   116 	ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
   117 	ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
   118 } ASN1_SEQUENCE_END(POLICYINFO)
   119 
   120 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
   121 
   122 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
   123 
   124 ASN1_ADB(POLICYQUALINFO) = {
   125 	ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
   126 	ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
   127 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
   128 
   129 ASN1_SEQUENCE(POLICYQUALINFO) = {
   130 	ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
   131 	ASN1_ADB_OBJECT(POLICYQUALINFO)
   132 } ASN1_SEQUENCE_END(POLICYQUALINFO)
   133 
   134 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
   135 
   136 ASN1_SEQUENCE(USERNOTICE) = {
   137 	ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
   138 	ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
   139 } ASN1_SEQUENCE_END(USERNOTICE)
   140 
   141 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
   142 
   143 ASN1_SEQUENCE(NOTICEREF) = {
   144 	ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
   145 	ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
   146 } ASN1_SEQUENCE_END(NOTICEREF)
   147 
   148 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
   149 
   150 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
   151 		X509V3_CTX *ctx, char *value)
   152 {
   153 	STACK_OF(POLICYINFO) *pols = NULL;
   154 	char *pstr;
   155 	POLICYINFO *pol;
   156 	ASN1_OBJECT *pobj;
   157 	STACK_OF(CONF_VALUE) *vals;
   158 	CONF_VALUE *cnf;
   159 	int i, ia5org;
   160 	pols = sk_POLICYINFO_new_null();
   161 	if (pols == NULL) {
   162 		X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
   163 		return NULL;
   164 	}
   165 	vals =  X509V3_parse_list(value);
   166 	if (vals == NULL) {
   167 		X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
   168 		goto err;
   169 	}
   170 	ia5org = 0;
   171 	for(i = 0; i < sk_CONF_VALUE_num(vals); i++) {
   172 		cnf = sk_CONF_VALUE_value(vals, i);
   173 		if(cnf->value || !cnf->name ) {
   174 			X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_POLICY_IDENTIFIER);
   175 			X509V3_conf_err(cnf);
   176 			goto err;
   177 		}
   178 		pstr = cnf->name;
   179 		if(!strcmp(pstr,"ia5org")) {
   180 			ia5org = 1;
   181 			continue;
   182 		} else if(*pstr == '@') {
   183 			STACK_OF(CONF_VALUE) *polsect;
   184 			polsect = X509V3_get_section(ctx, pstr + 1);
   185 			if(!polsect) {
   186 				X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_SECTION);
   187 
   188 				X509V3_conf_err(cnf);
   189 				goto err;
   190 			}
   191 			pol = policy_section(ctx, polsect, ia5org);
   192 			X509V3_section_free(ctx, polsect);
   193 			if(!pol) goto err;
   194 		} else {
   195 			if(!(pobj = OBJ_txt2obj(cnf->name, 0))) {
   196 				X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_OBJECT_IDENTIFIER);
   197 				X509V3_conf_err(cnf);
   198 				goto err;
   199 			}
   200 			pol = POLICYINFO_new();
   201 			pol->policyid = pobj;
   202 		}
   203 		sk_POLICYINFO_push(pols, pol);
   204 	}
   205 	sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
   206 	return pols;
   207 	err:
   208 	sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
   209 	sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
   210 	return NULL;
   211 }
   212 
   213 static POLICYINFO *policy_section(X509V3_CTX *ctx,
   214 				STACK_OF(CONF_VALUE) *polstrs, int ia5org)
   215 {
   216 	int i;
   217 	CONF_VALUE *cnf;
   218 	POLICYINFO *pol;
   219 	POLICYQUALINFO *qual;
   220 	if(!(pol = POLICYINFO_new())) goto merr;
   221 	for(i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
   222 		cnf = sk_CONF_VALUE_value(polstrs, i);
   223 		if(!strcmp(cnf->name, "policyIdentifier")) {
   224 			ASN1_OBJECT *pobj;
   225 			if(!(pobj = OBJ_txt2obj(cnf->value, 0))) {
   226 				X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OBJECT_IDENTIFIER);
   227 				X509V3_conf_err(cnf);
   228 				goto err;
   229 			}
   230 			pol->policyid = pobj;
   231 
   232 		} else if(!name_cmp(cnf->name, "CPS")) {
   233 			if(!pol->qualifiers) pol->qualifiers =
   234 						 sk_POLICYQUALINFO_new_null();
   235 			if(!(qual = POLICYQUALINFO_new())) goto merr;
   236 			if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
   237 								 goto merr;
   238 			qual->pqualid = OBJ_nid2obj(NID_id_qt_cps);
   239 			qual->d.cpsuri = M_ASN1_IA5STRING_new();
   240 			if(!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
   241 						 strlen(cnf->value))) goto merr;
   242 		} else if(!name_cmp(cnf->name, "userNotice")) {
   243 			STACK_OF(CONF_VALUE) *unot;
   244 			if(*cnf->value != '@') {
   245 				X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_EXPECTED_A_SECTION_NAME);
   246 				X509V3_conf_err(cnf);
   247 				goto err;
   248 			}
   249 			unot = X509V3_get_section(ctx, cnf->value + 1);
   250 			if(!unot) {
   251 				X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_SECTION);
   252 
   253 				X509V3_conf_err(cnf);
   254 				goto err;
   255 			}
   256 			qual = notice_section(ctx, unot, ia5org);
   257 			X509V3_section_free(ctx, unot);
   258 			if(!qual) goto err;
   259 			if(!pol->qualifiers) pol->qualifiers =
   260 						 sk_POLICYQUALINFO_new_null();
   261 			if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
   262 								 goto merr;
   263 		} else {
   264 			X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OPTION);
   265 
   266 			X509V3_conf_err(cnf);
   267 			goto err;
   268 		}
   269 	}
   270 	if(!pol->policyid) {
   271 		X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_NO_POLICY_IDENTIFIER);
   272 		goto err;
   273 	}
   274 
   275 	return pol;
   276 
   277 	merr:
   278 	X509V3err(X509V3_F_POLICY_SECTION,ERR_R_MALLOC_FAILURE);
   279 
   280 	err:
   281 	POLICYINFO_free(pol);
   282 	return NULL;
   283 	
   284 	
   285 }
   286 
   287 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
   288 					STACK_OF(CONF_VALUE) *unot, int ia5org)
   289 {
   290 	int i, ret;
   291 	CONF_VALUE *cnf;
   292 	USERNOTICE *not;
   293 	POLICYQUALINFO *qual;
   294 	if(!(qual = POLICYQUALINFO_new())) goto merr;
   295 	qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice);
   296 	if(!(not = USERNOTICE_new())) goto merr;
   297 	qual->d.usernotice = not;
   298 	for(i = 0; i < sk_CONF_VALUE_num(unot); i++) {
   299 		cnf = sk_CONF_VALUE_value(unot, i);
   300 		if(!strcmp(cnf->name, "explicitText")) {
   301 			not->exptext = M_ASN1_VISIBLESTRING_new();
   302 			if(!ASN1_STRING_set(not->exptext, cnf->value,
   303 						 strlen(cnf->value))) goto merr;
   304 		} else if(!strcmp(cnf->name, "organization")) {
   305 			NOTICEREF *nref;
   306 			if(!not->noticeref) {
   307 				if(!(nref = NOTICEREF_new())) goto merr;
   308 				not->noticeref = nref;
   309 			} else nref = not->noticeref;
   310 			if(ia5org) nref->organization->type = V_ASN1_IA5STRING;
   311 			else nref->organization->type = V_ASN1_VISIBLESTRING;
   312 			if(!ASN1_STRING_set(nref->organization, cnf->value,
   313 						 strlen(cnf->value))) goto merr;
   314 		} else if(!strcmp(cnf->name, "noticeNumbers")) {
   315 			NOTICEREF *nref;
   316 			STACK_OF(CONF_VALUE) *nos;
   317 			if(!not->noticeref) {
   318 				if(!(nref = NOTICEREF_new())) goto merr;
   319 				not->noticeref = nref;
   320 			} else nref = not->noticeref;
   321 			nos = X509V3_parse_list(cnf->value);
   322 			if(!nos || !sk_CONF_VALUE_num(nos)) {
   323 				X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_NUMBERS);
   324 				X509V3_conf_err(cnf);
   325 				goto err;
   326 			}
   327 			ret = nref_nos(nref->noticenos, nos);
   328 			sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
   329 			if (!ret)
   330 				goto err;
   331 		} else {
   332 			X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_OPTION);
   333 			X509V3_conf_err(cnf);
   334 			goto err;
   335 		}
   336 	}
   337 
   338 	if(not->noticeref && 
   339 	      (!not->noticeref->noticenos || !not->noticeref->organization)) {
   340 			X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
   341 			goto err;
   342 	}
   343 
   344 	return qual;
   345 
   346 	merr:
   347 	X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
   348 
   349 	err:
   350 	POLICYQUALINFO_free(qual);
   351 	return NULL;
   352 }
   353 
   354 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
   355 {
   356 	CONF_VALUE *cnf;
   357 	ASN1_INTEGER *aint;
   358 
   359 	int i;
   360 
   361 	for(i = 0; i < sk_CONF_VALUE_num(nos); i++) {
   362 		cnf = sk_CONF_VALUE_value(nos, i);
   363 		if(!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
   364 			X509V3err(X509V3_F_NREF_NOS,X509V3_R_INVALID_NUMBER);
   365 			goto err;
   366 		}
   367 		if(!sk_ASN1_INTEGER_push(nnums, aint)) goto merr;
   368 	}
   369 	return 1;
   370 
   371 	merr:
   372 	X509V3err(X509V3_F_NREF_NOS,ERR_R_MALLOC_FAILURE);
   373 
   374 	err:
   375 	sk_ASN1_INTEGER_pop_free(nnums, ASN1_STRING_free);
   376 	return 0;
   377 }
   378 
   379 
   380 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
   381 		BIO *out, int indent)
   382 {
   383 	int i;
   384 	POLICYINFO *pinfo;
   385 	/* First print out the policy OIDs */
   386 	for(i = 0; i < sk_POLICYINFO_num(pol); i++) {
   387 		pinfo = sk_POLICYINFO_value(pol, i);
   388 		BIO_printf(out, "%*sPolicy: ", indent, "");
   389 		i2a_ASN1_OBJECT(out, pinfo->policyid);
   390 		BIO_puts(out, "\n");
   391 		if(pinfo->qualifiers)
   392 			 print_qualifiers(out, pinfo->qualifiers, indent + 2);
   393 	}
   394 	return 1;
   395 }
   396 
   397 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
   398 		int indent)
   399 {
   400 	POLICYQUALINFO *qualinfo;
   401 	int i;
   402 	for(i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
   403 		qualinfo = sk_POLICYQUALINFO_value(quals, i);
   404 		switch(OBJ_obj2nid(qualinfo->pqualid))
   405 		{
   406 			case NID_id_qt_cps:
   407 			BIO_printf(out, "%*sCPS: %s\n", indent, "",
   408 						qualinfo->d.cpsuri->data);
   409 			break;
   410 		
   411 			case NID_id_qt_unotice:
   412 			BIO_printf(out, "%*sUser Notice:\n", indent, "");
   413 			print_notice(out, qualinfo->d.usernotice, indent + 2);
   414 			break;
   415 
   416 			default:
   417 			BIO_printf(out, "%*sUnknown Qualifier: ",
   418 							 indent + 2, "");
   419 			
   420 			i2a_ASN1_OBJECT(out, qualinfo->pqualid);
   421 			BIO_puts(out, "\n");
   422 			break;
   423 		}
   424 	}
   425 }
   426 
   427 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
   428 {
   429 	int i;
   430 	if(notice->noticeref) {
   431 		NOTICEREF *ref;
   432 		ref = notice->noticeref;
   433 		BIO_printf(out, "%*sOrganization: %s\n", indent, "",
   434 						 ref->organization->data);
   435 		BIO_printf(out, "%*sNumber%s: ", indent, "",
   436 			   sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
   437 		for(i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
   438 			ASN1_INTEGER *num;
   439 			char *tmp;
   440 			num = sk_ASN1_INTEGER_value(ref->noticenos, i);
   441 			if(i) BIO_puts(out, ", ");
   442 			tmp = i2s_ASN1_INTEGER(NULL, num);
   443 			BIO_puts(out, tmp);
   444 			OPENSSL_free(tmp);
   445 		}
   446 		BIO_puts(out, "\n");
   447 	}
   448 	if(notice->exptext)
   449 		BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
   450 							 notice->exptext->data);
   451 }
   452 
   453 EXPORT_C void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
   454 	{
   455 	const X509_POLICY_DATA *dat = node->data;
   456 
   457 	BIO_printf(out, "%*sPolicy: ", indent, "");
   458 			
   459 	i2a_ASN1_OBJECT(out, dat->valid_policy);
   460 	BIO_puts(out, "\n");
   461 	BIO_printf(out, "%*s%s\n", indent + 2, "",
   462 		node_data_critical(dat) ? "Critical" : "Non Critical");
   463 	if (dat->qualifier_set)
   464 		print_qualifiers(out, dat->qualifier_set, indent + 2);
   465 	else
   466 		BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
   467 	}
   468