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 le_devices[index].addr_type = addr_type; 285 memcpy(le_devices[index].addr, addr, 6); 286 memcpy(le_devices[index].irk, irk, 16); 287 #ifdef ENABLE_LE_SIGNED_WRITE 288 le_devices[index].remote_counter = 0; 289 #endif 290 le_device_db_store(); 291 292 return index; 293 } 294 295 296 // get device information: addr type and address 297 void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk){ 298 if (addr_type) *addr_type = le_devices[index].addr_type; 299 if (addr) memcpy(addr, le_devices[index].addr, 6); 300 if (irk) memcpy(irk, le_devices[index].irk, 16); 301 } 302 303 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){ 304 log_info("Central Device DB set encryption for %u, ediv x%04x, key size %u, authenticated %u, authorized %u", 305 index, ediv, key_size, authenticated, authorized); 306 le_device_memory_db_t * device = &le_devices[index]; 307 device->ediv = ediv; 308 if (rand) memcpy(device->rand, rand, 8); 309 if (ltk) memcpy(device->ltk, ltk, 16); 310 device->key_size = key_size; 311 device->authenticated = authenticated; 312 device->authorized = authorized; 313 314 le_device_db_store(); 315 } 316 317 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){ 318 le_device_memory_db_t * device = &le_devices[index]; 319 log_info("Central Device DB encryption for %u, ediv x%04x, keysize %u, authenticated %u, authorized %u", 320 index, device->ediv, device->key_size, device->authenticated, device->authorized); 321 if (ediv) *ediv = device->ediv; 322 if (rand) memcpy(rand, device->rand, 8); 323 if (ltk) memcpy(ltk, device->ltk, 16); 324 if (key_size) *key_size = device->key_size; 325 if (authenticated) *authenticated = device->authenticated; 326 if (authorized) *authorized = device->authorized; 327 } 328 329 #ifdef ENABLE_LE_SIGNED_WRITE 330 331 // get signature key 332 void le_device_db_remote_csrk_get(int index, sm_key_t csrk){ 333 if (index < 0 || index >= LE_DEVICE_MEMORY_SIZE){ 334 log_error("le_device_db_remote_csrk_get called with invalid index %d", index); 335 return; 336 } 337 if (csrk) memcpy(csrk, le_devices[index].remote_csrk, 16); 338 } 339 340 void le_device_db_remote_csrk_set(int index, sm_key_t csrk){ 341 if (index < 0 || index >= LE_DEVICE_MEMORY_SIZE){ 342 log_error("le_device_db_remote_csrk_set called with invalid index %d", index); 343 return; 344 } 345 if (csrk) memcpy(le_devices[index].remote_csrk, csrk, 16); 346 347 le_device_db_store(); 348 } 349 350 void le_device_db_local_csrk_get(int index, sm_key_t csrk){ 351 if (index < 0 || index >= LE_DEVICE_MEMORY_SIZE){ 352 log_error("le_device_db_local_csrk_get called with invalid index %d", index); 353 return; 354 } 355 if (csrk) memcpy(csrk, le_devices[index].local_csrk, 16); 356 } 357 358 void le_device_db_local_csrk_set(int index, sm_key_t csrk){ 359 if (index < 0 || index >= LE_DEVICE_MEMORY_SIZE){ 360 log_error("le_device_db_local_csrk_set called with invalid index %d", index); 361 return; 362 } 363 if (csrk) memcpy(le_devices[index].local_csrk, csrk, 16); 364 365 le_device_db_store(); 366 } 367 368 // query last used/seen signing counter 369 uint32_t le_device_db_remote_counter_get(int index){ 370 return le_devices[index].remote_counter; 371 } 372 373 // update signing counter 374 void le_device_db_remote_counter_set(int index, uint32_t counter){ 375 le_devices[index].remote_counter = counter; 376 377 le_device_db_store(); 378 } 379 380 // query last used/seen signing counter 381 uint32_t le_device_db_local_counter_get(int index){ 382 return le_devices[index].local_counter; 383 } 384 385 // update signing counter 386 void le_device_db_local_counter_set(int index, uint32_t counter){ 387 le_devices[index].local_counter = counter; 388 389 le_device_db_store(); 390 } 391 #endif 392 393 void le_device_db_dump(void){ 394 log_info("Central Device DB dump, devices: %d", le_device_db_count()); 395 int i; 396 for (i=0;i<LE_DEVICE_MEMORY_SIZE;i++){ 397 if (le_devices[i].addr_type == INVALID_ENTRY_ADDR_TYPE) continue; 398 log_info("%u: %u %s", i, le_devices[i].addr_type, bd_addr_to_str(le_devices[i].addr)); 399 log_info_key("ltk", le_devices[i].ltk); 400 log_info_key("irk", le_devices[i].irk); 401 #ifdef ENABLE_LE_SIGNED_WRITE 402 log_info_key("local csrk", le_devices[i].local_csrk); 403 log_info_key("remote csrk", le_devices[i].remote_csrk); 404 #endif 405 } 406 } 407