1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "smp.h"
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "log.h"
22
23 #include "btstack.h"
24 #include "l2cap_le_if.h"
25 #include "packet.h"
26
27 #include "platform/include/alarm.h"
28 #include "platform/include/allocator.h"
29 #include "platform/include/event.h"
30 #include "platform/include/list.h"
31 #include "platform/include/module.h"
32 #include "platform/include/mutex.h"
33 #include "platform/include/random.h"
34
35 #include "btm/btm_thread.h"
36 #include "hci/hci.h"
37
38 #include "smp_aes_encryption.h"
39 #include "smp_cmd.h"
40 #include "smp_common.h"
41 #include "smp_def.h"
42 #include "smp_hci_event.h"
43 #include "smp_legacy.h"
44 #include "smp_oob.h"
45 #include "smp_privacy.h"
46 #include "smp_receive.h"
47 #include "smp_sc_accepter.h"
48 #include "smp_sc_initiator.h"
49 #include "smp_send.h"
50 #include "smp_tool.h"
51
52 static const uint8_t SALT[SMP_ENCRYPT_KEY_LEN] = {
53 0x6C, 0x88, 0x83, 0x91, 0xAA, 0xF5, 0xA5, 0x38, 0x60, 0x37, 0x0B, 0xDB, 0x5A, 0x60, 0x83, 0xBE
54 }; // The key of an encryption algorithm.
55
56 static SMP_PairMng g_smpPairMng = {0x00};
57 static SMP_Callback_t g_smpCallBack = {0x00};
58 static bool g_smpSecureConnOnlyMode = false;
59
60 static int SMP_AuthReqReplyProcessMaster(
61 bool accept, uint8_t rejectReason, uint8_t pairMethod, const uint8_t *entryValue);
62 static int SMP_AuthReqReplyJudgeExceptionMaster(bool accept, uint8_t rejectReason);
63 static int SMP_AuthReqReplyStepTransMaster(uint8_t pairMethod);
64 static int SMP_AuthReqReplyProcessSlave(
65 bool accept, uint8_t rejectReason, uint8_t pairMethod, const uint8_t *entryValue);
66 static int SMP_AuthReqReplyJudgeExceptionSlave(bool accept, uint8_t rejectReason);
67 static int SMP_AuthReqReplyStepTransSlave(uint8_t pairMethod);
68 static int SMP_AuthReqReplyNumericSlave();
69 static int SMP_AuthReqReplyPasskeyEntrySlave();
70 static int SMP_AuthReqReplyOobSlave();
71 static void SMP_AuthReqReplyObtainEntryValue(uint8_t pairMethod, const uint8_t *entryValue);
72 static int SMP_PairReqReplyJudgeException(
73 uint16_t handle, bool accept, uint8_t rejectReason, const SMP_PairParam *param);
74 static int SMP_PairReqReplyLegacyPair();
75 static int SMP_PairReqReplyScPair();
76 static int SMP_PairReqReplyScPairOob();
77 static int SMP_PairRspReplyJudgeException(uint16_t handle, bool accept, uint8_t rejectReason);
78 static int SMP_PairRspReplyLegacyPair();
79 static int SMP_PairRspReplyScPair();
80 static int SMP_PairRspReplyScPairOob();
81 static int SMP_LtkReqReplyNormal(uint16_t handle, bool accept, const uint8_t *key);
82 static int SMP_LtkReqReplyException(uint16_t handle);
83 static void SMP_GenPairRetNormal(uint16_t handle, uint8_t status);
84 static void SMP_GenPairRetException(uint16_t handle, uint8_t status, uint8_t failedReason, Alarm *cancelTimer);
85 static void SMP_L2cifLeRegisterFixChannelCallback(uint16_t cid, int result);
86 static void SMP_L2cifLeDeregisterFixChannelCallback(uint16_t cid, int result);
87
88 static void SMP_StartupTask(void *context);
89 static void SMP_ShutdownTask(void *context);
90 static void SMP_SetIrkTask(void *context);
91 static void SMP_SetIdentAddrTask(void *context);
92 static void SMP_AsyncResoRpaTask(void *context);
93 static void SMP_GenRpaTask(void *context);
94 static void SMP_SetSecConnOnlyModeTask(void *context);
95 static void SMP_SendSecReqToRemoteTask(void *context);
96 static void SMP_GenSignTask(void *context);
97 static void SMP_StartEncTask(void *context);
98 static void SMP_StartPairTask(void *context);
99 static void SMP_AuthReqReplyTask(void *context);
100 static void SMP_RemotePairReqReplyTask(void *context);
101 static void SMP_RemotePairRspReplyTask(void *context);
102 static void SMP_RemoteSecReqReplyTask(void *context);
103 static void SMP_LongTermKeyReqReplyTask(void *context);
104 static void SMP_CancelPairTask(void *context);
105 static void SMP_RegCbTask(void *context);
106 static void SMP_UnregCbTask(void *context);
107 static void SMP_GenScOobDataTask(void *context);
108 static void SMP_PairTimeoutTask(void *context);
109
110 static HciEventCallbacks g_smpHciEventCallbacks = {
111 .leRandComplete = SMP_OnLERandComplete,
112 .leEncryptComplete = SMP_OnLEEncryptComplete,
113 .encryptionChange = SMP_OnLEEncryptionChange,
114 .encryptionKeyRefreshComplete = SMP_OnLEEncryptionKeyRefreshComplete,
115 .leLongTermKeyRequest = SMP_OnLELongTermKeyRequest,
116 .leLongTermKeyRequestReplyComplete = SMP_OnLELongTermKeyRequestReplyComplete,
117 .leReadLocalP256PublicKeyComplete = SMP_OnLEReadLocalP256PublicKeyComplete,
118 .leGenerateDHKeyComplete = SMP_OnLEGenerateDHKeyComplete,
119 };
120
SMP_Initialize(int traceLevel)121 static void SMP_Initialize(int traceLevel)
122 {
123 LOG_INFO("%{public}s", __FUNCTION__);
124 }
125
SMP_Finalize(void)126 static void SMP_Finalize(void)
127 {
128 LOG_INFO("%{public}s", __FUNCTION__);
129 }
130
SMP_Startup()131 static void SMP_Startup()
132 {
133 int ret;
134 LOG_INFO("%{public}s", __FUNCTION__);
135 Event *ctx = EventCreate(true);
136 ret = BTM_CreateProcessingQueue(PROCESSING_QUEUE_ID_SMP, BTM_PROCESSING_QUEUE_SIZE_DEFAULT);
137 if (ret != SMP_SUCCESS) {
138 EventDelete(ctx);
139 return;
140 }
141 ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_StartupTask, ctx);
142 if (ret != SMP_SUCCESS) {
143 EventDelete(ctx);
144 return;
145 }
146 EventWait(ctx, SMP_INIT_WAIT_TIME);
147 EventDelete(ctx);
148 }
149
SMP_StartupTask(void * context)150 static void SMP_StartupTask(void *context)
151 {
152 Event *ctx = context;
153 LOG_DEBUG("%{public}s", __FUNCTION__);
154 L2capLeFixChannel channel;
155 g_smpSecureConnOnlyMode = false;
156 SMP_ClearScOobData(true);
157 SMP_ClearPairState(&g_smpPairMng);
158 SMP_SetEncCmdList(SMP_ListCreate(SMP_FreeEncCmd));
159 channel.cid = L2CAP_LE_SMP_CHANNEL;
160 channel.leConnected = SMP_Connected;
161 channel.leDisconnected = SMP_Disconnected;
162 channel.recvLeData = SMP_ReceiveData;
163 int ret = L2CIF_LeRegisterFixChannel(L2CAP_LE_SMP_CHANNEL, &channel, SMP_L2cifLeRegisterFixChannelCallback);
164 if (ret != SMP_SUCCESS) {
165 LOG_ERROR("RegisterFixChannel error %{public}d", ret);
166 }
167 HCI_RegisterEventCallbacks(&g_smpHciEventCallbacks);
168 if (ctx != NULL) {
169 EventSet(ctx);
170 }
171 }
172
SMP_Shutdown()173 static void SMP_Shutdown()
174 {
175 int ret;
176 LOG_INFO("%{public}s", __FUNCTION__);
177 Event *ctx = EventCreate(true);
178 ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_ShutdownTask, ctx);
179 if (ret != SMP_SUCCESS) {
180 EventDelete(ctx);
181 return;
182 }
183 EventWait(ctx, SMP_INIT_WAIT_TIME);
184 EventDelete(ctx);
185 }
186
SMP_ShutdownTask(void * context)187 static void SMP_ShutdownTask(void *context)
188 {
189 Event *ctx = context;
190 LOG_DEBUG("%{public}s", __FUNCTION__);
191 L2CIF_LeDeregisterFixChannel(L2CAP_LE_SMP_CHANNEL, SMP_L2cifLeDeregisterFixChannelCallback);
192 HCI_DeregisterEventCallbacks(&g_smpHciEventCallbacks);
193 g_smpSecureConnOnlyMode = false;
194 SMP_ClearScOobData(true);
195 SMP_ClearPairState(&g_smpPairMng);
196 SMP_ListDelete(SMP_GetEncCmdList());
197 SMP_SetEncCmdList(NULL);
198 BTM_DeleteProcessingQueue(PROCESSING_QUEUE_ID_SMP);
199 if (ctx != NULL) {
200 EventSet(ctx);
201 }
202 }
203
SMP_RegisterCallback(const SMP_Callback_t * cb)204 int SMP_RegisterCallback(const SMP_Callback_t *cb)
205 {
206 LOG_INFO("%{public}s", __FUNCTION__);
207 SMP_RegCbTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_RegCbTask_t));
208 if (ctx == NULL) {
209 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
210 return SMP_ERR_OUT_OF_RES;
211 }
212 (void)memset_s(ctx, sizeof(SMP_RegCbTask_t), 0x00, sizeof(SMP_RegCbTask_t));
213 (void)memcpy_s(&ctx->cb, sizeof(SMP_Callback_t), cb, sizeof(ctx->cb));
214 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_RegCbTask, (void *)ctx);
215 if (ret != SMP_SUCCESS) {
216 MEM_MALLOC.free(ctx);
217 return ret;
218 }
219 return ret;
220 }
221
SMP_RegCbTask(void * context)222 static void SMP_RegCbTask(void *context)
223 {
224 SMP_RegCbTask_t *param = (SMP_RegCbTask_t *)context;
225 if (param->cb.SMP_CallbackAuthenticationRequest != NULL) {
226 g_smpCallBack.SMP_CallbackAuthenticationRequest = param->cb.SMP_CallbackAuthenticationRequest;
227 }
228 if (param->cb.SMP_CallbackEncryptionComplete != NULL) {
229 g_smpCallBack.SMP_CallbackEncryptionComplete = param->cb.SMP_CallbackEncryptionComplete;
230 }
231 if (param->cb.SMP_CallbackLongTermKeyRequest != NULL) {
232 g_smpCallBack.SMP_CallbackLongTermKeyRequest = param->cb.SMP_CallbackLongTermKeyRequest;
233 }
234 if (param->cb.SMP_CallbackPairResult != NULL) {
235 g_smpCallBack.SMP_CallbackPairResult = param->cb.SMP_CallbackPairResult;
236 }
237 if (param->cb.SMP_CallbackRemotePairRequest != NULL) {
238 g_smpCallBack.SMP_CallbackRemotePairRequest = param->cb.SMP_CallbackRemotePairRequest;
239 }
240 if (param->cb.SMP_CallbackRemotePairResponse != NULL) {
241 g_smpCallBack.SMP_CallbackRemotePairResponse = param->cb.SMP_CallbackRemotePairResponse;
242 }
243 if (param->cb.SMP_CallbackRemoteSecurityRequest != NULL) {
244 g_smpCallBack.SMP_CallbackRemoteSecurityRequest = param->cb.SMP_CallbackRemoteSecurityRequest;
245 }
246 if (param->cb.SMP_CallbackGenerateSignatureResult != NULL) {
247 g_smpCallBack.SMP_CallbackGenerateSignatureResult = param->cb.SMP_CallbackGenerateSignatureResult;
248 }
249 if (param->cb.SMP_CallbackGenerateRPAResult != NULL) {
250 g_smpCallBack.SMP_CallbackGenerateRPAResult = param->cb.SMP_CallbackGenerateRPAResult;
251 }
252 if (param->cb.SMP_CallbackResolveRPAResult != NULL) {
253 g_smpCallBack.SMP_CallbackResolveRPAResult = param->cb.SMP_CallbackResolveRPAResult;
254 }
255 if (param->cb.SMP_CallbackGenerateScOobDataResult != NULL) {
256 g_smpCallBack.SMP_CallbackGenerateScOobDataResult = param->cb.SMP_CallbackGenerateScOobDataResult;
257 }
258 MEM_MALLOC.free(param);
259 }
260
SMP_UnregisterCallback()261 int SMP_UnregisterCallback()
262 {
263 LOG_INFO("%{public}s", __FUNCTION__);
264 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_UnregCbTask, NULL);
265 if (ret != SMP_SUCCESS) {
266 return ret;
267 }
268 return ret;
269 }
270
SMP_UnregCbTask(void * context)271 static void SMP_UnregCbTask(void *context)
272 {
273 g_smpCallBack.SMP_CallbackAuthenticationRequest = NULL;
274 g_smpCallBack.SMP_CallbackEncryptionComplete = NULL;
275 g_smpCallBack.SMP_CallbackLongTermKeyRequest = NULL;
276 g_smpCallBack.SMP_CallbackPairResult = NULL;
277 g_smpCallBack.SMP_CallbackRemotePairRequest = NULL;
278 g_smpCallBack.SMP_CallbackRemotePairResponse = NULL;
279 g_smpCallBack.SMP_CallbackRemoteSecurityRequest = NULL;
280 g_smpCallBack.SMP_CallbackGenerateSignatureResult = NULL;
281 g_smpCallBack.SMP_CallbackGenerateRPAResult = NULL;
282 g_smpCallBack.SMP_CallbackResolveRPAResult = NULL;
283 g_smpCallBack.SMP_CallbackGenerateScOobDataResult = NULL;
284 (void)context;
285 }
286
SMP_StartPair(uint16_t handle,const BtAddr * localAddr,const BtAddr * peerAddr,const SMP_PairParam * param)287 int SMP_StartPair(uint16_t handle, const BtAddr *localAddr, const BtAddr *peerAddr, const SMP_PairParam *param)
288 {
289 LOG_INFO("%{public}s", __FUNCTION__);
290 SMP_StartPairTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_StartPairTask_t));
291 if (ctx == NULL) {
292 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
293 return SMP_ERR_OUT_OF_RES;
294 }
295 (void)memset_s(ctx, sizeof(SMP_StartPairTask_t), 0x00, sizeof(SMP_StartPairTask_t));
296 ctx->handle = handle;
297 (void)memcpy_s(&ctx->localAddr, sizeof(BtAddr), localAddr, sizeof(ctx->localAddr));
298 (void)memcpy_s(&ctx->peerAddr, sizeof(BtAddr), peerAddr, sizeof(ctx->peerAddr));
299 (void)memcpy_s(&ctx->param, sizeof(SMP_PairParam), param, sizeof(ctx->param));
300 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_StartPairTask, (void *)ctx);
301 if (ret != SMP_SUCCESS) {
302 MEM_MALLOC.free(ctx);
303 return ret;
304 }
305 return ret;
306 }
307
SMP_StartPairTask(void * context)308 static void SMP_StartPairTask(void *context)
309 {
310 int ret = SMP_SUCCESS;
311 SMP_StartPairTask_t *param = (SMP_StartPairTask_t *)context;
312 LOG_DEBUG("%{public}s", __FUNCTION__);
313 if (g_smpPairMng.state == SMP_STATE_PAIRING) {
314 LOG_ERROR("It's already pairing state.");
315 } else if (SMP_GetScOobMng()->state == SMP_STATE_SC_OOB_DATA_GENERATING) {
316 LOG_ERROR("It's already SC OOB data generating state.");
317 ret = SMP_ERR_INVAL_STATE;
318 } else if (SMP_GetSecureConnOnlyMode() && (!(param->param.authReq & SMP_AUTH_REQ_BIT_SC))) {
319 LOG_ERROR("SC bit is not set in authReq.");
320 ret = SMP_ERR_INVAL_PARAM;
321 } else {
322 (void)memset_s(&g_smpPairMng, sizeof(g_smpPairMng), 0x00, sizeof(g_smpPairMng));
323 g_smpPairMng.state = SMP_STATE_PAIRING;
324 g_smpPairMng.handle = param->handle;
325 g_smpPairMng.role = SMP_ROLE_MASTER;
326 g_smpPairMng.alarm = AlarmCreate("", false);
327 g_smpPairMng.local.addr.type = param->localAddr.type;
328 g_smpPairMng.peer.addr.type = param->peerAddr.type;
329 SMP_MemoryReverseCopy(
330 g_smpPairMng.local.addr.addr, param->localAddr.addr, sizeof(g_smpPairMng.local.addr.addr));
331 SMP_MemoryReverseCopy(g_smpPairMng.peer.addr.addr, param->peerAddr.addr, sizeof(g_smpPairMng.peer.addr.addr));
332 (void)memcpy_s(&g_smpPairMng.local.pairParam, sizeof(SMP_PairParam), ¶m->param, sizeof(SMP_PairParam));
333 g_smpPairMng.step = SMP_LEGACY_PAIR_MASTER_STEP_1;
334 LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_1 started.");
335 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
336 ret = SMP_SendPairingRequest(param->handle, ¶m->param, SMP_SendDataCallback);
337 if (ret != SMP_SUCCESS) {
338 LOG_ERROR("Send pairing request failed.");
339 AlarmCancel(g_smpPairMng.alarm);
340 }
341 }
342 if (ret != SMP_SUCCESS) {
343 SMP_GeneratePairResult(param->handle, SMP_PAIR_STATUS_FAILED, 0x00, NULL);
344 }
345 MEM_MALLOC.free(param);
346 }
347
SMP_CancelPair(uint16_t handle)348 int SMP_CancelPair(uint16_t handle)
349 {
350 LOG_INFO("%{public}s", __FUNCTION__);
351 SMP_CancelPairTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_CancelPairTask_t));
352 if (ctx == NULL) {
353 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
354 return SMP_ERR_OUT_OF_RES;
355 }
356 (void)memset_s(ctx, sizeof(SMP_CancelPairTask_t), 0x00, sizeof(SMP_CancelPairTask_t));
357 ctx->handle = handle;
358 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_CancelPairTask, (void *)ctx);
359 if (ret != SMP_SUCCESS) {
360 MEM_MALLOC.free(ctx);
361 return ret;
362 }
363 return ret;
364 }
365
SMP_CancelPairTask(void * context)366 static void SMP_CancelPairTask(void *context)
367 {
368 SMP_CancelPairTask_t *param = (SMP_CancelPairTask_t *)context;
369
370 LOG_DEBUG("%{public}s", __FUNCTION__);
371 if (g_smpPairMng.state != SMP_STATE_PAIRING) {
372 LOG_ERROR("It's not pairing state. ");
373 MEM_MALLOC.free(param);
374 return;
375 }
376 if (param->handle != g_smpPairMng.handle) {
377 LOG_ERROR("Connection handle error. ");
378 MEM_MALLOC.free(param);
379 return;
380 }
381 SMP_GeneratePairResult(
382 param->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, g_smpPairMng.alarm);
383 MEM_MALLOC.free(param);
384 }
385
SMP_AuthenticationRequestReply(uint16_t handle,bool accept,uint8_t rejectReason,uint8_t pairMethod,const uint8_t * entryValue)386 int SMP_AuthenticationRequestReply(
387 uint16_t handle, bool accept, uint8_t rejectReason, uint8_t pairMethod, const uint8_t *entryValue)
388 {
389 LOG_INFO("%{public}s", __FUNCTION__);
390 SMP_AuthReqReplyTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_AuthReqReplyTask_t));
391 if (ctx == NULL) {
392 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
393 return SMP_ERR_OUT_OF_RES;
394 }
395 (void)memset_s(ctx, sizeof(SMP_AuthReqReplyTask_t), 0x00, sizeof(SMP_AuthReqReplyTask_t));
396 ctx->handle = handle;
397 ctx->accept = accept;
398 ctx->rejectReason = rejectReason;
399 ctx->pairMethod = pairMethod;
400 if (accept && SMP_IsPasskeyEntryPair(pairMethod)) {
401 (void)memcpy_s(ctx->entryValue, SMP_ENTRY_VALUE_LEN, entryValue, SMP_PASSKEY_LEN);
402 } else if (accept && (pairMethod == SMP_PAIR_METHOD_OOB_LEGACY)) {
403 (void)memcpy_s(ctx->entryValue, SMP_ENTRY_VALUE_LEN, entryValue, SMP_LEGACY_OOB_LEN);
404 } else if ((accept && (pairMethod == SMP_PAIR_METHOD_OOB_SC_BOTH_SIDE_SEND_RECV)) ||
405 (accept && (pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND))) {
406 (void)memcpy_s(ctx->entryValue, SMP_ENTRY_VALUE_LEN, entryValue, SMP_SC_OOB_LEN);
407 }
408 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_AuthReqReplyTask, (void *)ctx);
409 if (ret != SMP_SUCCESS) {
410 MEM_MALLOC.free(ctx);
411 return ret;
412 }
413 return ret;
414 }
415
SMP_AuthReqReplyTask(void * context)416 static void SMP_AuthReqReplyTask(void *context)
417 {
418 SMP_AuthReqReplyTask_t *param = (SMP_AuthReqReplyTask_t *)context;
419
420 LOG_DEBUG("%{public}s", __FUNCTION__);
421 if (g_smpPairMng.state != SMP_STATE_PAIRING) {
422 LOG_ERROR("It's not pairing state");
423 MEM_MALLOC.free(param);
424 return;
425 }
426 if (g_smpPairMng.handle != param->handle) {
427 LOG_ERROR("Connection handle error");
428 MEM_MALLOC.free(param);
429 return;
430 }
431 if (g_smpPairMng.role == SMP_ROLE_MASTER) {
432 LOG_INFO("It's initiator role.");
433 SMP_AuthReqReplyProcessMaster(param->accept, param->rejectReason, param->pairMethod, param->entryValue);
434 } else {
435 LOG_INFO("It's accepter role.");
436 SMP_AuthReqReplyProcessSlave(param->accept, param->rejectReason, param->pairMethod, param->entryValue);
437 }
438 MEM_MALLOC.free(param);
439 }
440
SMP_RemotePairRequestReply(uint16_t handle,uint8_t rejectReason,const BtAddr * localAddr,const BtAddr * peerAddr,const SMP_PairParam * param)441 int SMP_RemotePairRequestReply(
442 uint16_t handle, uint8_t rejectReason, const BtAddr *localAddr, const BtAddr *peerAddr, const SMP_PairParam *param)
443 {
444 LOG_INFO("%{public}s", __FUNCTION__);
445 SMP_RemotePairReqReplyTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_RemotePairReqReplyTask_t));
446 if (ctx == NULL) {
447 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
448 return SMP_ERR_OUT_OF_RES;
449 }
450 (void)memset_s(ctx, sizeof(SMP_RemotePairReqReplyTask_t), 0x00, sizeof(SMP_RemotePairReqReplyTask_t));
451 ctx->handle = handle;
452 ctx->rejectReason = rejectReason;
453 if (rejectReason == SMP_PAIR_FAILED_NO_FAILED) {
454 ctx->accept = true;
455 } else {
456 ctx->accept = false;
457 }
458 if (ctx->accept) {
459 (void)memcpy_s(&ctx->localAddr, sizeof(BtAddr), localAddr, sizeof(ctx->localAddr));
460 (void)memcpy_s(&ctx->peerAddr, sizeof(BtAddr), peerAddr, sizeof(ctx->peerAddr));
461 (void)memcpy_s(&ctx->param, sizeof(BtAddr), param, sizeof(ctx->param));
462 }
463 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_RemotePairReqReplyTask, (void *)ctx);
464 if (ret != SMP_SUCCESS) {
465 MEM_MALLOC.free(ctx);
466 return ret;
467 }
468 return ret;
469 }
470
SMP_RemotePairReqReplyTask(void * context)471 static void SMP_RemotePairReqReplyTask(void *context)
472 {
473 SMP_RemotePairReqReplyTask_t *param = (SMP_RemotePairReqReplyTask_t *)context;
474
475 LOG_DEBUG("%{public}s", __FUNCTION__);
476 int ret = SMP_PairReqReplyJudgeException(param->handle, param->accept, param->rejectReason, ¶m->param);
477 if (!ret) {
478 g_smpPairMng.local.addr.type = param->localAddr.type;
479 g_smpPairMng.peer.addr.type = param->peerAddr.type;
480 SMP_MemoryReverseCopy(
481 g_smpPairMng.local.addr.addr, param->localAddr.addr, sizeof(g_smpPairMng.local.addr.addr));
482 SMP_MemoryReverseCopy(g_smpPairMng.peer.addr.addr, param->peerAddr.addr, sizeof(g_smpPairMng.peer.addr.addr));
483 (void)memcpy_s(&g_smpPairMng.local.pairParam, sizeof(SMP_PairParam), ¶m->param, sizeof(SMP_PairParam));
484 SMP_CalculatePairType(&g_smpPairMng);
485 SMP_CalculateEncKeySize(&g_smpPairMng);
486 SMP_CalculatePairMethod(&g_smpPairMng);
487 SMP_CalculateKeyDistribution(&g_smpPairMng);
488 ret = SMP_SendPairingResponse(param->handle, &g_smpPairMng.local.pairParam, SMP_SendPairingResponseCallback);
489 if (ret != SMP_SUCCESS) {
490 LOG_ERROR("Send pairing response failed.");
491 SMP_GeneratePairResult(param->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
492 } else {
493 if (g_smpPairMng.pairType == SMP_PAIR_TYPE_LEGACY) {
494 LOG_INFO("It's legacy pair.");
495 SMP_PairReqReplyLegacyPair();
496 } else {
497 LOG_INFO("It's SC pair.");
498 SMP_PairReqReplyScPair();
499 }
500 }
501 }
502 MEM_MALLOC.free(param);
503 }
504
SMP_RemotePairResponseReply(uint16_t handle,bool accept,uint8_t rejectReason)505 int SMP_RemotePairResponseReply(uint16_t handle, bool accept, uint8_t rejectReason)
506 {
507 LOG_INFO("%{public}s", __FUNCTION__);
508 SMP_RemotePairRspReplyTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_RemotePairRspReplyTask_t));
509 if (ctx == NULL) {
510 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
511 return SMP_ERR_OUT_OF_RES;
512 }
513 (void)memset_s(ctx, sizeof(SMP_RemotePairRspReplyTask_t), 0x00, sizeof(SMP_RemotePairRspReplyTask_t));
514 ctx->handle = handle;
515 ctx->accept = accept;
516 ctx->rejectReason = rejectReason;
517 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_RemotePairRspReplyTask, (void *)ctx);
518 if (ret != SMP_SUCCESS) {
519 MEM_MALLOC.free(ctx);
520 return ret;
521 }
522 return ret;
523 }
524
SMP_RemotePairRspReplyTask(void * context)525 static void SMP_RemotePairRspReplyTask(void *context)
526 {
527 SMP_RemotePairRspReplyTask_t *param = (SMP_RemotePairRspReplyTask_t *)context;
528
529 LOG_DEBUG("%{public}s", __FUNCTION__);
530 int ret = SMP_PairRspReplyJudgeException(param->handle, param->accept, param->rejectReason);
531 if (!ret) {
532 SMP_CalculatePairType(&g_smpPairMng);
533 SMP_CalculateEncKeySize(&g_smpPairMng);
534 SMP_CalculatePairMethod(&g_smpPairMng);
535 SMP_CalculateKeyDistribution(&g_smpPairMng);
536
537 if (g_smpPairMng.pairType == SMP_PAIR_TYPE_LEGACY) {
538 LOG_INFO("It's legacy pair.");
539 SMP_PairRspReplyLegacyPair();
540 } else {
541 LOG_INFO("It's SC pair.");
542 SMP_PairRspReplyScPair();
543 }
544 }
545 MEM_MALLOC.free(param);
546 }
547
SMP_RemoteSecurityRequestReply(uint16_t handle,bool accept,uint8_t rejectReason)548 int SMP_RemoteSecurityRequestReply(uint16_t handle, bool accept, uint8_t rejectReason)
549 {
550 LOG_INFO("%{public}s", __FUNCTION__);
551 SMP_RemoteSecReqReplyTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_RemoteSecReqReplyTask_t));
552 if (ctx == NULL) {
553 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
554 return SMP_ERR_OUT_OF_RES;
555 }
556 (void)memset_s(ctx, sizeof(SMP_RemoteSecReqReplyTask_t), 0x00, sizeof(SMP_RemoteSecReqReplyTask_t));
557 ctx->handle = handle;
558 ctx->accept = accept;
559 ctx->rejectReason = rejectReason;
560 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_RemoteSecReqReplyTask, (void *)ctx);
561 if (ret != SMP_SUCCESS) {
562 MEM_MALLOC.free(ctx);
563 return ret;
564 }
565 return ret;
566 }
567
SMP_RemoteSecReqReplyTask(void * context)568 static void SMP_RemoteSecReqReplyTask(void *context)
569 {
570 SMP_RemoteSecReqReplyTask_t *param = (SMP_RemoteSecReqReplyTask_t *)context;
571 LOG_DEBUG("%{public}s", __FUNCTION__);
572 if (!param->accept) {
573 LOG_ERROR("User rejected.");
574 SMP_SendPairingFailed(param->handle, param->rejectReason, SMP_SendPairingFailedCallback);
575 }
576 MEM_MALLOC.free(param);
577 }
578
SMP_LongTermKeyRequestReply(uint16_t handle,bool accept,const uint8_t * key)579 int SMP_LongTermKeyRequestReply(uint16_t handle, bool accept, const uint8_t *key)
580 {
581 LOG_INFO("%{public}s", __FUNCTION__);
582 SMP_LongTermKeyReqReplyTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_LongTermKeyReqReplyTask_t));
583 if (ctx == NULL) {
584 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
585 return SMP_ERR_OUT_OF_RES;
586 }
587 (void)memset_s(ctx, sizeof(SMP_LongTermKeyReqReplyTask_t), 0x00, sizeof(SMP_LongTermKeyReqReplyTask_t));
588 ctx->handle = handle;
589 ctx->accept = accept;
590 if (key != NULL) {
591 (void)memcpy_s(ctx->key, SMP_LTK_LEN, key, SMP_LTK_LEN);
592 }
593 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_LongTermKeyReqReplyTask, (void *)ctx);
594 if (ret != SMP_SUCCESS) {
595 MEM_MALLOC.free(ctx);
596 return ret;
597 }
598 return ret;
599 }
600
SMP_LongTermKeyReqReplyTask(void * context)601 static void SMP_LongTermKeyReqReplyTask(void *context)
602 {
603 SMP_LongTermKeyReqReplyTask_t *param = (SMP_LongTermKeyReqReplyTask_t *)context;
604
605 LOG_DEBUG("%{public}s", __FUNCTION__);
606 if ((g_smpPairMng.state == SMP_STATE_PAIRING) && (g_smpPairMng.handle == param->handle)) {
607 SMP_LtkReqReplyException(param->handle);
608 } else {
609 SMP_LtkReqReplyNormal(param->handle, param->accept, param->key);
610 }
611 MEM_MALLOC.free(param);
612 }
613
SMP_LtkReqReplyNormal(uint16_t handle,bool accept,const uint8_t * key)614 static int SMP_LtkReqReplyNormal(uint16_t handle, bool accept, const uint8_t *key)
615 {
616 int ret = SMP_SUCCESS;
617 HciLeLongTermKeyRequestReplyParam replycmdParam;
618 HciLeLongTermKeyRequestNegativeReplyParam negativeReplyParam;
619
620 if (!accept) {
621 LOG_ERROR("User rejected.");
622 negativeReplyParam.connectionHandle = handle;
623 HCI_LeLongTermKeyRequestNegativeReply(&negativeReplyParam);
624 SMP_NotifyCbEncComp(handle, SMP_ENCRYPT_STATUS_FAILED);
625 } else {
626 replycmdParam.connectionHandle = handle;
627 (void)memcpy_s(replycmdParam.longTermKey, SMP_LTK_LEN, key, SMP_LTK_LEN);
628 ret = HCI_LeLongTermKeyRequestReply(&replycmdParam);
629 if (ret != SMP_SUCCESS) {
630 SMP_NotifyCbEncComp(handle, SMP_ENCRYPT_STATUS_FAILED);
631 }
632 }
633
634 return ret;
635 }
636
SMP_LtkReqReplyException(uint16_t handle)637 static int SMP_LtkReqReplyException(uint16_t handle)
638 {
639 int ret = SMP_SUCCESS;
640 HciLeLongTermKeyRequestNegativeReplyParam negativeReplyParam;
641
642 LOG_ERROR("Invalid state.");
643 negativeReplyParam.connectionHandle = handle;
644 HCI_LeLongTermKeyRequestNegativeReply(&negativeReplyParam);
645 SMP_NotifyCbEncComp(handle, SMP_ENCRYPT_STATUS_FAILED);
646
647 return ret;
648 }
649
SMP_StartEncryption(uint16_t handle,const uint8_t * random,uint16_t ediv,const uint8_t * key)650 int SMP_StartEncryption(uint16_t handle, const uint8_t *random, uint16_t ediv, const uint8_t *key)
651 {
652 LOG_INFO("%{public}s", __FUNCTION__);
653 SMP_StartEncTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_StartEncTask_t));
654 if (ctx == NULL) {
655 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
656 return SMP_ERR_OUT_OF_RES;
657 }
658 (void)memset_s(ctx, sizeof(SMP_StartEncTask_t), 0x00, sizeof(SMP_StartEncTask_t));
659 ctx->handle = handle;
660 (void)memcpy_s(ctx->random, SMP_MASTER_RAND_LEN, random, SMP_MASTER_RAND_LEN);
661 ctx->ediv = ediv;
662 (void)memcpy_s(ctx->key, SMP_LTK_LEN, key, SMP_LTK_LEN);
663 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_StartEncTask, (void *)ctx);
664 if (ret != SMP_SUCCESS) {
665 MEM_MALLOC.free(ctx);
666 return ret;
667 }
668 return ret;
669 }
670
SMP_StartEncTask(void * context)671 static void SMP_StartEncTask(void *context)
672 {
673 int ret;
674 HciLeStartEncryptionParam startEncParam;
675 SMP_StartEncTask_t *param = (SMP_StartEncTask_t *)context;
676
677 LOG_DEBUG("%{public}s", __FUNCTION__);
678 if ((g_smpPairMng.state == SMP_STATE_PAIRING) && (g_smpPairMng.handle == param->handle)) {
679 LOG_ERROR("Invalid state.");
680 ret = SMP_ERR_INVAL_STATE;
681 } else {
682 uint8_t edivTemp[sizeof(param->ediv)] = {0x00};
683 startEncParam.connectionHandle = param->handle;
684 edivTemp[0x00] = ((uint8_t)((param->ediv) & 0xFF));
685 edivTemp[0x01] = ((uint8_t)(((param->ediv) >> 0x08) & 0xFF));
686 (void)memcpy_s(&startEncParam.encryptDiversifier, sizeof(edivTemp), edivTemp, sizeof(edivTemp));
687 SMP_MemoryReverseCopy(startEncParam.randomNumber, param->random, SMP_MASTER_RAND_LEN);
688 (void)memcpy_s(startEncParam.longTermKey, SMP_LTK_LEN, param->key, SMP_LTK_LEN);
689 ret = HCI_LeStartEncryption(&startEncParam);
690 if (ret != SMP_SUCCESS) {
691 LOG_ERROR("HCI_LeStartEncryption failed.");
692 }
693 }
694 if (ret != SMP_SUCCESS) {
695 SMP_NotifyCbEncComp(param->handle, SMP_ENCRYPT_STATUS_FAILED);
696 }
697 MEM_MALLOC.free(param);
698 }
699
SMP_PairTimeout(void * parameter)700 void SMP_PairTimeout(void *parameter)
701 {
702 LOG_INFO("%{public}s", __FUNCTION__);
703 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_PairTimeoutTask, NULL);
704 if (ret != SMP_SUCCESS) {
705 return;
706 }
707 (void)parameter;
708 }
709
SMP_PairTimeoutTask(void * context)710 static void SMP_PairTimeoutTask(void *context)
711 {
712 LOG_DEBUG("%{public}s", __FUNCTION__);
713 if (g_smpPairMng.state != SMP_STATE_PAIRING) {
714 LOG_ERROR("It's not pairing state. ");
715 return;
716 }
717 if (g_smpPairMng.role == SMP_ROLE_MASTER) {
718 LOG_ERROR("Master, step:%hu. ", g_smpPairMng.step);
719 } else {
720 LOG_ERROR("Slave, step:%hu. ", g_smpPairMng.step);
721 }
722 SMP_GeneratePairResult(g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, 0x00, NULL);
723 (void)context;
724 }
725
SMP_SendSecurityRequestToRemote(uint16_t handle,uint8_t authReq)726 int SMP_SendSecurityRequestToRemote(uint16_t handle, uint8_t authReq)
727 {
728 LOG_INFO("%{public}s", __FUNCTION__);
729 SMP_SendSecReqToRemoteTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SendSecReqToRemoteTask_t));
730 if (ctx == NULL) {
731 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
732 return SMP_ERR_OUT_OF_RES;
733 }
734 (void)memset_s(ctx, sizeof(SMP_SendSecReqToRemoteTask_t), 0x00, sizeof(SMP_SendSecReqToRemoteTask_t));
735 ctx->handle = handle;
736 ctx->authReq = authReq;
737 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SendSecReqToRemoteTask, (void *)ctx);
738 if (ret != SMP_SUCCESS) {
739 MEM_MALLOC.free(ctx);
740 return ret;
741 }
742 return ret;
743 }
744
SMP_SendSecReqToRemoteTask(void * context)745 static void SMP_SendSecReqToRemoteTask(void *context)
746 {
747 SMP_SendSecReqToRemoteTask_t *param = (SMP_SendSecReqToRemoteTask_t *)context;
748 LOG_DEBUG("%{public}s", __FUNCTION__);
749 SMP_SendSecurityRequest(param->handle, param->authReq, SMP_SendSecurityRequestCallback);
750 MEM_MALLOC.free(param);
751 }
752
SMP_GenerateSignature(const uint8_t * csrk,uint32_t counter,const uint8_t * data,uint16_t dataLen)753 int SMP_GenerateSignature(const uint8_t *csrk, uint32_t counter, const uint8_t *data, uint16_t dataLen)
754 {
755 int ret;
756 LOG_INFO("%{public}s", __FUNCTION__);
757 SMP_GenSignTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_GenSignTask_t));
758 if (ctx == NULL) {
759 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
760 return SMP_ERR_OUT_OF_RES;
761 }
762 (void)memset_s(ctx, sizeof(SMP_GenSignTask_t), 0x00, sizeof(SMP_GenSignTask_t));
763 (void)memcpy_s(ctx->csrk, SMP_CSRK_LEN, csrk, SMP_CSRK_LEN);
764 ctx->counter = counter;
765 ctx->dataLen = dataLen;
766 ctx->data = MEM_MALLOC.alloc(dataLen);
767 (void)memcpy_s(ctx->data, dataLen, data, dataLen);
768 ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_GenSignTask, (void *)ctx);
769 if (ret != SMP_SUCCESS) {
770 MEM_MALLOC.free(ctx);
771 return ret;
772 }
773 return ret;
774 }
775
SMP_GenSignTask(void * context)776 static void SMP_GenSignTask(void *context)
777 {
778 uint8_t cryptAesCmacZ[CRYPT_AESCMAC_Z_LEN] = {0x00};
779 SMP_EncCmd *encCmd = SMP_AllocEncCmd();
780 if (encCmd == NULL) {
781 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
782 return;
783 }
784 HciLeEncryptParam encryptParam;
785 SMP_GenSignTask_t *param = (SMP_GenSignTask_t *)context;
786
787 LOG_DEBUG("%{public}s", __FUNCTION__);
788 encCmd->length = param->dataLen + SMP_SIGNCOUNTER_LEN;
789 encCmd->M = MEM_MALLOC.alloc(encCmd->length);
790 (void)memcpy_s(encCmd->M, param->dataLen, param->data, param->dataLen);
791 (void)memcpy_s(encCmd->M + param->dataLen, SMP_SIGNCOUNTER_LEN, (uint8_t *)¶m->counter, SMP_SIGNCOUNTER_LEN);
792 encCmd->signDataLen = param->dataLen + SMP_SIGNCOUNTER_LEN;
793 SMP_ReverseMemoryOrder(encCmd->M, encCmd->signDataLen);
794 (void)memcpy_s(encCmd->key, sizeof(encCmd->key), param->csrk, sizeof(encCmd->key));
795 encCmd->signCounter = param->counter;
796 (void)memcpy_s(encryptParam.key, SMP_CSRK_LEN, param->csrk, SMP_CSRK_LEN);
797 (void)memcpy_s(encryptParam.plaintextData, sizeof(encryptParam.plaintextData), cryptAesCmacZ, CRYPT_AESCMAC_Z_LEN);
798 LOG_DEBUG("SMP_GENERATE_SIGNATURE_STEP_1 started.");
799 int ret = SMP_SendLeEncryptCmd(&encryptParam, SMP_GENERATE_SIGNATURE_STEP_1, encCmd, SMP_USING_HW_AES128_SIGN);
800 (void)memset_s(encryptParam.key, SMP_ENCRYPT_KEY_LEN, 0x00, SMP_ENCRYPT_KEY_LEN);
801 if (ret != SMP_SUCCESS) {
802 SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL);
803 }
804 SMP_FreeEncCmd(encCmd);
805 MEM_MALLOC.free(param->data);
806 MEM_MALLOC.free(param);
807 }
808
SMP_ResolveRPA(const uint8_t * addr,const uint8_t * irk)809 int SMP_ResolveRPA(const uint8_t *addr, const uint8_t *irk)
810 {
811 LOG_INFO("%{public}s", __FUNCTION__);
812 int ret;
813 uint8_t message[SMP_ENCRYPT_PLAINTEXTDATA_LEN] = {0x00};
814 (void)memcpy_s(message, sizeof(message), addr + SMP_RPA_HIGH_BIT_LEN, SMP_RPA_HIGH_BIT_LEN);
815 uint8_t encryptedData[SMP_ENCRYPT_PLAINTEXTDATA_LEN] = {0x00};
816 ret = SMP_Aes128(irk, SMP_IRK_LEN, message, sizeof(message), encryptedData);
817 if (ret != SMP_SUCCESS) {
818 LOG_ERROR("status = %{public}d.", ret);
819 ret = SMP_ERR_INVAL_STATE;
820 return ret;
821 }
822 if (memcmp(encryptedData, addr, SMP_RPA_HIGH_BIT_LEN) != 0x00) {
823 LOG_INFO("Resolve RPA failed");
824 ret = SMP_RESOLVE_RPA_RESULT_NO;
825 } else {
826 LOG_INFO("Resolve RPA success");
827 ret = SMP_RESOLVE_RPA_RESULT_YES;
828 }
829 return ret;
830 }
831
SMP_AsyncResolveRPA(const uint8_t * addr,const uint8_t * irk)832 int SMP_AsyncResolveRPA(const uint8_t *addr, const uint8_t *irk)
833 {
834 LOG_INFO("%{public}s", __FUNCTION__);
835 SMP_AsyncResoRpaTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_AsyncResoRpaTask_t));
836 if (ctx == NULL) {
837 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
838 return SMP_ERR_OUT_OF_RES;
839 }
840 (void)memset_s(ctx, sizeof(SMP_AsyncResoRpaTask_t), 0x00, sizeof(SMP_AsyncResoRpaTask_t));
841 (void)memcpy_s(ctx->addr, sizeof(ctx->addr), addr, sizeof(ctx->addr));
842 (void)memcpy_s(ctx->irk, sizeof(ctx->irk), irk, SMP_IRK_LEN);
843 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_AsyncResoRpaTask, (void *)ctx);
844 if (ret != SMP_SUCCESS) {
845 MEM_MALLOC.free(ctx);
846 return ret;
847 }
848 return ret;
849 }
850
SMP_AsyncResoRpaTask(void * context)851 static void SMP_AsyncResoRpaTask(void *context)
852 {
853 uint8_t message[SMP_ENCRYPT_PLAINTEXTDATA_LEN] = {0x00};
854 SMP_EncCmd *encCmd = SMP_AllocEncCmd();
855 if (encCmd == NULL) {
856 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
857 return;
858 }
859 HciLeEncryptParam encryptParam;
860 SMP_AsyncResoRpaTask_t *param = (SMP_AsyncResoRpaTask_t *)context;
861
862 LOG_DEBUG("%{public}s", __FUNCTION__);
863 (void)memcpy_s(message, sizeof(message), param->addr + SMP_RPA_HIGH_BIT_LEN, SMP_RPA_HIGH_BIT_LEN);
864 (void)memcpy_s(encCmd->address, sizeof(encCmd->address), param->addr, sizeof(encCmd->address));
865 (void)memcpy_s(encCmd->key, sizeof(encCmd->key), param->irk, SMP_IRK_LEN);
866 (void)memcpy_s(encryptParam.key, sizeof(encryptParam.key), param->irk, sizeof(encryptParam.key));
867 (void)memcpy_s(encryptParam.plaintextData, sizeof(encryptParam.plaintextData), message, sizeof(message));
868 LOG_DEBUG("SMP_RESOLVE_RPA_STEP_1 started.");
869 int ret = SMP_SendLeEncryptCmd(&encryptParam, SMP_RESOLVE_RPA_STEP_1, encCmd, SMP_USING_HW_AES128_RPA);
870 (void)memset_s(encryptParam.key, SMP_ENCRYPT_KEY_LEN, 0x00, SMP_ENCRYPT_KEY_LEN);
871 if (ret != SMP_SUCCESS) {
872 SMP_NotifyCbResoRpa(SMP_RESOLVE_RPA_STATUS_FAILED, false, param->addr, param->irk);
873 }
874 SMP_FreeEncCmd(encCmd);
875 MEM_MALLOC.free(param);
876 }
877
SMP_GenerateRPA(const uint8_t * irk)878 int SMP_GenerateRPA(const uint8_t *irk)
879 {
880 LOG_INFO("%{public}s", __FUNCTION__);
881 SMP_GenRpaTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_GenRpaTask_t));
882 if (ctx == NULL) {
883 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
884 return SMP_ERR_OUT_OF_RES;
885 }
886 (void)memset_s(ctx, sizeof(SMP_GenRpaTask_t), 0x00, sizeof(SMP_GenRpaTask_t));
887 (void)memcpy_s(ctx->irk, sizeof(ctx->irk), irk, SMP_IRK_LEN);
888 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_GenRpaTask, (void *)ctx);
889 if (ret != SMP_SUCCESS) {
890 MEM_MALLOC.free(ctx);
891 return ret;
892 }
893 return ret;
894 }
895
SMP_GenRpaTask(void * context)896 static void SMP_GenRpaTask(void *context)
897 {
898 uint32_t number = RandomGenerate();
899 uint8_t address[BT_ADDRESS_SIZE] = {0x00};
900 uint8_t message[SMP_ENCRYPT_PLAINTEXTDATA_LEN] = {0x00};
901 SMP_EncCmd *encCmd = SMP_AllocEncCmd();
902 if (encCmd == NULL) {
903 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
904 return;
905 }
906 HciLeEncryptParam encryptParam;
907 SMP_GenRpaTask_t *param = (SMP_GenRpaTask_t *)context;
908
909 LOG_DEBUG("%{public}s", __FUNCTION__);
910 (void)memcpy_s(address, sizeof(address), (uint8_t *)&number, SMP_RPA_HIGH_BIT_LEN);
911 address[0x00] &= 0x7Fu;
912 address[0x00] |= 0x40u;
913 for (int i = 0; i < (BT_ADDRESS_SIZE - SMP_RPA_HIGH_BIT_LEN); i++) {
914 message[sizeof(message) - SMP_RPA_HIGH_BIT_LEN + i] = address[i];
915 }
916 (void)memcpy_s(encCmd->address, sizeof(encCmd->address), address, sizeof(encCmd->address));
917 (void)memcpy_s(encryptParam.key, sizeof(encryptParam.key), param->irk, sizeof(encryptParam.key));
918 SMP_MemoryReverseCopy(encryptParam.plaintextData, message, sizeof(message));
919 LOG_DEBUG("SMP_GENERATE_RPA_STEP_1 started.");
920 int ret = SMP_SendLeEncryptCmd(&encryptParam, SMP_GENERATE_RPA_STEP_1, encCmd, SMP_USING_HW_AES128_RPA);
921 (void)memset_s(encryptParam.key, SMP_ENCRYPT_KEY_LEN, 0x00, SMP_ENCRYPT_KEY_LEN);
922 if (ret != SMP_SUCCESS) {
923 SMP_NotifyCbGenRpa(SMP_GENERATE_RPA_STATUS_FAILED, address);
924 }
925 SMP_FreeEncCmd(encCmd);
926 MEM_MALLOC.free(param);
927 }
928
SMP_SetSecureConnOnlyMode(bool mode)929 int SMP_SetSecureConnOnlyMode(bool mode)
930 {
931 LOG_INFO("%{public}s", __FUNCTION__);
932 SMP_SetSecConnOnlyModeTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SetSecConnOnlyModeTask_t));
933 if (ctx == NULL) {
934 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
935 return SMP_ERR_OUT_OF_RES;
936 }
937 (void)memset_s(ctx, sizeof(SMP_SetSecConnOnlyModeTask_t), 0x00, sizeof(SMP_SetSecConnOnlyModeTask_t));
938 ctx->mode = mode;
939 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SetSecConnOnlyModeTask, (void *)ctx);
940 if (ret != SMP_SUCCESS) {
941 MEM_MALLOC.free(ctx);
942 return ret;
943 }
944 return ret;
945 }
946
SMP_SetSecConnOnlyModeTask(void * context)947 static void SMP_SetSecConnOnlyModeTask(void *context)
948 {
949 SMP_SetSecConnOnlyModeTask_t *param = (SMP_SetSecConnOnlyModeTask_t *)context;
950 g_smpSecureConnOnlyMode = param->mode;
951 MEM_MALLOC.free(param);
952 }
953
SMP_GetSecureConnOnlyMode()954 bool SMP_GetSecureConnOnlyMode()
955 {
956 bool mode = false;
957
958 mode = g_smpSecureConnOnlyMode;
959
960 return mode;
961 }
962
SMP_SetIRK(const uint8_t * irk)963 int SMP_SetIRK(const uint8_t *irk)
964 {
965 LOG_INFO("%{public}s", __FUNCTION__);
966 SMP_SetIrkTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SetIrkTask_t));
967 if (ctx == NULL) {
968 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
969 return SMP_ERR_OUT_OF_RES;
970 }
971 (void)memset_s(ctx, sizeof(SMP_SetIrkTask_t), 0x00, sizeof(SMP_SetIrkTask_t));
972 (void)memcpy_s(ctx->irk, sizeof(ctx->irk), irk, SMP_IRK_LEN);
973 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SetIrkTask, (void *)ctx);
974 if (ret != SMP_SUCCESS) {
975 MEM_MALLOC.free(ctx);
976 return ret;
977 }
978 return ret;
979 }
980
SMP_SetIrkTask(void * context)981 static void SMP_SetIrkTask(void *context)
982 {
983 SMP_SetIrkTask_t *param = (SMP_SetIrkTask_t *)context;
984 SMP_SetLocalIrk(param->irk, SMP_IRK_LEN);
985 MEM_MALLOC.free(param);
986 }
987
SMP_SetIdentAddr(const BtAddr * addr)988 int SMP_SetIdentAddr(const BtAddr *addr)
989 {
990 LOG_INFO("%{public}s", __FUNCTION__);
991 SMP_SetIdentAddrTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SetIdentAddrTask_t));
992 if (ctx == NULL) {
993 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
994 return SMP_ERR_OUT_OF_RES;
995 }
996 (void)memset_s(ctx, sizeof(SMP_SetIdentAddrTask_t), 0x00, sizeof(SMP_SetIdentAddrTask_t));
997 (void)memcpy_s(&ctx->addr, sizeof(ctx->addr), addr, sizeof(BtAddr));
998 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SetIdentAddrTask, (void *)ctx);
999 if (ret != SMP_SUCCESS) {
1000 MEM_MALLOC.free(ctx);
1001 return ret;
1002 }
1003 return ret;
1004 }
1005
SMP_SetIdentAddrTask(void * context)1006 static void SMP_SetIdentAddrTask(void *context)
1007 {
1008 SMP_SetIdentAddrTask_t *param = (SMP_SetIdentAddrTask_t *)context;
1009 SMP_SetLocalIdentAddr(¶m->addr);
1010 MEM_MALLOC.free(param);
1011 }
1012
SMP_GeneratePairResult(uint16_t handle,uint8_t status,uint8_t failedReason,Alarm * cancelTimer)1013 void SMP_GeneratePairResult(uint16_t handle, uint8_t status, uint8_t failedReason, Alarm *cancelTimer)
1014 {
1015 if (g_smpPairMng.state != SMP_STATE_PAIRING) {
1016 return;
1017 }
1018
1019 if (status) {
1020 SMP_GenPairRetException(handle, status, failedReason, cancelTimer);
1021 } else {
1022 SMP_GenPairRetNormal(handle, status);
1023 }
1024 }
1025
SMP_EncryptCompleteJudgeException(uint8_t status,uint8_t role)1026 int SMP_EncryptCompleteJudgeException(uint8_t status, uint8_t role)
1027 {
1028 int ret = SMP_SUCCESS;
1029
1030 if (g_smpPairMng.state != SMP_STATE_PAIRING) {
1031 LOG_ERROR("It's not pairing state.");
1032 ret = SMP_ERR_INVAL_STATE;
1033 return ret;
1034 }
1035
1036 if (g_smpPairMng.role != role) {
1037 LOG_ERROR("Invalid role = %hhu.", g_smpPairMng.role);
1038 ret = SMP_ERR_INVAL_STATE;
1039 return ret;
1040 }
1041
1042 if (status) {
1043 LOG_ERROR("returnParam->status = %hhu.", status);
1044 SMP_GeneratePairResult(g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
1045 ret = SMP_ERR_INVAL_PARAM;
1046 return ret;
1047 }
1048
1049 return ret;
1050 }
1051
SMP_AuthReqReplyProcessMaster(bool accept,uint8_t rejectReason,uint8_t pairMethod,const uint8_t * entryValue)1052 static int SMP_AuthReqReplyProcessMaster(
1053 bool accept, uint8_t rejectReason, uint8_t pairMethod, const uint8_t *entryValue)
1054 {
1055 LOG_INFO("%{public}s", __FUNCTION__);
1056 int ret = SMP_AuthReqReplyJudgeExceptionMaster(accept, rejectReason);
1057 if (ret != SMP_SUCCESS) {
1058 return ret;
1059 }
1060 SMP_AuthReqReplyObtainEntryValue(pairMethod, entryValue);
1061 ret = SMP_AuthReqReplyStepTransMaster(pairMethod);
1062
1063 return ret;
1064 }
1065
SMP_AuthReqReplyJudgeExceptionMaster(bool accept,uint8_t rejectReason)1066 static int SMP_AuthReqReplyJudgeExceptionMaster(bool accept, uint8_t rejectReason)
1067 {
1068 int ret = SMP_SUCCESS;
1069
1070 if ((g_smpPairMng.step != SMP_LEGACY_PAIR_MASTER_STEP_2) &&
1071 (g_smpPairMng.step != SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_5) &&
1072 (g_smpPairMng.step != SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_14) &&
1073 (g_smpPairMng.step != SMP_SC_PAIR_OOB_MASTER_STEP_7)) {
1074 ret = SMP_ERR_INVAL_STATE;
1075 LOG_ERROR("It's invalid step:%hu. ", g_smpPairMng.step);
1076 return ret;
1077 }
1078 AlarmCancel(g_smpPairMng.alarm);
1079 if (!accept) {
1080 LOG_ERROR("User rejected. ");
1081 ret = SMP_ERR_INVAL_STATE;
1082 SMP_GeneratePairResult(g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, rejectReason, NULL);
1083 return ret;
1084 }
1085
1086 return ret;
1087 }
1088
SMP_AuthReqReplyStepTransMaster(uint8_t pairMethod)1089 static int SMP_AuthReqReplyStepTransMaster(uint8_t pairMethod)
1090 {
1091 int ret = SMP_SUCCESS;
1092 uint8_t cryptAesCmacZ[CRYPT_AESCMAC_Z_LEN] = {0x00};
1093 HciLeEncryptParam encryptParam;
1094 if (g_smpPairMng.pairType == SMP_PAIR_TYPE_LEGACY) {
1095 ret = SMP_SendHciLeRandCmd(0x00);
1096 } else {
1097 if (SMP_IsPasskeyEntryPair(pairMethod)) {
1098 LOG_DEBUG("SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_6 started.");
1099 ret = SMP_SendHciLeRandCmd(SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_6);
1100 } else if (pairMethod == SMP_PAIR_METHOD_NUMERIC_COMPARISON) {
1101 SMP_MemoryReverseCopy(encryptParam.key, SALT, sizeof(encryptParam.key));
1102 (void)memcpy_s(
1103 encryptParam.plaintextData, sizeof(encryptParam.plaintextData), cryptAesCmacZ, CRYPT_AESCMAC_Z_LEN);
1104 LOG_DEBUG("SMP_SC_PAIR_COMMON_MASTER_STEP_1 started.");
1105 ret = SMP_SendLeEncryptCmd(&encryptParam, SMP_SC_PAIR_COMMON_MASTER_STEP_1, NULL, SMP_USING_HW_AES128_PAIR);
1106 } else if (SMP_IsScOobPair(pairMethod)) {
1107 LOG_DEBUG("SMP_SC_PAIR_OOB_MASTER_STEP_8 started.");
1108 g_smpPairMng.step = SMP_SC_PAIR_OOB_MASTER_STEP_8;
1109 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1110 ret = SMP_SendPairingPublicKey(g_smpPairMng.handle, g_smpPairMng.local.publicKey, SMP_SendDataCallback);
1111 if (ret != SMP_SUCCESS) {
1112 LOG_ERROR("Send pairing public key failed.");
1113 AlarmCancel(g_smpPairMng.alarm);
1114 }
1115 }
1116 }
1117
1118 if (ret != SMP_SUCCESS) {
1119 SMP_GeneratePairResult(g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
1120 }
1121 (void)memset_s(encryptParam.key, SMP_ENCRYPT_KEY_LEN, 0x00, SMP_ENCRYPT_KEY_LEN);
1122 return ret;
1123 }
1124
SMP_AuthReqReplyProcessSlave(bool accept,uint8_t rejectReason,uint8_t pairMethod,const uint8_t * entryValue)1125 static int SMP_AuthReqReplyProcessSlave(
1126 bool accept, uint8_t rejectReason, uint8_t pairMethod, const uint8_t *entryValue)
1127 {
1128 LOG_INFO("%{public}s", __FUNCTION__);
1129 int ret = SMP_AuthReqReplyJudgeExceptionSlave(accept, rejectReason);
1130 if (ret != SMP_SUCCESS) {
1131 return ret;
1132 }
1133 SMP_AuthReqReplyObtainEntryValue(pairMethod, entryValue);
1134 ret = SMP_AuthReqReplyStepTransSlave(pairMethod);
1135
1136 return ret;
1137 }
1138
SMP_AuthReqReplyJudgeExceptionSlave(bool accept,uint8_t rejectReason)1139 static int SMP_AuthReqReplyJudgeExceptionSlave(bool accept, uint8_t rejectReason)
1140 {
1141 int ret = SMP_SUCCESS;
1142 LOG_DEBUG("%{public}s", __FUNCTION__);
1143 if ((g_smpPairMng.step != SMP_LEGACY_PAIR_SLAVE_STEP_2) &&
1144 (g_smpPairMng.step != SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_5) &&
1145 (g_smpPairMng.step != SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_13) &&
1146 (g_smpPairMng.step != SMP_SC_PAIR_OOB_SLAVE_STEP_7)) {
1147 ret = SMP_ERR_INVAL_STATE;
1148 LOG_ERROR("It's invalid step:%hu", g_smpPairMng.step);
1149 return ret;
1150 }
1151 AlarmCancel(g_smpPairMng.alarm);
1152 if (!accept) {
1153 ret = SMP_ERR_INVAL_STATE;
1154 SMP_GeneratePairResult(g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, rejectReason, NULL);
1155 LOG_ERROR("User rejected");
1156 return ret;
1157 }
1158
1159 return ret;
1160 }
1161
SMP_AuthReqReplyStepTransSlave(uint8_t pairMethod)1162 static int SMP_AuthReqReplyStepTransSlave(uint8_t pairMethod)
1163 {
1164 int ret = SMP_SUCCESS;
1165 LOG_DEBUG("%{public}s", __FUNCTION__);
1166 if (g_smpPairMng.pairType == SMP_PAIR_TYPE_LEGACY) {
1167 if (g_smpPairMng.slaveConfirmRecvFlag) {
1168 LOG_DEBUG("SMP_LEGACY_PAIR_SLAVE_STEP_4 started.");
1169 ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_SLAVE_STEP_4);
1170 if (ret != SMP_SUCCESS) {
1171 SMP_GeneratePairResult(
1172 g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
1173 }
1174 } else {
1175 LOG_DEBUG("SMP_LEGACY_PAIR_SLAVE_STEP_3 started.");
1176 g_smpPairMng.step = SMP_LEGACY_PAIR_SLAVE_STEP_3;
1177 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1178 }
1179 } else {
1180 if (SMP_IsPasskeyEntryPair(pairMethod)) {
1181 ret = SMP_AuthReqReplyPasskeyEntrySlave();
1182 } else if (pairMethod == SMP_PAIR_METHOD_NUMERIC_COMPARISON) {
1183 ret = SMP_AuthReqReplyNumericSlave();
1184 } else if (SMP_IsScOobPair(pairMethod)) {
1185 ret = SMP_AuthReqReplyOobSlave();
1186 }
1187 }
1188
1189 return ret;
1190 }
1191
SMP_AuthReqReplyNumericSlave()1192 static int SMP_AuthReqReplyNumericSlave()
1193 {
1194 int ret = SMP_SUCCESS;
1195 HciLeEncryptParam encryptParam;
1196 LOG_DEBUG("%{public}s", __FUNCTION__);
1197 if (g_smpPairMng.slaveDHKeyCheckRecvFlag) {
1198 uint8_t cryptAesCmacZ[CRYPT_AESCMAC_Z_LEN] = {0x00};
1199 SMP_MemoryReverseCopy(encryptParam.key, SALT, sizeof(encryptParam.key));
1200 (void)memcpy_s(
1201 encryptParam.plaintextData, sizeof(encryptParam.plaintextData), cryptAesCmacZ, CRYPT_AESCMAC_Z_LEN);
1202 LOG_DEBUG("SMP_SC_PAIR_COMMON_SLAVE_STEP_2 started.");
1203 ret = SMP_SendLeEncryptCmd(&encryptParam, SMP_SC_PAIR_COMMON_SLAVE_STEP_2, NULL, SMP_USING_HW_AES128_PAIR);
1204 if (ret != SMP_SUCCESS) {
1205 SMP_GeneratePairResult(
1206 g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
1207 }
1208 } else {
1209 LOG_DEBUG("SMP_SC_PAIR_COMMON_SLAVE_STEP_1 started.");
1210 g_smpPairMng.step = SMP_SC_PAIR_COMMON_SLAVE_STEP_1;
1211 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1212 }
1213 (void)memset_s(encryptParam.key, SMP_ENCRYPT_KEY_LEN, 0x00, SMP_ENCRYPT_KEY_LEN);
1214 return ret;
1215 }
1216
SMP_AuthReqReplyPasskeyEntrySlave()1217 static int SMP_AuthReqReplyPasskeyEntrySlave()
1218 {
1219 int ret = SMP_SUCCESS;
1220 LOG_DEBUG("%{public}s", __FUNCTION__);
1221 if (g_smpPairMng.slaveConfirmRecvFlag) {
1222 LOG_DEBUG("SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_7 started.");
1223 ret = SMP_SendHciLeRandCmd(SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_7);
1224 if (ret != SMP_SUCCESS) {
1225 SMP_GeneratePairResult(
1226 g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
1227 }
1228 } else {
1229 LOG_DEBUG("SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_6 started.");
1230 g_smpPairMng.step = SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_6;
1231 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1232 }
1233
1234 return ret;
1235 }
1236
SMP_AuthReqReplyOobSlave()1237 static int SMP_AuthReqReplyOobSlave()
1238 {
1239 int ret = SMP_SUCCESS;
1240 HciLeGenerateDHKeyParam DHKeyParam;
1241
1242 if (g_smpPairMng.slavePubKeyRecvFlag) {
1243 g_smpPairMng.step = SMP_SC_PAIR_OOB_SLAVE_STEP_9;
1244 (void)memcpy_s(
1245 DHKeyParam.remoteP256PublicKey, SMP_PUBLICKEY_LEN, g_smpPairMng.peer.publicKey, SMP_PUBLICKEY_LEN);
1246 LOG_DEBUG("SMP_SC_PAIR_OOB_SLAVE_STEP_9 started. ");
1247 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1248 ret = HCI_LeGenerateDHKey(&DHKeyParam);
1249 if (ret != SMP_SUCCESS) {
1250 LOG_ERROR("HCI_LeGenerateDHKey failed. ");
1251 SMP_GeneratePairResult(
1252 g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, g_smpPairMng.alarm);
1253 }
1254 } else {
1255 LOG_DEBUG("SMP_SC_PAIR_OOB_SLAVE_STEP_8 started. ");
1256 g_smpPairMng.step = SMP_SC_PAIR_OOB_SLAVE_STEP_8;
1257 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1258 }
1259
1260 return ret;
1261 }
1262
SMP_AuthReqReplyObtainEntryValue(uint8_t pairMethod,const uint8_t * entryValue)1263 static void SMP_AuthReqReplyObtainEntryValue(uint8_t pairMethod, const uint8_t *entryValue)
1264 {
1265 if (SMP_IsPasskeyEntryPair(pairMethod)) {
1266 LOG_INFO("SMP_PAIR_METHOD_PASSKEY_ENTRY.");
1267 LOG_INFO("passkey is %u.", *((uint32_t *)entryValue));
1268 (void)memset_s(g_smpPairMng.TK, SMP_TK_LEN, 0x00, sizeof(g_smpPairMng.TK));
1269 for (int i = 0; i < (int)sizeof(*((uint32_t *)entryValue)); i++) {
1270 g_smpPairMng.TK[SMP_TK_LEN - 1 - i] = entryValue[i];
1271 }
1272 } else if (pairMethod == SMP_PAIR_METHOD_OOB_LEGACY) {
1273 LOG_INFO("SMP_PAIR_METHOD_OOB_LEGACY.");
1274 (void)memcpy_s(g_smpPairMng.TK, SMP_TK_LEN, entryValue, SMP_TK_LEN);
1275 } else if ((pairMethod == SMP_PAIR_METHOD_OOB_SC_BOTH_SIDE_SEND_RECV) ||
1276 (pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND)) {
1277 LOG_INFO("SMP_PAIR_METHOD_OOB_SC_BOTH_SIDE_SEND_RECV or ");
1278 LOG_INFO("SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND");
1279 SMP_MemoryReverseCopy(g_smpPairMng.peer.addr.addr, entryValue, sizeof(g_smpPairMng.peer.addr.addr));
1280 (void)memcpy_s(g_smpPairMng.peer.oobRand,
1281 SMP_RANDOM_DATA_LEN,
1282 entryValue + sizeof(g_smpPairMng.peer.addr.addr),
1283 SMP_RANDOM_DATA_LEN);
1284 SMP_MemoryReverseCopy(g_smpPairMng.peer.confirm,
1285 entryValue + sizeof(g_smpPairMng.peer.addr.addr) + SMP_RANDOM_DATA_LEN,
1286 SMP_CONFIRM_DATA_LEN);
1287 }
1288 }
1289
SMP_PairReqReplyJudgeException(uint16_t handle,bool accept,uint8_t rejectReason,const SMP_PairParam * param)1290 static int SMP_PairReqReplyJudgeException(
1291 uint16_t handle, bool accept, uint8_t rejectReason, const SMP_PairParam *param)
1292 {
1293 int ret = SMP_SUCCESS;
1294
1295 if (g_smpPairMng.state != SMP_STATE_PAIRING) {
1296 LOG_ERROR("It's not pairing state. ");
1297 ret = SMP_ERR_INVAL_STATE;
1298 return ret;
1299 }
1300 if (g_smpPairMng.handle != handle) {
1301 LOG_ERROR("Connection handle error. ");
1302 ret = SMP_ERR_INVAL_PARAM;
1303 return ret;
1304 }
1305 if (g_smpPairMng.role != SMP_ROLE_SLAVE) {
1306 LOG_ERROR("It's not accepter role.");
1307 ret = SMP_ERR_INVAL_STATE;
1308 return ret;
1309 }
1310 if (g_smpPairMng.step != SMP_LEGACY_PAIR_SLAVE_STEP_1) {
1311 ret = SMP_ERR_INVAL_STATE;
1312 LOG_ERROR("It's not SMP_LEGACY_PAIR_SLAVE_STEP_1. ");
1313 return ret;
1314 }
1315 AlarmCancel(g_smpPairMng.alarm);
1316
1317 if (!accept) {
1318 LOG_ERROR("User rejected. ");
1319 SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, rejectReason, NULL);
1320 ret = SMP_ERR_INVAL_STATE;
1321 return ret;
1322 }
1323 if (SMP_GetSecureConnOnlyMode() && (!(param->authReq & SMP_AUTH_REQ_BIT_SC))) {
1324 LOG_ERROR("SC bit is not set in authReq.");
1325 SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
1326 ret = SMP_ERR_INVAL_PARAM;
1327 return ret;
1328 }
1329
1330 return ret;
1331 }
1332
SMP_PairReqReplyLegacyPair()1333 static int SMP_PairReqReplyLegacyPair()
1334 {
1335 int ret = SMP_SUCCESS;
1336 uint16_t handle = g_smpPairMng.handle;
1337 uint8_t pairMethod = g_smpPairMng.local.pairMethod;
1338
1339 if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_JUST_WORK) {
1340 LOG_INFO("SMP_PAIR_METHOD_JUST_WORK.");
1341 (void)memset_s(g_smpPairMng.TK, SMP_TK_LEN, 0x00, sizeof(g_smpPairMng.TK));
1342 LOG_DEBUG("SMP_LEGACY_PAIR_SLAVE_STEP_3 started.");
1343 g_smpPairMng.step = SMP_LEGACY_PAIR_SLAVE_STEP_3;
1344 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1345 } else if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_PASSKEY_DISPLAY) {
1346 LOG_INFO("SMP_PAIR_METHOD_PASSKEY_DISPLAY.");
1347 g_smpPairMng.slaveConfirmRecvFlag = SMP_SLAVE_CONFIRM_RECV_FLAG_NO;
1348 ret = SMP_SendHciLeRandCmd(0x00);
1349 if (ret != SMP_SUCCESS) {
1350 SMP_GeneratePairResult(
1351 g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
1352 }
1353 } else {
1354 LOG_DEBUG("SMP_LEGACY_PAIR_SLAVE_STEP_2 started.");
1355 g_smpPairMng.slaveConfirmRecvFlag = SMP_SLAVE_CONFIRM_RECV_FLAG_NO;
1356 g_smpPairMng.step = SMP_LEGACY_PAIR_SLAVE_STEP_2;
1357 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1358 SMP_NotifyCbAuthReq(handle, pairMethod, NULL);
1359 }
1360 return ret;
1361 }
1362
SMP_PairReqReplyScPair()1363 static int SMP_PairReqReplyScPair()
1364 {
1365 int ret = SMP_SUCCESS;
1366
1367 if ((SMP_IsPasskeyEntryPair(g_smpPairMng.local.pairMethod)) ||
1368 (SMP_IsJustworkOrNumericPair(g_smpPairMng.local.pairMethod))) {
1369 LOG_INFO("It's not OOB");
1370 if (SMP_IsJustworkOrNumericPair(g_smpPairMng.local.pairMethod)) {
1371 LOG_DEBUG("SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_1 started.");
1372 g_smpPairMng.step = SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_1;
1373 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1374 } else {
1375 g_smpPairMng.scConfirmCheckCounter = 0x00;
1376 g_smpPairMng.slaveConfirmRecvFlag = SMP_SLAVE_CONFIRM_RECV_FLAG_NO;
1377 LOG_DEBUG("SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_1 started.");
1378 g_smpPairMng.step = SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_1;
1379 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1380 }
1381 } else {
1382 LOG_INFO("It's OOB.");
1383 ret = SMP_PairReqReplyScPairOob();
1384 }
1385
1386 return ret;
1387 }
1388
SMP_PairReqReplyScPairOob()1389 static int SMP_PairReqReplyScPairOob()
1390 {
1391 int ret = SMP_SUCCESS;
1392
1393 if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_SEND_PEER_RECV) {
1394 (void)memset_s(g_smpPairMng.peer.oobRand, SMP_RANDOM_DATA_LEN, 0x00, sizeof(g_smpPairMng.peer.oobRand));
1395 } else if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND) {
1396 (void)memset_s(g_smpPairMng.local.oobRand, SMP_RANDOM_DATA_LEN, 0x00, sizeof(g_smpPairMng.local.oobRand));
1397 LOG_DEBUG("SMP_SC_PAIR_OOB_SLAVE_STEP_1 started");
1398 g_smpPairMng.step = SMP_SC_PAIR_OOB_SLAVE_STEP_1;
1399 g_smpPairMng.slavePubKeyRecvFlag = SMP_SLAVE_PUBLICKEY_RECV_FLAG_NO;
1400 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1401 ret = HCI_LeReadLocalP256PublicKey();
1402 if (ret != SMP_SUCCESS) {
1403 SMP_GeneratePairResult(
1404 g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, g_smpPairMng.alarm);
1405 LOG_ERROR("HCI_LeReadLocalP256PublicKey failed");
1406 }
1407 }
1408
1409 if (g_smpPairMng.local.pairMethod != SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND) {
1410 uint8_t scOobData[SMP_SC_OOB_LEN] = {0x00};
1411 SMP_MemoryReverseCopy(g_smpPairMng.local.oobRand, SMP_GetScOobMng()->random, SMP_RANDOM_DATA_LEN);
1412 SMP_MemoryReverseCopy(scOobData, g_smpPairMng.local.addr.addr, sizeof(g_smpPairMng.local.addr.addr));
1413 SMP_MemoryReverseCopy(
1414 scOobData + sizeof(g_smpPairMng.local.addr.addr), SMP_GetScOobMng()->random, SMP_RANDOM_DATA_LEN);
1415 SMP_MemoryReverseCopy(scOobData + sizeof(g_smpPairMng.local.addr.addr) + SMP_RANDOM_DATA_LEN,
1416 SMP_GetScOobMng()->confirm,
1417 SMP_CONFIRM_DATA_LEN);
1418 (void)memcpy_s(
1419 g_smpPairMng.local.publicKey, SMP_PUBLICKEY_LEN, SMP_GetScOobMng()->publicKey, SMP_PUBLICKEY_LEN);
1420 LOG_DEBUG("SMP_SC_PAIR_OOB_SLAVE_STEP_7 started.");
1421 g_smpPairMng.step = SMP_SC_PAIR_OOB_SLAVE_STEP_7;
1422 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1423 SMP_NotifyCbAuthReq(g_smpPairMng.handle, g_smpPairMng.local.pairMethod, scOobData);
1424 }
1425
1426 return ret;
1427 }
1428
SMP_PairRspReplyJudgeException(uint16_t handle,bool accept,uint8_t rejectReason)1429 static int SMP_PairRspReplyJudgeException(uint16_t handle, bool accept, uint8_t rejectReason)
1430 {
1431 int ret = SMP_SUCCESS;
1432
1433 if (g_smpPairMng.state != SMP_STATE_PAIRING) {
1434 LOG_ERROR("It's not pairing state");
1435 ret = SMP_ERR_INVAL_STATE;
1436 return ret;
1437 }
1438 if (g_smpPairMng.handle != handle) {
1439 LOG_ERROR("Connection handle error");
1440 ret = SMP_ERR_INVAL_PARAM;
1441 return ret;
1442 }
1443 if (g_smpPairMng.role != SMP_ROLE_MASTER) {
1444 LOG_ERROR("It's not initiator role.");
1445 ret = SMP_ERR_INVAL_STATE;
1446 return ret;
1447 }
1448 if (g_smpPairMng.step != SMP_LEGACY_PAIR_MASTER_STEP_1) {
1449 ret = SMP_ERR_INVAL_STATE;
1450 LOG_ERROR("It's not SMP_LEGACY_PAIR_MASTER_STEP_1");
1451 return ret;
1452 }
1453 AlarmCancel(g_smpPairMng.alarm);
1454
1455 if (!accept) {
1456 LOG_ERROR("User rejected");
1457 SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, rejectReason, NULL);
1458 ret = SMP_ERR_INVAL_STATE;
1459 return ret;
1460 }
1461
1462 return ret;
1463 }
1464
SMP_PairRspReplyLegacyPair()1465 static int SMP_PairRspReplyLegacyPair()
1466 {
1467 int ret = SMP_SUCCESS;
1468 uint16_t handle = g_smpPairMng.handle;
1469 uint8_t pairMethod = g_smpPairMng.local.pairMethod;
1470
1471 if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_JUST_WORK) {
1472 LOG_INFO("SMP_PAIR_METHOD_JUST_WORK.");
1473 LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_2 started.");
1474 (void)memset_s(g_smpPairMng.TK, SMP_TK_LEN, 0x00, sizeof(g_smpPairMng.TK));
1475 ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_2);
1476 if (ret != SMP_SUCCESS) {
1477 SMP_GeneratePairResult(
1478 g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
1479 }
1480 } else if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_PASSKEY_DISPLAY) {
1481 LOG_INFO("SMP_PAIR_METHOD_PASSKEY_DISPLAY.");
1482 ret = SMP_SendHciLeRandCmd(0x00);
1483 if (ret != SMP_SUCCESS) {
1484 SMP_GeneratePairResult(
1485 g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
1486 }
1487 } else {
1488 LOG_INFO("Other pair method.");
1489 LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_2 started.");
1490 g_smpPairMng.step = SMP_LEGACY_PAIR_MASTER_STEP_2;
1491 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1492 SMP_NotifyCbAuthReq(handle, pairMethod, NULL);
1493 }
1494
1495 return ret;
1496 }
1497
SMP_PairRspReplyScPair()1498 static int SMP_PairRspReplyScPair()
1499 {
1500 int ret;
1501
1502 if ((SMP_IsJustworkOrNumericPair(g_smpPairMng.local.pairMethod)) ||
1503 (SMP_IsPasskeyEntryPair(g_smpPairMng.local.pairMethod))) {
1504 LOG_INFO("It's not OOB. ");
1505 if (SMP_IsJustworkOrNumericPair(g_smpPairMng.local.pairMethod)) {
1506 LOG_DEBUG("SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_1 started.");
1507 g_smpPairMng.step = SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_1;
1508 } else {
1509 LOG_DEBUG("SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_1 started.");
1510 g_smpPairMng.step = SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_1;
1511 }
1512 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1513 ret = HCI_LeReadLocalP256PublicKey();
1514 if (ret != SMP_SUCCESS) {
1515 LOG_ERROR("HCI_LeReadLocalP256PublicKey failed.");
1516 SMP_GeneratePairResult(
1517 g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, g_smpPairMng.alarm);
1518 }
1519 } else {
1520 LOG_INFO("It's OOB.");
1521 ret = SMP_PairRspReplyScPairOob();
1522 }
1523
1524 return ret;
1525 }
1526
SMP_PairRspReplyScPairOob()1527 static int SMP_PairRspReplyScPairOob()
1528 {
1529 int ret = SMP_SUCCESS;
1530
1531 if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_SEND_PEER_RECV) {
1532 (void)memset_s(g_smpPairMng.peer.oobRand, SMP_RANDOM_DATA_LEN, 0x00, SMP_RANDOM_DATA_LEN);
1533 } else if (g_smpPairMng.local.pairMethod == SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND) {
1534 (void)memset_s(g_smpPairMng.local.oobRand, SMP_RANDOM_DATA_LEN, 0x00, SMP_RANDOM_DATA_LEN);
1535 LOG_DEBUG("SMP_SC_PAIR_OOB_MASTER_STEP_1 started.");
1536 g_smpPairMng.step = SMP_SC_PAIR_OOB_MASTER_STEP_1;
1537 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1538 ret = HCI_LeReadLocalP256PublicKey();
1539 if (ret != SMP_SUCCESS) {
1540 LOG_ERROR("HCI_LeReadLocalP256PublicKey failed.");
1541 SMP_GeneratePairResult(
1542 g_smpPairMng.handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, g_smpPairMng.alarm);
1543 }
1544 }
1545
1546 if (g_smpPairMng.local.pairMethod != SMP_PAIR_METHOD_OOB_SC_LOCAL_RECV_PEER_SEND) {
1547 uint8_t scOOBData[SMP_SC_OOB_LEN] = {0x00};
1548 SMP_MemoryReverseCopy(g_smpPairMng.local.oobRand, SMP_GetScOobMng()->random, SMP_RANDOM_DATA_LEN);
1549 SMP_MemoryReverseCopy(scOOBData, g_smpPairMng.local.addr.addr, sizeof(g_smpPairMng.local.addr.addr));
1550 SMP_MemoryReverseCopy(
1551 scOOBData + sizeof(g_smpPairMng.local.addr.addr), SMP_GetScOobMng()->random, SMP_RANDOM_DATA_LEN);
1552 SMP_MemoryReverseCopy(scOOBData + sizeof(g_smpPairMng.local.addr.addr) + SMP_RANDOM_DATA_LEN,
1553 SMP_GetScOobMng()->confirm,
1554 SMP_CONFIRM_DATA_LEN);
1555 (void)memcpy_s(
1556 g_smpPairMng.local.publicKey, SMP_PUBLICKEY_LEN, SMP_GetScOobMng()->publicKey, SMP_PUBLICKEY_LEN);
1557 LOG_DEBUG("SMP_SC_PAIR_OOB_MASTER_STEP_7 started.");
1558 g_smpPairMng.step = SMP_SC_PAIR_OOB_MASTER_STEP_7;
1559 AlarmSet(g_smpPairMng.alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
1560 SMP_NotifyCbAuthReq(g_smpPairMng.handle, g_smpPairMng.local.pairMethod, scOOBData);
1561 }
1562
1563 return ret;
1564 }
1565
SMP_GenPairRetNormal(uint16_t handle,uint8_t status)1566 static void SMP_GenPairRetNormal(uint16_t handle, uint8_t status)
1567 {
1568 SMP_PairResult pairResult;
1569
1570 (void)memset_s(&pairResult, sizeof(SMP_PairResult), 0x00, sizeof(pairResult));
1571 pairResult.pairType = g_smpPairMng.pairType;
1572
1573 if ((g_smpPairMng.local.pairParam.authReq & SMP_AUTH_REQ_BONDING) &&
1574 (g_smpPairMng.peer.pairParam.authReq & SMP_AUTH_REQ_BONDING)) {
1575 pairResult.bondedFlag = SMP_BONDED_FLAG_YES;
1576 if (g_smpPairMng.local.pairMethod != SMP_PAIR_METHOD_JUST_WORK) {
1577 pairResult.authFlag = SMP_AUTH_FLAG_YES;
1578 } else {
1579 pairResult.authFlag = SMP_AUTH_FLAG_NO;
1580 }
1581 pairResult.localKeyDist = g_smpPairMng.local.keyDist;
1582 pairResult.peerKeyDist = g_smpPairMng.peer.keyDist;
1583 (void)memcpy_s(pairResult.localLTK, SMP_LTK_LEN, g_smpPairMng.local.LTK, SMP_LTK_LEN);
1584 pairResult.localEdiv = g_smpPairMng.local.masterIdEdiv;
1585 (void)memcpy_s(
1586 pairResult.localRandom, SMP_MASTER_RAND_LEN, g_smpPairMng.local.masterIdRand, SMP_MASTER_RAND_LEN);
1587 (void)memcpy_s(pairResult.peerLTK, SMP_LTK_LEN, g_smpPairMng.peer.LTK, SMP_LTK_LEN);
1588 pairResult.peerEdiv = g_smpPairMng.peer.masterIdEdiv;
1589 (void)memcpy_s(pairResult.peerRandom, SMP_MASTER_RAND_LEN, g_smpPairMng.peer.masterIdRand, SMP_MASTER_RAND_LEN);
1590 (void)memcpy_s(pairResult.localIRK, SMP_IRK_LEN, g_smpPairMng.local.IRK, SMP_IRK_LEN);
1591 (void)memcpy_s(&pairResult.localIdentAddr, sizeof(BtAddr), &g_smpPairMng.local.identityAddr, sizeof(BtAddr));
1592 (void)memcpy_s(pairResult.peerIRK, SMP_IRK_LEN, g_smpPairMng.peer.IRK, SMP_IRK_LEN);
1593 (void)memcpy_s(&pairResult.peerIdentAddr, sizeof(BtAddr), &g_smpPairMng.peer.identityAddr, sizeof(BtAddr));
1594 (void)memcpy_s(pairResult.localCSRK, SMP_CSRK_LEN, g_smpPairMng.local.CSRK, SMP_CSRK_LEN);
1595 (void)memcpy_s(pairResult.peerCSRK, SMP_CSRK_LEN, g_smpPairMng.peer.CSRK, SMP_CSRK_LEN);
1596 pairResult.encKeySize = g_smpPairMng.encKeySize;
1597 } else {
1598 pairResult.bondedFlag = SMP_BONDED_FLAG_NO;
1599 }
1600
1601 AlarmDelete(g_smpPairMng.alarm);
1602 (void)memset_s(&g_smpPairMng, sizeof(g_smpPairMng), 0x00, sizeof(g_smpPairMng));
1603 SMP_NotifyCbPairRet(handle, status, &pairResult);
1604 }
1605
SMP_GenPairRetException(uint16_t handle,uint8_t status,uint8_t failedReason,Alarm * cancelTimer)1606 static void SMP_GenPairRetException(uint16_t handle, uint8_t status, uint8_t failedReason, Alarm *cancelTimer)
1607 {
1608 SMP_PairResult pairResult;
1609 (void)memset_s(&pairResult, sizeof(SMP_PairResult), 0x00, sizeof(pairResult));
1610 if (cancelTimer != NULL) {
1611 AlarmCancel(cancelTimer);
1612 }
1613 if (failedReason) {
1614 SMP_SendPairingFailed(handle, failedReason, SMP_SendPairingFailedCallback);
1615 }
1616 AlarmDelete(g_smpPairMng.alarm);
1617 (void)memset_s(&g_smpPairMng, sizeof(g_smpPairMng), 0x00, sizeof(g_smpPairMng));
1618 SMP_NotifyCbPairRet(handle, status, &pairResult);
1619 }
1620
SMP_L2cifLeRegisterFixChannelCallback(uint16_t cid,int result)1621 static void SMP_L2cifLeRegisterFixChannelCallback(uint16_t cid, int result)
1622 {
1623 LOG_INFO("%{public}s", __FUNCTION__);
1624 if (result) {
1625 LOG_ERROR("L2CIF_LeRegisterFixChannel failed result = %{public}d", result);
1626 }
1627 (void)cid;
1628 (void)result;
1629 }
1630
SMP_L2cifLeDeregisterFixChannelCallback(uint16_t cid,int result)1631 static void SMP_L2cifLeDeregisterFixChannelCallback(uint16_t cid, int result)
1632 {
1633 LOG_INFO("%{public}s", __FUNCTION__);
1634 if (result) {
1635 LOG_ERROR("L2CIF_LeDeregisterFixChannel failed result = %{public}d", result);
1636 }
1637 (void)cid;
1638 (void)result;
1639 }
1640
SMP_GenerateSignatureStep1(const SMP_StepParam * param)1641 void SMP_GenerateSignatureStep1(const SMP_StepParam *param)
1642 {
1643 if (SMP_ParamIsNULL(param) != SMP_SUCCESS) {
1644 return;
1645 }
1646 SMP_EncData *encData = (SMP_EncData *)param->data;
1647 SMP_EncCmd *pEncCmdData = encData->encCmd;
1648 const HciLeEncryptReturnParam *returnParam = encData->encRetParam;
1649 SMP_CryptAesCmacStep2Param cryptAesCmacstep2Param;
1650 SMP_CryptAesCmacStep3Param cryptAesCmacstep3Param;
1651 SMP_CryptAesCmacStep1Param cryptAesCmacStep1parm;
1652
1653 LOG_INFO("%{public}s ", __FUNCTION__);
1654 if (returnParam->status) {
1655 LOG_ERROR("returnParam->status = %hhu", returnParam->status);
1656 SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL);
1657 return;
1658 }
1659 SMP_MemoryReverseCopy(cryptAesCmacStep1parm.input, returnParam->encryptedData, sizeof(cryptAesCmacStep1parm.input));
1660 SMP_CryptographicAesCmacStep1(&cryptAesCmacStep1parm);
1661 (void)memcpy_s(
1662 cryptAesCmacstep2Param.input1, CRYPT_AESCMAC_IN_LEN, cryptAesCmacStep1parm.output1, CRYPT_AESCMAC_IN_LEN);
1663 (void)memcpy_s(
1664 cryptAesCmacstep2Param.input2, CRYPT_AESCMAC_IN_LEN, cryptAesCmacStep1parm.output2, CRYPT_AESCMAC_IN_LEN);
1665 cryptAesCmacstep2Param.input3 = pEncCmdData->M;
1666 cryptAesCmacstep2Param.length = pEncCmdData->signDataLen;
1667 SMP_CryptographicAesCmacStep2(&cryptAesCmacstep2Param);
1668 cryptAesCmacstep3Param.stepA = SMP_GENERATE_SIGNATURE_STEP_2;
1669 cryptAesCmacstep3Param.stepB = SMP_GENERATE_SIGNATURE_STEP_3;
1670 cryptAesCmacstep3Param.message = pEncCmdData->M;
1671 cryptAesCmacstep3Param.messageSize = pEncCmdData->signDataLen;
1672 (void)memcpy_s(cryptAesCmacstep3Param.aesCmacOutput,
1673 CRYPT_AESCMAC_OUT_LEN,
1674 cryptAesCmacstep2Param.output,
1675 CRYPT_AESCMAC_OUT_LEN);
1676 (void)memcpy_s(cryptAesCmacstep3Param.key, CRYPT_AESCMAC_KEY_LEN, pEncCmdData->key, CRYPT_AESCMAC_KEY_LEN);
1677 cryptAesCmacstep3Param.n = cryptAesCmacstep2Param.n;
1678 cryptAesCmacstep3Param.signCounter = pEncCmdData->signCounter;
1679 cryptAesCmacstep3Param.isUsingHwAes128 = SMP_USING_HW_AES128_SIGN;
1680 int ret = SMP_CryptographicAesCmacStep3(&cryptAesCmacstep3Param);
1681 if (ret != SMP_SUCCESS) {
1682 SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL);
1683 }
1684 (void)memset_s(cryptAesCmacstep3Param.key, CRYPT_AESCMAC_KEY_LEN, 0x00, CRYPT_AESCMAC_KEY_LEN);
1685 }
1686
SMP_GenerateSignatureStep2(const SMP_StepParam * param)1687 void SMP_GenerateSignatureStep2(const SMP_StepParam *param)
1688 {
1689 if (SMP_ParamIsNULL(param) != SMP_SUCCESS) {
1690 return;
1691 }
1692 SMP_EncData *encData = (SMP_EncData *)param->data;
1693 SMP_CryptAesCmacStep4Param cryptAesCmacStep4Param;
1694 const HciLeEncryptReturnParam *returnParam = encData->encRetParam;
1695
1696 LOG_INFO("%{public}s.", __FUNCTION__);
1697
1698 if (returnParam->status) {
1699 LOG_ERROR("returnParam->status = %hhu ", returnParam->status);
1700 SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL);
1701 return;
1702 }
1703
1704 cryptAesCmacStep4Param.stepA = SMP_GENERATE_SIGNATURE_STEP_2;
1705 cryptAesCmacStep4Param.stepB = SMP_GENERATE_SIGNATURE_STEP_3;
1706 cryptAesCmacStep4Param.pEncCmdData = encData->encCmd;
1707 (void)memcpy_s(cryptAesCmacStep4Param.X,
1708 sizeof(cryptAesCmacStep4Param.X),
1709 returnParam->encryptedData,
1710 sizeof(cryptAesCmacStep4Param.X));
1711 cryptAesCmacStep4Param.isUsingHwAes128 = SMP_USING_HW_AES128_SIGN;
1712 int ret = SMP_CryptographicAesCmacStep4(&cryptAesCmacStep4Param);
1713 if (ret != SMP_SUCCESS) {
1714 SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL);
1715 }
1716 }
1717
SMP_GenerateSignatureStep3(const SMP_StepParam * param)1718 void SMP_GenerateSignatureStep3(const SMP_StepParam *param)
1719 {
1720 if (SMP_ParamIsNULL(param) != SMP_SUCCESS) {
1721 return;
1722 }
1723 SMP_EncData *encData = (SMP_EncData *)param->data;
1724 SMP_EncCmd *pEncCmdData = encData->encCmd;
1725 const HciLeEncryptReturnParam *returnParam = encData->encRetParam;
1726 uint8_t signature[SMP_SIGNATURE_LEN] = {0};
1727
1728 LOG_INFO("%{public}s", __FUNCTION__);
1729
1730 if (returnParam->status) {
1731 LOG_ERROR("returnParam->status = %hhu. ", returnParam->status);
1732 SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL);
1733 return;
1734 }
1735
1736 (void)memcpy_s(signature, sizeof(signature), (uint8_t *)&pEncCmdData->signCounter, SMP_SIGNCOUNTER_LEN);
1737 (void)memcpy_s(signature + SMP_SIGNCOUNTER_LEN,
1738 (SMP_SIGNATURE_LEN - SMP_SIGNCOUNTER_LEN),
1739 &returnParam->encryptedData[SMP_SIGNATURE_LEN - SMP_SIGNCOUNTER_LEN],
1740 (SMP_SIGNATURE_LEN - SMP_SIGNCOUNTER_LEN));
1741 SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_SUCCESS, signature);
1742 }
1743
SMP_NotifyCbAuthReq(uint16_t handle,uint8_t pairMethod,const uint8_t * displayValue)1744 void SMP_NotifyCbAuthReq(uint16_t handle, uint8_t pairMethod, const uint8_t *displayValue)
1745 {
1746 if (g_smpCallBack.SMP_CallbackAuthenticationRequest != NULL) {
1747 g_smpCallBack.SMP_CallbackAuthenticationRequest(handle, pairMethod, displayValue);
1748 }
1749 }
1750
SMP_NotifyCbPairRet(uint16_t handle,uint8_t status,const SMP_PairResult * result)1751 void SMP_NotifyCbPairRet(uint16_t handle, uint8_t status, const SMP_PairResult *result)
1752 {
1753 if (g_smpCallBack.SMP_CallbackPairResult != NULL) {
1754 g_smpCallBack.SMP_CallbackPairResult(handle, status, result);
1755 }
1756 }
1757
SMP_NotifyCbPairReq(uint16_t handle,const SMP_PairParam * param)1758 void SMP_NotifyCbPairReq(uint16_t handle, const SMP_PairParam *param)
1759 {
1760 if (g_smpCallBack.SMP_CallbackRemotePairRequest != NULL) {
1761 g_smpCallBack.SMP_CallbackRemotePairRequest(handle, param);
1762 }
1763 }
1764
SMP_NotifyCbPairRsp(uint16_t handle,const SMP_PairParam * param)1765 void SMP_NotifyCbPairRsp(uint16_t handle, const SMP_PairParam *param)
1766 {
1767 if (g_smpCallBack.SMP_CallbackRemotePairResponse != NULL) {
1768 g_smpCallBack.SMP_CallbackRemotePairResponse(handle, param);
1769 }
1770 }
1771
SMP_NotifyCbSecReq(uint16_t handle,uint8_t authReq)1772 void SMP_NotifyCbSecReq(uint16_t handle, uint8_t authReq)
1773 {
1774 if (g_smpCallBack.SMP_CallbackRemoteSecurityRequest != NULL) {
1775 g_smpCallBack.SMP_CallbackRemoteSecurityRequest(handle, authReq);
1776 }
1777 }
1778
SMP_NotifyCbLtkReq(uint16_t handle,const uint8_t * random,uint16_t ediv)1779 void SMP_NotifyCbLtkReq(uint16_t handle, const uint8_t *random, uint16_t ediv)
1780 {
1781 if (g_smpCallBack.SMP_CallbackLongTermKeyRequest != NULL) {
1782 g_smpCallBack.SMP_CallbackLongTermKeyRequest(handle, random, ediv);
1783 }
1784 }
1785
SMP_NotifyCbEncComp(uint16_t handle,uint8_t status)1786 void SMP_NotifyCbEncComp(uint16_t handle, uint8_t status)
1787 {
1788 if (g_smpCallBack.SMP_CallbackEncryptionComplete != NULL) {
1789 g_smpCallBack.SMP_CallbackEncryptionComplete(handle, status);
1790 }
1791 }
1792
SMP_NotifyCbGenSign(uint8_t status,const uint8_t * sign)1793 void SMP_NotifyCbGenSign(uint8_t status, const uint8_t *sign)
1794 {
1795 if (g_smpCallBack.SMP_CallbackGenerateSignatureResult != NULL) {
1796 g_smpCallBack.SMP_CallbackGenerateSignatureResult(status, sign);
1797 }
1798 }
1799
SMP_NotifyCbGenRpa(uint8_t status,const uint8_t * addr)1800 void SMP_NotifyCbGenRpa(uint8_t status, const uint8_t *addr)
1801 {
1802 if (g_smpCallBack.SMP_CallbackGenerateRPAResult != NULL) {
1803 g_smpCallBack.SMP_CallbackGenerateRPAResult(status, addr);
1804 }
1805 }
1806
SMP_NotifyCbResoRpa(uint8_t status,bool result,const uint8_t * addr,const uint8_t * irk)1807 void SMP_NotifyCbResoRpa(uint8_t status, bool result, const uint8_t *addr, const uint8_t *irk)
1808 {
1809 if (g_smpCallBack.SMP_CallbackResolveRPAResult != NULL) {
1810 g_smpCallBack.SMP_CallbackResolveRPAResult(status, result, addr, irk);
1811 }
1812 }
1813
SMP_NotifyCbGenScOobData(uint8_t status,const uint8_t * random,const uint8_t * confirm)1814 void SMP_NotifyCbGenScOobData(uint8_t status, const uint8_t *random, const uint8_t *confirm)
1815 {
1816 if (g_smpCallBack.SMP_CallbackGenerateScOobDataResult != NULL) {
1817 g_smpCallBack.SMP_CallbackGenerateScOobDataResult(status, random, confirm);
1818 }
1819 }
1820
SMP_ParamIsNULL(const SMP_StepParam * param)1821 int SMP_ParamIsNULL(const SMP_StepParam *param)
1822 {
1823 if (param == NULL) {
1824 return SMP_ERR_INVAL_PARAM;
1825 }
1826 if (param->data == NULL) {
1827 return SMP_ERR_INVAL_PARAM;
1828 }
1829 return SMP_SUCCESS;
1830 }
1831
SMP_GenerateScOobData()1832 int SMP_GenerateScOobData()
1833 {
1834 LOG_INFO("%{public}s", __FUNCTION__);
1835 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_GenScOobDataTask, NULL);
1836 if (ret != SMP_SUCCESS) {
1837 return ret;
1838 }
1839 return ret;
1840 }
1841
SMP_GenScOobDataTask(void * context)1842 static void SMP_GenScOobDataTask(void *context)
1843 {
1844 int ret = SMP_SUCCESS;
1845
1846 LOG_INFO("%{public}s", __FUNCTION__);
1847 if (SMP_GetScOobMng()->state == SMP_STATE_SC_OOB_DATA_GENERATING) {
1848 LOG_ERROR("It's already SC OOB data generating state.");
1849 } else if (g_smpPairMng.state == SMP_STATE_PAIRING) {
1850 LOG_ERROR("It's already pairing state.");
1851 ret = SMP_ERR_INVAL_STATE;
1852 } else {
1853 SMP_ClearScOobData(false);
1854 SMP_GetScOobMng()->state = SMP_STATE_SC_OOB_DATA_GENERATING;
1855 SMP_GetScOobMng()->alarm = AlarmCreate("", false);
1856 LOG_DEBUG("SMP_GENERATE_SC_OOB_DATA_STEP_1 started.");
1857 SMP_GetScOobMng()->step = SMP_GENERATE_SC_OOB_DATA_STEP_1;
1858 AlarmSet(SMP_GetScOobMng()->alarm, SMP_GEN_SC_OOB_DATA_WAIT_TIME, SMP_GenerateScOobDataTimeout, NULL);
1859 ret = HCI_LeReadLocalP256PublicKey();
1860 if (ret != SMP_SUCCESS) {
1861 LOG_ERROR("HCI_LeReadLocalP256PublicKey failed.");
1862 SMP_ClearScOobData(true);
1863 }
1864 }
1865 if (ret != SMP_SUCCESS) {
1866 SMP_NotifyCbGenScOobData(SMP_GENERATE_SC_OOB_DATA_FAILED, NULL, NULL);
1867 }
1868 (void)context;
1869 }
1870
SMP_GetPairMng()1871 SMP_PairMng *SMP_GetPairMng()
1872 {
1873 return &g_smpPairMng;
1874 }
1875
1876 static Module g_smp = {
1877 .name = MODULE_NAME_SMP,
1878 .init = SMP_Initialize,
1879 .startup = SMP_Startup,
1880 .shutdown = SMP_Shutdown,
1881 .cleanup = SMP_Finalize,
1882 .dependencies = {MODULE_NAME_L2CAP},
1883 };
1884
1885 MODULE_DECL(g_smp)
1886