1 /******************************************************************************
2 *
3 * Copyright 1999-2012 Broadcom Corporation
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 #ifndef SMP_API_TYPES_H
20 #define SMP_API_TYPES_H
21
22 #include <bluetooth/log.h>
23
24 #include <cstdint>
25 #include <string>
26
27 #include "macros.h"
28 #include "stack/include/bt_octets.h"
29 #include "stack/include/btm_status.h"
30 #include "stack/include/smp_status.h"
31 #include "types/ble_address_with_type.h"
32 #include "types/raw_address.h"
33
34 /* SMP event type */
35 typedef enum : uint8_t {
36 SMP_EVT_NONE, /* Default no event */
37 SMP_IO_CAP_REQ_EVT, /* IO capability request event */
38 SMP_SEC_REQUEST_EVT, /* SMP pairing request */
39 SMP_PASSKEY_NOTIF_EVT, /* passkey notification event */
40 SMP_PASSKEY_REQ_EVT, /* passkey request event */
41 SMP_OOB_REQ_EVT, /* OOB request event */
42 SMP_NC_REQ_EVT, /* Numeric Comparison request event */
43 SMP_COMPLT_EVT, /* SMP complete event */
44 SMP_PEER_KEYPR_NOT_EVT, /* Peer keypress notification */
45
46 /* SC OOB request event (both local and peer OOB data can be expected in
47 * response) */
48 SMP_SC_OOB_REQ_EVT,
49 /* SC OOB local data set is created (as result of SMP_CrLocScOobData(...))
50 */
51 SMP_SC_LOC_OOB_DATA_UP_EVT,
52 SMP_UNUSED11,
53 SMP_BR_KEYS_REQ_EVT, /* SMP over BR keys request event */
54 SMP_UNUSED13,
55 SMP_CONSENT_REQ_EVT, /* Consent request event */
56 SMP_LE_ADDR_ASSOC_EVT, /* Identity address association event */
57 SMP_SIRK_VERIFICATION_REQ_EVT, /* SIRK verification request event */
58 } tSMP_EVT;
59
smp_evt_to_text(const tSMP_EVT evt)60 inline std::string smp_evt_to_text(const tSMP_EVT evt) {
61 switch (evt) {
62 CASE_RETURN_TEXT(SMP_EVT_NONE);
63 CASE_RETURN_TEXT(SMP_IO_CAP_REQ_EVT);
64 CASE_RETURN_TEXT(SMP_SEC_REQUEST_EVT);
65 CASE_RETURN_TEXT(SMP_PASSKEY_NOTIF_EVT);
66 CASE_RETURN_TEXT(SMP_PASSKEY_REQ_EVT);
67 CASE_RETURN_TEXT(SMP_OOB_REQ_EVT);
68 CASE_RETURN_TEXT(SMP_NC_REQ_EVT);
69 CASE_RETURN_TEXT(SMP_COMPLT_EVT);
70 CASE_RETURN_TEXT(SMP_PEER_KEYPR_NOT_EVT);
71 CASE_RETURN_TEXT(SMP_SC_OOB_REQ_EVT);
72 CASE_RETURN_TEXT(SMP_SC_LOC_OOB_DATA_UP_EVT);
73 CASE_RETURN_TEXT(SMP_UNUSED11);
74 CASE_RETURN_TEXT(SMP_BR_KEYS_REQ_EVT);
75 CASE_RETURN_TEXT(SMP_UNUSED13);
76 CASE_RETURN_TEXT(SMP_CONSENT_REQ_EVT);
77 CASE_RETURN_TEXT(SMP_LE_ADDR_ASSOC_EVT);
78 CASE_RETURN_TEXT(SMP_SIRK_VERIFICATION_REQ_EVT);
79 default:
80 return "UNKNOWN SMP EVENT";
81 }
82 }
83
84 /* Device IO capability */
85 #define SMP_IO_CAP_IO BTM_IO_CAP_IO /* DisplayYesNo */
86 #define SMP_IO_CAP_KBDISP BTM_IO_CAP_KBDISP /* Keyboard Display */
87 #define SMP_IO_CAP_MAX BTM_IO_CAP_MAX
88 typedef uint8_t tSMP_IO_CAP;
89
90 /* OOB data present or not */
91 enum { SMP_OOB_NONE, SMP_OOB_PRESENT, SMP_OOB_UNKNOWN };
92 typedef uint8_t tSMP_OOB_FLAG;
93
94 /* type of OOB data required from application */
95 typedef enum : uint8_t {
96 SMP_OOB_INVALID_TYPE,
97 SMP_OOB_PEER,
98 SMP_OOB_LOCAL,
99 SMP_OOB_BOTH,
100 } tSMP_OOB_DATA_TYPE;
101
102 enum : uint8_t {
103 SMP_AUTH_NO_BOND = 0x00,
104 /* no MITM, No Bonding, encryption only */
105 SMP_AUTH_NB_ENC_ONLY = 0x00, // (SMP_AUTH_MASK | BTM_AUTH_SP_NO)
106 SMP_AUTH_BOND = (1u << 0),
107 SMP_AUTH_UNUSED = (1u << 1),
108 /* SMP Authentication requirement */
109 SMP_AUTH_YN_BIT = (1u << 2),
110 SMP_SC_SUPPORT_BIT = (1u << 3),
111 SMP_KP_SUPPORT_BIT = (1u << 4),
112 SMP_H7_SUPPORT_BIT = (1u << 5),
113 };
114
115 #define SMP_AUTH_MASK \
116 (SMP_AUTH_BOND | SMP_AUTH_YN_BIT | SMP_SC_SUPPORT_BIT | SMP_KP_SUPPORT_BIT | SMP_H7_SUPPORT_BIT)
117
118 /* Secure Connections, no MITM, no Bonding */
119 #define SMP_AUTH_SC_ENC_ONLY (SMP_H7_SUPPORT_BIT | SMP_SC_SUPPORT_BIT)
120
121 /* Secure Connections, MITM, Bonding */
122 #define SMP_AUTH_SC_MITM_GB \
123 (SMP_H7_SUPPORT_BIT | SMP_SC_SUPPORT_BIT | SMP_AUTH_YN_BIT | SMP_AUTH_BOND)
124
125 typedef uint8_t tSMP_AUTH_REQ;
126
127 typedef enum : uint8_t {
128 SMP_SEC_NONE = 0,
129 SMP_SEC_UNAUTHENTICATE = 1,
130 SMP_SEC_AUTHENTICATED = 2,
131 } tSMP_SEC_LEVEL;
132
133 /* Maximum Encryption Key Size range */
134 #define SMP_ENCR_KEY_SIZE_MIN 7
135 #define SMP_ENCR_KEY_SIZE_MAX 16
136
137 /* SMP key types */
138 enum tSMP_KEYS_BITMASK : uint8_t {
139 SMP_SEC_KEY_TYPE_ENC = (1 << 0), /* encryption key */
140 SMP_SEC_KEY_TYPE_ID = (1 << 1), /* identity key */
141 SMP_SEC_KEY_TYPE_CSRK = (1 << 2), /* peripheral CSRK */
142 SMP_SEC_KEY_TYPE_LK = (1 << 3), /* BR/EDR link key */
143 };
144 typedef uint8_t tSMP_KEYS;
145
146 constexpr tSMP_KEYS SMP_BR_SEC_DEFAULT_KEY =
147 (SMP_SEC_KEY_TYPE_ENC | SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK);
148
149 /* default security key distribution value */
150 constexpr tSMP_KEYS SMP_SEC_DEFAULT_KEY =
151 (SMP_SEC_KEY_TYPE_ENC | SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK | SMP_SEC_KEY_TYPE_LK);
152
153 #define SMP_SC_KEY_OUT_OF_RANGE 5 /* out of range */
154 typedef uint8_t tSMP_SC_KEY_TYPE;
155
156 /* data type for BTM_SP_IO_REQ_EVT */
157 typedef struct {
158 tSMP_IO_CAP io_cap; /* local IO capabilities */
159 tSMP_OOB_FLAG oob_data; /* OOB data present (locally) for the peer device */
160 tSMP_AUTH_REQ auth_req; /* Authentication required (for local device) */
161 uint8_t max_key_size; /* max encryption key size */
162 tSMP_KEYS init_keys; /* initiator keys to be distributed */
163 tSMP_KEYS resp_keys; /* responder keys */
164 } tSMP_IO_REQ;
165
166 typedef struct {
167 tSMP_STATUS reason;
168 tSMP_SEC_LEVEL sec_level;
169 bool is_pair_cancel;
170 bool smp_over_br;
171 } tSMP_CMPL;
172
173 typedef struct {
174 BT_OCTET32 x;
175 BT_OCTET32 y;
176 } tSMP_PUBLIC_KEY;
177
178 /* the data associated with the info sent to the peer via OOB interface */
179 typedef struct {
180 bool present;
181 Octet16 randomizer;
182 Octet16 commitment;
183
184 tBLE_BD_ADDR addr_sent_to;
185 BT_OCTET32 private_key_used; /* is used to calculate: */
186 /* publ_key_used = P-256(private_key_used, curve_p256.G) - send it to the */
187 /* other side */
188 /* dhkey = P-256(private_key_used, publ key rcvd from the other side) */
189 tSMP_PUBLIC_KEY publ_key_used; /* P-256(private_key_used, curve_p256.G) */
190 } tSMP_LOC_OOB_DATA;
191
192 /* the data associated with the info received from the peer via OOB interface */
193 typedef struct {
194 bool present;
195 Octet16 randomizer;
196 Octet16 commitment;
197 tBLE_BD_ADDR addr_rcvd_from;
198 } tSMP_PEER_OOB_DATA;
199
200 typedef struct {
201 tSMP_LOC_OOB_DATA loc_oob_data;
202 tSMP_PEER_OOB_DATA peer_oob_data;
203 } tSMP_SC_OOB_DATA;
204
205 typedef union {
206 uint32_t passkey;
207 tSMP_IO_REQ io_req; /* IO request */
208 tSMP_CMPL cmplt;
209 tSMP_OOB_DATA_TYPE req_oob_type;
210 tSMP_LOC_OOB_DATA loc_oob_data;
211 tBLE_BD_ADDR id_addr_with_type;
212 } tSMP_EVT_DATA;
213
214 /* AES Encryption output */
215 typedef struct {
216 uint8_t status;
217 uint8_t param_len;
218 uint16_t opcode;
219 uint8_t param_buf[OCTET16_LEN];
220 } tSMP_ENC;
221
222 /* Security Manager events - Called by the stack when Security Manager related
223 * events occur.*/
224 typedef tBTM_STATUS(tSMP_CALLBACK)(tSMP_EVT event, const RawAddress& bd_addr,
225 tSMP_EVT_DATA* p_data);
226 /* Security Manager SIRK verification event - Called by the stack when Security
227 * Manager requires verification from CSIP.*/
228 typedef tBTM_STATUS(tSMP_SIRK_CALLBACK)(const RawAddress& bd_addr);
229
230 namespace std {
231 template <>
232 struct formatter<tSMP_OOB_DATA_TYPE> : enum_formatter<tSMP_OOB_DATA_TYPE> {};
233 template <>
234 struct formatter<tSMP_SEC_LEVEL> : enum_formatter<tSMP_SEC_LEVEL> {};
235 template <>
236 struct formatter<tSMP_EVT> : enum_formatter<tSMP_EVT> {};
237 } // namespace std
238
239 #endif // SMP_API_TYPES_H
240