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