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