// Copyright 2022 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_ADVERTISEMENT_H_ #define THIRD_PARTY_NEARBY_SHARING_ADVERTISEMENT_H_ #include #include #include #include #include #include "absl/types/span.h" #include "sharing/common/nearby_share_enums.h" namespace nearby { namespace sharing { // An advertisement in the form of // [VERSION|VISIBILITY][SALT][ACCOUNT_IDENTIFIER][LEN][DEVICE_NAME]. // A device name indicates the advertisement is visible to everyone; // a missing device name indicates the advertisement is contacts-only. class Advertisement { public: static constexpr uint8_t kSaltSize = 2; static constexpr uint8_t kMetadataEncryptionKeyHashByteSize = 14; static std::unique_ptr NewInstance( std::vector salt, std::vector encrypted_metadata_key, ShareTargetType device_type, std::optional device_name); Advertisement(int version, std::vector salt, std::vector encrypted_metadata_key, ShareTargetType device_type, std::optional device_name); ~Advertisement() = default; Advertisement(const Advertisement&) = default; Advertisement& operator=(const Advertisement&) = default; Advertisement(Advertisement&&) = default; Advertisement& operator=(Advertisement&&) = default; std::vector ToEndpointInfo(); int version() const { return version_; } const std::vector& salt() const { return salt_; } const std::vector& encrypted_metadata_key() const { return encrypted_metadata_key_; } ShareTargetType device_type() const { return device_type_; } const std::optional& device_name() const { return device_name_; } bool HasDeviceName() const { return device_name_.has_value(); } static std::unique_ptr FromEndpointInfo( absl::Span endpoint_info); private: // The version of the advertisement. Different versions can have different // ways of parsing the endpoint id. int version_; // Random bytes that were used as salt during encryption of public certificate // metadata. std::vector salt_ = {}; // An encrypted symmetric key that was used to encrypt public certificate // metadata, including an account identifier signifying the remote device. // The key can be decrypted using |salt| and the corresponding public // certificate's secret/authenticity key. std::vector encrypted_metadata_key_ = {}; // The type of device that the advertisement identifies. ShareTargetType device_type_ = ShareTargetType::kUnknown; // The human-readable name of the remote device. std::optional device_name_ = std::nullopt; }; } // namespace sharing } // namespace nearby #endif // THIRD_PARTY_NEARBY_SHARING_ADVERTISEMENT_H_