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