10Sstevel@tonic-gate /* crypto/x509/x509_v3.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 <openssl/stack.h>
610Sstevel@tonic-gate #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/asn1.h>
630Sstevel@tonic-gate #include <openssl/objects.h>
640Sstevel@tonic-gate #include <openssl/evp.h>
650Sstevel@tonic-gate #include <openssl/x509.h>
660Sstevel@tonic-gate #include <openssl/x509v3.h>
670Sstevel@tonic-gate
X509v3_get_ext_count(const STACK_OF (X509_EXTENSION)* x)680Sstevel@tonic-gate int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate if (x == NULL) return(0);
710Sstevel@tonic-gate return(sk_X509_EXTENSION_num(x));
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
X509v3_get_ext_by_NID(const STACK_OF (X509_EXTENSION)* x,int nid,int lastpos)740Sstevel@tonic-gate int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
750Sstevel@tonic-gate int lastpos)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate ASN1_OBJECT *obj;
780Sstevel@tonic-gate
790Sstevel@tonic-gate obj=OBJ_nid2obj(nid);
800Sstevel@tonic-gate if (obj == NULL) return(-2);
810Sstevel@tonic-gate return(X509v3_get_ext_by_OBJ(x,obj,lastpos));
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
X509v3_get_ext_by_OBJ(const STACK_OF (X509_EXTENSION)* sk,ASN1_OBJECT * obj,int lastpos)840Sstevel@tonic-gate int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk, ASN1_OBJECT *obj,
850Sstevel@tonic-gate int lastpos)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate int n;
880Sstevel@tonic-gate X509_EXTENSION *ex;
890Sstevel@tonic-gate
900Sstevel@tonic-gate if (sk == NULL) return(-1);
910Sstevel@tonic-gate lastpos++;
920Sstevel@tonic-gate if (lastpos < 0)
930Sstevel@tonic-gate lastpos=0;
940Sstevel@tonic-gate n=sk_X509_EXTENSION_num(sk);
950Sstevel@tonic-gate for ( ; lastpos < n; lastpos++)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate ex=sk_X509_EXTENSION_value(sk,lastpos);
980Sstevel@tonic-gate if (OBJ_cmp(ex->object,obj) == 0)
990Sstevel@tonic-gate return(lastpos);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate return(-1);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
X509v3_get_ext_by_critical(const STACK_OF (X509_EXTENSION)* sk,int crit,int lastpos)1040Sstevel@tonic-gate int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
1050Sstevel@tonic-gate int lastpos)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate int n;
1080Sstevel@tonic-gate X509_EXTENSION *ex;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (sk == NULL) return(-1);
1110Sstevel@tonic-gate lastpos++;
1120Sstevel@tonic-gate if (lastpos < 0)
1130Sstevel@tonic-gate lastpos=0;
1140Sstevel@tonic-gate n=sk_X509_EXTENSION_num(sk);
1150Sstevel@tonic-gate for ( ; lastpos < n; lastpos++)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate ex=sk_X509_EXTENSION_value(sk,lastpos);
1180Sstevel@tonic-gate if ( ((ex->critical > 0) && crit) ||
1190Sstevel@tonic-gate ((ex->critical <= 0) && !crit))
1200Sstevel@tonic-gate return(lastpos);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate return(-1);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
X509v3_get_ext(const STACK_OF (X509_EXTENSION)* x,int loc)1250Sstevel@tonic-gate X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
1280Sstevel@tonic-gate return NULL;
1290Sstevel@tonic-gate else
1300Sstevel@tonic-gate return sk_X509_EXTENSION_value(x,loc);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
X509v3_delete_ext(STACK_OF (X509_EXTENSION)* x,int loc)1330Sstevel@tonic-gate X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate X509_EXTENSION *ret;
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
1380Sstevel@tonic-gate return(NULL);
1390Sstevel@tonic-gate ret=sk_X509_EXTENSION_delete(x,loc);
1400Sstevel@tonic-gate return(ret);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
STACK_OF(X509_EXTENSION)1430Sstevel@tonic-gate STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
1440Sstevel@tonic-gate X509_EXTENSION *ex, int loc)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate X509_EXTENSION *new_ex=NULL;
1470Sstevel@tonic-gate int n;
1480Sstevel@tonic-gate STACK_OF(X509_EXTENSION) *sk=NULL;
1490Sstevel@tonic-gate
150*2139Sjp161948 if (x == NULL)
151*2139Sjp161948 {
152*2139Sjp161948 X509err(X509_F_X509V3_ADD_EXT,ERR_R_PASSED_NULL_PARAMETER);
153*2139Sjp161948 goto err2;
154*2139Sjp161948 }
155*2139Sjp161948
156*2139Sjp161948 if (*x == NULL)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate if ((sk=sk_X509_EXTENSION_new_null()) == NULL)
1590Sstevel@tonic-gate goto err;
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate else
1620Sstevel@tonic-gate sk= *x;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate n=sk_X509_EXTENSION_num(sk);
1650Sstevel@tonic-gate if (loc > n) loc=n;
1660Sstevel@tonic-gate else if (loc < 0) loc=n;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if ((new_ex=X509_EXTENSION_dup(ex)) == NULL)
1690Sstevel@tonic-gate goto err2;
1700Sstevel@tonic-gate if (!sk_X509_EXTENSION_insert(sk,new_ex,loc))
1710Sstevel@tonic-gate goto err;
172*2139Sjp161948 if (*x == NULL)
1730Sstevel@tonic-gate *x=sk;
1740Sstevel@tonic-gate return(sk);
1750Sstevel@tonic-gate err:
1760Sstevel@tonic-gate X509err(X509_F_X509V3_ADD_EXT,ERR_R_MALLOC_FAILURE);
1770Sstevel@tonic-gate err2:
1780Sstevel@tonic-gate if (new_ex != NULL) X509_EXTENSION_free(new_ex);
1790Sstevel@tonic-gate if (sk != NULL) sk_X509_EXTENSION_free(sk);
1800Sstevel@tonic-gate return(NULL);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
X509_EXTENSION_create_by_NID(X509_EXTENSION ** ex,int nid,int crit,ASN1_OCTET_STRING * data)1830Sstevel@tonic-gate X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
1840Sstevel@tonic-gate int crit, ASN1_OCTET_STRING *data)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate ASN1_OBJECT *obj;
1870Sstevel@tonic-gate X509_EXTENSION *ret;
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate obj=OBJ_nid2obj(nid);
1900Sstevel@tonic-gate if (obj == NULL)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate X509err(X509_F_X509_EXTENSION_CREATE_BY_NID,X509_R_UNKNOWN_NID);
1930Sstevel@tonic-gate return(NULL);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate ret=X509_EXTENSION_create_by_OBJ(ex,obj,crit,data);
1960Sstevel@tonic-gate if (ret == NULL) ASN1_OBJECT_free(obj);
1970Sstevel@tonic-gate return(ret);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate
X509_EXTENSION_create_by_OBJ(X509_EXTENSION ** ex,ASN1_OBJECT * obj,int crit,ASN1_OCTET_STRING * data)2000Sstevel@tonic-gate X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
2010Sstevel@tonic-gate ASN1_OBJECT *obj, int crit, ASN1_OCTET_STRING *data)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate X509_EXTENSION *ret;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if ((ex == NULL) || (*ex == NULL))
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate if ((ret=X509_EXTENSION_new()) == NULL)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,ERR_R_MALLOC_FAILURE);
2100Sstevel@tonic-gate return(NULL);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate else
2140Sstevel@tonic-gate ret= *ex;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate if (!X509_EXTENSION_set_object(ret,obj))
2170Sstevel@tonic-gate goto err;
2180Sstevel@tonic-gate if (!X509_EXTENSION_set_critical(ret,crit))
2190Sstevel@tonic-gate goto err;
2200Sstevel@tonic-gate if (!X509_EXTENSION_set_data(ret,data))
2210Sstevel@tonic-gate goto err;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate if ((ex != NULL) && (*ex == NULL)) *ex=ret;
2240Sstevel@tonic-gate return(ret);
2250Sstevel@tonic-gate err:
2260Sstevel@tonic-gate if ((ex == NULL) || (ret != *ex))
2270Sstevel@tonic-gate X509_EXTENSION_free(ret);
2280Sstevel@tonic-gate return(NULL);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
X509_EXTENSION_set_object(X509_EXTENSION * ex,ASN1_OBJECT * obj)2310Sstevel@tonic-gate int X509_EXTENSION_set_object(X509_EXTENSION *ex, ASN1_OBJECT *obj)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate if ((ex == NULL) || (obj == NULL))
2340Sstevel@tonic-gate return(0);
2350Sstevel@tonic-gate ASN1_OBJECT_free(ex->object);
2360Sstevel@tonic-gate ex->object=OBJ_dup(obj);
2370Sstevel@tonic-gate return(1);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
X509_EXTENSION_set_critical(X509_EXTENSION * ex,int crit)2400Sstevel@tonic-gate int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate if (ex == NULL) return(0);
2430Sstevel@tonic-gate ex->critical=(crit)?0xFF:-1;
2440Sstevel@tonic-gate return(1);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate
X509_EXTENSION_set_data(X509_EXTENSION * ex,ASN1_OCTET_STRING * data)2470Sstevel@tonic-gate int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate int i;
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate if (ex == NULL) return(0);
2520Sstevel@tonic-gate i=M_ASN1_OCTET_STRING_set(ex->value,data->data,data->length);
2530Sstevel@tonic-gate if (!i) return(0);
2540Sstevel@tonic-gate return(1);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate
X509_EXTENSION_get_object(X509_EXTENSION * ex)2570Sstevel@tonic-gate ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate if (ex == NULL) return(NULL);
2600Sstevel@tonic-gate return(ex->object);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
X509_EXTENSION_get_data(X509_EXTENSION * ex)2630Sstevel@tonic-gate ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate if (ex == NULL) return(NULL);
2660Sstevel@tonic-gate return(ex->value);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
X509_EXTENSION_get_critical(X509_EXTENSION * ex)2690Sstevel@tonic-gate int X509_EXTENSION_get_critical(X509_EXTENSION *ex)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate if (ex == NULL) return(0);
2720Sstevel@tonic-gate if(ex->critical > 0) return 1;
2730Sstevel@tonic-gate return 0;
2740Sstevel@tonic-gate }
275