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 24 #include "hci/address.h" 25 26 namespace test_vendor_lib { 27 28 using ::bluetooth::hci::Address; 29 30 enum class PairingType : uint8_t { 31 AUTO_CONFIRMATION, 32 CONFIRM_Y_N, 33 DISPLAY_PIN, 34 DISPLAY_AND_CONFIRM, 35 INPUT_PIN, 36 INVALID = 0xff, 37 }; 38 39 enum class IoCapabilityType : uint8_t { 40 DISPLAY_ONLY = 0, 41 DISPLAY_YES_NO = 1, 42 KEYBOARD_ONLY = 2, 43 NO_INPUT_NO_OUTPUT = 3, 44 INVALID = 0xff, 45 }; 46 47 enum class AuthenticationType : uint8_t { 48 NO_BONDING = 0, 49 NO_BONDING_MITM = 1, 50 DEDICATED_BONDING = 2, 51 DEDICATED_BONDING_MITM = 3, 52 GENERAL_BONDING = 4, 53 GENERAL_BONDING_MITM = 5, 54 INVALID = 0xff, 55 }; 56 57 // Encapsulate the details of storing and retrieving keys. 58 class SecurityManager { 59 public: SecurityManager(uint16_t num_keys)60 SecurityManager(uint16_t num_keys) : max_keys_(num_keys) {} 61 virtual ~SecurityManager() = default; 62 63 uint16_t DeleteAllKeys(); 64 uint16_t DeleteKey(const Address& addr); 65 uint16_t ReadAllKeys() const; 66 uint16_t ReadKey(const Address& addr) const; 67 uint16_t WriteKey(const Address& addr, const std::array<uint8_t, 16>& key); ReadCapacity()68 uint16_t ReadCapacity() const { return max_keys_; }; 69 70 const std::array<uint8_t, 16>& GetKey(const Address& addr) const; 71 72 void AuthenticationRequest(const Address& addr, uint16_t handle); 73 void AuthenticationRequestFinished(); 74 75 bool AuthenticationInProgress(); 76 uint16_t GetAuthenticationHandle(); 77 Address GetAuthenticationAddress(); 78 79 void SetPeerIoCapability(const Address& addr, uint8_t io_capability, uint8_t oob_present_flag, 80 uint8_t authentication_requirements); 81 void SetLocalIoCapability(const Address& peer, uint8_t io_capability, uint8_t oob_present_flag, 82 uint8_t authentication_requirements); 83 84 PairingType GetSimplePairingType(); 85 86 void InvalidateIoCapabilities(); 87 88 private: 89 uint16_t max_keys_; 90 std::unordered_map<std::string, std::array<uint8_t, 16>> key_store_; 91 92 bool peer_capabilities_valid_{false}; 93 IoCapabilityType peer_io_capability_{IoCapabilityType::DISPLAY_ONLY}; 94 bool peer_oob_present_flag_{false}; 95 AuthenticationType peer_authentication_requirements_{ 96 AuthenticationType::NO_BONDING}; 97 98 bool host_capabilities_valid_{false}; 99 IoCapabilityType host_io_capability_{IoCapabilityType::DISPLAY_ONLY}; 100 bool host_oob_present_flag_{false}; 101 AuthenticationType host_authentication_requirements_{ 102 AuthenticationType::NO_BONDING}; 103 104 bool authenticating_{false}; 105 uint16_t current_handle_{}; 106 Address peer_address_{}; 107 }; 108 109 } // namespace test_vendor_lib 110