xref: /onnv-gate/usr/src/common/openssl/crypto/x509v3/v3_akey.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* v3_akey.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate  * project 1999.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/conf.h>
620Sstevel@tonic-gate #include <openssl/asn1.h>
630Sstevel@tonic-gate #include <openssl/asn1t.h>
640Sstevel@tonic-gate #include <openssl/x509v3.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
670Sstevel@tonic-gate 			AUTHORITY_KEYID *akeyid, STACK_OF(CONF_VALUE) *extlist);
680Sstevel@tonic-gate static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
690Sstevel@tonic-gate 			X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *values);
700Sstevel@tonic-gate 
71*2139Sjp161948 X509V3_EXT_METHOD v3_akey_id =
72*2139Sjp161948 	{
73*2139Sjp161948 	NID_authority_key_identifier,
74*2139Sjp161948 	X509V3_EXT_MULTILINE, ASN1_ITEM_ref(AUTHORITY_KEYID),
75*2139Sjp161948 	0,0,0,0,
76*2139Sjp161948 	0,0,
77*2139Sjp161948 	(X509V3_EXT_I2V)i2v_AUTHORITY_KEYID,
78*2139Sjp161948 	(X509V3_EXT_V2I)v2i_AUTHORITY_KEYID,
79*2139Sjp161948 	0,0,
80*2139Sjp161948 	NULL
81*2139Sjp161948 	};
820Sstevel@tonic-gate 
STACK_OF(CONF_VALUE)830Sstevel@tonic-gate static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
840Sstevel@tonic-gate 	     AUTHORITY_KEYID *akeyid, STACK_OF(CONF_VALUE) *extlist)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	char *tmp;
870Sstevel@tonic-gate 	if(akeyid->keyid) {
880Sstevel@tonic-gate 		tmp = hex_to_string(akeyid->keyid->data, akeyid->keyid->length);
890Sstevel@tonic-gate 		X509V3_add_value("keyid", tmp, &extlist);
900Sstevel@tonic-gate 		OPENSSL_free(tmp);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 	if(akeyid->issuer)
930Sstevel@tonic-gate 		extlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist);
940Sstevel@tonic-gate 	if(akeyid->serial) {
950Sstevel@tonic-gate 		tmp = hex_to_string(akeyid->serial->data,
960Sstevel@tonic-gate 						 akeyid->serial->length);
970Sstevel@tonic-gate 		X509V3_add_value("serial", tmp, &extlist);
980Sstevel@tonic-gate 		OPENSSL_free(tmp);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 	return extlist;
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /* Currently two options:
1040Sstevel@tonic-gate  * keyid: use the issuers subject keyid, the value 'always' means its is
1050Sstevel@tonic-gate  * an error if the issuer certificate doesn't have a key id.
1060Sstevel@tonic-gate  * issuer: use the issuers cert issuer and serial number. The default is
1070Sstevel@tonic-gate  * to only use this if keyid is not present. With the option 'always'
1080Sstevel@tonic-gate  * this is always included.
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate 
v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* values)1110Sstevel@tonic-gate static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
1120Sstevel@tonic-gate 	     X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *values)
113*2139Sjp161948 	{
114*2139Sjp161948 	char keyid=0, issuer=0;
115*2139Sjp161948 	int i;
116*2139Sjp161948 	CONF_VALUE *cnf;
117*2139Sjp161948 	ASN1_OCTET_STRING *ikeyid = NULL;
118*2139Sjp161948 	X509_NAME *isname = NULL;
119*2139Sjp161948 	GENERAL_NAMES * gens = NULL;
120*2139Sjp161948 	GENERAL_NAME *gen = NULL;
121*2139Sjp161948 	ASN1_INTEGER *serial = NULL;
122*2139Sjp161948 	X509_EXTENSION *ext;
123*2139Sjp161948 	X509 *cert;
124*2139Sjp161948 	AUTHORITY_KEYID *akeyid;
125*2139Sjp161948 
126*2139Sjp161948 	for(i = 0; i < sk_CONF_VALUE_num(values); i++)
127*2139Sjp161948 		{
128*2139Sjp161948 		cnf = sk_CONF_VALUE_value(values, i);
129*2139Sjp161948 		if(!strcmp(cnf->name, "keyid"))
130*2139Sjp161948 			{
131*2139Sjp161948 			keyid = 1;
132*2139Sjp161948 			if(cnf->value && !strcmp(cnf->value, "always"))
133*2139Sjp161948 				keyid = 2;
134*2139Sjp161948 			}
135*2139Sjp161948 		else if(!strcmp(cnf->name, "issuer"))
136*2139Sjp161948 			{
137*2139Sjp161948 			issuer = 1;
138*2139Sjp161948 			if(cnf->value && !strcmp(cnf->value, "always"))
139*2139Sjp161948 				issuer = 2;
140*2139Sjp161948 			}
141*2139Sjp161948 		else
142*2139Sjp161948 			{
143*2139Sjp161948 			X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,X509V3_R_UNKNOWN_OPTION);
144*2139Sjp161948 			ERR_add_error_data(2, "name=", cnf->name);
145*2139Sjp161948 			return NULL;
146*2139Sjp161948 			}
147*2139Sjp161948 		}
148*2139Sjp161948 
149*2139Sjp161948 	if(!ctx || !ctx->issuer_cert)
150*2139Sjp161948 		{
151*2139Sjp161948 		if(ctx && (ctx->flags==CTX_TEST))
152*2139Sjp161948 			return AUTHORITY_KEYID_new();
153*2139Sjp161948 		X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,X509V3_R_NO_ISSUER_CERTIFICATE);
1540Sstevel@tonic-gate 		return NULL;
155*2139Sjp161948 		}
1560Sstevel@tonic-gate 
157*2139Sjp161948 	cert = ctx->issuer_cert;
1580Sstevel@tonic-gate 
159*2139Sjp161948 	if(keyid)
160*2139Sjp161948 		{
161*2139Sjp161948 		i = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1);
162*2139Sjp161948 		if((i >= 0)  && (ext = X509_get_ext(cert, i)))
163*2139Sjp161948 			ikeyid = X509V3_EXT_d2i(ext);
164*2139Sjp161948 		if(keyid==2 && !ikeyid)
165*2139Sjp161948 			{
166*2139Sjp161948 			X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
167*2139Sjp161948 			return NULL;
168*2139Sjp161948 			}
169*2139Sjp161948 		}
1700Sstevel@tonic-gate 
171*2139Sjp161948 	if((issuer && !ikeyid) || (issuer == 2))
172*2139Sjp161948 		{
173*2139Sjp161948 		isname = X509_NAME_dup(X509_get_issuer_name(cert));
174*2139Sjp161948 		serial = M_ASN1_INTEGER_dup(X509_get_serialNumber(cert));
175*2139Sjp161948 		if(!isname || !serial)
176*2139Sjp161948 			{
177*2139Sjp161948 			X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS);
178*2139Sjp161948 			goto err;
179*2139Sjp161948 			}
180*2139Sjp161948 		}
1810Sstevel@tonic-gate 
182*2139Sjp161948 	if(!(akeyid = AUTHORITY_KEYID_new())) goto err;
1830Sstevel@tonic-gate 
184*2139Sjp161948 	if(isname)
185*2139Sjp161948 		{
186*2139Sjp161948 		if(!(gens = sk_GENERAL_NAME_new_null())
187*2139Sjp161948 			|| !(gen = GENERAL_NAME_new())
188*2139Sjp161948 			|| !sk_GENERAL_NAME_push(gens, gen))
189*2139Sjp161948 			{
190*2139Sjp161948 			X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,ERR_R_MALLOC_FAILURE);
191*2139Sjp161948 			goto err;
192*2139Sjp161948 			}
193*2139Sjp161948 		gen->type = GEN_DIRNAME;
194*2139Sjp161948 		gen->d.dirn = isname;
195*2139Sjp161948 		}
1960Sstevel@tonic-gate 
197*2139Sjp161948 	akeyid->issuer = gens;
198*2139Sjp161948 	akeyid->serial = serial;
199*2139Sjp161948 	akeyid->keyid = ikeyid;
2000Sstevel@tonic-gate 
201*2139Sjp161948 	return akeyid;
2020Sstevel@tonic-gate 
203*2139Sjp161948  err:
204*2139Sjp161948 	X509_NAME_free(isname);
205*2139Sjp161948 	M_ASN1_INTEGER_free(serial);
206*2139Sjp161948 	M_ASN1_OCTET_STRING_free(ikeyid);
207*2139Sjp161948 	return NULL;
208*2139Sjp161948 	}
209