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