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