xref: /btstack/src/ble/le_device_db_memory.c (revision f9f2075ceac5e9dc08e9abea437e43d733a3a0ea)
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 INVALID_ENTRY_ADDR_TYPE 0xff
76 
77 #ifndef MAX_NR_LE_DEVICE_DB_ENTRIES
78 #error "MAX_NR_LE_DEVICE_DB_ENTRIES not defined, please define in btstack_config.h"
79 #endif
80 
81 static le_device_memory_db_t le_devices[MAX_NR_LE_DEVICE_DB_ENTRIES];
82 
83 void le_device_db_init(void){
84     int i;
85     for (i=0;i<MAX_NR_LE_DEVICE_DB_ENTRIES;i++){
86         le_device_db_remove(i);
87     }
88 }
89 
90 void le_device_db_set_local_bd_addr(bd_addr_t bd_addr){
91     (void)bd_addr;
92 }
93 
94 // @returns number of device in db
95 int le_device_db_count(void){
96     int i;
97     int counter = 0;
98     for (i=0;i<MAX_NR_LE_DEVICE_DB_ENTRIES;i++){
99         if (le_devices[i].addr_type != INVALID_ENTRY_ADDR_TYPE) counter++;
100     }
101     return counter;
102 }
103 
104 // free device
105 void le_device_db_remove(int index){
106     le_devices[index].addr_type = INVALID_ENTRY_ADDR_TYPE;
107 }
108 
109 int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){
110     int i;
111     int index = -1;
112     for (i=0;i<MAX_NR_LE_DEVICE_DB_ENTRIES;i++){
113          if (le_devices[i].addr_type == INVALID_ENTRY_ADDR_TYPE){
114             index = i;
115             break;
116          }
117     }
118 
119     if (index < 0) return -1;
120 
121     log_info("Central Device DB adding type %u - %s", addr_type, bd_addr_to_str(addr));
122     log_info_key("irk", irk);
123 
124     le_devices[index].addr_type = addr_type;
125     memcpy(le_devices[index].addr, addr, 6);
126     memcpy(le_devices[index].irk, irk, 16);
127 #ifdef ENABLE_LE_SIGNED_WRITE
128     le_devices[index].remote_counter = 0;
129 #endif
130     return index;
131 }
132 
133 
134 // get device information: addr type and address
135 void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk){
136     if (addr_type) *addr_type = le_devices[index].addr_type;
137     if (addr) memcpy(addr, le_devices[index].addr, 6);
138     if (irk) memcpy(irk, le_devices[index].irk, 16);
139 }
140 
141 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){
142     log_info("Central Device DB set encryption for %u, ediv x%04x, key size %u, authenticated %u, authorized %u",
143         index, ediv, key_size, authenticated, authorized);
144     le_device_memory_db_t * device = &le_devices[index];
145     device->ediv = ediv;
146     if (rand) memcpy(device->rand, rand, 8);
147     if (ltk) memcpy(device->ltk, ltk, 16);
148     device->key_size = key_size;
149     device->authenticated = authenticated;
150     device->authorized = authorized;
151 }
152 
153 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){
154     le_device_memory_db_t * device = &le_devices[index];
155     log_info("Central Device DB encryption for %u, ediv x%04x, keysize %u, authenticated %u, authorized %u",
156         index, device->ediv, device->key_size, device->authenticated, device->authorized);
157     if (ediv) *ediv = device->ediv;
158     if (rand) memcpy(rand, device->rand, 8);
159     if (ltk)  memcpy(ltk, device->ltk, 16);
160     if (key_size) *key_size = device->key_size;
161     if (authenticated) *authenticated = device->authenticated;
162     if (authorized) *authorized = device->authorized;
163 }
164 
165 #ifdef ENABLE_LE_SIGNED_WRITE
166 
167 // get signature key
168 void le_device_db_remote_csrk_get(int index, sm_key_t csrk){
169     if (index < 0 || index >= MAX_NR_LE_DEVICE_DB_ENTRIES){
170         log_error("le_device_db_remote_csrk_get called with invalid index %d", index);
171         return;
172     }
173     if (csrk) memcpy(csrk, le_devices[index].remote_csrk, 16);
174 }
175 
176 void le_device_db_remote_csrk_set(int index, sm_key_t csrk){
177     if (index < 0 || index >= MAX_NR_LE_DEVICE_DB_ENTRIES){
178         log_error("le_device_db_remote_csrk_set called with invalid index %d", index);
179         return;
180     }
181     if (csrk) memcpy(le_devices[index].remote_csrk, csrk, 16);
182 }
183 
184 void le_device_db_local_csrk_get(int index, sm_key_t csrk){
185     if (index < 0 || index >= MAX_NR_LE_DEVICE_DB_ENTRIES){
186         log_error("le_device_db_local_csrk_get called with invalid index %d", index);
187         return;
188     }
189     if (csrk) memcpy(csrk, le_devices[index].local_csrk, 16);
190 }
191 
192 void le_device_db_local_csrk_set(int index, sm_key_t csrk){
193     if (index < 0 || index >= MAX_NR_LE_DEVICE_DB_ENTRIES){
194         log_error("le_device_db_local_csrk_set called with invalid index %d", index);
195         return;
196     }
197     if (csrk) memcpy(le_devices[index].local_csrk, csrk, 16);
198 }
199 
200 // query last used/seen signing counter
201 uint32_t le_device_db_remote_counter_get(int index){
202     return le_devices[index].remote_counter;
203 }
204 
205 // update signing counter
206 void le_device_db_remote_counter_set(int index, uint32_t counter){
207     le_devices[index].remote_counter = counter;
208 }
209 
210 // query last used/seen signing counter
211 uint32_t le_device_db_local_counter_get(int index){
212     return le_devices[index].local_counter;
213 }
214 
215 // update signing counter
216 void le_device_db_local_counter_set(int index, uint32_t counter){
217     le_devices[index].local_counter = counter;
218 }
219 
220 #endif
221 
222 void le_device_db_dump(void){
223     log_info("Central Device DB dump, devices: %d", le_device_db_count());
224     int i;
225     for (i=0;i<MAX_NR_LE_DEVICE_DB_ENTRIES;i++){
226         if (le_devices[i].addr_type == INVALID_ENTRY_ADDR_TYPE) continue;
227         log_info("%u: %u %s", i, le_devices[i].addr_type, bd_addr_to_str(le_devices[i].addr));
228         log_info_key("irk", le_devices[i].irk);
229 #ifdef ENABLE_LE_SIGNED_WRITE
230         log_info_key("local csrk", le_devices[i].local_csrk);
231         log_info_key("remote csrk", le_devices[i].remote_csrk);
232 #endif
233     }
234 }
235