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