/src/boringssl/pki/ocsp.cc
Line | Count | Source |
1 | | // Copyright 2016 The Chromium Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "ocsp.h" |
16 | | |
17 | | #include <openssl/bytestring.h> |
18 | | #include <openssl/digest.h> |
19 | | #include <openssl/mem.h> |
20 | | #include <openssl/pool.h> |
21 | | #include <openssl/sha.h> |
22 | | #include <openssl/span.h> |
23 | | |
24 | | #include "cert_errors.h" |
25 | | #include "encode_values.h" |
26 | | #include "extended_key_usage.h" |
27 | | #include "parsed_certificate.h" |
28 | | #include "revocation_util.h" |
29 | | #include "simple_path_builder_delegate.h" |
30 | | #include "string_util.h" |
31 | | #include "verify_certificate_chain.h" |
32 | | #include "verify_name_match.h" |
33 | | #include "verify_signed_data.h" |
34 | | |
35 | | BSSL_NAMESPACE_BEGIN |
36 | | |
37 | | // CertID ::= SEQUENCE { |
38 | | // hashAlgorithm AlgorithmIdentifier, |
39 | | // issuerNameHash OCTET STRING, -- Hash of issuer's DN |
40 | | // issuerKeyHash OCTET STRING, -- Hash of issuer's public key |
41 | | // serialNumber CertificateSerialNumber |
42 | | // } |
43 | 803 | bool ParseOCSPCertID(der::Input raw_tlv, OCSPCertID *out) { |
44 | 803 | der::Parser outer_parser(raw_tlv); |
45 | 803 | der::Parser parser; |
46 | 803 | if (!outer_parser.ReadSequence(&parser)) { |
47 | 491 | return false; |
48 | 491 | } |
49 | 312 | if (outer_parser.HasMore()) { |
50 | 46 | return false; |
51 | 46 | } |
52 | | |
53 | 266 | der::Input sigalg_tlv; |
54 | 266 | if (!parser.ReadRawTLV(&sigalg_tlv)) { |
55 | 9 | return false; |
56 | 9 | } |
57 | 257 | if (!ParseHashAlgorithm(sigalg_tlv, &out->hash_algorithm)) { |
58 | 241 | return false; |
59 | 241 | } |
60 | 16 | if (!parser.ReadTag(CBS_ASN1_OCTETSTRING, &out->issuer_name_hash)) { |
61 | 7 | return false; |
62 | 7 | } |
63 | 9 | if (!parser.ReadTag(CBS_ASN1_OCTETSTRING, &out->issuer_key_hash)) { |
64 | 1 | return false; |
65 | 1 | } |
66 | 8 | if (!parser.ReadTag(CBS_ASN1_INTEGER, &out->serial_number)) { |
67 | 6 | return false; |
68 | 6 | } |
69 | | // TODO(crbug.com/533048005): We do not check `out->serial_number` is a valid |
70 | | // INTEGER because our certificate serial parser does not yet check for valid |
71 | | // INTEGERs. This ensures those non-integers can be represented in OCSP. |
72 | | |
73 | 2 | return !parser.HasMore(); |
74 | 8 | } |
75 | | |
76 | | namespace { |
77 | | |
78 | | // Parses `raw_tlv` to extract an OCSP RevokedInfo (RFC 6960) and stores the |
79 | | // result in the OCSPCertStatus `out`. Returns whether the parsing was |
80 | | // successful. |
81 | | // |
82 | | // RevokedInfo ::= SEQUENCE { |
83 | | // revocationTime GeneralizedTime, |
84 | | // revocationReason [0] EXPLICIT CRLReason OPTIONAL |
85 | | // } |
86 | 322 | bool ParseRevokedInfo(der::Input raw_tlv, OCSPCertStatus *out) { |
87 | 322 | der::Parser parser(raw_tlv); |
88 | 322 | if (!parser.ReadGeneralizedTime(&(out->revocation_time))) { |
89 | 60 | return false; |
90 | 60 | } |
91 | | |
92 | 262 | der::Input reason_input; |
93 | 262 | if (!parser.ReadOptionalTag( |
94 | 262 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0, &reason_input, |
95 | 262 | &(out->has_reason))) { |
96 | 3 | return false; |
97 | 3 | } |
98 | 259 | if (out->has_reason) { |
99 | 176 | der::Parser reason_parser(reason_input); |
100 | 176 | der::Input reason_value_input; |
101 | 176 | uint8_t reason_value; |
102 | 176 | if (!reason_parser.ReadTag(CBS_ASN1_ENUMERATED, &reason_value_input)) { |
103 | 3 | return false; |
104 | 3 | } |
105 | 173 | if (!der::ParseUint8(reason_value_input, &reason_value)) { |
106 | 132 | return false; |
107 | 132 | } |
108 | 41 | if (reason_value > |
109 | 41 | static_cast<uint8_t>(OCSPCertStatus::RevocationReason::LAST)) { |
110 | 14 | return false; |
111 | 14 | } |
112 | 27 | out->revocation_reason = |
113 | 27 | static_cast<OCSPCertStatus::RevocationReason>(reason_value); |
114 | 27 | if (out->revocation_reason == OCSPCertStatus::RevocationReason::UNUSED) { |
115 | 1 | return false; |
116 | 1 | } |
117 | 26 | if (reason_parser.HasMore()) { |
118 | 1 | return false; |
119 | 1 | } |
120 | 26 | } |
121 | 108 | return !parser.HasMore(); |
122 | 259 | } |
123 | | |
124 | | // Parses `raw_tlv` to extract an OCSP CertStatus (RFC 6960) and stores the |
125 | | // result in the OCSPCertStatus `out`. Returns whether the parsing was |
126 | | // successful. |
127 | | // |
128 | | // CertStatus ::= CHOICE { |
129 | | // good [0] IMPLICIT NULL, |
130 | | // revoked [1] IMPLICIT RevokedInfo, |
131 | | // unknown [2] IMPLICIT UnknownInfo |
132 | | // } |
133 | | // |
134 | | // UnknownInfo ::= NULL |
135 | 615 | bool ParseCertStatus(der::Input raw_tlv, OCSPCertStatus *out) { |
136 | 615 | der::Parser parser(raw_tlv); |
137 | 615 | CBS_ASN1_TAG status_tag; |
138 | 615 | der::Input status; |
139 | 615 | if (!parser.ReadTagAndValue(&status_tag, &status)) { |
140 | 0 | return false; |
141 | 0 | } |
142 | | |
143 | 615 | out->has_reason = false; |
144 | 615 | if (status_tag == (CBS_ASN1_CONTEXT_SPECIFIC | 0)) { |
145 | 130 | out->status = OCSPRevocationStatus::GOOD; |
146 | 130 | if (!status.empty()) { |
147 | 13 | return false; |
148 | 13 | } |
149 | 485 | } else if (status_tag == |
150 | 485 | (CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)) { |
151 | 322 | out->status = OCSPRevocationStatus::REVOKED; |
152 | 322 | if (!ParseRevokedInfo(status, out)) { |
153 | 222 | return false; |
154 | 222 | } |
155 | 322 | } else if (status_tag == (CBS_ASN1_CONTEXT_SPECIFIC | 2)) { |
156 | 49 | out->status = OCSPRevocationStatus::UNKNOWN; |
157 | 49 | if (!status.empty()) { |
158 | 13 | return false; |
159 | 13 | } |
160 | 114 | } else { |
161 | 114 | return false; |
162 | 114 | } |
163 | | |
164 | 253 | return !parser.HasMore(); |
165 | 615 | } |
166 | | |
167 | | // Writes the hash of `value` as an OCTET STRING to `cbb`, using `hash_type` as |
168 | | // the algorithm. Returns true on success. |
169 | | bool AppendHashAsOctetString(const EVP_MD *hash_type, CBB *cbb, |
170 | 0 | der::Input value) { |
171 | 0 | CBB octet_string; |
172 | 0 | unsigned hash_len; |
173 | 0 | uint8_t hash_buffer[EVP_MAX_MD_SIZE]; |
174 | |
|
175 | 0 | return CBB_add_asn1(cbb, &octet_string, CBS_ASN1_OCTETSTRING) && |
176 | 0 | EVP_Digest(value.data(), value.size(), hash_buffer, &hash_len, |
177 | 0 | hash_type, nullptr) && |
178 | 0 | CBB_add_bytes(&octet_string, hash_buffer, hash_len) && CBB_flush(cbb); |
179 | 0 | } |
180 | | |
181 | | } // namespace |
182 | | |
183 | | // SingleResponse ::= SEQUENCE { |
184 | | // certID CertID, |
185 | | // certStatus CertStatus, |
186 | | // thisUpdate GeneralizedTime, |
187 | | // nextUpdate [0] EXPLICIT GeneralizedTime OPTIONAL, |
188 | | // singleExtensions [1] EXPLICIT Extensions OPTIONAL |
189 | | // } |
190 | 1.20k | bool ParseOCSPSingleResponse(der::Input raw_tlv, OCSPSingleResponse *out) { |
191 | 1.20k | der::Parser outer_parser(raw_tlv); |
192 | 1.20k | der::Parser parser; |
193 | 1.20k | if (!outer_parser.ReadSequence(&parser)) { |
194 | 510 | return false; |
195 | 510 | } |
196 | 690 | if (outer_parser.HasMore()) { |
197 | 57 | return false; |
198 | 57 | } |
199 | | |
200 | 633 | if (!parser.ReadRawTLV(&(out->cert_id_tlv))) { |
201 | 11 | return false; |
202 | 11 | } |
203 | 622 | der::Input status_tlv; |
204 | 622 | if (!parser.ReadRawTLV(&status_tlv)) { |
205 | 7 | return false; |
206 | 7 | } |
207 | 615 | if (!ParseCertStatus(status_tlv, &(out->cert_status))) { |
208 | 362 | return false; |
209 | 362 | } |
210 | 253 | if (!parser.ReadGeneralizedTime(&(out->this_update))) { |
211 | 141 | return false; |
212 | 141 | } |
213 | | |
214 | 112 | der::Input next_update_input; |
215 | 112 | if (!parser.ReadOptionalTag( |
216 | 112 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0, |
217 | 112 | &next_update_input, &(out->has_next_update))) { |
218 | 7 | return false; |
219 | 7 | } |
220 | 105 | if (out->has_next_update) { |
221 | 36 | der::Parser next_update_parser(next_update_input); |
222 | 36 | if (!next_update_parser.ReadGeneralizedTime(&(out->next_update))) { |
223 | 19 | return false; |
224 | 19 | } |
225 | 17 | if (next_update_parser.HasMore()) { |
226 | 2 | return false; |
227 | 2 | } |
228 | 17 | } |
229 | | |
230 | 84 | if (!parser.ReadOptionalTag( |
231 | 84 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1, |
232 | 84 | &(out->extensions), &(out->has_extensions))) { |
233 | 1 | return false; |
234 | 1 | } |
235 | | |
236 | 83 | return !parser.HasMore(); |
237 | 84 | } |
238 | | |
239 | | namespace { |
240 | | |
241 | | // Parses `raw_tlv` to extract a ResponderID (RFC 6960) and stores the |
242 | | // result in the ResponderID `out`. Returns whether the parsing was successful. |
243 | | // |
244 | | // ResponderID ::= CHOICE { |
245 | | // byName [1] Name, |
246 | | // byKey [2] KeyHash |
247 | | // } |
248 | 376 | bool ParseResponderID(der::Input raw_tlv, OCSPResponseData::ResponderID *out) { |
249 | 376 | der::Parser parser(raw_tlv); |
250 | 376 | CBS_ASN1_TAG id_tag; |
251 | 376 | der::Input id_input; |
252 | 376 | if (!parser.ReadTagAndValue(&id_tag, &id_input)) { |
253 | 0 | return false; |
254 | 0 | } |
255 | | |
256 | 376 | if (id_tag == (CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)) { |
257 | 241 | out->type = OCSPResponseData::ResponderType::NAME; |
258 | 241 | out->name = id_input; |
259 | 241 | } else if (id_tag == (CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 2)) { |
260 | 17 | der::Parser key_parser(id_input); |
261 | 17 | der::Input key_hash; |
262 | 17 | if (!key_parser.ReadTag(CBS_ASN1_OCTETSTRING, &key_hash)) { |
263 | 4 | return false; |
264 | 4 | } |
265 | 13 | if (key_parser.HasMore()) { |
266 | 1 | return false; |
267 | 1 | } |
268 | 12 | if (key_hash.size() != SHA_DIGEST_LENGTH) { |
269 | 7 | return false; |
270 | 7 | } |
271 | | |
272 | 5 | out->type = OCSPResponseData::ResponderType::KEY_HASH; |
273 | 5 | out->key_hash = key_hash; |
274 | 118 | } else { |
275 | 118 | return false; |
276 | 118 | } |
277 | 246 | return !parser.HasMore(); |
278 | 376 | } |
279 | | |
280 | | } // namespace |
281 | | |
282 | | // ResponseData ::= SEQUENCE { |
283 | | // version [0] EXPLICIT Version DEFAULT v1, |
284 | | // responderID ResponderID, |
285 | | // producedAt GeneralizedTime, |
286 | | // responses SEQUENCE OF SingleResponse, |
287 | | // responseExtensions [1] EXPLICIT Extensions OPTIONAL |
288 | | // } |
289 | 1.15k | bool ParseOCSPResponseData(der::Input raw_tlv, OCSPResponseData *out) { |
290 | 1.15k | der::Parser outer_parser(raw_tlv); |
291 | 1.15k | der::Parser parser; |
292 | 1.15k | if (!outer_parser.ReadSequence(&parser)) { |
293 | 512 | return false; |
294 | 512 | } |
295 | 647 | if (outer_parser.HasMore()) { |
296 | 71 | return false; |
297 | 71 | } |
298 | | |
299 | 576 | der::Input version_input; |
300 | 576 | bool version_present; |
301 | 576 | if (!parser.ReadOptionalTag( |
302 | 576 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0, &version_input, |
303 | 576 | &version_present)) { |
304 | 20 | return false; |
305 | 20 | } |
306 | | |
307 | | // For compatibility, we ignore the restriction from X.690 Section 11.5 that |
308 | | // DEFAULT values should be omitted for values equal to the default value. |
309 | | // TODO: Add warning about non-strict parsing. |
310 | 556 | if (version_present) { |
311 | 191 | der::Parser version_parser(version_input); |
312 | 191 | if (!version_parser.ReadUint8(&(out->version))) { |
313 | 160 | return false; |
314 | 160 | } |
315 | 31 | if (version_parser.HasMore()) { |
316 | 1 | return false; |
317 | 1 | } |
318 | 365 | } else { |
319 | 365 | out->version = 0; |
320 | 365 | } |
321 | | |
322 | 395 | if (out->version != 0) { |
323 | 16 | return false; |
324 | 16 | } |
325 | | |
326 | 379 | der::Input responder_input; |
327 | 379 | if (!parser.ReadRawTLV(&responder_input)) { |
328 | 3 | return false; |
329 | 3 | } |
330 | 376 | if (!ParseResponderID(responder_input, &(out->responder_id))) { |
331 | 130 | return false; |
332 | 130 | } |
333 | 246 | if (!parser.ReadGeneralizedTime(&(out->produced_at))) { |
334 | 148 | return false; |
335 | 148 | } |
336 | | |
337 | 98 | der::Parser responses_parser; |
338 | 98 | if (!parser.ReadSequence(&responses_parser)) { |
339 | 33 | return false; |
340 | 33 | } |
341 | 65 | out->responses.clear(); |
342 | 39.0k | while (responses_parser.HasMore()) { |
343 | 39.0k | der::Input single_response; |
344 | 39.0k | if (!responses_parser.ReadRawTLV(&single_response)) { |
345 | 29 | return false; |
346 | 29 | } |
347 | 38.9k | out->responses.push_back(single_response); |
348 | 38.9k | } |
349 | | |
350 | 36 | if (!parser.ReadOptionalTag( |
351 | 36 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1, |
352 | 36 | &(out->extensions), &(out->has_extensions))) { |
353 | 6 | return false; |
354 | 6 | } |
355 | | |
356 | 30 | return !parser.HasMore(); |
357 | 36 | } |
358 | | |
359 | | namespace { |
360 | | |
361 | | // Parses `raw_tlv` to extract a BasicOCSPResponse (RFC 6960) and stores the |
362 | | // result in the OCSPResponse `out`. Returns whether the parsing was |
363 | | // successful. |
364 | | // |
365 | | // BasicOCSPResponse ::= SEQUENCE { |
366 | | // tbsResponseData ResponseData, |
367 | | // signatureAlgorithm AlgorithmIdentifier, |
368 | | // signature BIT STRING, |
369 | | // certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL |
370 | | // } |
371 | 441 | bool ParseBasicOCSPResponse(der::Input raw_tlv, OCSPResponse *out) { |
372 | 441 | der::Parser outer_parser(raw_tlv); |
373 | 441 | der::Parser parser; |
374 | 441 | if (!outer_parser.ReadSequence(&parser)) { |
375 | 1 | return false; |
376 | 1 | } |
377 | 440 | if (outer_parser.HasMore()) { |
378 | 1 | return false; |
379 | 1 | } |
380 | | |
381 | 439 | if (!parser.ReadRawTLV(&(out->data))) { |
382 | 3 | return false; |
383 | 3 | } |
384 | 436 | der::Input sigalg_tlv; |
385 | 436 | if (!parser.ReadRawTLV(&sigalg_tlv)) { |
386 | 3 | return false; |
387 | 3 | } |
388 | | // TODO(crbug.com/634443): Propagate the errors. |
389 | 433 | std::optional<SignatureAlgorithm> sigalg = |
390 | 433 | ParseSignatureAlgorithm(sigalg_tlv); |
391 | 433 | if (!sigalg) { |
392 | 303 | return false; |
393 | 303 | } |
394 | 130 | out->signature_algorithm = sigalg.value(); |
395 | 130 | std::optional<der::BitString> signature = parser.ReadBitString(); |
396 | 130 | if (!signature) { |
397 | 31 | return false; |
398 | 31 | } |
399 | 99 | out->signature = signature.value(); |
400 | 99 | der::Input certs_input; |
401 | 99 | if (!parser.ReadOptionalTag( |
402 | 99 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0, &certs_input, |
403 | 99 | &(out->has_certs))) { |
404 | 6 | return false; |
405 | 6 | } |
406 | | |
407 | 93 | out->certs.clear(); |
408 | 93 | if (out->has_certs) { |
409 | 31 | der::Parser certs_seq_parser(certs_input); |
410 | 31 | der::Parser certs_parser; |
411 | 31 | if (!certs_seq_parser.ReadSequence(&certs_parser)) { |
412 | 6 | return false; |
413 | 6 | } |
414 | 25 | if (certs_seq_parser.HasMore()) { |
415 | 3 | return false; |
416 | 3 | } |
417 | 851 | while (certs_parser.HasMore()) { |
418 | 845 | der::Input cert_tlv; |
419 | 845 | if (!certs_parser.ReadRawTLV(&cert_tlv)) { |
420 | 16 | return false; |
421 | 16 | } |
422 | 829 | out->certs.push_back(cert_tlv); |
423 | 829 | } |
424 | 22 | } |
425 | | |
426 | 68 | return !parser.HasMore(); |
427 | 93 | } |
428 | | |
429 | | } // namespace |
430 | | |
431 | | // OCSPResponse ::= SEQUENCE { |
432 | | // responseStatus OCSPResponseStatus, |
433 | | // responseBytes [0] EXPLICIT ResponseBytes OPTIONAL |
434 | | // } |
435 | | // |
436 | | // ResponseBytes ::= SEQUENCE { |
437 | | // responseType OBJECT IDENTIFIER, |
438 | | // response OCTET STRING |
439 | | // } |
440 | 1.32k | bool ParseOCSPResponse(der::Input raw_tlv, OCSPResponse *out) { |
441 | 1.32k | der::Parser outer_parser(raw_tlv); |
442 | 1.32k | der::Parser parser; |
443 | 1.32k | if (!outer_parser.ReadSequence(&parser)) { |
444 | 528 | return false; |
445 | 528 | } |
446 | 798 | if (outer_parser.HasMore()) { |
447 | 62 | return false; |
448 | 62 | } |
449 | | |
450 | 736 | der::Input response_status_input; |
451 | 736 | uint8_t response_status; |
452 | 736 | if (!parser.ReadTag(CBS_ASN1_ENUMERATED, &response_status_input)) { |
453 | 11 | return false; |
454 | 11 | } |
455 | 725 | if (!der::ParseUint8(response_status_input, &response_status)) { |
456 | 170 | return false; |
457 | 170 | } |
458 | 555 | if (response_status > |
459 | 555 | static_cast<uint8_t>(OCSPResponse::ResponseStatus::LAST)) { |
460 | 15 | return false; |
461 | 15 | } |
462 | 540 | out->status = static_cast<OCSPResponse::ResponseStatus>(response_status); |
463 | 540 | if (out->status == OCSPResponse::ResponseStatus::UNUSED) { |
464 | 1 | return false; |
465 | 1 | } |
466 | | |
467 | 539 | if (out->status == OCSPResponse::ResponseStatus::SUCCESSFUL) { |
468 | 534 | der::Parser outer_bytes_parser; |
469 | 534 | der::Parser bytes_parser; |
470 | 534 | if (!parser.ReadConstructed( |
471 | 534 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0, |
472 | 534 | &outer_bytes_parser)) { |
473 | 2 | return false; |
474 | 2 | } |
475 | 532 | if (!outer_bytes_parser.ReadSequence(&bytes_parser)) { |
476 | 3 | return false; |
477 | 3 | } |
478 | 529 | if (outer_bytes_parser.HasMore()) { |
479 | 1 | return false; |
480 | 1 | } |
481 | | |
482 | 528 | der::Input type_oid; |
483 | 528 | if (!bytes_parser.ReadTag(CBS_ASN1_OBJECT, &type_oid)) { |
484 | 1 | return false; |
485 | 1 | } |
486 | 527 | if (type_oid != der::Input(kBasicOCSPResponseOid)) { |
487 | 85 | return false; |
488 | 85 | } |
489 | | |
490 | | // As per RFC 6960 Section 4.2.1, the value of `response` SHALL be the DER |
491 | | // encoding of BasicOCSPResponse. |
492 | 442 | der::Input response; |
493 | 442 | if (!bytes_parser.ReadTag(CBS_ASN1_OCTETSTRING, &response)) { |
494 | 1 | return false; |
495 | 1 | } |
496 | 441 | if (!ParseBasicOCSPResponse(response, out)) { |
497 | 432 | return false; |
498 | 432 | } |
499 | 9 | if (bytes_parser.HasMore()) { |
500 | 3 | return false; |
501 | 3 | } |
502 | 9 | } |
503 | | |
504 | 11 | return !parser.HasMore(); |
505 | 539 | } |
506 | | |
507 | | namespace { |
508 | | |
509 | | // Checks that the `type` hash of `value` is equal to `hash` |
510 | 0 | bool VerifyHash(const EVP_MD *type, der::Input hash, der::Input value) { |
511 | 0 | unsigned value_hash_len; |
512 | 0 | uint8_t value_hash[EVP_MAX_MD_SIZE]; |
513 | 0 | if (!EVP_Digest(value.data(), value.size(), value_hash, &value_hash_len, type, |
514 | 0 | nullptr)) { |
515 | 0 | return false; |
516 | 0 | } |
517 | | |
518 | 0 | return hash == der::Input(value_hash, value_hash_len); |
519 | 0 | } |
520 | | |
521 | | // Extracts the bytes of the SubjectPublicKey bit string given an SPKI. That is |
522 | | // to say, the value of subjectPublicKey without the leading unused bit |
523 | | // count octet. |
524 | | // |
525 | | // Returns true on success and fills `*spk_tlv` with the result. |
526 | | // |
527 | | // From RFC 5280, Section 4.1 |
528 | | // SubjectPublicKeyInfo ::= SEQUENCE { |
529 | | // algorithm AlgorithmIdentifier, |
530 | | // subjectPublicKey BIT STRING } |
531 | | // |
532 | | // AlgorithmIdentifier ::= SEQUENCE { |
533 | | // algorithm OBJECT IDENTIFIER, |
534 | | // parameters ANY DEFINED BY algorithm OPTIONAL } |
535 | | // |
536 | 0 | bool GetSubjectPublicKeyBytes(der::Input spki_tlv, der::Input *spk_tlv) { |
537 | 0 | CBS outer, inner, alg, spk; |
538 | 0 | uint8_t unused_bit_count; |
539 | 0 | CBS_init(&outer, spki_tlv.data(), spki_tlv.size()); |
540 | | // The subjectPublicKey field includes the unused bit count. For this |
541 | | // application, the unused bit count must be zero, and is not included in |
542 | | // the result. We extract the subjectPubicKey bit string, verify the first |
543 | | // byte is 0, and if so set `spk_tlv` to the remaining bytes. |
544 | 0 | if (!CBS_get_asn1(&outer, &inner, CBS_ASN1_SEQUENCE) || |
545 | 0 | !CBS_get_asn1(&inner, &alg, CBS_ASN1_SEQUENCE) || |
546 | 0 | !CBS_get_asn1(&inner, &spk, CBS_ASN1_BITSTRING) || |
547 | 0 | !CBS_get_u8(&spk, &unused_bit_count) || unused_bit_count != 0) { |
548 | 0 | return false; |
549 | 0 | } |
550 | 0 | *spk_tlv = der::Input(CBS_data(&spk), CBS_len(&spk)); |
551 | 0 | return true; |
552 | 0 | } |
553 | | |
554 | | // Checks the OCSPCertID `id` identifies `certificate`. |
555 | | bool CheckCertIDMatchesCertificate( |
556 | | const OCSPCertID &id, const ParsedCertificate *certificate, |
557 | 0 | const ParsedCertificate *issuer_certificate) { |
558 | 0 | const EVP_MD *type = nullptr; |
559 | 0 | switch (id.hash_algorithm) { |
560 | 0 | case DigestAlgorithm::Md2: |
561 | 0 | case DigestAlgorithm::Md4: |
562 | 0 | case DigestAlgorithm::Md5: |
563 | | // Unsupported. |
564 | 0 | return false; |
565 | 0 | case DigestAlgorithm::Sha1: |
566 | 0 | type = EVP_sha1(); |
567 | 0 | break; |
568 | 0 | case DigestAlgorithm::Sha256: |
569 | 0 | type = EVP_sha256(); |
570 | 0 | break; |
571 | 0 | case DigestAlgorithm::Sha384: |
572 | 0 | type = EVP_sha384(); |
573 | 0 | break; |
574 | 0 | case DigestAlgorithm::Sha512: |
575 | 0 | type = EVP_sha512(); |
576 | 0 | break; |
577 | 0 | } |
578 | | |
579 | 0 | if (!VerifyHash(type, id.issuer_name_hash, certificate->tbs().issuer_tlv)) { |
580 | 0 | return false; |
581 | 0 | } |
582 | | |
583 | 0 | der::Input key_tlv; |
584 | 0 | if (!GetSubjectPublicKeyBytes(issuer_certificate->tbs().spki_tlv, &key_tlv)) { |
585 | 0 | return false; |
586 | 0 | } |
587 | | |
588 | 0 | if (!VerifyHash(type, id.issuer_key_hash, key_tlv)) { |
589 | 0 | return false; |
590 | 0 | } |
591 | | |
592 | 0 | return id.serial_number == certificate->tbs().serial_number; |
593 | 0 | } |
594 | | |
595 | | // TODO(eroman): Revisit how certificate parsing is used by this file. Ideally |
596 | | // would either pass in the parsed bits, or have a better abstraction for lazily |
597 | | // parsing. |
598 | | std::shared_ptr<const ParsedCertificate> OCSPParseCertificate( |
599 | 0 | Span<const uint8_t> der) { |
600 | 0 | ParseCertificateOptions parse_options; |
601 | 0 | parse_options.allow_invalid_serial_numbers = true; |
602 | | |
603 | | // The objects returned by this function only last for the duration of a |
604 | | // single certificate verification, so there is no need to pool them to save |
605 | | // memory. |
606 | | // |
607 | | // TODO(eroman): Swallows the parsing errors. However uses a permissive |
608 | | // parsing model. |
609 | 0 | CertErrors errors; |
610 | 0 | return ParsedCertificate::Create( |
611 | 0 | bssl::UniquePtr<CRYPTO_BUFFER>( |
612 | 0 | CRYPTO_BUFFER_new(der.data(), der.size(), nullptr)), |
613 | 0 | parse_options, &errors); |
614 | 0 | } |
615 | | |
616 | | // Checks that the ResponderID `id` matches the certificate `cert` either |
617 | | // by verifying the name matches that of the certificate or that the hash |
618 | | // matches the certificate's public key hash (RFC 6960, 4.2.2.3). |
619 | | [[nodiscard]] bool CheckResponderIDMatchesCertificate( |
620 | 0 | const OCSPResponseData::ResponderID &id, const ParsedCertificate *cert) { |
621 | 0 | switch (id.type) { |
622 | 0 | case OCSPResponseData::ResponderType::NAME: { |
623 | 0 | der::Input name_rdn; |
624 | 0 | der::Input cert_rdn; |
625 | 0 | if (!der::Parser(id.name).ReadTag(CBS_ASN1_SEQUENCE, &name_rdn) || |
626 | 0 | !der::Parser(cert->tbs().subject_tlv) |
627 | 0 | .ReadTag(CBS_ASN1_SEQUENCE, &cert_rdn)) { |
628 | 0 | return false; |
629 | 0 | } |
630 | 0 | return VerifyNameMatch(name_rdn, cert_rdn); |
631 | 0 | } |
632 | 0 | case OCSPResponseData::ResponderType::KEY_HASH: { |
633 | 0 | der::Input key; |
634 | 0 | if (!GetSubjectPublicKeyBytes(cert->tbs().spki_tlv, &key)) { |
635 | 0 | return false; |
636 | 0 | } |
637 | 0 | return VerifyHash(EVP_sha1(), id.key_hash, key); |
638 | 0 | } |
639 | 0 | } |
640 | | |
641 | 0 | return false; |
642 | 0 | } |
643 | | |
644 | | // Verifies that `responder_certificate` has been authority for OCSP signing, |
645 | | // delegated to it by `issuer_certificate`. |
646 | | // |
647 | | // TODO(eroman): No revocation checks are done (see id-pkix-ocsp-nocheck in the |
648 | | // spec). extension). |
649 | | [[nodiscard]] bool VerifyAuthorizedResponderCert( |
650 | | const std::shared_ptr<const ParsedCertificate> &responder_certificate, |
651 | | const std::shared_ptr<const ParsedCertificate> &issuer_certificate, |
652 | | int64_t verify_time_epoch_seconds, |
653 | 0 | VerifyCertificateChainDelegate *delegate) { |
654 | 0 | CertPathErrors errors; |
655 | 0 | TrustAnchor issuer_trust(CertificateTrust::ForTrustAnchor()); |
656 | 0 | der::GeneralizedTime verify_time; |
657 | 0 | if (!der::EncodePosixTimeAsGeneralizedTime(verify_time_epoch_seconds, |
658 | 0 | &verify_time)) { |
659 | 0 | return false; |
660 | 0 | } |
661 | | // The Authorized Responder must be directly signed by the issuer of the |
662 | | // certificate being checked. Use the RFC 5280 chain verifier to ensure that |
663 | | // all relevant verification checks are done. |
664 | 0 | VerifyCertificateChain( |
665 | 0 | {responder_certificate, issuer_certificate}, issuer_trust, delegate, |
666 | 0 | verify_time, KeyPurpose::ANY_EKU, InitialExplicitPolicy::kFalse, |
667 | 0 | /*user_initial_policy_set=*/{}, InitialPolicyMappingInhibit::kFalse, |
668 | 0 | InitialAnyPolicyInhibit::kFalse, |
669 | 0 | /*user_constrained_policy_set=*/nullptr, &errors); |
670 | 0 | if (errors.ContainsHighSeverityErrors()) { |
671 | 0 | return false; |
672 | 0 | } |
673 | | |
674 | | // TODO(mattm): maybe add a KeyPurpose::OCSP_SIGNING_STRICT to |
675 | | // VerifyCertificateChain instead of checking it as a separate step? |
676 | | // |
677 | | // The Authorized Responder must include the value id-kp-OCSPSigning as |
678 | | // part of the extended key usage extension. |
679 | 0 | if (!responder_certificate->has_extended_key_usage()) { |
680 | 0 | return false; |
681 | 0 | } |
682 | | |
683 | 0 | for (const auto &key_purpose_oid : |
684 | 0 | responder_certificate->extended_key_usage()) { |
685 | 0 | if (key_purpose_oid == der::Input(kOCSPSigning)) { |
686 | 0 | return true; |
687 | 0 | } |
688 | 0 | } |
689 | 0 | return false; |
690 | 0 | } |
691 | | |
692 | | [[nodiscard]] bool VerifyOCSPResponseSignatureGivenCert( |
693 | | const OCSPResponse &response, const ParsedCertificate *cert, |
694 | 0 | VerifyCertificateChainDelegate *delegate) { |
695 | | // Check the signature algorithm against policy. |
696 | 0 | CertErrors unused_errors; |
697 | 0 | if (!delegate->IsSignatureAlgorithmAcceptable(response.signature_algorithm, |
698 | 0 | &unused_errors)) { |
699 | 0 | return false; |
700 | 0 | } |
701 | | |
702 | 0 | return VerifySignedData(response.signature_algorithm, response.data, |
703 | 0 | response.signature, cert->tbs().spki_tlv, |
704 | 0 | delegate->GetVerifyCache()); |
705 | 0 | } |
706 | | |
707 | | // Verifies that the OCSP response has a valid signature using |
708 | | // `issuer_certificate`, or an authorized responder issued by |
709 | | // `issuer_certificate` for OCSP signing. |
710 | | [[nodiscard]] bool VerifyOCSPResponseSignature( |
711 | | const OCSPResponse &response, const OCSPResponseData &response_data, |
712 | | const std::shared_ptr<const ParsedCertificate> &issuer_certificate, |
713 | | int64_t verify_time_epoch_seconds, |
714 | 0 | VerifyCertificateChainDelegate *delegate) { |
715 | | // In order to verify the OCSP signature, a valid responder matching the OCSP |
716 | | // Responder ID must be located (RFC 6960, 4.2.2.2). The responder is allowed |
717 | | // to be either the certificate issuer or a delegated authority directly |
718 | | // signed by the issuer. |
719 | 0 | if (CheckResponderIDMatchesCertificate(response_data.responder_id, |
720 | 0 | issuer_certificate.get()) && |
721 | 0 | VerifyOCSPResponseSignatureGivenCert(response, issuer_certificate.get(), |
722 | 0 | delegate)) { |
723 | 0 | return true; |
724 | 0 | } |
725 | | |
726 | | // Otherwise search through the provided certificates for the Authorized |
727 | | // Responder. Want a certificate that: |
728 | | // (1) Matches the OCSP Responder ID. |
729 | | // (2) Has been given authority for OCSP signing by `issuer_certificate`. |
730 | | // (3) Has signed the OCSP response using its public key. |
731 | 0 | for (const auto &responder_cert_tlv : response.certs) { |
732 | 0 | std::shared_ptr<const ParsedCertificate> cur_responder_certificate = |
733 | 0 | OCSPParseCertificate(responder_cert_tlv); |
734 | | |
735 | | // If failed parsing the certificate, keep looking. |
736 | 0 | if (!cur_responder_certificate) { |
737 | 0 | continue; |
738 | 0 | } |
739 | | |
740 | | // If the certificate doesn't match the OCSP's responder ID, keep looking. |
741 | 0 | if (!CheckResponderIDMatchesCertificate(response_data.responder_id, |
742 | 0 | cur_responder_certificate.get())) { |
743 | 0 | continue; |
744 | 0 | } |
745 | | |
746 | | // If the certificate isn't a valid Authorized Responder certificate, keep |
747 | | // looking. |
748 | 0 | if (!VerifyAuthorizedResponderCert(cur_responder_certificate, |
749 | 0 | issuer_certificate, |
750 | 0 | verify_time_epoch_seconds, delegate)) { |
751 | 0 | continue; |
752 | 0 | } |
753 | | |
754 | | // If the certificate signed this OCSP response, have found a match. |
755 | | // Otherwise keep looking. |
756 | 0 | if (VerifyOCSPResponseSignatureGivenCert( |
757 | 0 | response, cur_responder_certificate.get(), delegate)) { |
758 | 0 | return true; |
759 | 0 | } |
760 | 0 | } |
761 | | |
762 | | // Failed to confirm the validity of the OCSP signature using any of the |
763 | | // candidate certificates. |
764 | 0 | return false; |
765 | 0 | } |
766 | | |
767 | | // Parse ResponseData and return false if any unhandled critical extensions are |
768 | | // found. No known critical ResponseData extensions exist. |
769 | | bool ParseOCSPResponseDataExtensions( |
770 | | der::Input response_extensions, |
771 | 0 | OCSPVerifyResult::ResponseStatus *response_details) { |
772 | 0 | std::map<der::Input, ParsedExtension> extensions; |
773 | 0 | if (!ParseExtensions(response_extensions, &extensions)) { |
774 | 0 | *response_details = OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR; |
775 | 0 | return false; |
776 | 0 | } |
777 | | |
778 | 0 | for (const auto &ext : extensions) { |
779 | | // TODO: handle ResponseData extensions |
780 | |
|
781 | 0 | if (ext.second.critical) { |
782 | 0 | *response_details = OCSPVerifyResult::UNHANDLED_CRITICAL_EXTENSION; |
783 | 0 | return false; |
784 | 0 | } |
785 | 0 | } |
786 | | |
787 | 0 | return true; |
788 | 0 | } |
789 | | |
790 | | // Parse SingleResponse and return false if any unhandled critical extensions |
791 | | // (other than the CT extension) are found. The CT-SCT extension is not required |
792 | | // to be marked critical, but since it is handled by Chrome, we will overlook |
793 | | // the flag setting. |
794 | | bool ParseOCSPSingleResponseExtensions( |
795 | | der::Input single_extensions, |
796 | 0 | OCSPVerifyResult::ResponseStatus *response_details) { |
797 | 0 | std::map<der::Input, ParsedExtension> extensions; |
798 | 0 | if (!ParseExtensions(single_extensions, &extensions)) { |
799 | 0 | *response_details = OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR; |
800 | 0 | return false; |
801 | 0 | } |
802 | | |
803 | | // The wire form of the OID 1.3.6.1.4.1.11129.2.4.5 - OCSP SingleExtension for |
804 | | // X.509v3 Certificate Transparency Signed Certificate Timestamp List, see |
805 | | // Section 3.3 of RFC6962. |
806 | 0 | const uint8_t ct_ocsp_ext_oid[] = {0x2B, 0x06, 0x01, 0x04, 0x01, |
807 | 0 | 0xD6, 0x79, 0x02, 0x04, 0x05}; |
808 | 0 | der::Input ct_ext_oid(ct_ocsp_ext_oid); |
809 | |
|
810 | 0 | for (const auto &ext : extensions) { |
811 | | // The CT OCSP extension is handled in ct::ExtractSCTListFromOCSPResponse |
812 | 0 | if (ext.second.oid == ct_ext_oid) { |
813 | 0 | continue; |
814 | 0 | } |
815 | | |
816 | | // TODO: handle SingleResponse extensions |
817 | | |
818 | 0 | if (ext.second.critical) { |
819 | 0 | *response_details = OCSPVerifyResult::UNHANDLED_CRITICAL_EXTENSION; |
820 | 0 | return false; |
821 | 0 | } |
822 | 0 | } |
823 | | |
824 | 0 | return true; |
825 | 0 | } |
826 | | |
827 | | // Loops through the OCSPSingleResponses to find the best match for `cert`. |
828 | | OCSPRevocationStatus GetRevocationStatusForCert( |
829 | | const OCSPResponseData &response_data, const ParsedCertificate *cert, |
830 | | const ParsedCertificate *issuer_certificate, |
831 | | int64_t verify_time_epoch_seconds, std::optional<int64_t> max_age_seconds, |
832 | 0 | OCSPVerifyResult::ResponseStatus *response_details) { |
833 | 0 | OCSPRevocationStatus result = OCSPRevocationStatus::UNKNOWN; |
834 | 0 | *response_details = OCSPVerifyResult::NO_MATCHING_RESPONSE; |
835 | |
|
836 | 0 | for (const auto &single_response_der : response_data.responses) { |
837 | | // In the common case, there should only be one SingleResponse in the |
838 | | // ResponseData (matching the certificate requested and used on this |
839 | | // connection). However, it is possible for the OCSP responder to provide |
840 | | // multiple responses for multiple certificates. Look through all the |
841 | | // provided SingleResponses, and check to see if any match the |
842 | | // certificate. A SingleResponse matches a certificate if it has the same |
843 | | // serial number, issuer name (hash), and issuer public key (hash). |
844 | 0 | OCSPSingleResponse single_response; |
845 | 0 | if (!ParseOCSPSingleResponse(single_response_der, &single_response)) { |
846 | 0 | return OCSPRevocationStatus::UNKNOWN; |
847 | 0 | } |
848 | | |
849 | | // Reject unhandled critical extensions in SingleResponse |
850 | 0 | if (single_response.has_extensions && |
851 | 0 | !ParseOCSPSingleResponseExtensions(single_response.extensions, |
852 | 0 | response_details)) { |
853 | 0 | return OCSPRevocationStatus::UNKNOWN; |
854 | 0 | } |
855 | | |
856 | 0 | OCSPCertID cert_id; |
857 | 0 | if (!ParseOCSPCertID(single_response.cert_id_tlv, &cert_id)) { |
858 | 0 | return OCSPRevocationStatus::UNKNOWN; |
859 | 0 | } |
860 | 0 | if (!CheckCertIDMatchesCertificate(cert_id, cert, issuer_certificate)) { |
861 | 0 | continue; |
862 | 0 | } |
863 | | |
864 | | // The SingleResponse matches the certificate, but may be out of date. Out |
865 | | // of date responses are noted separate from responses with mismatched |
866 | | // serial numbers. If an OCSP responder provides both an up to date |
867 | | // response and an expired response, the up to date response takes |
868 | | // precedence (PROVIDED > INVALID_DATE). |
869 | 0 | if (!CheckRevocationDateValid(single_response.this_update, |
870 | 0 | single_response.has_next_update |
871 | 0 | ? &single_response.next_update |
872 | 0 | : nullptr, |
873 | 0 | verify_time_epoch_seconds, max_age_seconds)) { |
874 | 0 | if (*response_details != OCSPVerifyResult::PROVIDED) { |
875 | 0 | *response_details = OCSPVerifyResult::INVALID_DATE; |
876 | 0 | } |
877 | 0 | continue; |
878 | 0 | } |
879 | | |
880 | | // In the case with multiple matching and up to date responses, keep only |
881 | | // the strictest status (REVOKED > UNKNOWN > GOOD). |
882 | 0 | if (*response_details != OCSPVerifyResult::PROVIDED || |
883 | 0 | result == OCSPRevocationStatus::GOOD || |
884 | 0 | single_response.cert_status.status == OCSPRevocationStatus::REVOKED) { |
885 | 0 | result = single_response.cert_status.status; |
886 | 0 | } |
887 | 0 | *response_details = OCSPVerifyResult::PROVIDED; |
888 | 0 | } |
889 | | |
890 | 0 | return result; |
891 | 0 | } |
892 | | |
893 | | OCSPRevocationStatus CheckOCSP( |
894 | | std::string_view raw_response, const ParsedCertificate *certificate, |
895 | | const std::shared_ptr<const ParsedCertificate> &issuer_certificate, |
896 | | int64_t verify_time_epoch_seconds, std::optional<int64_t> max_age_seconds, |
897 | | VerifyCertificateChainDelegate *delegate, |
898 | 0 | OCSPVerifyResult::ResponseStatus *response_details) { |
899 | 0 | *response_details = OCSPVerifyResult::NOT_CHECKED; |
900 | |
|
901 | 0 | if (raw_response.empty()) { |
902 | 0 | *response_details = OCSPVerifyResult::MISSING; |
903 | 0 | return OCSPRevocationStatus::UNKNOWN; |
904 | 0 | } |
905 | | |
906 | 0 | OCSPResponse response; |
907 | 0 | if (!ParseOCSPResponse(StringAsBytes(raw_response), &response)) { |
908 | 0 | *response_details = OCSPVerifyResult::PARSE_RESPONSE_ERROR; |
909 | 0 | return OCSPRevocationStatus::UNKNOWN; |
910 | 0 | } |
911 | | |
912 | | // RFC 6960 defines all responses `response_status` != SUCCESSFUL as error |
913 | | // responses. No revocation information is provided on error responses, and |
914 | | // the OCSPResponseData structure is not set. |
915 | 0 | if (response.status != OCSPResponse::ResponseStatus::SUCCESSFUL) { |
916 | 0 | *response_details = OCSPVerifyResult::ERROR_RESPONSE; |
917 | 0 | return OCSPRevocationStatus::UNKNOWN; |
918 | 0 | } |
919 | | |
920 | | // Actual revocation information is contained within the BasicOCSPResponse as |
921 | | // a ResponseData structure. The BasicOCSPResponse was parsed above, and |
922 | | // contains an unparsed ResponseData. From RFC 6960: |
923 | | // |
924 | | // BasicOCSPResponse ::= SEQUENCE { |
925 | | // tbsResponseData ResponseData, |
926 | | // signatureAlgorithm AlgorithmIdentifier, |
927 | | // signature BIT STRING, |
928 | | // certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL } |
929 | | // |
930 | | // ResponseData ::= SEQUENCE { |
931 | | // version [0] EXPLICIT Version DEFAULT v1, |
932 | | // responderID ResponderID, |
933 | | // producedAt GeneralizedTime, |
934 | | // responses SEQUENCE OF SingleResponse, |
935 | | // responseExtensions [1] EXPLICIT Extensions OPTIONAL } |
936 | 0 | OCSPResponseData response_data; |
937 | 0 | if (!ParseOCSPResponseData(response.data, &response_data)) { |
938 | 0 | *response_details = OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR; |
939 | 0 | return OCSPRevocationStatus::UNKNOWN; |
940 | 0 | } |
941 | | |
942 | | // Process the OCSP ResponseData extensions. In particular, must reject if |
943 | | // there are any critical extensions that are not understood. |
944 | 0 | if (response_data.has_extensions && |
945 | 0 | !ParseOCSPResponseDataExtensions(response_data.extensions, |
946 | 0 | response_details)) { |
947 | 0 | return OCSPRevocationStatus::UNKNOWN; |
948 | 0 | } |
949 | | |
950 | 0 | if (!certificate || !issuer_certificate) { |
951 | 0 | *response_details = OCSPVerifyResult::NOT_CHECKED; |
952 | 0 | return OCSPRevocationStatus::UNKNOWN; |
953 | 0 | } |
954 | | |
955 | | // If producedAt is outside of the certificate validity period, reject the |
956 | | // response. |
957 | 0 | if (response_data.produced_at < certificate->tbs().validity_not_before || |
958 | 0 | response_data.produced_at > certificate->tbs().validity_not_after) { |
959 | 0 | *response_details = OCSPVerifyResult::BAD_PRODUCED_AT; |
960 | 0 | return OCSPRevocationStatus::UNKNOWN; |
961 | 0 | } |
962 | | |
963 | | // Look through all of the OCSPSingleResponses for a match (based on CertID |
964 | | // and time). |
965 | 0 | OCSPRevocationStatus status = GetRevocationStatusForCert( |
966 | 0 | response_data, certificate, issuer_certificate.get(), |
967 | 0 | verify_time_epoch_seconds, max_age_seconds, response_details); |
968 | | |
969 | | // Check that the OCSP response has a valid signature. It must either be |
970 | | // signed directly by the issuing certificate, or a valid authorized |
971 | | // responder. |
972 | 0 | if (!VerifyOCSPResponseSignature(response, response_data, issuer_certificate, |
973 | 0 | verify_time_epoch_seconds, delegate)) { |
974 | 0 | return OCSPRevocationStatus::UNKNOWN; |
975 | 0 | } |
976 | | |
977 | 0 | return status; |
978 | 0 | } |
979 | | |
980 | | } // namespace |
981 | | |
982 | | OCSPRevocationStatus CheckOCSP( |
983 | | std::string_view raw_response, std::string_view certificate_der, |
984 | | std::string_view issuer_certificate_der, int64_t verify_time_epoch_seconds, |
985 | | std::optional<int64_t> max_age_seconds, |
986 | 0 | OCSPVerifyResult::ResponseStatus *response_details) { |
987 | 0 | std::shared_ptr<const ParsedCertificate> parsed_certificate = |
988 | 0 | OCSPParseCertificate(StringAsBytes(certificate_der)); |
989 | 0 | std::shared_ptr<const ParsedCertificate> parsed_issuer_certificate = |
990 | 0 | OCSPParseCertificate(StringAsBytes(issuer_certificate_der)); |
991 | | // Use a default delegate that matches the previous behavior of allowing |
992 | | // SHA-1. (This doesn't actually need a PathBuilderDelegate, but there isn't |
993 | | // a SimpleVerifyCertificateChainDelegate.) |
994 | | // TODO(mattm): tighten the default allowed sigalgs? |
995 | 0 | SimplePathBuilderDelegate default_verify_delegate( |
996 | 0 | /*min_rsa_modulus_length_bits=*/1024, |
997 | 0 | SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); |
998 | |
|
999 | 0 | return CheckOCSP(raw_response, parsed_certificate.get(), |
1000 | 0 | parsed_issuer_certificate, verify_time_epoch_seconds, |
1001 | 0 | max_age_seconds, &default_verify_delegate, response_details); |
1002 | 0 | } |
1003 | | |
1004 | | OCSPRevocationStatus CheckOCSP( |
1005 | | std::string_view raw_response, |
1006 | | const std::shared_ptr<const ParsedCertificate> &certificate, |
1007 | | const std::shared_ptr<const ParsedCertificate> &issuer_certificate, |
1008 | | int64_t verify_time_epoch_seconds, std::optional<int64_t> max_age_seconds, |
1009 | | VerifyCertificateChainDelegate *delegate, |
1010 | 0 | OCSPVerifyResult::ResponseStatus *response_details) { |
1011 | 0 | BSSL_CHECK(delegate); |
1012 | 0 | return CheckOCSP(raw_response, certificate.get(), issuer_certificate, |
1013 | 0 | verify_time_epoch_seconds, max_age_seconds, delegate, |
1014 | 0 | response_details); |
1015 | 0 | } |
1016 | | |
1017 | | bool CreateOCSPRequest(const ParsedCertificate *cert, |
1018 | | const ParsedCertificate *issuer, |
1019 | 0 | std::vector<uint8_t> *request_der) { |
1020 | 0 | request_der->clear(); |
1021 | |
|
1022 | 0 | bssl::ScopedCBB cbb; |
1023 | | |
1024 | | // This initial buffer size is big enough for 20 octet long serial numbers |
1025 | | // (upper bound from RFC 5280) and then a handful of extra bytes. This |
1026 | | // number doesn't matter for correctness. |
1027 | 0 | const size_t kInitialBufferSize = 100; |
1028 | |
|
1029 | 0 | if (!CBB_init(cbb.get(), kInitialBufferSize)) { |
1030 | 0 | return false; |
1031 | 0 | } |
1032 | | |
1033 | | // OCSPRequest ::= SEQUENCE { |
1034 | | // tbsRequest TBSRequest, |
1035 | | // optionalSignature [0] EXPLICIT Signature OPTIONAL } |
1036 | | // |
1037 | | // TBSRequest ::= SEQUENCE { |
1038 | | // version [0] EXPLICIT Version DEFAULT v1, |
1039 | | // requestorName [1] EXPLICIT GeneralName OPTIONAL, |
1040 | | // requestList SEQUENCE OF Request, |
1041 | | // requestExtensions [2] EXPLICIT Extensions OPTIONAL } |
1042 | 0 | CBB ocsp_request; |
1043 | 0 | if (!CBB_add_asn1(cbb.get(), &ocsp_request, CBS_ASN1_SEQUENCE)) { |
1044 | 0 | return false; |
1045 | 0 | } |
1046 | | |
1047 | 0 | CBB tbs_request; |
1048 | 0 | if (!CBB_add_asn1(&ocsp_request, &tbs_request, CBS_ASN1_SEQUENCE)) { |
1049 | 0 | return false; |
1050 | 0 | } |
1051 | | |
1052 | | // "version", "requestorName", and "requestExtensions" are omitted. |
1053 | | |
1054 | 0 | CBB request_list; |
1055 | 0 | if (!CBB_add_asn1(&tbs_request, &request_list, CBS_ASN1_SEQUENCE)) { |
1056 | 0 | return false; |
1057 | 0 | } |
1058 | | |
1059 | 0 | CBB request; |
1060 | 0 | if (!CBB_add_asn1(&request_list, &request, CBS_ASN1_SEQUENCE)) { |
1061 | 0 | return false; |
1062 | 0 | } |
1063 | | |
1064 | | // Request ::= SEQUENCE { |
1065 | | // reqCert CertID, |
1066 | | // singleRequestExtensions [0] EXPLICIT Extensions OPTIONAL } |
1067 | 0 | CBB req_cert; |
1068 | 0 | if (!CBB_add_asn1(&request, &req_cert, CBS_ASN1_SEQUENCE)) { |
1069 | 0 | return false; |
1070 | 0 | } |
1071 | | |
1072 | | // CertID ::= SEQUENCE { |
1073 | | // hashAlgorithm AlgorithmIdentifier, |
1074 | | // issuerNameHash OCTET STRING, -- Hash of issuer's DN |
1075 | | // issuerKeyHash OCTET STRING, -- Hash of issuer's public key |
1076 | | // serialNumber CertificateSerialNumber } |
1077 | | // |
1078 | | // It is unclear whether the parameters for hashAlgorithm should be omitted or |
1079 | | // NULL. Section 2.1 of RFC 4055 would suggest omitting it is the right |
1080 | | // default behavior. However, both OpenSSL and Go include it, so we match them |
1081 | | // for now. |
1082 | | |
1083 | | // TODO(eroman): Don't use SHA1. |
1084 | 0 | const EVP_MD *md = EVP_sha1(); |
1085 | 0 | if (!EVP_marshal_digest_algorithm(&req_cert, md)) { |
1086 | 0 | return false; |
1087 | 0 | } |
1088 | | |
1089 | 0 | AppendHashAsOctetString(md, &req_cert, issuer->tbs().subject_tlv); |
1090 | |
|
1091 | 0 | der::Input key_tlv; |
1092 | 0 | if (!GetSubjectPublicKeyBytes(issuer->tbs().spki_tlv, &key_tlv)) { |
1093 | 0 | return false; |
1094 | 0 | } |
1095 | 0 | AppendHashAsOctetString(md, &req_cert, key_tlv); |
1096 | |
|
1097 | 0 | CBB serial_number; |
1098 | 0 | if (!CBB_add_asn1(&req_cert, &serial_number, CBS_ASN1_INTEGER)) { |
1099 | 0 | return false; |
1100 | 0 | } |
1101 | 0 | if (!CBB_add_bytes(&serial_number, cert->tbs().serial_number.data(), |
1102 | 0 | cert->tbs().serial_number.size())) { |
1103 | 0 | return false; |
1104 | 0 | } |
1105 | | |
1106 | 0 | uint8_t *result_bytes; |
1107 | 0 | size_t result_bytes_length; |
1108 | 0 | if (!CBB_finish(cbb.get(), &result_bytes, &result_bytes_length)) { |
1109 | 0 | return false; |
1110 | 0 | } |
1111 | 0 | bssl::UniquePtr<uint8_t> delete_tbs_cert_bytes(result_bytes); |
1112 | |
|
1113 | 0 | request_der->assign(result_bytes, result_bytes + result_bytes_length); |
1114 | 0 | return true; |
1115 | 0 | } |
1116 | | |
1117 | | // From RFC 2560 section A.1.1: |
1118 | | // |
1119 | | // An OCSP request using the GET method is constructed as follows: |
1120 | | // |
1121 | | // GET {url}/{url-encoding of base-64 encoding of the DER encoding of |
1122 | | // the OCSPRequest} |
1123 | | std::optional<std::string> CreateOCSPGetURL( |
1124 | | const ParsedCertificate *cert, const ParsedCertificate *issuer, |
1125 | 0 | std::string_view ocsp_responder_url) { |
1126 | 0 | std::vector<uint8_t> ocsp_request_der; |
1127 | 0 | if (!CreateOCSPRequest(cert, issuer, &ocsp_request_der)) { |
1128 | | // Unexpected (means BoringSSL failed an operation). |
1129 | 0 | return std::nullopt; |
1130 | 0 | } |
1131 | | |
1132 | | // Base64 encode the request data. |
1133 | 0 | size_t len; |
1134 | 0 | if (!EVP_EncodedLength(&len, ocsp_request_der.size())) { |
1135 | 0 | return std::nullopt; |
1136 | 0 | } |
1137 | 0 | std::vector<uint8_t> encoded(len); |
1138 | 0 | len = EVP_EncodeBlock(encoded.data(), ocsp_request_der.data(), |
1139 | 0 | ocsp_request_der.size()); |
1140 | |
|
1141 | 0 | std::string b64_encoded(encoded.begin(), encoded.begin() + len); |
1142 | | |
1143 | | // In theory +, /, and = are valid in paths and don't need to be escaped. |
1144 | | // However from the example in RFC 5019 section 5 it is clear that the intent |
1145 | | // is to escape non-alphanumeric characters (the example conclusively escapes |
1146 | | // '/' and '=', but doesn't clarify '+'). |
1147 | 0 | b64_encoded = bssl::string_util::FindAndReplace(b64_encoded, "+", "%2B"); |
1148 | 0 | b64_encoded = bssl::string_util::FindAndReplace(b64_encoded, "/", "%2F"); |
1149 | 0 | b64_encoded = bssl::string_util::FindAndReplace(b64_encoded, "=", "%3D"); |
1150 | | |
1151 | | // No attempt is made to collapse double slashes for URLs that end in slash, |
1152 | | // since the spec doesn't do that. |
1153 | 0 | return std::string(ocsp_responder_url) + "/" + b64_encoded; |
1154 | 0 | } |
1155 | | |
1156 | | BSSL_NAMESPACE_END |