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