openscreen_cast_auth_util_fuzzer.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <openssl/asn1.h>
  5. #include <openssl/x509.h>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. #include "base/notreached.h"
  11. #include "base/time/time.h"
  12. #include "components/cast_certificate/cast_cert_reader.h"
  13. #include "components/cast_channel/cast_auth_util_fuzzer_shared.h"
  14. #include "components/cast_channel/fuzz_proto/fuzzer_inputs.pb.h"
  15. // Generated by the "cast_auth_util_fuzzer_certs" data_headers target.
  16. #include "components/test/data/cast_certificate/certificates/chromecast_gen1_data.h"
  17. #include "testing/libfuzzer/proto/lpm_interface.h"
  18. #include "third_party/openscreen/src/cast/common/public/parsed_certificate.h"
  19. #include "third_party/openscreen/src/cast/common/public/trust_store.h"
  20. #include "third_party/openscreen/src/cast/sender/channel/cast_auth_util.h"
  21. namespace cast_channel {
  22. namespace fuzz {
  23. namespace {
  24. using ::cast::channel::CastMessage;
  25. using ::openscreen::cast::TrustStore;
  26. const uint8_t kCertData[] = {
  27. // Generated by //net/data/ssl/certificates:generate_fuzzer_cert_includes
  28. #include "net/data/ssl/certificates/wildcard.inc"
  29. };
  30. static std::vector<std::string>* InitializeCertsOnce() {
  31. static std::vector<std::string> certs =
  32. cast_certificate::ReadCertificateChainFromString(
  33. openscreen::cast::kChromecastGen1);
  34. CHECK(certs.size() >= 1) << "We should always have at least one certificate.";
  35. return &certs;
  36. }
  37. static TrustStore* InitializeCastTrustStoreOnce() {
  38. static TrustStore* cast_trust_store =
  39. openscreen::cast::CastTrustStore::Create().release();
  40. return cast_trust_store;
  41. }
  42. static TrustStore* InitializeCastCRLTrustStoreOnce() {
  43. static TrustStore* crl_trust_store =
  44. openscreen::cast::CastCRLTrustStore::Create().release();
  45. return crl_trust_store;
  46. }
  47. time_t GenerateCertTime(TimeBoundCase c, int direction) {
  48. base::Time t;
  49. switch (c) {
  50. case TimeBoundCase::VALID:
  51. // Create bound that include the current date.
  52. t = base::Time::Now() + base::Days(direction);
  53. break;
  54. case TimeBoundCase::INVALID:
  55. // Create a bound that excludes the current date.
  56. t = base::Time::Now() + base::Days(-direction);
  57. break;
  58. case TimeBoundCase::OOB:
  59. // Create a bound so far in the past/future it's not valid.
  60. t = base::Time::Now() + base::Days(direction * 10000);
  61. break;
  62. case TimeBoundCase::MISSING:
  63. // Remove any existing bound.
  64. t = base::Time();
  65. break;
  66. default:
  67. NOTREACHED();
  68. }
  69. return t.ToTimeT();
  70. }
  71. DEFINE_PROTO_FUZZER(CastAuthUtilInputs& input_union) {
  72. std::vector<std::string>* certs = InitializeCertsOnce();
  73. TrustStore* cast_trust_store = InitializeCastTrustStoreOnce();
  74. TrustStore* crl_trust_store = InitializeCastCRLTrustStoreOnce();
  75. if (input_union.input_case() !=
  76. CastAuthUtilInputs::kAuthenticateChallengeReplyInput) {
  77. return;
  78. }
  79. CastAuthUtilInputs::AuthenticateChallengeReplyInput& input =
  80. *input_union.mutable_authenticate_challenge_reply_input();
  81. SetupAuthenticateChallengeReplyInput(*certs, &input);
  82. // Build a well-formed cert with start and expiry times relative to the
  83. // current time. The actual cert doesn't matter for testing purposes
  84. // because validation failures are ignored.
  85. std::vector<uint8_t> cert_data{std::begin(kCertData), std::end(kCertData)};
  86. std::unique_ptr<openscreen::cast::ParsedCertificate> peer_cert = std::move(
  87. openscreen::cast::ParsedCertificate::ParseFromDER(cert_data).value());
  88. time_t not_before_time = GenerateCertTime(input.start_case(), -1);
  89. time_t not_after_time = GenerateCertTime(input.expiry_case(), 1);
  90. peer_cert->SetNotBeforeTimeForTesting(not_before_time);
  91. peer_cert->SetNotAfterTimeForTesting(not_after_time);
  92. auto context = openscreen::cast::AuthContext::CreateForTest(input.nonce());
  93. openscreen::cast::AuthenticateChallengeReply(input.cast_message(), *peer_cert,
  94. context, cast_trust_store,
  95. crl_trust_store);
  96. }
  97. } // namespace
  98. } // namespace fuzz
  99. } // namespace cast_channel