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