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