10Sstevel@tonic-gate /* ocsp_ext.c */
20Sstevel@tonic-gate /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
30Sstevel@tonic-gate * project. */
40Sstevel@tonic-gate
50Sstevel@tonic-gate /* History:
60Sstevel@tonic-gate This file was transfered to Richard Levitte from CertCo by Kathy
70Sstevel@tonic-gate Weinhold in mid-spring 2000 to be included in OpenSSL or released
80Sstevel@tonic-gate as a patch kit. */
90Sstevel@tonic-gate
100Sstevel@tonic-gate /* ====================================================================
110Sstevel@tonic-gate * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
140Sstevel@tonic-gate * modification, are permitted provided that the following conditions
150Sstevel@tonic-gate * are met:
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
180Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
210Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
220Sstevel@tonic-gate * the documentation and/or other materials provided with the
230Sstevel@tonic-gate * distribution.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
260Sstevel@tonic-gate * software must display the following acknowledgment:
270Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
280Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
310Sstevel@tonic-gate * endorse or promote products derived from this software without
320Sstevel@tonic-gate * prior written permission. For written permission, please contact
330Sstevel@tonic-gate * openssl-core@openssl.org.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
360Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
370Sstevel@tonic-gate * permission of the OpenSSL Project.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
400Sstevel@tonic-gate * acknowledgment:
410Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
420Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
450Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
460Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
470Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
480Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
490Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
500Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
510Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
520Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
530Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
540Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
550Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
560Sstevel@tonic-gate * ====================================================================
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
590Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
600Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
610Sstevel@tonic-gate *
620Sstevel@tonic-gate */
630Sstevel@tonic-gate
640Sstevel@tonic-gate #include <stdio.h>
650Sstevel@tonic-gate #include <cryptlib.h>
660Sstevel@tonic-gate #include <openssl/objects.h>
670Sstevel@tonic-gate #include <openssl/x509.h>
680Sstevel@tonic-gate #include <openssl/ocsp.h>
690Sstevel@tonic-gate #include <openssl/rand.h>
700Sstevel@tonic-gate #include <openssl/x509v3.h>
710Sstevel@tonic-gate
720Sstevel@tonic-gate /* Standard wrapper functions for extensions */
730Sstevel@tonic-gate
740Sstevel@tonic-gate /* OCSP request extensions */
750Sstevel@tonic-gate
OCSP_REQUEST_get_ext_count(OCSP_REQUEST * x)760Sstevel@tonic-gate int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate return(X509v3_get_ext_count(x->tbsRequest->requestExtensions));
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST * x,int nid,int lastpos)810Sstevel@tonic-gate int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate return(X509v3_get_ext_by_NID(x->tbsRequest->requestExtensions,nid,lastpos));
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST * x,ASN1_OBJECT * obj,int lastpos)860Sstevel@tonic-gate int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, ASN1_OBJECT *obj, int lastpos)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate return(X509v3_get_ext_by_OBJ(x->tbsRequest->requestExtensions,obj,lastpos));
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST * x,int crit,int lastpos)910Sstevel@tonic-gate int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate return(X509v3_get_ext_by_critical(x->tbsRequest->requestExtensions,crit,lastpos));
940Sstevel@tonic-gate }
950Sstevel@tonic-gate
OCSP_REQUEST_get_ext(OCSP_REQUEST * x,int loc)960Sstevel@tonic-gate X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate return(X509v3_get_ext(x->tbsRequest->requestExtensions,loc));
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
OCSP_REQUEST_delete_ext(OCSP_REQUEST * x,int loc)1010Sstevel@tonic-gate X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate return(X509v3_delete_ext(x->tbsRequest->requestExtensions,loc));
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST * x,int nid,int * crit,int * idx)1060Sstevel@tonic-gate void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate return X509V3_get_d2i(x->tbsRequest->requestExtensions, nid, crit, idx);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST * x,int nid,void * value,int crit,unsigned long flags)1110Sstevel@tonic-gate int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit,
1120Sstevel@tonic-gate unsigned long flags)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate return X509V3_add1_i2d(&x->tbsRequest->requestExtensions, nid, value, crit, flags);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
OCSP_REQUEST_add_ext(OCSP_REQUEST * x,X509_EXTENSION * ex,int loc)1170Sstevel@tonic-gate int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate return(X509v3_add_ext(&(x->tbsRequest->requestExtensions),ex,loc) != NULL);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate /* Single extensions */
1230Sstevel@tonic-gate
OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ * x)1240Sstevel@tonic-gate int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate return(X509v3_get_ext_count(x->singleRequestExtensions));
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ * x,int nid,int lastpos)1290Sstevel@tonic-gate int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate return(X509v3_get_ext_by_NID(x->singleRequestExtensions,nid,lastpos));
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ * x,ASN1_OBJECT * obj,int lastpos)1340Sstevel@tonic-gate int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, ASN1_OBJECT *obj, int lastpos)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate return(X509v3_get_ext_by_OBJ(x->singleRequestExtensions,obj,lastpos));
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ * x,int crit,int lastpos)1390Sstevel@tonic-gate int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate return(X509v3_get_ext_by_critical(x->singleRequestExtensions,crit,lastpos));
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
OCSP_ONEREQ_get_ext(OCSP_ONEREQ * x,int loc)1440Sstevel@tonic-gate X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate return(X509v3_get_ext(x->singleRequestExtensions,loc));
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
OCSP_ONEREQ_delete_ext(OCSP_ONEREQ * x,int loc)1490Sstevel@tonic-gate X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate return(X509v3_delete_ext(x->singleRequestExtensions,loc));
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate
OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ * x,int nid,int * crit,int * idx)1540Sstevel@tonic-gate void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ * x,int nid,void * value,int crit,unsigned long flags)1590Sstevel@tonic-gate int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit,
1600Sstevel@tonic-gate unsigned long flags)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit, flags);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
OCSP_ONEREQ_add_ext(OCSP_ONEREQ * x,X509_EXTENSION * ex,int loc)1650Sstevel@tonic-gate int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate return(X509v3_add_ext(&(x->singleRequestExtensions),ex,loc) != NULL);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /* OCSP Basic response */
1710Sstevel@tonic-gate
OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP * x)1720Sstevel@tonic-gate int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate return(X509v3_get_ext_count(x->tbsResponseData->responseExtensions));
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP * x,int nid,int lastpos)1770Sstevel@tonic-gate int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate return(X509v3_get_ext_by_NID(x->tbsResponseData->responseExtensions,nid,lastpos));
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP * x,ASN1_OBJECT * obj,int lastpos)1820Sstevel@tonic-gate int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, ASN1_OBJECT *obj, int lastpos)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate return(X509v3_get_ext_by_OBJ(x->tbsResponseData->responseExtensions,obj,lastpos));
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP * x,int crit,int lastpos)1870Sstevel@tonic-gate int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit, int lastpos)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate return(X509v3_get_ext_by_critical(x->tbsResponseData->responseExtensions,crit,lastpos));
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
OCSP_BASICRESP_get_ext(OCSP_BASICRESP * x,int loc)1920Sstevel@tonic-gate X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate return(X509v3_get_ext(x->tbsResponseData->responseExtensions,loc));
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
OCSP_BASICRESP_delete_ext(OCSP_BASICRESP * x,int loc)1970Sstevel@tonic-gate X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate return(X509v3_delete_ext(x->tbsResponseData->responseExtensions,loc));
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP * x,int nid,int * crit,int * idx)2020Sstevel@tonic-gate void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit, int *idx)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate return X509V3_get_d2i(x->tbsResponseData->responseExtensions, nid, crit, idx);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP * x,int nid,void * value,int crit,unsigned long flags)2070Sstevel@tonic-gate int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value, int crit,
2080Sstevel@tonic-gate unsigned long flags)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate return X509V3_add1_i2d(&x->tbsResponseData->responseExtensions, nid, value, crit, flags);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
OCSP_BASICRESP_add_ext(OCSP_BASICRESP * x,X509_EXTENSION * ex,int loc)2130Sstevel@tonic-gate int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate return(X509v3_add_ext(&(x->tbsResponseData->responseExtensions),ex,loc) != NULL);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate /* OCSP single response extensions */
2190Sstevel@tonic-gate
OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP * x)2200Sstevel@tonic-gate int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate return(X509v3_get_ext_count(x->singleExtensions));
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP * x,int nid,int lastpos)2250Sstevel@tonic-gate int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate return(X509v3_get_ext_by_NID(x->singleExtensions,nid,lastpos));
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP * x,ASN1_OBJECT * obj,int lastpos)2300Sstevel@tonic-gate int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, ASN1_OBJECT *obj, int lastpos)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate return(X509v3_get_ext_by_OBJ(x->singleExtensions,obj,lastpos));
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate
OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP * x,int crit,int lastpos)2350Sstevel@tonic-gate int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit, int lastpos)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate return(X509v3_get_ext_by_critical(x->singleExtensions,crit,lastpos));
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP * x,int loc)2400Sstevel@tonic-gate X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate return(X509v3_get_ext(x->singleExtensions,loc));
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP * x,int loc)2450Sstevel@tonic-gate X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate return(X509v3_delete_ext(x->singleExtensions,loc));
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP * x,int nid,int * crit,int * idx)2500Sstevel@tonic-gate void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit, int *idx)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate return X509V3_get_d2i(x->singleExtensions, nid, crit, idx);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP * x,int nid,void * value,int crit,unsigned long flags)2550Sstevel@tonic-gate int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value, int crit,
2560Sstevel@tonic-gate unsigned long flags)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP * x,X509_EXTENSION * ex,int loc)2610Sstevel@tonic-gate int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate return(X509v3_add_ext(&(x->singleExtensions),ex,loc) != NULL);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /* also CRL Entry Extensions */
2670Sstevel@tonic-gate
ASN1_STRING_encode(ASN1_STRING * s,i2d_of_void * i2d,void * data,STACK_OF (ASN1_OBJECT)* sk)268*2139Sjp161948 ASN1_STRING *ASN1_STRING_encode(ASN1_STRING *s, i2d_of_void *i2d,
269*2139Sjp161948 void *data, STACK_OF(ASN1_OBJECT) *sk)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate int i;
2720Sstevel@tonic-gate unsigned char *p, *b = NULL;
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate if (data)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate if ((i=i2d(data,NULL)) <= 0) goto err;
277*2139Sjp161948 if (!(b=p=OPENSSL_malloc((unsigned int)i)))
2780Sstevel@tonic-gate goto err;
2790Sstevel@tonic-gate if (i2d(data, &p) <= 0) goto err;
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate else if (sk)
2820Sstevel@tonic-gate {
283*2139Sjp161948 if ((i=i2d_ASN1_SET_OF_ASN1_OBJECT(sk,NULL,
284*2139Sjp161948 (I2D_OF(ASN1_OBJECT))i2d,
285*2139Sjp161948 V_ASN1_SEQUENCE,
286*2139Sjp161948 V_ASN1_UNIVERSAL,
287*2139Sjp161948 IS_SEQUENCE))<=0) goto err;
288*2139Sjp161948 if (!(b=p=OPENSSL_malloc((unsigned int)i)))
2890Sstevel@tonic-gate goto err;
290*2139Sjp161948 if (i2d_ASN1_SET_OF_ASN1_OBJECT(sk,&p,(I2D_OF(ASN1_OBJECT))i2d,
291*2139Sjp161948 V_ASN1_SEQUENCE,
292*2139Sjp161948 V_ASN1_UNIVERSAL,
293*2139Sjp161948 IS_SEQUENCE)<=0) goto err;
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate else
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate OCSPerr(OCSP_F_ASN1_STRING_ENCODE,OCSP_R_BAD_DATA);
2980Sstevel@tonic-gate goto err;
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate if (!s && !(s = ASN1_STRING_new())) goto err;
3010Sstevel@tonic-gate if (!(ASN1_STRING_set(s, b, i))) goto err;
3020Sstevel@tonic-gate OPENSSL_free(b);
3030Sstevel@tonic-gate return s;
3040Sstevel@tonic-gate err:
3050Sstevel@tonic-gate if (b) OPENSSL_free(b);
3060Sstevel@tonic-gate return NULL;
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /* Nonce handling functions */
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate /* Add a nonce to an extension stack. A nonce can be specificed or if NULL
3120Sstevel@tonic-gate * a random nonce will be generated.
3130Sstevel@tonic-gate * Note: OpenSSL 0.9.7d and later create an OCTET STRING containing the
3140Sstevel@tonic-gate * nonce, previous versions used the raw nonce.
3150Sstevel@tonic-gate */
3160Sstevel@tonic-gate
ocsp_add1_nonce(STACK_OF (X509_EXTENSION)** exts,unsigned char * val,int len)3170Sstevel@tonic-gate static int ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts, unsigned char *val, int len)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate unsigned char *tmpval;
3200Sstevel@tonic-gate ASN1_OCTET_STRING os;
3210Sstevel@tonic-gate int ret = 0;
3220Sstevel@tonic-gate if (len <= 0) len = OCSP_DEFAULT_NONCE_LENGTH;
3230Sstevel@tonic-gate /* Create the OCTET STRING manually by writing out the header and
3240Sstevel@tonic-gate * appending the content octets. This avoids an extra memory allocation
3250Sstevel@tonic-gate * operation in some cases. Applications should *NOT* do this because
3260Sstevel@tonic-gate * it relies on library internals.
3270Sstevel@tonic-gate */
3280Sstevel@tonic-gate os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING);
3290Sstevel@tonic-gate os.data = OPENSSL_malloc(os.length);
3300Sstevel@tonic-gate if (os.data == NULL)
3310Sstevel@tonic-gate goto err;
3320Sstevel@tonic-gate tmpval = os.data;
3330Sstevel@tonic-gate ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL);
3340Sstevel@tonic-gate if (val)
3350Sstevel@tonic-gate memcpy(tmpval, val, len);
3360Sstevel@tonic-gate else
3370Sstevel@tonic-gate RAND_pseudo_bytes(tmpval, len);
3380Sstevel@tonic-gate if(!X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce,
3390Sstevel@tonic-gate &os, 0, X509V3_ADD_REPLACE))
3400Sstevel@tonic-gate goto err;
3410Sstevel@tonic-gate ret = 1;
3420Sstevel@tonic-gate err:
3430Sstevel@tonic-gate if (os.data)
3440Sstevel@tonic-gate OPENSSL_free(os.data);
3450Sstevel@tonic-gate return ret;
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate /* Add nonce to an OCSP request */
3500Sstevel@tonic-gate
OCSP_request_add1_nonce(OCSP_REQUEST * req,unsigned char * val,int len)3510Sstevel@tonic-gate int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate return ocsp_add1_nonce(&req->tbsRequest->requestExtensions, val, len);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate /* Same as above but for a response */
3570Sstevel@tonic-gate
OCSP_basic_add1_nonce(OCSP_BASICRESP * resp,unsigned char * val,int len)3580Sstevel@tonic-gate int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate return ocsp_add1_nonce(&resp->tbsResponseData->responseExtensions, val, len);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate /* Check nonce validity in a request and response.
3640Sstevel@tonic-gate * Return value reflects result:
3650Sstevel@tonic-gate * 1: nonces present and equal.
3660Sstevel@tonic-gate * 2: nonces both absent.
3670Sstevel@tonic-gate * 3: nonce present in response only.
3680Sstevel@tonic-gate * 0: nonces both present and not equal.
3690Sstevel@tonic-gate * -1: nonce in request only.
3700Sstevel@tonic-gate *
3710Sstevel@tonic-gate * For most responders clients can check return > 0.
3720Sstevel@tonic-gate * If responder doesn't handle nonces return != 0 may be
3730Sstevel@tonic-gate * necessary. return == 0 is always an error.
3740Sstevel@tonic-gate */
3750Sstevel@tonic-gate
OCSP_check_nonce(OCSP_REQUEST * req,OCSP_BASICRESP * bs)3760Sstevel@tonic-gate int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs)
3770Sstevel@tonic-gate {
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate * Since we are only interested in the presence or absence of
3800Sstevel@tonic-gate * the nonce and comparing its value there is no need to use
3810Sstevel@tonic-gate * the X509V3 routines: this way we can avoid them allocating an
3820Sstevel@tonic-gate * ASN1_OCTET_STRING structure for the value which would be
3830Sstevel@tonic-gate * freed immediately anyway.
3840Sstevel@tonic-gate */
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate int req_idx, resp_idx;
3870Sstevel@tonic-gate X509_EXTENSION *req_ext, *resp_ext;
3880Sstevel@tonic-gate req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
3890Sstevel@tonic-gate resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, NID_id_pkix_OCSP_Nonce, -1);
3900Sstevel@tonic-gate /* Check both absent */
3910Sstevel@tonic-gate if((req_idx < 0) && (resp_idx < 0))
3920Sstevel@tonic-gate return 2;
3930Sstevel@tonic-gate /* Check in request only */
3940Sstevel@tonic-gate if((req_idx >= 0) && (resp_idx < 0))
3950Sstevel@tonic-gate return -1;
3960Sstevel@tonic-gate /* Check in response but not request */
3970Sstevel@tonic-gate if((req_idx < 0) && (resp_idx >= 0))
3980Sstevel@tonic-gate return 3;
3990Sstevel@tonic-gate /* Otherwise nonce in request and response so retrieve the extensions */
4000Sstevel@tonic-gate req_ext = OCSP_REQUEST_get_ext(req, req_idx);
4010Sstevel@tonic-gate resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx);
4020Sstevel@tonic-gate if(ASN1_OCTET_STRING_cmp(req_ext->value, resp_ext->value))
4030Sstevel@tonic-gate return 0;
4040Sstevel@tonic-gate return 1;
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate /* Copy the nonce value (if any) from an OCSP request to
4080Sstevel@tonic-gate * a response.
4090Sstevel@tonic-gate */
4100Sstevel@tonic-gate
OCSP_copy_nonce(OCSP_BASICRESP * resp,OCSP_REQUEST * req)4110Sstevel@tonic-gate int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate X509_EXTENSION *req_ext;
4140Sstevel@tonic-gate int req_idx;
4150Sstevel@tonic-gate /* Check for nonce in request */
4160Sstevel@tonic-gate req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
4170Sstevel@tonic-gate /* If no nonce that's OK */
4180Sstevel@tonic-gate if (req_idx < 0) return 2;
4190Sstevel@tonic-gate req_ext = OCSP_REQUEST_get_ext(req, req_idx);
4200Sstevel@tonic-gate return OCSP_BASICRESP_add_ext(resp, req_ext, -1);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate
OCSP_crlID_new(char * url,long * n,char * tim)4230Sstevel@tonic-gate X509_EXTENSION *OCSP_crlID_new(char *url, long *n, char *tim)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate X509_EXTENSION *x = NULL;
4260Sstevel@tonic-gate OCSP_CRLID *cid = NULL;
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate if (!(cid = OCSP_CRLID_new())) goto err;
4290Sstevel@tonic-gate if (url)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate if (!(cid->crlUrl = ASN1_IA5STRING_new())) goto err;
4320Sstevel@tonic-gate if (!(ASN1_STRING_set(cid->crlUrl, url, -1))) goto err;
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate if (n)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate if (!(cid->crlNum = ASN1_INTEGER_new())) goto err;
4370Sstevel@tonic-gate if (!(ASN1_INTEGER_set(cid->crlNum, *n))) goto err;
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate if (tim)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate if (!(cid->crlTime = ASN1_GENERALIZEDTIME_new())) goto err;
4420Sstevel@tonic-gate if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim)))
4430Sstevel@tonic-gate goto err;
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate if (!(x = X509_EXTENSION_new())) goto err;
4460Sstevel@tonic-gate if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_CrlID))) goto err;
447*2139Sjp161948 if (!(ASN1_STRING_encode_of(OCSP_CRLID,x->value,i2d_OCSP_CRLID,cid,
448*2139Sjp161948 NULL)))
4490Sstevel@tonic-gate goto err;
4500Sstevel@tonic-gate OCSP_CRLID_free(cid);
4510Sstevel@tonic-gate return x;
4520Sstevel@tonic-gate err:
4530Sstevel@tonic-gate if (x) X509_EXTENSION_free(x);
4540Sstevel@tonic-gate if (cid) OCSP_CRLID_free(cid);
4550Sstevel@tonic-gate return NULL;
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate /* AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
OCSP_accept_responses_new(char ** oids)4590Sstevel@tonic-gate X509_EXTENSION *OCSP_accept_responses_new(char **oids)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate int nid;
4620Sstevel@tonic-gate STACK_OF(ASN1_OBJECT) *sk = NULL;
4630Sstevel@tonic-gate ASN1_OBJECT *o = NULL;
4640Sstevel@tonic-gate X509_EXTENSION *x = NULL;
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate if (!(sk = sk_ASN1_OBJECT_new_null())) goto err;
4670Sstevel@tonic-gate while (oids && *oids)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate if ((nid=OBJ_txt2nid(*oids))!=NID_undef&&(o=OBJ_nid2obj(nid)))
4700Sstevel@tonic-gate sk_ASN1_OBJECT_push(sk, o);
4710Sstevel@tonic-gate oids++;
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate if (!(x = X509_EXTENSION_new())) goto err;
4740Sstevel@tonic-gate if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_acceptableResponses)))
4750Sstevel@tonic-gate goto err;
476*2139Sjp161948 if (!(ASN1_STRING_encode_of(ASN1_OBJECT,x->value,i2d_ASN1_OBJECT,NULL,
477*2139Sjp161948 sk)))
4780Sstevel@tonic-gate goto err;
4790Sstevel@tonic-gate sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
4800Sstevel@tonic-gate return x;
4810Sstevel@tonic-gate err:
4820Sstevel@tonic-gate if (x) X509_EXTENSION_free(x);
4830Sstevel@tonic-gate if (sk) sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
4840Sstevel@tonic-gate return NULL;
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate /* ArchiveCutoff ::= GeneralizedTime */
OCSP_archive_cutoff_new(char * tim)4880Sstevel@tonic-gate X509_EXTENSION *OCSP_archive_cutoff_new(char* tim)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate X509_EXTENSION *x=NULL;
4910Sstevel@tonic-gate ASN1_GENERALIZEDTIME *gt = NULL;
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate if (!(gt = ASN1_GENERALIZEDTIME_new())) goto err;
4940Sstevel@tonic-gate if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim))) goto err;
4950Sstevel@tonic-gate if (!(x = X509_EXTENSION_new())) goto err;
4960Sstevel@tonic-gate if (!(x->object=OBJ_nid2obj(NID_id_pkix_OCSP_archiveCutoff)))goto err;
497*2139Sjp161948 if (!(ASN1_STRING_encode_of(ASN1_GENERALIZEDTIME,x->value,
498*2139Sjp161948 i2d_ASN1_GENERALIZEDTIME,gt,NULL))) goto err;
4990Sstevel@tonic-gate ASN1_GENERALIZEDTIME_free(gt);
5000Sstevel@tonic-gate return x;
5010Sstevel@tonic-gate err:
5020Sstevel@tonic-gate if (gt) ASN1_GENERALIZEDTIME_free(gt);
5030Sstevel@tonic-gate if (x) X509_EXTENSION_free(x);
5040Sstevel@tonic-gate return NULL;
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate /* per ACCESS_DESCRIPTION parameter are oids, of which there are currently
5080Sstevel@tonic-gate * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value. This
5090Sstevel@tonic-gate * method forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
5100Sstevel@tonic-gate */
OCSP_url_svcloc_new(X509_NAME * issuer,char ** urls)5110Sstevel@tonic-gate X509_EXTENSION *OCSP_url_svcloc_new(X509_NAME* issuer, char **urls)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate X509_EXTENSION *x = NULL;
5140Sstevel@tonic-gate ASN1_IA5STRING *ia5 = NULL;
5150Sstevel@tonic-gate OCSP_SERVICELOC *sloc = NULL;
5160Sstevel@tonic-gate ACCESS_DESCRIPTION *ad = NULL;
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate if (!(sloc = OCSP_SERVICELOC_new())) goto err;
5190Sstevel@tonic-gate if (!(sloc->issuer = X509_NAME_dup(issuer))) goto err;
5200Sstevel@tonic-gate if (urls && *urls && !(sloc->locator = sk_ACCESS_DESCRIPTION_new_null())) goto err;
5210Sstevel@tonic-gate while (urls && *urls)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate if (!(ad = ACCESS_DESCRIPTION_new())) goto err;
5240Sstevel@tonic-gate if (!(ad->method=OBJ_nid2obj(NID_ad_OCSP))) goto err;
5250Sstevel@tonic-gate if (!(ad->location = GENERAL_NAME_new())) goto err;
5260Sstevel@tonic-gate if (!(ia5 = ASN1_IA5STRING_new())) goto err;
5270Sstevel@tonic-gate if (!ASN1_STRING_set((ASN1_STRING*)ia5, *urls, -1)) goto err;
5280Sstevel@tonic-gate ad->location->type = GEN_URI;
5290Sstevel@tonic-gate ad->location->d.ia5 = ia5;
5300Sstevel@tonic-gate if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad)) goto err;
5310Sstevel@tonic-gate urls++;
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate if (!(x = X509_EXTENSION_new())) goto err;
5340Sstevel@tonic-gate if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_serviceLocator)))
5350Sstevel@tonic-gate goto err;
536*2139Sjp161948 if (!(ASN1_STRING_encode_of(OCSP_SERVICELOC,x->value,
537*2139Sjp161948 i2d_OCSP_SERVICELOC,sloc,NULL))) goto err;
5380Sstevel@tonic-gate OCSP_SERVICELOC_free(sloc);
5390Sstevel@tonic-gate return x;
5400Sstevel@tonic-gate err:
5410Sstevel@tonic-gate if (x) X509_EXTENSION_free(x);
5420Sstevel@tonic-gate if (sloc) OCSP_SERVICELOC_free(sloc);
5430Sstevel@tonic-gate return NULL;
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate
546