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