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