OpenCoverage

ocsp_cl.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/libressl/src/crypto/ocsp/ocsp_cl.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* $OpenBSD: ocsp_cl.c,v 1.15 2018/03/17 14:44:34 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/x509.h>-
72#include <openssl/x509v3.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 */-
81OCSP_ONEREQ *-
82OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid)-
83{-
84 OCSP_ONEREQ *one = NULL;-
85-
86 if (!(one = OCSP_ONEREQ_new()))
!(one = OCSP_ONEREQ_new())Description
TRUEnever evaluated
FALSEnever evaluated
0
87 goto err;
never executed: goto err;
0
88 if (one->reqCert)
one->reqCertDescription
TRUEnever evaluated
FALSEnever evaluated
0
89 OCSP_CERTID_free(one->reqCert);
never executed: OCSP_CERTID_free(one->reqCert);
0
90 one->reqCert = cid;-
91 if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest->requestList, one))
reqDescription
TRUEnever evaluated
FALSEnever evaluated
!sk_push(((_ST...P_ONEREQ*)0)))Description
TRUEnever evaluated
FALSEnever evaluated
0
92 goto err;
never executed: goto err;
0
93 return one;
never executed: return one;
0
94-
95err:-
96 OCSP_ONEREQ_free(one);-
97 return NULL;
never executed: return ((void *)0) ;
0
98}-
99-
100/* Set requestorName from an X509_NAME structure */-
101int-
102OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm)-
103{-
104 GENERAL_NAME *gen;-
105-
106 gen = GENERAL_NAME_new();-
107 if (gen == NULL)
gen == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
108 return 0;
never executed: return 0;
0
109 if (!X509_NAME_set(&gen->d.directoryName, nm)) {
!X509_NAME_set...ctoryName, nm)Description
TRUEnever evaluated
FALSEnever evaluated
0
110 GENERAL_NAME_free(gen);-
111 return 0;
never executed: return 0;
0
112 }-
113 gen->type = GEN_DIRNAME;-
114 if (req->tbsRequest->requestorName)
req->tbsRequest->requestorNameDescription
TRUEnever evaluated
FALSEnever evaluated
0
115 GENERAL_NAME_free(req->tbsRequest->requestorName);
never executed: GENERAL_NAME_free(req->tbsRequest->requestorName);
0
116 req->tbsRequest->requestorName = gen;-
117 return 1;
never executed: return 1;
0
118}-
119-
120/* Add a certificate to an OCSP request */-
121int-
122OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)-
123{-
124 OCSP_SIGNATURE *sig;-
125-
126 if (!req->optionalSignature)
!req->optionalSignatureDescription
TRUEnever evaluated
FALSEnever evaluated
0
127 req->optionalSignature = OCSP_SIGNATURE_new();
never executed: req->optionalSignature = OCSP_SIGNATURE_new();
0
128 sig = req->optionalSignature;-
129 if (!sig)
!sigDescription
TRUEnever evaluated
FALSEnever evaluated
0
130 return 0;
never executed: return 0;
0
131 if (!cert)
!certDescription
TRUEnever evaluated
FALSEnever evaluated
0
132 return 1;
never executed: return 1;
0
133 if (!sig->certs && !(sig->certs = sk_X509_new_null()))
!sig->certsDescription
TRUEnever evaluated
FALSEnever evaluated
!(sig->certs =...k_new_null()))Description
TRUEnever evaluated
FALSEnever evaluated
0
134 return 0;
never executed: return 0;
0
135-
136 if (!sk_X509_push(sig->certs, cert))
!sk_push(((_ST... : (X509*)0)))Description
TRUEnever evaluated
FALSEnever evaluated
0
137 return 0;
never executed: return 0;
0
138 CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);-
139 return 1;
never executed: return 1;
0
140}-
141-
142/* Sign an OCSP request set the requestorName to the subjec-
143 * name of an optional signers certificate and include one-
144 * or more optional certificates in the request. Behaves-
145 * like PKCS7_sign().-
146 */-
147int-
148OCSP_request_sign(OCSP_REQUEST *req, X509 *signer, EVP_PKEY *key,-
149 const EVP_MD *dgst, STACK_OF(X509) *certs, unsigned long flags)-
150{-
151 int i;-
152 OCSP_SIGNATURE *sig;-
153 X509 *x;-
154-
155 if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
!OCSP_request_..._name(signer))Description
TRUEnever evaluated
FALSEnever evaluated
0
156 goto err;
never executed: goto err;
0
157-
158 if (!(req->optionalSignature = sig = OCSP_SIGNATURE_new()))
!(req->optiona...GNATURE_new())Description
TRUEnever evaluated
FALSEnever evaluated
0
159 goto err;
never executed: goto err;
0
160 if (key) {
keyDescription
TRUEnever evaluated
FALSEnever evaluated
0
161 if (!X509_check_private_key(signer, key)) {
!X509_check_pr...y(signer, key)Description
TRUEnever evaluated
FALSEnever evaluated
0
162 OCSPerror(OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);-
163 goto err;
never executed: goto err;
0
164 }-
165 if (!OCSP_REQUEST_sign(req, key, dgst))
!ASN1_item_sig...uest,key,dgst)Description
TRUEnever evaluated
FALSEnever evaluated
0
166 goto err;
never executed: goto err;
0
167 }
never executed: end of block
0
168-
169 if (!(flags & OCSP_NOCERTS)) {
!(flags & 0x1)Description
TRUEnever evaluated
FALSEnever evaluated
0
170 if (!OCSP_request_add1_cert(req, signer))
!OCSP_request_...t(req, signer)Description
TRUEnever evaluated
FALSEnever evaluated
0
171 goto err;
never executed: goto err;
0
172 for (i = 0; i < sk_X509_num(certs); i++) {
i < sk_num(((_..._st_X509*)0)))Description
TRUEnever evaluated
FALSEnever evaluated
0
173 x = sk_X509_value(certs, i);-
174 if (!OCSP_request_add1_cert(req, x))
!OCSP_request_...1_cert(req, x)Description
TRUEnever evaluated
FALSEnever evaluated
0
175 goto err;
never executed: goto err;
0
176 }
never executed: end of block
0
177 }
never executed: end of block
0
178-
179 return 1;
never executed: return 1;
0
180-
181err:-
182 OCSP_SIGNATURE_free(req->optionalSignature);-
183 req->optionalSignature = NULL;-
184 return 0;
never executed: return 0;
0
185}-
186-
187/* Get response status */-
188int-
189OCSP_response_status(OCSP_RESPONSE *resp)-
190{-
191 return ASN1_ENUMERATED_get(resp->responseStatus);
never executed: return ASN1_ENUMERATED_get(resp->responseStatus);
0
192}-
193-
194/* Extract basic response from OCSP_RESPONSE or NULL if-
195 * no basic response present.-
196 */-
197OCSP_BASICRESP *-
198OCSP_response_get1_basic(OCSP_RESPONSE *resp)-
199{-
200 OCSP_RESPBYTES *rb;-
201-
202 rb = resp->responseBytes;-
203 if (!rb) {
!rbDescription
TRUEnever evaluated
FALSEnever evaluated
0
204 OCSPerror(OCSP_R_NO_RESPONSE_DATA);-
205 return NULL;
never executed: return ((void *)0) ;
0
206 }-
207 if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
OBJ_obj2nid(rb...seType) != 365Description
TRUEnever evaluated
FALSEnever evaluated
0
208 OCSPerror(OCSP_R_NOT_BASIC_RESPONSE);-
209 return NULL;
never executed: return ((void *)0) ;
0
210 }-
211-
212 return ASN1_item_unpack(rb->response, &OCSP_BASICRESP_it);
never executed: return ASN1_item_unpack(rb->response, &OCSP_BASICRESP_it);
0
213}-
214-
215/* Return number of OCSP_SINGLERESP reponses present in-
216 * a basic response.-
217 */-
218int-
219OCSP_resp_count(OCSP_BASICRESP *bs)-
220{-
221 if (!bs)
!bsDescription
TRUEnever evaluated
FALSEnever evaluated
0
222 return -1;
never executed: return -1;
0
223 return sk_OCSP_SINGLERESP_num(bs->tbsResponseData->responses);
never executed: return sk_num(((_STACK*) (1 ? (bs->tbsResponseData->responses) : (struct stack_st_OCSP_SINGLERESP*)0)));
0
224}-
225-
226/* Extract an OCSP_SINGLERESP response with a given index */-
227OCSP_SINGLERESP *-
228OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)-
229{-
230 if (!bs)
!bsDescription
TRUEnever evaluated
FALSEnever evaluated
0
231 return NULL;
never executed: return ((void *)0) ;
0
232 return sk_OCSP_SINGLERESP_value(bs->tbsResponseData->responses, idx);
never executed: return ((OCSP_SINGLERESP *)sk_value(((_STACK*) (1 ? (bs->tbsResponseData->responses) : (struct stack_st_OCSP_SINGLERESP*)0)), (idx)));
0
233}-
234-
235/* Look single response matching a given certificate ID */-
236int-
237OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)-
238{-
239 int i;-
240 STACK_OF(OCSP_SINGLERESP) *sresp;-
241 OCSP_SINGLERESP *single;-
242-
243 if (!bs)
!bsDescription
TRUEnever evaluated
FALSEnever evaluated
0
244 return -1;
never executed: return -1;
0
245 if (last < 0)
last < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
246 last = 0;
never executed: last = 0;
0
247 else-
248 last++;
never executed: last++;
0
249 sresp = bs->tbsResponseData->responses;-
250 for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
i < sk_num(((_...NGLERESP*)0)))Description
TRUEnever evaluated
FALSEnever evaluated
0
251 single = sk_OCSP_SINGLERESP_value(sresp, i);-
252 if (!OCSP_id_cmp(id, single->certId))
!OCSP_id_cmp(i...ingle->certId)Description
TRUEnever evaluated
FALSEnever evaluated
0
253 return i;
never executed: return i;
0
254 }
never executed: end of block
0
255 return -1;
never executed: return -1;
0
256}-
257-
258/* Extract status information from an OCSP_SINGLERESP structure.-
259 * Note: the revtime and reason values are only set if the-
260 * certificate status is revoked. Returns numerical value of-
261 * status.-
262 */-
263int-
264OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,-
265 ASN1_GENERALIZEDTIME **revtime, ASN1_GENERALIZEDTIME **thisupd,-
266 ASN1_GENERALIZEDTIME **nextupd)-
267{-
268 int ret;-
269 OCSP_CERTSTATUS *cst;-
270-
271 if (!single)
!singleDescription
TRUEnever evaluated
FALSEnever evaluated
0
272 return -1;
never executed: return -1;
0
273 cst = single->certStatus;-
274 ret = cst->type;-
275 if (ret == V_OCSP_CERTSTATUS_REVOKED) {
ret == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
276 OCSP_REVOKEDINFO *rev = cst->value.revoked;-
277-
278 if (revtime)
revtimeDescription
TRUEnever evaluated
FALSEnever evaluated
0
279 *revtime = rev->revocationTime;
never executed: *revtime = rev->revocationTime;
0
280 if (reason) {
reasonDescription
TRUEnever evaluated
FALSEnever evaluated
0
281 if (rev->revocationReason)
rev->revocationReasonDescription
TRUEnever evaluated
FALSEnever evaluated
0
282 *reason = ASN1_ENUMERATED_get(
never executed: *reason = ASN1_ENUMERATED_get( rev->revocationReason);
0
283 rev->revocationReason);
never executed: *reason = ASN1_ENUMERATED_get( rev->revocationReason);
0
284 else-
285 *reason = -1;
never executed: *reason = -1;
0
286 }-
287 }
never executed: end of block
0
288 if (thisupd)
thisupdDescription
TRUEnever evaluated
FALSEnever evaluated
0
289 *thisupd = single->thisUpdate;
never executed: *thisupd = single->thisUpdate;
0
290 if (nextupd)
nextupdDescription
TRUEnever evaluated
FALSEnever evaluated
0
291 *nextupd = single->nextUpdate;
never executed: *nextupd = single->nextUpdate;
0
292 return ret;
never executed: return ret;
0
293}-
294-
295/* This function combines the previous ones: look up a certificate ID and-
296 * if found extract status information. Return 0 is successful.-
297 */-
298int-
299OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,-
300 int *reason, ASN1_GENERALIZEDTIME **revtime, ASN1_GENERALIZEDTIME **thisupd,-
301 ASN1_GENERALIZEDTIME **nextupd)-
302{-
303 int i;-
304 OCSP_SINGLERESP *single;-
305-
306 i = OCSP_resp_find(bs, id, -1);-
307 /* Maybe check for multiple responses and give an error? */-
308 if (i < 0)
i < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
309 return 0;
never executed: return 0;
0
310 single = OCSP_resp_get0(bs, i);-
311 i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);-
312 if (status)
statusDescription
TRUEnever evaluated
FALSEnever evaluated
0
313 *status = i;
never executed: *status = i;
0
314 return 1;
never executed: return 1;
0
315}-
316-
317/* Check validity of thisUpdate and nextUpdate fields. It is possible that the request will-
318 * take a few seconds to process and/or the time wont be totally accurate. Therefore to avoid-
319 * rejecting otherwise valid time we allow the times to be within 'nsec' of the current time.-
320 * Also to avoid accepting very old responses without a nextUpdate field an optional maxage-
321 * parameter specifies the maximum age the thisUpdate field can be.-
322 */-
323int-
324OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,-
325 ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)-
326{-
327 time_t t_now, t_tmp;-
328 struct tm tm_this, tm_next, tm_tmp;-
329-
330 time(&t_now);-
331-
332 /*-
333 * Times must explicitly be a GENERALIZEDTIME as per section-
334 * 4.2.2.1 of RFC 6960 - It is invalid to accept other times-
335 * (such as UTCTIME permitted/required by RFC 5280 for certificates)-
336 */-
337-
338 /* Check thisUpdate is valid and not more than nsec in the future */-
339 if (ASN1_time_parse(thisupd->data, thisupd->length, &tm_this,
ASN1_time_pars...his, 24) != 24Description
TRUEnever evaluated
FALSEnever evaluated
0
340 V_ASN1_GENERALIZEDTIME) != V_ASN1_GENERALIZEDTIME) {
ASN1_time_pars...his, 24) != 24Description
TRUEnever evaluated
FALSEnever evaluated
0
341 OCSPerror(OCSP_R_ERROR_IN_THISUPDATE_FIELD);-
342 return 0;
never executed: return 0;
0
343 } else {-
344 t_tmp = t_now + nsec;-
345 if (gmtime_r(&t_tmp, &tm_tmp) == NULL)
gmtime_r(&t_tm...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
346 return 0;
never executed: return 0;
0
347 if (ASN1_time_tm_cmp(&tm_this, &tm_tmp) > 0) {
ASN1_time_tm_c..., &tm_tmp) > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
348 OCSPerror(OCSP_R_STATUS_NOT_YET_VALID);-
349 return 0;
never executed: return 0;
0
350 }-
351-
352 /*-
353 * If maxsec specified check thisUpdate is not more than maxsec-
354 * in the past-
355 */-
356 if (maxsec >= 0) {
maxsec >= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
357 t_tmp = t_now - maxsec;-
358 if (gmtime_r(&t_tmp, &tm_tmp) == NULL)
gmtime_r(&t_tm...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
359 return 0;
never executed: return 0;
0
360 if (ASN1_time_tm_cmp(&tm_this, &tm_tmp) < 0) {
ASN1_time_tm_c..., &tm_tmp) < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
361 OCSPerror(OCSP_R_STATUS_TOO_OLD);-
362 return 0;
never executed: return 0;
0
363 }-
364 }
never executed: end of block
0
365 }
never executed: end of block
0
366-
367 if (!nextupd)
!nextupdDescription
TRUEnever evaluated
FALSEnever evaluated
0
368 return 1;
never executed: return 1;
0
369-
370 /* Check nextUpdate is valid and not more than nsec in the past */-
371 if (ASN1_time_parse(nextupd->data, nextupd->length, &tm_next,
ASN1_time_pars...ext, 24) != 24Description
TRUEnever evaluated
FALSEnever evaluated
0
372 V_ASN1_GENERALIZEDTIME) != V_ASN1_GENERALIZEDTIME) {
ASN1_time_pars...ext, 24) != 24Description
TRUEnever evaluated
FALSEnever evaluated
0
373 OCSPerror(OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);-
374 return 0;
never executed: return 0;
0
375 } else {-
376 t_tmp = t_now - nsec;-
377 if (gmtime_r(&t_tmp, &tm_tmp) == NULL)
gmtime_r(&t_tm...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
378 return 0;
never executed: return 0;
0
379 if (ASN1_time_tm_cmp(&tm_next, &tm_tmp) < 0) {
ASN1_time_tm_c..., &tm_tmp) < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
380 OCSPerror(OCSP_R_STATUS_EXPIRED);-
381 return 0;
never executed: return 0;
0
382 }-
383 }
never executed: end of block
0
384-
385 /* Also don't allow nextUpdate to precede thisUpdate */-
386 if (ASN1_time_tm_cmp(&tm_next, &tm_this) < 0) {
ASN1_time_tm_c... &tm_this) < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
387 OCSPerror(OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);-
388 return 0;
never executed: return 0;
0
389 }-
390-
391 return 1;
never executed: return 1;
0
392}-
393-
394const OCSP_CERTID *-
395OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single)-
396{-
397 return single->certId;
never executed: return single->certId;
0
398}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2