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