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