/src/openssl/crypto/ocsp/ocsp_cl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <time.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include <openssl/asn1.h> |
14 | | #include <openssl/objects.h> |
15 | | #include <openssl/x509.h> |
16 | | #include <openssl/pem.h> |
17 | | #include <openssl/x509v3.h> |
18 | | #include <openssl/ocsp.h> |
19 | | #include "ocsp_lcl.h" |
20 | | |
21 | | /* |
22 | | * Utility functions related to sending OCSP requests and extracting relevant |
23 | | * information from the response. |
24 | | */ |
25 | | |
26 | | /* |
27 | | * Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ pointer: |
28 | | * useful if we want to add extensions. |
29 | | */ |
30 | | |
31 | | OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid) |
32 | 0 | { |
33 | 0 | OCSP_ONEREQ *one = NULL; |
34 | 0 |
|
35 | 0 | if ((one = OCSP_ONEREQ_new()) == NULL) |
36 | 0 | return NULL; |
37 | 0 | OCSP_CERTID_free(one->reqCert); |
38 | 0 | one->reqCert = cid; |
39 | 0 | if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest.requestList, one)) { |
40 | 0 | one->reqCert = NULL; /* do not free on error */ |
41 | 0 | goto err; |
42 | 0 | } |
43 | 0 | return one; |
44 | 0 | err: |
45 | 0 | OCSP_ONEREQ_free(one); |
46 | 0 | return NULL; |
47 | 0 | } |
48 | | |
49 | | /* Set requestorName from an X509_NAME structure */ |
50 | | |
51 | | int OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm) |
52 | 0 | { |
53 | 0 | GENERAL_NAME *gen; |
54 | 0 |
|
55 | 0 | gen = GENERAL_NAME_new(); |
56 | 0 | if (gen == NULL) |
57 | 0 | return 0; |
58 | 0 | if (!X509_NAME_set(&gen->d.directoryName, nm)) { |
59 | 0 | GENERAL_NAME_free(gen); |
60 | 0 | return 0; |
61 | 0 | } |
62 | 0 | gen->type = GEN_DIRNAME; |
63 | 0 | GENERAL_NAME_free(req->tbsRequest.requestorName); |
64 | 0 | req->tbsRequest.requestorName = gen; |
65 | 0 | return 1; |
66 | 0 | } |
67 | | |
68 | | /* Add a certificate to an OCSP request */ |
69 | | |
70 | | int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert) |
71 | 0 | { |
72 | 0 | OCSP_SIGNATURE *sig; |
73 | 0 | if (req->optionalSignature == NULL) |
74 | 0 | req->optionalSignature = OCSP_SIGNATURE_new(); |
75 | 0 | sig = req->optionalSignature; |
76 | 0 | if (sig == NULL) |
77 | 0 | return 0; |
78 | 0 | if (cert == NULL) |
79 | 0 | return 1; |
80 | 0 | if (sig->certs == NULL |
81 | 0 | && (sig->certs = sk_X509_new_null()) == NULL) |
82 | 0 | return 0; |
83 | 0 | |
84 | 0 | if (!sk_X509_push(sig->certs, cert)) |
85 | 0 | return 0; |
86 | 0 | X509_up_ref(cert); |
87 | 0 | return 1; |
88 | 0 | } |
89 | | |
90 | | /* |
91 | | * Sign an OCSP request set the requestorName to the subject name of an |
92 | | * optional signers certificate and include one or more optional certificates |
93 | | * in the request. Behaves like PKCS7_sign(). |
94 | | */ |
95 | | |
96 | | int OCSP_request_sign(OCSP_REQUEST *req, |
97 | | X509 *signer, |
98 | | EVP_PKEY *key, |
99 | | const EVP_MD *dgst, |
100 | | STACK_OF(X509) *certs, unsigned long flags) |
101 | 0 | { |
102 | 0 | int i; |
103 | 0 | X509 *x; |
104 | 0 |
|
105 | 0 | if (!OCSP_request_set1_name(req, X509_get_subject_name(signer))) |
106 | 0 | goto err; |
107 | 0 | |
108 | 0 | if ((req->optionalSignature = OCSP_SIGNATURE_new()) == NULL) |
109 | 0 | goto err; |
110 | 0 | if (key) { |
111 | 0 | if (!X509_check_private_key(signer, key)) { |
112 | 0 | OCSPerr(OCSP_F_OCSP_REQUEST_SIGN, |
113 | 0 | OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); |
114 | 0 | goto err; |
115 | 0 | } |
116 | 0 | if (!OCSP_REQUEST_sign(req, key, dgst)) |
117 | 0 | goto err; |
118 | 0 | } |
119 | 0 | |
120 | 0 | if (!(flags & OCSP_NOCERTS)) { |
121 | 0 | if (!OCSP_request_add1_cert(req, signer)) |
122 | 0 | goto err; |
123 | 0 | for (i = 0; i < sk_X509_num(certs); i++) { |
124 | 0 | x = sk_X509_value(certs, i); |
125 | 0 | if (!OCSP_request_add1_cert(req, x)) |
126 | 0 | goto err; |
127 | 0 | } |
128 | 0 | } |
129 | 0 |
|
130 | 0 | return 1; |
131 | 0 | err: |
132 | 0 | OCSP_SIGNATURE_free(req->optionalSignature); |
133 | 0 | req->optionalSignature = NULL; |
134 | 0 | return 0; |
135 | 0 | } |
136 | | |
137 | | /* Get response status */ |
138 | | |
139 | | int OCSP_response_status(OCSP_RESPONSE *resp) |
140 | 0 | { |
141 | 0 | return ASN1_ENUMERATED_get(resp->responseStatus); |
142 | 0 | } |
143 | | |
144 | | /* |
145 | | * Extract basic response from OCSP_RESPONSE or NULL if no basic response |
146 | | * present. |
147 | | */ |
148 | | |
149 | | OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp) |
150 | 0 | { |
151 | 0 | OCSP_RESPBYTES *rb; |
152 | 0 | rb = resp->responseBytes; |
153 | 0 | if (!rb) { |
154 | 0 | OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NO_RESPONSE_DATA); |
155 | 0 | return NULL; |
156 | 0 | } |
157 | 0 | if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) { |
158 | 0 | OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NOT_BASIC_RESPONSE); |
159 | 0 | return NULL; |
160 | 0 | } |
161 | 0 |
|
162 | 0 | return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP)); |
163 | 0 | } |
164 | | |
165 | | const ASN1_OCTET_STRING *OCSP_resp_get0_signature(const OCSP_BASICRESP *bs) |
166 | 0 | { |
167 | 0 | return bs->signature; |
168 | 0 | } |
169 | | |
170 | | /* |
171 | | * Return number of OCSP_SINGLERESP responses present in a basic response. |
172 | | */ |
173 | | |
174 | | int OCSP_resp_count(OCSP_BASICRESP *bs) |
175 | 0 | { |
176 | 0 | if (!bs) |
177 | 0 | return -1; |
178 | 0 | return sk_OCSP_SINGLERESP_num(bs->tbsResponseData.responses); |
179 | 0 | } |
180 | | |
181 | | /* Extract an OCSP_SINGLERESP response with a given index */ |
182 | | |
183 | | OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx) |
184 | 0 | { |
185 | 0 | if (!bs) |
186 | 0 | return NULL; |
187 | 0 | return sk_OCSP_SINGLERESP_value(bs->tbsResponseData.responses, idx); |
188 | 0 | } |
189 | | |
190 | | const ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(const OCSP_BASICRESP* bs) |
191 | 0 | { |
192 | 0 | return bs->tbsResponseData.producedAt; |
193 | 0 | } |
194 | | |
195 | | const STACK_OF(X509) *OCSP_resp_get0_certs(const OCSP_BASICRESP *bs) |
196 | 0 | { |
197 | 0 | return bs->certs; |
198 | 0 | } |
199 | | |
200 | | int OCSP_resp_get0_id(const OCSP_BASICRESP *bs, |
201 | | const ASN1_OCTET_STRING **pid, |
202 | | const X509_NAME **pname) |
203 | 0 | { |
204 | 0 | const OCSP_RESPID *rid = &bs->tbsResponseData.responderId; |
205 | 0 |
|
206 | 0 | if (rid->type == V_OCSP_RESPID_NAME) { |
207 | 0 | *pname = rid->value.byName; |
208 | 0 | *pid = NULL; |
209 | 0 | } else if (rid->type == V_OCSP_RESPID_KEY) { |
210 | 0 | *pid = rid->value.byKey; |
211 | 0 | *pname = NULL; |
212 | 0 | } else { |
213 | 0 | return 0; |
214 | 0 | } |
215 | 0 | return 1; |
216 | 0 | } |
217 | | |
218 | | int OCSP_resp_get1_id(const OCSP_BASICRESP *bs, |
219 | | ASN1_OCTET_STRING **pid, |
220 | | X509_NAME **pname) |
221 | 0 | { |
222 | 0 | const OCSP_RESPID *rid = &bs->tbsResponseData.responderId; |
223 | 0 |
|
224 | 0 | if (rid->type == V_OCSP_RESPID_NAME) { |
225 | 0 | *pname = X509_NAME_dup(rid->value.byName); |
226 | 0 | *pid = NULL; |
227 | 0 | } else if (rid->type == V_OCSP_RESPID_KEY) { |
228 | 0 | *pid = ASN1_OCTET_STRING_dup(rid->value.byKey); |
229 | 0 | *pname = NULL; |
230 | 0 | } else { |
231 | 0 | return 0; |
232 | 0 | } |
233 | 0 | if (*pname == NULL && *pid == NULL) |
234 | 0 | return 0; |
235 | 0 | return 1; |
236 | 0 | } |
237 | | |
238 | | /* Look single response matching a given certificate ID */ |
239 | | |
240 | | int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last) |
241 | 0 | { |
242 | 0 | int i; |
243 | 0 | STACK_OF(OCSP_SINGLERESP) *sresp; |
244 | 0 | OCSP_SINGLERESP *single; |
245 | 0 | if (!bs) |
246 | 0 | return -1; |
247 | 0 | if (last < 0) |
248 | 0 | last = 0; |
249 | 0 | else |
250 | 0 | last++; |
251 | 0 | sresp = bs->tbsResponseData.responses; |
252 | 0 | for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) { |
253 | 0 | single = sk_OCSP_SINGLERESP_value(sresp, i); |
254 | 0 | if (!OCSP_id_cmp(id, single->certId)) |
255 | 0 | return i; |
256 | 0 | } |
257 | 0 | return -1; |
258 | 0 | } |
259 | | |
260 | | /* |
261 | | * Extract status information from an OCSP_SINGLERESP structure. Note: the |
262 | | * revtime and reason values are only set if the certificate status is |
263 | | * revoked. Returns numerical value of status. |
264 | | */ |
265 | | |
266 | | int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason, |
267 | | ASN1_GENERALIZEDTIME **revtime, |
268 | | ASN1_GENERALIZEDTIME **thisupd, |
269 | | ASN1_GENERALIZEDTIME **nextupd) |
270 | 0 | { |
271 | 0 | int ret; |
272 | 0 | OCSP_CERTSTATUS *cst; |
273 | 0 | if (!single) |
274 | 0 | return -1; |
275 | 0 | cst = single->certStatus; |
276 | 0 | ret = cst->type; |
277 | 0 | if (ret == V_OCSP_CERTSTATUS_REVOKED) { |
278 | 0 | OCSP_REVOKEDINFO *rev = cst->value.revoked; |
279 | 0 | if (revtime) |
280 | 0 | *revtime = rev->revocationTime; |
281 | 0 | if (reason) { |
282 | 0 | if (rev->revocationReason) |
283 | 0 | *reason = ASN1_ENUMERATED_get(rev->revocationReason); |
284 | 0 | else |
285 | 0 | *reason = -1; |
286 | 0 | } |
287 | 0 | } |
288 | 0 | if (thisupd) |
289 | 0 | *thisupd = single->thisUpdate; |
290 | 0 | if (nextupd) |
291 | 0 | *nextupd = single->nextUpdate; |
292 | 0 | return ret; |
293 | 0 | } |
294 | | |
295 | | /* |
296 | | * This function combines the previous ones: look up a certificate ID and if |
297 | | * found extract status information. Return 0 is successful. |
298 | | */ |
299 | | |
300 | | int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status, |
301 | | int *reason, |
302 | | ASN1_GENERALIZEDTIME **revtime, |
303 | | ASN1_GENERALIZEDTIME **thisupd, |
304 | | ASN1_GENERALIZEDTIME **nextupd) |
305 | 0 | { |
306 | 0 | int i; |
307 | 0 | OCSP_SINGLERESP *single; |
308 | 0 | i = OCSP_resp_find(bs, id, -1); |
309 | 0 | /* Maybe check for multiple responses and give an error? */ |
310 | 0 | if (i < 0) |
311 | 0 | return 0; |
312 | 0 | single = OCSP_resp_get0(bs, i); |
313 | 0 | i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd); |
314 | 0 | if (status) |
315 | 0 | *status = i; |
316 | 0 | return 1; |
317 | 0 | } |
318 | | |
319 | | /* |
320 | | * Check validity of thisUpdate and nextUpdate fields. It is possible that |
321 | | * the request will take a few seconds to process and/or the time won't be |
322 | | * totally accurate. Therefore to avoid rejecting otherwise valid time we |
323 | | * allow the times to be within 'nsec' of the current time. Also to avoid |
324 | | * accepting very old responses without a nextUpdate field an optional maxage |
325 | | * parameter specifies the maximum age the thisUpdate field can be. |
326 | | */ |
327 | | |
328 | | int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd, |
329 | | ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec) |
330 | 0 | { |
331 | 0 | int ret = 1; |
332 | 0 | time_t t_now, t_tmp; |
333 | 0 | time(&t_now); |
334 | 0 | /* Check thisUpdate is valid and not more than nsec in the future */ |
335 | 0 | if (!ASN1_GENERALIZEDTIME_check(thisupd)) { |
336 | 0 | OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_THISUPDATE_FIELD); |
337 | 0 | ret = 0; |
338 | 0 | } else { |
339 | 0 | t_tmp = t_now + nsec; |
340 | 0 | if (X509_cmp_time(thisupd, &t_tmp) > 0) { |
341 | 0 | OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_NOT_YET_VALID); |
342 | 0 | ret = 0; |
343 | 0 | } |
344 | 0 |
|
345 | 0 | /* |
346 | 0 | * If maxsec specified check thisUpdate is not more than maxsec in |
347 | 0 | * the past |
348 | 0 | */ |
349 | 0 | if (maxsec >= 0) { |
350 | 0 | t_tmp = t_now - maxsec; |
351 | 0 | if (X509_cmp_time(thisupd, &t_tmp) < 0) { |
352 | 0 | OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_TOO_OLD); |
353 | 0 | ret = 0; |
354 | 0 | } |
355 | 0 | } |
356 | 0 | } |
357 | 0 |
|
358 | 0 | if (!nextupd) |
359 | 0 | return ret; |
360 | 0 | |
361 | 0 | /* Check nextUpdate is valid and not more than nsec in the past */ |
362 | 0 | if (!ASN1_GENERALIZEDTIME_check(nextupd)) { |
363 | 0 | OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD); |
364 | 0 | ret = 0; |
365 | 0 | } else { |
366 | 0 | t_tmp = t_now - nsec; |
367 | 0 | if (X509_cmp_time(nextupd, &t_tmp) < 0) { |
368 | 0 | OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_EXPIRED); |
369 | 0 | ret = 0; |
370 | 0 | } |
371 | 0 | } |
372 | 0 |
|
373 | 0 | /* Also don't allow nextUpdate to precede thisUpdate */ |
374 | 0 | if (ASN1_STRING_cmp(nextupd, thisupd) < 0) { |
375 | 0 | OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, |
376 | 0 | OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE); |
377 | 0 | ret = 0; |
378 | 0 | } |
379 | 0 |
|
380 | 0 | return ret; |
381 | 0 | } |
382 | | |
383 | | const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single) |
384 | 0 | { |
385 | 0 | return single->certId; |
386 | 0 | } |