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_send.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 "platform/include/list.h"
24 #include "smp.h"
25 #include "smp_aes_encryption.h"
26 #include "smp_cmd.h"
27 #include "smp_common.h"
28 #include "smp_def.h"
29 #include "smp_hci_event.h"
30 #include "smp_legacy.h"
31 #include "smp_oob.h"
32 #include "smp_privacy.h"
33 #include "smp_sc_accepter.h"
34 #include "smp_tool.h"
35 
36 static List *g_smpEncCmdList = NULL;
37 
38 static void SMP_SendKeysToRemoteCbProcess(uint16_t aclHandle, int result, uint8_t keyDistCmdFlag);
39 static int SMP_SendDistKeysToRemoteEncKey();
40 static int SMP_SendDistKeysToRemoteIdKey();
41 static int SMP_SendDistKeysToRemoteSignKey();
42 static void SMP_SendKeysCbProcException(uint16_t aclHandle);
43 static void SMP_EncCmdTimeoutGenSign();
44 static void SMP_EncCmdTimeoutGenRpa();
45 static void SMP_EncCmdTimeoutResoRpa(const SMP_EncCmd *pEncCmd);
46 static void SMP_EncCmdTimeoutGenScOobData();
47 static void SMP_EncCmdTimeoutDefault();
48 static int SMP_Aes128Hardware(const HciLeEncryptParam *pEncryptParam, SMP_EncCmd *encCmd);
49 static int SMP_Aes128Software(const HciLeEncryptParam *pEncryptParam, SMP_EncCmd *encCmd);
50 
51 static void SMP_EncCmdTimeoutTask(void *context);
52 static void SMP_SendDataCbTask(void *context);
53 static void SMP_SendPairRspCbTask(void *context);
54 static void SMP_SendEncInfoCbTask(void *context);
55 static void SMP_SendMasterIdentCbTask(void *context);
56 static void SMP_SendIdentInfoCbTask(void *context);
57 static void SMP_SendIdentAddrInfoCbTask(void *context);
58 static void SMP_SendSignInfoCbTask(void *context);
59 
SMP_SendDistributionKeysToRemote()60 int SMP_SendDistributionKeysToRemote()
61 {
62     int ret = SMP_SUCCESS;
63 
64     if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ENC_KEY) {
65         ret = SMP_SendDistKeysToRemoteEncKey();
66         if (ret != SMP_SUCCESS) {
67             return ret;
68         }
69     }
70 
71     if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ID_KEY) {
72         ret = SMP_SendDistKeysToRemoteIdKey();
73         if (ret != SMP_SUCCESS) {
74             return ret;
75         }
76     }
77 
78     if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_SIGN_KEY) {
79         ret = SMP_SendDistKeysToRemoteSignKey();
80         if (ret != SMP_SUCCESS) {
81             return ret;
82         }
83     }
84 
85     return ret;
86 }
87 
SMP_SendDistKeysToRemoteEncKey()88 static int SMP_SendDistKeysToRemoteEncKey()
89 {
90     uint8_t ltkTemp[SMP_LTK_LEN] = {0x00};
91     uint8_t masterIdRandTemp[SMP_MASTER_RAND_LEN] = {0x00};
92 
93     LOG_INFO("Local need to send ENC_KEY.");
94 
95     (void)memcpy_s(ltkTemp, SMP_LTK_LEN, SMP_GetPairMng()->local.LTK, SMP_LTK_LEN);
96     int ret = SMP_SendEncryptionInformation(SMP_GetPairMng()->handle, ltkTemp, SMP_SendEncryptionInformationCallback);
97     if (ret != SMP_SUCCESS) {
98         LOG_ERROR("Send Encryption Information failed.");
99         ret = SMP_ERR_REMOTE_ACTION;
100         return ret;
101     }
102 
103     SMP_MemoryReverseCopy(masterIdRandTemp, SMP_GetPairMng()->local.masterIdRand, SMP_MASTER_RAND_LEN);
104     ret = SMP_SendMasterIdentification(SMP_GetPairMng()->handle,
105         SMP_GetPairMng()->local.masterIdEdiv,
106         masterIdRandTemp,
107         SMP_SendMasterIdentificationCallback);
108     if (ret != SMP_SUCCESS) {
109         LOG_ERROR("Send Master Identification failed.");
110         ret = SMP_ERR_REMOTE_ACTION;
111         return ret;
112     }
113 
114     return ret;
115 }
116 
SMP_SendDistKeysToRemoteIdKey()117 static int SMP_SendDistKeysToRemoteIdKey()
118 {
119     uint8_t irkTemp[SMP_IRK_LEN] = {0x00};
120     BtAddr identAddrTemp;
121 
122     LOG_INFO("Local need to send ID_KEY.");
123 
124     SMP_GetLocalIrk(SMP_GetPairMng()->local.IRK, sizeof(SMP_GetPairMng()->local.IRK));
125     (void)memcpy_s(irkTemp, SMP_IRK_LEN, SMP_GetPairMng()->local.IRK, SMP_IRK_LEN);
126     int ret = SMP_SendIdentityInformation(SMP_GetPairMng()->handle, irkTemp, SMP_SendIdentityInformationCallback);
127     if (ret != SMP_SUCCESS) {
128         LOG_ERROR("Send Identity Information failed.");
129         ret = SMP_ERR_REMOTE_ACTION;
130         return ret;
131     }
132 
133     SMP_GetLocalIdentAddr(&SMP_GetPairMng()->local.identityAddr);
134     identAddrTemp.type = SMP_GetPairMng()->local.identityAddr.type;
135     SMP_MemoryReverseCopy(identAddrTemp.addr, SMP_GetPairMng()->local.identityAddr.addr, BT_ADDRESS_SIZE);
136     ret = SMP_SendIdentityAddressInformation(
137         SMP_GetPairMng()->handle, &identAddrTemp, SMP_SendIdentityAddressInformationCallback);
138     if (ret != SMP_SUCCESS) {
139         LOG_ERROR("Send Identity Address Information failed.");
140         ret = SMP_ERR_REMOTE_ACTION;
141         return ret;
142     }
143 
144     return ret;
145 }
146 
SMP_SendDistKeysToRemoteSignKey()147 static int SMP_SendDistKeysToRemoteSignKey()
148 {
149     uint8_t csrkTemp[SMP_CSRK_LEN] = {0x00};
150 
151     LOG_INFO("Local need to send SIGN_KEY.");
152 
153     (void)memcpy_s(csrkTemp, SMP_CSRK_LEN, SMP_GetPairMng()->local.CSRK, SMP_CSRK_LEN);
154     int ret = SMP_SendSigningInformation(SMP_GetPairMng()->handle, csrkTemp, SMP_SendSigningInformationCallback);
155     if (ret != SMP_SUCCESS) {
156         LOG_ERROR("Send Signing Information failed.");
157         ret = SMP_ERR_REMOTE_ACTION;
158         return ret;
159     }
160 
161     return ret;
162 }
163 
SMP_SendHciLeRandCmd(uint16_t step)164 int SMP_SendHciLeRandCmd(uint16_t step)
165 {
166     if (step) {
167         SMP_GetPairMng()->step = step;
168     }
169     AlarmSet(SMP_GetPairMng()->alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
170     int ret = HCI_LeRand();
171     if (ret != SMP_SUCCESS) {
172         LOG_ERROR("HCI_LeRand failed.");
173         AlarmCancel(SMP_GetPairMng()->alarm);
174     }
175 
176     return ret;
177 }
178 
SMP_SendLeEncryptCmd(const HciLeEncryptParam * pEncryptParam,uint16_t step,const SMP_EncCmd * pEncCmdData,bool isUsingHw)179 int SMP_SendLeEncryptCmd(
180     const HciLeEncryptParam *pEncryptParam, uint16_t step, const SMP_EncCmd *pEncCmdData, bool isUsingHw)
181 {
182     int ret;
183     SMP_EncCmd *encCmd = SMP_AllocEncCmd();
184     if (encCmd == NULL) {
185         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
186         return SMP_ERR_OUT_OF_RES;
187     }
188 
189     if (step) {
190         if ((step >= SMP_PAIR_STEP_START) && (step <= SMP_PAIR_STEP_END)) {
191             SMP_GetPairMng()->step = step;
192             encCmd->step = step;
193         } else {
194             encCmd->step = step;
195         }
196     }
197 
198     if (pEncCmdData != NULL) {
199         (void)memcpy_s(encCmd->X, sizeof(encCmd->X), pEncCmdData->X, sizeof(encCmd->X));
200         (void)memcpy_s(encCmd->Y, sizeof(encCmd->Y), pEncCmdData->Y, sizeof(encCmd->Y));
201         if (pEncCmdData->length != 0) {
202             encCmd->length = pEncCmdData->length;
203             encCmd->M = MEM_MALLOC.alloc(encCmd->length);
204             (void)memcpy_s(encCmd->M, encCmd->length, pEncCmdData->M, encCmd->length);
205         }
206         (void)memcpy_s(encCmd->M_last, sizeof(encCmd->M_last), pEncCmdData->M_last, sizeof(encCmd->M_last));
207         (void)memcpy_s(encCmd->key, sizeof(encCmd->key), pEncCmdData->key, sizeof(encCmd->key));
208         encCmd->n = pEncCmdData->n;
209         encCmd->i = pEncCmdData->i;
210         encCmd->signDataLen = pEncCmdData->signDataLen;
211         encCmd->signCounter = pEncCmdData->signCounter;
212         (void)memcpy_s(encCmd->address, sizeof(encCmd->address), pEncCmdData->address, sizeof(encCmd->address));
213     } else {
214         (void)memcpy_s(encCmd->key, sizeof(encCmd->key), pEncryptParam->key, sizeof(encCmd->key));
215     }
216 
217     SMP_ListAddLast(g_smpEncCmdList, encCmd);
218     AlarmSet(encCmd->timeoutTimer, SMP_PAIR_WAIT_TIME, SMP_EncCmdTimeout, encCmd);
219     if (isUsingHw) {
220         ret = SMP_Aes128Hardware(pEncryptParam, encCmd);
221     } else {
222         ret = SMP_Aes128Software(pEncryptParam, encCmd);
223     }
224 
225     return ret;
226 }
227 
SMP_SendDataCallback(uint16_t aclHandle,int result)228 void SMP_SendDataCallback(uint16_t aclHandle, int result)
229 {
230     LOG_INFO("%{public}s", __FUNCTION__);
231     SMP_SendL2capDataCbTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SendL2capDataCbTask_t));
232     if (ctx == NULL) {
233         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
234         return;
235     }
236     (void)memset_s(ctx, sizeof(SMP_SendL2capDataCbTask_t), 0x00, sizeof(SMP_SendL2capDataCbTask_t));
237     ctx->aclHandle = aclHandle;
238     ctx->result = result;
239     int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SendDataCbTask, (void *)ctx);
240     if (ret != SMP_SUCCESS) {
241         MEM_MALLOC.free(ctx);
242         return;
243     }
244 }
245 
SMP_SendDataCbTask(void * context)246 static void SMP_SendDataCbTask(void *context)
247 {
248     SMP_SendL2capDataCbTask_t *param = (SMP_SendL2capDataCbTask_t *)context;
249 
250     LOG_DEBUG("%{public}s", __FUNCTION__);
251     if (param->result) {
252         LOG_ERROR("Send Data failed result = %{public}d", param->result);
253         SMP_GeneratePairResult(
254             param->aclHandle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, SMP_GetPairMng()->alarm);
255         MEM_MALLOC.free(param);
256         return;
257     }
258     MEM_MALLOC.free(param);
259 }
260 
SMP_SendPairingResponseCallback(uint16_t aclHandle,int result)261 void SMP_SendPairingResponseCallback(uint16_t aclHandle, int result)
262 {
263     LOG_INFO("%{public}s", __FUNCTION__);
264     SMP_SendL2capDataCbTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SendL2capDataCbTask_t));
265     if (ctx == NULL) {
266         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
267         return;
268     }
269     (void)memset_s(ctx, sizeof(SMP_SendL2capDataCbTask_t), 0x00, sizeof(SMP_SendL2capDataCbTask_t));
270     ctx->aclHandle = aclHandle;
271     ctx->result = result;
272     int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SendPairRspCbTask, (void *)ctx);
273     if (ret != SMP_SUCCESS) {
274         MEM_MALLOC.free(ctx);
275         return;
276     }
277 }
278 
SMP_SendPairRspCbTask(void * context)279 static void SMP_SendPairRspCbTask(void *context)
280 {
281     SMP_SendL2capDataCbTask_t *param = (SMP_SendL2capDataCbTask_t *)context;
282 
283     LOG_DEBUG("%{public}s", __FUNCTION__);
284     if (param->result) {
285         LOG_ERROR("Send Data failed result = %{public}d", param->result);
286         SMP_GeneratePairResult(param->aclHandle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
287         MEM_MALLOC.free(param);
288         return;
289     }
290     MEM_MALLOC.free(param);
291 }
292 
SMP_SendPairingFailedCallback(uint16_t aclHandle,int result)293 void SMP_SendPairingFailedCallback(uint16_t aclHandle, int result)
294 {
295     LOG_INFO("%{public}s", __FUNCTION__);
296 
297     if (result) {
298         LOG_ERROR("Send Data failed result = %{public}d", result);
299     }
300     (void)aclHandle;
301 }
302 
SMP_SendSecurityRequestCallback(uint16_t aclHandle,int result)303 void SMP_SendSecurityRequestCallback(uint16_t aclHandle, int result)
304 {
305     LOG_INFO("%{public}s", __FUNCTION__);
306 
307     if (result) {
308         LOG_ERROR("Send Data failed result = %{public}d", result);
309     }
310     (void)aclHandle;
311 }
312 
SMP_SendEncryptionInformationCallback(uint16_t aclHandle,int result)313 void SMP_SendEncryptionInformationCallback(uint16_t aclHandle, int result)
314 {
315     LOG_INFO("%{public}s", __FUNCTION__);
316     SMP_SendL2capDataCbTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SendL2capDataCbTask_t));
317     if (ctx == NULL) {
318         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
319         return;
320     }
321     (void)memset_s(ctx, sizeof(SMP_SendL2capDataCbTask_t), 0x00, sizeof(SMP_SendL2capDataCbTask_t));
322     ctx->aclHandle = aclHandle;
323     ctx->result = result;
324     int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SendEncInfoCbTask, (void *)ctx);
325     if (ret != SMP_SUCCESS) {
326         MEM_MALLOC.free(ctx);
327         return;
328     }
329 }
330 
SMP_SendEncInfoCbTask(void * context)331 static void SMP_SendEncInfoCbTask(void *context)
332 {
333     LOG_DEBUG("%{public}s", __FUNCTION__);
334     SMP_SendL2capDataCbTask_t *param = (SMP_SendL2capDataCbTask_t *)context;
335     SMP_SendKeysToRemoteCbProcess(param->aclHandle, param->result, SMP_KEY_DIST_CMD_FLAG_BIT_ENC_INFO);
336     MEM_MALLOC.free(param);
337 }
338 
SMP_SendMasterIdentificationCallback(uint16_t aclHandle,int result)339 void SMP_SendMasterIdentificationCallback(uint16_t aclHandle, int result)
340 {
341     LOG_INFO("%{public}s", __FUNCTION__);
342     SMP_SendL2capDataCbTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SendL2capDataCbTask_t));
343     if (ctx == NULL) {
344         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
345         return;
346     }
347     (void)memset_s(ctx, sizeof(SMP_SendL2capDataCbTask_t), 0x00, sizeof(SMP_SendL2capDataCbTask_t));
348     ctx->aclHandle = aclHandle;
349     ctx->result = result;
350     int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SendMasterIdentCbTask, (void *)ctx);
351     if (ret != SMP_SUCCESS) {
352         MEM_MALLOC.free(ctx);
353         return;
354     }
355 }
356 
SMP_SendMasterIdentCbTask(void * context)357 static void SMP_SendMasterIdentCbTask(void *context)
358 {
359     LOG_DEBUG("%{public}s", __FUNCTION__);
360     SMP_SendL2capDataCbTask_t *param = (SMP_SendL2capDataCbTask_t *)context;
361     SMP_SendKeysToRemoteCbProcess(param->aclHandle, param->result, SMP_KEY_DIST_CMD_FLAG_BIT_MASTER_IDENT);
362     MEM_MALLOC.free(param);
363 }
364 
SMP_SendIdentityInformationCallback(uint16_t aclHandle,int result)365 void SMP_SendIdentityInformationCallback(uint16_t aclHandle, int result)
366 {
367     LOG_INFO("%{public}s", __FUNCTION__);
368     SMP_SendL2capDataCbTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SendL2capDataCbTask_t));
369     if (ctx == NULL) {
370         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
371         return;
372     }
373     (void)memset_s(ctx, sizeof(SMP_SendL2capDataCbTask_t), 0x00, sizeof(SMP_SendL2capDataCbTask_t));
374     ctx->aclHandle = aclHandle;
375     ctx->result = result;
376     int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SendIdentInfoCbTask, (void *)ctx);
377     if (ret != SMP_SUCCESS) {
378         MEM_MALLOC.free(ctx);
379         return;
380     }
381 }
382 
SMP_SendIdentInfoCbTask(void * context)383 static void SMP_SendIdentInfoCbTask(void *context)
384 {
385     LOG_DEBUG("%{public}s", __FUNCTION__);
386     SMP_SendL2capDataCbTask_t *param = (SMP_SendL2capDataCbTask_t *)context;
387     SMP_SendKeysToRemoteCbProcess(param->aclHandle, param->result, SMP_KEY_DIST_CMD_FLAG_BIT_IDENT_INFO);
388     MEM_MALLOC.free(param);
389 }
390 
SMP_SendIdentityAddressInformationCallback(uint16_t aclHandle,int result)391 void SMP_SendIdentityAddressInformationCallback(uint16_t aclHandle, int result)
392 {
393     LOG_INFO("%{public}s", __FUNCTION__);
394     SMP_SendL2capDataCbTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SendL2capDataCbTask_t));
395     if (ctx == NULL) {
396         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
397         return;
398     }
399     (void)memset_s(ctx, sizeof(SMP_SendL2capDataCbTask_t), 0x00, sizeof(SMP_SendL2capDataCbTask_t));
400     ctx->aclHandle = aclHandle;
401     ctx->result = result;
402     int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SendIdentAddrInfoCbTask, (void *)ctx);
403     if (ret != SMP_SUCCESS) {
404         MEM_MALLOC.free(ctx);
405         return;
406     }
407 }
408 
SMP_SendIdentAddrInfoCbTask(void * context)409 static void SMP_SendIdentAddrInfoCbTask(void *context)
410 {
411     LOG_DEBUG("%{public}s", __FUNCTION__);
412     SMP_SendL2capDataCbTask_t *param = (SMP_SendL2capDataCbTask_t *)context;
413     SMP_SendKeysToRemoteCbProcess(param->aclHandle, param->result, SMP_KEY_DIST_CMD_FLAG_BIT_IDENT_ADDR);
414     MEM_MALLOC.free(param);
415 }
416 
SMP_SendSigningInformationCallback(uint16_t aclHandle,int result)417 void SMP_SendSigningInformationCallback(uint16_t aclHandle, int result)
418 {
419     LOG_INFO("%{public}s", __FUNCTION__);
420     SMP_SendL2capDataCbTask_t *ctx = MEM_MALLOC.alloc(sizeof(SMP_SendL2capDataCbTask_t));
421     if (ctx == NULL) {
422         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
423         return;
424     }
425     (void)memset_s(ctx, sizeof(SMP_SendL2capDataCbTask_t), 0x00, sizeof(SMP_SendL2capDataCbTask_t));
426     ctx->aclHandle = aclHandle;
427     ctx->result = result;
428     int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_SendSignInfoCbTask, (void *)ctx);
429     if (ret != SMP_SUCCESS) {
430         MEM_MALLOC.free(ctx);
431         return;
432     }
433 }
434 
SMP_SendSignInfoCbTask(void * context)435 static void SMP_SendSignInfoCbTask(void *context)
436 {
437     LOG_DEBUG("%{public}s", __FUNCTION__);
438     SMP_SendL2capDataCbTask_t *param = (SMP_SendL2capDataCbTask_t *)context;
439     SMP_SendKeysToRemoteCbProcess(param->aclHandle, param->result, SMP_KEY_DIST_CMD_FLAG_BIT_SIGN_INFO);
440     MEM_MALLOC.free(param);
441 }
442 
SMP_FreeEncCmd(void * encCmd)443 void SMP_FreeEncCmd(void *encCmd)
444 {
445     SMP_EncCmd *pEncCmd = (SMP_EncCmd *)encCmd;
446 
447     LOG_DEBUG("%{public}s", __FUNCTION__);
448     if (pEncCmd != NULL) {
449         if (pEncCmd->timeoutTimer != NULL) {
450             AlarmCancel(pEncCmd->timeoutTimer);
451             AlarmDelete(pEncCmd->timeoutTimer);
452             pEncCmd->timeoutTimer = NULL;
453         }
454         if (pEncCmd->M != NULL) {
455             MEM_MALLOC.free(pEncCmd->M);
456         }
457         MEM_MALLOC.free(encCmd);
458     }
459 }
460 
SMP_AllocEncCmd()461 SMP_EncCmd *SMP_AllocEncCmd()
462 {
463     SMP_EncCmd *pEncCmd = MEM_MALLOC.alloc(sizeof(SMP_EncCmd));
464 
465     LOG_DEBUG("%{public}s", __FUNCTION__);
466     if (pEncCmd != NULL) {
467         pEncCmd->step = 0x00;
468         (void)memset_s(pEncCmd, sizeof(SMP_EncCmd), 0x00, sizeof(SMP_EncCmd));
469         pEncCmd->timeoutTimer = AlarmCreate("", false);
470     }
471     return pEncCmd;
472 }
473 
SMP_EncCmdTimeout(void * param)474 void SMP_EncCmdTimeout(void *param)
475 {
476     LOG_INFO("%{public}s", __FUNCTION__);
477     SMP_EncTimeoutParam *ctx = MEM_MALLOC.alloc(sizeof(SMP_EncTimeoutParam));
478     if (ctx == NULL) {
479         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
480         return;
481     }
482     (void)memset_s(ctx, sizeof(SMP_EncTimeoutParam), 0x00, sizeof(SMP_EncTimeoutParam));
483     ctx->encCmd = (SMP_EncCmd *)param;
484     int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_EncCmdTimeoutTask, (void *)ctx);
485     if (ret != SMP_SUCCESS) {
486         MEM_MALLOC.free(ctx);
487         return;
488     }
489 }
490 
SMP_EncCmdTimeoutTask(void * context)491 static void SMP_EncCmdTimeoutTask(void *context)
492 {
493     SMP_EncTimeoutParam *timeoutParam = (SMP_EncTimeoutParam *)context;
494     SMP_EncCmd *pEncCmd = timeoutParam->encCmd;
495 
496     LOG_DEBUG("%{public}s", __FUNCTION__);
497     ListNode *node = SMP_ListGetFirstNode(g_smpEncCmdList);
498     while (node != NULL) {
499         if (pEncCmd == SMP_ListGetNodeData(node)) {
500             switch (pEncCmd->step) {
501                 case SMP_GENERATE_SIGNATURE_STEP_1:
502                 case SMP_GENERATE_SIGNATURE_STEP_2:
503                 case SMP_GENERATE_SIGNATURE_STEP_3:
504                     SMP_EncCmdTimeoutGenSign();
505                     break;
506                 case SMP_GENERATE_RPA_STEP_1:
507                     SMP_EncCmdTimeoutGenRpa();
508                     break;
509                 case SMP_RESOLVE_RPA_STEP_1:
510                     SMP_EncCmdTimeoutResoRpa(pEncCmd);
511                     break;
512                 case SMP_GENERATE_SC_OOB_DATA_STEP_2:
513                 case SMP_GENERATE_SC_OOB_DATA_STEP_3:
514                 case SMP_GENERATE_SC_OOB_DATA_STEP_4:
515                     SMP_EncCmdTimeoutGenScOobData();
516                     break;
517                 default:
518                     SMP_EncCmdTimeoutDefault();
519                     break;
520             }
521             ListRemoveNode(g_smpEncCmdList, pEncCmd);
522             break;
523         }
524         node = ListGetNextNode(node);
525     }
526     MEM_MALLOC.free(timeoutParam);
527 }
528 
SMP_SendKeysToRemoteCbProcess(uint16_t aclHandle,int result,uint8_t keyDistCmdFlag)529 static void SMP_SendKeysToRemoteCbProcess(uint16_t aclHandle, int result, uint8_t keyDistCmdFlag)
530 {
531     LOG_DEBUG("%{public}s", __FUNCTION__);
532     if (result) {
533         LOG_ERROR("Send Data failed result = %{public}d", result);
534         SMP_SendKeysCbProcException(aclHandle);
535     } else {
536         SMP_GetPairMng()->local.keyDistCmdFlag = SMP_GetPairMng()->local.keyDistCmdFlag & (~keyDistCmdFlag);
537         if (SMP_GetPairMng()->local.keyDistCmdFlag != 0x00) {
538             return;
539         }
540         LOG_INFO("g_smpPairMng.local.keyDistCmdFlag is 0.");
541         if (SMP_ROLE_MASTER == SMP_GetPairMng()->role) {
542             SMP_GeneratePairResult(aclHandle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
543         } else {
544             if (!((SMP_GetPairMng()->local.pairParam.authReq & SMP_AUTH_REQ_BONDING) &&
545                 (SMP_GetPairMng()->peer.pairParam.authReq & SMP_AUTH_REQ_BONDING))) {
546                 LOG_INFO("It's no bonding.");
547                 AlarmCancel(SMP_GetPairMng()->alarm);
548                 SMP_GeneratePairResult(aclHandle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
549             } else if (SMP_GetPairMng()->peer.keyDist == 0x00) {
550                 LOG_INFO("g_smpPairMng.peer.keyDist is 0.");
551                 AlarmCancel(SMP_GetPairMng()->alarm);
552                 SMP_GeneratePairResult(aclHandle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
553             }
554         }
555     }
556 }
557 
SMP_SendKeysCbProcException(uint16_t aclHandle)558 static void SMP_SendKeysCbProcException(uint16_t aclHandle)
559 {
560     if (SMP_GetPairMng()->role == SMP_ROLE_MASTER) {
561         SMP_GeneratePairResult(aclHandle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
562     } else {
563         SMP_GeneratePairResult(
564             aclHandle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, SMP_GetPairMng()->alarm);
565     }
566 }
567 
SMP_EncCmdTimeoutGenSign()568 static void SMP_EncCmdTimeoutGenSign()
569 {
570     SMP_NotifyCbGenSign(SMP_GENERATE_SIGN_STATUS_FAILED, NULL);
571 }
572 
SMP_EncCmdTimeoutGenRpa()573 static void SMP_EncCmdTimeoutGenRpa()
574 {
575     SMP_NotifyCbGenRpa(SMP_GENERATE_RPA_STATUS_FAILED, NULL);
576 }
577 
SMP_EncCmdTimeoutResoRpa(const SMP_EncCmd * pEncCmd)578 static void SMP_EncCmdTimeoutResoRpa(const SMP_EncCmd *pEncCmd)
579 {
580     uint8_t key[SMP_ENCRYPT_KEY_LEN] = {0x00};
581     uint8_t address[BT_ADDRESS_SIZE] = {0x00};
582 
583     SMP_MemoryReverseCopy(address, pEncCmd->address, sizeof(address));
584     (void)memcpy_s(key, sizeof(key), pEncCmd->key, sizeof(key));
585     SMP_NotifyCbResoRpa(SMP_RESOLVE_RPA_STATUS_FAILED, false, address, key);
586 }
587 
SMP_EncCmdTimeoutGenScOobData()588 static void SMP_EncCmdTimeoutGenScOobData()
589 {
590     SMP_ClearScOobData(false);
591     SMP_NotifyCbGenScOobData(SMP_GENERATE_SC_OOB_DATA_FAILED, NULL, NULL);
592 }
593 
SMP_EncCmdTimeoutDefault()594 static void SMP_EncCmdTimeoutDefault()
595 {
596     if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
597         LOG_ERROR("It's not pairing state");
598         return;
599     }
600 
601     if (SMP_GetPairMng()->role == SMP_ROLE_MASTER) {
602         LOG_ERROR("Initiator, step:%hu", SMP_GetPairMng()->step);
603     } else {
604         LOG_ERROR("Accepter, step:%hu", SMP_GetPairMng()->step);
605     }
606 
607     SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
608 }
609 
SMP_Aes128Hardware(const HciLeEncryptParam * pEncryptParam,SMP_EncCmd * encCmd)610 static int SMP_Aes128Hardware(const HciLeEncryptParam *pEncryptParam, SMP_EncCmd *encCmd)
611 {
612     int ret = HCI_LeEncrypt(pEncryptParam);
613     if (ret != SMP_SUCCESS) {
614         LOG_ERROR("HCI_LeEncrypt failed.");
615         AlarmCancel(encCmd->timeoutTimer);
616         SMP_ListRemoveNode(g_smpEncCmdList, encCmd);
617     }
618 
619     return ret;
620 }
621 
SMP_Aes128Software(const HciLeEncryptParam * pEncryptParam,SMP_EncCmd * encCmd)622 static int SMP_Aes128Software(const HciLeEncryptParam *pEncryptParam, SMP_EncCmd *encCmd)
623 {
624     uint8_t encryptedData[SMP_ENCRYPT_PLAINTEXTDATA_LEN] = {0x00};
625 
626     int ret = SMP_Aes128(pEncryptParam->key,
627         sizeof(pEncryptParam->key),
628         pEncryptParam->plaintextData,
629         sizeof(pEncryptParam->plaintextData),
630         encryptedData);
631     HciLeEncryptReturnParam *ctx = MEM_MALLOC.alloc(sizeof(HciLeEncryptReturnParam));
632     if (ctx == NULL) {
633         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
634         return SMP_ERR_OUT_OF_RES;
635     }
636     (void)memset_s(ctx, sizeof(HciLeEncryptReturnParam), 0x00, sizeof(HciLeEncryptReturnParam));
637     ctx->status = (uint8_t)ret;
638     (void)memcpy_s(ctx->encryptedData, sizeof(ctx->encryptedData), encryptedData, sizeof(ctx->encryptedData));
639     ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_OnLeEncCompTask, (void *)ctx);
640     if (ret != SMP_SUCCESS) {
641         LOG_ERROR("SMP_Aes128 failed.");
642         MEM_MALLOC.free(ctx);
643         AlarmCancel(encCmd->timeoutTimer);
644         SMP_ListRemoveNode(g_smpEncCmdList, encCmd);
645     }
646 
647     return ret;
648 }
649 
SMP_GetEncCmdList()650 List *SMP_GetEncCmdList()
651 {
652     return g_smpEncCmdList;
653 }
654 
SMP_SetEncCmdList(List * list)655 void SMP_SetEncCmdList(List *list)
656 {
657     g_smpEncCmdList = list;
658 }