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_hci_event.h"
17 #include <string.h>
18
19 #include "btm/btm_thread.h"
20 #include "log.h"
21 #include "platform/include/allocator.h"
22 #include "smp.h"
23 #include "smp_cmd.h"
24 #include "smp_common.h"
25 #include "smp_def.h"
26 #include "smp_legacy.h"
27 #include "smp_oob.h"
28 #include "smp_privacy.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 SMP_StepFunc g_genOOB[] = {
35 SMP_GenerateScOobDataStep1,
36 SMP_GenerateScOobDataStep2,
37 SMP_GenerateScOobDataStep3,
38 SMP_GenerateScOobDataStep4,
39 };
40 static SMP_StepFunc g_genSignature[] = {
41 SMP_GenerateSignatureStep1,
42 SMP_GenerateSignatureStep2,
43 SMP_GenerateSignatureStep3,
44 };
45
46 static SMP_StepFunc g_masLegacy[] = {
47 SMP_LegacyPairMasterStep1,
48 SMP_LegacyPairMasterStep2,
49 SMP_LegacyPairMasterStep3,
50 SMP_LegacyPairMasterStep4,
51 SMP_LegacyPairMasterStep5,
52 NULL,
53 NULL,
54 SMP_LegacyPairMasterStep8,
55 SMP_LegacyPairMasterStep9,
56 SMP_LegacyPairMasterStep10,
57 NULL,
58 NULL,
59 SMP_LegacyPairMasterStep13,
60 SMP_LegacyPairMasterStep14,
61 SMP_LegacyPairMasterStep15,
62 SMP_LegacyPairMasterStep16,
63 SMP_LegacyPairMasterStep17,
64 SMP_LegacyPairMasterStep18,
65 };
66
67 static SMP_StepFunc g_slaLegacy[] = {
68 SMP_LegacyPairSlaveStep1,
69 NULL,
70 NULL,
71 SMP_LegacyPairSlaveStep4,
72 SMP_LegacyPairSlaveStep5,
73 SMP_LegacyPairSlaveStep6,
74 SMP_LegacyPairSlaveStep7,
75 NULL,
76 SMP_LegacyPairSlaveStep9,
77 SMP_LegacyPairSlaveStep10,
78 SMP_LegacyPairSlaveStep11,
79 NULL,
80 NULL,
81 SMP_LegacyPairSlaveStep14,
82 SMP_LegacyPairSlaveStep15,
83 SMP_LegacyPairSlaveStep16,
84 SMP_LegacyPairSlaveStep17,
85 SMP_LegacyPairSlaveStep18,
86 SMP_LegacyPairSlaveStep19,
87 NULL,
88 };
89
90 static SMP_StepFunc g_masScjw[] = {
91 SMP_ScPairJustworkOrNumericMasterStep1,
92 SMP_ScPairJustworkOrNumericMasterStep2,
93 SMP_ScPairJustworkOrNumericMasterStep3,
94 SMP_ScPairJustworkOrNumericMasterStep4,
95 SMP_ScPairJustworkOrNumericMasterStep5,
96 SMP_ScPairJustworkOrNumericMasterStep6,
97 SMP_ScPairJustworkOrNumericMasterStep7,
98 SMP_ScPairJustworkOrNumericMasterStep8,
99 SMP_ScPairJustworkOrNumericMasterStep9,
100 SMP_ScPairJustworkOrNumericMasterStep10,
101 SMP_ScPairJustworkOrNumericMasterStep11,
102 SMP_ScPairJustworkOrNumericMasterStep12,
103 SMP_ScPairJustworkOrNumericMasterStep13,
104 NULL,
105 };
106
107 static SMP_StepFunc g_masScpk[] = {
108 SMP_ScPairPasskeyEntryMasterStep1,
109 SMP_ScPairPasskeyEntryMasterStep2,
110 SMP_ScPairPasskeyEntryMasterStep3,
111 SMP_ScPairPasskeyEntryMasterStep4,
112 NULL,
113 SMP_ScPairPasskeyEntryMasterStep6,
114 SMP_ScPairPasskeyEntryMasterStep7,
115 SMP_ScPairPasskeyEntryMasterStep8,
116 SMP_ScPairPasskeyEntryMasterStep9,
117 SMP_ScPairPasskeyEntryMasterStep10,
118 SMP_ScPairPasskeyEntryMasterStep11,
119 SMP_ScPairPasskeyEntryMasterStep12,
120 SMP_ScPairPasskeyEntryMasterStep13,
121 SMP_ScPairPasskeyEntryMasterStep14,
122 SMP_ScPairPasskeyEntryMasterStep15,
123 };
124
125 static SMP_StepFunc g_masScob[] = {
126 SMP_ScPairOobMasterStep1,
127 NULL,
128 NULL,
129 NULL,
130 NULL,
131 NULL,
132 NULL,
133 SMP_ScPairOobMasterStep8,
134 SMP_ScPairOobMasterStep9,
135 SMP_ScPairOobMasterStep10,
136 SMP_ScPairOobMasterStep11,
137 SMP_ScPairOobMasterStep12,
138 SMP_ScPairOobMasterStep13,
139 SMP_ScPairOobMasterStep14,
140 SMP_ScPairOobMasterStep15,
141 };
142
143 static SMP_StepFunc g_masScCommon[] = {
144 SMP_ScPairCommonMasterStep1,
145 SMP_ScPairCommonMasterStep2,
146 SMP_ScPairCommonMasterStep3,
147 SMP_ScPairCommonMasterStep4,
148 SMP_ScPairCommonMasterStep5,
149 SMP_ScPairCommonMasterStep6,
150 SMP_ScPairCommonMasterStep7,
151 SMP_ScPairCommonMasterStep8,
152 SMP_ScPairCommonMasterStep9,
153 SMP_ScPairCommonMasterStep10,
154 SMP_ScPairCommonMasterStep11,
155 SMP_ScPairCommonMasterStep12,
156 SMP_ScPairCommonMasterStep13,
157 SMP_ScPairCommonMasterStep14,
158 SMP_ScPairCommonMasterStep15,
159 SMP_ScPairCommonMasterStep16,
160 NULL,
161 NULL,
162 SMP_ScPairCommonMasterStep19,
163 SMP_ScPairCommonMasterStep20,
164 };
165
166 static SMP_StepFunc g_slaScjw[] = {
167 SMP_ScPairJustworkOrNumericSlaveStep1,
168 SMP_ScPairJustworkOrNumericSlaveStep2,
169 SMP_ScPairJustworkOrNumericSlaveStep3,
170 SMP_ScPairJustworkOrNumericSlaveStep4,
171 SMP_ScPairJustworkOrNumericSlaveStep5,
172 SMP_ScPairJustworkOrNumericSlaveStep6,
173 SMP_ScPairJustworkOrNumericSlaveStep7,
174 SMP_ScPairJustworkOrNumericSlaveStep8,
175 SMP_ScPairJustworkOrNumericSlaveStep9,
176 SMP_ScPairJustworkOrNumericSlaveStep10,
177 SMP_ScPairJustworkOrNumericSlaveStep11,
178 SMP_ScPairJustworkOrNumericSlaveStep12,
179 SMP_ScPairJustworkOrNumericSlaveStep13,
180 NULL,
181 };
182
183 static SMP_StepFunc g_slaScpk[] = {
184 SMP_ScPairPasskeyEntrySlaveStep1,
185 SMP_ScPairPasskeyEntrySlaveStep2,
186 SMP_ScPairPasskeyEntrySlaveStep3,
187 SMP_ScPairPasskeyEntrySlaveStep4,
188 NULL,
189 SMP_ScPairPasskeyEntrySlaveStep6,
190 SMP_ScPairPasskeyEntrySlaveStep7,
191 SMP_ScPairPasskeyEntrySlaveStep8,
192 SMP_ScPairPasskeyEntrySlaveStep9,
193 SMP_ScPairPasskeyEntrySlaveStep10,
194 SMP_ScPairPasskeyEntrySlaveStep11,
195 SMP_ScPairPasskeyEntrySlaveStep12,
196 SMP_ScPairPasskeyEntrySlaveStep13,
197 SMP_ScPairPasskeyEntrySlaveStep14,
198 SMP_ScPairPasskeyEntrySlaveStep15,
199 };
200
201 static SMP_StepFunc g_slaScob[] = {
202 SMP_ScPairOobSlaveStep1,
203 NULL,
204 NULL,
205 NULL,
206 NULL,
207 NULL,
208 NULL,
209 SMP_ScPairOobSlaveStep8,
210 SMP_ScPairOobSlaveStep9,
211 SMP_ScPairOobSlaveStep10,
212 SMP_ScPairOobSlaveStep11,
213 SMP_ScPairOobSlaveStep12,
214 SMP_ScPairOobSlaveStep13,
215 SMP_ScPairOobSlaveStep14,
216 SMP_ScPairOobSlaveStep15,
217 };
218
219 static SMP_StepFunc g_slaScCommon[] = {
220 SMP_ScPairCommonSlaveStep1,
221 SMP_ScPairCommonSlaveStep2,
222 SMP_ScPairCommonSlaveStep3,
223 SMP_ScPairCommonSlaveStep4,
224 SMP_ScPairCommonSlaveStep5,
225 SMP_ScPairCommonSlaveStep6,
226 SMP_ScPairCommonSlaveStep7,
227 SMP_ScPairCommonSlaveStep8,
228 SMP_ScPairCommonSlaveStep9,
229 SMP_ScPairCommonSlaveStep10,
230 SMP_ScPairCommonSlaveStep11,
231 SMP_ScPairCommonSlaveStep12,
232 SMP_ScPairCommonSlaveStep13,
233 SMP_ScPairCommonSlaveStep14,
234 SMP_ScPairCommonSlaveStep15,
235 SMP_ScPairCommonSlaveStep16,
236 NULL,
237 NULL,
238 SMP_ScPairCommonSlaveStep19,
239 SMP_ScPairCommonSlaveStep20,
240 NULL,
241 };
242
GetPairTable(uint16_t step)243 SMP_StepFunc GetPairTable(uint16_t step)
244 {
245 if (step >= SMP_LEGACY_PAIR_MASTER_STEP_1 && step <= SMP_LEGACY_PAIR_MASTER_STEP_18) {
246 return g_masLegacy[step - SMP_LEGACY_PAIR_MASTER_STEP_1];
247 } else if (step >= SMP_LEGACY_PAIR_SLAVE_STEP_1 && step <= SMP_LEGACY_PAIR_SLAVE_STEP_20) {
248 return g_slaLegacy[step - SMP_LEGACY_PAIR_SLAVE_STEP_1];
249 } else if (step >= SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_1 &&
250 step <= SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_14) {
251 return g_masScjw[step - SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_1];
252 } else if (step >= SMP_SC_PAIR_COMMON_MASTER_STEP_1 && step <= SMP_SC_PAIR_COMMON_MASTER_STEP_20) {
253 return g_masScCommon[step - SMP_SC_PAIR_COMMON_MASTER_STEP_1];
254 } else if (step >= SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_1 && step <= SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_15) {
255 return g_masScpk[step - SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_1];
256 } else if (step >= SMP_SC_PAIR_OOB_MASTER_STEP_1 && step <= SMP_SC_PAIR_OOB_MASTER_STEP_15) {
257 return g_masScob[step - SMP_SC_PAIR_OOB_MASTER_STEP_1];
258 } else if (step >= SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_1 &&
259 step <= SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_14) {
260 return g_slaScjw[step - SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_1];
261 } else if (step >= SMP_SC_PAIR_COMMON_SLAVE_STEP_1 && step <= SMP_SC_PAIR_COMMON_SLAVE_STEP_21) {
262 return g_slaScCommon[step - SMP_SC_PAIR_COMMON_SLAVE_STEP_1];
263 } else if (step >= SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_1 && step <= SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_15) {
264 return g_slaScpk[step - SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_1];
265 } else if (step >= SMP_SC_PAIR_OOB_SLAVE_STEP_1 && step <= SMP_SC_PAIR_OOB_SLAVE_STEP_15) {
266 return g_slaScob[step - SMP_SC_PAIR_OOB_SLAVE_STEP_1];
267 } else if (step >= SMP_GENERATE_SC_OOB_DATA_STEP_1 && step <= SMP_GENERATE_SC_OOB_DATA_STEP_4) {
268 return g_genOOB[step - SMP_GENERATE_SC_OOB_DATA_STEP_1];
269 } else if (step >= SMP_GENERATE_SIGNATURE_STEP_1 && step <= SMP_GENERATE_SIGNATURE_STEP_3) {
270 return g_genSignature[step - SMP_GENERATE_SIGNATURE_STEP_1];
271 } else {
272 return NULL;
273 }
274 }
275
SMP_ExecuteStepFunc(uint16_t step,const SMP_StepParam * param)276 void SMP_ExecuteStepFunc(uint16_t step, const SMP_StepParam *param)
277 {
278 SMP_StepFunc nextStepFunc;
279 if (step == SMP_RESOLVE_RPA_STEP_1) {
280 SMP_ResolveRpaStep1(param);
281 } else if (step == SMP_GENERATE_RPA_STEP_1) {
282 SMP_GenerateRpaStep1(param);
283 } else {
284 nextStepFunc = GetPairTable(step);
285 if (nextStepFunc != NULL) {
286 nextStepFunc(param);
287 } else {
288 LOG_ERROR("It's invalid step:%hu.", step);
289 return;
290 }
291 }
292 }
293
294 static void SMP_LinkLayerEncryptionComplete(uint16_t handle, uint8_t status);
295 static void SMP_LinkLayerEncCompMaster();
296 static void SMP_LinkLayerEncCompSlave();
297 static void SMP_OnLtkReqLegacyPair();
298 static void SMP_OnLtkReqScPair();
299 static void SMP_OnLtkReqEncProc(const HciLeLongTermKeyRequestEventParam *eventParam);
300 static void SMP_OnGenDHKeyCompScJwOrNumMasterStep3(const HciLeGenerateDHKeyCompleteEventParam *eventParam);
301 static void SMP_OnGenDHKeyCompScJwOrNumMasterStep4(const HciLeGenerateDHKeyCompleteEventParam *eventParam);
302 static void SMP_OnReadLocalPubKeyCompStepDistribution(uint16_t step, const SMP_StepParam *param);
303 static void SMP_OnGenDhKeyCompStepDistribution(uint16_t step, const SMP_StepParam *param);
304
305 static void SMP_OnLeRandCompTask(void *context);
306 static void SMP_OnLeEncChangeTask(void *context);
307 static void SMP_OnLeEncKeyRefCompTask(void *context);
308 static void SMP_OnLeLongTermKeyReqTask(void *context);
309 static void SMP_OnLeLongTermKeyReqReplyCompTask(void *context);
310 static void SMP_OnLeReadLocalP256PubKeyCompTask(void *context);
311 static void SMP_OnLeGenDhKeyCompTask(void *context);
312
SMP_OnLERandComplete(const HciLeRandReturnParam * returnParam)313 void SMP_OnLERandComplete(const HciLeRandReturnParam *returnParam)
314 {
315 LOG_INFO("%{public}s", __FUNCTION__);
316 HciLeRandReturnParam *ctx = MEM_MALLOC.alloc(sizeof(HciLeRandReturnParam));
317 if (ctx == NULL) {
318 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
319 return;
320 }
321 (void)memset_s(ctx, sizeof(HciLeRandReturnParam), 0x00, sizeof(HciLeRandReturnParam));
322 (void)memcpy_s(ctx, sizeof(HciLeRandReturnParam), returnParam, sizeof(HciLeRandReturnParam));
323 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_OnLeRandCompTask, (void *)ctx);
324 if (ret != SMP_SUCCESS) {
325 MEM_MALLOC.free(ctx);
326 return;
327 }
328 }
329
SMP_OnLeRandCompTask(void * context)330 static void SMP_OnLeRandCompTask(void *context)
331 {
332 HciLeRandReturnParam *returnParam = (HciLeRandReturnParam *)context;
333 SMP_StepParam param = {.data = (void *)returnParam};
334
335 LOG_DEBUG("%{public}s", __FUNCTION__);
336 if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
337 LOG_ERROR("It's not pairing state.");
338 MEM_MALLOC.free(returnParam);
339 return;
340 }
341 AlarmCancel(SMP_GetPairMng()->alarm);
342 if (returnParam->status) {
343 LOG_ERROR("returnParam->status = %hhu.", returnParam->status);
344 SMP_GeneratePairResult(
345 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
346 } else {
347 SMP_ExecuteStepFunc(SMP_GetPairMng()->step, ¶m);
348 }
349 MEM_MALLOC.free(returnParam);
350 }
351
SMP_OnLEEncryptComplete(const HciLeEncryptReturnParam * returnParam)352 void SMP_OnLEEncryptComplete(const HciLeEncryptReturnParam *returnParam)
353 {
354 LOG_INFO("%{public}s", __FUNCTION__);
355 HciLeEncryptReturnParam *ctx = MEM_MALLOC.alloc(sizeof(HciLeEncryptReturnParam));
356 if (ctx == NULL) {
357 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
358 return;
359 }
360 (void)memset_s(ctx, sizeof(HciLeEncryptReturnParam), 0x00, sizeof(HciLeEncryptReturnParam));
361 (void)memcpy_s(ctx, sizeof(HciLeEncryptReturnParam), returnParam, sizeof(HciLeEncryptReturnParam));
362 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_OnLeEncCompTask, (void *)ctx);
363 if (ret != SMP_SUCCESS) {
364 MEM_MALLOC.free(ctx);
365 return;
366 }
367 }
368
SMP_OnLeEncCompTask(void * context)369 void SMP_OnLeEncCompTask(void *context)
370 {
371 SMP_EncCmd *pEncCmdData = NULL;
372 ListNode *listNode = NULL;
373 HciLeEncryptReturnParam *returnParam = (HciLeEncryptReturnParam *)context;
374 SMP_EncData encData = {
375 .encRetParam = returnParam,
376 .encCmd = NULL,
377 };
378 SMP_StepParam param = {.data = (void *)&encData};
379
380 LOG_DEBUG("%{public}s", __FUNCTION__);
381 listNode = SMP_ListGetFirstNode(SMP_GetEncCmdList());
382 if (listNode == NULL) {
383 LOG_ERROR("g_smpEncCmdList is NULL.");
384 MEM_MALLOC.free(returnParam);
385 return;
386 }
387 pEncCmdData = SMP_ListGetNodeData(listNode);
388 if (pEncCmdData == NULL) {
389 LOG_ERROR("pEncCmdData is NULL.");
390 MEM_MALLOC.free(returnParam);
391 return;
392 }
393 encData.encCmd = pEncCmdData;
394 AlarmCancel(pEncCmdData->timeoutTimer);
395 SMP_ExecuteStepFunc(pEncCmdData->step, ¶m);
396 SMP_ListRemoveNode(SMP_GetEncCmdList(), pEncCmdData);
397 MEM_MALLOC.free(returnParam);
398 }
399
SMP_OnLEEncryptionChange(const HciEncryptionChangeEventParam * eventParam)400 void SMP_OnLEEncryptionChange(const HciEncryptionChangeEventParam *eventParam)
401 {
402 LOG_INFO("%{public}s", __FUNCTION__);
403 HciEncryptionChangeEventParam *ctx = MEM_MALLOC.alloc(sizeof(HciEncryptionChangeEventParam));
404 if (ctx == NULL) {
405 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
406 return;
407 }
408 (void)memset_s(ctx, sizeof(HciEncryptionChangeEventParam), 0x00, sizeof(HciEncryptionChangeEventParam));
409 (void)memcpy_s(ctx, sizeof(HciEncryptionChangeEventParam), eventParam, sizeof(HciEncryptionChangeEventParam));
410 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_OnLeEncChangeTask, (void *)ctx);
411 if (ret != SMP_SUCCESS) {
412 MEM_MALLOC.free(ctx);
413 return;
414 }
415 }
416
SMP_OnLeEncChangeTask(void * context)417 static void SMP_OnLeEncChangeTask(void *context)
418 {
419 HciEncryptionChangeEventParam *eventParam = (HciEncryptionChangeEventParam *)context;
420
421 LOG_DEBUG("%{public}s", __FUNCTION__);
422 SMP_LinkLayerEncryptionComplete(eventParam->connectionHandle, eventParam->status);
423 MEM_MALLOC.free(eventParam);
424 }
425
SMP_OnLEEncryptionKeyRefreshComplete(const HciEncryptionKeyRefreshCompleteEventParam * eventParam)426 void SMP_OnLEEncryptionKeyRefreshComplete(const HciEncryptionKeyRefreshCompleteEventParam *eventParam)
427 {
428 LOG_INFO("%{public}s", __FUNCTION__);
429 HciEncryptionKeyRefreshCompleteEventParam *ctx =
430 MEM_MALLOC.alloc(sizeof(HciEncryptionKeyRefreshCompleteEventParam));
431 if (ctx == NULL) {
432 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
433 return;
434 }
435 (void)memset_s(ctx,
436 sizeof(HciEncryptionKeyRefreshCompleteEventParam),
437 0x00,
438 sizeof(HciEncryptionKeyRefreshCompleteEventParam));
439 (void)memcpy_s(ctx,
440 sizeof(HciEncryptionKeyRefreshCompleteEventParam),
441 eventParam,
442 sizeof(HciEncryptionKeyRefreshCompleteEventParam));
443 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_OnLeEncKeyRefCompTask, (void *)ctx);
444 if (ret != SMP_SUCCESS) {
445 MEM_MALLOC.free(ctx);
446 return;
447 }
448 }
449
SMP_OnLeEncKeyRefCompTask(void * context)450 static void SMP_OnLeEncKeyRefCompTask(void *context)
451 {
452 HciEncryptionKeyRefreshCompleteEventParam *eventParam = (HciEncryptionKeyRefreshCompleteEventParam *)context;
453
454 LOG_DEBUG("%{public}s", __FUNCTION__);
455 SMP_LinkLayerEncryptionComplete(eventParam->connectionHandle, eventParam->status);
456 MEM_MALLOC.free(eventParam);
457 }
458
SMP_OnLELongTermKeyRequest(const HciLeLongTermKeyRequestEventParam * eventParam)459 void SMP_OnLELongTermKeyRequest(const HciLeLongTermKeyRequestEventParam *eventParam)
460 {
461 LOG_INFO("%{public}s", __FUNCTION__);
462 HciLeLongTermKeyRequestEventParam *ctx = MEM_MALLOC.alloc(sizeof(HciLeLongTermKeyRequestEventParam));
463 if (ctx == NULL) {
464 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
465 return;
466 }
467 (void)memset_s(ctx, sizeof(HciLeLongTermKeyRequestEventParam), 0x00, sizeof(HciLeLongTermKeyRequestEventParam));
468 (void)memcpy_s(
469 ctx, sizeof(HciLeLongTermKeyRequestEventParam), eventParam, sizeof(HciLeLongTermKeyRequestEventParam));
470 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_OnLeLongTermKeyReqTask, (void *)ctx);
471 if (ret != SMP_SUCCESS) {
472 MEM_MALLOC.free(ctx);
473 return;
474 }
475 }
476
SMP_OnLeLongTermKeyReqTask(void * context)477 static void SMP_OnLeLongTermKeyReqTask(void *context)
478 {
479 HciLeLongTermKeyRequestNegativeReplyParam negativeReplyParam;
480 HciLeLongTermKeyRequestEventParam *eventParam = (HciLeLongTermKeyRequestEventParam *)context;
481
482 LOG_DEBUG("%{public}s", __FUNCTION__);
483 if ((SMP_GetPairMng()->state == SMP_STATE_PAIRING) && (SMP_GetPairMng()->handle == eventParam->connectionHandle)) {
484 LOG_INFO("Pairing process.");
485 if (SMP_GetPairMng()->role != SMP_ROLE_SLAVE) {
486 LOG_ERROR("It's not accepter role.");
487 negativeReplyParam.connectionHandle = SMP_GetPairMng()->handle;
488 HCI_LeLongTermKeyRequestNegativeReply(&negativeReplyParam);
489 MEM_MALLOC.free(eventParam);
490 return;
491 }
492 if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_SLAVE_STEP_12) &&
493 (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_SLAVE_STEP_17)) {
494 LOG_ERROR("It's invalid step:%hu.", SMP_GetPairMng()->step);
495 negativeReplyParam.connectionHandle = SMP_GetPairMng()->handle;
496 HCI_LeLongTermKeyRequestNegativeReply(&negativeReplyParam);
497 MEM_MALLOC.free(eventParam);
498 return;
499 }
500 AlarmCancel(SMP_GetPairMng()->alarm);
501 if (SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) {
502 SMP_OnLtkReqLegacyPair();
503 } else {
504 SMP_OnLtkReqScPair();
505 }
506 } else {
507 LOG_INFO("Encryption process.");
508 SMP_OnLtkReqEncProc(eventParam);
509 }
510 MEM_MALLOC.free(eventParam);
511 }
512
SMP_OnLELongTermKeyRequestReplyComplete(const HciLeLongTermKeyRequestReplyReturnParam * returnParam)513 void SMP_OnLELongTermKeyRequestReplyComplete(const HciLeLongTermKeyRequestReplyReturnParam *returnParam)
514 {
515 LOG_INFO("%{public}s", __FUNCTION__);
516 HciLeLongTermKeyRequestReplyReturnParam *ctx = MEM_MALLOC.alloc(sizeof(HciLeLongTermKeyRequestReplyReturnParam));
517 if (ctx == NULL) {
518 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
519 return;
520 }
521 (void)memset_s(
522 ctx, sizeof(HciLeLongTermKeyRequestReplyReturnParam), 0x00, sizeof(HciLeLongTermKeyRequestReplyReturnParam));
523 (void)memcpy_s(ctx,
524 sizeof(HciLeLongTermKeyRequestReplyReturnParam),
525 returnParam,
526 sizeof(HciLeLongTermKeyRequestReplyReturnParam));
527 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_OnLeLongTermKeyReqReplyCompTask, (void *)ctx);
528 if (ret != SMP_SUCCESS) {
529 MEM_MALLOC.free(ctx);
530 return;
531 }
532 }
533
SMP_OnLeLongTermKeyReqReplyCompTask(void * context)534 static void SMP_OnLeLongTermKeyReqReplyCompTask(void *context)
535 {
536 HciLeLongTermKeyRequestReplyReturnParam *returnParam = (HciLeLongTermKeyRequestReplyReturnParam *)context;
537 LOG_DEBUG("%{public}s", __FUNCTION__);
538 if ((SMP_GetPairMng()->state == SMP_STATE_PAIRING) && (SMP_GetPairMng()->handle == returnParam->connectionHandle)) {
539 LOG_INFO("Pairing process.");
540 if (SMP_GetPairMng()->role != SMP_ROLE_SLAVE) {
541 LOG_ERROR("It's not accepter role.");
542 MEM_MALLOC.free(returnParam);
543 return;
544 }
545 if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_SLAVE_STEP_13) &&
546 (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_SLAVE_STEP_18)) {
547 LOG_ERROR("It's not SMP_LEGACY_PAIR_SLAVE_STEP_13.");
548 MEM_MALLOC.free(returnParam);
549 return;
550 }
551 if (returnParam->status) {
552 LOG_ERROR("eventParam->status = %hhu.", returnParam->status);
553 SMP_GeneratePairResult(SMP_GetPairMng()->handle,
554 SMP_PAIR_STATUS_FAILED,
555 SMP_PAIR_FAILED_UNSPECIFIED_REASION,
556 SMP_GetPairMng()->alarm);
557 }
558 } else {
559 LOG_INFO("Encryption process.");
560 if (returnParam->status) {
561 LOG_ERROR("eventParam->status = %hhu.", returnParam->status);
562 LOG_ERROR("Encryption failed.");
563 SMP_NotifyCbEncComp(returnParam->connectionHandle, SMP_ENCRYPT_STATUS_FAILED);
564 }
565 }
566 MEM_MALLOC.free(returnParam);
567 }
568
SMP_OnLEReadLocalP256PublicKeyComplete(const HciLeReadLocalP256PublicKeyCompleteEventParam * eventParam)569 void SMP_OnLEReadLocalP256PublicKeyComplete(const HciLeReadLocalP256PublicKeyCompleteEventParam *eventParam)
570 {
571 LOG_INFO("%{public}s", __FUNCTION__);
572 HciLeReadLocalP256PublicKeyCompleteEventParam *ctx =
573 MEM_MALLOC.alloc(sizeof(HciLeReadLocalP256PublicKeyCompleteEventParam));
574 if (ctx == NULL) {
575 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
576 return;
577 }
578 (void)memset_s(ctx,
579 sizeof(HciLeReadLocalP256PublicKeyCompleteEventParam),
580 0x00,
581 sizeof(HciLeReadLocalP256PublicKeyCompleteEventParam));
582 (void)memcpy_s(ctx,
583 sizeof(HciLeReadLocalP256PublicKeyCompleteEventParam),
584 eventParam,
585 sizeof(HciLeReadLocalP256PublicKeyCompleteEventParam));
586 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_OnLeReadLocalP256PubKeyCompTask, (void *)ctx);
587 if (ret != SMP_SUCCESS) {
588 MEM_MALLOC.free(ctx);
589 return;
590 }
591 }
592
SMP_OnLeReadLocalP256PubKeyCompTask(void * context)593 static void SMP_OnLeReadLocalP256PubKeyCompTask(void *context)
594 {
595 HciLeReadLocalP256PublicKeyCompleteEventParam *eventParam =
596 (HciLeReadLocalP256PublicKeyCompleteEventParam *)context;
597 SMP_StepParam param = {.data = (void *)eventParam};
598 LOG_DEBUG("%{public}s", __FUNCTION__);
599 if (SMP_GetPairMng()->state == SMP_STATE_PAIRING) {
600 LOG_INFO("It's Pairing State.");
601 AlarmCancel(SMP_GetPairMng()->alarm);
602 if (eventParam->status) {
603 LOG_ERROR("returnParam->status = %hhu.", eventParam->status);
604 SMP_GeneratePairResult(
605 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
606 } else {
607 SMP_OnReadLocalPubKeyCompStepDistribution(SMP_GetPairMng()->step, ¶m);
608 }
609 } else if (SMP_GetScOobMng()->state == SMP_STATE_SC_OOB_DATA_GENERATING) {
610 LOG_INFO("It's SC OOB data generating state.");
611 if (SMP_GetScOobMng()->step == SMP_GENERATE_SC_OOB_DATA_STEP_1) {
612 SMP_GenerateScOobDataStep1(¶m);
613 } else {
614 LOG_ERROR("It's invalid step:%hu.", SMP_GetPairMng()->step);
615 }
616 } else {
617 LOG_ERROR("It's invalid state.");
618 }
619 MEM_MALLOC.free(eventParam);
620 }
621
SMP_OnReadLocalPubKeyCompStepDistribution(uint16_t step,const SMP_StepParam * param)622 static void SMP_OnReadLocalPubKeyCompStepDistribution(uint16_t step, const SMP_StepParam *param)
623 {
624 switch (step) {
625 case SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_1:
626 SMP_ScPairJustworkOrNumericMasterStep1(param);
627 break;
628 case SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_1:
629 SMP_ScPairPasskeyEntryMasterStep1(param);
630 break;
631 case SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_2:
632 SMP_ScPairJustworkOrNumericSlaveStep2(param);
633 break;
634 case SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_2:
635 SMP_ScPairPasskeyEntrySlaveStep2(param);
636 break;
637 case SMP_SC_PAIR_OOB_MASTER_STEP_1:
638 SMP_ScPairOobMasterStep1(param);
639 break;
640 case SMP_SC_PAIR_OOB_SLAVE_STEP_1:
641 SMP_ScPairOobSlaveStep1(param);
642 break;
643 default:
644 LOG_ERROR("It's invalid step:%hu.", step);
645 SMP_GeneratePairResult(
646 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
647 break;
648 }
649 }
650
SMP_OnLEGenerateDHKeyComplete(const HciLeGenerateDHKeyCompleteEventParam * eventParam)651 void SMP_OnLEGenerateDHKeyComplete(const HciLeGenerateDHKeyCompleteEventParam *eventParam)
652 {
653 LOG_INFO("%{public}s", __FUNCTION__);
654 HciLeGenerateDHKeyCompleteEventParam *ctx = MEM_MALLOC.alloc(sizeof(HciLeGenerateDHKeyCompleteEventParam));
655 if (ctx == NULL) {
656 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
657 return;
658 }
659 (void)memset_s(
660 ctx, sizeof(HciLeGenerateDHKeyCompleteEventParam), 0x00, sizeof(HciLeGenerateDHKeyCompleteEventParam));
661 (void)memcpy_s(
662 ctx, sizeof(HciLeGenerateDHKeyCompleteEventParam), eventParam, sizeof(HciLeGenerateDHKeyCompleteEventParam));
663 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_SMP, SMP_OnLeGenDhKeyCompTask, (void *)ctx);
664 if (ret != SMP_SUCCESS) {
665 MEM_MALLOC.free(ctx);
666 return;
667 }
668 }
669
SMP_OnLeGenDhKeyCompTask(void * context)670 static void SMP_OnLeGenDhKeyCompTask(void *context)
671 {
672 HciLeGenerateDHKeyCompleteEventParam *eventParam = (HciLeGenerateDHKeyCompleteEventParam *)context;
673 SMP_StepParam param = {.data = (void *)eventParam};
674 LOG_DEBUG("%{public}s", __FUNCTION__);
675 if (SMP_GetPairMng()->state != SMP_STATE_PAIRING) {
676 LOG_ERROR("It's not pairing state.");
677 MEM_MALLOC.free(eventParam);
678 return;
679 }
680 AlarmCancel(SMP_GetPairMng()->alarm);
681 if (eventParam->status) {
682 SMP_GeneratePairResult(
683 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
684 LOG_ERROR("returnParam->status = %hhu.", eventParam->status);
685 } else {
686 SMP_OnGenDhKeyCompStepDistribution(SMP_GetPairMng()->step, ¶m);
687 }
688 MEM_MALLOC.free(eventParam);
689 }
690
SMP_OnGenDhKeyCompStepDistribution(uint16_t step,const SMP_StepParam * param)691 static void SMP_OnGenDhKeyCompStepDistribution(uint16_t step, const SMP_StepParam *param)
692 {
693 switch (step) {
694 case SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_3:
695 SMP_OnGenDHKeyCompScJwOrNumMasterStep3((const HciLeGenerateDHKeyCompleteEventParam *)param->data);
696 break;
697 case SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_4:
698 SMP_OnGenDHKeyCompScJwOrNumMasterStep4((const HciLeGenerateDHKeyCompleteEventParam *)param->data);
699 break;
700 case SMP_SC_PAIR_PASSKEYENTRY_MASTER_STEP_3:
701 SMP_ScPairPasskeyEntryMasterStep3(param);
702 break;
703 case SMP_SC_PAIR_JUSTWORKORNUMERIC_SLAVE_STEP_3:
704 SMP_ScPairJustworkOrNumericSlaveStep3(param);
705 break;
706 case SMP_SC_PAIR_PASSKEYENTRY_SLAVE_STEP_3:
707 SMP_ScPairPasskeyEntrySlaveStep3(param);
708 break;
709 case SMP_SC_PAIR_OOB_MASTER_STEP_9:
710 SMP_ScPairOobMasterStep9(param);
711 break;
712 case SMP_SC_PAIR_OOB_SLAVE_STEP_9:
713 SMP_ScPairOobSlaveStep9(param);
714 break;
715 default:
716 LOG_ERROR("It's invalid step:%hu.", step);
717 SMP_GeneratePairResult(
718 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
719 break;
720 }
721 }
722
SMP_OnGenDHKeyCompScJwOrNumMasterStep3(const HciLeGenerateDHKeyCompleteEventParam * eventParam)723 static void SMP_OnGenDHKeyCompScJwOrNumMasterStep3(const HciLeGenerateDHKeyCompleteEventParam *eventParam)
724 {
725 SMP_MemoryReverseCopy(SMP_GetPairMng()->DHKey, eventParam->DHKey, SMP_DHKEY_LEN);
726 LOG_DEBUG("SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_4 started.");
727 SMP_GetPairMng()->step = SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_4;
728 AlarmSet(SMP_GetPairMng()->alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
729 }
730
SMP_OnGenDHKeyCompScJwOrNumMasterStep4(const HciLeGenerateDHKeyCompleteEventParam * eventParam)731 static void SMP_OnGenDHKeyCompScJwOrNumMasterStep4(const HciLeGenerateDHKeyCompleteEventParam *eventParam)
732 {
733 SMP_MemoryReverseCopy(SMP_GetPairMng()->DHKey, eventParam->DHKey, SMP_DHKEY_LEN);
734 LOG_DEBUG("SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_5 started.");
735 if (SMP_SendHciLeRandCmd(SMP_SC_PAIR_JUSTWORKORNUMERIC_MASTER_STEP_5)) {
736 SMP_GeneratePairResult(
737 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
738 }
739 }
740
SMP_LinkLayerEncryptionComplete(uint16_t handle,uint8_t status)741 static void SMP_LinkLayerEncryptionComplete(uint16_t handle, uint8_t status)
742 {
743 if ((SMP_GetPairMng()->state == SMP_STATE_PAIRING) && (SMP_GetPairMng()->handle == handle)) {
744 LOG_INFO("Pairing process.");
745 if (SMP_GetPairMng()->role == SMP_ROLE_MASTER) {
746 LOG_INFO("It's initiator role.");
747 if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_MASTER_STEP_11) &&
748 (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_MASTER_STEP_17)) {
749 LOG_ERROR("It's not SMP_LEGACY_PAIR_MASTER_STEP_11.");
750 LOG_ERROR("and It's not SMP_SC_PAIR_COMMON_MASTER_STEP_17.");
751 return;
752 }
753 AlarmCancel(SMP_GetPairMng()->alarm);
754 if (status) {
755 SMP_GeneratePairResult(
756 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
757 LOG_ERROR("eventParam->status = %hhu.", status);
758 return;
759 }
760 SMP_GetPairMng()->masterEncryptedFlag = SMP_MASTER_ENCRYPTED_FLAG_YES;
761 SMP_LinkLayerEncCompMaster();
762 } else {
763 LOG_INFO("It's accepter role.");
764 if ((SMP_GetPairMng()->step != SMP_LEGACY_PAIR_SLAVE_STEP_13) &&
765 (SMP_GetPairMng()->step != SMP_SC_PAIR_COMMON_SLAVE_STEP_18)) {
766 LOG_ERROR("It's not SMP_LEGACY_PAIR_SLAVE_STEP_13.");
767 LOG_ERROR("and It's not SMP_SC_PAIR_COMMON_SLAVE_STEP_18.");
768 return;
769 }
770 AlarmCancel(SMP_GetPairMng()->alarm);
771 if (status) {
772 LOG_ERROR("eventParam->status = %hhu.", status);
773 SMP_GeneratePairResult(
774 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
775 return;
776 }
777 SMP_LinkLayerEncCompSlave();
778 }
779 }
780 }
781
SMP_LinkLayerEncCompMaster()782 static void SMP_LinkLayerEncCompMaster()
783 {
784 int ret = SMP_SUCCESS;
785 if (SMP_GetPairMng()->peer.keyDistCmdFlag != 0x00) {
786 if (SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) {
787 LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_12 started.");
788 SMP_GetPairMng()->step = SMP_LEGACY_PAIR_MASTER_STEP_12;
789 AlarmSet(SMP_GetPairMng()->alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
790 } else {
791 LOG_DEBUG("SMP_SC_PAIR_COMMON_MASTER_STEP_18 started.");
792 SMP_GetPairMng()->step = SMP_SC_PAIR_COMMON_MASTER_STEP_18;
793 AlarmSet(SMP_GetPairMng()->alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
794 }
795 return;
796 }
797 LOG_INFO("g_smpPairMng.peer.keyDistCmdFlag is 0.");
798 if ((SMP_GetPairMng()->pairType) == SMP_PAIR_TYPE_LEGACY &&
799 (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ENC_KEY)) {
800 LOG_INFO("Local need to send ENC_KEY.");
801 LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_13 Started.");
802 ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_13);
803 } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_SIGN_KEY) {
804 LOG_INFO("Local Need to send SIGN_KEY.");
805 if (SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) {
806 LOG_DEBUG("SMP_LEGACY_PAIR_MASTER_STEP_17 Started.");
807 ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_MASTER_STEP_17);
808 } else {
809 LOG_DEBUG("SMP_SC_PAIR_COMMON_MASTER_STEP_19 Started.");
810 ret = SMP_SendHciLeRandCmd(SMP_SC_PAIR_COMMON_MASTER_STEP_19);
811 }
812 } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ID_KEY) {
813 ret = SMP_SendDistributionKeysToRemote();
814 LOG_INFO("Send Keys To Remote");
815 } else {
816 SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
817 return;
818 }
819 if (ret != SMP_SUCCESS) {
820 LOG_INFO("Pair Failed.");
821 SMP_GeneratePairResult(
822 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
823 }
824 }
825
SMP_LinkLayerEncCompSlave()826 static void SMP_LinkLayerEncCompSlave()
827 {
828 int ret = SMP_SUCCESS;
829 if ((SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) &&
830 (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ENC_KEY)) {
831 ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_SLAVE_STEP_14);
832 } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_SIGN_KEY) {
833 if (SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) {
834 ret = SMP_SendHciLeRandCmd(SMP_LEGACY_PAIR_SLAVE_STEP_18);
835 } else {
836 ret = SMP_SendHciLeRandCmd(SMP_SC_PAIR_COMMON_SLAVE_STEP_19);
837 }
838 } else if (SMP_GetPairMng()->local.keyDist & SMP_KEY_DIST_BIT_ID_KEY) {
839 if (SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) {
840 SMP_GetPairMng()->step = SMP_LEGACY_PAIR_SLAVE_STEP_20;
841 } else {
842 SMP_GetPairMng()->step = SMP_SC_PAIR_COMMON_SLAVE_STEP_21;
843 }
844 AlarmSet(SMP_GetPairMng()->alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
845 ret = SMP_SendDistributionKeysToRemote();
846 if (ret != SMP_SUCCESS) {
847 AlarmCancel(SMP_GetPairMng()->alarm);
848 }
849 } else {
850 if (SMP_GetPairMng()->peer.keyDist == 0x00) {
851 LOG_INFO("g_smpPairMng.peer.keyDist is 0.");
852 SMP_GeneratePairResult(SMP_GetPairMng()->handle, SMP_PAIR_STATUS_SUCCESS, 0x00, NULL);
853 return;
854 } else {
855 if (SMP_GetPairMng()->pairType == SMP_PAIR_TYPE_LEGACY) {
856 SMP_GetPairMng()->step = SMP_LEGACY_PAIR_SLAVE_STEP_20;
857 } else {
858 SMP_GetPairMng()->step = SMP_SC_PAIR_COMMON_SLAVE_STEP_21;
859 }
860 AlarmSet(SMP_GetPairMng()->alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
861 }
862 }
863 if (ret != SMP_SUCCESS) {
864 SMP_GeneratePairResult(
865 SMP_GetPairMng()->handle, SMP_PAIR_STATUS_FAILED, SMP_PAIR_FAILED_UNSPECIFIED_REASION, NULL);
866 }
867 }
868
SMP_OnLtkReqLegacyPair()869 static void SMP_OnLtkReqLegacyPair()
870 {
871 HciLeLongTermKeyRequestReplyParam replycmdParam;
872
873 replycmdParam.connectionHandle = SMP_GetPairMng()->handle;
874 (void)memcpy_s(replycmdParam.longTermKey, SMP_LTK_LEN, SMP_GetPairMng()->STK, SMP_STK_LEN);
875 SMP_GetPairMng()->step = SMP_LEGACY_PAIR_SLAVE_STEP_13;
876 AlarmSet(SMP_GetPairMng()->alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
877 int ret = HCI_LeLongTermKeyRequestReply(&replycmdParam);
878 if (ret != SMP_SUCCESS) {
879 LOG_ERROR("HCI_LeLongTermKeyRequestReply failed.");
880 SMP_GeneratePairResult(SMP_GetPairMng()->handle,
881 SMP_PAIR_STATUS_FAILED,
882 SMP_PAIR_FAILED_UNSPECIFIED_REASION,
883 SMP_GetPairMng()->alarm);
884 }
885 }
886
SMP_OnLtkReqScPair()887 static void SMP_OnLtkReqScPair()
888 {
889 HciLeLongTermKeyRequestReplyParam replycmdParam;
890
891 replycmdParam.connectionHandle = SMP_GetPairMng()->handle;
892 (void)memcpy_s(replycmdParam.longTermKey, SMP_LTK_LEN, SMP_GetPairMng()->local.LTK, SMP_LTK_LEN);
893 SMP_GetPairMng()->step = SMP_SC_PAIR_COMMON_SLAVE_STEP_18;
894 AlarmSet(SMP_GetPairMng()->alarm, SMP_PAIR_WAIT_TIME, SMP_PairTimeout, NULL);
895 int ret = HCI_LeLongTermKeyRequestReply(&replycmdParam);
896 if (ret != SMP_SUCCESS) {
897 LOG_ERROR("HCI_LeLongTermKeyRequestReply failed.");
898 SMP_GeneratePairResult(SMP_GetPairMng()->handle,
899 SMP_PAIR_STATUS_FAILED,
900 SMP_PAIR_FAILED_UNSPECIFIED_REASION,
901 SMP_GetPairMng()->alarm);
902 }
903 }
904
SMP_OnLtkReqEncProc(const HciLeLongTermKeyRequestEventParam * eventParam)905 static void SMP_OnLtkReqEncProc(const HciLeLongTermKeyRequestEventParam *eventParam)
906 {
907 uint16_t ediv;
908 uint8_t edivTemp[sizeof(ediv)] = {0x00};
909 uint8_t randomTemp[SMP_MASTER_RAND_LEN] = {0x00};
910
911 edivTemp[0x00] = ((uint8_t)((eventParam->encryptedDiversifier) & 0xFF));
912 edivTemp[0x01] = ((uint8_t)(((eventParam->encryptedDiversifier) >> 0x08) & 0xFF));
913 ediv = *((uint16_t *)edivTemp);
914 SMP_MemoryReverseCopy(randomTemp, eventParam->randomNumber, SMP_MASTER_RAND_LEN);
915 SMP_NotifyCbLtkReq(eventParam->connectionHandle, randomTemp, ediv);
916 }