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 <cstdint> 20 #include <string> 21 #include <unordered_map> 22 #include <vector> 23 24 #include "types/address.h" 25 26 namespace test_vendor_lib { 27 28 enum class PairingType : uint8_t { 29 AUTO_CONFIRMATION, 30 CONFIRM_Y_N, 31 DISPLAY_PIN, 32 DISPLAY_AND_CONFIRM, 33 INPUT_PIN, 34 INVALID = 0xff, 35 }; 36 37 enum class IoCapabilityType : uint8_t { 38 DISPLAY_ONLY = 0, 39 DISPLAY_YES_NO = 1, 40 KEYBOARD_ONLY = 2, 41 NO_INPUT_NO_OUTPUT = 3, 42 INVALID = 0xff, 43 }; 44 45 enum class AuthenticationType : uint8_t { 46 NO_BONDING = 0, 47 NO_BONDING_MITM = 1, 48 DEDICATED_BONDING = 2, 49 DEDICATED_BONDING_MITM = 3, 50 GENERAL_BONDING = 4, 51 GENERAL_BONDING_MITM = 5, 52 INVALID = 0xff, 53 }; 54 55 // Encapsulate the details of storing and retrieving keys. 56 class SecurityManager { 57 public: SecurityManager(uint16_t num_keys)58 SecurityManager(uint16_t num_keys) : max_keys_(num_keys) {} 59 virtual ~SecurityManager() = default; 60 61 uint16_t DeleteAllKeys(); 62 uint16_t DeleteKey(const Address& addr); 63 uint16_t ReadAllKeys() const; 64 uint16_t ReadKey(const Address& addr) const; 65 uint16_t WriteKey(const Address& addr, const std::vector<uint8_t>& key); ReadCapacity()66 uint16_t ReadCapacity() const { 67 return max_keys_; 68 }; 69 70 const std::vector<uint8_t>& 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::vector<uint8_t>> key_store_; 91 92 bool peer_capabilities_valid_{false}; 93 IoCapabilityType peer_io_capability_; 94 bool peer_oob_present_flag_; 95 AuthenticationType peer_authentication_requirements_; 96 97 bool host_capabilities_valid_{false}; 98 IoCapabilityType host_io_capability_; 99 bool host_oob_present_flag_; 100 AuthenticationType host_authentication_requirements_; 101 102 bool authenticating_{false}; 103 uint16_t current_handle_; 104 Address peer_address_; 105 }; 106 107 } // namespace test_vendor_lib 108