xref: /onnv-gate/usr/src/common/openssl/crypto/x509v3/v3_ocsp.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* v3_ocsp.c */
2*0Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3*0Sstevel@tonic-gate  * project 1999.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate /* ====================================================================
6*0Sstevel@tonic-gate  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate  * are met:
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
13*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
17*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
18*0Sstevel@tonic-gate  *    distribution.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
21*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
22*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
23*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
27*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
28*0Sstevel@tonic-gate  *    licensing@OpenSSL.org.
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
31*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
32*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
35*0Sstevel@tonic-gate  *    acknowledgment:
36*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
37*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
51*0Sstevel@tonic-gate  * ====================================================================
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
54*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
55*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #ifndef OPENSSL_NO_OCSP
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #include <stdio.h>
62*0Sstevel@tonic-gate #include "cryptlib.h"
63*0Sstevel@tonic-gate #include <openssl/conf.h>
64*0Sstevel@tonic-gate #include <openssl/asn1.h>
65*0Sstevel@tonic-gate #include <openssl/ocsp.h>
66*0Sstevel@tonic-gate #include <openssl/x509v3.h>
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /* OCSP extensions and a couple of CRL entry extensions
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static int i2r_ocsp_crlid(X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent);
72*0Sstevel@tonic-gate static int i2r_ocsp_acutoff(X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent);
73*0Sstevel@tonic-gate static int i2r_object(X509V3_EXT_METHOD *method, void *obj, BIO *out, int indent);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate static void *ocsp_nonce_new(void);
76*0Sstevel@tonic-gate static int i2d_ocsp_nonce(void *a, unsigned char **pp);
77*0Sstevel@tonic-gate static void *d2i_ocsp_nonce(void *a, unsigned char **pp, long length);
78*0Sstevel@tonic-gate static void ocsp_nonce_free(void *a);
79*0Sstevel@tonic-gate static int i2r_ocsp_nonce(X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent);
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate static int i2r_ocsp_nocheck(X509V3_EXT_METHOD *method, void *nocheck, BIO *out, int indent);
82*0Sstevel@tonic-gate static void *s2i_ocsp_nocheck(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *str);
83*0Sstevel@tonic-gate static int i2r_ocsp_serviceloc(X509V3_EXT_METHOD *method, void *in, BIO *bp, int ind);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate X509V3_EXT_METHOD v3_ocsp_crlid = {
86*0Sstevel@tonic-gate 	NID_id_pkix_OCSP_CrlID, 0, ASN1_ITEM_ref(OCSP_CRLID),
87*0Sstevel@tonic-gate 	0,0,0,0,
88*0Sstevel@tonic-gate 	0,0,
89*0Sstevel@tonic-gate 	0,0,
90*0Sstevel@tonic-gate 	i2r_ocsp_crlid,0,
91*0Sstevel@tonic-gate 	NULL
92*0Sstevel@tonic-gate };
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate X509V3_EXT_METHOD v3_ocsp_acutoff = {
95*0Sstevel@tonic-gate 	NID_id_pkix_OCSP_archiveCutoff, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
96*0Sstevel@tonic-gate 	0,0,0,0,
97*0Sstevel@tonic-gate 	0,0,
98*0Sstevel@tonic-gate 	0,0,
99*0Sstevel@tonic-gate 	i2r_ocsp_acutoff,0,
100*0Sstevel@tonic-gate 	NULL
101*0Sstevel@tonic-gate };
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate X509V3_EXT_METHOD v3_crl_invdate = {
104*0Sstevel@tonic-gate 	NID_invalidity_date, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
105*0Sstevel@tonic-gate 	0,0,0,0,
106*0Sstevel@tonic-gate 	0,0,
107*0Sstevel@tonic-gate 	0,0,
108*0Sstevel@tonic-gate 	i2r_ocsp_acutoff,0,
109*0Sstevel@tonic-gate 	NULL
110*0Sstevel@tonic-gate };
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate X509V3_EXT_METHOD v3_crl_hold = {
113*0Sstevel@tonic-gate 	NID_hold_instruction_code, 0, ASN1_ITEM_ref(ASN1_OBJECT),
114*0Sstevel@tonic-gate 	0,0,0,0,
115*0Sstevel@tonic-gate 	0,0,
116*0Sstevel@tonic-gate 	0,0,
117*0Sstevel@tonic-gate 	i2r_object,0,
118*0Sstevel@tonic-gate 	NULL
119*0Sstevel@tonic-gate };
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate X509V3_EXT_METHOD v3_ocsp_nonce = {
122*0Sstevel@tonic-gate 	NID_id_pkix_OCSP_Nonce, 0, NULL,
123*0Sstevel@tonic-gate 	ocsp_nonce_new,
124*0Sstevel@tonic-gate 	ocsp_nonce_free,
125*0Sstevel@tonic-gate 	d2i_ocsp_nonce,
126*0Sstevel@tonic-gate 	i2d_ocsp_nonce,
127*0Sstevel@tonic-gate 	0,0,
128*0Sstevel@tonic-gate 	0,0,
129*0Sstevel@tonic-gate 	i2r_ocsp_nonce,0,
130*0Sstevel@tonic-gate 	NULL
131*0Sstevel@tonic-gate };
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate X509V3_EXT_METHOD v3_ocsp_nocheck = {
134*0Sstevel@tonic-gate 	NID_id_pkix_OCSP_noCheck, 0, ASN1_ITEM_ref(ASN1_NULL),
135*0Sstevel@tonic-gate 	0,0,0,0,
136*0Sstevel@tonic-gate 	0,s2i_ocsp_nocheck,
137*0Sstevel@tonic-gate 	0,0,
138*0Sstevel@tonic-gate 	i2r_ocsp_nocheck,0,
139*0Sstevel@tonic-gate 	NULL
140*0Sstevel@tonic-gate };
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate X509V3_EXT_METHOD v3_ocsp_serviceloc = {
143*0Sstevel@tonic-gate 	NID_id_pkix_OCSP_serviceLocator, 0, ASN1_ITEM_ref(OCSP_SERVICELOC),
144*0Sstevel@tonic-gate 	0,0,0,0,
145*0Sstevel@tonic-gate 	0,0,
146*0Sstevel@tonic-gate 	0,0,
147*0Sstevel@tonic-gate 	i2r_ocsp_serviceloc,0,
148*0Sstevel@tonic-gate 	NULL
149*0Sstevel@tonic-gate };
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate static int i2r_ocsp_crlid(X509V3_EXT_METHOD *method, void *in, BIO *bp, int ind)
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate 	OCSP_CRLID *a = in;
154*0Sstevel@tonic-gate 	if (a->crlUrl)
155*0Sstevel@tonic-gate 	        {
156*0Sstevel@tonic-gate 		if (!BIO_printf(bp, "%*scrlUrl: ", ind, "")) goto err;
157*0Sstevel@tonic-gate 		if (!ASN1_STRING_print(bp, (ASN1_STRING*)a->crlUrl)) goto err;
158*0Sstevel@tonic-gate 		if (!BIO_write(bp, "\n", 1)) goto err;
159*0Sstevel@tonic-gate 		}
160*0Sstevel@tonic-gate 	if (a->crlNum)
161*0Sstevel@tonic-gate 	        {
162*0Sstevel@tonic-gate 		if (!BIO_printf(bp, "%*scrlNum: ", ind, "")) goto err;
163*0Sstevel@tonic-gate 		if (!i2a_ASN1_INTEGER(bp, a->crlNum)) goto err;
164*0Sstevel@tonic-gate 		if (!BIO_write(bp, "\n", 1)) goto err;
165*0Sstevel@tonic-gate 		}
166*0Sstevel@tonic-gate 	if (a->crlTime)
167*0Sstevel@tonic-gate 	        {
168*0Sstevel@tonic-gate 		if (!BIO_printf(bp, "%*scrlTime: ", ind, "")) goto err;
169*0Sstevel@tonic-gate 		if (!ASN1_GENERALIZEDTIME_print(bp, a->crlTime)) goto err;
170*0Sstevel@tonic-gate 		if (!BIO_write(bp, "\n", 1)) goto err;
171*0Sstevel@tonic-gate 		}
172*0Sstevel@tonic-gate 	return 1;
173*0Sstevel@tonic-gate 	err:
174*0Sstevel@tonic-gate 	return 0;
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate static int i2r_ocsp_acutoff(X509V3_EXT_METHOD *method, void *cutoff, BIO *bp, int ind)
178*0Sstevel@tonic-gate {
179*0Sstevel@tonic-gate 	if (!BIO_printf(bp, "%*s", ind, "")) return 0;
180*0Sstevel@tonic-gate 	if(!ASN1_GENERALIZEDTIME_print(bp, cutoff)) return 0;
181*0Sstevel@tonic-gate 	return 1;
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate static int i2r_object(X509V3_EXT_METHOD *method, void *oid, BIO *bp, int ind)
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	if (!BIO_printf(bp, "%*s", ind, "")) return 0;
188*0Sstevel@tonic-gate 	if(!i2a_ASN1_OBJECT(bp, oid)) return 0;
189*0Sstevel@tonic-gate 	return 1;
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate /* OCSP nonce. This is needs special treatment because it doesn't have
193*0Sstevel@tonic-gate  * an ASN1 encoding at all: it just contains arbitrary data.
194*0Sstevel@tonic-gate  */
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate static void *ocsp_nonce_new(void)
197*0Sstevel@tonic-gate {
198*0Sstevel@tonic-gate 	return ASN1_OCTET_STRING_new();
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate static int i2d_ocsp_nonce(void *a, unsigned char **pp)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	ASN1_OCTET_STRING *os = a;
204*0Sstevel@tonic-gate 	if(pp) {
205*0Sstevel@tonic-gate 		memcpy(*pp, os->data, os->length);
206*0Sstevel@tonic-gate 		*pp += os->length;
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 	return os->length;
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate static void *d2i_ocsp_nonce(void *a, unsigned char **pp, long length)
212*0Sstevel@tonic-gate {
213*0Sstevel@tonic-gate 	ASN1_OCTET_STRING *os, **pos;
214*0Sstevel@tonic-gate 	pos = a;
215*0Sstevel@tonic-gate 	if(!pos || !*pos) os = ASN1_OCTET_STRING_new();
216*0Sstevel@tonic-gate 	else os = *pos;
217*0Sstevel@tonic-gate 	if(!ASN1_OCTET_STRING_set(os, *pp, length)) goto err;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	*pp += length;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	if(pos) *pos = os;
222*0Sstevel@tonic-gate 	return os;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	err:
225*0Sstevel@tonic-gate 	if(os && (!pos || (*pos != os))) M_ASN1_OCTET_STRING_free(os);
226*0Sstevel@tonic-gate 	OCSPerr(OCSP_F_D2I_OCSP_NONCE, ERR_R_MALLOC_FAILURE);
227*0Sstevel@tonic-gate 	return NULL;
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate static void ocsp_nonce_free(void *a)
231*0Sstevel@tonic-gate {
232*0Sstevel@tonic-gate 	M_ASN1_OCTET_STRING_free(a);
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate static int i2r_ocsp_nonce(X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent)
236*0Sstevel@tonic-gate {
237*0Sstevel@tonic-gate 	if(BIO_printf(out, "%*s", indent, "") <= 0) return 0;
238*0Sstevel@tonic-gate 	if(i2a_ASN1_STRING(out, nonce, V_ASN1_OCTET_STRING) <= 0) return 0;
239*0Sstevel@tonic-gate 	return 1;
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate /* Nocheck is just a single NULL. Don't print anything and always set it */
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate static int i2r_ocsp_nocheck(X509V3_EXT_METHOD *method, void *nocheck, BIO *out, int indent)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate 	return 1;
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate static void *s2i_ocsp_nocheck(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *str)
250*0Sstevel@tonic-gate {
251*0Sstevel@tonic-gate 	return ASN1_NULL_new();
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate static int i2r_ocsp_serviceloc(X509V3_EXT_METHOD *method, void *in, BIO *bp, int ind)
255*0Sstevel@tonic-gate         {
256*0Sstevel@tonic-gate 	int i;
257*0Sstevel@tonic-gate 	OCSP_SERVICELOC *a = in;
258*0Sstevel@tonic-gate 	ACCESS_DESCRIPTION *ad;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate         if (BIO_printf(bp, "%*sIssuer: ", ind, "") <= 0) goto err;
261*0Sstevel@tonic-gate         if (X509_NAME_print_ex(bp, a->issuer, 0, XN_FLAG_ONELINE) <= 0) goto err;
262*0Sstevel@tonic-gate 	for (i = 0; i < sk_ACCESS_DESCRIPTION_num(a->locator); i++)
263*0Sstevel@tonic-gate 	        {
264*0Sstevel@tonic-gate 				ad = sk_ACCESS_DESCRIPTION_value(a->locator,i);
265*0Sstevel@tonic-gate 				if (BIO_printf(bp, "\n%*s", (2*ind), "") <= 0)
266*0Sstevel@tonic-gate 					goto err;
267*0Sstevel@tonic-gate 				if(i2a_ASN1_OBJECT(bp, ad->method) <= 0) goto err;
268*0Sstevel@tonic-gate 				if(BIO_puts(bp, " - ") <= 0) goto err;
269*0Sstevel@tonic-gate 				if(GENERAL_NAME_print(bp, ad->location) <= 0) goto err;
270*0Sstevel@tonic-gate 		}
271*0Sstevel@tonic-gate 	return 1;
272*0Sstevel@tonic-gate err:
273*0Sstevel@tonic-gate 	return 0;
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate #endif
276