The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 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 | * |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 17 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 18 | |
| 19 | /****************************************************************************** |
| 20 | * |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 21 | * This file contains functions for the SMP L2CAP utility functions |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 22 | * |
| 23 | ******************************************************************************/ |
| 24 | #include "bt_target.h" |
| 25 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 26 | #if (SMP_INCLUDED == TRUE) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 27 | |
| 28 | #include "bt_types.h" |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 29 | #include "bt_utils.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 30 | #include <string.h> |
| 31 | #include <ctype.h> |
| 32 | #include "hcidefs.h" |
| 33 | #include "btm_ble_api.h" |
| 34 | #include "l2c_api.h" |
| 35 | #include "l2c_int.h" |
| 36 | #include "smp_int.h" |
Chris Manton | 79ecab5 | 2014-10-31 14:54:51 -0700 | [diff] [blame] | 37 | #include "device/include/controller.h" |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 38 | #include "btm_int.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 39 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 40 | |
| 41 | extern fixed_queue_t *btu_general_alarm_queue; |
| 42 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 43 | #define SMP_PAIRING_REQ_SIZE 7 |
| 44 | #define SMP_CONFIRM_CMD_SIZE (BT_OCTET16_LEN + 1) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 45 | #define SMP_RAND_CMD_SIZE (BT_OCTET16_LEN + 1) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 46 | #define SMP_INIT_CMD_SIZE (BT_OCTET16_LEN + 1) |
| 47 | #define SMP_ENC_INFO_SIZE (BT_OCTET16_LEN + 1) |
| 48 | #define SMP_MASTER_ID_SIZE (BT_OCTET8_LEN + 2 + 1) |
| 49 | #define SMP_ID_INFO_SIZE (BT_OCTET16_LEN + 1) |
| 50 | #define SMP_ID_ADDR_SIZE (BD_ADDR_LEN + 1 + 1) |
| 51 | #define SMP_SIGN_INFO_SIZE (BT_OCTET16_LEN + 1) |
| 52 | #define SMP_PAIR_FAIL_SIZE 2 |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 53 | #define SMP_SECURITY_REQUEST_SIZE 2 |
| 54 | #define SMP_PAIR_PUBL_KEY_SIZE (1 /* opcode */ + (2*BT_OCTET32_LEN)) |
| 55 | #define SMP_PAIR_COMMITM_SIZE (1 /* opcode */ + BT_OCTET16_LEN /*Commitment*/) |
| 56 | #define SMP_PAIR_DHKEY_CHECK_SIZE (1 /* opcode */ + BT_OCTET16_LEN /*DHKey Check*/) |
| 57 | #define SMP_PAIR_KEYPR_NOTIF_SIZE (1 /* opcode */ + 1 /*Notif Type*/) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 58 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 59 | /* SMP command sizes per spec */ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 60 | static const uint8_t smp_cmd_size_per_spec[] = |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 61 | { |
| 62 | 0, |
| 63 | SMP_PAIRING_REQ_SIZE, /* 0x01: pairing request */ |
| 64 | SMP_PAIRING_REQ_SIZE, /* 0x02: pairing response */ |
| 65 | SMP_CONFIRM_CMD_SIZE, /* 0x03: pairing confirm */ |
| 66 | SMP_RAND_CMD_SIZE, /* 0x04: pairing random */ |
| 67 | SMP_PAIR_FAIL_SIZE, /* 0x05: pairing failed */ |
| 68 | SMP_ENC_INFO_SIZE, /* 0x06: encryption information */ |
| 69 | SMP_MASTER_ID_SIZE, /* 0x07: master identification */ |
| 70 | SMP_ID_INFO_SIZE, /* 0x08: identity information */ |
| 71 | SMP_ID_ADDR_SIZE, /* 0x09: identity address information */ |
| 72 | SMP_SIGN_INFO_SIZE, /* 0x0A: signing information */ |
| 73 | SMP_SECURITY_REQUEST_SIZE, /* 0x0B: security request */ |
| 74 | SMP_PAIR_PUBL_KEY_SIZE, /* 0x0C: pairing public key */ |
| 75 | SMP_PAIR_DHKEY_CHECK_SIZE, /* 0x0D: pairing dhkey check */ |
| 76 | SMP_PAIR_KEYPR_NOTIF_SIZE, /* 0x0E: pairing keypress notification */ |
| 77 | SMP_PAIR_COMMITM_SIZE /* 0x0F: pairing commitment */ |
| 78 | }; |
| 79 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 80 | static bool smp_parameter_unconditionally_valid(tSMP_CB *p_cb); |
| 81 | static bool smp_parameter_unconditionally_invalid(tSMP_CB *p_cb); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 82 | |
| 83 | /* type for SMP command length validation functions */ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 84 | typedef bool (*tSMP_CMD_LEN_VALID)(tSMP_CB *p_cb); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 85 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 86 | static bool smp_command_has_valid_fixed_length(tSMP_CB *p_cb); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 87 | |
| 88 | static const tSMP_CMD_LEN_VALID smp_cmd_len_is_valid[] = |
| 89 | { |
| 90 | smp_parameter_unconditionally_invalid, |
| 91 | smp_command_has_valid_fixed_length, /* 0x01: pairing request */ |
| 92 | smp_command_has_valid_fixed_length, /* 0x02: pairing response */ |
| 93 | smp_command_has_valid_fixed_length, /* 0x03: pairing confirm */ |
| 94 | smp_command_has_valid_fixed_length, /* 0x04: pairing random */ |
| 95 | smp_command_has_valid_fixed_length, /* 0x05: pairing failed */ |
| 96 | smp_command_has_valid_fixed_length, /* 0x06: encryption information */ |
| 97 | smp_command_has_valid_fixed_length, /* 0x07: master identification */ |
| 98 | smp_command_has_valid_fixed_length, /* 0x08: identity information */ |
| 99 | smp_command_has_valid_fixed_length, /* 0x09: identity address information */ |
| 100 | smp_command_has_valid_fixed_length, /* 0x0A: signing information */ |
| 101 | smp_command_has_valid_fixed_length, /* 0x0B: security request */ |
| 102 | smp_command_has_valid_fixed_length, /* 0x0C: pairing public key */ |
| 103 | smp_command_has_valid_fixed_length, /* 0x0D: pairing dhkey check */ |
| 104 | smp_command_has_valid_fixed_length, /* 0x0E: pairing keypress notification */ |
| 105 | smp_command_has_valid_fixed_length /* 0x0F: pairing commitment */ |
| 106 | }; |
| 107 | |
| 108 | /* type for SMP command parameter ranges validation functions */ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 109 | typedef bool (*tSMP_CMD_PARAM_RANGES_VALID)(tSMP_CB *p_cb); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 110 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 111 | static bool smp_pairing_request_response_parameters_are_valid(tSMP_CB *p_cb); |
| 112 | static bool smp_pairing_keypress_notification_is_valid(tSMP_CB *p_cb); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 113 | |
| 114 | static const tSMP_CMD_PARAM_RANGES_VALID smp_cmd_param_ranges_are_valid[] = |
| 115 | { |
| 116 | smp_parameter_unconditionally_invalid, |
| 117 | smp_pairing_request_response_parameters_are_valid, /* 0x01: pairing request */ |
| 118 | smp_pairing_request_response_parameters_are_valid, /* 0x02: pairing response */ |
| 119 | smp_parameter_unconditionally_valid, /* 0x03: pairing confirm */ |
| 120 | smp_parameter_unconditionally_valid, /* 0x04: pairing random */ |
| 121 | smp_parameter_unconditionally_valid, /* 0x05: pairing failed */ |
| 122 | smp_parameter_unconditionally_valid, /* 0x06: encryption information */ |
| 123 | smp_parameter_unconditionally_valid, /* 0x07: master identification */ |
| 124 | smp_parameter_unconditionally_valid, /* 0x08: identity information */ |
| 125 | smp_parameter_unconditionally_valid, /* 0x09: identity address information */ |
| 126 | smp_parameter_unconditionally_valid, /* 0x0A: signing information */ |
| 127 | smp_parameter_unconditionally_valid, /* 0x0B: security request */ |
| 128 | smp_parameter_unconditionally_valid, /* 0x0C: pairing public key */ |
| 129 | smp_parameter_unconditionally_valid, /* 0x0D: pairing dhkey check */ |
| 130 | smp_pairing_keypress_notification_is_valid, /* 0x0E: pairing keypress notification */ |
| 131 | smp_parameter_unconditionally_valid /* 0x0F: pairing commitment */ |
| 132 | }; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 133 | |
| 134 | /* type for action functions */ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 135 | typedef BT_HDR * (*tSMP_CMD_ACT)(uint8_t cmd_code, tSMP_CB *p_cb); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 136 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 137 | static BT_HDR *smp_build_pairing_cmd(uint8_t cmd_code, tSMP_CB *p_cb); |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 138 | static BT_HDR *smp_build_confirm_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 139 | static BT_HDR *smp_build_rand_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 140 | static BT_HDR *smp_build_pairing_fail(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 141 | static BT_HDR *smp_build_identity_info_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 142 | static BT_HDR *smp_build_encrypt_info_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 143 | static BT_HDR *smp_build_security_request(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 144 | static BT_HDR *smp_build_signing_info_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 145 | static BT_HDR *smp_build_master_id_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 146 | static BT_HDR *smp_build_id_addr_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 147 | static BT_HDR *smp_build_pair_public_key_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 148 | static BT_HDR *smp_build_pairing_commitment_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 149 | static BT_HDR *smp_build_pair_dhkey_check_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
| 150 | static BT_HDR *smp_build_pairing_keypress_notification_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 151 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 152 | static const tSMP_CMD_ACT smp_cmd_build_act[] = |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 153 | { |
| 154 | NULL, |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 155 | smp_build_pairing_cmd, /* 0x01: pairing request */ |
| 156 | smp_build_pairing_cmd, /* 0x02: pairing response */ |
| 157 | smp_build_confirm_cmd, /* 0x03: pairing confirm */ |
| 158 | smp_build_rand_cmd, /* 0x04: pairing random */ |
| 159 | smp_build_pairing_fail, /* 0x05: pairing failure */ |
| 160 | smp_build_encrypt_info_cmd, /* 0x06: encryption information */ |
| 161 | smp_build_master_id_cmd, /* 0x07: master identification */ |
| 162 | smp_build_identity_info_cmd, /* 0x08: identity information */ |
| 163 | smp_build_id_addr_cmd, /* 0x09: identity address information */ |
| 164 | smp_build_signing_info_cmd, /* 0x0A: signing information */ |
| 165 | smp_build_security_request, /* 0x0B: security request */ |
| 166 | smp_build_pair_public_key_cmd, /* 0x0C: pairing public key */ |
| 167 | smp_build_pair_dhkey_check_cmd, /* 0x0D: pairing DHKey check */ |
| 168 | smp_build_pairing_keypress_notification_cmd, /* 0x0E: pairing keypress notification */ |
| 169 | smp_build_pairing_commitment_cmd /* 0x0F: pairing commitment */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 170 | }; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 171 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 172 | static const uint8_t smp_association_table[2][SMP_IO_CAP_MAX][SMP_IO_CAP_MAX] = |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 173 | { |
| 174 | /* display only */ /* Display Yes/No */ /* keyboard only */ |
| 175 | /* No Input/Output */ /* keyboard display */ |
| 176 | |
| 177 | /* initiator */ |
| 178 | /* model = tbl[peer_io_caps][loc_io_caps] */ |
| 179 | /* Display Only */ |
| 180 | {{SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY, |
| 181 | SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}, |
| 182 | |
| 183 | /* Display Yes/No */ |
| 184 | {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY, |
| 185 | SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}, |
| 186 | |
| 187 | /* Keyboard only */ |
| 188 | {SMP_MODEL_KEY_NOTIF, SMP_MODEL_KEY_NOTIF, SMP_MODEL_PASSKEY, |
| 189 | SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}, |
| 190 | |
| 191 | /* No Input No Output */ |
| 192 | {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, |
| 193 | SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY}, |
| 194 | |
| 195 | /* keyboard display */ |
| 196 | {SMP_MODEL_KEY_NOTIF, SMP_MODEL_KEY_NOTIF, SMP_MODEL_PASSKEY, |
| 197 | SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}}, |
| 198 | |
| 199 | /* responder */ |
| 200 | /* model = tbl[loc_io_caps][peer_io_caps] */ |
| 201 | /* Display Only */ |
| 202 | {{SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF, |
| 203 | SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}, |
| 204 | |
| 205 | /* Display Yes/No */ |
| 206 | {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF, |
| 207 | SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}, |
| 208 | |
| 209 | /* keyboard only */ |
| 210 | {SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY, |
| 211 | SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}, |
| 212 | |
| 213 | /* No Input No Output */ |
| 214 | {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, |
| 215 | SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY}, |
| 216 | |
| 217 | /* keyboard display */ |
| 218 | {SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY, SMP_MODEL_KEY_NOTIF, |
| 219 | SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}} |
| 220 | }; |
| 221 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 222 | static const uint8_t smp_association_table_sc[2][SMP_IO_CAP_MAX][SMP_IO_CAP_MAX] = |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 223 | { |
| 224 | /* display only */ /* Display Yes/No */ /* keyboard only */ |
| 225 | /* No InputOutput */ /* keyboard display */ |
| 226 | |
| 227 | /* initiator */ |
| 228 | /* model = tbl[peer_io_caps][loc_io_caps] */ |
| 229 | |
| 230 | /* Display Only */ |
| 231 | {{SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_ENT, |
| 232 | SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_ENT}, |
| 233 | |
| 234 | /* Display Yes/No */ |
| 235 | {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP, SMP_MODEL_SEC_CONN_PASSKEY_ENT, |
| 236 | SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP}, |
| 237 | |
| 238 | /* keyboard only */ |
| 239 | {SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_PASSKEY_ENT, |
| 240 | SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_DISP}, |
| 241 | |
| 242 | /* No Input No Output */ |
| 243 | {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS, |
| 244 | SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS}, |
| 245 | |
| 246 | /* keyboard display */ |
| 247 | {SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_NUM_COMP, SMP_MODEL_SEC_CONN_PASSKEY_ENT, |
| 248 | SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP}}, |
| 249 | |
| 250 | /* responder */ |
| 251 | /* model = tbl[loc_io_caps][peer_io_caps] */ |
| 252 | |
| 253 | /* Display Only */ |
| 254 | {{SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_DISP, |
| 255 | SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_DISP}, |
| 256 | |
| 257 | /* Display Yes/No */ |
| 258 | {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP, SMP_MODEL_SEC_CONN_PASSKEY_DISP, |
| 259 | SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP}, |
| 260 | |
| 261 | /* keyboard only */ |
| 262 | {SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_PASSKEY_ENT, |
| 263 | SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_ENT}, |
| 264 | |
| 265 | /* No Input No Output */ |
| 266 | {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS, |
| 267 | SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS}, |
| 268 | |
| 269 | /* keyboard display */ |
| 270 | {SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_NUM_COMP, SMP_MODEL_SEC_CONN_PASSKEY_DISP, |
| 271 | SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP}} |
| 272 | }; |
| 273 | |
| 274 | static tSMP_ASSO_MODEL smp_select_legacy_association_model(tSMP_CB *p_cb); |
| 275 | static tSMP_ASSO_MODEL smp_select_association_model_secure_connections(tSMP_CB *p_cb); |
| 276 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 277 | /******************************************************************************* |
| 278 | ** |
| 279 | ** Function smp_send_msg_to_L2CAP |
| 280 | ** |
| 281 | ** Description Send message to L2CAP. |
| 282 | ** |
| 283 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 284 | bool smp_send_msg_to_L2CAP(BD_ADDR rem_bda, BT_HDR *p_toL2CAP) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 285 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 286 | uint16_t l2cap_ret; |
| 287 | uint16_t fixed_cid = L2CAP_SMP_CID; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 288 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 289 | if (smp_cb.smp_over_br) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 290 | { |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 291 | fixed_cid = L2CAP_SMP_BR_CID; |
| 292 | } |
| 293 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 294 | SMP_TRACE_EVENT("%s", __func__); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 295 | smp_cb.total_tx_unacked += 1; |
| 296 | |
| 297 | if ((l2cap_ret = L2CA_SendFixedChnlData (fixed_cid, rem_bda, p_toL2CAP)) == L2CAP_DW_FAILED) |
| 298 | { |
| 299 | smp_cb.total_tx_unacked -= 1; |
Sharvil Nanavati | b44cc59 | 2014-05-04 10:03:35 -0700 | [diff] [blame] | 300 | SMP_TRACE_ERROR("SMP failed to pass msg:0x%0x to L2CAP", |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 301 | *((uint8_t *)(p_toL2CAP + 1) + p_toL2CAP->offset)); |
| 302 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 303 | } |
| 304 | else |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 305 | return true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 306 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 307 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 308 | /******************************************************************************* |
| 309 | ** |
| 310 | ** Function smp_send_cmd |
| 311 | ** |
| 312 | ** Description send a SMP command on L2CAP channel. |
| 313 | ** |
| 314 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 315 | bool smp_send_cmd(uint8_t cmd_code, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 316 | { |
| 317 | BT_HDR *p_buf; |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 318 | bool sent = false; |
| 319 | uint8_t failure = SMP_PAIR_INTERNAL_ERR; |
Sharvil Nanavati | b44cc59 | 2014-05-04 10:03:35 -0700 | [diff] [blame] | 320 | SMP_TRACE_EVENT("smp_send_cmd on l2cap cmd_code=0x%x", cmd_code); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 321 | if ( cmd_code <= (SMP_OPCODE_MAX + 1 /* for SMP_OPCODE_PAIR_COMMITM */) && |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 322 | smp_cmd_build_act[cmd_code] != NULL) |
| 323 | { |
| 324 | p_buf = (*smp_cmd_build_act[cmd_code])(cmd_code, p_cb); |
| 325 | |
| 326 | if (p_buf != NULL && |
| 327 | smp_send_msg_to_L2CAP(p_cb->pairing_bda, p_buf)) |
| 328 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 329 | sent = true; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 330 | alarm_set_on_queue(p_cb->smp_rsp_timer_ent, |
| 331 | SMP_WAIT_FOR_RSP_TIMEOUT_MS, smp_rsp_timeout, |
| 332 | NULL, btu_general_alarm_queue); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | if (!sent) |
| 337 | { |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 338 | if (p_cb->smp_over_br) |
| 339 | { |
| 340 | smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &failure); |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &failure); |
| 345 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 346 | } |
| 347 | return sent; |
| 348 | } |
| 349 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 350 | /******************************************************************************* |
| 351 | ** |
| 352 | ** Function smp_rsp_timeout |
| 353 | ** |
| 354 | ** Description Called when SMP wait for SMP command response timer expires |
| 355 | ** |
| 356 | ** Returns void |
| 357 | ** |
| 358 | *******************************************************************************/ |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 359 | void smp_rsp_timeout(UNUSED_ATTR void *data) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 360 | { |
| 361 | tSMP_CB *p_cb = &smp_cb; |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 362 | uint8_t failure = SMP_RSP_TIMEOUT; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 363 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 364 | SMP_TRACE_EVENT("%s state:%d br_state:%d", __func__, p_cb->state, p_cb->br_state); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 365 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 366 | if (p_cb->smp_over_br) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 367 | { |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 368 | smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &failure); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 369 | } |
| 370 | else |
| 371 | { |
| 372 | smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &failure); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /******************************************************************************* |
| 377 | ** |
Jacky Cheung | 373d928 | 2016-05-17 13:42:43 -0700 | [diff] [blame] | 378 | ** Function smp_delayed_auth_complete_timeout |
| 379 | ** |
| 380 | ** Description Called when no pairing failed command received within timeout |
| 381 | ** period. |
| 382 | ** |
| 383 | ** Returns void |
| 384 | ** |
| 385 | *******************************************************************************/ |
| 386 | void smp_delayed_auth_complete_timeout(UNUSED_ATTR void *data) |
| 387 | { |
| 388 | /* |
| 389 | * Waited for potential pair failure. Send SMP_AUTH_CMPL_EVT if |
| 390 | * the state is still in bond pending. |
| 391 | */ |
| 392 | if (smp_get_state() == SMP_STATE_BOND_PENDING) |
| 393 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 394 | uint8_t reason = SMP_SUCCESS; |
Jacky Cheung | 373d928 | 2016-05-17 13:42:43 -0700 | [diff] [blame] | 395 | SMP_TRACE_EVENT("%s sending delayed auth complete.", __func__); |
| 396 | smp_sm_event(&smp_cb, SMP_AUTH_CMPL_EVT, &reason); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /******************************************************************************* |
| 401 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 402 | ** Function smp_build_pairing_req_cmd |
| 403 | ** |
| 404 | ** Description Build pairing request command. |
| 405 | ** |
| 406 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 407 | BT_HDR * smp_build_pairing_cmd(uint8_t cmd_code, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 408 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 409 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 410 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 411 | SMP_PAIRING_REQ_SIZE + L2CAP_MIN_OFFSET); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 412 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 413 | SMP_TRACE_EVENT("%s", __func__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 414 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 415 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 416 | UINT8_TO_STREAM(p, cmd_code); |
| 417 | UINT8_TO_STREAM(p, p_cb->local_io_capability); |
| 418 | UINT8_TO_STREAM(p, p_cb->loc_oob_flag); |
| 419 | UINT8_TO_STREAM(p, p_cb->loc_auth_req); |
| 420 | UINT8_TO_STREAM(p, p_cb->loc_enc_size); |
| 421 | UINT8_TO_STREAM(p, p_cb->local_i_key); |
| 422 | UINT8_TO_STREAM(p, p_cb->local_r_key); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 423 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 424 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 425 | /* 1B ERR_RSP op code + 1B cmd_op_code + 2B handle + 1B status */ |
| 426 | p_buf->len = SMP_PAIRING_REQ_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 427 | |
| 428 | return p_buf; |
| 429 | } |
| 430 | |
| 431 | /******************************************************************************* |
| 432 | ** |
| 433 | ** Function smp_build_confirm_cmd |
| 434 | ** |
| 435 | ** Description Build confirm request command. |
| 436 | ** |
| 437 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 438 | static BT_HDR * smp_build_confirm_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 439 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 440 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 441 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 442 | SMP_CONFIRM_CMD_SIZE + L2CAP_MIN_OFFSET); |
| 443 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 444 | SMP_TRACE_EVENT("%s", __func__); |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 445 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 446 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 447 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 448 | UINT8_TO_STREAM(p, SMP_OPCODE_CONFIRM); |
| 449 | ARRAY_TO_STREAM(p, p_cb->confirm, BT_OCTET16_LEN); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 450 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 451 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 452 | p_buf->len = SMP_CONFIRM_CMD_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 453 | |
| 454 | return p_buf; |
| 455 | } |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 456 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 457 | /******************************************************************************* |
| 458 | ** |
| 459 | ** Function smp_build_rand_cmd |
| 460 | ** |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 461 | ** Description Build Random command. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 462 | ** |
| 463 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 464 | static BT_HDR * smp_build_rand_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 465 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 466 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 467 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 468 | SMP_RAND_CMD_SIZE + L2CAP_MIN_OFFSET); |
| 469 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 470 | SMP_TRACE_EVENT("%s", __func__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 471 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 472 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 473 | UINT8_TO_STREAM(p, SMP_OPCODE_RAND); |
| 474 | ARRAY_TO_STREAM(p, p_cb->rand, BT_OCTET16_LEN); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 475 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 476 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 477 | p_buf->len = SMP_RAND_CMD_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 478 | |
| 479 | return p_buf; |
| 480 | } |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 481 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 482 | /******************************************************************************* |
| 483 | ** |
| 484 | ** Function smp_build_encrypt_info_cmd |
| 485 | ** |
| 486 | ** Description Build security information command. |
| 487 | ** |
| 488 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 489 | static BT_HDR * smp_build_encrypt_info_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 490 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 491 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 492 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 493 | SMP_ENC_INFO_SIZE + L2CAP_MIN_OFFSET); |
| 494 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 495 | SMP_TRACE_EVENT("%s", __func__); |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 496 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 497 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 498 | UINT8_TO_STREAM(p, SMP_OPCODE_ENCRYPT_INFO); |
| 499 | ARRAY_TO_STREAM(p, p_cb->ltk, BT_OCTET16_LEN); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 500 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 501 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 502 | p_buf->len = SMP_ENC_INFO_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 503 | |
| 504 | return p_buf; |
| 505 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 506 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 507 | /******************************************************************************* |
| 508 | ** |
| 509 | ** Function smp_build_master_id_cmd |
| 510 | ** |
| 511 | ** Description Build security information command. |
| 512 | ** |
| 513 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 514 | static BT_HDR * smp_build_master_id_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 515 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 516 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 517 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 518 | SMP_MASTER_ID_SIZE + L2CAP_MIN_OFFSET); |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 519 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 520 | SMP_TRACE_EVENT("%s", __func__); |
| 521 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 522 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 523 | UINT8_TO_STREAM(p, SMP_OPCODE_MASTER_ID); |
| 524 | UINT16_TO_STREAM(p, p_cb->ediv); |
| 525 | ARRAY_TO_STREAM(p, p_cb->enc_rand, BT_OCTET8_LEN); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 526 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 527 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 528 | p_buf->len = SMP_MASTER_ID_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 529 | |
| 530 | return p_buf; |
| 531 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 532 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 533 | /******************************************************************************* |
| 534 | ** |
| 535 | ** Function smp_build_identity_info_cmd |
| 536 | ** |
| 537 | ** Description Build identity information command. |
| 538 | ** |
| 539 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 540 | static BT_HDR * smp_build_identity_info_cmd(UNUSED_ATTR uint8_t cmd_code, |
| 541 | UNUSED_ATTR tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 542 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 543 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 544 | BT_OCTET16 irk; |
| 545 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 546 | SMP_ID_INFO_SIZE + L2CAP_MIN_OFFSET); |
| 547 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 548 | SMP_TRACE_EVENT("%s", __func__); |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 549 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 550 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 551 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 552 | BTM_GetDeviceIDRoot(irk); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 553 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 554 | UINT8_TO_STREAM(p, SMP_OPCODE_IDENTITY_INFO); |
| 555 | ARRAY_TO_STREAM(p, irk, BT_OCTET16_LEN); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 556 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 557 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 558 | p_buf->len = SMP_ID_INFO_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 559 | |
| 560 | return p_buf; |
| 561 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 562 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 563 | /******************************************************************************* |
| 564 | ** |
| 565 | ** Function smp_build_id_addr_cmd |
| 566 | ** |
| 567 | ** Description Build identity address information command. |
| 568 | ** |
| 569 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 570 | static BT_HDR * smp_build_id_addr_cmd(UNUSED_ATTR uint8_t cmd_code, |
| 571 | UNUSED_ATTR tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 572 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 573 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 574 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 575 | SMP_ID_ADDR_SIZE + L2CAP_MIN_OFFSET); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 576 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 577 | SMP_TRACE_EVENT("%s", __func__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 578 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 579 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 580 | UINT8_TO_STREAM(p, SMP_OPCODE_ID_ADDR); |
| 581 | UINT8_TO_STREAM(p, 0); |
| 582 | BDADDR_TO_STREAM(p, controller_get_interface()->get_address()->address); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 583 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 584 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 585 | p_buf->len = SMP_ID_ADDR_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 586 | |
| 587 | return p_buf; |
| 588 | } |
| 589 | |
| 590 | /******************************************************************************* |
| 591 | ** |
| 592 | ** Function smp_build_signing_info_cmd |
| 593 | ** |
| 594 | ** Description Build signing information command. |
| 595 | ** |
| 596 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 597 | static BT_HDR * smp_build_signing_info_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 598 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 599 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 600 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 601 | SMP_SIGN_INFO_SIZE + L2CAP_MIN_OFFSET); |
| 602 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 603 | SMP_TRACE_EVENT("%s", __func__); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 604 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 605 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 606 | UINT8_TO_STREAM(p, SMP_OPCODE_SIGN_INFO); |
| 607 | ARRAY_TO_STREAM(p, p_cb->csrk, BT_OCTET16_LEN); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 608 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 609 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 610 | p_buf->len = SMP_SIGN_INFO_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 611 | |
| 612 | return p_buf; |
| 613 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 614 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 615 | /******************************************************************************* |
| 616 | ** |
| 617 | ** Function smp_build_pairing_fail |
| 618 | ** |
| 619 | ** Description Build Pairing Fail command. |
| 620 | ** |
| 621 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 622 | static BT_HDR * smp_build_pairing_fail(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 623 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 624 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 625 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 626 | SMP_PAIR_FAIL_SIZE + L2CAP_MIN_OFFSET); |
| 627 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 628 | SMP_TRACE_EVENT("%s", __func__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 629 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 630 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 631 | UINT8_TO_STREAM(p, SMP_OPCODE_PAIRING_FAILED); |
| 632 | UINT8_TO_STREAM(p, p_cb->failure); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 633 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 634 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 635 | p_buf->len = SMP_PAIR_FAIL_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 636 | |
| 637 | return p_buf; |
| 638 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 639 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 640 | /******************************************************************************* |
| 641 | ** |
| 642 | ** Function smp_build_security_request |
| 643 | ** |
| 644 | ** Description Build security request command. |
| 645 | ** |
| 646 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 647 | static BT_HDR *smp_build_security_request(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 648 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 649 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 650 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 651 | 2 + L2CAP_MIN_OFFSET); |
| 652 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 653 | SMP_TRACE_EVENT("%s", __func__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 654 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 655 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 656 | UINT8_TO_STREAM(p, SMP_OPCODE_SEC_REQ); |
| 657 | UINT8_TO_STREAM(p, p_cb->loc_auth_req); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 658 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 659 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 660 | p_buf->len = SMP_SECURITY_REQUEST_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 661 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 662 | SMP_TRACE_EVENT("opcode=%d auth_req=0x%x",SMP_OPCODE_SEC_REQ, p_cb->loc_auth_req ); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 663 | |
| 664 | return p_buf; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /******************************************************************************* |
| 668 | ** |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 669 | ** Function smp_build_pair_public_key_cmd |
| 670 | ** |
| 671 | ** Description Build pairing public key command. |
| 672 | ** |
| 673 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 674 | static BT_HDR *smp_build_pair_public_key_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 675 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 676 | uint8_t *p; |
| 677 | uint8_t publ_key[2*BT_OCTET32_LEN]; |
| 678 | uint8_t *p_publ_key = publ_key; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 679 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 680 | SMP_PAIR_PUBL_KEY_SIZE + L2CAP_MIN_OFFSET); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 681 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 682 | SMP_TRACE_EVENT("%s", __func__); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 683 | |
| 684 | memcpy(p_publ_key, p_cb->loc_publ_key.x, BT_OCTET32_LEN); |
| 685 | memcpy(p_publ_key + BT_OCTET32_LEN, p_cb->loc_publ_key.y, BT_OCTET32_LEN); |
| 686 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 687 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 688 | UINT8_TO_STREAM(p, SMP_OPCODE_PAIR_PUBLIC_KEY); |
| 689 | ARRAY_TO_STREAM(p, p_publ_key, 2*BT_OCTET32_LEN); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 690 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 691 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 692 | p_buf->len = SMP_PAIR_PUBL_KEY_SIZE; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 693 | |
| 694 | return p_buf; |
| 695 | } |
| 696 | |
| 697 | /******************************************************************************* |
| 698 | ** |
| 699 | ** Function smp_build_pairing_commitment_cmd |
| 700 | ** |
| 701 | ** Description Build pairing commitment command. |
| 702 | ** |
| 703 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 704 | static BT_HDR *smp_build_pairing_commitment_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 705 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 706 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 707 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 708 | SMP_PAIR_COMMITM_SIZE + L2CAP_MIN_OFFSET); |
| 709 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 710 | SMP_TRACE_EVENT("%s", __func__); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 711 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 712 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 713 | UINT8_TO_STREAM(p, SMP_OPCODE_CONFIRM); |
| 714 | ARRAY_TO_STREAM(p, p_cb->commitment, BT_OCTET16_LEN); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 715 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 716 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 717 | p_buf->len = SMP_PAIR_COMMITM_SIZE; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 718 | |
| 719 | return p_buf; |
| 720 | } |
| 721 | |
| 722 | /******************************************************************************* |
| 723 | ** |
| 724 | ** Function smp_build_pair_dhkey_check_cmd |
| 725 | ** |
| 726 | ** Description Build pairing DHKey check command. |
| 727 | ** |
| 728 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 729 | static BT_HDR *smp_build_pair_dhkey_check_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 730 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 731 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 732 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 733 | SMP_PAIR_DHKEY_CHECK_SIZE + L2CAP_MIN_OFFSET); |
| 734 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 735 | SMP_TRACE_EVENT("%s", __func__); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 736 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 737 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 738 | UINT8_TO_STREAM(p, SMP_OPCODE_PAIR_DHKEY_CHECK); |
| 739 | ARRAY_TO_STREAM(p, p_cb->dhkey_check, BT_OCTET16_LEN); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 740 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 741 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 742 | p_buf->len = SMP_PAIR_DHKEY_CHECK_SIZE; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 743 | |
| 744 | return p_buf; |
| 745 | } |
| 746 | |
| 747 | /******************************************************************************* |
| 748 | ** |
| 749 | ** Function smp_build_pairing_keypress_notification_cmd |
| 750 | ** |
| 751 | ** Description Build keypress notification command. |
| 752 | ** |
| 753 | *******************************************************************************/ |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame^] | 754 | static BT_HDR * smp_build_pairing_keypress_notification_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 755 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 756 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 757 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 758 | SMP_PAIR_KEYPR_NOTIF_SIZE + L2CAP_MIN_OFFSET); |
| 759 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 760 | SMP_TRACE_EVENT("%s", __func__); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 761 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 762 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 763 | UINT8_TO_STREAM(p, SMP_OPCODE_PAIR_KEYPR_NOTIF); |
| 764 | UINT8_TO_STREAM(p, p_cb->local_keypress_notification); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 765 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 766 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 767 | p_buf->len = SMP_PAIR_KEYPR_NOTIF_SIZE; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 768 | |
| 769 | return p_buf; |
| 770 | } |
| 771 | |
| 772 | /******************************************************************************* |
| 773 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 774 | ** Function smp_convert_string_to_tk |
| 775 | ** |
| 776 | ** Description This function is called to convert a 6 to 16 digits numeric |
| 777 | ** character string into SMP TK. |
| 778 | ** |
| 779 | ** |
| 780 | ** Returns void |
| 781 | ** |
| 782 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 783 | void smp_convert_string_to_tk(BT_OCTET16 tk, uint32_t passkey) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 784 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 785 | uint8_t *p = tk; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 786 | tSMP_KEY key; |
Sharvil Nanavati | b44cc59 | 2014-05-04 10:03:35 -0700 | [diff] [blame] | 787 | SMP_TRACE_EVENT("smp_convert_string_to_tk"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 788 | UINT32_TO_STREAM(p, passkey); |
| 789 | |
| 790 | key.key_type = SMP_KEY_TYPE_TK; |
| 791 | key.p_data = tk; |
| 792 | |
| 793 | smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &key); |
| 794 | } |
| 795 | |
| 796 | /******************************************************************************* |
| 797 | ** |
| 798 | ** Function smp_mask_enc_key |
| 799 | ** |
| 800 | ** Description This function is called to mask off the encryption key based |
| 801 | ** on the maximum encryption key size. |
| 802 | ** |
| 803 | ** |
| 804 | ** Returns void |
| 805 | ** |
| 806 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 807 | void smp_mask_enc_key(uint8_t loc_enc_size, uint8_t * p_data) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 808 | { |
Sharvil Nanavati | b44cc59 | 2014-05-04 10:03:35 -0700 | [diff] [blame] | 809 | SMP_TRACE_EVENT("smp_mask_enc_key"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 810 | if (loc_enc_size < BT_OCTET16_LEN) |
| 811 | { |
| 812 | for (; loc_enc_size < BT_OCTET16_LEN; loc_enc_size ++) |
| 813 | * (p_data + loc_enc_size) = 0; |
| 814 | } |
| 815 | return; |
| 816 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 817 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 818 | /******************************************************************************* |
| 819 | ** |
| 820 | ** Function smp_xor_128 |
| 821 | ** |
| 822 | ** Description utility function to do an biteise exclusive-OR of two bit |
| 823 | ** strings of the length of BT_OCTET16_LEN. |
| 824 | ** |
| 825 | ** Returns void |
| 826 | ** |
| 827 | *******************************************************************************/ |
| 828 | void smp_xor_128(BT_OCTET16 a, BT_OCTET16 b) |
| 829 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 830 | uint8_t i, *aa = a, *bb = b; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 831 | |
Sharvil Nanavati | b44cc59 | 2014-05-04 10:03:35 -0700 | [diff] [blame] | 832 | SMP_TRACE_EVENT("smp_xor_128"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 833 | for (i = 0; i < BT_OCTET16_LEN; i++) |
| 834 | { |
| 835 | aa[i] = aa[i] ^ bb[i]; |
| 836 | } |
| 837 | } |
| 838 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 839 | /******************************************************************************* |
| 840 | ** |
| 841 | ** Function smp_cb_cleanup |
| 842 | ** |
| 843 | ** Description Clean up SMP control block |
| 844 | ** |
| 845 | ** Returns void |
| 846 | ** |
| 847 | *******************************************************************************/ |
| 848 | void smp_cb_cleanup(tSMP_CB *p_cb) |
| 849 | { |
| 850 | tSMP_CALLBACK *p_callback = p_cb->p_callback; |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 851 | uint8_t trace_level = p_cb->trace_level; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 852 | |
Sharvil Nanavati | b44cc59 | 2014-05-04 10:03:35 -0700 | [diff] [blame] | 853 | SMP_TRACE_EVENT("smp_cb_cleanup"); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 854 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 855 | alarm_free(p_cb->smp_rsp_timer_ent); |
Jacky Cheung | 373d928 | 2016-05-17 13:42:43 -0700 | [diff] [blame] | 856 | alarm_free(p_cb->delayed_auth_timer_ent); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 857 | memset(p_cb, 0, sizeof(tSMP_CB)); |
| 858 | p_cb->p_callback = p_callback; |
| 859 | p_cb->trace_level = trace_level; |
Pavlin Radoslavov | 98ae9b2 | 2016-01-22 13:13:45 -0800 | [diff] [blame] | 860 | p_cb->smp_rsp_timer_ent = alarm_new("smp.smp_rsp_timer_ent"); |
Jacky Cheung | 373d928 | 2016-05-17 13:42:43 -0700 | [diff] [blame] | 861 | p_cb->delayed_auth_timer_ent = alarm_new("smp.delayed_auth_timer_ent"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 862 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 863 | |
| 864 | /******************************************************************************* |
| 865 | ** |
| 866 | ** Function smp_remove_fixed_channel |
| 867 | ** |
| 868 | ** Description This function is called to remove the fixed channel |
| 869 | ** |
| 870 | ** Returns void |
| 871 | ** |
| 872 | *******************************************************************************/ |
| 873 | void smp_remove_fixed_channel(tSMP_CB *p_cb) |
| 874 | { |
| 875 | SMP_TRACE_DEBUG("%s", __func__); |
| 876 | |
| 877 | if (p_cb->smp_over_br) |
| 878 | L2CA_RemoveFixedChnl (L2CAP_SMP_BR_CID, p_cb->pairing_bda); |
| 879 | else |
| 880 | L2CA_RemoveFixedChnl (L2CAP_SMP_CID, p_cb->pairing_bda); |
| 881 | } |
| 882 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 883 | /******************************************************************************* |
| 884 | ** |
| 885 | ** Function smp_reset_control_value |
| 886 | ** |
| 887 | ** Description This function is called to reset the control block value when |
| 888 | ** pairing procedure finished. |
| 889 | ** |
| 890 | ** |
| 891 | ** Returns void |
| 892 | ** |
| 893 | *******************************************************************************/ |
| 894 | void smp_reset_control_value(tSMP_CB *p_cb) |
| 895 | { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 896 | SMP_TRACE_EVENT("%s", __func__); |
| 897 | |
| 898 | alarm_cancel(p_cb->smp_rsp_timer_ent); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 899 | p_cb->flags = 0; |
| 900 | /* set the link idle timer to drop the link when pairing is done |
| 901 | usually service discovery will follow authentication complete, to avoid |
| 902 | racing condition for a link down/up, set link idle timer to be |
| 903 | SMP_LINK_TOUT_MIN to guarantee SMP key exchange */ |
| 904 | L2CA_SetIdleTimeoutByBdAddr(p_cb->pairing_bda, SMP_LINK_TOUT_MIN, BT_TRANSPORT_LE); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 905 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 906 | /* We can tell L2CAP to remove the fixed channel (if it has one) */ |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 907 | smp_remove_fixed_channel(p_cb); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 908 | smp_cb_cleanup(p_cb); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 909 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 910 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 911 | /******************************************************************************* |
| 912 | ** |
| 913 | ** Function smp_proc_pairing_cmpl |
| 914 | ** |
| 915 | ** Description This function is called to process pairing complete |
| 916 | ** |
| 917 | ** |
| 918 | ** Returns void |
| 919 | ** |
| 920 | *******************************************************************************/ |
| 921 | void smp_proc_pairing_cmpl(tSMP_CB *p_cb) |
| 922 | { |
| 923 | tSMP_EVT_DATA evt_data = {0}; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 924 | tSMP_CALLBACK *p_callback = p_cb->p_callback; |
| 925 | BD_ADDR pairing_bda; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 926 | |
Sharvil Nanavati | b44cc59 | 2014-05-04 10:03:35 -0700 | [diff] [blame] | 927 | SMP_TRACE_DEBUG ("smp_proc_pairing_cmpl "); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 928 | |
| 929 | evt_data.cmplt.reason = p_cb->status; |
Chaojing Sun | e280553 | 2015-04-22 13:40:21 -0700 | [diff] [blame] | 930 | evt_data.cmplt.smp_over_br = p_cb->smp_over_br; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 931 | |
| 932 | if (p_cb->status == SMP_SUCCESS) |
| 933 | evt_data.cmplt.sec_level = p_cb->sec_level; |
| 934 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 935 | evt_data.cmplt.is_pair_cancel = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 936 | |
| 937 | if (p_cb->is_pair_cancel) |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 938 | evt_data.cmplt.is_pair_cancel = true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 939 | |
| 940 | |
Sharvil Nanavati | b44cc59 | 2014-05-04 10:03:35 -0700 | [diff] [blame] | 941 | SMP_TRACE_DEBUG ("send SMP_COMPLT_EVT reason=0x%0x sec_level=0x%0x", |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 942 | evt_data.cmplt.reason, |
| 943 | evt_data.cmplt.sec_level ); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 944 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 945 | memcpy (pairing_bda, p_cb->pairing_bda, BD_ADDR_LEN); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 946 | |
| 947 | smp_reset_control_value(p_cb); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 948 | |
| 949 | if (p_callback) |
| 950 | (*p_callback) (SMP_COMPLT_EVT, pairing_bda, &evt_data); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 953 | /******************************************************************************* |
| 954 | ** |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 955 | ** Function smp_command_has_invalid_parameters |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 956 | ** |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 957 | ** Description Checks if the received SMP command has invalid parameters i.e. |
| 958 | ** if the command length is valid and the command parameters are |
| 959 | ** inside specified range. |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 960 | ** It returns true if the command has invalid parameters. |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 961 | ** |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 962 | ** Returns true if the command has invalid parameters, false otherwise. |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 963 | ** |
| 964 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 965 | bool smp_command_has_invalid_parameters(tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 966 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 967 | uint8_t cmd_code = p_cb->rcvd_cmd_code; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 968 | |
| 969 | SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, cmd_code); |
| 970 | |
| 971 | if ((cmd_code > (SMP_OPCODE_MAX + 1 /* for SMP_OPCODE_PAIR_COMMITM */)) || |
| 972 | (cmd_code < SMP_OPCODE_MIN)) |
| 973 | { |
| 974 | SMP_TRACE_WARNING("Somehow received command with the RESERVED code 0x%02x", cmd_code); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 975 | return true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | if (!(*smp_cmd_len_is_valid[cmd_code])(p_cb)) |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 979 | return true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 980 | |
| 981 | if (!(*smp_cmd_param_ranges_are_valid[cmd_code])(p_cb)) |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 982 | return true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 983 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 984 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /******************************************************************************* |
| 988 | ** |
| 989 | ** Function smp_command_has_valid_fixed_length |
| 990 | ** |
| 991 | ** Description Checks if the received command size is equal to the size |
| 992 | ** according to specs. |
| 993 | ** |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 994 | ** Returns true if the command size is as expected, false otherwise. |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 995 | ** |
| 996 | ** Note The command is expected to have fixed length. |
| 997 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 998 | bool smp_command_has_valid_fixed_length(tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 999 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1000 | uint8_t cmd_code = p_cb->rcvd_cmd_code; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1001 | |
| 1002 | SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, cmd_code); |
| 1003 | |
| 1004 | if (p_cb->rcvd_cmd_len != smp_cmd_size_per_spec[cmd_code]) |
| 1005 | { |
| 1006 | SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with invalid length\ |
| 1007 | 0x%02x (per spec the length is 0x%02x).", |
| 1008 | cmd_code, p_cb->rcvd_cmd_len, smp_cmd_size_per_spec[cmd_code]); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1009 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1012 | return true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | /******************************************************************************* |
| 1016 | ** |
| 1017 | ** Function smp_pairing_request_response_parameters_are_valid |
| 1018 | ** |
| 1019 | ** Description Validates parameter ranges in the received SMP command |
| 1020 | ** pairing request or pairing response. |
| 1021 | ** The parameters to validate: |
| 1022 | ** IO capability, |
| 1023 | ** OOB data flag, |
| 1024 | ** Bonding_flags in AuthReq |
| 1025 | ** Maximum encryption key size. |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1026 | ** Returns false if at least one of these parameters is out of range. |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1027 | ** |
| 1028 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1029 | bool smp_pairing_request_response_parameters_are_valid(tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1030 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1031 | uint8_t io_caps = p_cb->peer_io_caps; |
| 1032 | uint8_t oob_flag = p_cb->peer_oob_flag; |
| 1033 | uint8_t bond_flag = p_cb->peer_auth_req & 0x03; //0x03 is gen bond with appropriate mask |
| 1034 | uint8_t enc_size = p_cb->peer_enc_size; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1035 | |
| 1036 | SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, p_cb->rcvd_cmd_code); |
| 1037 | |
| 1038 | if (io_caps >= BTM_IO_CAP_MAX) |
| 1039 | { |
| 1040 | SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with IO Capabilty \ |
| 1041 | value (0x%02x) out of range).", |
| 1042 | p_cb->rcvd_cmd_code, io_caps); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1043 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | if (!((oob_flag == SMP_OOB_NONE) || (oob_flag == SMP_OOB_PRESENT))) |
| 1047 | { |
| 1048 | SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with OOB data flag value \ |
| 1049 | (0x%02x) out of range).", |
| 1050 | p_cb->rcvd_cmd_code, oob_flag); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1051 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | if (!((bond_flag == SMP_AUTH_NO_BOND) || (bond_flag == SMP_AUTH_BOND))) |
| 1055 | { |
| 1056 | SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Bonding_Flags value (0x%02x)\ |
| 1057 | out of range).", |
| 1058 | p_cb->rcvd_cmd_code, bond_flag); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1059 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | if ((enc_size < SMP_ENCR_KEY_SIZE_MIN) || (enc_size > SMP_ENCR_KEY_SIZE_MAX)) |
| 1063 | { |
| 1064 | SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Maximum Encryption \ |
| 1065 | Key value (0x%02x) out of range).", |
| 1066 | p_cb->rcvd_cmd_code, enc_size); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1067 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1070 | return true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /******************************************************************************* |
| 1074 | ** |
| 1075 | ** Function smp_pairing_keypress_notification_is_valid |
| 1076 | ** |
| 1077 | ** Description Validates Notification Type parameter range in the received SMP command |
| 1078 | ** pairing keypress notification. |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1079 | ** Returns false if this parameter is out of range. |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1080 | ** |
| 1081 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1082 | bool smp_pairing_keypress_notification_is_valid(tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1083 | { |
| 1084 | tBTM_SP_KEY_TYPE keypress_notification = p_cb->peer_keypress_notification; |
| 1085 | |
| 1086 | SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, p_cb->rcvd_cmd_code); |
| 1087 | |
| 1088 | if (keypress_notification >= BTM_SP_KEY_OUT_OF_RANGE) |
| 1089 | { |
| 1090 | SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Pairing Keypress \ |
| 1091 | Notification value (0x%02x) out of range).", |
| 1092 | p_cb->rcvd_cmd_code, keypress_notification); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1093 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1096 | return true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /******************************************************************************* |
| 1100 | ** |
| 1101 | ** Function smp_parameter_unconditionally_valid |
| 1102 | ** |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1103 | ** Description Always returns true. |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1104 | ** |
| 1105 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1106 | bool smp_parameter_unconditionally_valid(UNUSED_ATTR tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1107 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1108 | return true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /******************************************************************************* |
| 1112 | ** |
| 1113 | ** Function smp_parameter_unconditionally_invalid |
| 1114 | ** |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1115 | ** Description Always returns false. |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1116 | ** |
| 1117 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1118 | bool smp_parameter_unconditionally_invalid(UNUSED_ATTR tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1119 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1120 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /******************************************************************************* |
| 1124 | ** |
| 1125 | ** Function smp_reject_unexpected_pairing_command |
| 1126 | ** |
| 1127 | ** Description send pairing failure to an unexpected pairing command during |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 1128 | ** an active pairing process. |
| 1129 | ** |
| 1130 | ** Returns void |
| 1131 | ** |
| 1132 | *******************************************************************************/ |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1133 | void smp_reject_unexpected_pairing_command(BD_ADDR bd_addr) |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 1134 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1135 | uint8_t *p; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 1136 | BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + |
| 1137 | SMP_PAIR_FAIL_SIZE + L2CAP_MIN_OFFSET); |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 1138 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 1139 | SMP_TRACE_DEBUG("%s", __func__); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1140 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1141 | p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET; |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 1142 | UINT8_TO_STREAM(p, SMP_OPCODE_PAIRING_FAILED); |
| 1143 | UINT8_TO_STREAM(p, SMP_PAIR_NOT_SUPPORT); |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 1144 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 1145 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 1146 | p_buf->len = SMP_PAIR_FAIL_SIZE; |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 1147 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 1148 | smp_send_msg_to_L2CAP(bd_addr, p_buf); |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1151 | /******************************************************************************* |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1152 | ** Function smp_select_association_model |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1153 | ** |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1154 | ** Description This function selects association model to use for STK |
| 1155 | ** generation. Selection is based on both sides' io capability, |
| 1156 | ** oob data flag and authentication request. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1157 | ** |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1158 | ** Note If Secure Connections Only mode is required locally then we |
| 1159 | ** come to this point only if both sides support Secure Connections |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1160 | ** mode, i.e. if p_cb->secure_connections_only_mode_required = true then we come |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1161 | ** to this point only if |
| 1162 | ** (p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) == |
| 1163 | ** (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT) == |
| 1164 | ** SMP_SC_SUPPORT_BIT |
| 1165 | ** |
| 1166 | *******************************************************************************/ |
| 1167 | tSMP_ASSO_MODEL smp_select_association_model(tSMP_CB *p_cb) |
| 1168 | { |
| 1169 | tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE; |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1170 | p_cb->le_secure_connections_mode_is_used = false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1171 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1172 | SMP_TRACE_EVENT("%s", __func__); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1173 | SMP_TRACE_DEBUG("%s p_cb->peer_io_caps = %d p_cb->local_io_capability = %d", |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1174 | __func__, p_cb->peer_io_caps, p_cb->local_io_capability); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1175 | SMP_TRACE_DEBUG("%s p_cb->peer_oob_flag = %d p_cb->loc_oob_flag = %d", |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1176 | __func__, p_cb->peer_oob_flag, p_cb->loc_oob_flag); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1177 | SMP_TRACE_DEBUG("%s p_cb->peer_auth_req = 0x%02x p_cb->loc_auth_req = 0x%02x", |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1178 | __func__, p_cb->peer_auth_req, p_cb->loc_auth_req); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1179 | SMP_TRACE_DEBUG("%s p_cb->secure_connections_only_mode_required = %s", |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1180 | __func__, p_cb->secure_connections_only_mode_required ? |
| 1181 | "true" : "false"); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1182 | |
| 1183 | if ((p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) && (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT)) |
| 1184 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1185 | p_cb->le_secure_connections_mode_is_used = true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | SMP_TRACE_DEBUG("use_sc_process = %d", p_cb->le_secure_connections_mode_is_used); |
| 1189 | |
| 1190 | if (p_cb->le_secure_connections_mode_is_used) |
| 1191 | { |
| 1192 | model = smp_select_association_model_secure_connections(p_cb); |
| 1193 | } |
| 1194 | else |
| 1195 | { |
| 1196 | model = smp_select_legacy_association_model(p_cb); |
| 1197 | } |
| 1198 | return model; |
| 1199 | } |
| 1200 | |
| 1201 | /******************************************************************************* |
| 1202 | ** Function smp_select_legacy_association_model |
| 1203 | ** |
| 1204 | ** Description This function is called to select association mode if at least |
| 1205 | ** one side doesn't support secure connections. |
| 1206 | ** |
| 1207 | *******************************************************************************/ |
| 1208 | tSMP_ASSO_MODEL smp_select_legacy_association_model(tSMP_CB *p_cb) |
| 1209 | { |
| 1210 | tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE; |
| 1211 | |
| 1212 | SMP_TRACE_DEBUG("%s", __func__); |
| 1213 | /* if OOB data is present on both devices, then use OOB association model */ |
| 1214 | if (p_cb->peer_oob_flag == SMP_OOB_PRESENT && p_cb->loc_oob_flag == SMP_OOB_PRESENT) |
| 1215 | return SMP_MODEL_OOB; |
| 1216 | |
| 1217 | /* else if neither device requires MITM, then use Just Works association model */ |
| 1218 | if (SMP_NO_MITM_REQUIRED (p_cb->peer_auth_req) && SMP_NO_MITM_REQUIRED(p_cb->loc_auth_req)) |
| 1219 | return SMP_MODEL_ENCRYPTION_ONLY; |
| 1220 | |
| 1221 | /* otherwise use IO capability to select association model */ |
| 1222 | if (p_cb->peer_io_caps < SMP_IO_CAP_MAX && p_cb->local_io_capability < SMP_IO_CAP_MAX) |
| 1223 | { |
| 1224 | if (p_cb->role == HCI_ROLE_MASTER) |
| 1225 | { |
| 1226 | model = smp_association_table[p_cb->role][p_cb->peer_io_caps] |
| 1227 | [p_cb->local_io_capability]; |
| 1228 | } |
| 1229 | else |
| 1230 | { |
| 1231 | model = smp_association_table[p_cb->role][p_cb->local_io_capability] |
| 1232 | [p_cb->peer_io_caps]; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | return model; |
| 1237 | } |
| 1238 | |
| 1239 | /******************************************************************************* |
| 1240 | ** Function smp_select_association_model_secure_connections |
| 1241 | ** |
| 1242 | ** Description This function is called to select association mode if both |
| 1243 | ** sides support secure connections. |
| 1244 | ** |
| 1245 | *******************************************************************************/ |
| 1246 | tSMP_ASSO_MODEL smp_select_association_model_secure_connections(tSMP_CB *p_cb) |
| 1247 | { |
| 1248 | tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE; |
| 1249 | |
| 1250 | SMP_TRACE_DEBUG("%s", __func__); |
| 1251 | /* if OOB data is present on at least one device, then use OOB association model */ |
| 1252 | if (p_cb->peer_oob_flag == SMP_OOB_PRESENT || p_cb->loc_oob_flag == SMP_OOB_PRESENT) |
| 1253 | return SMP_MODEL_SEC_CONN_OOB; |
| 1254 | |
| 1255 | /* else if neither device requires MITM, then use Just Works association model */ |
| 1256 | if (SMP_NO_MITM_REQUIRED (p_cb->peer_auth_req) && SMP_NO_MITM_REQUIRED(p_cb->loc_auth_req)) |
| 1257 | return SMP_MODEL_SEC_CONN_JUSTWORKS; |
| 1258 | |
| 1259 | /* otherwise use IO capability to select association model */ |
| 1260 | if (p_cb->peer_io_caps < SMP_IO_CAP_MAX && p_cb->local_io_capability < SMP_IO_CAP_MAX) |
| 1261 | { |
| 1262 | if (p_cb->role == HCI_ROLE_MASTER) |
| 1263 | { |
| 1264 | model = smp_association_table_sc[p_cb->role][p_cb->peer_io_caps] |
| 1265 | [p_cb->local_io_capability]; |
| 1266 | } |
| 1267 | else |
| 1268 | { |
| 1269 | model = smp_association_table_sc[p_cb->role][p_cb->local_io_capability] |
| 1270 | [p_cb->peer_io_caps]; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | return model; |
| 1275 | } |
| 1276 | |
| 1277 | /******************************************************************************* |
| 1278 | ** Function smp_reverse_array |
| 1279 | ** |
| 1280 | ** Description This function reverses array bytes |
| 1281 | ** |
| 1282 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1283 | void smp_reverse_array(uint8_t *arr, uint8_t len) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1284 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1285 | uint8_t i =0, tmp; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1286 | |
| 1287 | SMP_TRACE_DEBUG("smp_reverse_array"); |
| 1288 | |
| 1289 | for (i = 0; i < len/2; i ++) |
| 1290 | { |
| 1291 | tmp = arr[i]; |
| 1292 | arr[i] = arr[len -1 - i]; |
| 1293 | arr[len -1 - i] = tmp; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | /******************************************************************************* |
| 1298 | ** Function smp_calculate_random_input |
| 1299 | ** |
| 1300 | ** Description This function returns random input value to be used in commitment |
| 1301 | ** calculation for SC passkey entry association mode |
| 1302 | ** (if bit["round"] in "random" array == 1 then returns 0x81 |
| 1303 | ** else returns 0x80). |
| 1304 | ** |
| 1305 | ** Returns ri value |
| 1306 | ** |
| 1307 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1308 | uint8_t smp_calculate_random_input(uint8_t *random, uint8_t round) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1309 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1310 | uint8_t i = round/8; |
| 1311 | uint8_t j = round%8; |
| 1312 | uint8_t ri; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1313 | |
| 1314 | SMP_TRACE_DEBUG("random: 0x%02x, round: %d, i: %d, j: %d", random[i], round, i, j); |
| 1315 | ri = ((random[i] >> j) & 1) | 0x80; |
| 1316 | SMP_TRACE_DEBUG("%s ri=0x%02x", __func__, ri); |
| 1317 | return ri; |
| 1318 | } |
| 1319 | |
| 1320 | /******************************************************************************* |
| 1321 | ** Function smp_collect_local_io_capabilities |
| 1322 | ** |
| 1323 | ** Description This function puts into IOcap array local device |
| 1324 | ** IOCapability, OOB data, AuthReq. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1325 | ** |
| 1326 | ** Returns void |
| 1327 | ** |
| 1328 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1329 | void smp_collect_local_io_capabilities(uint8_t *iocap, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1330 | { |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1331 | SMP_TRACE_DEBUG("%s", __func__); |
| 1332 | |
| 1333 | iocap[0] = p_cb->local_io_capability; |
| 1334 | iocap[1] = p_cb->loc_oob_flag; |
| 1335 | iocap[2] = p_cb->loc_auth_req; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1338 | /******************************************************************************* |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1339 | ** Function smp_collect_peer_io_capabilities |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1340 | ** |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1341 | ** Description This function puts into IOcap array peer device |
| 1342 | ** IOCapability, OOB data, AuthReq. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1343 | ** |
| 1344 | ** Returns void |
| 1345 | ** |
| 1346 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1347 | void smp_collect_peer_io_capabilities(uint8_t *iocap, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1348 | { |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1349 | SMP_TRACE_DEBUG("%s", __func__); |
| 1350 | |
| 1351 | iocap[0] = p_cb->peer_io_caps; |
| 1352 | iocap[1] = p_cb->peer_oob_flag; |
| 1353 | iocap[2] = p_cb->peer_auth_req; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1354 | } |
| 1355 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1356 | /******************************************************************************* |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1357 | ** Function smp_collect_local_ble_address |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1358 | ** |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1359 | ** Description This function puts into le_addr array local device le address: |
| 1360 | ** le_addr[0-5] = local BD ADDR, |
| 1361 | ** le_addr[6] = local le address type (PUBLIC/RANDOM). |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1362 | ** |
| 1363 | ** Returns void |
| 1364 | ** |
| 1365 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1366 | void smp_collect_local_ble_address(uint8_t *le_addr, tSMP_CB *p_cb) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1367 | { |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1368 | tBLE_ADDR_TYPE addr_type = 0; |
| 1369 | BD_ADDR bda; |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1370 | uint8_t *p = le_addr; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1371 | |
| 1372 | SMP_TRACE_DEBUG("%s", __func__); |
| 1373 | |
| 1374 | BTM_ReadConnectionAddr( p_cb->pairing_bda, bda, &addr_type); |
| 1375 | BDADDR_TO_STREAM(p, bda); |
| 1376 | UINT8_TO_STREAM(p, addr_type); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /******************************************************************************* |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1380 | ** Function smp_collect_peer_ble_address |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1381 | ** |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1382 | ** Description This function puts into le_addr array peer device le address: |
| 1383 | ** le_addr[0-5] = peer BD ADDR, |
| 1384 | ** le_addr[6] = peer le address type (PUBLIC/RANDOM). |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 1385 | ** |
| 1386 | ** Returns void |
| 1387 | ** |
| 1388 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1389 | void smp_collect_peer_ble_address(uint8_t *le_addr, tSMP_CB *p_cb) |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 1390 | { |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1391 | tBLE_ADDR_TYPE addr_type = 0; |
| 1392 | BD_ADDR bda; |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1393 | uint8_t *p = le_addr; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1394 | |
| 1395 | SMP_TRACE_DEBUG("%s", __func__); |
| 1396 | |
| 1397 | if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, bda, &addr_type)) |
| 1398 | { |
| 1399 | SMP_TRACE_ERROR("can not collect peer le addr information for unknown device"); |
| 1400 | return; |
| 1401 | } |
| 1402 | |
| 1403 | BDADDR_TO_STREAM(p, bda); |
| 1404 | UINT8_TO_STREAM(p, addr_type); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 1405 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1406 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1407 | /******************************************************************************* |
| 1408 | ** Function smp_check_commitment |
| 1409 | ** |
| 1410 | ** Description This function compares peer commitment values: |
| 1411 | ** - expected (i.e. calculated locally), |
| 1412 | ** - received from the peer. |
| 1413 | ** |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1414 | ** Returns true if the values are the same |
| 1415 | ** false otherwise |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1416 | ** |
| 1417 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1418 | bool smp_check_commitment(tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1419 | { |
| 1420 | BT_OCTET16 expected; |
| 1421 | |
| 1422 | SMP_TRACE_DEBUG("%s", __func__); |
| 1423 | |
| 1424 | smp_calculate_peer_commitment(p_cb, expected); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1425 | print128(expected, (const uint8_t *)"calculated peer commitment"); |
| 1426 | print128(p_cb->remote_commitment, (const uint8_t *)"received peer commitment"); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1427 | |
| 1428 | if (memcmp(p_cb->remote_commitment, expected, BT_OCTET16_LEN)) |
| 1429 | { |
Jakub Pawlowski | ce83251 | 2016-07-28 06:11:36 -0700 | [diff] [blame] | 1430 | SMP_TRACE_WARNING("%s: Commitment check fails", __func__); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1431 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1432 | } |
| 1433 | |
Jakub Pawlowski | ce83251 | 2016-07-28 06:11:36 -0700 | [diff] [blame] | 1434 | SMP_TRACE_DEBUG("%s: Commitment check succeeds", __func__); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1435 | return true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | /******************************************************************************* |
| 1439 | ** |
| 1440 | ** Function smp_save_secure_connections_long_term_key |
| 1441 | ** |
| 1442 | ** Description The function saves SC LTK as BLE key for future use as local |
| 1443 | ** and/or peer key. |
| 1444 | ** |
| 1445 | ** Returns void |
| 1446 | ** |
| 1447 | *******************************************************************************/ |
| 1448 | void smp_save_secure_connections_long_term_key(tSMP_CB *p_cb) |
| 1449 | { |
| 1450 | tBTM_LE_LENC_KEYS lle_key; |
| 1451 | tBTM_LE_PENC_KEYS ple_key; |
| 1452 | |
| 1453 | SMP_TRACE_DEBUG("%s-Save LTK as local LTK key", __func__); |
| 1454 | memcpy(lle_key.ltk, p_cb->ltk, BT_OCTET16_LEN); |
| 1455 | lle_key.div = 0; |
| 1456 | lle_key.key_size = p_cb->loc_enc_size; |
| 1457 | lle_key.sec_level = p_cb->sec_level; |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1458 | btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LENC, (tBTM_LE_KEY_VALUE *)&lle_key, true); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1459 | |
| 1460 | SMP_TRACE_DEBUG("%s-Save LTK as peer LTK key", __func__); |
| 1461 | ple_key.ediv = 0; |
| 1462 | memset(ple_key.rand, 0, BT_OCTET8_LEN); |
| 1463 | memcpy(ple_key.ltk, p_cb->ltk, BT_OCTET16_LEN); |
| 1464 | ple_key.sec_level = p_cb->sec_level; |
| 1465 | ple_key.key_size = p_cb->loc_enc_size; |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1466 | btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_PENC, (tBTM_LE_KEY_VALUE *)&ple_key, true); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /******************************************************************************* |
| 1470 | ** |
| 1471 | ** Function smp_calculate_f5_mackey_and_long_term_key |
| 1472 | ** |
| 1473 | ** Description The function calculates MacKey and LTK and saves them in CB. |
| 1474 | ** To calculate MacKey and LTK it calls smp_calc_f5(...). |
| 1475 | ** MacKey is used in dhkey calculation, LTK is used to encrypt |
| 1476 | ** the link. |
| 1477 | ** |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1478 | ** Returns false if out of resources, true otherwise. |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1479 | ** |
| 1480 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1481 | bool smp_calculate_f5_mackey_and_long_term_key(tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1482 | { |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1483 | uint8_t a[7]; |
| 1484 | uint8_t b[7]; |
| 1485 | uint8_t *p_na; |
| 1486 | uint8_t *p_nb; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1487 | |
| 1488 | SMP_TRACE_DEBUG("%s", __func__); |
| 1489 | |
| 1490 | if (p_cb->role == HCI_ROLE_MASTER) |
| 1491 | { |
| 1492 | smp_collect_local_ble_address(a, p_cb); |
| 1493 | smp_collect_peer_ble_address(b, p_cb); |
| 1494 | p_na = p_cb->rand; |
| 1495 | p_nb = p_cb->rrand; |
| 1496 | } |
| 1497 | else |
| 1498 | { |
| 1499 | smp_collect_local_ble_address(b, p_cb); |
| 1500 | smp_collect_peer_ble_address(a, p_cb); |
| 1501 | p_na = p_cb->rrand; |
| 1502 | p_nb = p_cb->rand; |
| 1503 | } |
| 1504 | |
| 1505 | if(!smp_calculate_f5(p_cb->dhkey, p_na, p_nb, a, b, p_cb->mac_key, p_cb->ltk)) |
| 1506 | { |
| 1507 | SMP_TRACE_ERROR("%s failed", __func__); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1508 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | SMP_TRACE_EVENT ("%s is completed", __func__); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1512 | return true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | /******************************************************************************* |
| 1516 | ** |
| 1517 | ** Function smp_request_oob_data |
| 1518 | ** |
| 1519 | ** Description Requests application to provide OOB data. |
| 1520 | ** |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1521 | ** Returns true - OOB data has to be provided by application |
| 1522 | ** false - otherwise (unexpected) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1523 | ** |
| 1524 | *******************************************************************************/ |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1525 | bool smp_request_oob_data(tSMP_CB *p_cb) |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1526 | { |
| 1527 | tSMP_OOB_DATA_TYPE req_oob_type = SMP_OOB_INVALID_TYPE; |
| 1528 | |
| 1529 | SMP_TRACE_DEBUG("%s", __func__); |
| 1530 | |
| 1531 | if (p_cb->peer_oob_flag == SMP_OOB_PRESENT && p_cb->loc_oob_flag == SMP_OOB_PRESENT) |
| 1532 | { |
| 1533 | /* both local and peer rcvd data OOB */ |
| 1534 | req_oob_type = SMP_OOB_BOTH; |
| 1535 | } |
| 1536 | else if (p_cb->peer_oob_flag == SMP_OOB_PRESENT) |
| 1537 | { |
| 1538 | /* peer rcvd OOB local data, local didn't receive OOB peer data */ |
| 1539 | req_oob_type = SMP_OOB_LOCAL; |
| 1540 | } |
| 1541 | else if (p_cb->loc_oob_flag == SMP_OOB_PRESENT) |
| 1542 | { |
| 1543 | req_oob_type = SMP_OOB_PEER; |
| 1544 | } |
| 1545 | |
| 1546 | SMP_TRACE_DEBUG("req_oob_type = %d", req_oob_type); |
| 1547 | |
| 1548 | if (req_oob_type == SMP_OOB_INVALID_TYPE) |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1549 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1550 | |
| 1551 | p_cb->req_oob_type = req_oob_type; |
| 1552 | p_cb->cb_evt = SMP_SC_OOB_REQ_EVT; |
| 1553 | smp_sm_event(p_cb, SMP_TK_REQ_EVT, &req_oob_type); |
| 1554 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1555 | return true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 1556 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1557 | |
| 1558 | |
| 1559 | #endif |
| 1560 | |