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 BLUEKITCHEN 24 * GMBH 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_fs.c" 39 40 #include <stdio.h> 41 #include <string.h> 42 43 #include "btstack_config.h" 44 #include "btstack_debug.h" 45 #include "ble/le_device_db.h" 46 #include "ble/core.h" 47 48 // Central Device db implemenation using static memory 49 typedef struct le_device_memory_db { 50 51 // Identification 52 int addr_type; 53 bd_addr_t addr; 54 sm_key_t irk; 55 56 // Stored pairing information allows to re-establish an enncrypted connection 57 // with a peripheral that doesn't have any persistent memory 58 sm_key_t ltk; 59 uint16_t ediv; 60 uint8_t rand[8]; 61 uint8_t key_size; 62 uint8_t authenticated; 63 uint8_t authorized; 64 uint8_t secure_connection; 65 66 #ifdef ENABLE_LE_SIGNED_WRITE 67 // Signed Writes by remote 68 sm_key_t remote_csrk; 69 uint32_t remote_counter; 70 71 // Signed Writes by us 72 sm_key_t local_csrk; 73 uint32_t local_counter; 74 #endif 75 76 } le_device_memory_db_t; 77 78 #define LE_DEVICE_MEMORY_SIZE 20 79 80 #ifndef LE_DEVICE_DB_PATH 81 #ifdef _WIN32 82 #define LE_DEVICE_DB_PATH "" 83 #else 84 #define LE_DEVICE_DB_PATH "/tmp/" 85 #endif 86 #endif 87 88 #define DB_PATH_TEMPLATE (LE_DEVICE_DB_PATH "btstack_at_%s_le_device_db.txt") 89 90 #ifdef ENABLE_LE_SIGNED_WRITE 91 const char * csv_header = "# addr_type, addr, irk, ltk, ediv, rand[8], key_size, authenticated, authorized, remote_csrk, remote_counter, local_csrk, local_counter, secure_connection"; 92 #else 93 const char * csv_header = "# addr_type, addr, irk, ltk, ediv, rand[8], key_size, authenticated, authorized, secure_connection"; 94 #endif 95 96 static char db_path[sizeof(DB_PATH_TEMPLATE) - 2 + 17 + 1]; 97 98 static le_device_memory_db_t le_devices[LE_DEVICE_MEMORY_SIZE]; 99 100 static char * bd_addr_to_dash_str(bd_addr_t addr){ 101 return bd_addr_to_str_with_delimiter(addr, '-'); 102 } 103 104 static inline void write_delimiter(FILE * wFile){ 105 fwrite(",", 1, 1, wFile); 106 } 107 static inline void write_hex_byte(FILE * wFile, uint8_t value){ 108 char buffer[2]; 109 buffer[0] = char_for_nibble(value >> 4); 110 buffer[1] = char_for_nibble(value & 0x0f); 111 fwrite(buffer, 2, 1, wFile); 112 } 113 114 static inline void write_str(FILE * wFile, const char * str){ 115 fwrite(str, strlen(str), 1, wFile); 116 write_delimiter(wFile); 117 } 118 static void write_hex(FILE * wFile, const uint8_t * value, int len){ 119 int i; 120 for (i = 0; i < len; i++){ 121 write_hex_byte(wFile, value[i]); 122 } 123 write_delimiter(wFile); 124 } 125 static void write_value(FILE * wFile, uint32_t value, int len){ 126 switch (len){ 127 case 4: 128 write_hex_byte(wFile, value >> 24); 129 case 3: 130 write_hex_byte(wFile, value >> 16); 131 case 2: 132 write_hex_byte(wFile, value >> 8); 133 case 1: 134 default: 135 write_hex_byte(wFile, value); 136 } 137 write_delimiter(wFile); 138 } 139 140 static void le_device_db_store(void) { 141 int i; 142 // open file 143 FILE * wFile = fopen(db_path,"w+"); 144 if (wFile == NULL) return; 145 fwrite(csv_header, strlen(csv_header), 1, wFile); 146 fwrite("\n", 1, 1, wFile); 147 for (i=0;i<LE_DEVICE_MEMORY_SIZE;i++){ 148 if (le_devices[i].addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 149 write_value(wFile, le_devices[i].addr_type, 1); 150 write_str(wFile, bd_addr_to_str(le_devices[i].addr)); 151 write_hex(wFile, le_devices[i].irk, 16); 152 write_hex(wFile, le_devices[i].ltk, 16); 153 write_value(wFile, le_devices[i].ediv, 2); 154 write_hex(wFile, le_devices[i].rand, 8); 155 write_value(wFile, le_devices[i].key_size, 1); 156 write_value(wFile, le_devices[i].authenticated, 1); 157 write_value(wFile, le_devices[i].authorized, 1); 158 #ifdef ENABLE_LE_SIGNED_WRITE 159 write_hex(wFile, le_devices[i].remote_csrk, 16); 160 write_value(wFile, le_devices[i].remote_counter, 2); 161 write_hex(wFile, le_devices[i].local_csrk, 16); 162 write_value(wFile, le_devices[i].local_counter, 2); 163 #endif 164 write_value(wFile, le_devices[i].secure_connection, 1); 165 fwrite("\n", 1, 1, wFile); 166 } 167 fclose(wFile); 168 } 169 static void read_delimiter(FILE * wFile){ 170 fgetc(wFile); 171 } 172 173 static uint8_t read_hex_byte(FILE * wFile){ 174 int c = fgetc(wFile); 175 if (c == ':') { 176 c = fgetc(wFile); 177 } 178 int d = fgetc(wFile); 179 return nibble_for_char(c) << 4 | nibble_for_char(d); 180 } 181 182 static void read_hex(FILE * wFile, uint8_t * buffer, int len){ 183 int i; 184 for (i=0;i<len;i++){ 185 buffer[i] = read_hex_byte(wFile); 186 } 187 read_delimiter(wFile); 188 } 189 190 static uint32_t read_value(FILE * wFile, int len){ 191 uint32_t res = 0; 192 int i; 193 for (i=0;i<len;i++){ 194 res = res << 8 | read_hex_byte(wFile); 195 } 196 read_delimiter(wFile); 197 return res; 198 } 199 200 static void le_device_db_read(void){ 201 // open file 202 FILE * wFile = fopen(db_path,"r"); 203 if (wFile == NULL) return; 204 // skip header 205 while (true) { 206 int c = fgetc(wFile); 207 if (feof(wFile)) goto exit; 208 if (c == '\n') break; 209 } 210 // read entries 211 int i; 212 for (i=0 ; i<LE_DEVICE_MEMORY_SIZE ; i++){ 213 memset(&le_devices[i], 0, sizeof(le_device_memory_db_t)); 214 le_devices[i].addr_type = read_value(wFile, 1); 215 if (feof(wFile)){ 216 le_devices[i].addr_type = BD_ADDR_TYPE_UNKNOWN; 217 break; 218 } 219 read_hex(wFile, le_devices[i].addr, 6); 220 read_hex(wFile, le_devices[i].irk, 16); 221 read_hex(wFile, le_devices[i].ltk, 16); 222 le_devices[i].ediv = read_value(wFile, 2); 223 read_hex(wFile, le_devices[i].rand, 8); 224 le_devices[i].key_size = read_value(wFile, 1); 225 le_devices[i].authenticated = read_value(wFile, 1); 226 le_devices[i].authorized = read_value(wFile, 1); 227 #ifdef ENABLE_LE_SIGNED_WRITE 228 read_hex(wFile, le_devices[i].remote_csrk, 16); 229 le_devices[i].remote_counter = read_value(wFile, 2); 230 read_hex(wFile, le_devices[i].local_csrk, 16); 231 le_devices[i].local_counter = read_value(wFile, 2); 232 #endif 233 // read next character and secure connection field if hex nibble follows 234 // (if not, we just read the newline) 235 int c = fgetc(wFile); 236 if (nibble_for_char(c) >= 0){ 237 int d = fgetc(wFile); 238 le_devices[i].secure_connection = nibble_for_char(c) << 4 | nibble_for_char(d); 239 // read delimiter 240 read_delimiter(wFile); 241 // read newline 242 fgetc(wFile); 243 } 244 } 245 exit: 246 fclose(wFile); 247 } 248 249 void le_device_db_init(void){ 250 int i; 251 for (i=0;i<LE_DEVICE_MEMORY_SIZE;i++){ 252 le_devices[i].addr_type = BD_ADDR_TYPE_UNKNOWN; 253 } 254 sprintf(db_path, DB_PATH_TEMPLATE, "00-00-00-00-00-00"); 255 } 256 257 void le_device_db_set_local_bd_addr(bd_addr_t addr){ 258 sprintf(db_path, DB_PATH_TEMPLATE, bd_addr_to_dash_str(addr)); 259 log_info("le_device_db_fs: path %s", db_path); 260 le_device_db_read(); 261 le_device_db_dump(); 262 } 263 264 // @returns number of device in db 265 int le_device_db_count(void){ 266 int i; 267 int counter = 0; 268 for (i=0;i<LE_DEVICE_MEMORY_SIZE;i++){ 269 if (le_devices[i].addr_type != BD_ADDR_TYPE_UNKNOWN) counter++; 270 } 271 return counter; 272 } 273 274 int le_device_db_max_count(void){ 275 return LE_DEVICE_MEMORY_SIZE; 276 } 277 278 // free device 279 void le_device_db_remove(int index){ 280 le_devices[index].addr_type = BD_ADDR_TYPE_UNKNOWN; 281 le_device_db_store(); 282 } 283 284 int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){ 285 int i; 286 int index = -1; 287 for (i=0;i<LE_DEVICE_MEMORY_SIZE;i++){ 288 if (le_devices[i].addr_type == BD_ADDR_TYPE_UNKNOWN){ 289 index = i; 290 break; 291 } 292 } 293 294 if (index < 0) return -1; 295 296 log_info("Central Device DB adding type %u - %s", addr_type, bd_addr_to_str(addr)); 297 log_info_key("irk", irk); 298 299 memset(&le_devices[index], 0, sizeof(le_device_memory_db_t)); 300 301 le_devices[index].addr_type = addr_type; 302 memcpy(le_devices[index].addr, addr, 6); 303 memcpy(le_devices[index].irk, irk, 16); 304 #ifdef ENABLE_LE_SIGNED_WRITE 305 le_devices[index].remote_counter = 0; 306 #endif 307 le_device_db_store(); 308 309 return index; 310 } 311 312 313 // get device information: addr type and address 314 void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk){ 315 if (addr_type) *addr_type = le_devices[index].addr_type; 316 if (addr) memcpy(addr, le_devices[index].addr, 6); 317 if (irk) memcpy(irk, le_devices[index].irk, 16); 318 } 319 320 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, int secure_connection){ 321 log_info("LE Device DB set encryption for %u, ediv x%04x, key size %u, authenticated %u, authorized %u, secure connection %u", 322 index, ediv, key_size, authenticated, authorized, secure_connection); 323 le_device_memory_db_t * device = &le_devices[index]; 324 device->ediv = ediv; 325 if (rand) memcpy(device->rand, rand, 8); 326 if (ltk) memcpy(device->ltk, ltk, 16); 327 device->key_size = key_size; 328 device->authenticated = authenticated; 329 device->authorized = authorized; 330 device->secure_connection = secure_connection; 331 332 le_device_db_store(); 333 } 334 335 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, int * secure_connection){ 336 le_device_memory_db_t * device = &le_devices[index]; 337 log_info("LE Device DB encryption for %u, ediv x%04x, keysize %u, authenticated %u, authorized %u, secure connection %u", 338 index, device->ediv, device->key_size, device->authenticated, device->authorized, device->secure_connection); 339 if (ediv) *ediv = device->ediv; 340 if (rand) memcpy(rand, device->rand, 8); 341 if (ltk) memcpy(ltk, device->ltk, 16); 342 if (key_size) *key_size = device->key_size; 343 if (authenticated) *authenticated = device->authenticated; 344 if (authorized) *authorized = device->authorized; 345 if (secure_connection) *secure_connection = device->secure_connection; 346 } 347 348 #ifdef ENABLE_LE_SIGNED_WRITE 349 350 // get signature key 351 void le_device_db_remote_csrk_get(int index, sm_key_t csrk){ 352 if (index < 0 || index >= LE_DEVICE_MEMORY_SIZE){ 353 log_error("le_device_db_remote_csrk_get called with invalid index %d", index); 354 return; 355 } 356 if (csrk) memcpy(csrk, le_devices[index].remote_csrk, 16); 357 } 358 359 void le_device_db_remote_csrk_set(int index, sm_key_t csrk){ 360 if (index < 0 || index >= LE_DEVICE_MEMORY_SIZE){ 361 log_error("le_device_db_remote_csrk_set called with invalid index %d", index); 362 return; 363 } 364 if (csrk) memcpy(le_devices[index].remote_csrk, csrk, 16); 365 366 le_device_db_store(); 367 } 368 369 void le_device_db_local_csrk_get(int index, sm_key_t csrk){ 370 if (index < 0 || index >= LE_DEVICE_MEMORY_SIZE){ 371 log_error("le_device_db_local_csrk_get called with invalid index %d", index); 372 return; 373 } 374 if (csrk) memcpy(csrk, le_devices[index].local_csrk, 16); 375 } 376 377 void le_device_db_local_csrk_set(int index, sm_key_t csrk){ 378 if (index < 0 || index >= LE_DEVICE_MEMORY_SIZE){ 379 log_error("le_device_db_local_csrk_set called with invalid index %d", index); 380 return; 381 } 382 if (csrk) memcpy(le_devices[index].local_csrk, csrk, 16); 383 384 le_device_db_store(); 385 } 386 387 // query last used/seen signing counter 388 uint32_t le_device_db_remote_counter_get(int index){ 389 return le_devices[index].remote_counter; 390 } 391 392 // update signing counter 393 void le_device_db_remote_counter_set(int index, uint32_t counter){ 394 le_devices[index].remote_counter = counter; 395 396 le_device_db_store(); 397 } 398 399 // query last used/seen signing counter 400 uint32_t le_device_db_local_counter_get(int index){ 401 return le_devices[index].local_counter; 402 } 403 404 // update signing counter 405 void le_device_db_local_counter_set(int index, uint32_t counter){ 406 le_devices[index].local_counter = counter; 407 408 le_device_db_store(); 409 } 410 #endif 411 412 void le_device_db_dump(void){ 413 log_info("Central Device DB dump, devices: %d", le_device_db_count()); 414 int i; 415 for (i=0;i<LE_DEVICE_MEMORY_SIZE;i++){ 416 if (le_devices[i].addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 417 log_info("%u: %u %s", i, le_devices[i].addr_type, bd_addr_to_str(le_devices[i].addr)); 418 log_info_key("ltk", le_devices[i].ltk); 419 log_info_key("irk", le_devices[i].irk); 420 #ifdef ENABLE_LE_SIGNED_WRITE 421 log_info_key("local csrk", le_devices[i].local_csrk); 422 log_info_key("remote csrk", le_devices[i].remote_csrk); 423 #endif 424 } 425 } 426