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