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_receive.h"
17 
18 #include <string.h>
19 
20 #include "btm/btm_thread.h"
21 #include "log.h"
22 #include "platform/include/allocator.h"
23 #include "smp.h"
24 #include "smp_cmd.h"
25 #include "smp_common.h"
26 #include "smp_def.h"
27 #include "smp_legacy.h"
28 #include "smp_oob.h"
29 #include "smp_sc_accepter.h"
30 #include "smp_sc_initiator.h"
31 #include "smp_send.h"
32 #include "smp_tool.h"
33 
34 static void SMP_ReceivePairingRequest(uint16_t handle, const Buffer *buffer);
35 static void SMP_ReceivePairingResponse(uint16_t handle, const Buffer *buffer);
36 static void SMP_ReceivePairingConfirm(uint16_t handle, const Buffer *buffer);
37 static void SMP_ReceivePairingRandom(uint16_t handle, const Buffer *buffer);
38 static void SMP_ReceiveEncryptionInformation(uint16_t handle, const Buffer *buffer);
39 static void SMP_ReceiveMasterIdentification(uint16_t handle, const Buffer *buffer);
40 static void SMP_ReceiveIdentityInformation(uint16_t handle, const Buffer *buffer);
41 static void SMP_ReceiveIdentityAddressInformation(uint16_t handle, const Buffer *buffer);
42 static void SMP_ReceiveSigningInformation(uint16_t handle, const Buffer *buffer);
43 static void SMP_ReceivePairingFailed(uint16_t handle, const Buffer *buffer);
44 static void SMP_ReceiveSecurityRequest(uint16_t handle, const Buffer *buffer);
45 static void SMP_ReceivePairingPublicKey(uint16_t handle, const Buffer *buffer);
46 static void SMP_ReceivePairingDHKeyCheck(uint16_t handle, const Buffer *buffer);
47 static void SMP_RecvEncInfoProcessMaster(const Buffer *buffer);
48 static void SMP_RecvEncInfoProcessSlave(const Buffer *buffer);
49 static void SMP_RecvMasterIdentProcessMaster(const Buffer *buffer);
50 static void SMP_RecvMasterIdentProcessSlave(const Buffer *buffer);
51 static void SMP_RecvIdentInfoProcessMaster(const Buffer *buffer);
52 static void SMP_RecvIdentInfoProcessSlave(const Buffer *buffer);
53 static void SMP_RecvIdentAddrInfoProcessMaster(const Buffer *buffer);
54 static void SMP_RecvIdentAddrInfoProcessSlave(const Buffer *buffer);
55 static void SMP_RecvSignInfoProcessMaster(const Buffer *buffer);
56 static void SMP_RecvSignInfoProcessSlave(const Buffer *buffer);
57 static void SMP_RecvDataCmdDistribution(uint16_t handle, uint8_t code, const Buffer *buffer);
58 static int SMP_RecvPairReqJudgeException(uint16_t handle, const SMP_PairParam *pairParam);
59 static int SMP_RecvPairRspJudgeException(uint16_t handle, const SMP_PairParam *pairParam);
60 static void SMP_RecvPairCfmStepDistribution(uint16_t step, const SMP_StepParam *param);
61 static void SMP_RecvPairRandStepDistribution(uint16_t step, const SMP_StepParam *param);
62 static void SMP_RecvPairPubKeyStepDistribution(uint16_t step, const SMP_StepParam *param);
63 
64 static void SMP_RecvDataTask(void *context);
65 static void SMP_DisconnectedTask(void *context);
66 
SMP_ReceiveData(uint16_t handle,const Packet * pkt)67 void SMP_ReceiveData(uint16_t handle, const Packet *pkt)
68 {
69     LOG_INFO("%{public}s", __FUNCTION__);
70     Packet *pktPtr = PacketRefMalloc(pkt);
71     SMP_RecvDataTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_RecvDataTask_t));
72     if (ctx == NULL) {
73         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
74         return;
75     }
76     (void)memset_s(ctx, sizeof(SMP_RecvDataTask_t), 0x00, sizeof(SMP_RecvDataTask_t));
77     ctx->handle = handle;
78     ctx->pkt = pktPtr;
79     int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_RecvDataTask, (void *)ctx);
80     if (ret != SMP_SUCCESS) {
81         PacketFree(ctx->pkt);
82         MEM_MALLOC.free(ctx);
83         return;
84     }
85 }
86 
SMP_RecvDataTask(void * context)87 static void SMP_RecvDataTask(void *context)
88 {
89     uint8_t code = 0x00;
90     Buffer *buffer = NULL;
91     SMP_RecvDataTask_t *param = (SMP_RecvDataTask_t *)context;
92     LOG_DEBUG("%{public}s", __FUNCTION__);
93     PacketExtractHead(param->pkt, &code, sizeof(code));
94     buffer = PacketContinuousPayload(param->pkt);
95     SMP_RecvDataCmdDistribution(param->handle, code, buffer);
96     PacketFree(param->pkt);
97     MEM_MALLOC.free(param);
98 }
99 
SMP_Connected(const BtAddr * addr,uint16_t handle,uint8_t role,uint8_t status)100 void SMP_Connected(const BtAddr *addr, uint16_t handle, uint8_t role, uint8_t status)
101 {
102     LOG_INFO("%{public}s", __FUNCTION__);
103     (void)addr;
104     (void)handle;
105     (void)role;
106     (void)status;
107 }
108 
SMP_Disconnected(uint16_t handle,uint8_t role,uint8_t reason)109 void SMP_Disconnected(uint16_t handle, uint8_t role, uint8_t reason)
110 {
111     LOG_INFO("%{public}s", __FUNCTION__);
112     SMP_DisconnectedTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_DisconnectedTask_t));
113     if (ctx == NULL) {
114         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
115         return;
116     }
117     (void)memset_s(ctx, sizeof(SMP_DisconnectedTask_t), 0x00, sizeof(SMP_DisconnectedTask_t));
118     ctx->handle = handle;
119     int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_DisconnectedTask, (void *)ctx);
120     if (ret != SMP_SUCCESS) {
121         MEM_MALLOC.free(ctx);
122         return;
123     }
124     (void)role;
125     (void)reason;
126 }
127 
SMP_DisconnectedTask(void * context)128 static void SMP_DisconnectedTask(void *context)
129 {
130     SMP_DisconnectedTask_t *param = (SMP_DisconnectedTask_t *)context;
131 
132     LOG_DEBUG("%{public}s", __FUNCTION__);
133     if ((SMP_GetPairMng()->state == SMP_STATE_PAIRING) && (SMP_GetPairMng()->handle == param->handle)) {
134         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, 0x00, SMP_GetPairMng()->alarm);
135         MEM_MALLOC.free(param);
136         return;
137     }
138     MEM_MALLOC.free(param);
139 }
140 
SMP_RecvDataCmdDistribution(uint16_t handle,uint8_t code,const Buffer * buffer)141 static void SMP_RecvDataCmdDistribution(uint16_t handle, uint8_t code, const Buffer *buffer)
142 {
143     switch (code) {
144         case SMP_CODE_PAIRING_REQ:
145             SMP_ReceivePairingRequest(handle, buffer);
146             break;
147         case SMP_CODE_PAIRING_RSP:
148             SMP_ReceivePairingResponse(handle, buffer);
149             break;
150         case SMP_CODE_PAIRING_CFM:
151             SMP_ReceivePairingConfirm(handle, buffer);
152             break;
153         case SMP_CODE_PAIRING_RAND:
154             SMP_ReceivePairingRandom(handle, buffer);
155             break;
156         case SMP_CODE_ENCRYPTION_INFO:
157             SMP_ReceiveEncryptionInformation(handle, buffer);
158             break;
159         case SMP_CODE_MASTER_IDENTITY:
160             SMP_ReceiveMasterIdentification(handle, buffer);
161             break;
162         case SMP_CODE_IDENTITY_INFO:
163             SMP_ReceiveIdentityInformation(handle, buffer);
164             break;
165         case SMP_CODE_IDENTITY_ADDR_INFO:
166             SMP_ReceiveIdentityAddressInformation(handle, buffer);
167             break;
168         case SMP_CODE_SIGNING_INFO:
169             SMP_ReceiveSigningInformation(handle, buffer);
170             break;
171         case SMP_CODE_PAIRING_FAIL:
172             SMP_ReceivePairingFailed(handle, buffer);
173             break;
174         case SMP_CODE_SECURITY_REQ:
175             SMP_ReceiveSecurityRequest(handle, buffer);
176             break;
177         case SMP_CODE_PAIRING_PUBLIC_KEY:
178             SMP_ReceivePairingPublicKey(handle, buffer);
179             break;
180         case SMP_CODE_PAIRING_DHKEY_CHECK:
181             SMP_ReceivePairingDHKeyCheck(handle, buffer);
182             break;
183         default:
184             break;
185     }
186 }
187 
SMP_ReceivePairingRequest(uint16_t handle,const Buffer * buffer)188 static void SMP_ReceivePairingRequest(uint16_t handle, const Buffer *buffer)
189 {
190     uint8_t *pData = NULL;
191     SMP_PairParam pairParamTemp;
192 
193     LOG_INFO("%{public}s", __FUNCTION__);
194     pData = (uint8_t *)BufferPtr(buffer);
195     int offset = 0;
196     pairParamTemp.ioCapability = pData[offset];
197     offset += sizeof(pairParamTemp.ioCapability);
198     pairParamTemp.oobDataFlag = pData[offset];
199     offset += sizeof(pairParamTemp.oobDataFlag);
200     pairParamTemp.authReq = pData[offset];
201     offset += sizeof(pairParamTemp.authReq);
202     pairParamTemp.maxEncKeySize = pData[offset];
203     offset += sizeof(pairParamTemp.maxEncKeySize);
204     pairParamTemp.initKeyDist = pData[offset];
205     offset += sizeof(pairParamTemp.initKeyDist);
206     pairParamTemp.respKeyDist = pData[offset];
207     if (SMP_RecvPairReqJudgeException(handle, &pairParamTemp)) {
208         return;
209     }
210     (void)memset_s(SMP_GetPairMng(), sizeof(SMP_PairMng), 0x00, sizeof(SMP_PairMng));
211     SMP_GetPairMng()->state = SMP_STATE_PAIRING;
212     SMP_GetPairMng()->handle = handle;
213     SMP_GetPairMng()->role = SMP_ROLE_SLAVE;
214     SMP_GetPairMng()->alarm = AlarmCreate("", false);
215     (void)memcpy_s(&SMP_GetPairMng()->peer.pairParam, sizeof(SMP_PairParam), &pairParamTemp, sizeof(SMP_PairParam));
216     SMP_GetPairMng()->step = SMP_LEGACY_PAIR_SLAVE_STEP_1;
217     LOG_INFO("SMP_LEGACY_PAIR_SLAVE_STEP_1 started.");
218     AlarmSet(SMP_GetPairMng()->alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
219     SMP_NotifyCbPairReq(handle, &pairParamTemp);
220 }
221 
SMP_RecvPairReqJudgeException(uint16_t handle,const SMP_PairParam * pairParam)222 static int SMP_RecvPairReqJudgeException(uint16_t handle, const SMP_PairParam *pairParam)
223 {
224     int ret = SMP_SUCCESS;
225     uint8_t reason = 0x00;
226 
227     if ((SMP_GetPairMng()->state == SMP_STATE_PAIRING) ||
228         (SMP_GetScOobMng()->state == SMP_STATE_SC_OOB_DATA_GENERATING)) {
229         LOG_ERROR("It's already pairing state or SC OOB data generating state.");
230         if (handle == SMP_GetPairMng()->handle) {
231             SMP_SendPairingFailed(handle, SMP_PAIR_FAILED_REPAETED_ATTEMPTS, SMP_SendPairingFailedCallback);
232         } else {
233             SMP_SendPairingFailed(handle, SMP_PAIR_FAILED_PAIRING_NOT_SUPPORTED, SMP_SendPairingFailedCallback);
234         }
235         ret = SMP_ERR_INVAL_STATE;
236         return ret;
237     }
238 
239     if (SMP_CheckRemotePairParam(pairParam, &reason)) {
240         LOG_ERROR("Check Remote Pair Param failed.");
241         SMP_SendPairingFailed(handle, reason, SMP_SendPairingFailedCallback);
242         ret = SMP_ERR_INVAL_PARAM;
243         return ret;
244     }
245 
246     if (SMP_GetSecureConnOnlyMode() && (!(pairParam->authReq & SMP_AUTH_REQ_BIT_SC))) {
247         LOG_ERROR("SC bit is not set in authReq.");
248         SMP_SendPairingFailed(handle, SMP_PAIR_FAILED_AUTH_REQ, SMP_SendPairingFailedCallback);
249         ret = SMP_ERR_INVAL_STATE;
250         return ret;
251     }
252 
253     return ret;
254 }
255 
SMP_ReceivePairingResponse(uint16_t handle,const Buffer * buffer)256 static void SMP_ReceivePairingResponse(uint16_t handle, const Buffer *buffer)
257 {
258     SMP_PairParam pairParamTemp;
259 
260     LOG_INFO("%{public}s", __FUNCTION__);
261     if (memcpy_s(&pairParamTemp, sizeof(SMP_PairParam), (uint8_t *)BufferPtr(buffer), sizeof(SMP_PairParam)) != EOK) {
262         LOG_ERROR("[%{public}s][%{public}d] pairParamTemp memcpy_s fail.", __FUNCTION__, __LINE__);
263         return;
264     }
265     if (SMP_RecvPairRspJudgeException(handle, &pairParamTemp)) {
266         return;
267     }
268     AlarmCancel(SMP_GetPairMng()->alarm);
269     if (memcpy_s(&SMP_GetPairMng()->peer.pairParam,
270         sizeof(SMP_PairParam), (uint8_t *)BufferPtr(buffer), sizeof(SMP_PairParam)) != EOK) {
271             LOG_ERROR("[%{public}s][%{public}d] peer.pairParam memcpy_s fail.", __FUNCTION__, __LINE__);
272             return;
273         }
274     AlarmSet(SMP_GetPairMng()->alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
275     SMP_NotifyCbPairRsp(handle, &pairParamTemp);
276 }
277 
SMP_RecvPairRspJudgeException(uint16_t handle,const SMP_PairParam * pairParam)278 static int SMP_RecvPairRspJudgeException(uint16_t handle, const SMP_PairParam *pairParam)
279 {
280     int ret = SMP_SUCCESS;
281     uint8_t reason = 0x00;
282 
283     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
284         LOG_ERROR("It's not pairing state.");
285         ret = SMP_ERR_INVAL_STATE;
286         return ret;
287     }
288     if (SMP_GetPairMng()->handle != handle) {
289         LOG_ERROR("Connection handle error.");
290         ret = SMP_ERR_INVAL_PARAM;
291         return ret;
292     }
293     if (SMP_GetPairMng()->role != SMP_ROLE_MASTER) {
294         LOG_INFO("It's not initiator role.");
295         ret = SMP_ERR_INVAL_STATE;
296         return ret;
297     }
298     if (SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_1) {
299         LOG_INFO("It's not SMP_LEGACY_PAIR_MASTER_STEP_1.");
300         SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
301         ret = SMP_ERR_INVAL_STATE;
302         return ret;
303     }
304 
305     if (SMP_CheckRemotePairParam(pairParam, &reason)) {
306         LOG_ERROR("Check Remote Pair Param failed.");
307         SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, reason, SMP_GetPairMng()->alarm);
308         ret = SMP_ERR_INVAL_PARAM;
309         return ret;
310     }
311     if (SMP_GetSecureConnOnlyMode() && (!(pairParam->authReq & SMP_AUTH_REQ_BIT_SC))) {
312         LOG_ERROR("SC bit is not set in authReq.");
313         SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_AUTH_REQ, SMP_GetPairMng()->alarm);
314         ret = SMP_ERR_INVAL_STATE;
315         return ret;
316     }
317 
318     return ret;
319 }
320 
SMP_ReceivePairingConfirm(uint16_t handle,const Buffer * buffer)321 static void SMP_ReceivePairingConfirm(uint16_t handle, const Buffer *buffer)
322 {
323     SMP_StepParam param = {.data = (void *)BufferPtr(buffer)};
324     LOG_INFO("%{public}s", __FUNCTION__);
325     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
326         LOG_ERROR("It's Not Pairing State");
327         return;
328     }
329     if (SMP_GetPairMng()->handle != handle) {
330         LOG_ERROR("Connection Handle Error");
331         return;
332     }
333     SMP_RecvPairCfmStepDistribution(SMP_GetPairMng()->step, &param);
334 }
335 
SMP_RecvPairCfmStepDistribution(uint16_t step,const SMP_StepParam * param)336 static void SMP_RecvPairCfmStepDistribution(uint16_t step, const SMP_StepParam *param)
337 {
338     switch (step) {
339         case SMP_LEGACY_PAIR_MASTER_STEP_6:
340             SMP_LegacyPairMasterStep6(param);
341             break;
342         case SMP_LEGACY_PAIR_SLAVE_STEP_1:
343         case SMP_LEGACY_PAIR_SLAVE_STEP_2:
344             SMP_LegacyPairSlaveStep2(param);
345             break;
346         case SMP_LEGACY_PAIR_SLAVE_STEP_3:
347             SMP_LegacyPairSlaveStep3(param);
348             break;
349         case SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_3:
350             SMP_ScPairJustworkOrNumericMasterStep3(param);
351             break;
352         case SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_4:
353             SMP_ScPairJustworkOrNumericMasterStep4(param);
354             break;
355         case SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_11:
356             SMP_ScPairPasskeyEntryMasterStep11(param);
357             break;
358         case SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_5:
359             SMP_ScPairPasskeyEntrySlaveStep5(param);
360             break;
361         case SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_6:
362             SMP_ScPairPasskeyEntrySlaveStep6(param);
363             break;
364         default:
365             LOG_ERROR("It's invalid step:%hu.", step);
366             SMP_GeneratePairResult(
367                 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
368             break;
369     }
370 }
371 
SMP_ReceivePairingRandom(uint16_t handle,const Buffer * buffer)372 static void SMP_ReceivePairingRandom(uint16_t handle, const Buffer *buffer)
373 {
374     SMP_StepParam param = {.data = (void *)BufferPtr(buffer)};
375     LOG_INFO("%{public}s", __FUNCTION__);
376     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
377         LOG_ERROR("It is not pairing state.");
378         return;
379     }
380     if (SMP_GetPairMng()->handle != handle) {
381         LOG_ERROR("Connection handle is error.");
382         return;
383     }
384     AlarmCancel(SMP_GetPairMng()->alarm);
385     SMP_RecvPairRandStepDistribution(SMP_GetPairMng()->step, &param);
386 }
387 
SMP_RecvPairRandStepDistribution(uint16_t step,const SMP_StepParam * param)388 static void SMP_RecvPairRandStepDistribution(uint16_t step, const SMP_StepParam *param)
389 {
390     switch (step) {
391         case SMP_LEGACY_PAIR_MASTER_STEP_7:
392             SMP_LegacyPairMasterStep7(param);
393             break;
394         case SMP_LEGACY_PAIR_SLAVE_STEP_8:
395             SMP_LegacyPairSlaveStep8(param);
396             break;
397         case SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_7:
398             SMP_ScPairJustworkOrNumericMasterStep7(param);
399             break;
400         case SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_12:
401             SMP_ScPairPasskeyEntryMasterStep12(param);
402             break;
403         case SMP_SC_PAIR_OOB_MASTER_STEP_15:
404             SMP_ScPairOobMasterStep15(param);
405             break;
406         case SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_9:
407             SMP_ScPairJustworkOrNumericSlaveStep9(param);
408             break;
409         case SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_12:
410             SMP_ScPairPasskeyEntrySlaveStep12(param);
411             break;
412         case SMP_SC_PAIR_OOB_SLAVE_STEP_13:
413             SMP_ScPairOobSlaveStep13(param);
414             break;
415         default:
416             LOG_ERROR("It's invalid step:%hu.", step);
417             SMP_GeneratePairResult(
418                 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
419             break;
420     }
421 }
422 
SMP_ReceiveEncryptionInformation(uint16_t handle,const Buffer * buffer)423 static void SMP_ReceiveEncryptionInformation(uint16_t handle, const Buffer *buffer)
424 {
425     LOG_INFO("%{public}s", __FUNCTION__);
426     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
427         LOG_ERROR("It's not pairing state");
428         return;
429     }
430     if (SMP_GetPairMng()->handle != handle) {
431         LOG_ERROR("Connection handle error");
432         return;
433     }
434     if (SMP_GetPairMng()->role == SMP_ROLE_MASTER) {
435         LOG_INFO("It's initiator role");
436         SMP_RecvEncInfoProcessMaster(buffer);
437     } else {
438         LOG_INFO("It's accepter role.");
439         SMP_RecvEncInfoProcessSlave(buffer);
440     }
441 }
442 
SMP_ReceiveMasterIdentification(uint16_t handle,const Buffer * buffer)443 static void SMP_ReceiveMasterIdentification(uint16_t handle, const Buffer *buffer)
444 {
445     LOG_INFO("%{public}s", __FUNCTION__);
446     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
447         LOG_ERROR("It's Not pairing state");
448         return;
449     }
450     if (SMP_GetPairMng()->handle != handle) {
451         LOG_ERROR("Connection Handle error");
452         return;
453     }
454     if (SMP_GetPairMng()->role == SMP_ROLE_MASTER) {
455         LOG_INFO("It's initiator role");
456         SMP_RecvMasterIdentProcessMaster(buffer);
457     } else {
458         LOG_INFO("It's accepter role.");
459         SMP_RecvMasterIdentProcessSlave(buffer);
460     }
461 }
462 
SMP_ReceiveIdentityInformation(uint16_t handle,const Buffer * buffer)463 static void SMP_ReceiveIdentityInformation(uint16_t handle, const Buffer *buffer)
464 {
465     LOG_INFO("%{public}s", __FUNCTION__);
466     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
467         LOG_ERROR("It's Not pairing state.");
468         return;
469     }
470     if (SMP_GetPairMng()->handle != handle) {
471         LOG_ERROR("Connection Handle error.");
472         return;
473     }
474     if (SMP_GetPairMng()->role == SMP_ROLE_MASTER) {
475         LOG_INFO("It's initiator role.");
476         SMP_RecvIdentInfoProcessMaster(buffer);
477     } else {
478         LOG_INFO("It's accepter role.");
479         SMP_RecvIdentInfoProcessSlave(buffer);
480     }
481 }
482 
SMP_ReceiveIdentityAddressInformation(uint16_t handle,const Buffer * buffer)483 static void SMP_ReceiveIdentityAddressInformation(uint16_t handle, const Buffer *buffer)
484 {
485     LOG_INFO("%{public}s", __FUNCTION__);
486     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
487         LOG_ERROR("It's not pairing state!");
488         return;
489     }
490     if (SMP_GetPairMng()->handle != handle) {
491         LOG_ERROR("Connection handle error!");
492         return;
493     }
494     if (SMP_GetPairMng()->role == SMP_ROLE_MASTER) {
495         LOG_INFO("It's initiator role!");
496         SMP_RecvIdentAddrInfoProcessMaster(buffer);
497     } else {
498         LOG_INFO("It's accepter role.");
499         SMP_RecvIdentAddrInfoProcessSlave(buffer);
500     }
501 }
502 
SMP_ReceiveSigningInformation(uint16_t handle,const Buffer * buffer)503 static void SMP_ReceiveSigningInformation(uint16_t handle, const Buffer *buffer)
504 {
505     LOG_INFO("%{public}s", __FUNCTION__);
506     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
507         LOG_ERROR("It's not pairing state.");
508         return;
509     }
510     if (SMP_GetPairMng()->handle != handle) {
511         LOG_ERROR("Connection handle error.");
512         return;
513     }
514     if (SMP_GetPairMng()->role == SMP_ROLE_MASTER) {
515         LOG_INFO("It's initiator role.");
516         SMP_RecvSignInfoProcessMaster(buffer);
517     } else {
518         LOG_INFO("It's accepter role.");
519         SMP_RecvSignInfoProcessSlave(buffer);
520     }
521 }
522 
SMP_ReceivePairingFailed(uint16_t handle,const Buffer * buffer)523 static void SMP_ReceivePairingFailed(uint16_t handle, const Buffer *buffer)
524 {
525     uint8_t reason = *((uint8_t *)BufferPtr(buffer));
526 
527     LOG_INFO("%{public}s", __FUNCTION__);
528     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
529         LOG_ERROR("It Is Not Pairing State!");
530         return;
531     }
532     if (SMP_GetPairMng()->handle != handle) {
533         LOG_ERROR("Connection Handle Is Error!");
534         return;
535     }
536     if (SMP_GetPairMng()->role == SMP_ROLE_MASTER) {
537         LOG_ERROR("Initiator, step :%hu.", SMP_GetPairMng()->step);
538     } else {
539         LOG_ERROR("Accepter, step :%hu.", SMP_GetPairMng()->step);
540     }
541     if (reason != SMP_PAIR_FAILED_REPAETED_ATTEMPTS) {
542         SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, 0x00, NULL);
543     }
544 }
545 
SMP_ReceiveSecurityRequest(uint16_t handle,const Buffer * buffer)546 static void SMP_ReceiveSecurityRequest(uint16_t handle, const Buffer *buffer)
547 {
548     uint8_t authReq = *((uint8_t *)BufferPtr(buffer));
549 
550     LOG_INFO("%{public}s", __FUNCTION__);
551     if ((SMP_GetPairMng()->state == SMP_STATE_PAIRING) && (SMP_GetPairMng()->handle == handle)) {
552         return;
553     }
554     if (SMP_GetSecureConnOnlyMode() && (!(authReq & SMP_AUTH_REQ_BIT_SC))) {
555         LOG_ERROR("SC bit is not set in authReq.");
556         SMP_SendPairingFailed(handle, SMP_PAIR_FAILED_AUTH_REQ, SMP_SendPairingFailedCallback);
557         return;
558     }
559     SMP_NotifyCbSecReq(handle, authReq);
560 }
561 
SMP_ReceivePairingPublicKey(uint16_t handle,const Buffer * buffer)562 static void SMP_ReceivePairingPublicKey(uint16_t handle, const Buffer *buffer)
563 {
564     SMP_StepParam param = {.data = (void *)BufferPtr(buffer)};
565     LOG_INFO("%{public}s", __FUNCTION__);
566     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
567         LOG_ERROR("It is Not Pairing state!");
568         return;
569     }
570     if (SMP_GetPairMng()->handle != handle) {
571         LOG_ERROR("Connection Handle is error!");
572         return;
573     }
574     SMP_RecvPairPubKeyStepDistribution(SMP_GetPairMng()->step, &param);
575 }
576 
SMP_RecvPairPubKeyStepDistribution(uint16_t step,const SMP_StepParam * param)577 static void SMP_RecvPairPubKeyStepDistribution(uint16_t step, const SMP_StepParam *param)
578 {
579     switch (step) {
580         case SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_2:
581             SMP_ScPairJustworkOrNumericMasterStep2(param);
582             break;
583         case SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_2:
584             SMP_ScPairPasskeyEntryMasterStep2(param);
585             break;
586         case SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_1:
587             SMP_ScPairJustworkOrNumericSlaveStep1(param);
588             break;
589         case SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_1:
590             SMP_ScPairPasskeyEntrySlaveStep1(param);
591             break;
592         case SMP_SC_PAIR_OOB_MASTER_STEP_8:
593             SMP_ScPairOobMasterStep8(param);
594             break;
595         case SMP_SC_PAIR_OOB_SLAVE_STEP_7:
596             SMP_ScPairOobSlaveStep7(param);
597             break;
598         case SMP_SC_PAIR_OOB_SLAVE_STEP_8:
599             SMP_ScPairOobSlaveStep8(param);
600             break;
601         default:
602             LOG_ERROR("It's invalid step:%hu.", step);
603             SMP_GeneratePairResult(
604                 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
605             break;
606     }
607 }
608 
SMP_ReceivePairingDHKeyCheck(uint16_t handle,const Buffer * buffer)609 static void SMP_ReceivePairingDHKeyCheck(uint16_t handle, const Buffer *buffer)
610 {
611     SMP_StepParam param = {.data = (void *)BufferPtr(buffer)};
612 
613     LOG_INFO("%{public}s", __FUNCTION__);
614     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
615         LOG_ERROR("It Is Not Pairing State");
616         return;
617     }
618     if (SMP_GetPairMng()->handle != handle) {
619         LOG_ERROR("Connection Handle Is Error");
620         return;
621     }
622     switch (SMP_GetPairMng()->step) {
623         case SMP_SC_PAIR_COMMON_MASTER_STEP_13:
624             SMP_ScPairCommonMasterStep13(&param);
625             break;
626         case SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_13:
627             SMP_ScPairJustworkOrNumericSlaveStep13(&param);
628             break;
629         case SMP_SC_PAIR_COMMON_SLAVE_STEP_1:
630             SMP_ScPairCommonSlaveStep1(&param);
631             break;
632         default:
633             LOG_ERROR("It's invalid step:%hu.", SMP_GetPairMng()->step);
634             SMP_GeneratePairResult(handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
635             break;
636     }
637 }
638 
SMP_RecvEncInfoProcessMaster(const Buffer * buffer)639 static void SMP_RecvEncInfoProcessMaster(const Buffer *buffer)
640 {
641     LOG_INFO("%{public}s", __FUNCTION__);
642     if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_11) &&
643         (SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_12)) {
644         LOG_ERROR("It's invalid step.");
645         return;
646     }
647     if (memcpy_s(SMP_GetPairMng()->peer.LTK, SMP_LTK_LEN, (uint8_t *)BufferPtr(buffer), SMP_LTK_LEN) != EOK) {
648         LOG_ERROR("[%{public}s][%{public}d] memcpy_s fail.", __FUNCTION__, __LINE__);
649         return;
650     }
651     SMP_GetPairMng()->peer.keyDistCmdFlag =
652         SMP_GetPairMng()->peer.keyDistCmdFlag & (~SMP_KEY_DIST_CMD_FLAG_BIT_ENC_INFO);
653     if ((SMP_GetPairMng()->peer.keyDistCmdFlag != 0x00) ||
654         (SMP_GetPairMng()->masterEncryptedFlag == SMP_MASTER_ENCRYPTED_FLAG_NO)) {
655         return;
656     }
657     LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0");
658     AlarmCancel(SMP_GetPairMng()->alarm);
659     if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ENC_KEY) {
660         LOG_INFO("Local need to send ENC_KEY");
661         LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_13 started");
662         int ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_13);
663         if (ret != SMP_SUCCESS) {
664             SMP_GeneratePairResult(
665                 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
666         }
667     } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_SIGN_KEY) {
668         LOG_INFO("Local need to send SIGN_KEY");
669         LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_17 started");
670         int ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_17);
671         if (ret != SMP_SUCCESS) {
672             SMP_GeneratePairResult(
673                 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
674         }
675     } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ID_KEY) {
676         LOG_INFO("Send keys to remote");
677         if (SMP_SendDistributionKeysToRemote()) {
678             SMP_GeneratePairResult(
679                 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
680             LOG_INFO("Pair failed.");
681         }
682     } else {
683         LOG_INFO("Pair success.");
684         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
685     }
686 }
687 
SMP_RecvEncInfoProcessSlave(const Buffer * buffer)688 static void SMP_RecvEncInfoProcessSlave(const Buffer *buffer)
689 {
690     LOG_INFO("%{public}s", __FUNCTION__);
691 
692     if (SMP_GetPairMng()->step != SMP_LEGACY_PAIR_SLAVE_STEP_20) {
693         LOG_ERROR("It's not SMP_LEGACY_PAIR_SLAVE_STEP_20.");
694         return;
695     }
696 
697     if (memcpy_s(SMP_GetPairMng()->peer.LTK, SMP_LTK_LEN, (uint8_t *)BufferPtr(buffer), SMP_LTK_LEN) != EOK) {
698         LOG_ERROR("[%{public}s][%{public}d] memcpy_s fail.", __FUNCTION__, __LINE__);
699         return;
700     }
701     SMP_GetPairMng()->peer.keyDistCmdFlag =
702         SMP_GetPairMng()->peer.keyDistCmdFlag & (~SMP_KEY_DIST_CMD_FLAG_BIT_ENC_INFO);
703     if (SMP_GetPairMng()->peer.keyDistCmdFlag == 0x00) {
704         LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0.");
705         AlarmCancel(SMP_GetPairMng()->alarm);
706         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
707     }
708 }
709 
SMP_RecvMasterIdentProcessMaster(const Buffer * buffer)710 static void SMP_RecvMasterIdentProcessMaster(const Buffer *buffer)
711 {
712     uint8_t *pData = (uint8_t *)BufferPtr(buffer);
713     LOG_INFO("%{public}s", __FUNCTION__);
714     if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_11) &&
715         (SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_12)) {
716         LOG_ERROR("It's invalid step.");
717         return;
718     }
719     SMP_GetPairMng()->peer.masterIdEdiv = *(uint16_t *)pData;
720     SMP_MemoryReverseCopy(SMP_GetPairMng()->peer.masterIdRand, &(pData[0x02]), SMP_MASTER_RAND_LEN);
721     SMP_GetPairMng()->peer.keyDistCmdFlag =
722         SMP_GetPairMng()->peer.keyDistCmdFlag & (~SMP_KEY_DIST_CMD_FLAG_BIT_MASTER_IDENT);
723     if ((SMP_GetPairMng()->peer.keyDistCmdFlag != 0x00) ||
724         (SMP_GetPairMng()->masterEncryptedFlag == SMP_MASTER_ENCRYPTED_FLAG_NO)) {
725         return;
726     }
727     LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0!");
728     AlarmCancel(SMP_GetPairMng()->alarm);
729     if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ENC_KEY) {
730         LOG_INFO("Local need to send ENC_KEY!");
731         LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_13 started!");
732         int ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_13);
733         if (ret != SMP_SUCCESS) {
734             SMP_GeneratePairResult(
735                 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
736         }
737     } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_SIGN_KEY) {
738         LOG_INFO("Local need to send SIGN_KEY!");
739         LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_17 started!");
740         int ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_17);
741         if (ret != SMP_SUCCESS) {
742             SMP_GeneratePairResult(
743                 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
744         }
745     } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ID_KEY) {
746         LOG_INFO("Send Keys to remote.");
747         if (SMP_SendDistributionKeysToRemote()) {
748             SMP_GeneratePairResult(
749                 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
750             LOG_INFO("Pair is failed.");
751         }
752     } else {
753         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
754     }
755 }
756 
SMP_RecvMasterIdentProcessSlave(const Buffer * buffer)757 static void SMP_RecvMasterIdentProcessSlave(const Buffer *buffer)
758 {
759     uint8_t *pData = NULL;
760 
761     LOG_INFO("%{public}s", __FUNCTION__);
762 
763     if (SMP_GetPairMng()->step != SMP_LEGACY_PAIR_SLAVE_STEP_20) {
764         LOG_ERROR("It's not SMP_LEGACY_PAIR_SLAVE_STEP_20.");
765         return;
766     }
767 
768     pData = (uint8_t *)BufferPtr(buffer);
769     SMP_GetPairMng()->peer.masterIdEdiv = *(uint16_t *)pData;
770     SMP_MemoryReverseCopy(SMP_GetPairMng()->peer.masterIdRand, &(pData[0x02]), SMP_MASTER_RAND_LEN);
771     SMP_GetPairMng()->peer.keyDistCmdFlag =
772         SMP_GetPairMng()->peer.keyDistCmdFlag & (~SMP_KEY_DIST_CMD_FLAG_BIT_MASTER_IDENT);
773     if (SMP_GetPairMng()->peer.keyDistCmdFlag == 0x00) {
774         LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0.");
775         AlarmCancel(SMP_GetPairMng()->alarm);
776         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
777     }
778 }
779 
SMP_RecvIdentInfoProcessMaster(const Buffer * buffer)780 static void SMP_RecvIdentInfoProcessMaster(const Buffer *buffer)
781 {
782     int ret = SMP_SUCCESS;
783     LOG_INFO("%{public}s", __FUNCTION__);
784     if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_11) &&
785         (SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_12) &&
786         (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_MASTER_STEP_17) &&
787         (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_MASTER_STEP_18)) {
788         LOG_ERROR("It's invalid step.");
789         return;
790     }
791     if (memcpy_s(SMP_GetPairMng()->peer.IRK, SMP_IRK_LEN, (uint8_t *)BufferPtr(buffer), SMP_IRK_LEN) != EOK) {
792         LOG_ERROR("[%{public}s][%{public}d] memcpy_s fail.", __FUNCTION__, __LINE__);
793         return;
794     }
795     SMP_GetPairMng()->peer.keyDistCmdFlag =
796         SMP_GetPairMng()->peer.keyDistCmdFlag & (~SMP_KEY_DIST_CMD_FLAG_BIT_IDENT_INFO);
797     if ((SMP_GetPairMng()->peer.keyDistCmdFlag != 0x00) ||
798         (SMP_GetPairMng()->masterEncryptedFlag == SMP_MASTER_ENCRYPTED_FLAG_NO)) {
799         return;
800     }
801     AlarmCancel(SMP_GetPairMng()->alarm);
802     LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0.");
803     if ((SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) &&
804         (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ENC_KEY)) {
805         LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_13 is started.");
806         LOG_INFO("Local is need to send ENC_KEY.");
807         ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_13);
808     } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_SIGN_KEY) {
809         LOG_INFO("Local is need to send SIGN_KEY.");
810         if (SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) {
811             LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_17 is started.");
812             ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_17);
813         } else {
814             LOG_DEBUG("SMP_SC_PAIR_COMMON_MASTER_STEP_19 is started.");
815             ret = SMP_SendHciLeRandCmd(SMP_SC_PAIR_COMMON_MASTER_STEP_19);
816         }
817     } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ID_KEY) {
818         LOG_INFO("Send keys to Remote.");
819         ret = SMP_SendDistributionKeysToRemote();
820     } else {
821         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
822         LOG_INFO("Pair success!");
823         return;
824     }
825     if (ret != SMP_SUCCESS) {
826         SMP_GeneratePairResult(
827             SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
828         LOG_INFO("Pair failed!");
829     }
830 }
831 
SMP_RecvIdentInfoProcessSlave(const Buffer * buffer)832 static void SMP_RecvIdentInfoProcessSlave(const Buffer *buffer)
833 {
834     LOG_INFO("%{public}s", __FUNCTION__);
835 
836     if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_SLAVE_STEP_20) &&
837         (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_SLAVE_STEP_21)) {
838         LOG_ERROR("It's not SMP_LEGACY_PAIR_SLAVE_STEP_20");
839         LOG_ERROR("and It's not SMP_SC_PAIR_COMMON_SLAVE_STEP_21");
840         return;
841     }
842 
843     if (memcpy_s(SMP_GetPairMng()->peer.IRK, SMP_IRK_LEN, (uint8_t *)BufferPtr(buffer), SMP_IRK_LEN) != EOK) {
844         LOG_ERROR("[%{public}s][%{public}d] memcpy_s fail.", __FUNCTION__, __LINE__);
845         return;
846     }
847     SMP_GetPairMng()->peer.keyDistCmdFlag =
848         SMP_GetPairMng()->peer.keyDistCmdFlag & (~SMP_KEY_DIST_CMD_FLAG_BIT_IDENT_INFO);
849     if (SMP_GetPairMng()->peer.keyDistCmdFlag == 0x00) {
850         LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0.");
851         AlarmCancel(SMP_GetPairMng()->alarm);
852         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
853     }
854 }
855 
SMP_RecvIdentAddrInfoProcessMaster(const Buffer * buffer)856 static void SMP_RecvIdentAddrInfoProcessMaster(const Buffer *buffer)
857 {
858     int ret = SMP_SUCCESS;
859     uint8_t *pData1 = (uint8_t *)BufferPtr(buffer);
860     LOG_INFO("%{public}s", __FUNCTION__);
861     if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_11) &&
862         (SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_12) &&
863         (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_MASTER_STEP_17) &&
864         (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_MASTER_STEP_18)) {
865         LOG_ERROR("It's invalid step.");
866         return;
867     }
868     SMP_GetPairMng()->peer.identityAddr.type = pData1[0x00];
869     if (memcpy_s(SMP_GetPairMng()->peer.identityAddr.addr, BT_ADDRESS_SIZE, &(pData1[0x01]), BT_ADDRESS_SIZE) != EOK) {
870         LOG_ERROR("[%{public}s][%{public}d] memcpy_s fail.", __FUNCTION__, __LINE__);
871         return;
872     }
873     SMP_GetPairMng()->peer.keyDistCmdFlag =
874         SMP_GetPairMng()->peer.keyDistCmdFlag & (~SMP_KEY_DIST_CMD_FLAG_BIT_IDENT_ADDR);
875     if ((SMP_GetPairMng()->peer.keyDistCmdFlag != 0x00) ||
876         (SMP_GetPairMng()->masterEncryptedFlag == SMP_MASTER_ENCRYPTED_FLAG_NO)) {
877         return;
878     }
879     LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0");
880     AlarmCancel(SMP_GetPairMng()->alarm);
881     if ((SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) &&
882         (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ENC_KEY)) {
883         LOG_INFO("Local is need to send ENC_KEY!");
884         LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_13 is started!");
885         ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_13);
886     } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_SIGN_KEY) {
887         LOG_INFO("Local is need to send SIGN_KEY!");
888         if (SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) {
889             LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_17 is started!");
890             ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_17);
891         } else {
892             LOG_DEBUG("SMP_SC_PAIR_COMMON_MASTER_STEP_19 is started!");
893             ret = SMP_SendHciLeRandCmd(SMP_SC_PAIR_COMMON_MASTER_STEP_19);
894         }
895     } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ID_KEY) {
896         LOG_INFO("Send Keys to Remote.");
897         ret = SMP_SendDistributionKeysToRemote();
898     } else {
899         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
900         LOG_INFO("Pair Success");
901         return;
902     }
903     if (ret != SMP_SUCCESS) {
904         SMP_GeneratePairResult(
905             SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
906         LOG_INFO("Pair Failed");
907     }
908 }
909 
SMP_RecvIdentAddrInfoProcessSlave(const Buffer * buffer)910 static void SMP_RecvIdentAddrInfoProcessSlave(const Buffer *buffer)
911 {
912     uint8_t *pData2 = NULL;
913 
914     LOG_INFO("%{public}s", __FUNCTION__);
915 
916     if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_SLAVE_STEP_20) &&
917         (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_SLAVE_STEP_21)) {
918         LOG_ERROR("It's not SMP_LEGACY_PAIR_SLAVE_STEP_20.");
919         LOG_ERROR("and It's not SMP_SC_PAIR_COMMON_SLAVE_STEP_21.");
920         return;
921     }
922 
923     pData2 = (uint8_t *)BufferPtr(buffer);
924     SMP_GetPairMng()->peer.identityAddr.type = pData2[0x00];
925     if (memcpy_s(SMP_GetPairMng()->peer.identityAddr.addr,
926         sizeof(SMP_GetPairMng()->peer.identityAddr.addr),
927         &(pData2[0x01]),
928         sizeof(SMP_GetPairMng()->peer.identityAddr.addr)) != EOK) {
929             LOG_ERROR("[%{public}s][%{public}d] memcpy_s fail.", __FUNCTION__, __LINE__);
930             return;
931         }
932     SMP_GetPairMng()->peer.keyDistCmdFlag =
933         SMP_GetPairMng()->peer.keyDistCmdFlag & (~SMP_KEY_DIST_CMD_FLAG_BIT_IDENT_ADDR);
934     if (SMP_GetPairMng()->peer.keyDistCmdFlag == 0x00) {
935         AlarmCancel(SMP_GetPairMng()->alarm);
936         LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0.");
937         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
938     }
939 }
940 
SMP_RecvSignInfoProcessMaster(const Buffer * buffer)941 static void SMP_RecvSignInfoProcessMaster(const Buffer *buffer)
942 {
943     int ret = SMP_SUCCESS;
944     LOG_INFO("%{public}s", __FUNCTION__);
945     if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_11) &&
946         (SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_12) &&
947         (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_MASTER_STEP_17) &&
948         (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_MASTER_STEP_18)) {
949         LOG_ERROR("It's invalid step.");
950         return;
951     }
952     if (memcpy_s(SMP_GetPairMng()->peer.CSRK, SMP_CSRK_LEN, (uint8_t *)BufferPtr(buffer), SMP_CSRK_LEN) != EOK) {
953         LOG_ERROR("[%{public}s][%{public}d] memcpy_s fail.", __FUNCTION__, __LINE__);
954         return;
955     }
956     SMP_GetPairMng()->peer.keyDistCmdFlag =
957         SMP_GetPairMng()->peer.keyDistCmdFlag & (~SMP_KEY_DIST_CMD_FLAG_BIT_SIGN_INFO);
958     if ((SMP_GetPairMng()->peer.keyDistCmdFlag != 0x00) ||
959         (SMP_GetPairMng()->masterEncryptedFlag == SMP_MASTER_ENCRYPTED_FLAG_NO)) {
960         return;
961     }
962     LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0.");
963     AlarmCancel(SMP_GetPairMng()->alarm);
964     if ((SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) &&
965         (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ENC_KEY)) {
966         LOG_INFO("Local is need to send ENC_KEY");
967         LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_13 is started");
968         ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_13);
969     } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_SIGN_KEY) {
970         LOG_INFO("Local is need to send SIGN_KEY");
971         if (SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) {
972             LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_17 is started");
973             ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_17);
974         } else {
975             LOG_DEBUG("SMP_SC_PAIR_COMMON_MASTER_STEP_19 is started");
976             ret = SMP_SendHciLeRandCmd(SMP_SC_PAIR_COMMON_MASTER_STEP_19);
977         }
978     } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ID_KEY) {
979         LOG_INFO("Send Keys to Remote!");
980         ret = SMP_SendDistributionKeysToRemote();
981     } else {
982         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
983         LOG_INFO("Pair is Success!");
984         return;
985     }
986     if (ret != SMP_SUCCESS) {
987         SMP_GeneratePairResult(
988             SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
989         LOG_INFO("Pair is Failed!");
990     }
991 }
992 
SMP_RecvSignInfoProcessSlave(const Buffer * buffer)993 static void SMP_RecvSignInfoProcessSlave(const Buffer *buffer)
994 {
995     LOG_INFO("%{public}s", __FUNCTION__);
996 
997     if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_SLAVE_STEP_20) &&
998         (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_SLAVE_STEP_21)) {
999         LOG_ERROR("It's not SMP_LEGACY_PAIR_SLAVE_STEP_20.");
1000         LOG_ERROR("and It's not SMP_SC_PAIR_COMMON_SLAVE_STEP_21.");
1001         return;
1002     }
1003 
1004     if (memcpy_s(SMP_GetPairMng()->peer.CSRK, SMP_CSRK_LEN, (uint8_t *)BufferPtr(buffer), SMP_CSRK_LEN) != EOK) {
1005         LOG_ERROR("%{public}s, memcpy_s fail", __FUNCTION__);
1006         return;
1007     }
1008     SMP_GetPairMng()->peer.keyDistCmdFlag =
1009         SMP_GetPairMng()->peer.keyDistCmdFlag & (~SMP_KEY_DIST_CMD_FLAG_BIT_SIGN_INFO);
1010     if (SMP_GetPairMng()->peer.keyDistCmdFlag == 0x00) {
1011         LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0.");
1012         AlarmCancel(SMP_GetPairMng()->alarm);
1013         SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
1014     }
1015 }