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