1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #ifndef SM_H 39 #define SM_H 40 41 #if defined __cplusplus 42 extern "C" { 43 #endif 44 45 #include <stdint.h> 46 #include "btstack_util.h" 47 #include "btstack_defines.h" 48 #include "hci.h" 49 50 typedef struct { 51 btstack_linked_item_t item; 52 bd_addr_t address; 53 bd_addr_type_t address_type; 54 } sm_lookup_entry_t; 55 56 /* API_START */ 57 58 /** 59 * @brief Initializes the Security Manager, connects to L2CAP 60 */ 61 void sm_init(void); 62 63 /** 64 * @brief Set secret ER key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2 65 * @note If not set and btstack_tlv is configured, ER key is generated and stored in TLV by SM 66 * @param er 67 */ 68 void sm_set_er(sm_key_t er); 69 70 /** 71 * @brief Set secret IR key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2 72 * @note If not set and btstack_tlv is configured, IR key is generated and stored in TLV by SM 73 */ 74 void sm_set_ir(sm_key_t ir); 75 76 /** 77 * 78 * @brief Registers OOB Data Callback. The callback should set the oob_data and return 1 if OOB data is availble 79 * @param get_oob_data_callback 80 */ 81 void sm_register_oob_data_callback( int (*get_oob_data_callback)(uint8_t address_type, bd_addr_t addr, uint8_t * oob_data)); 82 83 /** 84 * @brief Add event packet handler. 85 */ 86 void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler); 87 88 /** 89 * @brief Limit the STK generation methods. Bonding is stopped if the resulting one isn't in the list 90 * @param OR combination of SM_STK_GENERATION_METHOD_ 91 */ 92 void sm_set_accepted_stk_generation_methods(uint8_t accepted_stk_generation_methods); 93 94 /** 95 * @brief Set the accepted encryption key size range. Bonding is stopped if the result isn't within the range 96 * @param min_size (default 7) 97 * @param max_size (default 16) 98 */ 99 void sm_set_encryption_key_size_range(uint8_t min_size, uint8_t max_size); 100 101 /** 102 * @brief Sets the requested authentication requirements, bonding yes/no, MITM yes/no, SC yes/no, keypress yes/no 103 * @param OR combination of SM_AUTHREQ_ flags 104 */ 105 void sm_set_authentication_requirements(uint8_t auth_req); 106 107 /** 108 * @brief Sets the available IO Capabilities 109 * @param IO_CAPABILITY_ 110 */ 111 void sm_set_io_capabilities(io_capability_t io_capability); 112 113 /** 114 * @brief Let Peripheral request an encrypted connection right after connecting 115 * @note Not used normally. Bonding is triggered by access to protected attributes in ATT Server 116 */ 117 void sm_set_request_security(int enable); 118 119 /** 120 * @brief Trigger Security Request 121 * @note Not used normally. Bonding is triggered by access to protected attributes in ATT Server 122 */ 123 void sm_send_security_request(hci_con_handle_t con_handle); 124 125 /** 126 * @brief Decline bonding triggered by event before 127 * @param con_handle 128 */ 129 void sm_bonding_decline(hci_con_handle_t con_handle); 130 131 /** 132 * @brief Confirm Just Works bonding 133 * @param con_handle 134 */ 135 void sm_just_works_confirm(hci_con_handle_t con_handle); 136 137 /** 138 * @brief Confirm value from SM_EVENT_NUMERIC_COMPARISON_REQUEST for Numeric Comparison bonding 139 * @param con_handle 140 */ 141 void sm_numeric_comparison_confirm(hci_con_handle_t con_handle); 142 143 /** 144 * @brief Reports passkey input by user 145 * @param con_handle 146 * @param passkey in [0..999999] 147 */ 148 void sm_passkey_input(hci_con_handle_t con_handle, uint32_t passkey); 149 150 /** 151 * @brief Send keypress notification for keyboard only devices 152 * @param con_handle 153 * @param action see SM_KEYPRESS_* in bluetooth.h 154 */ 155 void sm_keypress_notification(hci_con_handle_t con_handle, uint8_t action); 156 157 /** 158 * @brief Used by att_server.c to request user authorization. 159 * @param con_handle 160 */ 161 void sm_request_pairing(hci_con_handle_t con_handle); 162 163 /** 164 * @brief Report user authorization decline. 165 * @param con_handle 166 */ 167 void sm_authorization_decline(hci_con_handle_t con_handle); 168 169 /** 170 * @brief Report user authorization grant. 171 * @param con_handle 172 */ 173 void sm_authorization_grant(hci_con_handle_t con_handle); 174 175 /** 176 * @brief Support for signed writes, used by att_server. 177 * @returns ready 178 */ 179 int sm_cmac_ready(void); 180 181 /** 182 * @brief Support for signed writes, used by att_server. 183 * @note Message is in little endian to allows passing in ATT PDU without flipping. 184 * @note signing data: [opcode, attribute_handle, message, sign_counter] 185 * @note calculated hash in done_callback is big endian and has 16 byte. 186 * @param key 187 * @param opcde 188 * @param attribute_handle 189 * @param message_len 190 * @param message 191 * @param sign_counter 192 */ 193 void sm_cmac_signed_write_start(const sm_key_t key, uint8_t opcode, uint16_t attribute_handle, uint16_t message_len, const uint8_t * message, uint32_t sign_counter, void (*done_callback)(uint8_t * hash)); 194 195 /* 196 * @brief Match address against bonded devices 197 * @return 0 if successfully added to lookup queue 198 * @note Triggers SM_IDENTITY_RESOLVING_* events 199 */ 200 int sm_address_resolution_lookup(uint8_t addr_type, bd_addr_t addr); 201 202 /** 203 * @brief Get Identity Resolving state 204 * @param con_handle 205 * @return irk_lookup_state_t 206 * @note returns IRK_LOOKUP_IDLE if connection does not exist 207 */ 208 irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle); 209 210 /** 211 * @brief Identify device in LE Device DB. 212 * @param handle 213 * @return index from le_device_db or -1 if not found/identified 214 */ 215 int sm_le_device_index(hci_con_handle_t con_handle ); 216 217 /** 218 * @brief Use fixec passkey for Legacy and SC instead of generating a random number 219 * @note Can be used to improve security over Just Works if no keyboard or displary are present and 220 * individual random passkey can be printed on the device during production 221 * @param passkey 222 */ 223 void sm_use_fixed_passkey_in_display_role(uint32_t passkey); 224 225 /** 226 * @brief Allow connection re-encryption in Peripheral (Responder) role for LE Legacy Pairing 227 * without entry for Central device stored in LE Device DB 228 * @note BTstack in Peripheral Role (Responder) supports LE Legacy Pairing without a persistent LE Device DB as 229 * the LTK is reconstructed from a local secret IRK and EDIV + Random stored on Central (Initiator) device 230 * On the downside, it's not really possible to delete a pairing if this is enabled. 231 * @param allow encryption using reconstructed LTK without stored entry (Default: 1) 232 */ 233 void sm_allow_ltk_reconstruction_without_le_device_db_entry(int allow); 234 235 /** 236 * @brief Generate OOB data for LE Secure Connections 237 * @note This generates a 128 bit random number ra and then calculates Ca = f4(PKa, PKa, ra, 0) 238 * New OOB data should be generated for each pairing. Ra is used for subsequent OOB pairings 239 * @param callback 240 * @returns status 241 */ 242 uint8_t sm_generate_sc_oob_data(void (*callback)(const uint8_t * confirm_value, const uint8_t * random_value)); 243 244 /** 245 * 246 * @brief Registers OOB Data Callback for LE Secure Conections. The callback should set all arguments and return 1 if OOB data is availble 247 * @note the oob_sc_local_random usually is the random_value returend by sm_generate_sc_oob_data 248 * @param get_oob_data_callback 249 */ 250 void sm_register_sc_oob_data_callback( int (*get_sc_oob_data_callback)(uint8_t address_type, bd_addr_t addr, uint8_t * oob_sc_peer_confirm, uint8_t * oob_sc_peer_random)); 251 252 /* API_END */ 253 254 // PTS testing 255 void sm_test_set_irk(sm_key_t irk); 256 void sm_test_use_fixed_local_csrk(void); 257 258 #ifdef ENABLE_TESTING_SUPPORT 259 void sm_test_set_pairing_failure(int reason); 260 #endif 261 262 #if defined __cplusplus 263 } 264 #endif 265 266 #endif // SM_H 267