1 /******************************************************************************
2  *
3  *  Copyright 2020 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include <base/strings/string_number_conversions.h>
20 #include <bluetooth/log.h>
21 
22 #include <list>
23 
24 #include "crypto_toolbox/crypto_toolbox.h"
25 #include "gatt_int.h"
26 #include "stack/include/bt_types.h"
27 #include "types/bluetooth/uuid.h"
28 
29 using bluetooth::Uuid;
30 using namespace bluetooth;
31 
calculate_database_info_size(std::list<tGATT_SRV_LIST_ELEM> * lst_ptr)32 static size_t calculate_database_info_size(std::list<tGATT_SRV_LIST_ELEM>* lst_ptr) {
33   size_t len = 0;
34   auto srv_it = lst_ptr->begin();
35   for (; srv_it != lst_ptr->end(); srv_it++) {
36     auto attr_list = &srv_it->p_db->attr_list;
37     auto attr_it = attr_list->begin();
38     for (; attr_it != attr_list->end(); attr_it++) {
39       if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_PRI_SERVICE) ||
40           attr_it->uuid == Uuid::From16Bit(GATT_UUID_SEC_SERVICE)) {
41         // Service declaration (Handle + Type + Value)
42         len += 4 + gatt_build_uuid_to_stream_len(attr_it->p_value->uuid);
43       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_INCLUDE_SERVICE)){
44         // Included service declaration (Handle + Type + Value)
45         len += 8 + gatt_build_uuid_to_stream_len(attr_it->p_value->incl_handle.service_type);
46       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_DECLARE)) {
47         // Characteristic declaration (Handle + Type + Value)
48         len += 7 + gatt_build_uuid_to_stream_len((++attr_it)->uuid);
49       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_DESCRIPTION) ||
50                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_CLIENT_CONFIG) ||
51                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_SRVR_CONFIG) ||
52                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_PRESENT_FORMAT) ||
53                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_AGG_FORMAT)) {
54         // Descriptor (Handle + Type)
55         len += 4;
56       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_EXT_PROP)) {
57         // Descriptor for ext property (Handle + Type + Value)
58         len += 6;
59       }
60     }
61   }
62   return len;
63 }
64 
fill_database_info(std::list<tGATT_SRV_LIST_ELEM> * lst_ptr,uint8_t * p_data)65 static void fill_database_info(std::list<tGATT_SRV_LIST_ELEM>* lst_ptr, uint8_t* p_data) {
66   auto srv_it = lst_ptr->begin();
67   for (; srv_it != lst_ptr->end(); srv_it++) {
68     auto attr_list = &srv_it->p_db->attr_list;
69     auto attr_it = attr_list->begin();
70     for (; attr_it != attr_list->end(); attr_it++) {
71       if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_PRI_SERVICE) ||
72           attr_it->uuid == Uuid::From16Bit(GATT_UUID_SEC_SERVICE)) {
73         // Service declaration
74         UINT16_TO_STREAM(p_data, attr_it->handle);
75 
76         if (srv_it->is_primary) {
77           UINT16_TO_STREAM(p_data, GATT_UUID_PRI_SERVICE);
78         } else {
79           UINT16_TO_STREAM(p_data, GATT_UUID_SEC_SERVICE);
80         }
81 
82         gatt_build_uuid_to_stream(&p_data, attr_it->p_value->uuid);
83       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_INCLUDE_SERVICE)){
84         // Included service declaration
85         UINT16_TO_STREAM(p_data, attr_it->handle);
86         UINT16_TO_STREAM(p_data, GATT_UUID_INCLUDE_SERVICE);
87         UINT16_TO_STREAM(p_data, attr_it->p_value->incl_handle.s_handle);
88         UINT16_TO_STREAM(p_data, attr_it->p_value->incl_handle.e_handle);
89 
90         gatt_build_uuid_to_stream(&p_data, attr_it->p_value->incl_handle.service_type);
91       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_DECLARE)) {
92         // Characteristic declaration
93         UINT16_TO_STREAM(p_data, attr_it->handle);
94         UINT16_TO_STREAM(p_data, GATT_UUID_CHAR_DECLARE);
95         UINT8_TO_STREAM(p_data, attr_it->p_value->char_decl.property);
96         UINT16_TO_STREAM(p_data, attr_it->p_value->char_decl.char_val_handle);
97 
98         // Increment 1 to fetch characteristic uuid from value declaration attribute
99         gatt_build_uuid_to_stream(&p_data, (++attr_it)->uuid);
100       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_DESCRIPTION) ||
101                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_CLIENT_CONFIG) ||
102                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_SRVR_CONFIG) ||
103                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_PRESENT_FORMAT) ||
104                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_AGG_FORMAT)) {
105         // Descriptor
106         UINT16_TO_STREAM(p_data, attr_it->handle);
107         UINT16_TO_STREAM(p_data, attr_it->uuid.As16Bit());
108       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_EXT_PROP)) {
109         // Descriptor
110         UINT16_TO_STREAM(p_data, attr_it->handle);
111         UINT16_TO_STREAM(p_data, attr_it->uuid.As16Bit());
112         UINT16_TO_STREAM(p_data, attr_it->p_value
113                                      ? attr_it->p_value->char_ext_prop
114                                      : 0x0000);
115       }
116     }
117   }
118 }
119 
gatts_calculate_database_hash(std::list<tGATT_SRV_LIST_ELEM> * lst_ptr)120 Octet16 gatts_calculate_database_hash(std::list<tGATT_SRV_LIST_ELEM>* lst_ptr) {
121   int len = calculate_database_info_size(lst_ptr);
122 
123   std::vector<uint8_t> serialized(len);
124   fill_database_info(lst_ptr, serialized.data());
125 
126   std::reverse(serialized.begin(), serialized.end());
127   Octet16 db_hash = crypto_toolbox::aes_cmac(Octet16{0}, serialized.data(),
128                                   serialized.size());
129   log::info("hash={}", base::HexEncode(db_hash.data(), db_hash.size()));
130 
131   return db_hash;
132 }
133