xref: /openbsd-src/lib/libcrypto/ocsp/ocsp_cl.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: ocsp_cl.c,v 1.7 2014/07/11 08:44:49 jsing Exp $ */
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 
67 #include <openssl/err.h>
68 #include <openssl/ocsp.h>
69 #include <openssl/objects.h>
70 #include <openssl/pem.h>
71 #include <openssl/rand.h>
72 #include <openssl/x509.h>
73 #include <openssl/x509v3.h>
74 
75 /* Utility functions related to sending OCSP requests and extracting
76  * relevant information from the response.
77  */
78 
79 /* Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ
80  * pointer: useful if we want to add extensions.
81  */
82 OCSP_ONEREQ *
83 OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid)
84 {
85 	OCSP_ONEREQ *one = NULL;
86 
87 	if (!(one = OCSP_ONEREQ_new()))
88 		goto err;
89 	if (one->reqCert)
90 		OCSP_CERTID_free(one->reqCert);
91 	one->reqCert = cid;
92 	if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest->requestList, one))
93 		goto err;
94 	return one;
95 
96 err:
97 	OCSP_ONEREQ_free(one);
98 	return NULL;
99 }
100 
101 /* Set requestorName from an X509_NAME structure */
102 int
103 OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm)
104 {
105 	GENERAL_NAME *gen;
106 
107 	gen = GENERAL_NAME_new();
108 	if (gen == NULL)
109 		return 0;
110 	if (!X509_NAME_set(&gen->d.directoryName, nm)) {
111 		GENERAL_NAME_free(gen);
112 		return 0;
113 	}
114 	gen->type = GEN_DIRNAME;
115 	if (req->tbsRequest->requestorName)
116 		GENERAL_NAME_free(req->tbsRequest->requestorName);
117 	req->tbsRequest->requestorName = gen;
118 	return 1;
119 }
120 
121 /* Add a certificate to an OCSP request */
122 int
123 OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
124 {
125 	OCSP_SIGNATURE *sig;
126 
127 	if (!req->optionalSignature)
128 		req->optionalSignature = OCSP_SIGNATURE_new();
129 	sig = req->optionalSignature;
130 	if (!sig)
131 		return 0;
132 	if (!cert)
133 		return 1;
134 	if (!sig->certs && !(sig->certs = sk_X509_new_null()))
135 		return 0;
136 
137 	if (!sk_X509_push(sig->certs, cert))
138 		return 0;
139 	CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
140 	return 1;
141 }
142 
143 /* Sign an OCSP request set the requestorName to the subjec
144  * name of an optional signers certificate and include one
145  * or more optional certificates in the request. Behaves
146  * like PKCS7_sign().
147  */
148 int
149 OCSP_request_sign(OCSP_REQUEST *req, X509 *signer, EVP_PKEY *key,
150     const EVP_MD *dgst, STACK_OF(X509) *certs, unsigned long flags)
151 {
152 	int i;
153 	OCSP_SIGNATURE *sig;
154 	X509 *x;
155 
156 	if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
157 		goto err;
158 
159 	if (!(req->optionalSignature = sig = OCSP_SIGNATURE_new()))
160 		goto err;
161 	if (key) {
162 		if (!X509_check_private_key(signer, key)) {
163 			OCSPerr(OCSP_F_OCSP_REQUEST_SIGN,
164 			    OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
165 			goto err;
166 		}
167 		if (!OCSP_REQUEST_sign(req, key, dgst))
168 			goto err;
169 	}
170 
171 	if (!(flags & OCSP_NOCERTS)) {
172 		if (!OCSP_request_add1_cert(req, signer))
173 			goto err;
174 		for (i = 0; i < sk_X509_num(certs); i++) {
175 			x = sk_X509_value(certs, i);
176 			if (!OCSP_request_add1_cert(req, x))
177 				goto err;
178 		}
179 	}
180 
181 	return 1;
182 
183 err:
184 	OCSP_SIGNATURE_free(req->optionalSignature);
185 	req->optionalSignature = NULL;
186 	return 0;
187 }
188 
189 /* Get response status */
190 int
191 OCSP_response_status(OCSP_RESPONSE *resp)
192 {
193 	return ASN1_ENUMERATED_get(resp->responseStatus);
194 }
195 
196 /* Extract basic response from OCSP_RESPONSE or NULL if
197  * no basic response present.
198  */
199 OCSP_BASICRESP *
200 OCSP_response_get1_basic(OCSP_RESPONSE *resp)
201 {
202 	OCSP_RESPBYTES *rb;
203 
204 	rb = resp->responseBytes;
205 	if (!rb) {
206 		OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC,
207 		    OCSP_R_NO_RESPONSE_DATA);
208 		return NULL;
209 	}
210 	if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
211 		OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC,
212 		    OCSP_R_NOT_BASIC_RESPONSE);
213 		return NULL;
214 	}
215 
216 	return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
217 }
218 
219 /* Return number of OCSP_SINGLERESP reponses present in
220  * a basic response.
221  */
222 int
223 OCSP_resp_count(OCSP_BASICRESP *bs)
224 {
225 	if (!bs)
226 		return -1;
227 	return sk_OCSP_SINGLERESP_num(bs->tbsResponseData->responses);
228 }
229 
230 /* Extract an OCSP_SINGLERESP response with a given index */
231 OCSP_SINGLERESP *
232 OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
233 {
234 	if (!bs)
235 		return NULL;
236 	return sk_OCSP_SINGLERESP_value(bs->tbsResponseData->responses, idx);
237 }
238 
239 /* Look single response matching a given certificate ID */
240 int
241 OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
242 {
243 	int i;
244 	STACK_OF(OCSP_SINGLERESP) *sresp;
245 	OCSP_SINGLERESP *single;
246 
247 	if (!bs)
248 		return -1;
249 	if (last < 0)
250 		last = 0;
251 	else
252 		last++;
253 	sresp = bs->tbsResponseData->responses;
254 	for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
255 		single = sk_OCSP_SINGLERESP_value(sresp, i);
256 		if (!OCSP_id_cmp(id, single->certId))
257 			return i;
258 	}
259 	return -1;
260 }
261 
262 /* Extract status information from an OCSP_SINGLERESP structure.
263  * Note: the revtime and reason values are only set if the
264  * certificate status is revoked. Returns numerical value of
265  * status.
266  */
267 int
268 OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
269     ASN1_GENERALIZEDTIME **revtime, ASN1_GENERALIZEDTIME **thisupd,
270     ASN1_GENERALIZEDTIME **nextupd)
271 {
272 	int ret;
273 	OCSP_CERTSTATUS *cst;
274 
275 	if (!single)
276 		return -1;
277 	cst = single->certStatus;
278 	ret = cst->type;
279 	if (ret == V_OCSP_CERTSTATUS_REVOKED) {
280 		OCSP_REVOKEDINFO *rev = cst->value.revoked;
281 
282 		if (revtime)
283 			*revtime = rev->revocationTime;
284 		if (reason) {
285 			if (rev->revocationReason)
286 				*reason = ASN1_ENUMERATED_get(
287 				    rev->revocationReason);
288 			else
289 				*reason = -1;
290 		}
291 	}
292 	if (thisupd)
293 		*thisupd = single->thisUpdate;
294 	if (nextupd)
295 		*nextupd = single->nextUpdate;
296 	return ret;
297 }
298 
299 /* This function combines the previous ones: look up a certificate ID and
300  * if found extract status information. Return 0 is successful.
301  */
302 int
303 OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
304     int *reason, ASN1_GENERALIZEDTIME **revtime, ASN1_GENERALIZEDTIME **thisupd,
305     ASN1_GENERALIZEDTIME **nextupd)
306 {
307 	int i;
308 	OCSP_SINGLERESP *single;
309 
310 	i = OCSP_resp_find(bs, id, -1);
311 	/* Maybe check for multiple responses and give an error? */
312 	if (i < 0)
313 		return 0;
314 	single = OCSP_resp_get0(bs, i);
315 	i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
316 	if (status)
317 		*status = i;
318 	return 1;
319 }
320 
321 /* Check validity of thisUpdate and nextUpdate fields. It is possible that the request will
322  * take a few seconds to process and/or the time wont be totally accurate. Therefore to avoid
323  * rejecting otherwise valid time we allow the times to be within 'nsec' of the current time.
324  * Also to avoid accepting very old responses without a nextUpdate field an optional maxage
325  * parameter specifies the maximum age the thisUpdate field can be.
326  */
327 int
328 OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
329     ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
330 {
331 	int ret = 1;
332 	time_t t_now, t_tmp;
333 
334 	time(&t_now);
335 	/* Check thisUpdate is valid and not more than nsec in the future */
336 	if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
337 		OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
338 		    OCSP_R_ERROR_IN_THISUPDATE_FIELD);
339 		ret = 0;
340 	} else {
341 		t_tmp = t_now + nsec;
342 		if (X509_cmp_time(thisupd, &t_tmp) > 0) {
343 			OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
344 			    OCSP_R_STATUS_NOT_YET_VALID);
345 			ret = 0;
346 		}
347 
348 		/* If maxsec specified check thisUpdate is not more than maxsec in the past */
349 		if (maxsec >= 0) {
350 			t_tmp = t_now - maxsec;
351 			if (X509_cmp_time(thisupd, &t_tmp) < 0) {
352 				OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
353 				    OCSP_R_STATUS_TOO_OLD);
354 				ret = 0;
355 			}
356 		}
357 	}
358 
359 	if (!nextupd)
360 		return ret;
361 
362 	/* Check nextUpdate is valid and not more than nsec in the past */
363 	if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
364 		OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
365 		    OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
366 		ret = 0;
367 	} else {
368 		t_tmp = t_now - nsec;
369 		if (X509_cmp_time(nextupd, &t_tmp) < 0) {
370 			OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
371 			    OCSP_R_STATUS_EXPIRED);
372 			ret = 0;
373 		}
374 	}
375 
376 	/* Also don't allow nextUpdate to precede thisUpdate */
377 	if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
378 		OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
379 		    OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
380 		ret = 0;
381 	}
382 
383 	return ret;
384 }
385