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