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