Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/ocsp/ocsp_test.cc

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3 #include <gtest/gtest.h>
4 #include <array>
5 #include <ctime>
6
7 #include "openssl/ocsp.h"
8 #include "openssl/pem.h"
9 #include "openssl/bio.h"
10
11 #include "../internal.h"
12 #include "../test/test_util.h"
13 #include "internal.h"
14
15 static const time_t invalid_before_ocsp_update_time = 1621988613;
16 static const time_t valid_after_ocsp_update_time = 1621988615;
17 static const time_t valid_before_ocsp_expire_time = 1937348613;
18 static const time_t invalid_after_ocsp_expire_time = 1937348615;
19
20 static const time_t invalid_before_ocsp_update_time_sha256 = 1622145762;
21 static const time_t valid_after_ocsp_update_time_sha256 = 1622145764;
22 static const time_t valid_before_ocsp_expire_time_sha256 = 1937505762;
23 static const time_t invalid_after_ocsp_expire_time_sha256 = 1937505764;
24
25 #define OCSP_VERIFYSTATUS_SUCCESS 1
26 #define OCSP_VERIFYSTATUS_ERROR 0
27 #define OCSP_VERIFYSTATUS_FATALERROR -1
28
29 #define OCSP_RESPFINDSTATUS_SUCCESS 1
30 #define OCSP_RESPFINDSTATUS_ERROR 0
31 #define OCSP_RESPFINDSTATUS_UNDEFINED -1
32
33 #define OCSP_REQUEST_PARSE_SUCCESS 1
34 #define OCSP_REQUEST_PARSE_ERROR 0
35
36 #define OCSP_HTTP_PARSE_SUCCESS 1
37 #define OCSP_HTTP_PARSE_ERROR 0
38
39 #define OCSP_SIGN_SUCCESS 1
40 #define OCSP_SIGN_ERROR 0
41
42 #define OCSP_URL_PARSE_SUCCESS 1
43 #define OCSP_URL_PARSE_ERROR 0
44
45 std::string GetTestData(const char *path);
46
47 static bool DecodeBase64(std::vector<uint8_t> *out, const char *in) {
48 int temp_length = 0;
49 size_t total_length = 0;
50 EVP_ENCODE_CTX ctx;
51 EVP_DecodeInit(&ctx);
52 out->resize(strlen(in));
53
54 int decode_update_result = EVP_DecodeUpdate(&ctx, out->data(), &temp_length,
55 (const uint8_t *)in, strlen(in));
56 if (decode_update_result != 1 && decode_update_result != 0) {
57 fprintf(stderr, "EVP_DecodeUpdate failed\n");
58 return false;
59 }
60 total_length += temp_length;
61 temp_length = 0;
62
63 if (1 != EVP_DecodeFinal(&ctx, &out->data()[total_length], &temp_length)) {
64 fprintf(stderr, "EVP_DecodeFinal failed\n");
65 return false;
66 }
67 total_length += temp_length;
68 out->resize(total_length);
69 return true;
70 }
71
72 static bssl::UniquePtr<EC_KEY> ECDSAFromPEM(const char *pem) {
73 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(pem, strlen(pem)));
74 return bssl::UniquePtr<EC_KEY>(
75 PEM_read_bio_ECPrivateKey(bio.get(), nullptr, nullptr, nullptr));
76 }
77
78 static bssl::UniquePtr<STACK_OF(X509)> CertChainFromPEM(const char *pem) {
79 bssl::UniquePtr<STACK_OF(X509)> stack(sk_X509_new_null());
80 if (!stack) {
81 return nullptr;
82 }
83
84 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(pem, strlen(pem)));
85 for (;;) {
86 bssl::UniquePtr<X509> cert = bssl::UniquePtr<X509>(
87 PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
88 if (cert == nullptr) {
89 break;
90 }
91 if (!bssl::PushToStack(stack.get(), bssl::UpRef(cert.get()))) {
92 return nullptr;
93 }
94 }
95 return stack;
96 }
97
98 static bssl::UniquePtr<OCSP_RESPONSE> LoadOCSP_RESPONSE(
99 bssl::Span<const uint8_t> der) {
100 const uint8_t *ptr = der.data();
101 return bssl::UniquePtr<OCSP_RESPONSE>(
102 d2i_OCSP_RESPONSE(nullptr, &ptr, der.size()));
103 }
104
105 static bssl::UniquePtr<OCSP_REQUEST> LoadOCSP_REQUEST(
106 bssl::Span<const uint8_t> der) {
107 const uint8_t *ptr = der.data();
108 return bssl::UniquePtr<OCSP_REQUEST>(
109 d2i_OCSP_REQUEST(nullptr, &ptr, der.size()));
110 }
111
112 // Load a generic |OCSP_CERTID| for testing.
113 static OCSP_CERTID *LoadTestOCSP_CERTID() {
114 bssl::UniquePtr<X509> ca_cert(CertFromPEM(
115 GetTestData(std::string("crypto/ocsp/test/aws/ca_cert.pem").c_str())
116 .c_str()));
117 bssl::UniquePtr<X509> server_cert(CertFromPEM(
118 GetTestData(std::string("crypto/ocsp/test/aws/rsa_cert.pem").c_str())
119 .c_str()));
120 return OCSP_cert_to_id(nullptr, ca_cert.get(), server_cert.get());
121 }
122
123 static void ExtractAndVerifyBasicOCSP(
124 bssl::Span<const uint8_t> der, const std::string ca_cert_file,
125 const std::string server_cert_file, int expected_ocsp_verify_status,
126 bssl::UniquePtr<OCSP_BASICRESP> *basic_response,
127 bssl::UniquePtr<STACK_OF(X509)> *server_cert_chain) {
128 bssl::UniquePtr<OCSP_RESPONSE> ocsp_response;
129 ocsp_response = LoadOCSP_RESPONSE(der);
130 ASSERT_TRUE(ocsp_response);
131
132 int ret = OCSP_response_status(ocsp_response.get());
133 if (ret != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
134 return;
135 }
136
137 *basic_response = bssl::UniquePtr<OCSP_BASICRESP>(
138 OCSP_response_get1_basic(ocsp_response.get()));
139 ASSERT_TRUE(*basic_response);
140
141 // Set up trust store and certificate chain.
142 bssl::UniquePtr<X509> ca_cert(CertFromPEM(
143 GetTestData(
144 std::string("crypto/ocsp/test/aws/" + ca_cert_file + ".pem").c_str())
145 .c_str()));
146 bssl::UniquePtr<X509> server_cert(
147 CertFromPEM(GetTestData(std::string("crypto/ocsp/test/aws/" +
148 server_cert_file + ".pem")
149 .c_str())
150 .c_str()));
151
152 bssl::UniquePtr<X509_STORE> trust_store(X509_STORE_new());
153 X509_STORE_add_cert(trust_store.get(), ca_cert.get());
154 *server_cert_chain = CertsToStack({server_cert.get(), ca_cert.get()});
155 ASSERT_TRUE(*server_cert_chain);
156
157 // Verifies the OCSP responder's signature on the OCSP response data.
158 const int ocsp_verify_err = OCSP_basic_verify(
159 basic_response->get(), server_cert_chain->get(), trust_store.get(), 0);
160 ASSERT_EQ(expected_ocsp_verify_status, ocsp_verify_err);
161 }
162
163 static void CheckOCSP_CERTSTATUS(
164 bssl::UniquePtr<OCSP_BASICRESP> *basic_response,
165 bssl::UniquePtr<STACK_OF(X509)> *server_cert_chain, const EVP_MD *dgst,
166 int expected_resp_find_status, int *status, ASN1_GENERALIZEDTIME **thisupd,
167 ASN1_GENERALIZEDTIME **nextupd) {
168 X509 *subject = sk_X509_value(server_cert_chain->get(), 0);
169 X509 *issuer = sk_X509_value(server_cert_chain->get(), 1);
170 // Convert issuer certificate to |OCSP_CERTID|.
171 bssl::UniquePtr<OCSP_CERTID> cert_id =
172 bssl::UniquePtr<OCSP_CERTID>(OCSP_cert_to_id(dgst, subject, issuer));
173 ASSERT_TRUE(cert_id);
174
175 int reason = 0;
176 ASN1_GENERALIZEDTIME *revtime;
177 // Checks revocation status of the response.
178 const int ocsp_resp_find_status_res =
179 OCSP_resp_find_status(basic_response->get(), cert_id.get(), status,
180 &reason, &revtime, thisupd, nextupd);
181 ASSERT_EQ(expected_resp_find_status, ocsp_resp_find_status_res);
182 }
183
184 // Test data below are taken from s2n's ocsp test files:
185 // https://github.com/aws/s2n-tls/blob/main/tests/pems/ocsp
186 // OCSP testing methods were taken from s2n's validation tests:
187 // https://github.com/aws/s2n-tls/blob/main/tests/unit/s2n_x509_validator_test.c
188 struct OCSPAWSTestVector {
189 const char *ocsp_response;
190 const char *cafile;
191 const char *server_cert;
192 const EVP_MD *dgst;
193 int expected_ocsp_verify_status;
194 int expected_ocsp_resp_find_status;
195 int expected_ocsp_cert_status;
196 const char *expected_ocsp_cert_status_string;
197 };
198
199 static const OCSPAWSTestVector nTestVectors[] = {
200 // === SHA1 OCSP RESPONSES ===
201 // Test valid OCSP response signed by an OCSP responder.
202 {"ocsp_response", "ca_cert", "rsa_cert", EVP_sha1(),
203 OCSP_VERIFYSTATUS_SUCCESS, OCSP_RESPFINDSTATUS_SUCCESS,
204 V_OCSP_CERTSTATUS_GOOD, "good"},
205 // Test against same good OCSP response, but checking behavior of not
206 // specifying hash algorithm used for |OCSP_cert_to_id| this time (should
207 // default to sha1). When |*dgst| is set to NULL, the default hash algorithm
208 // should automatically be set to sha1. The revocation status check of the
209 // response should work if hash algorithm of |cert_id| has been set to sha1
210 // successfully.
211 {"ocsp_response", "ca_cert", "rsa_cert", nullptr, OCSP_VERIFYSTATUS_SUCCESS,
212 OCSP_RESPFINDSTATUS_SUCCESS, V_OCSP_CERTSTATUS_GOOD, "good"},
213 // Test valid OCSP response directly signed by the CA certificate.
214 {"ocsp_response_ca_signed", "ca_cert", "rsa_cert", EVP_sha1(),
215 OCSP_VERIFYSTATUS_SUCCESS, OCSP_RESPFINDSTATUS_SUCCESS,
216 V_OCSP_CERTSTATUS_GOOD, "good"},
217 // Test OCSP response status is revoked.
218 {"ocsp_response_revoked", "ca_cert", "rsa_cert", EVP_sha1(),
219 OCSP_VERIFYSTATUS_SUCCESS, OCSP_RESPFINDSTATUS_SUCCESS,
220 V_OCSP_CERTSTATUS_REVOKED, "revoked"},
221 // Test OCSP response status is unknown.
222 {"ocsp_response_unknown", "ca_cert", "rsa_cert", EVP_sha1(),
223 OCSP_VERIFYSTATUS_SUCCESS, OCSP_RESPFINDSTATUS_SUCCESS,
224 V_OCSP_CERTSTATUS_UNKNOWN, "unknown"},
225 // for the requested certificate. (So this would be a completely valid
226 // response to a different OCSP request for the other certificate.)
227 {"ocsp_response", "ca_cert", "ecdsa_cert", EVP_sha1(),
228 OCSP_VERIFYSTATUS_SUCCESS, OCSP_RESPFINDSTATUS_ERROR, -1, nullptr},
229 // Test OCSP response where the requested certificate was signed by the OCSP
230 // responder, but signed by the wrong requested OCSP responder key
231 // certificate.
232 // However, this incorrect OCSP responder certificate may be a valid OCSP
233 // responder for some other case and also chains to a trusted root.
234 {"ocsp_response_wrong_signer", "ca_cert", "rsa_cert", EVP_sha1(),
235 OCSP_VERIFYSTATUS_ERROR, OCSP_RESPFINDSTATUS_UNDEFINED, -1, nullptr},
236 // Test OCSP response where the requested certificate was signed by an OCSP
237 // responder with an expired certificate.
238 // However, this incorrect OCSP responder certificate may be a valid OCSP
239 // responder for some other case and also chains to a trusted root.
240 {"ocsp_response_expired_signer", "ca_cert", "rsa_cert", EVP_sha1(),
241 OCSP_VERIFYSTATUS_ERROR, OCSP_RESPFINDSTATUS_UNDEFINED, -1, nullptr},
242
243 // === SHA256 OCSP RESPONSES ===
244 // Test valid OCSP response signed by an OCSP responder.
245 {"ocsp_response_sha256", "ca_cert", "rsa_cert", EVP_sha256(),
246 OCSP_VERIFYSTATUS_SUCCESS, OCSP_RESPFINDSTATUS_SUCCESS,
247 V_OCSP_CERTSTATUS_GOOD, "good"},
248 // Test a SHA-256 revoked OCSP response status.
249 {"ocsp_response_revoked_sha256", "ca_cert", "rsa_cert", EVP_sha256(),
250 OCSP_VERIFYSTATUS_SUCCESS, OCSP_RESPFINDSTATUS_SUCCESS,
251 V_OCSP_CERTSTATUS_REVOKED, "revoked"},
252 // Test a SHA-256 unknown OCSP response status.
253 {"ocsp_response_unknown_sha256", "ca_cert", "rsa_cert", EVP_sha256(),
254 OCSP_VERIFYSTATUS_SUCCESS, OCSP_RESPFINDSTATUS_SUCCESS,
255 V_OCSP_CERTSTATUS_UNKNOWN, "unknown"},
256 // Test a SHA-256 OCSP response signed by the correct responder certificate,
257 // but not for the requested certificate. (So this would be a completely
258 // valid response to a different OCSP request for the other certificate.)
259 {"ocsp_response_sha256", "ca_cert", "ecdsa_cert", EVP_sha256(),
260 OCSP_VERIFYSTATUS_SUCCESS, OCSP_RESPFINDSTATUS_ERROR, -1, nullptr},
261 // Test a SHA-256 OCSP response signed by the wrong responder certificate,
262 // but the requested certificate was signed. (however this incorrect OCSP
263 // responder certificate is a valid OCSP responder for some other case and
264 // chains to a trusted root). Thus, this response is not valid for any
265 // request.
266 {"ocsp_response_wrong_signer_sha256", "ca_cert", "rsa_cert", EVP_sha256(),
267 OCSP_VERIFYSTATUS_ERROR, OCSP_RESPFINDSTATUS_UNDEFINED, -1, nullptr},
268 };
269
270 class OCSPTestAWS : public testing::TestWithParam<OCSPAWSTestVector> {};
271
272 INSTANTIATE_TEST_SUITE_P(All, OCSPTestAWS, testing::ValuesIn(nTestVectors));
273
274 TEST_P(OCSPTestAWS, VerifyOCSPResponse) {
275 const OCSPAWSTestVector &t = GetParam();
276
277 std::string data =
278 GetTestData(std::string("crypto/ocsp/test/aws/" +
279 std::string(t.ocsp_response) + ".der")
280 .c_str());
281 std::vector<uint8_t> ocsp_reponse_data(data.begin(), data.end());
282
283 // OCSP response parsing and verification step.
284 bssl::UniquePtr<OCSP_BASICRESP> basic_response;
285 bssl::UniquePtr<STACK_OF(X509)> server_cert_chain;
286 ExtractAndVerifyBasicOCSP(ocsp_reponse_data, t.cafile, t.server_cert,
287 t.expected_ocsp_verify_status, &basic_response,
288 &server_cert_chain);
289
290 // If OCSP basic verify is successful, we check the OCSP response status.
291 if (t.expected_ocsp_verify_status == OCSP_VERIFYSTATUS_SUCCESS) {
292 int status = 0;
293 ASN1_GENERALIZEDTIME *thisupd, *nextupd;
294 CheckOCSP_CERTSTATUS(&basic_response, &server_cert_chain, t.dgst,
295 t.expected_ocsp_resp_find_status, &status, &thisupd,
296 &nextupd);
297 // If OCSP response status retrieving is successful, we check if the cert
298 // status of the OCSP response is correct.
299 if (t.expected_ocsp_resp_find_status == OCSP_RESPFINDSTATUS_SUCCESS) {
300 ASSERT_EQ(t.expected_ocsp_cert_status, status);
301 ASSERT_EQ(std::string(t.expected_ocsp_cert_status_string),
302 std::string(OCSP_cert_status_str(status)));
303 }
304 }
305 }
306
307 struct OCSPResponseStatusTestVector {
308 const char *ocsp_response;
309 int expected_ocsp_status;
310 const char *expected_ocsp_status_string;
311 };
312
313 static const OCSPResponseStatusTestVector respTestVectors[] = {
314 // === Invalid OCSP response requests sent back an OCSP responder ===
315 // https://datatracker.ietf.org/doc/html/rfc6960#section-4.2.1
316 // OCSPResponseStatus: successful
317 {"ocsp_response", OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful"},
318 // OCSPResponseStatus: malformedRequest
319 {"ocsp_response_malformedrequest", OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
320 "malformedrequest"},
321 // OCSPResponseStatus: internalError
322 {"ocsp_response_internalerror", OCSP_RESPONSE_STATUS_INTERNALERROR,
323 "internalerror"},
324 // OCSPResponseStatus: tryLater
325 {"ocsp_response_trylater", OCSP_RESPONSE_STATUS_TRYLATER, "trylater"},
326 // OCSPResponseStatus: sigRequired
327 {"ocsp_response_sigrequired", OCSP_RESPONSE_STATUS_SIGREQUIRED,
328 "sigrequired"},
329 // OCSPResponseStatus: unauthorized
330 {"ocsp_response_unauthorized", OCSP_RESPONSE_STATUS_UNAUTHORIZED,
331 "unauthorized"},
332 };
333
334 class OCSPResponseStatusTest
335 : public testing::TestWithParam<OCSPResponseStatusTestVector> {};
336
337 INSTANTIATE_TEST_SUITE_P(All, OCSPResponseStatusTest,
338 testing::ValuesIn(respTestVectors));
339
340 TEST_P(OCSPResponseStatusTest, OCSPResponseStatus) {
341 const OCSPResponseStatusTestVector &t = GetParam();
342
343 std::string data =
344 GetTestData(std::string("crypto/ocsp/test/aws/" +
345 std::string(t.ocsp_response) + ".der")
346 .c_str());
347 std::vector<uint8_t> ocsp_reponse_data(data.begin(), data.end());
348
349 bssl::UniquePtr<OCSP_RESPONSE> ocsp_response(
350 LoadOCSP_RESPONSE(ocsp_reponse_data));
351 ASSERT_TRUE(ocsp_response);
352
353 int ret = OCSP_response_status(ocsp_response.get());
354 ASSERT_EQ(t.expected_ocsp_status, ret);
355 ASSERT_EQ(std::string(t.expected_ocsp_status_string),
356 std::string(OCSP_response_status_str(ret)));
357 }
358
359
360 // === Specific test cases ===
361
362 // Test valid OCSP response signed by an OCSP responder along with check for
363 // correct time field parsing.
364 TEST(OCSPTest, TestGoodOCSP) {
365 std::string data = GetTestData(
366 std::string("crypto/ocsp/test/aws/ocsp_response.der").c_str());
367 std::vector<uint8_t> ocsp_reponse_data(data.begin(), data.end());
368
369 bssl::UniquePtr<OCSP_BASICRESP> basic_response;
370 bssl::UniquePtr<STACK_OF(X509)> server_cert_chain;
371 ExtractAndVerifyBasicOCSP(ocsp_reponse_data, "ca_cert", "rsa_cert",
372 OCSP_VERIFYSTATUS_SUCCESS, &basic_response,
373 &server_cert_chain);
374
375 int status = 0;
376 ASN1_GENERALIZEDTIME *thisupd, *nextupd;
377 CheckOCSP_CERTSTATUS(&basic_response, &server_cert_chain, EVP_sha1(),
378 OCSP_RESPFINDSTATUS_SUCCESS, &status, &thisupd,
379 &nextupd);
380 ASSERT_EQ(V_OCSP_CERTSTATUS_GOOD, status);
381
382 // There is 1 |OCSP_SINGLERESP| in the |basic_response|.
383 EXPECT_EQ(OCSP_resp_count(basic_response.get()), 1);
384
385 // If OCSP response is verifiable and all good, an OCSP client should check
386 // time fields to see if the response is still valid.
387
388 // Check before OCSP was last updated connection timestamp
389 time_t connection_time = invalid_before_ocsp_update_time;
390 ASSERT_EQ(1, X509_cmp_time(thisupd, &connection_time));
391 ASSERT_EQ(1, X509_cmp_time(nextupd, &connection_time));
392
393 // Check valid connection timestamp right after OCSP response was validated.
394 connection_time = valid_after_ocsp_update_time;
395 ASSERT_EQ(-1, X509_cmp_time(thisupd, &connection_time));
396 ASSERT_EQ(1, X509_cmp_time(nextupd, &connection_time));
397
398 // Check valid connection timestamp right before OCSP response expires.
399 connection_time = valid_before_ocsp_expire_time;
400 ASSERT_EQ(-1, X509_cmp_time(thisupd, &connection_time));
401 ASSERT_EQ(1, X509_cmp_time(nextupd, &connection_time));
402
403 // Check expired connection timestamp
404 connection_time = invalid_after_ocsp_expire_time;
405 ASSERT_EQ(-1, X509_cmp_time(thisupd, &connection_time));
406 ASSERT_EQ(-1, X509_cmp_time(nextupd, &connection_time));
407
408 // Check that |OCSP_check_validity| sees that |thisupd| is more than 100
409 // seconds in the past, and disallows it.
410 EXPECT_FALSE(OCSP_check_validity(thisupd, nextupd, 0, 100));
411 uint32_t err = ERR_get_error();
412 EXPECT_EQ(OCSP_R_STATUS_TOO_OLD, ERR_GET_REASON(err));
413
414 // We offset "nsec" in |OCSP_check_validity| with a negative timestamp
415 // equal to the current time. We offset this larger timestamp on purpose to
416 // check if |OCSP_check_validity| can properly reject improper timestamps.
417 // This will cause the function to fail in two places, once when checking
418 // if "(current_time + nsec) > thisupd [Status Not Yet Valid]", and a second
419 // time when checking if "nextupd > (current_time - nsec) [Status Expired]".
420 ERR_clear_error();
421 EXPECT_FALSE(OCSP_check_validity(thisupd, nextupd, -time(nullptr), -1));
422 err = ERR_get_error();
423 EXPECT_EQ(OCSP_R_STATUS_NOT_YET_VALID, ERR_GET_REASON(err));
424 err = ERR_get_error();
425 EXPECT_EQ(OCSP_R_STATUS_EXPIRED, ERR_GET_REASON(err));
426 ERR_clear_error();
427
428 // Check that "NEXTUPDATE_BEFORE_THISUPDATE" is properly detected. We have to
429 // use |valid_after_ocsp_update_time| instead of |nextupd| to avoid a
430 // ticking time-bomb test. |nextupd| will throw an additional
431 // "STATUS_NOT_YET_VALID" error until it is a valid timestamp.
432 bssl::UniquePtr<ASN1_GENERALIZEDTIME> after_thisupd(
433 ASN1_GENERALIZEDTIME_new());
434 ASN1_GENERALIZEDTIME_set(after_thisupd.get(), valid_after_ocsp_update_time);
435 EXPECT_FALSE(OCSP_check_validity(after_thisupd.get(), thisupd, 0, -1));
436 err = ERR_get_error();
437 EXPECT_EQ(OCSP_R_STATUS_EXPIRED, ERR_GET_REASON(err));
438 err = ERR_get_error();
439 EXPECT_EQ(OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE, ERR_GET_REASON(err));
440
441 // We set |nextupd| to NULL on purpose to avoid another ticking time-bomb
442 // expiration in our tests. We only check if |thisupd| is a valid timestamp
443 // in the past.
444 EXPECT_TRUE(OCSP_check_validity(thisupd, nullptr, 0, -1));
445 }
446
447 // Test valid OCSP response, but the data itself is untrusted.
448 TEST(OCSPTest, TestUntrustedDataOCSP) {
449 std::string data = GetTestData(
450 std::string("crypto/ocsp/test/aws/ocsp_response.der").c_str());
451 std::vector<uint8_t> ocsp_reponse_data(data.begin(), data.end());
452
453 // Mess up a byte right in the middle of the cert.
454 ocsp_reponse_data[800] = ocsp_reponse_data[800] + 1;
455
456 bssl::UniquePtr<OCSP_BASICRESP> basic_response;
457 bssl::UniquePtr<STACK_OF(X509)> server_cert_chain;
458 ExtractAndVerifyBasicOCSP(ocsp_reponse_data, "ca_cert", "rsa_cert",
459 OCSP_VERIFYSTATUS_ERROR, &basic_response,
460 &server_cert_chain);
461 }
462
463
464 // Test valid OCSP response hashed with sha256 along with check for correct time
465 // field parsing.
466 TEST(OCSPTest, TestGoodOCSP_SHA256) {
467 std::string data = GetTestData(
468 std::string("crypto/ocsp/test/aws/ocsp_response_sha256.der").c_str());
469 std::vector<uint8_t> ocsp_reponse_data(data.begin(), data.end());
470
471 bssl::UniquePtr<OCSP_BASICRESP> basic_response;
472 bssl::UniquePtr<STACK_OF(X509)> server_cert_chain;
473 ExtractAndVerifyBasicOCSP(ocsp_reponse_data, "ca_cert", "rsa_cert",
474 OCSP_VERIFYSTATUS_SUCCESS, &basic_response,
475 &server_cert_chain);
476
477 int status = 0;
478 ASN1_GENERALIZEDTIME *thisupd, *nextupd;
479 CheckOCSP_CERTSTATUS(&basic_response, &server_cert_chain, EVP_sha256(),
480 OCSP_RESPFINDSTATUS_SUCCESS, &status, &thisupd,
481 &nextupd);
482 ASSERT_EQ(V_OCSP_CERTSTATUS_GOOD, status);
483
484 // There is 1 |OCSP_SINGLERESP| in the |basic_response|.
485 EXPECT_EQ(OCSP_resp_count(basic_response.get()), 1);
486
487 // If OCSP response is verifiable and all good, an OCSP client should check
488 // time fields to see if the response is still valid.
489
490 // Check before OCSP was last updated connection timestamp.
491 time_t connection_time = invalid_before_ocsp_update_time_sha256;
492 ASSERT_EQ(1, X509_cmp_time(thisupd, &connection_time));
493 ASSERT_EQ(1, X509_cmp_time(nextupd, &connection_time));
494
495 // Check valid connection timestamp right after OCSP response was validated.
496 connection_time = valid_after_ocsp_update_time_sha256;
497 ASSERT_EQ(-1, X509_cmp_time(thisupd, &connection_time));
498 ASSERT_EQ(1, X509_cmp_time(nextupd, &connection_time));
499
500 // Check valid connection timestamp right before OCSP response expires.
501 connection_time = valid_before_ocsp_expire_time_sha256;
502 ASSERT_EQ(-1, X509_cmp_time(thisupd, &connection_time));
503 ASSERT_EQ(1, X509_cmp_time(nextupd, &connection_time));
504
505 // Check expired connection timestamp.
506 connection_time = invalid_after_ocsp_expire_time_sha256;
507 ASSERT_EQ(-1, X509_cmp_time(thisupd, &connection_time));
508 ASSERT_EQ(-1, X509_cmp_time(nextupd, &connection_time));
509 }
510
511 struct OCSPResponseVerifyFlagTestVector {
512 const char *ocsp_response;
513 unsigned long ocsp_verify_flag;
514 bool include_expired_signer_cert;
515 int expected_ocsp_verify_status;
516 };
517
518 static const OCSPResponseVerifyFlagTestVector
519 OCSPResponseVerifyFlagTestVectors[] = {
520 {"ocsp_response", OCSP_NOINTERN, false, OCSP_VERIFYSTATUS_ERROR},
521 {"ocsp_response", OCSP_NOCHAIN, false, OCSP_VERIFYSTATUS_SUCCESS},
522 {"ocsp_response_expired_signer", OCSP_NOVERIFY, false,
523 OCSP_VERIFYSTATUS_SUCCESS},
524 {"ocsp_response_wrong_signer", OCSP_NOVERIFY, false,
525 OCSP_VERIFYSTATUS_SUCCESS},
526 {"ocsp_response", OCSP_NOEXPLICIT, false, OCSP_VERIFYSTATUS_SUCCESS},
527 {"ocsp_response", OCSP_TRUSTOTHER, false, OCSP_VERIFYSTATUS_SUCCESS},
528 {"ocsp_response_expired_signer", OCSP_TRUSTOTHER, false,
529 OCSP_VERIFYSTATUS_ERROR},
530 // |OCSP_TRUSTOTHER| sets |OCSP_NOVERIFY| if the signer cert is included
531 // within the parameters.
532 {"ocsp_response_expired_signer", OCSP_TRUSTOTHER, true,
533 OCSP_VERIFYSTATUS_SUCCESS},
534 };
535
536 class OCSPVerifyFlagTest
537 : public testing::TestWithParam<OCSPResponseVerifyFlagTestVector> {};
538
539 INSTANTIATE_TEST_SUITE_P(All, OCSPVerifyFlagTest,
540 testing::ValuesIn(OCSPResponseVerifyFlagTestVectors));
541
542 TEST_P(OCSPVerifyFlagTest, OCSPVerifyFlagTest) {
543 const OCSPResponseVerifyFlagTestVector &t = GetParam();
544
545 std::string respData =
546 GetTestData(std::string("crypto/ocsp/test/aws/" +
547 std::string(t.ocsp_response) + ".der")
548 .c_str());
549 std::vector<uint8_t> ocsp_response_data(respData.begin(), respData.end());
550
551 bssl::UniquePtr<OCSP_RESPONSE> ocsp_response =
552 LoadOCSP_RESPONSE(ocsp_response_data);
553 ASSERT_TRUE(ocsp_response);
554
555 int ret = OCSP_response_status(ocsp_response.get());
556 ASSERT_EQ(OCSP_RESPONSE_STATUS_SUCCESSFUL, ret);
557
558 bssl::UniquePtr<OCSP_BASICRESP> basic_response =
559 bssl::UniquePtr<OCSP_BASICRESP>(
560 OCSP_response_get1_basic(ocsp_response.get()));
561 ASSERT_TRUE(basic_response);
562
563 // Set up trust store and certificate chain.
564 bssl::UniquePtr<X509> ca_cert(CertFromPEM(
565 GetTestData(std::string("crypto/ocsp/test/aws/ca_cert.pem").c_str())
566 .c_str()));
567 bssl::UniquePtr<X509> server_cert(CertFromPEM(
568 GetTestData(std::string("crypto/ocsp/test/aws/rsa_cert.pem").c_str())
569 .c_str()));
570
571 bssl::UniquePtr<X509_STORE> trust_store(X509_STORE_new());
572 X509_STORE_add_cert(trust_store.get(), ca_cert.get());
573 bssl::UniquePtr<STACK_OF(X509)> server_cert_chain(
574 CertsToStack({server_cert.get(), ca_cert.get()}));
575 ASSERT_TRUE(server_cert_chain);
576 if (t.include_expired_signer_cert) {
577 bssl::UniquePtr<X509> signing_cert(CertFromPEM(
578 GetTestData(
579 std::string("crypto/ocsp/test/aws/ocsp_expired_cert.pem").c_str())
580 .c_str()));
581 ASSERT_TRUE(sk_X509_push(server_cert_chain.get(), signing_cert.get()));
582 X509_up_ref(signing_cert.get());
583 }
584
585 // Does basic verification on OCSP response.
586 const int ocsp_verify_status =
587 OCSP_basic_verify(basic_response.get(), server_cert_chain.get(),
588 trust_store.get(), t.ocsp_verify_flag);
589 ASSERT_EQ(t.expected_ocsp_verify_status, ocsp_verify_status);
590 }
591
592 struct OCSPRequestVerifyFlagTestVector {
593 const char *ocsp_request;
594 unsigned long ocsp_verify_flag;
595 bool include_expired_signer_cert;
596 int expected_ocsp_verify_status;
597 };
598
599 static const OCSPRequestVerifyFlagTestVector
600 OCSPRequestVerifyFlagTestVectors[] = {
601 // Although signatures for OCSP requests are optional according to the
602 // RFC, OCSP requests with absent signatures can't be verified.
603 {"ocsp_request", 0, false, OCSP_VERIFYSTATUS_ERROR},
604 // ocsp_request_signed's certificate is embedded in the request.
605 {"ocsp_request_signed", 0, false, OCSP_VERIFYSTATUS_SUCCESS},
606 {"ocsp_request_signed", OCSP_NOINTERN, false, OCSP_VERIFYSTATUS_ERROR},
607 {"ocsp_request_signed", OCSP_NOCHAIN, false, OCSP_VERIFYSTATUS_SUCCESS},
608 {"ocsp_request_wrong_signer", 0, false, OCSP_VERIFYSTATUS_ERROR},
609 {"ocsp_request_wrong_signer", OCSP_NOCHAIN, false,
610 OCSP_VERIFYSTATUS_ERROR},
611 // OCSP request verification will fail if the cert can't be found or
612 // when working with an expired cert.
613 {"ocsp_request_expired_signer", 0, true, OCSP_VERIFYSTATUS_ERROR},
614 {"ocsp_request_expired_signer", 0, false, OCSP_VERIFYSTATUS_ERROR},
615 {"ocsp_request_expired_signer_no_certs", 0, true,
616 OCSP_VERIFYSTATUS_ERROR},
617 {"ocsp_request_expired_signer_no_certs", 0, false,
618 OCSP_VERIFYSTATUS_ERROR},
619 // |OCSP_NOVERIFY| skips certificate verification, but can't succeed if
620 // the signer cert isn't found.
621 {"ocsp_request_expired_signer", OCSP_NOVERIFY, true,
622 OCSP_VERIFYSTATUS_SUCCESS},
623 {"ocsp_request_expired_signer", OCSP_NOVERIFY, false,
624 OCSP_VERIFYSTATUS_SUCCESS},
625 {"ocsp_request_expired_signer_no_certs", OCSP_NOVERIFY, true,
626 OCSP_VERIFYSTATUS_SUCCESS},
627 {"ocsp_request_expired_signer_no_certs", OCSP_NOVERIFY, false,
628 OCSP_VERIFYSTATUS_ERROR},
629 // |OCSP_TRUSTOTHER| sets |OCSP_NOVERIFY| if the signer cert is included
630 // within the parameters.
631 {"ocsp_request_expired_signer", OCSP_TRUSTOTHER, true,
632 OCSP_VERIFYSTATUS_ERROR},
633 {"ocsp_request_expired_signer", OCSP_TRUSTOTHER, false,
634 OCSP_VERIFYSTATUS_ERROR},
635 {"ocsp_request_expired_signer_no_certs", OCSP_TRUSTOTHER, true,
636 OCSP_VERIFYSTATUS_SUCCESS},
637 {"ocsp_request_expired_signer_no_certs", OCSP_TRUSTOTHER, false,
638 OCSP_VERIFYSTATUS_ERROR},
639 };
640
641 class OCSPRequestVerifyFlagTest
642 : public testing::TestWithParam<OCSPRequestVerifyFlagTestVector> {};
643
644 INSTANTIATE_TEST_SUITE_P(All, OCSPRequestVerifyFlagTest,
645 testing::ValuesIn(OCSPRequestVerifyFlagTestVectors));
646
647 TEST_P(OCSPRequestVerifyFlagTest, OCSPVerifyFlagTest) {
648 const OCSPRequestVerifyFlagTestVector &t = GetParam();
649
650 std::string respData =
651 GetTestData(std::string("crypto/ocsp/test/aws/" +
652 std::string(t.ocsp_request) + ".der")
653 .c_str());
654 std::vector<uint8_t> ocsp_request_data(respData.begin(), respData.end());
655 bssl::UniquePtr<OCSP_REQUEST> ocsp_request =
656 LoadOCSP_REQUEST(ocsp_request_data);
657 ASSERT_TRUE(ocsp_request);
658
659 // Set up trust store and certificate chain.
660 bssl::UniquePtr<X509> ca_cert(CertFromPEM(
661 GetTestData(std::string("crypto/ocsp/test/aws/ca_cert.pem").c_str())
662 .c_str()));
663
664 bssl::UniquePtr<X509_STORE> trust_store(X509_STORE_new());
665 X509_STORE_add_cert(trust_store.get(), ca_cert.get());
666 bssl::UniquePtr<STACK_OF(X509)> server_cert_chain(sk_X509_new_null());
667 ASSERT_TRUE(server_cert_chain);
668 if (t.include_expired_signer_cert) {
669 bssl::UniquePtr<X509> signing_cert(CertFromPEM(
670 GetTestData(
671 std::string("crypto/ocsp/test/aws/ocsp_expired_cert.pem").c_str())
672 .c_str()));
673 ASSERT_TRUE(sk_X509_push(server_cert_chain.get(), signing_cert.get()));
674 X509_up_ref(signing_cert.get());
675 }
676
677 // Does basic verification on OCSP request.
678 const int ocsp_verify_status =
679 OCSP_request_verify(ocsp_request.get(), server_cert_chain.get(),
680 trust_store.get(), t.ocsp_verify_flag);
681 ASSERT_EQ(t.expected_ocsp_verify_status, ocsp_verify_status);
682 }
683
684 TEST(OCSPTest, GetInfo) {
685 // Create a sample |OCSP_CERTID| structure.
686 bssl::UniquePtr<OCSP_CERTID> cert_id(LoadTestOCSP_CERTID());
687 ASSERT_TRUE(cert_id);
688
689 ASN1_OCTET_STRING *nameHash = nullptr;
690 ASN1_OBJECT *algo = nullptr;
691 ASN1_OCTET_STRING *keyHash = nullptr;
692 ASN1_INTEGER *serial = nullptr;
693 EXPECT_TRUE(
694 OCSP_id_get0_info(&nameHash, &algo, &keyHash, &serial, cert_id.get()));
695 EXPECT_EQ(nameHash, cert_id.get()->issuerNameHash);
696 EXPECT_EQ(algo, cert_id.get()->hashAlgorithm->algorithm);
697 EXPECT_EQ(keyHash, cert_id.get()->issuerKeyHash);
698 EXPECT_EQ(serial, cert_id.get()->serialNumber);
699 }
700
701 TEST(OCSPTest, BasicAddCert) {
702 std::string respData = GetTestData(
703 std::string("crypto/ocsp/test/aws/ocsp_response.der").c_str());
704 std::vector<uint8_t> ocsp_response_data(respData.begin(), respData.end());
705 bssl::UniquePtr<OCSP_RESPONSE> ocspResponse =
706 LoadOCSP_RESPONSE(ocsp_response_data);
707 bssl::UniquePtr<OCSP_BASICRESP> basicResponse(
708 OCSP_response_get1_basic(ocspResponse.get()));
709 ASSERT_TRUE(basicResponse);
710
711 bssl::UniquePtr<X509> cert(CertFromPEM(
712 GetTestData(std::string("crypto/ocsp/test/aws/ca_cert.pem").c_str())
713 .c_str()));
714
715 EXPECT_TRUE(OCSP_basic_add1_cert(basicResponse.get(), cert.get()));
716 // Check that the cert is the same as the one added to the stack.
717 EXPECT_EQ(sk_X509_value(basicResponse.get()->certs,
718 sk_X509_num(basicResponse.get()->certs) - 1),
719 cert.get());
720 }
721
722 TEST(OCSPTest, BasicAddStatus) {
723 bssl::UniquePtr<OCSP_BASICRESP> basicResponse(OCSP_BASICRESP_new());
724 ASSERT_TRUE(basicResponse);
725 bssl::UniquePtr<OCSP_CERTID> certId(LoadTestOCSP_CERTID());
726 ASSERT_TRUE(certId);
727
728 bssl::UniquePtr<ASN1_TIME> revoked_time(ASN1_TIME_new()),
729 this_update(ASN1_TIME_new()), next_update(ASN1_TIME_new());
730 ASSERT_TRUE(
731 ASN1_TIME_set(revoked_time.get(), invalid_before_ocsp_update_time));
732 ASSERT_TRUE(ASN1_TIME_set(this_update.get(), valid_after_ocsp_update_time));
733 ASSERT_TRUE(ASN1_TIME_set(next_update.get(), valid_before_ocsp_expire_time));
734
735 EXPECT_TRUE(OCSP_basic_add1_status(basicResponse.get(), certId.get(),
736 V_OCSP_CERTSTATUS_GOOD, 0, nullptr,
737 this_update.get(), next_update.get()));
738
739 // |revoked_reason| and |revoked_time| are ignored if |status| is
740 // |V_OCSP_CERTSTATUS_GOOD|, but will still succeed.
741 EXPECT_TRUE(OCSP_basic_add1_status(
742 basicResponse.get(), certId.get(), V_OCSP_CERTSTATUS_GOOD,
743 OCSP_REVOKED_STATUS_CACOMPROMISE, revoked_time.get(), this_update.get(),
744 next_update.get()));
745
746 EXPECT_TRUE(OCSP_basic_add1_status(basicResponse.get(), certId.get(),
747 V_OCSP_CERTSTATUS_UNKNOWN, 0, nullptr,
748 this_update.get(), next_update.get()));
749
750 // |revoked_reason| and |revoked_time| are ignored if |status| is
751 // |V_OCSP_CERTSTATUS_UNKNOWN|, but will still succeed.
752 EXPECT_TRUE(OCSP_basic_add1_status(
753 basicResponse.get(), certId.get(), V_OCSP_CERTSTATUS_UNKNOWN,
754 OCSP_REVOKED_STATUS_SUPERSEDED, revoked_time.get(), this_update.get(),
755 next_update.get()));
756
757 // Try setting a revoked response without an |ASN1_TIME|.
758 EXPECT_FALSE(OCSP_basic_add1_status(basicResponse.get(), certId.get(),
759 V_OCSP_CERTSTATUS_REVOKED, 0, nullptr,
760 this_update.get(), nullptr));
761
762 // Try setting a revoked response with an invalid revoked reason number.
763 EXPECT_FALSE(OCSP_basic_add1_status(
764 basicResponse.get(), certId.get(), V_OCSP_CERTSTATUS_REVOKED,
765 OCSP_UNASSIGNED_REVOKED_STATUS, revoked_time.get(), this_update.get(),
766 nullptr));
767
768 EXPECT_TRUE(OCSP_basic_add1_status(
769 basicResponse.get(), certId.get(), V_OCSP_CERTSTATUS_REVOKED,
770 OCSP_REVOKED_STATUS_UNSPECIFIED, revoked_time.get(), this_update.get(),
771 nullptr));
772
773 // Try setting an invalid status to the response.
774 EXPECT_FALSE(OCSP_basic_add1_status(basicResponse.get(), certId.get(), 4, 0,
775 nullptr, this_update.get(), nullptr));
776
777 // |OCSP_basic_add1_status| has succeeded 5 times at this point, so the
778 // |basicResponse| should have 5 |OCSP_SINGLERESP|s in the internal stack.
779 EXPECT_EQ((int)sk_OCSP_SINGLERESP_num(
780 basicResponse.get()->tbsResponseData->responses),
781 5);
782 }
783
784 TEST(OCSPTest, OCSPResponseRecreate) {
785 std::string data = GetTestData(
786 std::string("crypto/ocsp/test/aws/ocsp_response.der").c_str());
787 std::vector<uint8_t> ocsp_response_data(data.begin(), data.end());
788 bssl::UniquePtr<OCSP_RESPONSE> ocsp_response(
789 LoadOCSP_RESPONSE(ocsp_response_data));
790 ASSERT_TRUE(ocsp_response);
791 bssl::UniquePtr<OCSP_BASICRESP> basic_response(
792 OCSP_response_get1_basic(ocsp_response.get()));
793 ASSERT_TRUE(basic_response);
794
795 // Recreate the same |OCSP_RESPONSE| with the same contents.
796 bssl::UniquePtr<OCSP_RESPONSE> ocsp_response_recreated(OCSP_response_create(
797 OCSP_response_status(ocsp_response.get()), basic_response.get()));
798 EXPECT_TRUE(ocsp_response_recreated);
799
800 // Write out the bytes from |ocsp_response_recreated| to compare with the
801 // original.
802 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
803 EXPECT_TRUE(i2d_OCSP_RESPONSE_bio(bio.get(), ocsp_response_recreated.get()));
804 const uint8_t *bio_data;
805 size_t bio_len;
806 ASSERT_TRUE(BIO_mem_contents(bio.get(), &bio_data, &bio_len));
807 EXPECT_EQ(Bytes(bio_data, bio_len),
808 Bytes(ocsp_response_data.data(), ocsp_response_data.size()));
809
810 // Disallow creation of an |OCSP_RESPONSE| with an invalid status number.
811 EXPECT_FALSE(OCSP_response_create(OCSP_UNASSIGNED_RESPONSE_STATUS,
812 basic_response.get()));
813 }
814
815 // === Translation of OpenSSL's OCSP tests ===
816
817 // https://github.com/openssl/openssl/blob/OpenSSL_1_1_1-stable/test/recipes/80-test_ocsp.t
818 struct OCSPTestVector {
819 const char *ocsp_response;
820 const char *cafile;
821 const char *untrusted;
822 int expected_ocsp_verify_status;
823 };
824
825 // Test vectors from OpenSSL OCSP's tests
826 static const OCSPTestVector kTestVectors[] = {
827 // === VALID OCSP RESPONSES ===
828 {"ND1", "ND1_Issuer_ICA", "", OCSP_VERIFYSTATUS_SUCCESS},
829 {"ND2", "ND2_Issuer_Root", "", OCSP_VERIFYSTATUS_SUCCESS},
830 {"ND3", "ND3_Issuer_Root", "", OCSP_VERIFYSTATUS_SUCCESS},
831 {"ND1", "ND1_Cross_Root", "ND1_Issuer_ICA-Cross",
832 OCSP_VERIFYSTATUS_SUCCESS},
833 {"D1", "D1_Issuer_ICA", "", OCSP_VERIFYSTATUS_SUCCESS},
834 {"D2", "D2_Issuer_Root", "", OCSP_VERIFYSTATUS_SUCCESS},
835 {"D3", "D3_Issuer_Root", "", OCSP_VERIFYSTATUS_SUCCESS},
836 // === INVALID SIGNATURE on the OCSP RESPONSE ===
837 {"ISOP_ND1", "ND1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
838 {"ISOP_ND2", "ND2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
839 {"ISOP_ND3", "ND3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
840 {"ISOP_D1", "D1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
841 {"ISOP_D2", "D2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
842 {"ISOP_D3", "D3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
843 // === WRONG RESPONDERID in the OCSP RESPONSE ===
844 {"WRID_ND1", "ND1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
845 {"WRID_ND2", "ND2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
846 {"WRID_ND3", "ND3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
847 {"WRID_D1", "D1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
848 {"WRID_D2", "D2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
849 {"WRID_D3", "D3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
850 // === WRONG ISSUERNAMEHASH in the OCSP RESPONSE ===
851 {"WINH_ND1", "ND1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
852 {"WINH_ND2", "ND2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
853 {"WINH_ND3", "ND3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
854 {"WINH_D1", "D1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
855 {"WINH_D2", "D2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
856 {"WINH_D3", "D3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
857 // === WRONG ISSUERKEYHASH in the OCSP RESPONSE ===
858 {"WIKH_ND1", "ND1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
859 {"WIKH_ND2", "ND2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
860 {"WIKH_ND3", "ND3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
861 {"WIKH_D1", "D1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
862 {"WIKH_D2", "D2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
863 {"WIKH_D3", "D3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
864 // === WRONG KEY in the DELEGATED OCSP SIGNING CERTIFICATE ===
865 {"WKDOSC_D1", "D1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
866 {"WKDOSC_D2", "D2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
867 {"WKDOSC_D3", "D3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
868 // === INVALID SIGNATURE on the DELEGATED OCSP SIGNING CERTIFICATE ===
869 {"ISDOSC_D1", "D1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
870 {"ISDOSC_D1", "D2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
871 {"ISDOSC_D1", "D3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
872 // === WRONG SUBJECT NAME in the ISSUER CERTIFICATE ===
873 {"ND1", "WSNIC_ND1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
874 {"ND2", "WSNIC_ND2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
875 {"ND3", "WSNIC_ND3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
876 {"D1", "WSNIC_D1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
877 {"D2", "WSNIC_D2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
878 {"D3", "WSNIC_D3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
879 // === WRONG KEY in the ISSUER CERTIFICATE ===
880 {"ND1", "WKIC_ND1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
881 {"ND2", "WKIC_ND2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
882 {"ND3", "WKIC_ND3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
883 {"D1", "WKIC_D1_Issuer_ICA", "", OCSP_VERIFYSTATUS_ERROR},
884 {"D2", "WKIC_D2_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
885 {"D3", "WKIC_D3_Issuer_Root", "", OCSP_VERIFYSTATUS_ERROR},
886 // === INVALID SIGNATURE on the ISSUER CERTIFICATE ===
887 // Expect success, because we're explicitly trusting the issuer certificate.
888 // https://datatracker.ietf.org/doc/html/rfc6960#section-2.6
889 {"ND1", "ISIC_ND1_Issuer_ICA", "", OCSP_VERIFYSTATUS_SUCCESS},
890 {"ND2", "ISIC_ND2_Issuer_Root", "", OCSP_VERIFYSTATUS_SUCCESS},
891 {"ND3", "ISIC_ND3_Issuer_Root", "", OCSP_VERIFYSTATUS_SUCCESS},
892 {"D1", "ISIC_D1_Issuer_ICA", "", OCSP_VERIFYSTATUS_SUCCESS},
893 {"D2", "ISIC_D2_Issuer_Root", "", OCSP_VERIFYSTATUS_SUCCESS},
894 {"D3", "ISIC_D3_Issuer_Root", "", OCSP_VERIFYSTATUS_SUCCESS},
895 };
896
897
898 class OCSPTest : public testing::TestWithParam<OCSPTestVector> {};
899
900 INSTANTIATE_TEST_SUITE_P(All, OCSPTest, testing::ValuesIn(kTestVectors));
901
902 TEST_P(OCSPTest, VerifyOCSPResponse) {
903 const OCSPTestVector &t = GetParam();
904
905 bssl::UniquePtr<OCSP_RESPONSE> ocsp_response;
906 bssl::UniquePtr<OCSP_BASICRESP> basic_response;
907
908 // Get OCSP response from path.
909 std::string data = GetTestData(
910 std::string("crypto/ocsp/test/" + std::string(t.ocsp_response) + ".ors")
911 .c_str());
912 std::vector<uint8_t> input;
913 ASSERT_TRUE(DecodeBase64(&input, data.c_str()));
914
915 ocsp_response = LoadOCSP_RESPONSE(input);
916 ASSERT_TRUE(ocsp_response);
917
918 int ret = OCSP_response_status(ocsp_response.get());
919 ASSERT_EQ(OCSP_RESPONSE_STATUS_SUCCESSFUL, ret);
920
921 basic_response = bssl::UniquePtr<OCSP_BASICRESP>(
922 OCSP_response_get1_basic(ocsp_response.get()));
923 ASSERT_TRUE(basic_response);
924
925 // Set up OpenSSL OCSP tests trust store parameters and cert chain.
926 bssl::UniquePtr<STACK_OF(X509)> server_cert_chain;
927 bssl::UniquePtr<X509_STORE> trust_store(X509_STORE_new());
928 bssl::UniquePtr<X509_VERIFY_PARAM> vpm(X509_VERIFY_PARAM_new());
929 X509_VERIFY_PARAM_set_time(vpm.get(), (time_t)1355875200);
930 // Discussion for whether this flag should be on by default or not:
931 // https://github.com/openssl/openssl/issues/7871
932 ASSERT_EQ(X509_VERIFY_PARAM_set_flags(vpm.get(), X509_V_FLAG_PARTIAL_CHAIN),
933 1);
934
935 // Get CA root certificate from path and set up trust store.
936 bssl::UniquePtr<X509> ca_certificate(
937 CertFromPEM(GetTestData(std::string("crypto/ocsp/test/" +
938 std::string(t.cafile) + ".pem")
939 .c_str())
940 .c_str()));
941 X509_STORE_add_cert(trust_store.get(), ca_certificate.get());
942 ASSERT_EQ(X509_STORE_set1_param(trust_store.get(), vpm.get()), 1);
943
944 // If untrusted cert chain isn't available, we only use CA cert as root
945 // cert.
946 if (std::string(t.untrusted).empty()) {
947 server_cert_chain = CertsToStack({ca_certificate.get()});
948 } else {
949 server_cert_chain = CertChainFromPEM(
950 GetTestData(
951 std::string("crypto/ocsp/test/" + std::string(t.untrusted) + ".pem")
952 .c_str())
953 .c_str());
954 }
955 ASSERT_TRUE(server_cert_chain);
956
957 // Does basic verification on OCSP response.
958 const int ocsp_verify_status = OCSP_basic_verify(
959 basic_response.get(), server_cert_chain.get(), trust_store.get(), 0);
960 ASSERT_EQ(t.expected_ocsp_verify_status, ocsp_verify_status);
961 }
962
963 struct OCSPRequestTestVector {
964 const char *ocsp_request;
965 int expected_parse_status;
966 int expected_sign_status;
967 const char *signer_cert;
968 const char *key_type;
969 const EVP_MD *dgst;
970 };
971
972 static const OCSPRequestTestVector kRequestTestVectors[] = {
973 {"ocsp_request", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_SUCCESS, "rsa_cert",
974 "rsa_key", EVP_sha1()},
975 {"ocsp_request", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_SUCCESS, "rsa_cert",
976 "rsa_key", EVP_sha256()},
977 {"ocsp_request", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_SUCCESS, "rsa_cert",
978 "rsa_key", nullptr},
979 {"ocsp_request_attached_cert", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_ERROR,
980 "rsa_cert", "rsa_key", nullptr},
981 {"ocsp_request_no_nonce", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_SUCCESS,
982 "rsa_cert", "rsa_key", EVP_sha512()},
983 {"ocsp_request_signed", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_ERROR,
984 "rsa_cert", "rsa_key", nullptr},
985 {"ocsp_request_signed_sha256", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_ERROR,
986 "rsa_cert", "rsa_key", nullptr},
987 {"ocsp_response", OCSP_REQUEST_PARSE_ERROR, OCSP_SIGN_ERROR, "rsa_cert",
988 "rsa_key", nullptr},
989 // Test signing with ECDSA certs and keys.
990 {"ocsp_request", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_SUCCESS,
991 "ecdsa_cert", "ecdsa_key", EVP_sha1()},
992 {"ocsp_request", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_SUCCESS,
993 "ecdsa_cert", "ecdsa_key", EVP_sha256()},
994 {"ocsp_request", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_SUCCESS,
995 "ecdsa_cert", "ecdsa_key", nullptr},
996 {"ocsp_request_no_nonce", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_SUCCESS,
997 "rsa_cert", "rsa_key", EVP_sha256()},
998 {"ocsp_request_no_nonce", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_SUCCESS,
999 "ecdsa_cert", "ecdsa_key", EVP_sha256()},
1000 // Test certificate type mismatch.
1001 {"ocsp_request", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_ERROR, "rsa_cert",
1002 "ecdsa_key", EVP_sha256()},
1003 {"ocsp_request", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_ERROR, "ecdsa_cert",
1004 "rsa_key", EVP_sha256()},
1005 // Test certificate key and cert mismatch.
1006 {"ocsp_request", OCSP_REQUEST_PARSE_SUCCESS, OCSP_SIGN_ERROR, "ca_cert",
1007 "rsa_key", EVP_sha256()},
1008 };
1009
1010 class OCSPRequestTest : public testing::TestWithParam<OCSPRequestTestVector> {};
1011
1012 INSTANTIATE_TEST_SUITE_P(All, OCSPRequestTest,
1013 testing::ValuesIn(kRequestTestVectors));
1014
1015 static const char good_http_request_hdr[] =
1016 "POST / HTTP/1.0\r\n"
1017 "Content-Type: application/ocsp-request\r\n"
1018 "Content-Length: ";
1019
1020 TEST_P(OCSPRequestTest, OCSPRequestParse) {
1021 const OCSPRequestTestVector &t = GetParam();
1022
1023 std::string data =
1024 GetTestData(std::string("crypto/ocsp/test/aws/" +
1025 std::string(t.ocsp_request) + ".der")
1026 .c_str());
1027 std::vector<uint8_t> ocsp_request_data(data.begin(), data.end());
1028
1029 bssl::UniquePtr<OCSP_REQUEST> ocspRequest =
1030 LoadOCSP_REQUEST(ocsp_request_data);
1031
1032 if (t.expected_parse_status == OCSP_REQUEST_PARSE_SUCCESS) {
1033 ASSERT_TRUE(ocspRequest);
1034
1035 // If request parsing is successful, try setting up a |OCSP_REQ_CTX| with
1036 // default settings.
1037 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1038 const uint8_t *out;
1039 size_t outlen;
1040 bssl::UniquePtr<OCSP_REQ_CTX> ocspReqCtx(
1041 OCSP_sendreq_new(bio.get(), nullptr, ocspRequest.get(), 0));
1042 ASSERT_TRUE(ocspReqCtx);
1043 // Get internal memory of |OCSP_REQ_CTX| and ensure contents are written.
1044 ASSERT_TRUE(BIO_mem_contents(OCSP_REQ_CTX_get0_mem_bio(ocspReqCtx.get()),
1045 &out, &outlen));
1046 ASSERT_GT(outlen, (size_t)0);
1047
1048 // Set up |OCSP_REQ_CTX| without a |OCSP_REQUEST|. Then finalize the context
1049 // later.
1050 bssl::UniquePtr<BIO> bio2(BIO_new(BIO_s_mem()));
1051 const uint8_t *out2;
1052 size_t outlen2;
1053 bssl::UniquePtr<OCSP_REQ_CTX> ocspReqCtxLater(
1054 OCSP_sendreq_new(bio2.get(), nullptr, nullptr, 0));
1055 ASSERT_TRUE(ocspReqCtxLater);
1056 ASSERT_TRUE(
1057 OCSP_REQ_CTX_set1_req(ocspReqCtxLater.get(), ocspRequest.get()));
1058 ASSERT_TRUE(BIO_mem_contents(
1059 OCSP_REQ_CTX_get0_mem_bio(ocspReqCtxLater.get()), &out2, &outlen2));
1060
1061 // Ensure header contents are written as expected.
1062 EXPECT_EQ(
1063 good_http_request_hdr + std::to_string(ocsp_request_data.size()) + "\r",
1064 std::string(reinterpret_cast<const char *>(out),
1065 sizeof(good_http_request_hdr) +
1066 std::to_string(ocsp_request_data.size()).size()));
1067 // Check |OCSP_REQ_CTX| construction methods write the exact same contents.
1068 ASSERT_EQ(Bytes(out, outlen), Bytes(out2, outlen2));
1069 } else {
1070 ASSERT_FALSE(ocspRequest);
1071 }
1072 }
1073
1074 TEST_P(OCSPRequestTest, OCSPRequestSign) {
1075 const OCSPRequestTestVector &t = GetParam();
1076
1077 std::string data =
1078 GetTestData(std::string("crypto/ocsp/test/aws/" +
1079 std::string(t.ocsp_request) + ".der")
1080 .c_str());
1081 std::vector<uint8_t> ocsp_request_data(data.begin(), data.end());
1082 bssl::UniquePtr<OCSP_REQUEST> ocspRequest =
1083 LoadOCSP_REQUEST(ocsp_request_data);
1084
1085 bssl::UniquePtr<X509> signer_cert(
1086 CertFromPEM(GetTestData(std::string("crypto/ocsp/test/aws/" +
1087 std::string(t.signer_cert) + ".pem")
1088 .c_str())
1089 .c_str()));
1090 ASSERT_TRUE(signer_cert);
1091 bssl::UniquePtr<X509> ca_cert(CertFromPEM(
1092 GetTestData(std::string("crypto/ocsp/test/aws/ca_cert.pem").c_str())
1093 .c_str()));
1094 ASSERT_TRUE(ca_cert);
1095
1096 if (t.expected_parse_status == OCSP_REQUEST_PARSE_SUCCESS) {
1097 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
1098 if (std::string(t.key_type) == "rsa_key") {
1099 bssl::UniquePtr<RSA> rsa(
1100 RSAFromPEM(GetTestData(std::string("crypto/ocsp/test/aws/" +
1101 std::string(t.key_type) + ".pem")
1102 .c_str())
1103 .c_str()));
1104 ASSERT_TRUE(rsa);
1105 ASSERT_TRUE(EVP_PKEY_set1_RSA(pkey.get(), rsa.get()));
1106 } else {
1107 bssl::UniquePtr<EC_KEY> ecdsa(
1108 ECDSAFromPEM(GetTestData(std::string("crypto/ocsp/test/aws/" +
1109 std::string(t.key_type) + ".pem")
1110 .c_str())
1111 .c_str()));
1112 ASSERT_TRUE(ecdsa);
1113 ASSERT_TRUE(EVP_PKEY_set1_EC_KEY(pkey.get(), ecdsa.get()));
1114 }
1115
1116 int ret =
1117 OCSP_request_sign(ocspRequest.get(), signer_cert.get(), pkey.get(),
1118 t.dgst, CertsToStack({ca_cert.get()}).get(), 0);
1119 if (t.expected_sign_status == OCSP_SIGN_SUCCESS) {
1120 ASSERT_TRUE(ret);
1121 EXPECT_TRUE(OCSP_request_is_signed(ocspRequest.get()));
1122 bssl::UniquePtr<X509_STORE> trust_store(X509_STORE_new());
1123 ASSERT_TRUE(X509_STORE_add_cert(trust_store.get(), ca_cert.get()));
1124 EXPECT_TRUE(OCSP_request_verify(ocspRequest.get(),
1125 CertsToStack({signer_cert.get()}).get(),
1126 trust_store.get(), 0));
1127 } else {
1128 ASSERT_FALSE(ret);
1129 EXPECT_FALSE(OCSP_request_is_signed(ocspRequest.get()));
1130 }
1131 }
1132 }
1133
1134 struct OCSPResponseSignTestVector {
1135 int expected_sign_status;
1136 const char *signer_cert;
1137 const char *key_type;
1138 const EVP_MD *dgst;
1139 };
1140
1141 static const OCSPResponseSignTestVector kOCSPResponseSignTestVectors[] = {
1142 {OCSP_SIGN_SUCCESS, "rsa_cert", "rsa_key", EVP_sha1()},
1143 {OCSP_SIGN_SUCCESS, "rsa_cert", "rsa_key", EVP_sha256()},
1144 {OCSP_SIGN_SUCCESS, "rsa_cert", "rsa_key", EVP_sha512()},
1145 {OCSP_SIGN_SUCCESS, "rsa_cert", "rsa_key", nullptr},
1146 // Test signing with ECDSA certs and keys.
1147 {OCSP_SIGN_SUCCESS, "ecdsa_cert", "ecdsa_key", EVP_sha1()},
1148 {OCSP_SIGN_SUCCESS, "ecdsa_cert", "ecdsa_key", EVP_sha256()},
1149 {OCSP_SIGN_SUCCESS, "ecdsa_cert", "ecdsa_key", EVP_sha512()},
1150 {OCSP_SIGN_SUCCESS, "ecdsa_cert", "ecdsa_key", nullptr},
1151 // Test certificate type mismatch.
1152 {OCSP_SIGN_ERROR, "rsa_cert", "ecdsa_key", EVP_sha256()},
1153 {OCSP_SIGN_ERROR, "ecdsa_cert", "rsa_key", EVP_sha256()},
1154 // Test certificate key and cert mismatch.
1155 {OCSP_SIGN_ERROR, "ca_cert", "rsa_key", EVP_sha256()},
1156 };
1157
1158 class OCSPResponseSignTest
1159 : public testing::TestWithParam<OCSPResponseSignTestVector> {};
1160
1161 INSTANTIATE_TEST_SUITE_P(All, OCSPResponseSignTest,
1162 testing::ValuesIn(kOCSPResponseSignTestVectors));
1163
1164 TEST_P(OCSPResponseSignTest, OCSPResponseSign) {
1165 const OCSPResponseSignTestVector &t = GetParam();
1166
1167 // Set up an empty |OCSP_BASICRESP| for signing.
1168 bssl::UniquePtr<OCSP_BASICRESP> basic_response(OCSP_BASICRESP_new());
1169 ASSERT_TRUE(basic_response);
1170
1171 // Gather certs and key needed for signing.
1172 bssl::UniquePtr<X509> signer_cert(
1173 CertFromPEM(GetTestData(std::string("crypto/ocsp/test/aws/" +
1174 std::string(t.signer_cert) + ".pem")
1175 .c_str())
1176 .c_str()));
1177 ASSERT_TRUE(signer_cert);
1178 bssl::UniquePtr<X509> ca_cert(CertFromPEM(
1179 GetTestData(std::string("crypto/ocsp/test/aws/ca_cert.pem").c_str())
1180 .c_str()));
1181 ASSERT_TRUE(ca_cert);
1182 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
1183 if (std::string(t.key_type) == "rsa_key") {
1184 bssl::UniquePtr<RSA> rsa(
1185 RSAFromPEM(GetTestData(std::string("crypto/ocsp/test/aws/" +
1186 std::string(t.key_type) + ".pem")
1187 .c_str())
1188 .c_str()));
1189 ASSERT_TRUE(rsa);
1190 ASSERT_TRUE(EVP_PKEY_set1_RSA(pkey.get(), rsa.get()));
1191 } else {
1192 bssl::UniquePtr<EC_KEY> ecdsa(
1193 ECDSAFromPEM(GetTestData(std::string("crypto/ocsp/test/aws/" +
1194 std::string(t.key_type) + ".pem")
1195 .c_str())
1196 .c_str()));
1197 ASSERT_TRUE(ecdsa);
1198 ASSERT_TRUE(EVP_PKEY_set1_EC_KEY(pkey.get(), ecdsa.get()));
1199 }
1200
1201 // Do the actual sign.
1202 EXPECT_EQ(OCSP_basic_sign(basic_response.get(), signer_cert.get(), pkey.get(),
1203 t.dgst, CertsToStack({ca_cert.get()}).get(), 0),
1204 t.expected_sign_status);
1205 if (t.expected_sign_status == OCSP_SIGN_SUCCESS) {
1206 bssl::UniquePtr<X509_STORE> trust_store(X509_STORE_new());
1207 ASSERT_TRUE(X509_STORE_add_cert(trust_store.get(), ca_cert.get()));
1208 EXPECT_TRUE(OCSP_basic_verify(basic_response.get(),
1209 CertsToStack({signer_cert.get()}).get(),
1210 trust_store.get(), 0));
1211 }
1212 }
1213
1214 // Test against various flags for |OCSP_basic_sign|.
1215 TEST(OCSPResponseSignTestExtended, OCSPResponseSign) {
1216 bssl::UniquePtr<OCSP_BASICRESP> basic_response(OCSP_BASICRESP_new());
1217 ASSERT_TRUE(basic_response);
1218
1219 bssl::UniquePtr<X509> signer_cert(CertFromPEM(
1220 GetTestData(std::string("crypto/ocsp/test/aws/rsa_cert.pem").c_str())
1221 .c_str()));
1222 ASSERT_TRUE(signer_cert);
1223 bssl::UniquePtr<X509> ca_cert(CertFromPEM(
1224 GetTestData(std::string("crypto/ocsp/test/aws/ca_cert.pem").c_str())
1225 .c_str()));
1226 ASSERT_TRUE(ca_cert);
1227 bssl::UniquePtr<STACK_OF(X509)> additional_cert =
1228 CertsToStack({ca_cert.get()});
1229
1230 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
1231 bssl::UniquePtr<RSA> rsa(RSAFromPEM(
1232 GetTestData(std::string("crypto/ocsp/test/aws/rsa_key.pem").c_str())
1233 .c_str()));
1234 ASSERT_TRUE(rsa);
1235 ASSERT_TRUE(EVP_PKEY_set1_RSA(pkey.get(), rsa.get()));
1236
1237 // Call |OCSP_basic_sign| with no flags and check the expected output.
1238 EXPECT_TRUE(OCSP_basic_sign(basic_response.get(), signer_cert.get(),
1239 pkey.get(), EVP_sha256(), additional_cert.get(),
1240 0));
1241 EXPECT_TRUE(
1242 ASN1_TIME_check(basic_response.get()->tbsResponseData->producedAt));
1243 // Allow for time field to be within two hours.
1244 EXPECT_GT(
1245 X509_cmp_time_posix(basic_response.get()->tbsResponseData->producedAt,
1246 time(nullptr) - 3600),
1247 0);
1248 EXPECT_LT(
1249 X509_cmp_time_posix(basic_response.get()->tbsResponseData->producedAt,
1250 time(nullptr) + 3600),
1251 0);
1252
1253 EXPECT_EQ(basic_response.get()->tbsResponseData->responderId->type,
1254 V_OCSP_RESPID_NAME);
1255 EXPECT_EQ(
1256 X509_NAME_cmp(
1257 basic_response.get()->tbsResponseData->responderId->value.byName,
1258 X509_get_subject_name(signer_cert.get())),
1259 0);
1260 EXPECT_EQ((int)sk_X509_num(basic_response.get()->certs), 2);
1261 EXPECT_EQ(X509_cmp(sk_X509_value(basic_response.get()->certs, 0),
1262 signer_cert.get()),
1263 0);
1264 EXPECT_EQ(X509_cmp(sk_X509_value(basic_response.get()->certs, 1),
1265 sk_X509_value(additional_cert.get(), 0)),
1266 0);
1267
1268 // Check expected effects of |OCSP_NOTIME|.
1269 basic_response.reset(OCSP_BASICRESP_new());
1270 ASSERT_TRUE(basic_response);
1271 EXPECT_TRUE(OCSP_basic_sign(basic_response.get(), signer_cert.get(),
1272 pkey.get(), EVP_sha256(), additional_cert.get(),
1273 OCSP_NOTIME));
1274 EXPECT_FALSE(
1275 ASN1_TIME_check(basic_response.get()->tbsResponseData->producedAt));
1276
1277 // Check expected effects of |OCSP_RESPID_KEY|.
1278 basic_response.reset(OCSP_BASICRESP_new());
1279 ASSERT_TRUE(basic_response);
1280 ASSERT_FALSE(basic_response.get()->tbsResponseData->responderId->value.byKey);
1281 EXPECT_TRUE(OCSP_basic_sign(basic_response.get(), signer_cert.get(),
1282 pkey.get(), EVP_sha256(), additional_cert.get(),
1283 OCSP_RESPID_KEY));
1284 EXPECT_EQ(basic_response.get()->tbsResponseData->responderId->type,
1285 V_OCSP_RESPID_KEY);
1286 EXPECT_TRUE(basic_response.get()->tbsResponseData->responderId->value.byKey);
1287
1288
1289 // Check expected effects of |OCSP_NOCERTS|.
1290 basic_response.reset(OCSP_BASICRESP_new());
1291 ASSERT_TRUE(basic_response);
1292 EXPECT_TRUE(OCSP_basic_sign(basic_response.get(), signer_cert.get(),
1293 pkey.get(), EVP_sha256(), additional_cert.get(),
1294 OCSP_NOCERTS));
1295 EXPECT_EQ((int)sk_X509_num(basic_response.get()->certs), 0);
1296 }
1297
1298 static const char extended_good_http_request_hdr[] =
1299 "POST / HTTP/1.0\r\n"
1300 "Accept-Charset: character-set\r\n"
1301 "Content-Type: application/ocsp-request\r\n"
1302 "Content-Length: ";
1303
1304 // Check HTTP header can be written with OCSP_REQ_CTX_add1_header().
1305 TEST(OCSPRequestTest, AddHeader) {
1306 std::string data = GetTestData(std::string("crypto/ocsp/test/aws/"
1307 "ocsp_request.der")
1308 .c_str());
1309 std::vector<uint8_t> ocsp_request_data(data.begin(), data.end());
1310 bssl::UniquePtr<OCSP_REQUEST> ocspRequest =
1311 LoadOCSP_REQUEST(ocsp_request_data);
1312
1313 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1314 const uint8_t *out;
1315 size_t outlen;
1316 bssl::UniquePtr<OCSP_REQ_CTX> ocspReqCtx(
1317 OCSP_sendreq_new(bio.get(), nullptr, nullptr, 0));
1318 ASSERT_TRUE(ocspReqCtx);
1319 // Set an additional HTTP header.
1320 ASSERT_TRUE(OCSP_REQ_CTX_add1_header(ocspReqCtx.get(), "Accept-Charset",
1321 "character-set"));
1322 ASSERT_TRUE(OCSP_REQ_CTX_set1_req(ocspReqCtx.get(), ocspRequest.get()));
1323
1324 ASSERT_TRUE(BIO_mem_contents(OCSP_REQ_CTX_get0_mem_bio(ocspReqCtx.get()),
1325 &out, &outlen));
1326
1327 // Ensure additional header contents are written as expected.
1328 EXPECT_EQ(
1329 extended_good_http_request_hdr +
1330 std::to_string(ocsp_request_data.size()) + "\r\n",
1331 std::string(reinterpret_cast<const char *>(out),
1332 sizeof(extended_good_http_request_hdr) +
1333 std::to_string(ocsp_request_data.size()).size() + 1));
1334 }
1335
1336 // Check a |OCSP_CERTID| can be added to an |OCSP_REQUEST| with
1337 // OCSP_request_add0_id().
1338 TEST(OCSPRequestTest, AddCert) {
1339 std::string data = GetTestData(std::string("crypto/ocsp/test/aws/"
1340 "ocsp_request.der")
1341 .c_str());
1342 std::vector<uint8_t> ocsp_request_data(data.begin(), data.end());
1343 bssl::UniquePtr<OCSP_REQUEST> ocspRequest =
1344 LoadOCSP_REQUEST(ocsp_request_data);
1345 ASSERT_TRUE(ocspRequest);
1346
1347 // Construct |OCSP_CERTID| from certs.
1348 OCSP_CERTID *certId = LoadTestOCSP_CERTID();
1349 ASSERT_TRUE(certId);
1350
1351 OCSP_ONEREQ *oneRequest = OCSP_request_add0_id(ocspRequest.get(), certId);
1352 ASSERT_TRUE(oneRequest);
1353 // OCSP_request_add0_id() allocates a new |OCSP_ONEREQ| and assigns the
1354 // pointer to |OCSP_CERTID| to it, then adds it to the stack within
1355 // |OCSP_REQUEST|.
1356 // |OCSP_REQUEST| now takes ownership of the pointer to |OCSP_CERTID| and
1357 // still maintains ownership of the pointer |OCSP_ONEREQ|, so we have to set
1358 // the references to NULL to avoid freeing the same pointers twice.
1359 oneRequest = nullptr;
1360 certId = nullptr;
1361 }
1362
1363 static const char good_http_response_hdr[] =
1364 "HTTP/1.1 200 OK\r\n"
1365 "Content-Type: application/ocsp-response\r\n"
1366 "Content-Length: ";
1367
1368 // Test that |OCSP_set_max_response_length| correctly sets the maximum response
1369 // length.
1370 TEST(OCSPRequestTest, SetResponseLength) {
1371 std::string data = GetTestData(std::string("crypto/ocsp/test/aws/"
1372 "ocsp_request.der")
1373 .c_str());
1374 std::vector<uint8_t> ocsp_request_data(data.begin(), data.end());
1375 bssl::UniquePtr<OCSP_REQUEST> ocspRequest =
1376 LoadOCSP_REQUEST(ocsp_request_data);
1377 ASSERT_TRUE(ocspRequest);
1378
1379 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1380 bssl::UniquePtr<OCSP_REQ_CTX> ocspReqCtx(
1381 OCSP_sendreq_new(bio.get(), nullptr, nullptr, 0));
1382 ASSERT_TRUE(ocspReqCtx);
1383
1384 // Check custom length is set correctly.
1385 int new_len = 40000;
1386 OCSP_set_max_response_length(ocspReqCtx.get(), new_len);
1387 EXPECT_EQ((int)ocspReqCtx.get()->max_resp_len, new_len);
1388
1389 // Check that default length is set correctly.
1390 OCSP_set_max_response_length(ocspReqCtx.get(), 0);
1391 EXPECT_EQ((int)ocspReqCtx.get()->max_resp_len, OCSP_MAX_RESP_LENGTH);
1392
1393 // Write an HTTP OCSP response into the BIO.
1394 std::string respData = GetTestData(
1395 std::string("crypto/ocsp/test/aws/ocsp_response.der").c_str());
1396 std::vector<uint8_t> ocsp_response_data(respData.begin(), respData.end());
1397 const int respLen = (int)ocsp_response_data.size();
1398 ASSERT_GT(BIO_printf(bio.get(), "%s", good_http_response_hdr), 0);
1399 ASSERT_GT(BIO_printf(bio.get(), "%d\r\n\r\n", respLen), 0);
1400 ASSERT_EQ(BIO_write(bio.get(), ocsp_response_data.data(), respLen), respLen);
1401
1402 // Set max response length to a length that is too short on purpose.
1403 OCSP_set_max_response_length(ocspReqCtx.get(), 1);
1404
1405 // Sends out an OCSP request and expects an OCSP response in |BIO| with
1406 // |OCSP_REQ_CTX_nbio|. This should fail if the expected length is too short.
1407 EXPECT_FALSE(OCSP_REQ_CTX_nbio(ocspReqCtx.get()));
1408 }
1409
1410 static const char malformed_http_response_hdr[] =
1411 "HTTP/1.200 OK\r\n"
1412 "Content-Type: application/ocsp-response\r\n"
1413 "Content-Length: ";
1414
1415 // This parses in OpenSSL, but fails in AWS-LC because we check for the HTTP
1416 // protocol characters at the front.
1417 static const char non_http_response_hdr[] =
1418 "HTPT/1.1 200 OK\r\n"
1419 "Content-Type: application/ocsp-response\r\n"
1420 "Content-Length: ";
1421
1422 // Only status code 200 should be accepted.
1423 static const char not_ok_http_response_hdr[] =
1424 "HTTP/1.1 404 OK\r\n"
1425 "Content-Type: application/ocsp-response\r\n"
1426 "Content-Length: ";
1427
1428 // This should parse. Only the status code is mandatory, subsequent lines are
1429 // optional.
1430 static const char no_type_http_response_hdr[] =
1431 "HTTP/1.1 200 OK\r\n"
1432 "Content-Length: ";
1433
1434 // This should parse. Only the status code is mandatory, additional information
1435 // is optional.
1436 static const char no_info_http_response_hdr[] =
1437 "HTTP/1.0 200\r\n"
1438 "Content-Length: ";
1439
1440 // OpenSSL uses isspace() to test for white-space characters, which includes
1441 // the following. ``\t''``\n''``\v''``\f''``\r''`` '
1442 // This should parse. "\n" is not included since it will skip the header to the
1443 // next line and fail the http parsing.
1444 static const char additional_space_http_response_hdr[] =
1445 "HTTP/1.1 \t\v\r\f\t 200 \t\v\f\r\t OK \r\n"
1446 "Content-Type: application/ocsp-response\r\n"
1447 "Content-Length: ";
1448
1449 // This should fail, since the status code is expected on the first line.
1450 static const char next_line_http_response_hdr[] =
1451 "HTTP/1.1 \n 200 \f OK\r\n"
1452 "Content-Type: application/ocsp-response\r\n"
1453 "Content-Length: ";
1454
1455 // This should fail. Protocol info is expected at the front.
1456 static const char only_code_http_response_hdr[] =
1457 "200\r\n"
1458 "Content-Length: ";
1459
1460 // This should fail. We're appending a negative content length.
1461 static const char negative_length_http_response_hdr[] =
1462 "HTTP/1.0 200\r\n"
1463 "Content-Length: -10";
1464
1465 // This should fail. We're appending a non-numeric content length.
1466 static const char nonnumeric_length_http_response_hdr[] =
1467 "HTTP/1.0 200\r\n"
1468 "Content-Length: abcd";
1469
1470 // This should fail. We're appending a non-numeric content length.
1471 static const char format_string_http_response_hdr[] =
1472 "HTTP/1.0 200\r\n"
1473 "Content-Length: %s%s%s%s%s";
1474
1475 // This should fail. We're appending a value that will be beyond the default max
1476 // response length.
1477 static const char max_length_http_response_hdr[] =
1478 "HTTP/1.0 200\r\n"
1479 "Content-Length: 999999";
1480
1481 struct OCSPHTTPTestVector {
1482 const char *http_header;
1483 bool response_attached;
1484 int expected_status;
1485 };
1486
1487 static const OCSPHTTPTestVector kResponseHTTPVectors[] = {
1488 {good_http_response_hdr, true, OCSP_HTTP_PARSE_SUCCESS},
1489 {malformed_http_response_hdr, true, OCSP_HTTP_PARSE_ERROR},
1490 {non_http_response_hdr, true, OCSP_HTTP_PARSE_ERROR},
1491 {not_ok_http_response_hdr, true, OCSP_HTTP_PARSE_ERROR},
1492 {no_type_http_response_hdr, true, OCSP_REQUEST_PARSE_SUCCESS},
1493 {no_info_http_response_hdr, true, OCSP_REQUEST_PARSE_SUCCESS},
1494 {additional_space_http_response_hdr, true, OCSP_REQUEST_PARSE_SUCCESS},
1495 {next_line_http_response_hdr, true, OCSP_HTTP_PARSE_ERROR},
1496 {only_code_http_response_hdr, true, OCSP_HTTP_PARSE_ERROR},
1497 // HTTP Header is OK, but no actual response is attached.
1498 {good_http_response_hdr, false, OCSP_HTTP_PARSE_ERROR},
1499 {no_type_http_response_hdr, false, OCSP_HTTP_PARSE_ERROR},
1500 {no_info_http_response_hdr, false, OCSP_HTTP_PARSE_ERROR},
1501 {negative_length_http_response_hdr, false, OCSP_HTTP_PARSE_ERROR},
1502 {nonnumeric_length_http_response_hdr, false, OCSP_HTTP_PARSE_ERROR},
1503 {format_string_http_response_hdr, false, OCSP_HTTP_PARSE_ERROR},
1504 {max_length_http_response_hdr, false, OCSP_HTTP_PARSE_ERROR}};
1505
1506 class OCSPHTTPTest : public testing::TestWithParam<OCSPHTTPTestVector> {};
1507
1508 INSTANTIATE_TEST_SUITE_P(All, OCSPHTTPTest,
1509 testing::ValuesIn(kResponseHTTPVectors));
1510
1511 // Test if OCSP_sendreq_bio() can properly send out an HTTP request with
1512 // a |OCSP_REQUEST|, and expect an HTTP response with an OCSP response back.
1513 // Always sends the same OCSP request and replies with the same OCSP response.
1514 // This test focuses parsing the OCSP response with HTTP. We have other tests
1515 // to test if the actual OCSP response is parsable and verifiable.
1516 TEST_P(OCSPHTTPTest, OCSPRequestHTTP) {
1517 const OCSPHTTPTestVector &t = GetParam();
1518
1519 std::string data =
1520 GetTestData(std::string("crypto/ocsp/test/aws/ocsp_request.der").c_str());
1521 std::vector<uint8_t> ocsp_request_data(data.begin(), data.end());
1522 std::string respData = GetTestData(
1523 std::string("crypto/ocsp/test/aws/ocsp_response.der").c_str());
1524 std::vector<uint8_t> ocsp_response_data(respData.begin(), respData.end());
1525
1526 // Write an HTTP OCSP response into the BIO.
1527 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1528 const int respLen = (int)ocsp_response_data.size();
1529 ASSERT_GT(BIO_printf(bio.get(), "%s", t.http_header), 0);
1530 ASSERT_GT(BIO_printf(bio.get(), "%d\r\n\r\n", respLen), 0);
1531 if (t.response_attached) {
1532 ASSERT_EQ(BIO_write(bio.get(), ocsp_response_data.data(), respLen),
1533 respLen);
1534 }
1535
1536 // Sends out an OCSP request and expects an OCSP response in |BIO|.
1537 bssl::UniquePtr<OCSP_REQUEST> ocspRequest =
1538 LoadOCSP_REQUEST(ocsp_request_data);
1539 ASSERT_TRUE(ocspRequest);
1540 bssl::UniquePtr<OCSP_RESPONSE> ocspResponse(
1541 OCSP_sendreq_bio(bio.get(), nullptr, ocspRequest.get()));
1542 if (t.expected_status == OCSP_HTTP_PARSE_SUCCESS && t.response_attached) {
1543 ASSERT_TRUE(ocspResponse);
1544 } else {
1545 ASSERT_FALSE(ocspResponse);
1546 }
1547 }
1548
1549 struct OCSPURLTestVector {
1550 const char *ocsp_url;
1551 const char *expected_host;
1552 const char *expected_port;
1553 const char *expected_path;
1554 int expected_ssl;
1555 int expected_status;
1556 };
1557
1558 static const OCSPURLTestVector kOCSPURLVectors[] = {
1559 // === VALID URLs ===
1560 {"http://ocsp.example.com/", "ocsp.example.com", "80", "/", 0,
1561 OCSP_URL_PARSE_SUCCESS},
1562 // Non-standard port.
1563 {"http://ocsp.example.com:8080/", "ocsp.example.com", "8080", "/", 0,
1564 OCSP_URL_PARSE_SUCCESS},
1565 {"http://ocsp.example.com/ocsp", "ocsp.example.com", "80", "/ocsp", 0,
1566 OCSP_URL_PARSE_SUCCESS},
1567 // Port for HTTPS is 443.
1568 {"https://ocsp.example.com/", "ocsp.example.com", "443", "/", 1,
1569 OCSP_URL_PARSE_SUCCESS},
1570 // Empty path, the default should be "/".
1571 {"https://ocsp.example.com", "ocsp.example.com", "443", "/", 1,
1572 OCSP_URL_PARSE_SUCCESS},
1573 // Parsing an IPv6 host address.
1574 {"http://[2001:db8::1]/ocsp", "2001:db8::1", "80", "/ocsp", 0,
1575 OCSP_URL_PARSE_SUCCESS},
1576 {"https://[2001:db8::1]/ocsp", "2001:db8::1", "443", "/ocsp", 1,
1577 OCSP_URL_PARSE_SUCCESS},
1578 // Parsing a URL with an invalid port, but OpenSSL still parses the string
1579 // where the port is expected.
1580 {"http://ocsp.example.com:invalid/", "ocsp.example.com", "invalid", "/", 0,
1581 OCSP_URL_PARSE_SUCCESS},
1582 // Empty host component.
1583 {"http:///ocsp", "", "80", "/ocsp", 0, OCSP_URL_PARSE_SUCCESS},
1584 // Potential Format String attacks.
1585 {"http://%s%s%s%s%s/ocsp", "%s%s%s%s%s", "80", "/ocsp", 0,
1586 OCSP_URL_PARSE_SUCCESS},
1587 {"http://%s%s%s%s%s, argv[1]/ocsp", "%s%s%s%s%s, argv[1]", "80", "/ocsp", 0,
1588 OCSP_URL_PARSE_SUCCESS},
1589 {"http://ocsp/%s%s%s%s%s", "ocsp", "80", "/%s%s%s%s%s", 0,
1590 OCSP_URL_PARSE_SUCCESS},
1591
1592 // === INVALID URLs ===
1593 // Not http or https in front.
1594 {"htp://ocsp.example.com/", nullptr, nullptr, nullptr, 0,
1595 OCSP_URL_PARSE_ERROR},
1596 {"%s%s%s%s://%s%s%s%s%s/ocsp", nullptr, nullptr, nullptr, 0,
1597 OCSP_URL_PARSE_ERROR},
1598 // Double slash is mandatory.
1599 {"http:/ocsp.example.com/", nullptr, nullptr, nullptr, 0,
1600 OCSP_URL_PARSE_ERROR},
1601 // Malformed.
1602 {"httocsp.example.com/", nullptr, nullptr, nullptr, 0,
1603 OCSP_URL_PARSE_ERROR},
1604 // No closing bracket for ipv6.
1605 {"http://[2001:db8::1/", nullptr, nullptr, nullptr, 0,
1606 OCSP_URL_PARSE_ERROR},
1607 };
1608
1609 class OCSPURLTest : public testing::TestWithParam<OCSPURLTestVector> {};
1610
1611 INSTANTIATE_TEST_SUITE_P(All, OCSPURLTest, testing::ValuesIn(kOCSPURLVectors));
1612
1613 TEST_P(OCSPURLTest, OCSPParseURL) {
1614 const OCSPURLTestVector &t = GetParam();
1615
1616 char *host = nullptr;
1617 char *path = nullptr;
1618 char *port = nullptr;
1619 int is_ssl;
1620
1621 int ret = OCSP_parse_url(t.ocsp_url, &host, &port, &path, &is_ssl);
1622 if (t.expected_status == OCSP_URL_PARSE_SUCCESS) {
1623 EXPECT_EQ(ret, OCSP_URL_PARSE_SUCCESS);
1624 EXPECT_EQ(std::string(host), std::string(t.expected_host));
1625 EXPECT_EQ(std::string(port), std::string(t.expected_port));
1626 EXPECT_EQ(std::string(path), std::string(t.expected_path));
1627 OPENSSL_free(host);
1628 OPENSSL_free(path);
1629 OPENSSL_free(port);
1630 } else {
1631 EXPECT_EQ(ret, OCSP_URL_PARSE_ERROR);
1632 EXPECT_FALSE(host);
1633 EXPECT_FALSE(port);
1634 EXPECT_FALSE(path);
1635 }
1636 }
1637
1638 // Nonce value used in "crypto/ocsp/test/aws/ocsp_response.der".
1639 static unsigned char ocsp_response_nonce[] = {
1640 0xaf, 0xab, 0xd4, 0xec, 0x6a, 0x17, 0x2c, 0x4a,
1641 0x98, 0xfb, 0x1a, 0x6d, 0x22, 0xff, 0x29, 0x28};
1642
1643 struct OCSPNonceTestVector {
1644 const char *ocsp_request;
1645 const char *ocsp_response;
1646 bool add_nonce;
1647 unsigned char *nonce;
1648 int nonce_len;
1649 int nonce_check_status;
1650 };
1651
1652 static const OCSPNonceTestVector kNonceTestVectors[] = {
1653 // Add a nonce to an OCSP request without a nonce.
1654 {"ocsp_request_no_nonce", "ocsp_response", true, ocsp_response_nonce,
1655 sizeof(ocsp_response_nonce), OCSP_NONCE_EQUAL},
1656 // Add a nonce to an OCSP request with a nonce. The original nonce should be
1657 // overwritten.
1658 {"ocsp_request", "ocsp_response", true, ocsp_response_nonce,
1659 sizeof(ocsp_response_nonce), OCSP_NONCE_EQUAL},
1660 // Generate a random nonce, which should be different from the nonce
1661 // available in the OCSP response (unless we have a collision).
1662 {"ocsp_request", "ocsp_response", true, nullptr, 0, OCSP_NONCE_NOT_EQUAL},
1663 // OCSP request with no nonce, but an OCSP response that does have one.
1664 {"ocsp_request_no_nonce", "ocsp_response", false, nullptr, 0,
1665 OCSP_NONCE_RESPONSE_ONLY},
1666 // OCSP request with a nonce, but an OCSP response that does not have one.
1667 {"ocsp_request", "ocsp_response_no_nonce", false, nullptr, 0,
1668 OCSP_NONCE_REQUEST_ONLY},
1669 // An OCSP request and an OCSP response that don't have a nonce to compare.
1670 {"ocsp_request_no_nonce", "ocsp_response_no_nonce", false, nullptr, 0,
1671 OCSP_NONCE_BOTH_ABSENT},
1672 };
1673
1674 class OCSPNonceTest : public testing::TestWithParam<OCSPNonceTestVector> {};
1675
1676 INSTANTIATE_TEST_SUITE_P(All, OCSPNonceTest,
1677 testing::ValuesIn(kNonceTestVectors));
1678
1679 TEST_P(OCSPNonceTest, OCSPNonce) {
1680 const OCSPNonceTestVector &t = GetParam();
1681
1682 std::string data =
1683 GetTestData(std::string("crypto/ocsp/test/aws/" +
1684 std::string(t.ocsp_request) + ".der")
1685 .c_str());
1686 std::vector<uint8_t> ocsp_request_data(data.begin(), data.end());
1687 bssl::UniquePtr<OCSP_REQUEST> ocspRequest =
1688 LoadOCSP_REQUEST(ocsp_request_data);
1689 std::string respData =
1690 GetTestData(std::string("crypto/ocsp/test/aws/" +
1691 std::string(t.ocsp_response) + ".der")
1692 .c_str());
1693 std::vector<uint8_t> ocsp_response_data(respData.begin(), respData.end());
1694 bssl::UniquePtr<OCSP_RESPONSE> ocspResponse =
1695 LoadOCSP_RESPONSE(ocsp_response_data);
1696 bssl::UniquePtr<OCSP_BASICRESP> basicResponse(
1697 OCSP_response_get1_basic(ocspResponse.get()));
1698 ASSERT_TRUE(basicResponse);
1699
1700 if (t.add_nonce) {
1701 // Adding a nonce should succeed. The original nonce will get overwritten
1702 // if one already exists.
1703 EXPECT_TRUE(
1704 OCSP_request_add1_nonce(ocspRequest.get(), t.nonce, t.nonce_len));
1705
1706 // Check if hard coded nonce value has been written correctly.
1707 if (t.nonce) {
1708 int req_idx = OCSP_REQUEST_get_ext_by_NID(ocspRequest.get(),
1709 NID_id_pkix_OCSP_Nonce, -1);
1710 const ASN1_OCTET_STRING *nonce_data = X509_EXTENSION_get_data(
1711 OCSP_REQUEST_get_ext(ocspRequest.get(), req_idx));
1712
1713 // The ASN.1 Octet String data type encoding begins with a Tag byte of
1714 // 0x04. The second byte is the length of the encoded value, so we compare
1715 // the rest of the bytes to ensure the nonce value was written correctly.
1716 // https://www.ietf.org/rfc/rfc6025.html#section-2.1.2
1717 EXPECT_EQ(Bytes(&ASN1_STRING_get0_data(nonce_data)[2],
1718 ASN1_STRING_length(nonce_data) - 2),
1719 Bytes(t.nonce, t.nonce_len));
1720 }
1721 }
1722 EXPECT_EQ(OCSP_check_nonce(ocspRequest.get(), basicResponse.get()),
1723 t.nonce_check_status);
1724
1725 // Check that nonce copying from |req| to |bs| also works as expected.
1726 if (t.nonce_check_status == OCSP_NONCE_RESPONSE_ONLY ||
1727 t.nonce_check_status == OCSP_NONCE_BOTH_ABSENT) {
1728 EXPECT_EQ(OCSP_copy_nonce(basicResponse.get(), ocspRequest.get()), 2);
1729 EXPECT_EQ(OCSP_check_nonce(ocspRequest.get(), basicResponse.get()),
1730 t.nonce_check_status);
1731 } else {
1732 // OpenSSL's implementation of |OCSP_copy_nonce| keeps the original nonce in
1733 // |resp| at the start of the list. We have to remove the old nonce prior to
1734 // copying the new one over.
1735 int resp_idx = OCSP_BASICRESP_get_ext_by_NID(basicResponse.get(),
1736 NID_id_pkix_OCSP_Nonce, -1);
1737 if (resp_idx >= 0) {
1738 bssl::UniquePtr<X509_EXTENSION> old_resp_ext(
1739 OCSP_BASICRESP_delete_ext(basicResponse.get(), resp_idx));
1740 }
1741 EXPECT_EQ(OCSP_copy_nonce(basicResponse.get(), ocspRequest.get()), 1);
1742 EXPECT_EQ(OCSP_check_nonce(ocspRequest.get(), basicResponse.get()),
1743 OCSP_NONCE_EQUAL);
1744 }
1745 }
1746
1747 TEST(OCSPTest, OCSPNonce) {
1748 std::string data = GetTestData(
1749 std::string("crypto/ocsp/test/aws/ocsp_request_no_nonce.der").c_str());
1750 std::vector<uint8_t> ocsp_request_data(data.begin(), data.end());
1751 bssl::UniquePtr<OCSP_REQUEST> ocspRequest =
1752 LoadOCSP_REQUEST(ocsp_request_data);
1753
1754 // Adding a nonce but using 0 to trigger the default specified length
1755 // should fail. A length must be specified when adding a specified nonce.
1756 EXPECT_FALSE(
1757 OCSP_request_add1_nonce(ocspRequest.get(), ocsp_response_nonce, 0));
1758
1759 // Adding a random nonce with the default length should succeed.
1760 // |OCSP_REQUEST_get_ext_by_NID| returns a negative number if a nonce does
1761 // not exist.
1762 EXPECT_LT(OCSP_REQUEST_get_ext_by_NID(ocspRequest.get(),
1763 NID_id_pkix_OCSP_Nonce, -1),
1764 0);
1765 EXPECT_TRUE(OCSP_request_add1_nonce(ocspRequest.get(), nullptr, 0));
1766 EXPECT_GE(OCSP_REQUEST_get_ext_by_NID(ocspRequest.get(),
1767 NID_id_pkix_OCSP_Nonce, -1),
1768 0);
1769
1770 // Same tests as above, but against an |OCSP_BASICRESP|.
1771 data = GetTestData(
1772 std::string("crypto/ocsp/test/aws/ocsp_response_no_nonce.der").c_str());
1773 std::vector<uint8_t> ocsp_response_data(data.begin(), data.end());
1774 bssl::UniquePtr<OCSP_RESPONSE> ocspResponse =
1775 LoadOCSP_RESPONSE(ocsp_response_data);
1776 ASSERT_TRUE(ocspResponse);
1777 bssl::UniquePtr<OCSP_BASICRESP> basicResponse(
1778 OCSP_response_get1_basic(ocspResponse.get()));
1779 ASSERT_TRUE(basicResponse);
1780
1781 EXPECT_FALSE(
1782 OCSP_basic_add1_nonce(basicResponse.get(), ocsp_response_nonce, 0));
1783
1784 // Adding a random nonce with the default length should succeed.
1785 // |OCSP_BASICRESP_get_ext_by_NID| returns a negative number if a nonce does
1786 // not exist.
1787 // Add a random nonce with a specified length this time around.
1788 EXPECT_LT(OCSP_BASICRESP_get_ext_by_NID(basicResponse.get(),
1789 NID_id_pkix_OCSP_Nonce, -1),
1790 0);
1791 EXPECT_TRUE(OCSP_basic_add1_nonce(basicResponse.get(), nullptr, 10));
1792 EXPECT_GE(OCSP_BASICRESP_get_ext_by_NID(basicResponse.get(),
1793 NID_id_pkix_OCSP_Nonce, -1),
1794 0);
1795 }
1796
1797 TEST(OCSPTest, OCSPCRLString) {
1798 for (int reason_code = 0; reason_code < 11; reason_code++) {
1799 if (reason_code == 7) {
1800 // Reason Code 7 is not used.
1801 EXPECT_EQ("(UNKNOWN)", std::string(OCSP_crl_reason_str(7)));
1802 continue;
1803 }
1804 EXPECT_NE("(UNKNOWN)", std::string(OCSP_crl_reason_str(reason_code)));
1805 }
1806 // More unexpected cases.
1807 EXPECT_EQ("(UNKNOWN)", std::string(OCSP_crl_reason_str(100)));
1808 EXPECT_EQ("(UNKNOWN)", std::string(OCSP_crl_reason_str(-1)));
1809 EXPECT_EQ("(UNKNOWN)", std::string(OCSP_crl_reason_str(-100)));
1810 }
1811
1812 TEST(OCSPTest, OCSPBIOTest) {
1813 std::string reqData =
1814 GetTestData(std::string("crypto/ocsp/test/aws/ocsp_request.der").c_str());
1815 std::vector<uint8_t> ocsp_request_data(reqData.begin(), reqData.end());
1816 std::string respData = GetTestData(
1817 std::string("crypto/ocsp/test/aws/ocsp_response.der").c_str());
1818 std::vector<uint8_t> ocsp_response_data(respData.begin(), respData.end());
1819
1820 // Write an OCSP response into the BIO.
1821 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1822 int respLen = (int)ocsp_response_data.size();
1823 ASSERT_EQ(BIO_write(bio.get(), ocsp_response_data.data(), respLen), respLen);
1824 bssl::UniquePtr<OCSP_RESPONSE> ocspResponse(
1825 d2i_OCSP_RESPONSE_bio(bio.get(), nullptr));
1826 EXPECT_TRUE(ocspResponse);
1827
1828 // Reserialize the OCSP response.
1829 EXPECT_TRUE(i2d_OCSP_RESPONSE_bio(bio.get(), ocspResponse.get()));
1830 const uint8_t *bio_data;
1831 size_t bio_len;
1832 ASSERT_TRUE(BIO_mem_contents(bio.get(), &bio_data, &bio_len));
1833 EXPECT_EQ(Bytes(bio_data, bio_len),
1834 Bytes(ocsp_response_data.data(), respLen));
1835
1836 // Write an OCSP request into the BIO, but it shouldn't successfully parse
1837 // with |d2i_OCSP_RESPONSE_bio|.
1838 ASSERT_TRUE(BIO_reset(bio.get()));
1839 const int reqLen = (int)ocsp_request_data.size();
1840 ASSERT_EQ(BIO_write(bio.get(), ocsp_request_data.data(), reqLen), reqLen);
1841 ocspResponse.reset(d2i_OCSP_RESPONSE_bio(bio.get(), nullptr));
1842 EXPECT_FALSE(ocspResponse);
1843
1844 // Write an OCSP request into the BIO.
1845 bio.reset(BIO_new(BIO_s_mem()));
1846 ASSERT_EQ(BIO_write(bio.get(), ocsp_request_data.data(), reqLen), reqLen);
1847 bssl::UniquePtr<OCSP_REQUEST> ocspRequest(
1848 d2i_OCSP_REQUEST_bio(bio.get(), nullptr));
1849 EXPECT_TRUE(ocspRequest);
1850
1851 // Reserialize the OCSP request.
1852 EXPECT_TRUE(i2d_OCSP_REQUEST_bio(bio.get(), ocspRequest.get()));
1853 ASSERT_TRUE(BIO_mem_contents(bio.get(), &bio_data, &bio_len));
1854 EXPECT_EQ(Bytes(bio_data, bio_len), Bytes(ocsp_request_data.data(), reqLen));
1855
1856 // Write an OCSP response into the BIO, but it shouldn't successfully parse
1857 // with |d2i_OCSP_REQUEST_bio|.
1858 ASSERT_TRUE(BIO_reset(bio.get()));
1859 ASSERT_EQ(BIO_write(bio.get(), ocsp_response_data.data(), respLen), respLen);
1860 ocspRequest.reset(d2i_OCSP_REQUEST_bio(bio.get(), nullptr));
1861 EXPECT_FALSE(ocspRequest);
1862 }
1863
1864 TEST(OCSPTest, CertIDDup) {
1865 bssl::UniquePtr<X509> issuer(CertFromPEM(
1866 GetTestData(std::string("crypto/ocsp/test/aws/ca_cert.pem").c_str())
1867 .c_str()));
1868 bssl::UniquePtr<X509> subject(CertFromPEM(
1869 GetTestData(std::string("crypto/ocsp/test/aws/rsa_cert.pem").c_str())
1870 .c_str()));
1871
1872 // Create a sample OCSP_CERTID structure
1873 bssl::UniquePtr<OCSP_CERTID> orig_id(
1874 OCSP_cert_to_id(EVP_sha256(), subject.get(), issuer.get()));
1875 ASSERT_TRUE(orig_id);
1876
1877 // Duplicate the OCSP_CERTID structure
1878 bssl::UniquePtr<OCSP_CERTID> dup_id(OCSP_CERTID_dup(orig_id.get()));
1879 ASSERT_TRUE(dup_id);
1880
1881 // Check that the duplicated structure has the same values as the original.
1882 EXPECT_EQ(
1883 ASN1_INTEGER_cmp(orig_id.get()->serialNumber, dup_id.get()->serialNumber),
1884 0);
1885 EXPECT_EQ(ASN1_STRING_cmp(orig_id.get()->issuerNameHash,
1886 dup_id.get()->issuerNameHash),
1887 0);
1888 EXPECT_EQ(ASN1_STRING_cmp(orig_id.get()->issuerKeyHash,
1889 dup_id.get()->issuerKeyHash),
1890 0);
1891 EXPECT_EQ(
1892 X509_ALGOR_cmp(orig_id.get()->hashAlgorithm, dup_id.get()->hashAlgorithm),
1893 0);
1894 // Check that the duplicated structure is not just a replicated pointer.
1895 EXPECT_NE(orig_id.get(), dup_id.get());
1896 }
1897
1898 TEST(OCSPTest, OCSPResponsePrint) {
1899 static const std::array<std::string, 19> kExpected{
1900 {"OCSP Response Data:", " OCSP Response Status: successful (0x0)",
1901 " Response Type: Basic OCSP Response", " Version: 1 (0x0)",
1902 " Responder Id: C = US, ST = WA, O = s2n, OU = s2n Test OCSP, CN = "
1903 "ocsp.s2ntest.com",
1904 " Produced At: May 26 00:23:34 2021 GMT",
1905 " Responses:", " Certificate ID:", " Hash Algorithm: sha1",
1906 " Issuer Name Hash: DE7932B3217E48FB4E47AE0B9007A55376AE44CA",
1907 " Issuer Key Hash: 12DF817571CA92D3CE1B2C2B773B9E3377F3F76F",
1908 " Serial Number: 7778", " Cert Status: good",
1909 " This Update: May 26 00:23:34 2021 GMT",
1910 " Next Update: May 24 00:23:34 2031 GMT", "",
1911 " Response Extensions:", " OCSP Nonce: ",
1912 " 0410AFABD4EC6A172C4A98FB1A6D22FF2928"}};
1913
1914 std::string respData = GetTestData(
1915 std::string("crypto/ocsp/test/aws/ocsp_response.der").c_str());
1916 std::vector<uint8_t> ocsp_response_data(respData.begin(), respData.end());
1917 bssl::UniquePtr<OCSP_RESPONSE> ocspResponse =
1918 LoadOCSP_RESPONSE(ocsp_response_data);
1919
1920 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1921 EXPECT_TRUE(OCSP_RESPONSE_print(bio.get(), ocspResponse.get(), 0));
1922 const uint8_t *out;
1923 size_t outlen;
1924 ASSERT_TRUE(BIO_mem_contents(bio.get(), &out, &outlen));
1925
1926 // Iterate through |kExpected| and verify that |OCSP_RESPONSE_print| has
1927 // the expected format.
1928 std::istringstream iss((std::string((char *)out, outlen)));
1929 std::string line;
1930 for (const auto &expected : kExpected) {
1931 std::getline(iss, line);
1932 EXPECT_EQ(line, expected);
1933 }
1934 }
1935
1936 // Verify that malformed OCSP responder IDs cannot be encoded into an
1937 // |OCSP_RESPONSE|.
1938 TEST(OCSPTest, OCSPResponsePrintMalformedResponderId) {
1939 // A default-initialized |OCSP_RESPID| (ASN1_CHOICE) has type -1, indicating
1940 // no alternative has been selected.
1941 bssl::UniquePtr<OCSP_BASICRESP> br(OCSP_BASICRESP_new());
1942 ASSERT_TRUE(br);
1943 OCSP_RESPID *rid = br->tbsResponseData->responderId;
1944 EXPECT_EQ(rid->type, -1);
1945 EXPECT_EQ(rid->value.byName, nullptr);
1946
1947 // The ASN.1 encoder rejects an |OCSP_BASICRESP| with an uninitialized
1948 // responderId choice.
1949 EXPECT_FALSE(
1950 OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, br.get()));
1951
1952 // Set type to |V_OCSP_RESPID_KEY| without providing a key. The encoder
1953 // rejects this.
1954 br.reset(OCSP_BASICRESP_new());
1955 ASSERT_TRUE(br);
1956 rid = br->tbsResponseData->responderId;
1957 rid->type = V_OCSP_RESPID_KEY;
1958 EXPECT_EQ(rid->value.byKey, nullptr);
1959 EXPECT_FALSE(
1960 OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, br.get()));
1961
1962 // Set type to |V_OCSP_RESPID_NAME| without providing a name. The encoder
1963 // rejects this.
1964 br.reset(OCSP_BASICRESP_new());
1965 ASSERT_TRUE(br);
1966 rid = br->tbsResponseData->responderId;
1967 rid->type = V_OCSP_RESPID_NAME;
1968 EXPECT_EQ(rid->value.byName, nullptr);
1969 EXPECT_FALSE(
1970 OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, br.get()));
1971
1972 // Set an unrecognized type. The encoder rejects this.
1973 br.reset(OCSP_BASICRESP_new());
1974 ASSERT_TRUE(br);
1975 rid = br->tbsResponseData->responderId;
1976 rid->type = 42;
1977 EXPECT_FALSE(
1978 OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, br.get()));
1979 }
1980
1981 TEST(OCSPTest, OCSPRequestPrint) {
1982 static const std::array<std::string, 11> kExpected{
1983 {"OCSP Request Data:", " Version: 1 (0x0)", " Requestor List:",
1984 " Certificate ID:", " Hash Algorithm: sha1",
1985 " Issuer Name Hash: DE7932B3217E48FB4E47AE0B9007A55376AE44CA",
1986 " Issuer Key Hash: 12DF817571CA92D3CE1B2C2B773B9E3377F3F76F",
1987 " Serial Number: 7778",
1988 " Request Extensions:", " OCSP Nonce: ",
1989 " 0410303F128CD824A2B465F4C846882B3E1F"}};
1990
1991 std::string data =
1992 GetTestData(std::string("crypto/ocsp/test/aws/ocsp_request.der").c_str());
1993 std::vector<uint8_t> ocsp_request_data(data.begin(), data.end());
1994 bssl::UniquePtr<OCSP_REQUEST> ocspRequest =
1995 LoadOCSP_REQUEST(ocsp_request_data);
1996
1997 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1998 EXPECT_TRUE(OCSP_REQUEST_print(bio.get(), ocspRequest.get(), 0));
1999 const uint8_t *out;
2000 size_t outlen;
2001 ASSERT_TRUE(BIO_mem_contents(bio.get(), &out, &outlen));
2002
2003 // Iterate through |kExpected| and verify that |OCSP_REQUEST_print| has
2004 // the expected format.
2005 std::istringstream iss((std::string((char *)out, outlen)));
2006 std::string line;
2007 for (const auto &expected : kExpected) {
2008 std::getline(iss, line);
2009 EXPECT_EQ(line, expected);
2010 }
2011 }
2012
2013 TEST(OCSPTest, OCSPUtilityFunctions) {
2014 // Create new OCSP_CERTID
2015 OCSP_CERTID *cert_id = OCSP_CERTID_new();
2016 ASSERT_TRUE(cert_id);
2017
2018 bssl::UniquePtr<OCSP_REQUEST> request(OCSP_REQUEST_new());
2019 ASSERT_TRUE(request);
2020
2021 // Test that an |OCSP_ONEREQ| does not exist yet.
2022 EXPECT_EQ(OCSP_request_onereq_count(request.get()), 0);
2023 EXPECT_FALSE(OCSP_request_onereq_get0(request.get(), 0));
2024
2025 OCSP_ONEREQ *one = OCSP_request_add0_id(request.get(), cert_id);
2026 ASSERT_TRUE(one);
2027 EXPECT_EQ(OCSP_request_onereq_count(request.get()), 1);
2028 EXPECT_TRUE(OCSP_request_onereq_get0(request.get(), 0));
2029
2030 // Call function to get OCSP_CERTID
2031 OCSP_CERTID *returned_id = OCSP_onereq_get0_id(one);
2032
2033 // Verify the returned OCSP_CERTID is same as the one set
2034 ASSERT_EQ(returned_id, cert_id);
2035 }
2036
2037 TEST(OCSPTest, OCSP_SINGLERESP) {
2038 bssl::UniquePtr<OCSP_SINGLERESP> single_resp(OCSP_SINGLERESP_new());
2039 ASSERT_TRUE(single_resp);
2040
2041 // Initialize an |X509_EXTENSION| for testing.
2042 bssl::UniquePtr<ASN1_OCTET_STRING> ext_oct(ASN1_OCTET_STRING_new());
2043 const uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05};
2044 ASSERT_TRUE(ASN1_OCTET_STRING_set(ext_oct.get(), data, sizeof(data)));
2045 bssl::UniquePtr<X509_EXTENSION> ext(X509_EXTENSION_create_by_NID(
2046 nullptr, NID_id_pkix_OCSP_CrlID, 0, ext_oct.get()));
2047 ASSERT_TRUE(ext);
2048
2049 // Test |X509_EXTENSION|s work with |OCSP_SINGLERESP|.
2050 EXPECT_TRUE(OCSP_SINGLERESP_add_ext(single_resp.get(), ext.get(), -1));
2051 EXPECT_EQ(OCSP_SINGLERESP_get_ext_count(single_resp.get()), 1);
2052 X509_EXTENSION *retrieved_ext = OCSP_SINGLERESP_get_ext(single_resp.get(), 0);
2053 ASSERT_EQ(ASN1_OCTET_STRING_cmp(X509_EXTENSION_get_data(retrieved_ext),
2054 X509_EXTENSION_get_data(ext.get())),
2055 0);
2056 }
2057
2058 // Helper to create an OCSP responder cert issued by |ca_cert|/|ca_key|, with
2059 // EKU=OCSP Signing. If |add_nocheck| is true, the id-pkix-ocsp-nocheck
2060 // extension is added.
2061 static bssl::UniquePtr<X509> MakeOCSPResponderCert(EVP_PKEY *responder_key,
2062 X509 *ca_cert,
2063 EVP_PKEY *ca_key,
2064 bool add_nocheck) {
2065 bssl::UniquePtr<X509> cert(X509_new());
2066 if (!cert ||
2067 !X509_set_version(cert.get(), X509_VERSION_3) ||
2068 !ASN1_INTEGER_set(X509_get_serialNumber(cert.get()), 1) ||
2069 !X509_set_issuer_name(cert.get(), X509_get_subject_name(ca_cert)) ||
2070 !X509_NAME_add_entry_by_txt(
2071 X509_get_subject_name(cert.get()), "CN", MBSTRING_UTF8,
2072 reinterpret_cast<const uint8_t *>("OCSP Responder"), -1, -1, 0) ||
2073 !X509_set_pubkey(cert.get(), responder_key) ||
2074 !ASN1_TIME_adj(X509_getm_notBefore(cert.get()), kReferenceTime, -1, 0) ||
2075 !ASN1_TIME_adj(X509_getm_notAfter(cert.get()), kReferenceTime, 1, 0)) {
2076 return nullptr;
2077 }
2078
2079 // Add EKU: OCSP Signing (required for delegated responder).
2080 bssl::UniquePtr<STACK_OF(ASN1_OBJECT)> eku(sk_ASN1_OBJECT_new_null());
2081 if (!eku || !sk_ASN1_OBJECT_push(eku.get(), OBJ_nid2obj(NID_OCSP_sign)) ||
2082 !X509_add1_ext_i2d(cert.get(), NID_ext_key_usage, eku.get(),
2083 /*crit=*/1, /*flags=*/0)) {
2084 return nullptr;
2085 }
2086
2087 if (add_nocheck) {
2088 ASN1_NULL *null_val = ASN1_NULL_new();
2089 if (null_val == NULL ||
2090 !X509_add1_ext_i2d(cert.get(), NID_id_pkix_OCSP_noCheck, null_val,
2091 /*crit=*/0, /*flags=*/0)) {
2092 ASN1_NULL_free(null_val);
2093 return nullptr;
2094 }
2095 ASN1_NULL_free(null_val);
2096 }
2097
2098 if (!X509_sign(cert.get(), ca_key, EVP_sha256())) {
2099 return nullptr;
2100 }
2101 return cert;
2102 }
2103
2104 // Test that the id-pkix-ocsp-nocheck extension on a delegated OCSP responder
2105 // certificate causes CRL checking to be skipped during verification. Without
2106 // the extension, verification should fail when CRL checking is enabled because
2107 // no CRL is available.
2108 TEST(OCSPTest, OCSPNoCheckExtension) {
2109 // Generate CA key and self-signed CA cert.
2110 bssl::UniquePtr<EVP_PKEY> ca_key(EVP_PKEY_new());
2111 ASSERT_TRUE(ca_key);
2112 bssl::UniquePtr<RSA> ca_rsa(RSA_new());
2113 bssl::UniquePtr<BIGNUM> e(BN_new());
2114 ASSERT_TRUE(BN_set_word(e.get(), RSA_F4));
2115 ASSERT_TRUE(RSA_generate_key_ex(ca_rsa.get(), 2048, e.get(), nullptr));
2116 ASSERT_TRUE(EVP_PKEY_set1_RSA(ca_key.get(), ca_rsa.get()));
2117
2118 bssl::UniquePtr<X509> ca_cert =
2119 MakeTestCert("Test CA", "Test CA", ca_key.get(), /*is_ca=*/true);
2120 ASSERT_TRUE(ca_cert);
2121 ASSERT_TRUE(X509_sign(ca_cert.get(), ca_key.get(), EVP_sha256()));
2122
2123 // Generate responder key.
2124 bssl::UniquePtr<EVP_PKEY> resp_key(EVP_PKEY_new());
2125 ASSERT_TRUE(resp_key);
2126 bssl::UniquePtr<RSA> resp_rsa(RSA_new());
2127 ASSERT_TRUE(RSA_generate_key_ex(resp_rsa.get(), 2048, e.get(), nullptr));
2128 ASSERT_TRUE(EVP_PKEY_set1_RSA(resp_key.get(), resp_rsa.get()));
2129
2130 // Create trust store with CRL checking enabled and verification time set
2131 // to kReferenceTime (certs from MakeTestCert are only valid around that time).
2132 bssl::UniquePtr<X509_STORE> trust_store(X509_STORE_new());
2133 ASSERT_TRUE(trust_store);
2134 ASSERT_TRUE(X509_STORE_add_cert(trust_store.get(), ca_cert.get()));
2135 X509_STORE_set_flags(trust_store.get(), X509_V_FLAG_CRL_CHECK);
2136 X509_VERIFY_PARAM *store_param = X509_STORE_get0_param(trust_store.get());
2137 ASSERT_TRUE(store_param);
2138 X509_VERIFY_PARAM_set_time_posix(store_param, kReferenceTime);
2139
2140 // Create an OCSP_CERTID for use in the OCSP response.
2141 bssl::UniquePtr<OCSP_CERTID> cert_id(
2142 OCSP_cert_to_id(EVP_sha1(), ca_cert.get(), ca_cert.get()));
2143 ASSERT_TRUE(cert_id);
2144
2145 // Test with nocheck extension: verification should succeed despite CRL
2146 // checking being enabled, because the extension causes the flag to be
2147 // cleared for the responder cert.
2148 {
2149 bssl::UniquePtr<X509> responder_cert = MakeOCSPResponderCert(
2150 resp_key.get(), ca_cert.get(), ca_key.get(), /*add_nocheck=*/true);
2151 ASSERT_TRUE(responder_cert);
2152
2153 bssl::UniquePtr<OCSP_BASICRESP> bs(OCSP_BASICRESP_new());
2154 ASSERT_TRUE(bs);
2155
2156 bssl::UniquePtr<ASN1_TIME> this_update(ASN1_TIME_adj(nullptr, kReferenceTime, -1, 0));
2157 bssl::UniquePtr<ASN1_TIME> next_update(ASN1_TIME_adj(nullptr, kReferenceTime, 1, 0));
2158 ASSERT_TRUE(OCSP_basic_add1_status(bs.get(), cert_id.get(),
2159 V_OCSP_CERTSTATUS_GOOD, 0, nullptr,
2160 this_update.get(), next_update.get()));
2161
2162 ASSERT_TRUE(OCSP_basic_sign(bs.get(), responder_cert.get(), resp_key.get(),
2163 EVP_sha256(),
2164 CertsToStack({ca_cert.get()}).get(), 0));
2165
2166 int ret = OCSP_basic_verify(
2167 bs.get(), CertsToStack({responder_cert.get()}).get(), trust_store.get(),
2168 0);
2169 EXPECT_EQ(ret, OCSP_VERIFYSTATUS_SUCCESS)
2170 << "Verification should succeed with nocheck extension";
2171 }
2172
2173 ERR_clear_error();
2174
2175 // Test without nocheck extension: verification should fail because CRL
2176 // checking is enabled but no CRL is available.
2177 {
2178 bssl::UniquePtr<X509> responder_cert = MakeOCSPResponderCert(
2179 resp_key.get(), ca_cert.get(), ca_key.get(), /*add_nocheck=*/false);
2180 ASSERT_TRUE(responder_cert);
2181
2182 bssl::UniquePtr<OCSP_BASICRESP> bs(OCSP_BASICRESP_new());
2183 ASSERT_TRUE(bs);
2184
2185 bssl::UniquePtr<ASN1_TIME> this_update(ASN1_TIME_adj(nullptr, kReferenceTime, -1, 0));
2186 bssl::UniquePtr<ASN1_TIME> next_update(ASN1_TIME_adj(nullptr, kReferenceTime, 1, 0));
2187 ASSERT_TRUE(OCSP_basic_add1_status(bs.get(), cert_id.get(),
2188 V_OCSP_CERTSTATUS_GOOD, 0, nullptr,
2189 this_update.get(), next_update.get()));
2190
2191 ASSERT_TRUE(OCSP_basic_sign(bs.get(), responder_cert.get(), resp_key.get(),
2192 EVP_sha256(),
2193 CertsToStack({ca_cert.get()}).get(), 0));
2194
2195 int ret = OCSP_basic_verify(
2196 bs.get(), CertsToStack({responder_cert.get()}).get(), trust_store.get(),
2197 0);
2198 EXPECT_NE(ret, OCSP_VERIFYSTATUS_SUCCESS)
2199 << "Verification should fail without nocheck extension when CRL "
2200 "checking is enabled";
2201 }
2202 }