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