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