xref: /onnv-gate/usr/src/common/openssl/crypto/ocsp/ocsp_cl.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* ocsp_cl.c */
2*0Sstevel@tonic-gate /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
3*0Sstevel@tonic-gate  * project. */
4*0Sstevel@tonic-gate 
5*0Sstevel@tonic-gate /* History:
6*0Sstevel@tonic-gate    This file was transfered to Richard Levitte from CertCo by Kathy
7*0Sstevel@tonic-gate    Weinhold in mid-spring 2000 to be included in OpenSSL or released
8*0Sstevel@tonic-gate    as a patch kit. */
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate /* ====================================================================
11*0Sstevel@tonic-gate  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
14*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
15*0Sstevel@tonic-gate  * are met:
16*0Sstevel@tonic-gate  *
17*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
18*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
21*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
22*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
23*0Sstevel@tonic-gate  *    distribution.
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
26*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
27*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
28*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
31*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
32*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
33*0Sstevel@tonic-gate  *    openssl-core@openssl.org.
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
36*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
37*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
40*0Sstevel@tonic-gate  *    acknowledgment:
41*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
42*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
45*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
48*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
56*0Sstevel@tonic-gate  * ====================================================================
57*0Sstevel@tonic-gate  *
58*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
59*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
60*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate #include <stdio.h>
65*0Sstevel@tonic-gate #include <time.h>
66*0Sstevel@tonic-gate #include <cryptlib.h>
67*0Sstevel@tonic-gate #include <openssl/objects.h>
68*0Sstevel@tonic-gate #include <openssl/rand.h>
69*0Sstevel@tonic-gate #include <openssl/x509.h>
70*0Sstevel@tonic-gate #include <openssl/pem.h>
71*0Sstevel@tonic-gate #include <openssl/x509v3.h>
72*0Sstevel@tonic-gate #include <openssl/ocsp.h>
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /* Utility functions related to sending OCSP requests and extracting
75*0Sstevel@tonic-gate  * relevant information from the response.
76*0Sstevel@tonic-gate  */
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /* Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ
79*0Sstevel@tonic-gate  * pointer: useful if we want to add extensions.
80*0Sstevel@tonic-gate  */
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid)
83*0Sstevel@tonic-gate         {
84*0Sstevel@tonic-gate 	OCSP_ONEREQ *one = NULL;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	if (!(one = OCSP_ONEREQ_new())) goto err;
87*0Sstevel@tonic-gate 	if (one->reqCert) OCSP_CERTID_free(one->reqCert);
88*0Sstevel@tonic-gate 	one->reqCert = cid;
89*0Sstevel@tonic-gate 	if (req &&
90*0Sstevel@tonic-gate 		!sk_OCSP_ONEREQ_push(req->tbsRequest->requestList, one))
91*0Sstevel@tonic-gate 				goto err;
92*0Sstevel@tonic-gate 	return one;
93*0Sstevel@tonic-gate err:
94*0Sstevel@tonic-gate 	OCSP_ONEREQ_free(one);
95*0Sstevel@tonic-gate 	return NULL;
96*0Sstevel@tonic-gate         }
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate /* Set requestorName from an X509_NAME structure */
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate int OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm)
101*0Sstevel@tonic-gate 	{
102*0Sstevel@tonic-gate 	GENERAL_NAME *gen;
103*0Sstevel@tonic-gate 	gen = GENERAL_NAME_new();
104*0Sstevel@tonic-gate 	if (!X509_NAME_set(&gen->d.directoryName, nm))
105*0Sstevel@tonic-gate 		{
106*0Sstevel@tonic-gate 		GENERAL_NAME_free(gen);
107*0Sstevel@tonic-gate 		return 0;
108*0Sstevel@tonic-gate 		}
109*0Sstevel@tonic-gate 	gen->type = GEN_DIRNAME;
110*0Sstevel@tonic-gate 	if (req->tbsRequest->requestorName)
111*0Sstevel@tonic-gate 		GENERAL_NAME_free(req->tbsRequest->requestorName);
112*0Sstevel@tonic-gate 	req->tbsRequest->requestorName = gen;
113*0Sstevel@tonic-gate 	return 1;
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate /* Add a certificate to an OCSP request */
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
120*0Sstevel@tonic-gate 	{
121*0Sstevel@tonic-gate 	OCSP_SIGNATURE *sig;
122*0Sstevel@tonic-gate 	if (!req->optionalSignature)
123*0Sstevel@tonic-gate 		req->optionalSignature = OCSP_SIGNATURE_new();
124*0Sstevel@tonic-gate 	sig = req->optionalSignature;
125*0Sstevel@tonic-gate 	if (!sig) return 0;
126*0Sstevel@tonic-gate 	if (!cert) return 1;
127*0Sstevel@tonic-gate 	if (!sig->certs && !(sig->certs = sk_X509_new_null()))
128*0Sstevel@tonic-gate 		return 0;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	if(!sk_X509_push(sig->certs, cert)) return 0;
131*0Sstevel@tonic-gate 	CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
132*0Sstevel@tonic-gate 	return 1;
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate /* Sign an OCSP request set the requestorName to the subjec
136*0Sstevel@tonic-gate  * name of an optional signers certificate and include one
137*0Sstevel@tonic-gate  * or more optional certificates in the request. Behaves
138*0Sstevel@tonic-gate  * like PKCS7_sign().
139*0Sstevel@tonic-gate  */
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate int OCSP_request_sign(OCSP_REQUEST   *req,
142*0Sstevel@tonic-gate 		      X509           *signer,
143*0Sstevel@tonic-gate 		      EVP_PKEY       *key,
144*0Sstevel@tonic-gate 		      const EVP_MD   *dgst,
145*0Sstevel@tonic-gate 		      STACK_OF(X509) *certs,
146*0Sstevel@tonic-gate 		      unsigned long flags)
147*0Sstevel@tonic-gate         {
148*0Sstevel@tonic-gate 	int i;
149*0Sstevel@tonic-gate 	OCSP_SIGNATURE *sig;
150*0Sstevel@tonic-gate 	X509 *x;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
153*0Sstevel@tonic-gate 			goto err;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	if (!(req->optionalSignature = sig = OCSP_SIGNATURE_new())) goto err;
156*0Sstevel@tonic-gate 	if (!dgst) dgst = EVP_sha1();
157*0Sstevel@tonic-gate 	if (key)
158*0Sstevel@tonic-gate 		{
159*0Sstevel@tonic-gate 		if (!X509_check_private_key(signer, key))
160*0Sstevel@tonic-gate 			{
161*0Sstevel@tonic-gate 			OCSPerr(OCSP_F_OCSP_REQUEST_SIGN, OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
162*0Sstevel@tonic-gate 			goto err;
163*0Sstevel@tonic-gate 			}
164*0Sstevel@tonic-gate 		if (!OCSP_REQUEST_sign(req, key, dgst)) goto err;
165*0Sstevel@tonic-gate 		}
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	if (!(flags & OCSP_NOCERTS))
168*0Sstevel@tonic-gate 		{
169*0Sstevel@tonic-gate 		if(!OCSP_request_add1_cert(req, signer)) goto err;
170*0Sstevel@tonic-gate 		for (i = 0; i < sk_X509_num(certs); i++)
171*0Sstevel@tonic-gate 			{
172*0Sstevel@tonic-gate 			x = sk_X509_value(certs, i);
173*0Sstevel@tonic-gate 			if (!OCSP_request_add1_cert(req, x)) goto err;
174*0Sstevel@tonic-gate 			}
175*0Sstevel@tonic-gate 		}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	return 1;
178*0Sstevel@tonic-gate err:
179*0Sstevel@tonic-gate 	OCSP_SIGNATURE_free(req->optionalSignature);
180*0Sstevel@tonic-gate 	req->optionalSignature = NULL;
181*0Sstevel@tonic-gate 	return 0;
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate /* Get response status */
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate int OCSP_response_status(OCSP_RESPONSE *resp)
187*0Sstevel@tonic-gate 	{
188*0Sstevel@tonic-gate 	return ASN1_ENUMERATED_get(resp->responseStatus);
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate /* Extract basic response from OCSP_RESPONSE or NULL if
192*0Sstevel@tonic-gate  * no basic response present.
193*0Sstevel@tonic-gate  */
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp)
197*0Sstevel@tonic-gate 	{
198*0Sstevel@tonic-gate 	OCSP_RESPBYTES *rb;
199*0Sstevel@tonic-gate 	rb = resp->responseBytes;
200*0Sstevel@tonic-gate 	if (!rb)
201*0Sstevel@tonic-gate 		{
202*0Sstevel@tonic-gate 		OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NO_RESPONSE_DATA);
203*0Sstevel@tonic-gate 		return NULL;
204*0Sstevel@tonic-gate 		}
205*0Sstevel@tonic-gate 	if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic)
206*0Sstevel@tonic-gate 		{
207*0Sstevel@tonic-gate 		OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NOT_BASIC_RESPONSE);
208*0Sstevel@tonic-gate 		return NULL;
209*0Sstevel@tonic-gate 		}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate /* Return number of OCSP_SINGLERESP reponses present in
215*0Sstevel@tonic-gate  * a basic response.
216*0Sstevel@tonic-gate  */
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate int OCSP_resp_count(OCSP_BASICRESP *bs)
219*0Sstevel@tonic-gate 	{
220*0Sstevel@tonic-gate 	if (!bs) return -1;
221*0Sstevel@tonic-gate 	return sk_OCSP_SINGLERESP_num(bs->tbsResponseData->responses);
222*0Sstevel@tonic-gate 	}
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate /* Extract an OCSP_SINGLERESP response with a given index */
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
227*0Sstevel@tonic-gate 	{
228*0Sstevel@tonic-gate 	if (!bs) return NULL;
229*0Sstevel@tonic-gate 	return sk_OCSP_SINGLERESP_value(bs->tbsResponseData->responses, idx);
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate /* Look single response matching a given certificate ID */
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
235*0Sstevel@tonic-gate 	{
236*0Sstevel@tonic-gate 	int i;
237*0Sstevel@tonic-gate 	STACK_OF(OCSP_SINGLERESP) *sresp;
238*0Sstevel@tonic-gate 	OCSP_SINGLERESP *single;
239*0Sstevel@tonic-gate 	if (!bs) return -1;
240*0Sstevel@tonic-gate 	if (last < 0) last = 0;
241*0Sstevel@tonic-gate 	else last++;
242*0Sstevel@tonic-gate 	sresp = bs->tbsResponseData->responses;
243*0Sstevel@tonic-gate 	for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++)
244*0Sstevel@tonic-gate 		{
245*0Sstevel@tonic-gate 		single = sk_OCSP_SINGLERESP_value(sresp, i);
246*0Sstevel@tonic-gate 		if (!OCSP_id_cmp(id, single->certId)) return i;
247*0Sstevel@tonic-gate 		}
248*0Sstevel@tonic-gate 	return -1;
249*0Sstevel@tonic-gate 	}
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate /* Extract status information from an OCSP_SINGLERESP structure.
252*0Sstevel@tonic-gate  * Note: the revtime and reason values are only set if the
253*0Sstevel@tonic-gate  * certificate status is revoked. Returns numerical value of
254*0Sstevel@tonic-gate  * status.
255*0Sstevel@tonic-gate  */
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
258*0Sstevel@tonic-gate 				ASN1_GENERALIZEDTIME **revtime,
259*0Sstevel@tonic-gate 				ASN1_GENERALIZEDTIME **thisupd,
260*0Sstevel@tonic-gate 				ASN1_GENERALIZEDTIME **nextupd)
261*0Sstevel@tonic-gate 	{
262*0Sstevel@tonic-gate 	int ret;
263*0Sstevel@tonic-gate 	OCSP_CERTSTATUS *cst;
264*0Sstevel@tonic-gate 	if(!single) return -1;
265*0Sstevel@tonic-gate 	cst = single->certStatus;
266*0Sstevel@tonic-gate 	ret = cst->type;
267*0Sstevel@tonic-gate 	if (ret == V_OCSP_CERTSTATUS_REVOKED)
268*0Sstevel@tonic-gate 		{
269*0Sstevel@tonic-gate 		OCSP_REVOKEDINFO *rev = cst->value.revoked;
270*0Sstevel@tonic-gate 		if (revtime) *revtime = rev->revocationTime;
271*0Sstevel@tonic-gate 		if (reason)
272*0Sstevel@tonic-gate 			{
273*0Sstevel@tonic-gate 			if(rev->revocationReason)
274*0Sstevel@tonic-gate 				*reason = ASN1_ENUMERATED_get(rev->revocationReason);
275*0Sstevel@tonic-gate 			else *reason = -1;
276*0Sstevel@tonic-gate 			}
277*0Sstevel@tonic-gate 		}
278*0Sstevel@tonic-gate 	if(thisupd) *thisupd = single->thisUpdate;
279*0Sstevel@tonic-gate 	if(nextupd) *nextupd = single->nextUpdate;
280*0Sstevel@tonic-gate 	return ret;
281*0Sstevel@tonic-gate 	}
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate /* This function combines the previous ones: look up a certificate ID and
284*0Sstevel@tonic-gate  * if found extract status information. Return 0 is successful.
285*0Sstevel@tonic-gate  */
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
288*0Sstevel@tonic-gate 				int *reason,
289*0Sstevel@tonic-gate 				ASN1_GENERALIZEDTIME **revtime,
290*0Sstevel@tonic-gate 				ASN1_GENERALIZEDTIME **thisupd,
291*0Sstevel@tonic-gate 				ASN1_GENERALIZEDTIME **nextupd)
292*0Sstevel@tonic-gate 	{
293*0Sstevel@tonic-gate 	int i;
294*0Sstevel@tonic-gate 	OCSP_SINGLERESP *single;
295*0Sstevel@tonic-gate 	i = OCSP_resp_find(bs, id, -1);
296*0Sstevel@tonic-gate 	/* Maybe check for multiple responses and give an error? */
297*0Sstevel@tonic-gate 	if(i < 0) return 0;
298*0Sstevel@tonic-gate 	single = OCSP_resp_get0(bs, i);
299*0Sstevel@tonic-gate 	i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
300*0Sstevel@tonic-gate 	if(status) *status = i;
301*0Sstevel@tonic-gate 	return 1;
302*0Sstevel@tonic-gate 	}
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate /* Check validity of thisUpdate and nextUpdate fields. It is possible that the request will
305*0Sstevel@tonic-gate  * take a few seconds to process and/or the time wont be totally accurate. Therefore to avoid
306*0Sstevel@tonic-gate  * rejecting otherwise valid time we allow the times to be within 'nsec' of the current time.
307*0Sstevel@tonic-gate  * Also to avoid accepting very old responses without a nextUpdate field an optional maxage
308*0Sstevel@tonic-gate  * parameter specifies the maximum age the thisUpdate field can be.
309*0Sstevel@tonic-gate  */
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd, ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
312*0Sstevel@tonic-gate 	{
313*0Sstevel@tonic-gate 	int ret = 1;
314*0Sstevel@tonic-gate 	time_t t_now, t_tmp;
315*0Sstevel@tonic-gate 	time(&t_now);
316*0Sstevel@tonic-gate 	/* Check thisUpdate is valid and not more than nsec in the future */
317*0Sstevel@tonic-gate 	if (!ASN1_GENERALIZEDTIME_check(thisupd))
318*0Sstevel@tonic-gate 		{
319*0Sstevel@tonic-gate 		OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
320*0Sstevel@tonic-gate 		ret = 0;
321*0Sstevel@tonic-gate 		}
322*0Sstevel@tonic-gate 	else
323*0Sstevel@tonic-gate 		{
324*0Sstevel@tonic-gate 			t_tmp = t_now + nsec;
325*0Sstevel@tonic-gate 			if (X509_cmp_time(thisupd, &t_tmp) > 0)
326*0Sstevel@tonic-gate 			{
327*0Sstevel@tonic-gate 			OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_NOT_YET_VALID);
328*0Sstevel@tonic-gate 			ret = 0;
329*0Sstevel@tonic-gate 			}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 		/* If maxsec specified check thisUpdate is not more than maxsec in the past */
332*0Sstevel@tonic-gate 		if (maxsec >= 0)
333*0Sstevel@tonic-gate 			{
334*0Sstevel@tonic-gate 			t_tmp = t_now - maxsec;
335*0Sstevel@tonic-gate 			if (X509_cmp_time(thisupd, &t_tmp) < 0)
336*0Sstevel@tonic-gate 				{
337*0Sstevel@tonic-gate 				OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_TOO_OLD);
338*0Sstevel@tonic-gate 				ret = 0;
339*0Sstevel@tonic-gate 				}
340*0Sstevel@tonic-gate 			}
341*0Sstevel@tonic-gate 		}
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	if (!nextupd) return ret;
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	/* Check nextUpdate is valid and not more than nsec in the past */
347*0Sstevel@tonic-gate 	if (!ASN1_GENERALIZEDTIME_check(nextupd))
348*0Sstevel@tonic-gate 		{
349*0Sstevel@tonic-gate 		OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
350*0Sstevel@tonic-gate 		ret = 0;
351*0Sstevel@tonic-gate 		}
352*0Sstevel@tonic-gate 	else
353*0Sstevel@tonic-gate 		{
354*0Sstevel@tonic-gate 		t_tmp = t_now - nsec;
355*0Sstevel@tonic-gate 		if (X509_cmp_time(nextupd, &t_tmp) < 0)
356*0Sstevel@tonic-gate 			{
357*0Sstevel@tonic-gate 			OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_EXPIRED);
358*0Sstevel@tonic-gate 			ret = 0;
359*0Sstevel@tonic-gate 			}
360*0Sstevel@tonic-gate 		}
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	/* Also don't allow nextUpdate to precede thisUpdate */
363*0Sstevel@tonic-gate 	if (ASN1_STRING_cmp(nextupd, thisupd) < 0)
364*0Sstevel@tonic-gate 		{
365*0Sstevel@tonic-gate 		OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
366*0Sstevel@tonic-gate 		ret = 0;
367*0Sstevel@tonic-gate 		}
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	return ret;
370*0Sstevel@tonic-gate 	}
371