cast_auth_util_fuzzer_shared.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "components/cast_channel/cast_auth_util_fuzzer_shared.h"
  5. #include "third_party/openscreen/src/cast/common/channel/proto/cast_channel.pb.h"
  6. namespace cast_channel {
  7. namespace fuzz {
  8. using ::cast::channel::CastMessage;
  9. void SetupAuthenticateChallengeReplyInput(
  10. const std::vector<std::string>& certs,
  11. CastAuthUtilInputs::AuthenticateChallengeReplyInput* input) {
  12. // If we have a DeviceAuthMessage, use it to override the cast_message()
  13. // payload with a more interesting value.
  14. if (input->has_auth_message()) {
  15. // Optimization: if the payload_binary() field is going to be
  16. // overwritten, insist that it has to be empty initially. This cuts
  17. // down on how much time is spent generating identical arguments for
  18. // AuthenticateChallengeReply() from different values of |input|.
  19. if (input->cast_message().has_payload_binary()) {
  20. return;
  21. }
  22. if (!input->auth_message().has_response()) {
  23. // Optimization.
  24. if (input->nonce_ok() || input->response_certs_ok() ||
  25. input->tbs_crls_size() || input->crl_certs_ok() ||
  26. input->crl_signatures_ok()) {
  27. return;
  28. }
  29. } else {
  30. cast::channel::AuthResponse& response =
  31. *input->mutable_auth_message()->mutable_response();
  32. // Maybe force the nonce to be the correct value.
  33. if (input->nonce_ok()) {
  34. // Optimization.
  35. if (response.has_sender_nonce()) {
  36. return;
  37. }
  38. response.set_sender_nonce(input->nonce());
  39. }
  40. // Maybe force the response certs to be valid.
  41. if (input->response_certs_ok()) {
  42. // Optimization.
  43. if (!response.client_auth_certificate().empty() ||
  44. response.intermediate_certificate_size() > 0) {
  45. return;
  46. }
  47. response.set_client_auth_certificate(certs.front());
  48. response.clear_intermediate_certificate();
  49. for (std::size_t i = 1; i < certs.size(); i++) {
  50. response.add_intermediate_certificate(certs.at(i));
  51. }
  52. }
  53. // Maybe replace the crl() field in the response with valid data.
  54. if (input->tbs_crls_size() == 0) {
  55. // Optimization.
  56. if (input->crl_certs_ok() || input->crl_signatures_ok()) {
  57. return;
  58. }
  59. } else {
  60. // Optimization.
  61. if (response.has_crl()) {
  62. return;
  63. }
  64. cast::certificate::CrlBundle crl_bundle;
  65. for (const auto& tbs_crl : input->tbs_crls()) {
  66. cast::certificate::Crl& crl = *crl_bundle.add_crls();
  67. if (input->crl_certs_ok()) {
  68. crl.set_signer_cert(certs.at(0));
  69. }
  70. if (input->crl_signatures_ok()) {
  71. crl.set_signature("");
  72. }
  73. tbs_crl.SerializeToString(crl.mutable_tbs_crl());
  74. }
  75. crl_bundle.SerializeToString(response.mutable_crl());
  76. }
  77. }
  78. input->mutable_cast_message()->set_payload_type(CastMessage::BINARY);
  79. input->auth_message().SerializeToString(
  80. input->mutable_cast_message()->mutable_payload_binary());
  81. }
  82. }
  83. } // namespace fuzz
  84. } // namespace cast_channel