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 | \
118    SMP_H7_SUPPORT_BIT)
119 
120 /* Secure Connections, no MITM, no Bonding */
121 #define SMP_AUTH_SC_ENC_ONLY (SMP_H7_SUPPORT_BIT | SMP_SC_SUPPORT_BIT)
122 
123 /* Secure Connections, MITM, Bonding */
124 #define SMP_AUTH_SC_MITM_GB \
125   (SMP_H7_SUPPORT_BIT | SMP_SC_SUPPORT_BIT | SMP_AUTH_YN_BIT | SMP_AUTH_BOND)
126 
127 typedef uint8_t tSMP_AUTH_REQ;
128 
129 typedef enum : uint8_t {
130   SMP_SEC_NONE = 0,
131   SMP_SEC_UNAUTHENTICATE = 1,
132   SMP_SEC_AUTHENTICATED = 2,
133 } tSMP_SEC_LEVEL;
134 
135 /* Maximum Encryption Key Size range */
136 #define SMP_ENCR_KEY_SIZE_MIN 7
137 #define SMP_ENCR_KEY_SIZE_MAX 16
138 
139 /* SMP key types */
140 enum tSMP_KEYS_BITMASK : uint8_t {
141   SMP_SEC_KEY_TYPE_ENC = (1 << 0),  /* encryption key */
142   SMP_SEC_KEY_TYPE_ID = (1 << 1),   /* identity key */
143   SMP_SEC_KEY_TYPE_CSRK = (1 << 2), /* peripheral CSRK */
144   SMP_SEC_KEY_TYPE_LK = (1 << 3),   /* BR/EDR link key */
145 };
146 typedef uint8_t tSMP_KEYS;
147 
148 constexpr tSMP_KEYS SMP_BR_SEC_DEFAULT_KEY =
149     (SMP_SEC_KEY_TYPE_ENC | SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK);
150 
151 /* default security key distribution value */
152 constexpr tSMP_KEYS SMP_SEC_DEFAULT_KEY =
153     (SMP_SEC_KEY_TYPE_ENC | SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK |
154      SMP_SEC_KEY_TYPE_LK);
155 
156 #define SMP_SC_KEY_OUT_OF_RANGE 5 /* out of range */
157 typedef uint8_t tSMP_SC_KEY_TYPE;
158 
159 /* data type for BTM_SP_IO_REQ_EVT */
160 typedef struct {
161   tSMP_IO_CAP io_cap;     /* local IO capabilities */
162   tSMP_OOB_FLAG oob_data; /* OOB data present (locally) for the peer device */
163   tSMP_AUTH_REQ auth_req; /* Authentication required (for local device) */
164   uint8_t max_key_size;   /* max encryption key size */
165   tSMP_KEYS init_keys;    /* initiator keys to be distributed */
166   tSMP_KEYS resp_keys;    /* responder keys */
167 } tSMP_IO_REQ;
168 
169 typedef struct {
170   tSMP_STATUS reason;
171   tSMP_SEC_LEVEL sec_level;
172   bool is_pair_cancel;
173   bool smp_over_br;
174 } tSMP_CMPL;
175 
176 typedef struct {
177   BT_OCTET32 x;
178   BT_OCTET32 y;
179 } tSMP_PUBLIC_KEY;
180 
181 /* the data associated with the info sent to the peer via OOB interface */
182 typedef struct {
183   bool present;
184   Octet16 randomizer;
185   Octet16 commitment;
186 
187   tBLE_BD_ADDR addr_sent_to;
188   BT_OCTET32 private_key_used; /* is used to calculate: */
189   /* publ_key_used = P-256(private_key_used, curve_p256.G) - send it to the */
190   /* other side */
191   /* dhkey = P-256(private_key_used, publ key rcvd from the other side) */
192   tSMP_PUBLIC_KEY publ_key_used; /* P-256(private_key_used, curve_p256.G) */
193 } tSMP_LOC_OOB_DATA;
194 
195 /* the data associated with the info received from the peer via OOB interface */
196 typedef struct {
197   bool present;
198   Octet16 randomizer;
199   Octet16 commitment;
200   tBLE_BD_ADDR addr_rcvd_from;
201 } tSMP_PEER_OOB_DATA;
202 
203 typedef struct {
204   tSMP_LOC_OOB_DATA loc_oob_data;
205   tSMP_PEER_OOB_DATA peer_oob_data;
206 } tSMP_SC_OOB_DATA;
207 
208 typedef union {
209   uint32_t passkey;
210   tSMP_IO_REQ io_req; /* IO request */
211   tSMP_CMPL cmplt;
212   tSMP_OOB_DATA_TYPE req_oob_type;
213   tSMP_LOC_OOB_DATA loc_oob_data;
214   RawAddress id_addr;
215 } tSMP_EVT_DATA;
216 
217 /* AES Encryption output */
218 typedef struct {
219   uint8_t status;
220   uint8_t param_len;
221   uint16_t opcode;
222   uint8_t param_buf[OCTET16_LEN];
223 } tSMP_ENC;
224 
225 /* Security Manager events - Called by the stack when Security Manager related
226  * events occur.*/
227 typedef tBTM_STATUS(tSMP_CALLBACK)(tSMP_EVT event, const RawAddress& bd_addr,
228                                    const tSMP_EVT_DATA* p_data);
229 /* Security Manager SIRK verification event - Called by the stack when Security
230  * Manager requires verification from CSIP.*/
231 typedef tBTM_STATUS(tSMP_SIRK_CALLBACK)(const RawAddress& bd_addr);
232 
233 namespace fmt {
234 template <>
235 struct formatter<tSMP_OOB_DATA_TYPE> : enum_formatter<tSMP_OOB_DATA_TYPE> {};
236 template <>
237 struct formatter<tSMP_SEC_LEVEL> : enum_formatter<tSMP_SEC_LEVEL> {};
238 template <>
239 struct formatter<tSMP_EVT> : enum_formatter<tSMP_EVT> {};
240 }  // namespace fmt
241 
242 #endif  // SMP_API_TYPES_H
243