• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/logging.h>
20 #include <base/strings/string_number_conversions.h>
21 
22 #include <list>
23 
24 #include "gatt_int.h"
25 #include "stack/crypto_toolbox/crypto_toolbox.h"
26 #include "types/bluetooth/uuid.h"
27 
28 using bluetooth::Uuid;
29 
calculate_database_info_size(std::list<tGATT_SRV_LIST_ELEM> * lst_ptr)30 static size_t calculate_database_info_size(std::list<tGATT_SRV_LIST_ELEM>* lst_ptr) {
31   size_t len = 0;
32   auto srv_it = lst_ptr->begin();
33   for (; srv_it != lst_ptr->end(); srv_it++) {
34     auto attr_list = &srv_it->p_db->attr_list;
35     auto attr_it = attr_list->begin();
36     for (; attr_it != attr_list->end(); attr_it++) {
37       if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_PRI_SERVICE) ||
38           attr_it->uuid == Uuid::From16Bit(GATT_UUID_SEC_SERVICE)) {
39         // Service declaration (Handle + Type + Value)
40         len += 4 + gatt_build_uuid_to_stream_len(attr_it->p_value->uuid);
41       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_INCLUDE_SERVICE)){
42         // Included service declaration (Handle + Type + Value)
43         len += 8 + gatt_build_uuid_to_stream_len(attr_it->p_value->incl_handle.service_type);
44       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_DECLARE)) {
45         // Characteristic declaration (Handle + Type + Value)
46         len += 7 + gatt_build_uuid_to_stream_len((++attr_it)->uuid);
47       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_DESCRIPTION) ||
48                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_CLIENT_CONFIG) ||
49                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_SRVR_CONFIG) ||
50                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_PRESENT_FORMAT) ||
51                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_AGG_FORMAT)) {
52         // Descriptor (Handle + Type)
53         len += 4;
54       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_EXT_PROP)) {
55         // Descriptor for ext property (Handle + Type + Value)
56         len += 6;
57       }
58     }
59   }
60   return len;
61 }
62 
fill_database_info(std::list<tGATT_SRV_LIST_ELEM> * lst_ptr,uint8_t * p_data)63 static void fill_database_info(std::list<tGATT_SRV_LIST_ELEM>* lst_ptr, uint8_t* p_data) {
64   auto srv_it = lst_ptr->begin();
65   for (; srv_it != lst_ptr->end(); srv_it++) {
66     auto attr_list = &srv_it->p_db->attr_list;
67     auto attr_it = attr_list->begin();
68     for (; attr_it != attr_list->end(); attr_it++) {
69       if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_PRI_SERVICE) ||
70           attr_it->uuid == Uuid::From16Bit(GATT_UUID_SEC_SERVICE)) {
71         // Service declaration
72         UINT16_TO_STREAM(p_data, attr_it->handle);
73 
74         if (srv_it->is_primary) {
75           UINT16_TO_STREAM(p_data, GATT_UUID_PRI_SERVICE);
76         } else {
77           UINT16_TO_STREAM(p_data, GATT_UUID_SEC_SERVICE);
78         }
79 
80         gatt_build_uuid_to_stream(&p_data, attr_it->p_value->uuid);
81       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_INCLUDE_SERVICE)){
82         // Included service declaration
83         UINT16_TO_STREAM(p_data, attr_it->handle);
84         UINT16_TO_STREAM(p_data, GATT_UUID_INCLUDE_SERVICE);
85         UINT16_TO_STREAM(p_data, attr_it->p_value->incl_handle.s_handle);
86         UINT16_TO_STREAM(p_data, attr_it->p_value->incl_handle.e_handle);
87 
88         gatt_build_uuid_to_stream(&p_data, attr_it->p_value->incl_handle.service_type);
89       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_DECLARE)) {
90         // Characteristic declaration
91         UINT16_TO_STREAM(p_data, attr_it->handle);
92         UINT16_TO_STREAM(p_data, GATT_UUID_CHAR_DECLARE);
93         UINT8_TO_STREAM(p_data, attr_it->p_value->char_decl.property);
94         UINT16_TO_STREAM(p_data, attr_it->p_value->char_decl.char_val_handle);
95 
96         // Increment 1 to fetch characteristic uuid from value declaration attribute
97         gatt_build_uuid_to_stream(&p_data, (++attr_it)->uuid);
98       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_DESCRIPTION) ||
99                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_CLIENT_CONFIG) ||
100                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_SRVR_CONFIG) ||
101                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_PRESENT_FORMAT) ||
102                  attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_AGG_FORMAT)) {
103         // Descriptor
104         UINT16_TO_STREAM(p_data, attr_it->handle);
105         UINT16_TO_STREAM(p_data, attr_it->uuid.As16Bit());
106       } else if (attr_it->uuid == Uuid::From16Bit(GATT_UUID_CHAR_EXT_PROP)) {
107         // Descriptor
108         UINT16_TO_STREAM(p_data, attr_it->handle);
109         UINT16_TO_STREAM(p_data, attr_it->uuid.As16Bit());
110         UINT16_TO_STREAM(p_data, attr_it->p_value
111                                      ? attr_it->p_value->char_ext_prop
112                                      : 0x0000);
113       }
114     }
115   }
116 }
117 
gatts_calculate_database_hash(std::list<tGATT_SRV_LIST_ELEM> * lst_ptr)118 Octet16 gatts_calculate_database_hash(std::list<tGATT_SRV_LIST_ELEM>* lst_ptr) {
119   int len = calculate_database_info_size(lst_ptr);
120 
121   std::vector<uint8_t> serialized(len);
122   fill_database_info(lst_ptr, serialized.data());
123 
124   std::reverse(serialized.begin(), serialized.end());
125   Octet16 db_hash = crypto_toolbox::aes_cmac(Octet16{0}, serialized.data(),
126                                   serialized.size());
127   LOG(INFO) << __func__ << ": hash="
128            << base::HexEncode(db_hash.data(), db_hash.size());
129 
130   return db_hash;
131 }
132