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