xref: /btstack/src/ble/le_device_db_memory.c (revision c37cd8f3d1350b92a2f66c31b2a5fcd75f8c91a4)
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_memory.c"
39 
40 #include "ble/le_device_db.h"
41 
42 #include "ble/core.h"
43 
44 #include <stdio.h>
45 #include <string.h>
46 #include "btstack_debug.h"
47 
48 // ignore if NVM_LE_DEVICE_DB_ENTRIES is defined
49 #ifndef NVM_NUM_DEVICE_DB_ENTRIES
50 
51 // LE Device db implemenation using static memory
52 typedef struct le_device_memory_db {
53 
54     // Identification
55     int addr_type;
56     bd_addr_t addr;
57     sm_key_t irk;
58 
59     // Stored pairing information allows to re-establish an enncrypted connection
60     // with a peripheral that doesn't have any persistent memory
61     sm_key_t ltk;
62     uint16_t ediv;
63     uint8_t  rand[8];
64     uint8_t  key_size;
65     uint8_t  authenticated;
66     uint8_t  authorized;
67 
68 #ifdef ENABLE_LE_SIGNED_WRITE
69     // Signed Writes by remote
70     sm_key_t remote_csrk;
71     uint32_t remote_counter;
72 
73     // Signed Writes by us
74     sm_key_t local_csrk;
75     uint32_t local_counter;
76 #endif
77 
78 } le_device_memory_db_t;
79 
80 #define INVALID_ENTRY_ADDR_TYPE 0xff
81 
82 #ifndef MAX_NR_LE_DEVICE_DB_ENTRIES
83 #error "MAX_NR_LE_DEVICE_DB_ENTRIES not defined, please define in btstack_config.h"
84 #endif
85 
86 static le_device_memory_db_t le_devices[MAX_NR_LE_DEVICE_DB_ENTRIES];
87 
88 void le_device_db_init(void){
89     int i;
90     for (i=0;i<MAX_NR_LE_DEVICE_DB_ENTRIES;i++){
91         le_device_db_remove(i);
92     }
93 }
94 
95 void le_device_db_set_local_bd_addr(bd_addr_t bd_addr){
96     (void)bd_addr;
97 }
98 
99 // @returns number of device in db
100 int le_device_db_count(void){
101     int i;
102     int counter = 0;
103     for (i=0;i<MAX_NR_LE_DEVICE_DB_ENTRIES;i++){
104         if (le_devices[i].addr_type != INVALID_ENTRY_ADDR_TYPE) counter++;
105     }
106     return counter;
107 }
108 
109 // free device
110 void le_device_db_remove(int index){
111     le_devices[index].addr_type = INVALID_ENTRY_ADDR_TYPE;
112 }
113 
114 int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){
115     int i;
116     int index = -1;
117     for (i=0;i<MAX_NR_LE_DEVICE_DB_ENTRIES;i++){
118          if (le_devices[i].addr_type == INVALID_ENTRY_ADDR_TYPE){
119             index = i;
120             break;
121          }
122     }
123 
124     if (index < 0) return -1;
125 
126     log_info("LE Device DB adding type %u - %s", addr_type, bd_addr_to_str(addr));
127     log_info_key("irk", irk);
128 
129     le_devices[index].addr_type = addr_type;
130     memcpy(le_devices[index].addr, addr, 6);
131     memcpy(le_devices[index].irk, irk, 16);
132 #ifdef ENABLE_LE_SIGNED_WRITE
133     le_devices[index].remote_counter = 0;
134 #endif
135     return index;
136 }
137 
138 
139 // get device information: addr type and address
140 void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk){
141     if (addr_type) *addr_type = le_devices[index].addr_type;
142     if (addr) memcpy(addr, le_devices[index].addr, 6);
143     if (irk) memcpy(irk, le_devices[index].irk, 16);
144 }
145 
146 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){
147     log_info("LE Device DB set encryption for %u, ediv x%04x, key size %u, authenticated %u, authorized %u",
148         index, ediv, key_size, authenticated, authorized);
149     le_device_memory_db_t * device = &le_devices[index];
150     device->ediv = ediv;
151     if (rand) memcpy(device->rand, rand, 8);
152     if (ltk) memcpy(device->ltk, ltk, 16);
153     device->key_size = key_size;
154     device->authenticated = authenticated;
155     device->authorized = authorized;
156 }
157 
158 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){
159     le_device_memory_db_t * device = &le_devices[index];
160     log_info("LE Device DB encryption for %u, ediv x%04x, keysize %u, authenticated %u, authorized %u",
161         index, device->ediv, device->key_size, device->authenticated, device->authorized);
162     if (ediv) *ediv = device->ediv;
163     if (rand) memcpy(rand, device->rand, 8);
164     if (ltk)  memcpy(ltk, device->ltk, 16);
165     if (key_size) *key_size = device->key_size;
166     if (authenticated) *authenticated = device->authenticated;
167     if (authorized) *authorized = device->authorized;
168 }
169 
170 #ifdef ENABLE_LE_SIGNED_WRITE
171 
172 // get signature key
173 void le_device_db_remote_csrk_get(int index, sm_key_t csrk){
174     if (index < 0 || index >= MAX_NR_LE_DEVICE_DB_ENTRIES){
175         log_error("le_device_db_remote_csrk_get called with invalid index %d", index);
176         return;
177     }
178     if (csrk) memcpy(csrk, le_devices[index].remote_csrk, 16);
179 }
180 
181 void le_device_db_remote_csrk_set(int index, sm_key_t csrk){
182     if (index < 0 || index >= MAX_NR_LE_DEVICE_DB_ENTRIES){
183         log_error("le_device_db_remote_csrk_set called with invalid index %d", index);
184         return;
185     }
186     if (csrk) memcpy(le_devices[index].remote_csrk, csrk, 16);
187 }
188 
189 void le_device_db_local_csrk_get(int index, sm_key_t csrk){
190     if (index < 0 || index >= MAX_NR_LE_DEVICE_DB_ENTRIES){
191         log_error("le_device_db_local_csrk_get called with invalid index %d", index);
192         return;
193     }
194     if (csrk) memcpy(csrk, le_devices[index].local_csrk, 16);
195 }
196 
197 void le_device_db_local_csrk_set(int index, sm_key_t csrk){
198     if (index < 0 || index >= MAX_NR_LE_DEVICE_DB_ENTRIES){
199         log_error("le_device_db_local_csrk_set called with invalid index %d", index);
200         return;
201     }
202     if (csrk) memcpy(le_devices[index].local_csrk, csrk, 16);
203 }
204 
205 // query last used/seen signing counter
206 uint32_t le_device_db_remote_counter_get(int index){
207     return le_devices[index].remote_counter;
208 }
209 
210 // update signing counter
211 void le_device_db_remote_counter_set(int index, uint32_t counter){
212     le_devices[index].remote_counter = counter;
213 }
214 
215 // query last used/seen signing counter
216 uint32_t le_device_db_local_counter_get(int index){
217     return le_devices[index].local_counter;
218 }
219 
220 // update signing counter
221 void le_device_db_local_counter_set(int index, uint32_t counter){
222     le_devices[index].local_counter = counter;
223 }
224 
225 #endif
226 
227 void le_device_db_dump(void){
228     log_info("LE Device DB dump, devices: %d", le_device_db_count());
229     int i;
230     for (i=0;i<MAX_NR_LE_DEVICE_DB_ENTRIES;i++){
231         if (le_devices[i].addr_type == INVALID_ENTRY_ADDR_TYPE) continue;
232         log_info("%u: %u %s", i, le_devices[i].addr_type, bd_addr_to_str(le_devices[i].addr));
233         log_info_key("irk", le_devices[i].irk);
234 #ifdef ENABLE_LE_SIGNED_WRITE
235         log_info_key("local csrk", le_devices[i].local_csrk);
236         log_info_key("remote csrk", le_devices[i].remote_csrk);
237 #endif
238     }
239 }
240 
241 #endif
242 
243