1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at:
7  *
8  *  http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17 
18 #pragma once
19 
20 #include <bluetooth/log.h>
21 
22 #include <cstdint>
23 
24 #include "stack/include/bt_octets.h"
25 #include "stack/include/btm_api_types.h"  // tBTM_CMPL_CB
26 #include "stack/include/btm_ble_sec_api_types.h"
27 #include "stack/include/btm_sec_api_types.h"
28 #include "types/raw_address.h"
29 
30 /*
31  * Local device configuration
32  */
33 typedef struct {
34   BD_NAME bd_name;          /* local Bluetooth device name */
35   bool pin_type;            /* true if PIN type is fixed */
36   uint8_t pin_code_len;     /* Bonding information */
37   PIN_CODE pin_code;        /* PIN CODE if pin type is fixed */
38 } tBTM_CFG;
39 
40 /* Pairing State */
41 enum tBTM_PAIRING_STATE : uint8_t {
42   BTM_PAIR_STATE_IDLE, /* Idle                                         */
43   BTM_PAIR_STATE_GET_REM_NAME, /* Getting the remote name (to check for SM4) */
44   BTM_PAIR_STATE_WAIT_PIN_REQ, /* Started authentication, waiting for PIN req
45                                   (PIN is pre-fetched) */
46   BTM_PAIR_STATE_WAIT_LOCAL_PIN,       /* Waiting for local PIN code */
47   BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM, /* Waiting user 'yes' to numeric
48                                           confirmation   */
49   BTM_PAIR_STATE_KEY_ENTRY, /* Key entry state (we are a keyboard)          */
50   BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP, /* Waiting for local response to peer OOB
51                                         data  */
52   BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS, /* Waiting for local IO capabilities and OOB
53                                        data */
54   BTM_PAIR_STATE_INCOMING_SSP, /* Incoming SSP (got peer IO caps when idle) */
55   BTM_PAIR_STATE_WAIT_AUTH_COMPLETE, /* All done, waiting authentication
56                                         complete    */
57   BTM_PAIR_STATE_WAIT_DISCONNECT     /* Waiting to disconnect the ACL */
58 };
59 
60 #define BTM_PAIR_FLAGS_WE_STARTED_DD \
61   0x01 /* We want to do dedicated bonding              */
62 #define BTM_PAIR_FLAGS_PEER_STARTED_DD \
63   0x02 /* Peer initiated dedicated bonding             */
64 #define BTM_PAIR_FLAGS_DISC_WHEN_DONE 0x04 /* Disconnect when done     */
65 #define BTM_PAIR_FLAGS_PIN_REQD \
66   0x08 /* set this bit when pin_callback is called     */
67 #define BTM_PAIR_FLAGS_PRE_FETCH_PIN \
68   0x10 /* set this bit when pre-fetch pin     */
69 #define BTM_PAIR_FLAGS_REJECTED_CONNECT \
70   0x20 /* set this bit when rejected incoming connection  */
71 #define BTM_PAIR_FLAGS_WE_CANCEL_DD \
72   0x40 /* set this bit when cancelling a bonding procedure */
73 #define BTM_PAIR_FLAGS_LE_ACTIVE \
74   0x80 /* use this bit when SMP pairing is active */
75 
76 typedef struct {
77   bool is_mux;
78   RawAddress bd_addr;
79   uint16_t psm;
80   bool is_orig;
81   tBTM_SEC_CALLBACK* p_callback;
82   tSMP_SIRK_CALLBACK* p_sirk_callback;
83   void* p_ref_data;
84   uint16_t rfcomm_security_requirement;
85   tBT_TRANSPORT transport;
86   tBTM_BLE_SEC_ACT sec_act;
87 } tBTM_SEC_QUEUE_ENTRY;
88 
89 /* Define the Device Management control structure
90  */
91 typedef struct tBTM_SEC_DEVCB {
92   tBTM_CMPL_CB*
93       p_stored_link_key_cmpl_cb; /* Read/Write/Delete stored link key    */
94 
95   tBTM_BLE_LOCAL_ID_KEYS id_keys;   /* local BLE ID keys */
96   Octet16 ble_encryption_key_value; /* BLE encryption key */
97 
98   tBTM_IO_CAP loc_io_caps;    /* IO capability of the local device */
99   tBTM_AUTH_REQ loc_auth_req; /* the auth_req flag  */
100 } tBTM_SEC_DEVCB;
101 
102 /* security action for L2CAP COC channels */
103 #define BTM_SEC_OK 1
104 #define BTM_SEC_ENCRYPT 2         /* encrypt the link with current key */
105 #define BTM_SEC_ENCRYPT_NO_MITM 3 /* unauthenticated encryption or better */
106 #define BTM_SEC_ENCRYPT_MITM 4    /* authenticated encryption */
107 #define BTM_SEC_ENC_PENDING 5     /* wait for link encryption pending */
108 
109 typedef uint8_t tBTM_SEC_ACTION;
110 
111 namespace fmt {
112 template <>
113 struct formatter<tBTM_PAIRING_STATE> : enum_formatter<tBTM_PAIRING_STATE> {};
114 }  // namespace fmt
115