1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <array> 20 #include <cstdint> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "hci/address.h" 26 27 namespace test_vendor_lib { 28 29 using ::bluetooth::hci::Address; 30 31 enum class PairingType : uint8_t { 32 AUTO_CONFIRMATION, 33 CONFIRM_Y_N, 34 DISPLAY_PIN, 35 DISPLAY_AND_CONFIRM, 36 INPUT_PIN, 37 OUT_OF_BAND, 38 PEER_HAS_OUT_OF_BAND, 39 INVALID = 0xff, 40 }; 41 42 enum class IoCapabilityType : uint8_t { 43 DISPLAY_ONLY = 0, 44 DISPLAY_YES_NO = 1, 45 KEYBOARD_ONLY = 2, 46 NO_INPUT_NO_OUTPUT = 3, 47 INVALID = 0xff, 48 }; 49 50 enum class AuthenticationType : uint8_t { 51 NO_BONDING = 0, 52 NO_BONDING_MITM = 1, 53 DEDICATED_BONDING = 2, 54 DEDICATED_BONDING_MITM = 3, 55 GENERAL_BONDING = 4, 56 GENERAL_BONDING_MITM = 5, 57 INVALID = 0xff, 58 }; 59 60 // Encapsulate the details of storing and retrieving keys. 61 class SecurityManager { 62 public: SecurityManager(uint16_t num_keys)63 SecurityManager(uint16_t num_keys) : max_keys_(num_keys) {} 64 virtual ~SecurityManager() = default; 65 66 uint16_t DeleteAllKeys(); 67 uint16_t DeleteKey(const Address& addr); 68 uint16_t ReadAllKeys() const; 69 uint16_t ReadKey(const Address& addr) const; 70 uint16_t WriteKey(const Address& addr, const std::array<uint8_t, 16>& key); ReadCapacity()71 uint16_t ReadCapacity() const { return max_keys_; }; 72 73 const std::array<uint8_t, 16>& GetKey(const Address& addr) const; 74 75 void AuthenticationRequest(const Address& addr, uint16_t handle); 76 void AuthenticationRequestFinished(); 77 78 bool AuthenticationInProgress(); 79 uint16_t GetAuthenticationHandle(); 80 Address GetAuthenticationAddress(); 81 82 void SetPinRequested(const Address& addr); 83 bool GetPinRequested(const Address& addr); 84 void SetLocalPin(const Address& peer, const std::vector<uint8_t>& pin); 85 void SetRemotePin(const Address& peer, const std::vector<uint8_t>& pin); 86 bool GetLocalPinResponseReceived(const Address& peer); 87 bool GetRemotePinResponseReceived(const Address& peer); 88 bool PinCompare(); 89 90 void SetPeerIoCapability(const Address& addr, uint8_t io_capability, uint8_t oob_present_flag, 91 uint8_t authentication_requirements); 92 void SetLocalIoCapability(const Address& peer, uint8_t io_capability, uint8_t oob_present_flag, 93 uint8_t authentication_requirements); 94 95 PairingType GetSimplePairingType(); 96 97 void InvalidateIoCapabilities(); 98 99 private: 100 uint16_t max_keys_; 101 std::unordered_map<std::string, std::array<uint8_t, 16>> key_store_; 102 103 bool peer_capabilities_valid_{false}; 104 IoCapabilityType peer_io_capability_{IoCapabilityType::DISPLAY_ONLY}; 105 uint8_t peer_oob_present_flag_{0}; 106 AuthenticationType peer_authentication_requirements_{ 107 AuthenticationType::NO_BONDING}; 108 bool peer_pin_requested_{false}; 109 bool peer_pin_received_{false}; 110 std::vector<uint8_t> peer_pin_; 111 112 bool host_capabilities_valid_{false}; 113 IoCapabilityType host_io_capability_{IoCapabilityType::DISPLAY_ONLY}; 114 uint8_t host_oob_present_flag_{0}; 115 AuthenticationType host_authentication_requirements_{ 116 AuthenticationType::NO_BONDING}; 117 std::vector<uint8_t> host_pin_; 118 bool host_pin_received_{false}; 119 120 bool authenticating_{false}; 121 uint16_t current_handle_{}; 122 Address peer_address_{}; 123 }; 124 125 } // namespace test_vendor_lib 126