10Sstevel@tonic-gate /* v3_lib.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 /* X509 v3 extension utilities */
590Sstevel@tonic-gate
600Sstevel@tonic-gate #include <stdio.h>
610Sstevel@tonic-gate #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/conf.h>
630Sstevel@tonic-gate #include <openssl/x509v3.h>
640Sstevel@tonic-gate
650Sstevel@tonic-gate #include "ext_dat.h"
660Sstevel@tonic-gate
670Sstevel@tonic-gate static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
680Sstevel@tonic-gate
690Sstevel@tonic-gate static int ext_cmp(const X509V3_EXT_METHOD * const *a,
700Sstevel@tonic-gate const X509V3_EXT_METHOD * const *b);
710Sstevel@tonic-gate static void ext_list_free(X509V3_EXT_METHOD *ext);
720Sstevel@tonic-gate
X509V3_EXT_add(X509V3_EXT_METHOD * ext)730Sstevel@tonic-gate int X509V3_EXT_add(X509V3_EXT_METHOD *ext)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate if(!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp))) {
760Sstevel@tonic-gate X509V3err(X509V3_F_X509V3_EXT_ADD,ERR_R_MALLOC_FAILURE);
770Sstevel@tonic-gate return 0;
780Sstevel@tonic-gate }
790Sstevel@tonic-gate if(!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
800Sstevel@tonic-gate X509V3err(X509V3_F_X509V3_EXT_ADD,ERR_R_MALLOC_FAILURE);
810Sstevel@tonic-gate return 0;
820Sstevel@tonic-gate }
830Sstevel@tonic-gate return 1;
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
ext_cmp(const X509V3_EXT_METHOD * const * a,const X509V3_EXT_METHOD * const * b)860Sstevel@tonic-gate static int ext_cmp(const X509V3_EXT_METHOD * const *a,
870Sstevel@tonic-gate const X509V3_EXT_METHOD * const *b)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate return ((*a)->ext_nid - (*b)->ext_nid);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
X509V3_EXT_get_nid(int nid)920Sstevel@tonic-gate X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate X509V3_EXT_METHOD tmp, *t = &tmp, **ret;
950Sstevel@tonic-gate int idx;
960Sstevel@tonic-gate if(nid < 0) return NULL;
970Sstevel@tonic-gate tmp.ext_nid = nid;
980Sstevel@tonic-gate ret = (X509V3_EXT_METHOD **) OBJ_bsearch((char *)&t,
990Sstevel@tonic-gate (char *)standard_exts, STANDARD_EXTENSION_COUNT,
1000Sstevel@tonic-gate sizeof(X509V3_EXT_METHOD *), (int (*)(const void *, const void *))ext_cmp);
1010Sstevel@tonic-gate if(ret) return *ret;
1020Sstevel@tonic-gate if(!ext_list) return NULL;
1030Sstevel@tonic-gate idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
1040Sstevel@tonic-gate if(idx == -1) return NULL;
1050Sstevel@tonic-gate return sk_X509V3_EXT_METHOD_value(ext_list, idx);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
X509V3_EXT_get(X509_EXTENSION * ext)1080Sstevel@tonic-gate X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate int nid;
1110Sstevel@tonic-gate if((nid = OBJ_obj2nid(ext->object)) == NID_undef) return NULL;
1120Sstevel@tonic-gate return X509V3_EXT_get_nid(nid);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate
X509V3_EXT_add_list(X509V3_EXT_METHOD * extlist)1160Sstevel@tonic-gate int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate for(;extlist->ext_nid!=-1;extlist++)
1190Sstevel@tonic-gate if(!X509V3_EXT_add(extlist)) return 0;
1200Sstevel@tonic-gate return 1;
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
X509V3_EXT_add_alias(int nid_to,int nid_from)1230Sstevel@tonic-gate int X509V3_EXT_add_alias(int nid_to, int nid_from)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate X509V3_EXT_METHOD *ext, *tmpext;
1260Sstevel@tonic-gate if(!(ext = X509V3_EXT_get_nid(nid_from))) {
1270Sstevel@tonic-gate X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,X509V3_R_EXTENSION_NOT_FOUND);
1280Sstevel@tonic-gate return 0;
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate if(!(tmpext = (X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD)))) {
1310Sstevel@tonic-gate X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,ERR_R_MALLOC_FAILURE);
1320Sstevel@tonic-gate return 0;
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate *tmpext = *ext;
1350Sstevel@tonic-gate tmpext->ext_nid = nid_to;
1360Sstevel@tonic-gate tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
1370Sstevel@tonic-gate return X509V3_EXT_add(tmpext);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
X509V3_EXT_cleanup(void)1400Sstevel@tonic-gate void X509V3_EXT_cleanup(void)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
1430Sstevel@tonic-gate ext_list = NULL;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
ext_list_free(X509V3_EXT_METHOD * ext)1460Sstevel@tonic-gate static void ext_list_free(X509V3_EXT_METHOD *ext)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate if(ext->ext_flags & X509V3_EXT_DYNAMIC) OPENSSL_free(ext);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate /* Legacy function: we don't need to add standard extensions
1520Sstevel@tonic-gate * any more because they are now kept in ext_dat.h.
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate
X509V3_add_standard_extensions(void)1550Sstevel@tonic-gate int X509V3_add_standard_extensions(void)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate return 1;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate /* Return an extension internal structure */
1610Sstevel@tonic-gate
X509V3_EXT_d2i(X509_EXTENSION * ext)1620Sstevel@tonic-gate void *X509V3_EXT_d2i(X509_EXTENSION *ext)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate X509V3_EXT_METHOD *method;
165*2139Sjp161948 const unsigned char *p;
166*2139Sjp161948
1670Sstevel@tonic-gate if(!(method = X509V3_EXT_get(ext))) return NULL;
1680Sstevel@tonic-gate p = ext->value->data;
1690Sstevel@tonic-gate if(method->it) return ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it));
1700Sstevel@tonic-gate return method->d2i(NULL, &p, ext->value->length);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /* Get critical flag and decoded version of extension from a NID.
1740Sstevel@tonic-gate * The "idx" variable returns the last found extension and can
1750Sstevel@tonic-gate * be used to retrieve multiple extensions of the same NID.
1760Sstevel@tonic-gate * However multiple extensions with the same NID is usually
1770Sstevel@tonic-gate * due to a badly encoded certificate so if idx is NULL we
1780Sstevel@tonic-gate * choke if multiple extensions exist.
1790Sstevel@tonic-gate * The "crit" variable is set to the critical value.
1800Sstevel@tonic-gate * The return value is the decoded extension or NULL on
1810Sstevel@tonic-gate * error. The actual error can have several different causes,
1820Sstevel@tonic-gate * the value of *crit reflects the cause:
1830Sstevel@tonic-gate * >= 0, extension found but not decoded (reflects critical value).
1840Sstevel@tonic-gate * -1 extension not found.
1850Sstevel@tonic-gate * -2 extension occurs more than once.
1860Sstevel@tonic-gate */
1870Sstevel@tonic-gate
X509V3_get_d2i(STACK_OF (X509_EXTENSION)* x,int nid,int * crit,int * idx)1880Sstevel@tonic-gate void *X509V3_get_d2i(STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate int lastpos, i;
1910Sstevel@tonic-gate X509_EXTENSION *ex, *found_ex = NULL;
1920Sstevel@tonic-gate if(!x) {
1930Sstevel@tonic-gate if(idx) *idx = -1;
1940Sstevel@tonic-gate if(crit) *crit = -1;
1950Sstevel@tonic-gate return NULL;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate if(idx) lastpos = *idx + 1;
1980Sstevel@tonic-gate else lastpos = 0;
1990Sstevel@tonic-gate if(lastpos < 0) lastpos = 0;
2000Sstevel@tonic-gate for(i = lastpos; i < sk_X509_EXTENSION_num(x); i++)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate ex = sk_X509_EXTENSION_value(x, i);
2030Sstevel@tonic-gate if(OBJ_obj2nid(ex->object) == nid) {
2040Sstevel@tonic-gate if(idx) {
2050Sstevel@tonic-gate *idx = i;
2060Sstevel@tonic-gate found_ex = ex;
2070Sstevel@tonic-gate break;
2080Sstevel@tonic-gate } else if(found_ex) {
2090Sstevel@tonic-gate /* Found more than one */
2100Sstevel@tonic-gate if(crit) *crit = -2;
2110Sstevel@tonic-gate return NULL;
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate found_ex = ex;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate if(found_ex) {
2170Sstevel@tonic-gate /* Found it */
2180Sstevel@tonic-gate if(crit) *crit = X509_EXTENSION_get_critical(found_ex);
2190Sstevel@tonic-gate return X509V3_EXT_d2i(found_ex);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate /* Extension not found */
2230Sstevel@tonic-gate if(idx) *idx = -1;
2240Sstevel@tonic-gate if(crit) *crit = -1;
2250Sstevel@tonic-gate return NULL;
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate /* This function is a general extension append, replace and delete utility.
2290Sstevel@tonic-gate * The precise operation is governed by the 'flags' value. The 'crit' and
2300Sstevel@tonic-gate * 'value' arguments (if relevant) are the extensions internal structure.
2310Sstevel@tonic-gate */
2320Sstevel@tonic-gate
X509V3_add1_i2d(STACK_OF (X509_EXTENSION)** x,int nid,void * value,int crit,unsigned long flags)2330Sstevel@tonic-gate int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
2340Sstevel@tonic-gate int crit, unsigned long flags)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate int extidx = -1;
2370Sstevel@tonic-gate int errcode;
2380Sstevel@tonic-gate X509_EXTENSION *ext, *extmp;
2390Sstevel@tonic-gate unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate /* If appending we don't care if it exists, otherwise
2420Sstevel@tonic-gate * look for existing extension.
2430Sstevel@tonic-gate */
2440Sstevel@tonic-gate if(ext_op != X509V3_ADD_APPEND)
2450Sstevel@tonic-gate extidx = X509v3_get_ext_by_NID(*x, nid, -1);
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate /* See if extension exists */
2480Sstevel@tonic-gate if(extidx >= 0) {
2490Sstevel@tonic-gate /* If keep existing, nothing to do */
2500Sstevel@tonic-gate if(ext_op == X509V3_ADD_KEEP_EXISTING)
2510Sstevel@tonic-gate return 1;
2520Sstevel@tonic-gate /* If default then its an error */
2530Sstevel@tonic-gate if(ext_op == X509V3_ADD_DEFAULT) {
2540Sstevel@tonic-gate errcode = X509V3_R_EXTENSION_EXISTS;
2550Sstevel@tonic-gate goto err;
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate /* If delete, just delete it */
2580Sstevel@tonic-gate if(ext_op == X509V3_ADD_DELETE) {
2590Sstevel@tonic-gate if(!sk_X509_EXTENSION_delete(*x, extidx)) return -1;
2600Sstevel@tonic-gate return 1;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate } else {
2630Sstevel@tonic-gate /* If replace existing or delete, error since
2640Sstevel@tonic-gate * extension must exist
2650Sstevel@tonic-gate */
2660Sstevel@tonic-gate if((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
2670Sstevel@tonic-gate (ext_op == X509V3_ADD_DELETE)) {
2680Sstevel@tonic-gate errcode = X509V3_R_EXTENSION_NOT_FOUND;
2690Sstevel@tonic-gate goto err;
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate /* If we get this far then we have to create an extension:
2740Sstevel@tonic-gate * could have some flags for alternative encoding schemes...
2750Sstevel@tonic-gate */
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate ext = X509V3_EXT_i2d(nid, crit, value);
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate if(!ext) {
280*2139Sjp161948 X509V3err(X509V3_F_X509V3_ADD1_I2D, X509V3_R_ERROR_CREATING_EXTENSION);
2810Sstevel@tonic-gate return 0;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate /* If extension exists replace it.. */
2850Sstevel@tonic-gate if(extidx >= 0) {
2860Sstevel@tonic-gate extmp = sk_X509_EXTENSION_value(*x, extidx);
2870Sstevel@tonic-gate X509_EXTENSION_free(extmp);
2880Sstevel@tonic-gate if(!sk_X509_EXTENSION_set(*x, extidx, ext)) return -1;
2890Sstevel@tonic-gate return 1;
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate if(!*x && !(*x = sk_X509_EXTENSION_new_null())) return -1;
2930Sstevel@tonic-gate if(!sk_X509_EXTENSION_push(*x, ext)) return -1;
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate return 1;
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate err:
2980Sstevel@tonic-gate if(!(flags & X509V3_ADD_SILENT))
299*2139Sjp161948 X509V3err(X509V3_F_X509V3_ADD1_I2D, errcode);
3000Sstevel@tonic-gate return 0;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate IMPLEMENT_STACK_OF(X509V3_EXT_METHOD)
304