1 /* 2 * Copyright (C) 2017 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 #define __BTSTACK_FILE__ "le_device_db_tlv.c" 39 40 #include "ble/le_device_db.h" 41 #include "ble/le_device_db_tlv.h" 42 43 #include "ble/core.h" 44 45 #include <stdio.h> 46 #include <string.h> 47 #include "btstack_debug.h" 48 49 // LE Device DB Implementation storing entries in btstack_tlv 50 51 // Local cache is used to keep track of deleted entries in TLV 52 53 #define INVALID_ENTRY_ADDR_TYPE 0xff 54 55 // Single stored entry 56 typedef struct le_device_db_entry_t { 57 58 uint32_t seq_nr; // used for "least recently stored" eviction strategy 59 60 // Identification 61 int addr_type; 62 bd_addr_t addr; 63 sm_key_t irk; 64 65 // Stored pairing information allows to re-establish an enncrypted connection 66 // with a peripheral that doesn't have any persistent memory 67 sm_key_t ltk; 68 uint16_t ediv; 69 uint8_t rand[8]; 70 uint8_t key_size; 71 uint8_t authenticated; 72 uint8_t authorized; 73 74 #ifdef ENABLE_LE_SIGNED_WRITE 75 // Signed Writes by remote 76 sm_key_t remote_csrk; 77 uint32_t remote_counter; 78 79 // Signed Writes by us 80 sm_key_t local_csrk; 81 uint32_t local_counter; 82 #endif 83 84 } le_device_db_entry_t; 85 86 87 #ifndef NVM_NUM_DEVICE_DB_ENTRIES 88 #error "NVM_NUM_DEVICE_DB_ENTRIES not defined, please define in btstack_config.h" 89 #endif 90 91 #if NVM_NUM_DEVICE_DB_ENTRIES == 0 92 #error "NVM_NUM_DEVICE_DB_ENTRIES must not be 0, please update in btstack_config.h" 93 #endif 94 95 // only stores if entry present 96 static uint8_t entry_map[NVM_NUM_DEVICE_DB_ENTRIES]; 97 static uint32_t num_valid_entries; 98 99 static const btstack_tlv_t * le_device_db_tlv_btstack_tlv_impl; 100 static void * le_device_db_tlv_btstack_tlv_context; 101 102 static const char tag_0 = 'B'; 103 static const char tag_1 = 'T'; 104 static const char tag_2 = 'D'; 105 106 static uint32_t le_device_db_tlv_tag_for_index(uint8_t index){ 107 return (tag_0 << 24) | (tag_1 << 16) | (tag_2 << 8) | index; 108 } 109 110 // @returns success 111 // @param index = entry_pos 112 static int le_device_db_tlv_fetch(int index, le_device_db_entry_t * entry){ 113 if (index < 0 || index >= NVM_NUM_DEVICE_DB_ENTRIES){ 114 log_error("le_device_db_tlv_fetch called with invalid index %d", index); 115 return 0; 116 } 117 uint32_t tag = le_device_db_tlv_tag_for_index(index); 118 int size = le_device_db_tlv_btstack_tlv_impl->get_tag(le_device_db_tlv_btstack_tlv_context, tag, (uint8_t*) entry, sizeof(le_device_db_entry_t)); 119 return size == sizeof(le_device_db_entry_t); 120 } 121 122 // @returns success 123 // @param index = entry_pos 124 static int le_device_db_tlv_store(int index, le_device_db_entry_t * entry){ 125 if (index < 0 || index >= NVM_NUM_DEVICE_DB_ENTRIES){ 126 log_error("le_device_db_tlv_store called with invalid index %d", index); 127 return 0; 128 } 129 uint32_t tag = le_device_db_tlv_tag_for_index(index); 130 le_device_db_tlv_btstack_tlv_impl->store_tag(le_device_db_tlv_btstack_tlv_context, tag, (uint8_t*) entry, sizeof(le_device_db_entry_t)); 131 return 1; 132 } 133 134 // @param index = entry_pos 135 static int le_device_db_tlv_delete(int index){ 136 if (index < 0 || index >= NVM_NUM_DEVICE_DB_ENTRIES){ 137 log_error("le_device_db_tlv_delete called with invalid index %d", index); 138 return 0; 139 } 140 uint32_t tag = le_device_db_tlv_tag_for_index(index); 141 le_device_db_tlv_btstack_tlv_impl->delete_tag(le_device_db_tlv_btstack_tlv_context, tag); 142 return 1; 143 } 144 145 static void le_device_db_tlv_scan(void){ 146 int i; 147 num_valid_entries = 0; 148 memset(entry_map, 0, sizeof(entry_map)); 149 for (i=0;i<NVM_NUM_DEVICE_DB_ENTRIES;i++){ 150 // lookup entry 151 le_device_db_entry_t entry; 152 if (!le_device_db_tlv_fetch(i, &entry)) continue; 153 154 entry_map[i] = 1; 155 num_valid_entries++; 156 } 157 log_info("num valid le device entries %u", num_valid_entries); 158 } 159 160 void le_device_db_init(void){ 161 } 162 163 // not used 164 void le_device_db_set_local_bd_addr(bd_addr_t bd_addr){ 165 (void)bd_addr; 166 } 167 168 // @returns number of device in db 169 int le_device_db_count(void){ 170 return num_valid_entries; 171 } 172 173 int le_device_db_max_count(void){ 174 return NVM_NUM_DEVICE_DB_ENTRIES; 175 } 176 177 void le_device_db_remove(int index){ 178 // check if entry exists 179 if (entry_map[index] == 0) return; 180 181 // delete entry in TLV 182 le_device_db_tlv_delete(index); 183 184 // mark as unused 185 entry_map[index] = 0; 186 187 // keep track 188 num_valid_entries--; 189 } 190 191 int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){ 192 193 uint32_t highest_seq_nr = 0; 194 uint32_t lowest_seq_nr = 0xFFFFFFFF; 195 int index_for_lowest_seq_nr = -1; 196 int index_for_addr = -1; 197 int index_for_empty = -1; 198 199 // find unused entry in the used list 200 int i; 201 for (i=0;i<NVM_NUM_DEVICE_DB_ENTRIES;i++){ 202 if (entry_map[i]) { 203 le_device_db_entry_t entry; 204 le_device_db_tlv_fetch(i, &entry); 205 // found addr? 206 if ((memcmp(addr, entry.addr, 6) == 0) && addr_type == entry.addr_type){ 207 index_for_addr = i; 208 } 209 // update highest seq nr 210 if (entry.seq_nr > highest_seq_nr){ 211 highest_seq_nr = entry.seq_nr; 212 } 213 // find entry with lowest seq nr 214 if ((index_for_lowest_seq_nr == -1) || (entry.seq_nr < lowest_seq_nr)){ 215 index_for_lowest_seq_nr = i; 216 lowest_seq_nr = entry.seq_nr; 217 } 218 } else { 219 index_for_empty = i; 220 } 221 } 222 223 log_info("index_for_addr %x, index_for_empy %x, index_for_lowest_seq_nr %x", index_for_addr, index_for_empty, index_for_lowest_seq_nr); 224 225 uint32_t index_to_use = 0; 226 if (index_for_addr >= 0){ 227 index_to_use = index_for_addr; 228 } else if (index_for_empty >= 0){ 229 index_to_use = index_for_empty; 230 } else if (index_for_lowest_seq_nr >= 0){ 231 index_to_use = index_for_lowest_seq_nr; 232 } else { 233 // should not happen 234 return -1; 235 } 236 237 log_info("new entry for index %u", index_to_use); 238 239 // store entry at index 240 le_device_db_entry_t entry; 241 log_info("LE Device DB adding type %u - %s", addr_type, bd_addr_to_str(addr)); 242 log_info_key("irk", irk); 243 244 memset(&entry, 0, sizeof(le_device_db_entry_t)); 245 246 entry.addr_type = addr_type; 247 memcpy(entry.addr, addr, 6); 248 memcpy(entry.irk, irk, 16); 249 entry.seq_nr = highest_seq_nr + 1; 250 #ifdef ENABLE_LE_SIGNED_WRITE 251 entry.remote_counter = 0; 252 #endif 253 254 // store 255 le_device_db_tlv_store(index_to_use, &entry); 256 257 // set in entry_mape 258 entry_map[index_to_use] = 1; 259 260 // keep track - don't increase if old entry found 261 if (index_for_addr < 0){ 262 num_valid_entries++; 263 } 264 265 return index_to_use; 266 } 267 268 269 // get device information: addr type and address 270 void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk){ 271 272 // fetch entry 273 le_device_db_entry_t entry; 274 int ok = le_device_db_tlv_fetch(index, &entry); 275 if (!ok) return; 276 277 if (addr_type) *addr_type = entry.addr_type; 278 if (addr) memcpy(addr, entry.addr, 6); 279 if (irk) memcpy(irk, entry.irk, 16); 280 } 281 282 void le_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_key_t ltk, int key_size, int authenticated, int authorized){ 283 284 // fetch entry 285 le_device_db_entry_t entry; 286 int ok = le_device_db_tlv_fetch(index, &entry); 287 if (!ok) return; 288 289 // update 290 log_info("LE Device DB set encryption for %u, ediv x%04x, key size %u, authenticated %u, authorized %u", 291 index, ediv, key_size, authenticated, authorized); 292 entry.ediv = ediv; 293 if (rand) memcpy(entry.rand, rand, 8); 294 if (ltk) memcpy(entry.ltk, ltk, 16); 295 entry.key_size = key_size; 296 entry.authenticated = authenticated; 297 entry.authorized = authorized; 298 299 // store 300 le_device_db_tlv_store(index, &entry); 301 } 302 303 void le_device_db_encryption_get(int index, uint16_t * ediv, uint8_t rand[8], sm_key_t ltk, int * key_size, int * authenticated, int * authorized){ 304 305 // fetch entry 306 le_device_db_entry_t entry; 307 int ok = le_device_db_tlv_fetch(index, &entry); 308 if (!ok) return; 309 310 // update user fields 311 log_info("LE Device DB encryption for %u, ediv x%04x, keysize %u, authenticated %u, authorized %u", 312 index, entry.ediv, entry.key_size, entry.authenticated, entry.authorized); 313 if (ediv) *ediv = entry.ediv; 314 if (rand) memcpy(rand, entry.rand, 8); 315 if (ltk) memcpy(ltk, entry.ltk, 16); 316 if (key_size) *key_size = entry.key_size; 317 if (authenticated) *authenticated = entry.authenticated; 318 if (authorized) *authorized = entry.authorized; 319 } 320 321 #ifdef ENABLE_LE_SIGNED_WRITE 322 323 // get signature key 324 void le_device_db_remote_csrk_get(int index, sm_key_t csrk){ 325 326 // fetch entry 327 le_device_db_entry_t entry; 328 int ok = le_device_db_tlv_fetch(index, &entry); 329 if (!ok) return; 330 331 if (csrk) memcpy(csrk, entry.remote_csrk, 16); 332 } 333 334 void le_device_db_remote_csrk_set(int index, sm_key_t csrk){ 335 336 // fetch entry 337 le_device_db_entry_t entry; 338 int ok = le_device_db_tlv_fetch(index, &entry); 339 if (!ok) return; 340 341 if (!csrk) return; 342 343 // update 344 memcpy(entry.remote_csrk, csrk, 16); 345 346 // store 347 le_device_db_tlv_store(index, &entry); 348 } 349 350 void le_device_db_local_csrk_get(int index, sm_key_t csrk){ 351 352 // fetch entry 353 le_device_db_entry_t entry; 354 int ok = le_device_db_tlv_fetch(index, &entry); 355 if (!ok) return; 356 357 if (!csrk) return; 358 359 // fill 360 memcpy(csrk, entry.local_csrk, 16); 361 } 362 363 void le_device_db_local_csrk_set(int index, sm_key_t csrk){ 364 365 // fetch entry 366 le_device_db_entry_t entry; 367 int ok = le_device_db_tlv_fetch(index, &entry); 368 if (!ok) return; 369 370 if (!csrk) return; 371 372 // update 373 memcpy(entry.local_csrk, csrk, 16); 374 375 // store 376 le_device_db_tlv_store(index, &entry); 377 } 378 379 // query last used/seen signing counter 380 uint32_t le_device_db_remote_counter_get(int index){ 381 382 // fetch entry 383 le_device_db_entry_t entry; 384 int ok = le_device_db_tlv_fetch(index, &entry); 385 if (!ok) return 0; 386 387 return entry.remote_counter; 388 } 389 390 // update signing counter 391 void le_device_db_remote_counter_set(int index, uint32_t counter){ 392 393 // fetch entry 394 le_device_db_entry_t entry; 395 int ok = le_device_db_tlv_fetch(index, &entry); 396 if (!ok) return; 397 398 entry.remote_counter = counter; 399 400 // store 401 le_device_db_tlv_store(index, &entry); 402 } 403 404 // query last used/seen signing counter 405 uint32_t le_device_db_local_counter_get(int index){ 406 407 // fetch entry 408 le_device_db_entry_t entry; 409 int ok = le_device_db_tlv_fetch(index, &entry); 410 if (!ok) return 0; 411 412 return entry.local_counter; 413 } 414 415 // update signing counter 416 void le_device_db_local_counter_set(int index, uint32_t counter){ 417 418 // fetch entry 419 le_device_db_entry_t entry; 420 int ok = le_device_db_tlv_fetch(index, &entry); 421 if (!ok) return; 422 423 // update 424 entry.local_counter = counter; 425 426 // store 427 le_device_db_tlv_store(index, &entry); 428 } 429 430 #endif 431 432 void le_device_db_dump(void){ 433 log_info("LE Device DB dump, devices: %d", le_device_db_count()); 434 uint32_t i; 435 436 for (i=0;i<NVM_NUM_DEVICE_DB_ENTRIES;i++){ 437 if (!entry_map[i]) continue; 438 // fetch entry 439 le_device_db_entry_t entry; 440 le_device_db_tlv_fetch(i, &entry); 441 log_info("%u: %u %s", i, entry.addr_type, bd_addr_to_str(entry.addr)); 442 log_info_key("irk", entry.irk); 443 #ifdef ENABLE_LE_SIGNED_WRITE 444 log_info_key("local csrk", entry.local_csrk); 445 log_info_key("remote csrk", entry.remote_csrk); 446 #endif 447 } 448 } 449 450 void le_device_db_tlv_configure(const btstack_tlv_t * btstack_tlv_impl, void * btstack_tlv_context){ 451 le_device_db_tlv_btstack_tlv_impl = btstack_tlv_impl; 452 le_device_db_tlv_btstack_tlv_context = btstack_tlv_context; 453 le_device_db_tlv_scan(); 454 } 455