// Copyright 2022-2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef THIRD_PARTY_NEARBY_SHARING_PAIRED_KEY_VERIFICATION_RUNNER_H_ #define THIRD_PARTY_NEARBY_SHARING_PAIRED_KEY_VERIFICATION_RUNNER_H_ #include #include #include #include #include #include "absl/functional/any_invocable.h" #include "absl/time/time.h" #include "internal/platform/clock.h" #include "proto/sharing_enums.pb.h" #include "sharing/certificates/nearby_share_certificate_manager.h" #include "sharing/certificates/nearby_share_decrypted_public_certificate.h" #include "sharing/incoming_frames_reader.h" #include "sharing/proto/enums.pb.h" #include "sharing/proto/wire_format.pb.h" namespace nearby::sharing { class PairedKeyVerificationRunner : public std::enable_shared_from_this { public: enum class PairedKeyVerificationResult { // Default value for verification result. kUnknown, // Succeeded with verification. kSuccess, // Failed to verify. kFail, // Unable to verify. Occurs when missing proper certificates. kUnable, }; struct VisibilityHistory { proto::DeviceVisibility visibility; proto::DeviceVisibility last_visibility; absl::Time last_visibility_time; }; PairedKeyVerificationRunner( Clock* clock, location::nearby::proto::sharing::OSType os_type, bool share_target_is_incoming, const VisibilityHistory& visibility_history, const std::vector& token, absl::AnyInvocable< void(const nearby::sharing::service::proto::Frame& frame)> frame_writer, const std::optional& certificate, NearbyShareCertificateManager* certificate_manager, IncomingFramesReader* frames_reader, absl::Duration read_frame_timeout); ~PairedKeyVerificationRunner(); void Run(std::function< void(PairedKeyVerificationResult verification_result, ::location::nearby::proto::sharing::OSType remote_os_type)> callback); std::weak_ptr GetWeakPtr() { return this->weak_from_this(); } private: void SendPairedKeyEncryptionFrame(); void OnReadPairedKeyEncryptionFrame( std::optional frame); void OnReadPairedKeyResultFrame( std::optional frame); void SendPairedKeyResultFrame(PairedKeyVerificationResult result); // Verifies auth token hash in frame using private certificate for visibility. // Returns either kSuccess or kUnable. This function never returns kFail. PairedKeyVerificationResult VerifyAuthTokenHashWithPrivateCertificate( proto::DeviceVisibility visibility, const nearby::sharing::service::proto::V1Frame& frame); PairedKeyVerificationResult VerifyPairedKeyEncryptionFrame( const nearby::sharing::service::proto::V1Frame& frame); void ApplyResult(PairedKeyVerificationResult result); // True if visibility has changed recently. bool IsVisibilityRecentlyUpdated() const; nearby::Clock* const clock_; const location::nearby::proto::sharing::OSType os_type_; VisibilityHistory visibility_history_; std::vector raw_token_; absl::AnyInvocable frame_writer_; std::optional certificate_; NearbyShareCertificateManager* certificate_manager_; IncomingFramesReader* frames_reader_; const absl::Duration read_frame_timeout_; std::function callback_; PairedKeyVerificationResult verification_result_; char local_prefix_; char remote_prefix_; }; } // namespace nearby::sharing #endif // THIRD_PARTY_NEARBY_SHARING_PAIRED_KEY_VERIFICATION_RUNNER_H_