1 /*
2 * Copyright (C) 2021-2022 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 "gap.h"
17 #include "gap_internal.h"
18 #include "gap_task_internal.h"
19
20 #include <securec.h>
21
22 #include "allocator.h"
23 #include "log.h"
24 #include "thread.h"
25
26 #include "btm.h"
27 #include "btm/btm_interop.h"
28 #include "btm/btm_thread.h"
29
30 typedef struct {
31 GapSecurityCallback callback;
32 void *context;
33 } SecurityCallback;
34
35 typedef struct {
36 GapAuthenticationCallback callback;
37 void *context;
38 } AuthenticationCallback;
39
40 static SecurityCallback g_securityCallback;
41 static AuthenticationCallback g_authenticationCallback;
42
43 static int GapAuthenticationRequested(uint16_t handle);
44 static int GapSetConnectionEncryption(uint16_t handle, uint8_t encryptionEnable);
45
GapCompareServiceSecurityInfo(const GapServiceSecurityInfo * info1,const GapServiceSecurityInfo * info2)46 static bool GapCompareServiceSecurityInfo(const GapServiceSecurityInfo *info1, const GapServiceSecurityInfo *info2)
47 {
48 return ((info1->direction == info2->direction) && (info1->protocolId == info2->protocolId) &&
49 (GapCompareChannelID(info2->protocolId, info1->channelId, info2->channelId)) &&
50 ((info1->serviceId == info2->serviceId) || (info1->serviceId == UNKNOWN_SERVICE) ||
51 (info2->serviceId == UNKNOWN_SERVICE)));
52 }
53
GapAllocReqSecInfo(const BtAddr * addr,GapSecurityResultCallback callback,void * context,const ProfileSecurityInfo * info)54 static RequestSecInfo *GapAllocReqSecInfo(
55 const BtAddr *addr, GapSecurityResultCallback callback, void *context, const ProfileSecurityInfo *info)
56 {
57 RequestSecInfo *reqInfo = NULL;
58
59 reqInfo = MEM_MALLOC.alloc(sizeof(RequestSecInfo));
60 if (reqInfo == NULL) {
61 LOG_ERROR("%{public}s:malloc error.", __FUNCTION__);
62 } else {
63 (void)memset_s(reqInfo, sizeof(RequestSecInfo), 0x00, sizeof(RequestSecInfo));
64 reqInfo->addr = *addr;
65 reqInfo->callback = callback;
66 reqInfo->info = info->info;
67 reqInfo->context = context;
68 reqInfo->status = GAP_SEC_REQ_STATUS_WAIT_ACTION;
69 if (info->info.direction == OUTGOING) {
70 reqInfo->needAuthentication = !!(info->securityMode & GAP_SEC_OUT_AUTHENTICATION);
71 reqInfo->needEncryption = !!(info->securityMode & GAP_SEC_OUT_ENCRYPTION);
72 reqInfo->needAuthorization = false;
73 if (*GapGetSecurityMode() != SEC_MODE_2) {
74 reqInfo->needEncryption = true;
75 }
76 } else if (info->info.direction == INCOMING) {
77 reqInfo->needAuthentication = !!(info->securityMode & GAP_SEC_IN_AUTHENTICATION);
78 reqInfo->needEncryption = !!(info->securityMode & GAP_SEC_IN_ENCRYPTION);
79 reqInfo->needAuthorization = !!(info->securityMode & GAP_SEC_IN_AUTHORIZATION);
80 }
81 reqInfo->needUnauthentication = reqInfo->needEncryption && !reqInfo->needAuthentication;
82 reqInfo->needMITM = !!(info->securityMode & GAP_SEC_MITM);
83 }
84
85 return reqInfo;
86 }
87
GapDoAuthenticationCallback(const void * req)88 NO_SANITIZE("cfi") void GapDoAuthenticationCallback(const void *req)
89 {
90 const RequestSecInfo *reqInfo = req;
91 BtAddr addr = BT_ADDR_NULL;
92 uint8_t result = GAP_STATUS_FAILED;
93 if (reqInfo != NULL && reqInfo->status == GAP_SEC_REQ_STATUS_SUCCESS) {
94 result = GAP_STATUS_SUCCESS;
95 }
96 if (reqInfo != NULL) {
97 addr = reqInfo->addr;
98 }
99
100 if (g_authenticationCallback.callback.authenticationComplete) {
101 g_authenticationCallback.callback.authenticationComplete(&addr, result, g_authenticationCallback.context);
102 }
103 }
104
GapTrySecurityCallback(RequestSecInfo * reqInfo)105 static void GapTrySecurityCallback(RequestSecInfo *reqInfo)
106 {
107 if (!reqInfo->doCallback) {
108 reqInfo->doCallback = true;
109 GapDoSecurityCallback(reqInfo);
110 }
111 }
112
GapDoSecurityCallback(void * req)113 NO_SANITIZE("cfi") void GapDoSecurityCallback(void *req)
114 {
115 RequestSecInfo *reqInfo = (RequestSecInfo *)req;
116 uint16_t result = GAP_STATUS_FAILED;
117
118 if (GapIsBredrEnable() == false) {
119 return;
120 }
121
122 if (reqInfo->status == GAP_SEC_REQ_STATUS_SUCCESS) {
123 result = GAP_STATUS_SUCCESS;
124 } else if (reqInfo->hciStatus != HCI_SUCCESS) {
125 result = reqInfo->hciStatus;
126 }
127
128 LOG_INFO("%{public}s:%{public}s-%02d-%{public}s result:%02x:",
129 __FUNCTION__,
130 reqInfo->info.direction ? "IN" : "OUT",
131 reqInfo->info.serviceId,
132 reqInfo->info.protocolId ? "RFCOMM" : "L2CAP",
133 result);
134
135 if (reqInfo->callback) {
136 reqInfo->callback(result, reqInfo->info, reqInfo->context);
137 }
138
139 ProfileSecurityBlock *profileSecurityBlock = GapGetProfileSecurityBlock();
140 ListRemoveNode(profileSecurityBlock->requestlist, req);
141 }
142
GapDoAuthorizationCallback(const void * req)143 static void GapDoAuthorizationCallback(const void *req)
144 {
145 const RequestSecInfo *reqInfo = req;
146 bool reject = false;
147
148 if (g_securityCallback.callback.authorizeInd) {
149 g_securityCallback.callback.authorizeInd(&reqInfo->addr, reqInfo->info.serviceId, reqInfo->context);
150 } else {
151 reject = true;
152 }
153
154 if (reject) {
155 GAP_AuthorizeRes(&reqInfo->addr, reqInfo->info.serviceId, GAP_NOT_ACCEPT);
156 }
157 }
158
GapDoSecurityAction(RequestSecInfo * reqInfo,DeviceInfo * devInfo)159 static void GapDoSecurityAction(RequestSecInfo *reqInfo, DeviceInfo *devInfo)
160 {
161 int ret;
162
163 if (devInfo->status != GAP_DEV_SEC_STATUS_ACTION) {
164 if (devInfo->status == GAP_DEV_SEC_STATUS_IDLE) {
165 BTM_AclAddRef(devInfo->handle);
166 } else {
167 AlarmCancel(devInfo->alarm);
168 }
169 devInfo->status = GAP_DEV_SEC_STATUS_ACTION;
170 }
171 if (reqInfo->needAuthorization) {
172 GapDoAuthorizationCallback(reqInfo);
173 } else if (reqInfo->needAuthentication || reqInfo->needUnauthentication) {
174 if (devInfo->authenticationStatus == GAP_AUTH_STATUS_IDLE) {
175 devInfo->authenticationStatus = GAP_AUTH_STATUS_ACTION;
176 }
177 ret = GapAuthenticationRequested(devInfo->handle);
178 if (ret != BT_NO_ERROR) {
179 reqInfo->status = GAP_SEC_REQ_STATUS_FAILED;
180 devInfo->status = GAP_DEV_SEC_STATUS_IDLE;
181 devInfo->authenticationStatus = GAP_AUTH_STATUS_IDLE;
182 GapDoAuthenticationCallback(reqInfo);
183 GapTrySecurityCallback(reqInfo);
184 }
185 } else if (reqInfo->needEncryption) {
186 if (devInfo->encryptionStatus == GAP_ENC_STATUS_IDLE) {
187 devInfo->encryptionStatus = GAP_ENC_STATUS_ACTION;
188 }
189 ret = GapSetConnectionEncryption(devInfo->handle, LINK_LEVEL_ENCRYPTION_ON);
190 if (ret != BT_NO_ERROR) {
191 reqInfo->status = GAP_SEC_REQ_STATUS_FAILED;
192 devInfo->status = GAP_DEV_SEC_STATUS_IDLE;
193 devInfo->encryptionStatus = GAP_ENC_STATUS_IDLE;
194 GapTrySecurityCallback(reqInfo);
195 }
196 } else {
197 devInfo->status = GAP_DEV_SEC_STATUS_IDLE;
198 }
199 }
200
GapDoReleaseACLConnection(const void * dev)201 static void GapDoReleaseACLConnection(const void *dev)
202 {
203 const DeviceInfo *devInfo = dev;
204
205 ListNode *node = ListGetFirstNode(GapGetConnectionInfoBlock()->devicelist);
206 while (node != NULL) {
207 if (dev == ListGetNodeData(node)) {
208 break;
209 }
210 node = ListGetNextNode(node);
211 }
212
213 if (node != NULL) {
214 BTM_AclRelease(devInfo->handle);
215 }
216 }
217
GapDiscACLTimerTimeoutTask(void * ctx)218 static void GapDiscACLTimerTimeoutTask(void *ctx)
219 {
220 const DeviceInfo *dev = ((const GapGeneralPointerInfo *)ctx)->pointer;
221
222 if (GapIsBredrEnable()) {
223 GapDoReleaseACLConnection(dev);
224 }
225 }
226
GapDiscACLTimerTimeout(void * dev)227 static void GapDiscACLTimerTimeout(void *dev)
228 {
229 LOG_INFO("%{public}s: ", __FUNCTION__);
230 GapGeneralPointerInfo *ctx = MEM_MALLOC.alloc(sizeof(GapGeneralPointerInfo));
231 if (ctx == NULL) {
232 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
233 return;
234 }
235
236 ctx->pointer = dev;
237
238 int ret = GapRunTaskUnBlockProcess(GapDiscACLTimerTimeoutTask, ctx, NULL);
239 if (ret != BT_NO_ERROR) {
240 LOG_ERROR("%{public}s: Task error:%{public}d.", __FUNCTION__, ret);
241 }
242 }
243
GapCheckConnection(void)244 static void GapCheckConnection(void)
245 {
246 ListNode *node = NULL;
247 ConnectionInfoBlock *connectionInfoBlock = NULL;
248 DeviceInfo *devInfo = NULL;
249 connectionInfoBlock = GapGetConnectionInfoBlock();
250 node = ListGetFirstNode(connectionInfoBlock->devicelist);
251 while (node != 0) {
252 devInfo = ListGetNodeData(node);
253 if (devInfo->status == GAP_DEV_SEC_STATUS_WAIT_DISC) {
254 LOG_DEBUG("%{public}s: " BT_ADDR_FMT " 4s disconnect ACL", __FUNCTION__, BT_ADDR_FMT_OUTPUT(devInfo->addr.addr));
255 AlarmCancel(devInfo->alarm);
256 AlarmSet(devInfo->alarm, GAP_DISC_ACL_WAIT_TIME, GapDiscACLTimerTimeout, devInfo);
257 devInfo->status = GAP_DEV_SEC_STATUS_IDLE;
258 }
259 node = ListGetNextNode(node);
260 }
261 }
262
GapRequestSecurityProcess(void)263 void GapRequestSecurityProcess(void)
264 {
265 ListNode *requestNode = ListGetFirstNode(GapGetProfileSecurityBlock()->requestlist);
266 while (requestNode != 0) {
267 RequestSecInfo *reqInfo = ListGetNodeData(requestNode);
268 requestNode = ListGetNextNode(requestNode);
269
270 if (reqInfo->status == GAP_SEC_REQ_STATUS_FAILED) {
271 GapTrySecurityCallback(reqInfo);
272 continue;
273 }
274
275 if (reqInfo->status == GAP_SEC_REQ_STATUS_WAIT_CONNECT || reqInfo->status == GAP_SEC_REQ_STATUS_WAIT_FEATURE ||
276 reqInfo->status == GAP_SEC_REQ_STATUS_WAIT_ENCRYPT) {
277 continue;
278 }
279
280 DeviceInfo *devInfo =
281 ListForEachData(GapGetConnectionInfoBlock()->devicelist, GapFindConnectionDeviceByAddr, &reqInfo->addr);
282 if (devInfo == NULL) {
283 continue;
284 }
285
286 if (reqInfo->info.serviceId == GAP) {
287 devInfo->inDedicatedBonding = true;
288 } else {
289 if (devInfo->isAuthentication) {
290 reqInfo->needAuthentication = false;
291 reqInfo->needUnauthentication = false;
292 }
293 if (devInfo->isEncryption) {
294 reqInfo->needEncryption = false;
295 }
296 }
297
298 if (reqInfo->needAuthorization) {
299 reqInfo->status = GAP_SEC_REQ_STATUS_ACTION;
300 GapDoSecurityAction(reqInfo, devInfo);
301 } else if (reqInfo->needAuthentication || reqInfo->needUnauthentication || reqInfo->needEncryption) {
302 if (devInfo->actionReq != NULL) {
303 continue;
304 } else {
305 reqInfo->status = GAP_SEC_REQ_STATUS_ACTION;
306 devInfo->actionReq = reqInfo;
307 GapDoSecurityAction(reqInfo, devInfo);
308 }
309 } else {
310 reqInfo->status = GAP_SEC_REQ_STATUS_SUCCESS;
311 GapTrySecurityCallback(reqInfo);
312 }
313 }
314
315 GapCheckConnection();
316 }
317
GapDoDiscACLConnection(void * dev)318 static void GapDoDiscACLConnection(void *dev)
319 {
320 DeviceInfo *deviceInfo = dev;
321
322 ListNode *node = ListGetFirstNode(GapGetConnectionInfoBlock()->devicelist);
323 while (node != NULL) {
324 if (deviceInfo == ListGetNodeData(node)) {
325 break;
326 }
327 node = ListGetNextNode(node);
328 }
329
330 if (node != NULL) {
331 BTM_AclDisconnect(deviceInfo->handle, GAP_HCI_DISC_REASON_AUTH_FAIL);
332 }
333 }
334
GapWaitEncryptTimeoutTask(void * ctx)335 static void GapWaitEncryptTimeoutTask(void *ctx)
336 {
337 DeviceInfo *dev = ((GapGeneralPointerInfo *)ctx)->pointer;
338
339 GapDoDiscACLConnection(dev);
340 GapUpdateSecurityRequest(dev, GAP_SEC_EVENT_WAIT_ENC_TIMEOUT, HCI_SUCCESS);
341 GapRequestSecurityProcess();
342 }
343
GapWaitEncryptTimeout(void * dev)344 static void GapWaitEncryptTimeout(void *dev)
345 {
346 LOG_INFO("%{public}s: ", __FUNCTION__);
347 GapGeneralPointerInfo *ctx = MEM_MALLOC.alloc(sizeof(GapGeneralPointerInfo));
348 if (ctx == NULL) {
349 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
350 return;
351 }
352
353 ctx->pointer = dev;
354
355 int ret = GapRunTaskUnBlockProcess(GapWaitEncryptTimeoutTask, ctx, NULL);
356 if (ret != BT_NO_ERROR) {
357 LOG_ERROR("%{public}s: Task error:%{public}d.", __FUNCTION__, ret);
358 }
359 }
360
GapRemoteDeviceSupportHostSecureSimplePairingCallback(const BtAddr * addr,bool support)361 void GapRemoteDeviceSupportHostSecureSimplePairingCallback(const BtAddr *addr, bool support)
362 {
363 DeviceInfo *devInfo =
364 ListForEachData(GapGetConnectionInfoBlock()->devicelist, GapFindConnectionDeviceByAddr, (void *)addr);
365 if (devInfo != NULL) {
366 devInfo->supportSSP = support;
367 GapUpdateSecurityRequest(devInfo, GAP_SEC_EVENT_FEATURE_COMP, HCI_SUCCESS);
368 if (devInfo->waitEncryptAlarm != NULL) {
369 AlarmSet(devInfo->waitEncryptAlarm, GAP_SEC_WAIT_ENCRYPT_TIME, GapWaitEncryptTimeout, devInfo);
370 }
371 GapRequestSecurityProcess();
372 }
373 }
374
GAP_RequestSecurity(const BtAddr * addr,const GapRequestSecurityParam * param)375 int GAP_RequestSecurity(const BtAddr *addr, const GapRequestSecurityParam *param)
376 {
377 int ret = GAP_SUCCESS;
378 ProfileSecurityInfo *regInfo = NULL;
379
380 LOG_INFO("%{public}s:%{public}s-%02d-%{public}s",
381 __FUNCTION__,
382 param->info.direction ? "IN" : "OUT",
383 param->info.serviceId,
384 param->info.protocolId ? "RFCOMM" : "L2CAP");
385
386 do {
387 ListNode *node = ListGetFirstNode(GapGetProfileSecurityBlock()->registerlist);
388 while (node != 0) {
389 regInfo = ListGetNodeData(node);
390 if ((GapIsEmptyAddr(regInfo->addr.addr) || GapAddrCompare(®Info->addr, addr)) &&
391 GapCompareServiceSecurityInfo(®Info->info, ¶m->info)) {
392 break;
393 }
394 node = ListGetNextNode(node);
395 }
396
397 if (node == NULL) {
398 LOG_ERROR("%{public}s:Register(%{public}d, %{public}d)not found.", __FUNCTION__, param->info.serviceId, param->info.direction);
399 ret = GAP_ERR_INVAL_PARAM;
400 break;
401 }
402
403 RequestSecInfo *reqInfo = GapAllocReqSecInfo(addr, param->callback, param->context, regInfo);
404 if (reqInfo == NULL) {
405 LOG_ERROR("%{public}s:reqInfo alloc failed.", __FUNCTION__);
406 ret = GAP_ERR_OUT_OF_RES;
407 break;
408 }
409 ListAddLast(GapGetProfileSecurityBlock()->requestlist, reqInfo);
410
411 DeviceInfo *deviceInfo =
412 ListForEachData(GapGetConnectionInfoBlock()->devicelist, GapFindConnectionDeviceByAddr, (void *)addr);
413 if (deviceInfo != NULL) {
414 if (*GapGetSecurityMode() != SEC_MODE_2) {
415 GapIsRemoteDeviceSupportHostSecureSimplePairingAsync(addr);
416 reqInfo->status = GAP_SEC_REQ_STATUS_WAIT_FEATURE;
417 }
418 GapRequestSecurityProcess();
419 } else {
420 ret = BTM_AclConnect(addr);
421 reqInfo->status = GAP_SEC_REQ_STATUS_WAIT_CONNECT;
422 }
423 } while (0);
424
425 if (ret != BT_NO_ERROR) {
426 param->callback(GAP_STATUS_FAILED, param->info, param->context);
427 ret = BT_NO_ERROR;
428 }
429
430 return ret;
431 }
432
GapWriteSimplePairingMode(uint8_t simplePairingMode)433 static int GapWriteSimplePairingMode(uint8_t simplePairingMode)
434 {
435 int ret;
436
437 if (BTM_IsControllerSupportSecureSimplePairing()) {
438 HciWriteSimplePairingModeParam hciCmdParam = {
439 .simplePairingMode = simplePairingMode,
440 };
441 ret = HCI_WriteSimplePairingMode(&hciCmdParam);
442 } else {
443 ret = GAP_ERR_NOT_SUPPORT;
444 }
445
446 return ret;
447 }
448
GapWriteSecureConnectionsHostSupport(uint8_t support)449 static int GapWriteSecureConnectionsHostSupport(uint8_t support)
450 {
451 int ret;
452 if (BTM_IsControllerSupportSecureConnections()) {
453 HciWriteSecureConnectionsHostSupportParam hciCmdParam = {
454 .secureConnectionsHostSupport = support,
455 };
456
457 ret = HCI_WriteSecureConnectionsHostSupport(&hciCmdParam);
458 } else {
459 ret = GAP_SUCCESS;
460 }
461
462 return ret;
463 }
464
GAP_RegisterSecurityCallback(const GapSecurityCallback * callback,void * context)465 int GAP_RegisterSecurityCallback(const GapSecurityCallback *callback, void *context)
466 {
467 LOG_INFO("%{public}s:%{public}s", __FUNCTION__, callback ? "register" : "NULL");
468 if (callback == NULL) {
469 (void)memset_s(&g_securityCallback.callback,
470 sizeof(g_securityCallback.callback),
471 0x00,
472 sizeof(g_securityCallback.callback));
473 } else {
474 g_securityCallback.callback = *callback;
475 }
476 g_securityCallback.context = context;
477 return GAP_SUCCESS;
478 }
479
GAP_DeregisterSecurityCallback(void)480 int GAP_DeregisterSecurityCallback(void)
481 {
482 (void)memset_s(
483 &g_securityCallback.callback, sizeof(g_securityCallback.callback), 0x00, sizeof(g_securityCallback.callback));
484 g_securityCallback.context = NULL;
485 return GAP_SUCCESS;
486 }
487
GAP_SetSecurityMode(GAP_SecurityMode mode)488 int GAP_SetSecurityMode(GAP_SecurityMode mode)
489 {
490 LOG_INFO("%{public}s:mode[%{public}d]", __FUNCTION__, mode);
491
492 if (mode != SEC_MODE_2 && mode != SEC_MODE_4) {
493 return GAP_ERR_INVAL_PARAM;
494 }
495
496 if (GapIsBredrEnable() == false) {
497 return GAP_ERR_NOT_ENABLE;
498 }
499
500 int ret;
501 if (mode == SEC_MODE_4) {
502 ret = GapWriteSimplePairingMode(SIMPLE_PAIRING_ENABLED);
503 if (ret == BT_NO_ERROR) {
504 ret = GapWriteSecureConnectionsHostSupport(SUPPORT_SECURE_CONNECTIONS);
505 }
506 } else {
507 ret = GapWriteSimplePairingMode(SIMPLE_PAIRING_DISABLED);
508 ret |= GapWriteSecureConnectionsHostSupport(UNSUPPORT_SECURE_CONNECTIONS);
509 }
510
511 if (ret == BT_NO_ERROR) {
512 GAP_SecurityMode *secMode = GapGetSecurityMode();
513 *secMode = mode;
514 }
515
516 return GAP_SUCCESS;
517 }
518
GAP_AuthorizeRes(const BtAddr * addr,GAP_Service service,uint8_t accept)519 int GAP_AuthorizeRes(const BtAddr *addr, GAP_Service service, uint8_t accept)
520 {
521 int ret = GAP_SUCCESS;
522 ProfileSecurityBlock *profileSecurityBlock = NULL;
523 ConnectionInfoBlock *connectionInfoBlock = NULL;
524 ListNode *node = NULL;
525 RequestSecInfo *reqInfo = NULL;
526 DeviceInfo *devInfo = NULL;
527
528 LOG_INFO("%{public}s:" BT_ADDR_FMT " %{public}d, accept[%hhu]", __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr->addr), service, accept);
529
530 if (GapIsBredrEnable() == false) {
531 return GAP_ERR_NOT_ENABLE;
532 }
533
534 profileSecurityBlock = GapGetProfileSecurityBlock();
535 node = ListGetFirstNode(profileSecurityBlock->requestlist);
536 while (node != 0) {
537 reqInfo = ListGetNodeData(node);
538 if (reqInfo->info.direction == OUTGOING) {
539 node = ListGetNextNode(node);
540 continue;
541 }
542 if (reqInfo->info.serviceId == service && reqInfo->status == GAP_SEC_REQ_STATUS_ACTION) {
543 reqInfo->needAuthorization = false;
544 break;
545 } else {
546 ret = GAP_ERR_INVAL_STATE;
547 }
548 node = ListGetNextNode(node);
549 }
550
551 connectionInfoBlock = GapGetConnectionInfoBlock();
552 devInfo = ListForEachData(connectionInfoBlock->devicelist, GapFindConnectionDeviceByAddr, (void *)addr);
553 if (devInfo != NULL) {
554 if (devInfo->status == GAP_DEV_SEC_STATUS_ACTION) {
555 devInfo->status = GAP_DEV_SEC_STATUS_WAIT_DISC;
556 }
557 }
558
559 GapRequestSecurityProcess();
560 return ret;
561 }
562
GapDeviceLinkkeyIsAuthenticated(const DeviceInfo * deviceInfo)563 static bool GapDeviceLinkkeyIsAuthenticated(const DeviceInfo *deviceInfo)
564 {
565 if (deviceInfo->linkkeyType == AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192 ||
566 deviceInfo->linkkeyType == AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256 ||
567 deviceInfo->linkkeyType == COMBINATION_KEY) {
568 return true;
569 } else {
570 return false;
571 }
572 }
573
GapUpdateSecurityStatusUnauthenticationSuccess(const DeviceInfo * devInfo)574 static void GapUpdateSecurityStatusUnauthenticationSuccess(const DeviceInfo *devInfo)
575 {
576 if (devInfo->actionReq != NULL) {
577 if (devInfo->actionReq->needAuthentication) {
578 devInfo->actionReq->needAuthentication = false;
579 if (devInfo->actionReq->needMITM) {
580 devInfo->actionReq->status = GAP_SEC_REQ_STATUS_FAILED;
581 } else {
582 devInfo->actionReq->status = GAP_SEC_REQ_STATUS_SUCCESS;
583 }
584 }
585 if (devInfo->actionReq->needUnauthentication) {
586 devInfo->actionReq->needUnauthentication = false;
587 }
588 }
589 }
590
GapUpdateSecurityStatusAuthenticationSuccess(const DeviceInfo * devInfo)591 static void GapUpdateSecurityStatusAuthenticationSuccess(const DeviceInfo *devInfo)
592 {
593 ListNode *node = NULL;
594 ProfileSecurityBlock *profileSecurityBlock = GapGetProfileSecurityBlock();
595 RequestSecInfo *reqInfo = NULL;
596
597 node = ListGetFirstNode(profileSecurityBlock->requestlist);
598 while (node != 0) {
599 reqInfo = ListGetNodeData(node);
600 if (GapAddrCompare(&devInfo->addr, &reqInfo->addr) && reqInfo->needAuthentication) {
601 reqInfo->needAuthentication = false;
602 }
603 node = ListGetNextNode(node);
604 }
605 }
606
GapUpdateSecurityStatusEncryptionSuccess(const DeviceInfo * devInfo)607 static void GapUpdateSecurityStatusEncryptionSuccess(const DeviceInfo *devInfo)
608 {
609 ListNode *node = NULL;
610 ProfileSecurityBlock *profileSecurityBlock = GapGetProfileSecurityBlock();
611 RequestSecInfo *reqInfo = NULL;
612
613 node = ListGetFirstNode(profileSecurityBlock->requestlist);
614 while (node != 0) {
615 reqInfo = ListGetNodeData(node);
616 if (GapAddrCompare(&devInfo->addr, &reqInfo->addr) && reqInfo->needEncryption) {
617 reqInfo->needEncryption = false;
618 }
619 if (GapAddrCompare(&devInfo->addr, &reqInfo->addr) && reqInfo->status == GAP_SEC_REQ_STATUS_WAIT_ENCRYPT) {
620 reqInfo->status = GAP_SEC_REQ_STATUS_WAIT_ACTION;
621 }
622 node = ListGetNextNode(node);
623 }
624 }
625
GapUpdateSecurityStatusFailed(const DeviceInfo * devInfo,uint8_t hciStatus)626 static void GapUpdateSecurityStatusFailed(const DeviceInfo *devInfo, uint8_t hciStatus)
627 {
628 ListNode *node = NULL;
629 ProfileSecurityBlock *profileSecurityBlock = GapGetProfileSecurityBlock();
630 RequestSecInfo *reqInfo = NULL;
631
632 node = ListGetFirstNode(profileSecurityBlock->requestlist);
633 while (node != 0) {
634 reqInfo = ListGetNodeData(node);
635 if (GapAddrCompare(&devInfo->addr, &reqInfo->addr) && reqInfo->status == GAP_SEC_REQ_STATUS_ACTION) {
636 reqInfo->status = GAP_SEC_REQ_STATUS_FAILED;
637 reqInfo->hciStatus = hciStatus;
638 }
639 if (GapAddrCompare(&devInfo->addr, &reqInfo->addr) && reqInfo->status == GAP_SEC_REQ_STATUS_WAIT_ENCRYPT) {
640 reqInfo->status = GAP_SEC_REQ_STATUS_FAILED;
641 reqInfo->hciStatus = hciStatus;
642 }
643 node = ListGetNextNode(node);
644 }
645 }
646
GapUpdateSecurityStatusEncryptionTimeout(const DeviceInfo * devInfo)647 static void GapUpdateSecurityStatusEncryptionTimeout(const DeviceInfo *devInfo)
648 {
649 ListNode *node = NULL;
650 ProfileSecurityBlock *profileSecurityBlock = GapGetProfileSecurityBlock();
651 RequestSecInfo *reqInfo = NULL;
652
653 node = ListGetFirstNode(profileSecurityBlock->requestlist);
654 while (node != 0) {
655 reqInfo = ListGetNodeData(node);
656 if (GapAddrCompare(&devInfo->addr, &reqInfo->addr) && reqInfo->status == GAP_SEC_REQ_STATUS_WAIT_ENCRYPT) {
657 reqInfo->status = GAP_SEC_REQ_STATUS_FAILED;
658 }
659 node = ListGetNextNode(node);
660 }
661 }
662
GapUpdateSecurityStatusAclDisconnect(const DeviceInfo * devInfo,uint8_t hciStatus)663 static void GapUpdateSecurityStatusAclDisconnect(const DeviceInfo *devInfo, uint8_t hciStatus)
664 {
665 ListNode *node = NULL;
666 ProfileSecurityBlock *profileSecurityBlock = GapGetProfileSecurityBlock();
667 RequestSecInfo *reqInfo = NULL;
668
669 node = ListGetFirstNode(profileSecurityBlock->requestlist);
670 while (node != 0) {
671 reqInfo = ListGetNodeData(node);
672 if (GapAddrCompare(&devInfo->addr, &reqInfo->addr)) {
673 reqInfo->status = GAP_SEC_REQ_STATUS_FAILED;
674 reqInfo->hciStatus = hciStatus;
675 }
676 node = ListGetNextNode(node);
677 }
678 }
679
GapUpdateSecurityStatusConnectComplete(const DeviceInfo * devInfo)680 static void GapUpdateSecurityStatusConnectComplete(const DeviceInfo *devInfo)
681 {
682 ListNode *node = NULL;
683 ProfileSecurityBlock *profileSecurityBlock = GapGetProfileSecurityBlock();
684 RequestSecInfo *reqInfo = NULL;
685
686 node = ListGetFirstNode(profileSecurityBlock->requestlist);
687 while (node != 0) {
688 reqInfo = ListGetNodeData(node);
689 if (GapAddrCompare(&devInfo->addr, &reqInfo->addr) && reqInfo->status == GAP_SEC_REQ_STATUS_WAIT_CONNECT) {
690 if (*GapGetSecurityMode() == SEC_MODE_2) {
691 reqInfo->status = GAP_SEC_REQ_STATUS_WAIT_ACTION;
692 } else {
693 reqInfo->status = GAP_SEC_REQ_STATUS_WAIT_FEATURE;
694 }
695 }
696 node = ListGetNextNode(node);
697 }
698 }
699
GapUpdateSecurityStatusFeatureComplete(DeviceInfo * devInfo)700 static void GapUpdateSecurityStatusFeatureComplete(DeviceInfo *devInfo)
701 {
702 ListNode *node = NULL;
703 ProfileSecurityBlock *profileSecurityBlock = GapGetProfileSecurityBlock();
704 RequestSecInfo *reqInfo = NULL;
705
706 node = ListGetFirstNode(profileSecurityBlock->requestlist);
707 while (node != NULL) {
708 reqInfo = ListGetNodeData(node);
709 node = ListGetNextNode(node);
710 if (GapAddrCompare(&devInfo->addr, &reqInfo->addr) && reqInfo->status == GAP_SEC_REQ_STATUS_WAIT_FEATURE) {
711 if (!devInfo->supportSSP || reqInfo->info.direction == OUTGOING || devInfo->isEncryption) {
712 reqInfo->status = GAP_SEC_REQ_STATUS_WAIT_ACTION;
713 continue;
714 }
715 reqInfo->status = GAP_SEC_REQ_STATUS_WAIT_ENCRYPT;
716 if (devInfo->waitEncryptAlarm == NULL) {
717 devInfo->waitEncryptAlarm = AlarmCreate("wait encrypt", false);
718 }
719 }
720 }
721 }
722
GapUpdateSecurityRequest(DeviceInfo * devInfo,enum DeviceSecurityEvent event,uint8_t hciStatus)723 void GapUpdateSecurityRequest(DeviceInfo *devInfo, enum DeviceSecurityEvent event, uint8_t hciStatus)
724 {
725 switch (event) {
726 case GAP_SEC_EVENT_CONNECT_COMP:
727 GapUpdateSecurityStatusConnectComplete(devInfo);
728 break;
729 case GAP_SEC_EVENT_FEATURE_COMP:
730 GapUpdateSecurityStatusFeatureComplete(devInfo);
731 break;
732 case GAP_SEC_EVENT_UNAUTH_SUCCESS:
733 GapUpdateSecurityStatusUnauthenticationSuccess(devInfo);
734 break;
735 case GAP_SEC_EVENT_AUTH_SUCCESS:
736 GapUpdateSecurityStatusAuthenticationSuccess(devInfo);
737 break;
738 case GAP_SEC_EVENT_ENC_SUCCESS:
739 if (devInfo->waitEncryptAlarm != NULL) {
740 AlarmCancel(devInfo->waitEncryptAlarm);
741 AlarmDelete(devInfo->waitEncryptAlarm);
742 devInfo->waitEncryptAlarm = NULL;
743 }
744 GapUpdateSecurityStatusEncryptionSuccess(devInfo);
745 break;
746 case GAP_SEC_EVENT_ENC_FAILED:
747 if (devInfo->waitEncryptAlarm != NULL) {
748 AlarmCancel(devInfo->waitEncryptAlarm);
749 AlarmDelete(devInfo->waitEncryptAlarm);
750 devInfo->waitEncryptAlarm = NULL;
751 }
752 GapUpdateSecurityStatusFailed(devInfo, hciStatus);
753 break;
754 case GAP_SEC_EVENT_AUTH_FAILED:
755 GapUpdateSecurityStatusFailed(devInfo, hciStatus);
756 break;
757 case GAP_SEC_EVENT_WAIT_ENC_TIMEOUT:
758 GapUpdateSecurityStatusEncryptionTimeout(devInfo);
759 break;
760 case GAP_SEC_EVENT_ACL_DISCONNECT:
761 GapUpdateSecurityStatusAclDisconnect(devInfo, hciStatus);
762 break;
763 default:
764 break;
765 }
766 }
767
GapAuthenticationRequested(uint16_t handle)768 static int GapAuthenticationRequested(uint16_t handle)
769 {
770 HciAuthenticationRequestedParam hciCmdParam = {
771 .connectionHandle = handle,
772 };
773
774 return HCI_AuthenticationRequested(&hciCmdParam);
775 }
776
GapAuthenticationWaitRetryTimeoutTask(void * ctx)777 static void GapAuthenticationWaitRetryTimeoutTask(void *ctx)
778 {
779 RequestSecInfo *reqInfo = ListForEachData(
780 GapGetProfileSecurityBlock()->requestlist, GapFindCmpListData, ((GapGeneralPointerInfo *)ctx)->pointer);
781 if (reqInfo != NULL) {
782 DeviceInfo *deviceInfo =
783 ListForEachData(GapGetConnectionInfoBlock()->devicelist, GapFindConnectionDeviceByAddr, &reqInfo->addr);
784 if (deviceInfo != NULL) {
785 deviceInfo->authenticationStatus = GAP_AUTH_STATUS_RETRY;
786 deviceInfo->actionReq = NULL;
787 GapRequestSecurityProcess();
788 } else {
789 BTM_AclConnect(&reqInfo->addr);
790 }
791 }
792 }
793
GapAuthenticationWaitRetryTimeout(void * parameter)794 static void GapAuthenticationWaitRetryTimeout(void *parameter)
795 {
796 LOG_INFO("%{public}s: ", __FUNCTION__);
797 GapGeneralPointerInfo *ctx = MEM_MALLOC.alloc(sizeof(GapGeneralPointerInfo));
798 if (ctx == NULL) {
799 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
800 return;
801 }
802
803 ctx->pointer = parameter;
804
805 int ret = GapRunTaskUnBlockProcess(GapAuthenticationWaitRetryTimeoutTask, ctx, NULL);
806 if (ret != BT_NO_ERROR) {
807 LOG_ERROR("%{public}s: Task error:%{public}d.", __FUNCTION__, ret);
808 }
809 }
810
GapAuthenticationClearInfo(RequestSecInfo * reqInfo)811 void GapAuthenticationClearInfo(RequestSecInfo *reqInfo)
812 {
813 if (reqInfo == NULL) {
814 return;
815 }
816 if (reqInfo->waitRetryalarm != NULL) {
817 AlarmCancel(reqInfo->waitRetryalarm);
818 AlarmDelete(reqInfo->waitRetryalarm);
819 reqInfo->waitRetryalarm = NULL;
820 }
821 }
822
GapAuthenticationRetry(DeviceInfo * deviceInfo,RequestSecInfo * reqInfo,uint8_t hciStatus)823 void GapAuthenticationRetry(DeviceInfo *deviceInfo, RequestSecInfo *reqInfo, uint8_t hciStatus)
824 {
825 if (reqInfo == NULL) {
826 return;
827 }
828
829 if (reqInfo->waitRetryalarm == NULL) {
830 reqInfo->waitRetryalarm = AlarmCreate("retry pair", false);
831 }
832
833 if (++reqInfo->retryCount <= GAP_PAIR_RETRY_COUNT) {
834 AlarmCancel(reqInfo->waitRetryalarm);
835 AlarmSet(reqInfo->waitRetryalarm, GAP_PAIR_RETRY_WAIT_TIME, GapAuthenticationWaitRetryTimeout, reqInfo);
836 } else {
837 if (deviceInfo != NULL) {
838 GapUpdateSecurityRequest(deviceInfo, GAP_SEC_EVENT_AUTH_FAILED, hciStatus);
839 } else {
840 reqInfo->status = GAP_SEC_REQ_STATUS_FAILED;
841 reqInfo->hciStatus = hciStatus;
842 if (reqInfo->info.serviceId == GAP) {
843 GapDoAuthenticationCallback(reqInfo);
844 }
845 if (!reqInfo->doCallback) {
846 reqInfo->doCallback = true;
847 GapDoSecurityCallback(reqInfo);
848 }
849 }
850 }
851 }
852
GapOnAuthenticationComplete(const HciAuthenticationCompleteEventParam * eventParam)853 NO_SANITIZE("cfi") void GapOnAuthenticationComplete(const HciAuthenticationCompleteEventParam *eventParam)
854 {
855 LOG_DEBUG("%{public}s:", __FUNCTION__);
856 bool inDedicatedBonding = false;
857 bool isGapRequest = false;
858 bool callback = true;
859 BtAddr addr = {0};
860
861 ConnectionInfoBlock *connectionInfoBlock = GapGetConnectionInfoBlock();
862 DeviceInfo *deviceInfo = ListForEachData(
863 connectionInfoBlock->devicelist, GapFindConnectionDeviceByHandle, (void *)&eventParam->connectionHandle);
864 if (deviceInfo != NULL) {
865 isGapRequest = (deviceInfo->actionReq && deviceInfo->actionReq->info.serviceId == GAP);
866 deviceInfo->authenticationStatus = GAP_AUTH_STATUS_IDLE;
867 if (eventParam->status == HCI_SUCCESS) {
868 if (GapDeviceLinkkeyIsAuthenticated(deviceInfo)) {
869 deviceInfo->isAuthentication = true;
870 GapUpdateSecurityRequest(deviceInfo, GAP_SEC_EVENT_AUTH_SUCCESS, eventParam->status);
871 } else {
872 GapUpdateSecurityRequest(deviceInfo, GAP_SEC_EVENT_UNAUTH_SUCCESS, eventParam->status);
873 }
874 GapReadNewLocalOOBData();
875 GapAuthenticationClearInfo(deviceInfo->actionReq);
876 deviceInfo->actionReq = NULL;
877 } else if ((eventParam->status == HCI_PIN_OR_KEY_MISSING && GapIsKeyMissingRetry()) ||
878 BtmInteropIsMatchedAddr(INTEROP_AUTO_RETRY_PAIRING, &deviceInfo->addr)) {
879 deviceInfo->authenticationStatus = GAP_AUTH_STATUS_WAIT_RETRY;
880 GapAuthenticationRetry(deviceInfo, deviceInfo->actionReq, eventParam->status);
881 callback = false;
882 } else {
883 GapUpdateSecurityRequest(deviceInfo, GAP_SEC_EVENT_AUTH_FAILED, eventParam->status);
884 deviceInfo->actionReq = NULL;
885 }
886 inDedicatedBonding = deviceInfo->inDedicatedBonding;
887 (void)memcpy_s(&addr, sizeof(BtAddr), &deviceInfo->addr, sizeof(BtAddr));
888 if (deviceInfo->status == GAP_DEV_SEC_STATUS_ACTION) {
889 deviceInfo->status = GAP_DEV_SEC_STATUS_WAIT_DISC;
890 }
891 }
892
893 GapRequestSecurityProcess();
894
895 if (g_authenticationCallback.callback.authenticationComplete) {
896 if (inDedicatedBonding == true) {
897 if (isGapRequest || callback) {
898 g_authenticationCallback.callback.authenticationComplete(
899 &addr, eventParam->status, g_authenticationCallback.context);
900 }
901 } else if (inDedicatedBonding == false && callback) {
902 g_authenticationCallback.callback.authenticationComplete(
903 &addr, eventParam->status, g_authenticationCallback.context);
904 }
905 }
906 }
907
GapIOCapabilityRequestReply(const BtAddr * addr,uint8_t ioCapability,uint8_t authReq,uint8_t oobDataPresent)908 static int GapIOCapabilityRequestReply(
909 const BtAddr *addr, uint8_t ioCapability, uint8_t authReq, uint8_t oobDataPresent)
910 {
911 bool isDedicatedBonding = false;
912 uint8_t authenticationRequirements = AUTHENTICATION_UNKNOWN_MITM;
913
914 ConnectionInfoBlock *connectionInfoBlock = GapGetConnectionInfoBlock();
915 DeviceInfo *devInfo = NULL;
916 devInfo = ListForEachData(connectionInfoBlock->devicelist, GapFindConnectionDeviceByAddr, (void *)addr);
917 if (devInfo != NULL) {
918 devInfo->localIOCap = ioCapability;
919 isDedicatedBonding = devInfo->inDedicatedBonding;
920 authenticationRequirements = devInfo->remoteAuthReq;
921 }
922
923 if (authenticationRequirements == AUTHENTICATION_UNKNOWN_MITM) {
924 if (!GapIsBondMode()) {
925 authenticationRequirements = AUTHENTICATION_NO_MITM_NO_BONDING + authReq;
926 } else if (isDedicatedBonding) {
927 authenticationRequirements = AUTHENTICATION_NO_MITM_DEDICATED_BONDING + authReq;
928 } else {
929 authenticationRequirements = AUTHENTICATION_NO_MITM_GENERAL_BONDING + authReq;
930 }
931 }
932
933 HciIOCapabilityRequestReplyParam hciCmdParam;
934 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
935 hciCmdParam.ioCapability = ioCapability;
936 hciCmdParam.authenticationRequirements = authenticationRequirements;
937 hciCmdParam.oobDataPresent = oobDataPresent;
938
939 return HCI_IOCapabilityRequestReply(&hciCmdParam);
940 }
941
GapIOCapabilityRequestNegativeReply(const BtAddr * addr,uint8_t reason)942 static int GapIOCapabilityRequestNegativeReply(const BtAddr *addr, uint8_t reason)
943 {
944 HciIoCapabilityRequestNegativeReplyParam hciCmdParam;
945
946 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
947 hciCmdParam.reason = reason;
948
949 return HCI_IOCapabilityRequestNegativeReply(&hciCmdParam);
950 }
951
GapOnIOCapabilityRequestEvent(const HciIoCapabilityRequestEventParam * eventParam)952 NO_SANITIZE("cfi") void GapOnIOCapabilityRequestEvent(const HciIoCapabilityRequestEventParam *eventParam)
953 {
954 LOG_DEBUG("%{public}s:" BT_ADDR_FMT "", __FUNCTION__, BT_ADDR_FMT_OUTPUT(eventParam->bdAddr.raw));
955 BtAddr addr = BT_ADDR_NULL;
956 bool doReject = false;
957 GapChangeHCIAddr(&addr, &eventParam->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
958
959 ConnectionInfoBlock *connectionInfoBlock = GapGetConnectionInfoBlock();
960 DeviceInfo *devInfo = NULL;
961 devInfo = ListForEachData(connectionInfoBlock->devicelist, GapFindConnectionDeviceByAddr, (void *)&addr);
962 if (devInfo != NULL) {
963 if (devInfo->authenticationStatus == GAP_AUTH_STATUS_IDLE) {
964 GapStartUseAclConnection(devInfo, GAP_USE_ACL_CONNECTION_TIME);
965 }
966 if ((GapIsBondMode() == false) && (devInfo->remoteAuthReq != AUTHENTICATION_UNKNOWN_MITM) &&
967 (devInfo->remoteAuthReq != AUTHENTICATION_MITM_NO_BONDING) &&
968 (devInfo->remoteAuthReq != AUTHENTICATION_NO_MITM_NO_BONDING)) {
969 doReject = true;
970 }
971 }
972
973 if (doReject) {
974 GapIOCapabilityRequestNegativeReply(&addr, HCI_PAIRING_NOT_ALLOWED);
975 } else {
976 if (g_authenticationCallback.callback.IOCapabilityReq) {
977 g_authenticationCallback.callback.IOCapabilityReq(&addr, g_authenticationCallback.context);
978 } else {
979 LOG_INFO("Not Register");
980 GapIOCapabilityRequestNegativeReply(&addr, HCI_PAIRING_NOT_ALLOWED);
981 }
982 }
983 }
984
GapIOCapabilityRequestReplyComplete(const HciIOCapabilityRequestReplyReturnParam * param)985 void GapIOCapabilityRequestReplyComplete(const HciIOCapabilityRequestReplyReturnParam *param)
986 {
987 if (param->status != HCI_SUCCESS) {
988 BtAddr addr = BT_ADDR_NULL;
989 GapChangeHCIAddr(&addr, ¶m->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
990 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed: %02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(param->bdAddr.raw), param->status);
991 GapIOCapabilityRequestNegativeReply(&addr, HCI_PAIRING_NOT_ALLOWED);
992 }
993 }
994
GapIOCapabilityRequestNegativeReplyComplete(const HciIoCapabilityRequestNegativeReplyReturnParam * param)995 void GapIOCapabilityRequestNegativeReplyComplete(const HciIoCapabilityRequestNegativeReplyReturnParam *param)
996 {
997 if (param->status != HCI_SUCCESS) {
998 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed: %02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(param->bdAddr.raw), param->status);
999 }
1000 }
1001
GapOnIOCapabilityResponseEvent(const HciIoCapabilityResponseEventParam * eventParam)1002 NO_SANITIZE("cfi") void GapOnIOCapabilityResponseEvent(const HciIoCapabilityResponseEventParam *eventParam)
1003 {
1004 LOG_DEBUG("%{public}s:" BT_ADDR_FMT "", __FUNCTION__, BT_ADDR_FMT_OUTPUT(eventParam->bdAddr.raw));
1005 BtAddr addr = BT_ADDR_NULL;
1006
1007 GapChangeHCIAddr(&addr, &eventParam->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1008
1009 DeviceInfo *devInfo = NULL;
1010 devInfo = ListForEachData(GapGetConnectionInfoBlock()->devicelist, GapFindConnectionDeviceByAddr, (void *)&addr);
1011 if (devInfo != NULL) {
1012 devInfo->remoteAuthReq = eventParam->authenticationRequirements;
1013 }
1014
1015 if (g_authenticationCallback.callback.IOCapabilityRsp) {
1016 g_authenticationCallback.callback.IOCapabilityRsp(
1017 &addr, eventParam->IOCapability, g_authenticationCallback.context);
1018 }
1019 }
1020
GAP_RegisterAuthenticationCallback(const GapAuthenticationCallback * callback,void * context)1021 int GAP_RegisterAuthenticationCallback(const GapAuthenticationCallback *callback, void *context)
1022 {
1023 LOG_INFO("%{public}s:%{public}s", __FUNCTION__, callback ? "register" : "NULL");
1024 if (callback == NULL) {
1025 (void)memset_s(&g_authenticationCallback.callback,
1026 sizeof(g_authenticationCallback.callback),
1027 0x00,
1028 sizeof(g_authenticationCallback.callback));
1029 } else {
1030 g_authenticationCallback.callback = *callback;
1031 }
1032 g_authenticationCallback.context = context;
1033 return GAP_SUCCESS;
1034 }
1035
GAP_DeregisterAuthenticationCallback(void)1036 int GAP_DeregisterAuthenticationCallback(void)
1037 {
1038 (void)memset_s(&g_authenticationCallback.callback,
1039 sizeof(g_authenticationCallback.callback),
1040 0x00,
1041 sizeof(g_authenticationCallback.callback));
1042 g_authenticationCallback.context = NULL;
1043 return GAP_SUCCESS;
1044 }
1045
GAP_AuthenticationReq(const BtAddr * addr)1046 int GAP_AuthenticationReq(const BtAddr *addr)
1047 {
1048 int ret = GAP_SUCCESS;
1049 ConnectionInfoBlock *connectionInfoBlock = NULL;
1050 ProfileSecurityBlock *profileSecurityBlock = NULL;
1051 DeviceInfo *deviceInfo = NULL;
1052
1053 LOG_INFO("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr->addr));
1054
1055 if (GapIsBredrEnable() == false) {
1056 return GAP_ERR_NOT_ENABLE;
1057 }
1058
1059 if (!GapIsBondMode()) {
1060 ret = GAP_ERR_INVAL_STATE;
1061 } else {
1062 RequestSecInfo *secInfo = NULL;
1063 ProfileSecurityInfo tempInfo = {0};
1064 tempInfo.info.serviceId = GAP;
1065 tempInfo.info.direction = OUTGOING;
1066 tempInfo.securityMode = GAP_SEC_OUT_AUTHENTICATION;
1067 connectionInfoBlock = GapGetConnectionInfoBlock();
1068 profileSecurityBlock = GapGetProfileSecurityBlock();
1069 secInfo = GapAllocReqSecInfo(addr, NULL, NULL, &tempInfo);
1070 if (secInfo != NULL) {
1071 ListAddFirst(profileSecurityBlock->requestlist, secInfo);
1072 deviceInfo = ListForEachData(connectionInfoBlock->devicelist, GapFindConnectionDeviceByAddr, (void *)addr);
1073 if (deviceInfo != NULL) {
1074 GapRequestSecurityProcess();
1075 } else {
1076 ret = BTM_AclConnect(addr);
1077 }
1078 } else {
1079 ret = GAP_ERR_OUT_OF_RES;
1080 }
1081 }
1082
1083 return ret;
1084 }
1085
GAP_CancelAuthenticationReq(const BtAddr * addr)1086 int GAP_CancelAuthenticationReq(const BtAddr *addr)
1087 {
1088 if (GapIsBredrEnable() == false) {
1089 return GAP_ERR_NOT_ENABLE;
1090 }
1091
1092 DeviceInfo *devInfo = NULL;
1093 devInfo = ListForEachData(GapGetConnectionInfoBlock()->devicelist, GapFindConnectionDeviceByAddr, (void *)addr);
1094 if (devInfo != NULL) {
1095 if (devInfo->status == GAP_DEV_SEC_STATUS_ACTION) {
1096 devInfo->status = GAP_DEV_SEC_STATUS_IDLE;
1097 BTM_AclRelease(devInfo->handle);
1098 }
1099 }
1100
1101 return GAP_SUCCESS;
1102 }
1103
GAP_IOCapabilityRsp(const BtAddr * addr,uint8_t accept,uint8_t ioCapability,uint8_t oobDataPresent,uint8_t authReq)1104 int GAP_IOCapabilityRsp(
1105 const BtAddr *addr, uint8_t accept, uint8_t ioCapability, uint8_t oobDataPresent, uint8_t authReq)
1106 {
1107 int ret;
1108
1109 LOG_INFO("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr->addr));
1110
1111 if (GapIsBredrEnable() == false) {
1112 return GAP_ERR_NOT_ENABLE;
1113 }
1114
1115 DeviceInfo *devInfo = NULL;
1116 devInfo = ListForEachData(GapGetConnectionInfoBlock()->devicelist, GapFindConnectionDeviceByAddr, (void *)addr);
1117 if (devInfo != NULL && devInfo->actionReq != NULL) {
1118 if (!devInfo->actionReq->needAuthentication && devInfo->actionReq->needUnauthentication) {
1119 if (authReq == GAP_MITM_REQUIRED) {
1120 authReq = GAP_MITM_NOT_REQUIRED;
1121 }
1122 }
1123 }
1124
1125 if (accept == GAP_ACCEPT) {
1126 ret = GapIOCapabilityRequestReply(addr, ioCapability, authReq, oobDataPresent);
1127 } else if (accept == GAP_NOT_ACCEPT) {
1128 ret = GapIOCapabilityRequestNegativeReply(addr, HCI_PAIRING_NOT_ALLOWED);
1129 } else {
1130 ret = GAP_ERR_INVAL_PARAM;
1131 }
1132
1133 return ret;
1134 }
1135
GapUserConfirmationRequestReply(const BtAddr * addr)1136 static int GapUserConfirmationRequestReply(const BtAddr *addr)
1137 {
1138 HciUserConfirmationRequestReplyParam hciCmdParam;
1139
1140 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
1141
1142 return HCI_UserConfirmationRequestReply(&hciCmdParam);
1143 }
1144
GapUserConfirmationRequestNegativeReply(const BtAddr * addr)1145 static int GapUserConfirmationRequestNegativeReply(const BtAddr *addr)
1146 {
1147 HciUserConfirmationRequestNegativeReplyParam hciCmdParam;
1148
1149 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
1150
1151 return HCI_UserConfirmationRequestNegativeReply(&hciCmdParam);
1152 }
1153
GapOnUserConfirmationRequestEvent(const HciUserConfirmationRequestEventParam * eventParam)1154 NO_SANITIZE("cfi") void GapOnUserConfirmationRequestEvent(const HciUserConfirmationRequestEventParam *eventParam)
1155 {
1156 LOG_INFO("%{public}s:" BT_ADDR_FMT "", __FUNCTION__, BT_ADDR_FMT_OUTPUT(eventParam->bdAddr.raw));
1157 BtAddr addr = BT_ADDR_NULL;
1158 GapChangeHCIAddr(&addr, &eventParam->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1159 if (g_authenticationCallback.callback.userConfirmReq) {
1160 g_authenticationCallback.callback.userConfirmReq(
1161 &addr, eventParam->numericValue, g_authenticationCallback.context);
1162 } else {
1163 GapUserConfirmationRequestNegativeReply(&addr);
1164 }
1165 }
1166
GapUserConfirmationRequestReplyComplete(const HciUserConfirmationRequestReplyReturnParam * param)1167 void GapUserConfirmationRequestReplyComplete(const HciUserConfirmationRequestReplyReturnParam *param)
1168 {
1169 if (param->status != HCI_SUCCESS) {
1170 BtAddr addr = BT_ADDR_NULL;
1171 GapChangeHCIAddr(&addr, ¶m->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1172 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr.addr), param->status);
1173 GapUserConfirmationRequestNegativeReply(&addr);
1174 }
1175 }
1176
GapUserConfirmationRequestNegativeReplyComplete(const HciUserConfirmationRequestNegativeReplyReturnParam * param)1177 void GapUserConfirmationRequestNegativeReplyComplete(const HciUserConfirmationRequestNegativeReplyReturnParam *param)
1178 {
1179 if (param->status != HCI_SUCCESS) {
1180 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(param->bdAddr.raw), param->status);
1181 }
1182 }
1183
GAP_UserConfirmRsp(const BtAddr * addr,uint8_t accept)1184 int GAP_UserConfirmRsp(const BtAddr *addr, uint8_t accept)
1185 {
1186 int ret;
1187
1188 LOG_INFO("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr->addr));
1189
1190 if (GapIsBredrEnable() == false) {
1191 return GAP_ERR_NOT_ENABLE;
1192 }
1193
1194 if (accept == GAP_ACCEPT) {
1195 ret = GapUserConfirmationRequestReply(addr);
1196 } else if (accept == GAP_NOT_ACCEPT) {
1197 ret = GapUserConfirmationRequestNegativeReply(addr);
1198 } else {
1199 ret = GAP_ERR_INVAL_PARAM;
1200 }
1201
1202 return ret;
1203 }
1204
GapUserPasskeyRequestReply(const BtAddr * addr,int numericValue)1205 static int GapUserPasskeyRequestReply(const BtAddr *addr, int numericValue)
1206 {
1207 HciUserPasskeyRequestReplyParam hciCmdParam;
1208
1209 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
1210 hciCmdParam.numericValue = numericValue;
1211
1212 return HCI_UserPasskeyRequestReply(&hciCmdParam);
1213 }
1214
GapUserPasskeyRequestNegativeReply(const BtAddr * addr)1215 static int GapUserPasskeyRequestNegativeReply(const BtAddr *addr)
1216 {
1217 HciUserPasskeyRequestNegativeReplyParam hciCmdParam;
1218
1219 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
1220
1221 return HCI_UserPasskeyRequestNegativeReply(&hciCmdParam);
1222 }
1223
GapOnUserPasskeyNotificationEvent(const HciUserPasskeyNotificationEventParam * eventParam)1224 void GapOnUserPasskeyNotificationEvent(const HciUserPasskeyNotificationEventParam *eventParam)
1225 {
1226 LOG_DEBUG("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(eventParam->bdAddr.raw));
1227 BtAddr addr = BT_ADDR_NULL;
1228
1229 GapChangeHCIAddr(&addr, &eventParam->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1230 if (g_authenticationCallback.callback.userPasskeyNotification) {
1231 g_authenticationCallback.callback.userPasskeyNotification(
1232 &addr, eventParam->passkey, g_authenticationCallback.context);
1233 } else {
1234 LOG_WARN("%{public}s:not register callback", __FUNCTION__);
1235 }
1236 }
1237
GapOnUserPasskeyRequestEvent(const HciUserPasskeyRequestEventParam * eventParam)1238 void GapOnUserPasskeyRequestEvent(const HciUserPasskeyRequestEventParam *eventParam)
1239 {
1240 LOG_DEBUG("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(eventParam->bdAddr.raw));
1241 BtAddr addr = BT_ADDR_NULL;
1242
1243 GapChangeHCIAddr(&addr, &eventParam->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1244
1245 if (g_authenticationCallback.callback.userPasskeyReq) {
1246 g_authenticationCallback.callback.userPasskeyReq(&addr, g_authenticationCallback.context);
1247 } else {
1248 GapUserPasskeyRequestNegativeReply(&addr);
1249 }
1250 }
1251
GapUserPasskeyRequestReplyComplete(const HciUserPasskeyRequestReplyReturnParam * param)1252 void GapUserPasskeyRequestReplyComplete(const HciUserPasskeyRequestReplyReturnParam *param)
1253 {
1254 if (param->status != HCI_SUCCESS) {
1255 BtAddr addr = BT_ADDR_NULL;
1256 GapChangeHCIAddr(&addr, ¶m->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1257 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr.addr), param->status);
1258 GapUserPasskeyRequestNegativeReply(&addr);
1259 }
1260 }
1261
GapUserPasskeyRequestNegativeReplyComplete(const HciUserPasskeyRequestNegativeReplyReturnParam * param)1262 void GapUserPasskeyRequestNegativeReplyComplete(const HciUserPasskeyRequestNegativeReplyReturnParam *param)
1263 {
1264 if (param->status != HCI_SUCCESS) {
1265 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(param->bdAddr.raw), param->status);
1266 }
1267 }
1268
GAP_UserPasskeyRsp(const BtAddr * addr,uint8_t accept,uint32_t number)1269 int GAP_UserPasskeyRsp(const BtAddr *addr, uint8_t accept, uint32_t number)
1270 {
1271 int ret;
1272
1273 LOG_INFO("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr->addr));
1274
1275 if (GapIsBredrEnable() == false) {
1276 return GAP_ERR_NOT_ENABLE;
1277 }
1278
1279 if (accept == GAP_ACCEPT) {
1280 ret = GapUserPasskeyRequestReply(addr, number);
1281 } else if (accept == GAP_NOT_ACCEPT) {
1282 ret = GapUserPasskeyRequestNegativeReply(addr);
1283 } else {
1284 ret = GAP_ERR_INVAL_PARAM;
1285 }
1286
1287 return ret;
1288 }
1289
GapRemoteOOBDataRequestReply(const BtAddr * addr,const uint8_t * c,const uint8_t * r)1290 static int GapRemoteOOBDataRequestReply(const BtAddr *addr, const uint8_t *c, const uint8_t *r)
1291 {
1292 HciRemoteOobDataRequestReplyParam hciCmdParam;
1293 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
1294 (void)memcpy_s(hciCmdParam.C, GAP_OOB_DATA_CONFIRM_SIZE, c, GAP_OOB_DATA_CONFIRM_SIZE);
1295 (void)memcpy_s(hciCmdParam.r, GAP_OOB_DATA_RANDOM_SIZE, r, GAP_OOB_DATA_RANDOM_SIZE);
1296
1297 return HCI_RemoteOOBDataRequestReply(&hciCmdParam);
1298 }
1299
GapRemoteOOBDataRequestNegativeReply(const BtAddr * addr)1300 static int GapRemoteOOBDataRequestNegativeReply(const BtAddr *addr)
1301 {
1302 HciRemoteOobDataRequestNegativeReplyParam hciCmdParam;
1303
1304 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
1305
1306 return HCI_RemoteOOBDataRequestNegativeReply(&hciCmdParam);
1307 }
1308
GapOnRemoteOOBDataRequestEvent(const HciRemoteOobDataRequestEventParam * eventParam)1309 void GapOnRemoteOOBDataRequestEvent(const HciRemoteOobDataRequestEventParam *eventParam)
1310 {
1311 LOG_DEBUG("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(eventParam->bdAddr.raw));
1312 BtAddr addr;
1313
1314 GapChangeHCIAddr(&addr, &eventParam->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1315
1316 if (g_authenticationCallback.callback.remoteOobReq) {
1317 g_authenticationCallback.callback.remoteOobReq(&addr, g_authenticationCallback.context);
1318 } else {
1319 GapRemoteOOBDataRequestNegativeReply(&addr);
1320 }
1321 }
1322
GapRemoteOOBDataRequestReplyComplete(const HciRemoteOobDataRequestReplyReturnParam * param)1323 void GapRemoteOOBDataRequestReplyComplete(const HciRemoteOobDataRequestReplyReturnParam *param)
1324 {
1325 if (param->status != HCI_SUCCESS) {
1326 BtAddr addr;
1327 GapChangeHCIAddr(&addr, ¶m->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1328 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr.addr), param->status);
1329 GapRemoteOOBDataRequestNegativeReply(&addr);
1330 }
1331 }
1332
GapRemoteOOBExtendedDataRequestReplyComplete(const HciRemoteOobExtendedDataRequestReplyReturnParam * param)1333 void GapRemoteOOBExtendedDataRequestReplyComplete(const HciRemoteOobExtendedDataRequestReplyReturnParam *param)
1334 {
1335 if (param->status != HCI_SUCCESS) {
1336 BtAddr addr;
1337 GapChangeHCIAddr(&addr, ¶m->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1338 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr.addr), param->status);
1339 GapRemoteOOBDataRequestNegativeReply(&addr);
1340 }
1341 }
1342
GapRemoteOOBDataRequestNegativeReplyComplete(const HciRemoteOobDataRequestNegativeReplyReturnParam * param)1343 void GapRemoteOOBDataRequestNegativeReplyComplete(const HciRemoteOobDataRequestNegativeReplyReturnParam *param)
1344 {
1345 if (param->status != HCI_SUCCESS) {
1346 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(param->bdAddr.raw), param->status);
1347 }
1348 }
1349
GAP_RemoteOobRsp(const BtAddr * addr,uint8_t accept,const GapOOBData * data)1350 int GAP_RemoteOobRsp(const BtAddr *addr, uint8_t accept, const GapOOBData *data)
1351 {
1352 int ret;
1353
1354 if (GapIsBredrEnable() == false) {
1355 return GAP_ERR_NOT_ENABLE;
1356 }
1357
1358 if (addr == NULL || data == NULL) {
1359 return GAP_ERR_INVAL_PARAM;
1360 }
1361
1362 LOG_INFO("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr->addr));
1363
1364 if (accept == GAP_ACCEPT) {
1365 ret = GapRemoteOOBDataRequestReply(addr, (uint8_t *)data->C, (uint8_t *)data->R);
1366 } else if (accept == GAP_NOT_ACCEPT) {
1367 ret = GapRemoteOOBDataRequestNegativeReply(addr);
1368 } else {
1369 ret = GAP_ERR_INVAL_PARAM;
1370 }
1371
1372 return ret;
1373 }
1374
GapPINCodeRequestReply(const BtAddr * addr,uint8_t pinCode[GAP_PINCODE_SIZE],uint8_t pinCodeLength)1375 static int GapPINCodeRequestReply(const BtAddr *addr, uint8_t pinCode[GAP_PINCODE_SIZE], uint8_t pinCodeLength)
1376 {
1377 HciPinCodeRequestReplyParam hciCmdParam;
1378
1379 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
1380 (void)memcpy_s(hciCmdParam.pinCode, GAP_PINCODE_SIZE, pinCode, GAP_PINCODE_SIZE);
1381 hciCmdParam.pinCodeLength = pinCodeLength;
1382
1383 return HCI_PINCodeRequestReply(&hciCmdParam);
1384 }
1385
GapPINCodeRequestNegativeReply(const BtAddr * addr)1386 static int GapPINCodeRequestNegativeReply(const BtAddr *addr)
1387 {
1388 HciPinCodeRequestNegativeReplyParam hciCmdParam;
1389
1390 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
1391
1392 return HCI_PINCodeRequestNegativeReply(&hciCmdParam);
1393 }
1394
GapOnPINCodeRequestEvent(const HciPinCodeRequestEventParam * eventParam)1395 void GapOnPINCodeRequestEvent(const HciPinCodeRequestEventParam *eventParam)
1396 {
1397 LOG_INFO("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(eventParam->bdAddr.raw));
1398 BtAddr addr;
1399 bool isBondableMode = false;
1400
1401 GapChangeHCIAddr(&addr, &eventParam->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1402
1403 ConnectionInfoBlock *connectionInfoBlock = GapGetConnectionInfoBlock();
1404 DeviceInfo *devInfo = NULL;
1405 devInfo = ListForEachData(connectionInfoBlock->devicelist, GapFindConnectionDeviceByAddr, (void *)&addr);
1406 if (devInfo != NULL) {
1407 if (devInfo->authenticationStatus == GAP_AUTH_STATUS_IDLE) {
1408 GapStartUseAclConnection(devInfo, GAP_USE_ACL_CONNECTION_TIME);
1409 }
1410 }
1411
1412 isBondableMode = GapIsBondMode();
1413 if (isBondableMode == false) {
1414 GapPINCodeRequestNegativeReply(&addr);
1415 } else if (g_authenticationCallback.callback.pinCodeReq) {
1416 g_authenticationCallback.callback.pinCodeReq(&addr, g_authenticationCallback.context);
1417 } else {
1418 GapPINCodeRequestNegativeReply(&addr);
1419 }
1420 }
1421
GapPINCodeRequestReplyComplete(const HciPinCodeRequestReplyReturnParam * param)1422 void GapPINCodeRequestReplyComplete(const HciPinCodeRequestReplyReturnParam *param)
1423 {
1424 if (param->status != HCI_SUCCESS) {
1425 BtAddr addr;
1426 GapChangeHCIAddr(&addr, ¶m->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1427 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr.addr), param->status);
1428 GapPINCodeRequestNegativeReply(&addr);
1429 }
1430 }
1431
GapPINCodeRequestNegativeReplyComplete(const HciPinCodeRequestNegativeReplyReturnParam * param)1432 void GapPINCodeRequestNegativeReplyComplete(const HciPinCodeRequestNegativeReplyReturnParam *param)
1433 {
1434 if (param->status != HCI_SUCCESS) {
1435 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(param->bdAddr.raw), param->status);
1436 }
1437 }
1438
GAP_PinCodeRsp(const BtAddr * addr,uint8_t accept,const uint8_t * pinCode,uint8_t pinCodeLength)1439 int GAP_PinCodeRsp(const BtAddr *addr, uint8_t accept, const uint8_t *pinCode, uint8_t pinCodeLength)
1440 {
1441 int ret;
1442
1443 if (GapIsBredrEnable() == false) {
1444 return GAP_ERR_NOT_ENABLE;
1445 }
1446
1447 if (addr == NULL) {
1448 return GAP_ERR_INVAL_PARAM;
1449 } else if (accept == GAP_ACCEPT && (pinCode == NULL || pinCodeLength > GAP_PINCODE_SIZE)) {
1450 return GAP_ERR_INVAL_PARAM;
1451 }
1452
1453 LOG_INFO("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr->addr));
1454
1455 if (accept == GAP_ACCEPT) {
1456 uint8_t pin[GAP_PINCODE_SIZE] = {0};
1457 (void)memcpy_s(pin, GAP_PINCODE_SIZE, pinCode, pinCodeLength);
1458 ret = GapPINCodeRequestReply(addr, pin, pinCodeLength);
1459 } else if (accept == GAP_NOT_ACCEPT) {
1460 ret = GapPINCodeRequestNegativeReply(addr);
1461 } else {
1462 ret = GAP_ERR_INVAL_PARAM;
1463 }
1464
1465 return ret;
1466 }
1467
GapLinkKeyRequestReply(const BtAddr * addr,const uint8_t * linkKey)1468 static int GapLinkKeyRequestReply(const BtAddr *addr, const uint8_t *linkKey)
1469 {
1470 HciLinkKeyRequestReplyParam hciCmdParam;
1471 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
1472 if (memcpy_s(hciCmdParam.linkKey, GAP_LINKKEY_SIZE, linkKey, GAP_LINKKEY_SIZE) != EOK) {
1473 return GAP_STATUS_FAILED;
1474 }
1475
1476 return HCI_LinkKeyRequestReply(&hciCmdParam);
1477 }
1478
GapLinkKeyRequestNegativeReply(const BtAddr * addr)1479 static int GapLinkKeyRequestNegativeReply(const BtAddr *addr)
1480 {
1481 HciLinkKeyRequestNegativeReplyParam hciCmdParam;
1482
1483 (void)memcpy_s(hciCmdParam.bdAddr.raw, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
1484
1485 return HCI_LinkKeyRequestNegativeReply(&hciCmdParam);
1486 }
1487
GAP_PairIsFromLocal(const BtAddr * addr,bool * isLocal)1488 int GAP_PairIsFromLocal(const BtAddr *addr, bool *isLocal)
1489 {
1490 int ret = GAP_SUCCESS;
1491
1492 LOG_INFO("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr->addr));
1493
1494 if (GapIsBredrEnable() == false) {
1495 return GAP_ERR_NOT_ENABLE;
1496 }
1497
1498 ConnectionInfoBlock *connectionInfoBlock = GapGetConnectionInfoBlock();
1499 DeviceInfo *devInfo = NULL;
1500 devInfo = ListForEachData(connectionInfoBlock->devicelist, GapFindConnectionDeviceByAddr, (void *)addr);
1501 if (devInfo != NULL) {
1502 *isLocal = !!devInfo->authenticationStatus;
1503 } else {
1504 ret = GAP_ERR_INVAL_STATE;
1505 }
1506
1507 return ret;
1508 }
1509
GapOnLinkKeyNotificationEvent(const HciLinkKeyNotificationEventParam * eventParam)1510 NO_SANITIZE("cfi") void GapOnLinkKeyNotificationEvent(const HciLinkKeyNotificationEventParam *eventParam)
1511 {
1512 LOG_DEBUG("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(eventParam->bdAddr.raw));
1513 BtAddr addr;
1514
1515 GapChangeHCIAddr(&addr, &eventParam->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1516
1517 ConnectionInfoBlock *connectionInfoBlock = GapGetConnectionInfoBlock();
1518 DeviceInfo *devInfo = NULL;
1519 devInfo = ListForEachData(connectionInfoBlock->devicelist, GapFindConnectionDeviceByAddr, (void *)&addr);
1520 if (devInfo != NULL) {
1521 devInfo->linkkeyType = eventParam->keyType;
1522 }
1523
1524 if (g_authenticationCallback.callback.linkKeyNotification) {
1525 g_authenticationCallback.callback.linkKeyNotification(
1526 &addr, (uint8_t *)eventParam->linkKey, eventParam->keyType, g_authenticationCallback.context);
1527 }
1528 }
1529
GapOnLinkKeyRequestEvent(const HciLinkKeyRequestEventParam * eventParam)1530 NO_SANITIZE("cfi") void GapOnLinkKeyRequestEvent(const HciLinkKeyRequestEventParam *eventParam)
1531 {
1532 LOG_DEBUG("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(eventParam->bdAddr.raw));
1533 BtAddr addr;
1534 bool callback = true;
1535
1536 GapChangeHCIAddr(&addr, &eventParam->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1537
1538 ConnectionInfoBlock *connectionInfoBlock = GapGetConnectionInfoBlock();
1539 DeviceInfo *devInfo = NULL;
1540 devInfo = ListForEachData(connectionInfoBlock->devicelist, GapFindConnectionDeviceByAddr, (void *)&addr);
1541 if (devInfo != NULL && devInfo->authenticationStatus == GAP_AUTH_STATUS_RETRY) {
1542 callback = false;
1543 devInfo->authenticationStatus = GAP_AUTH_STATUS_ACTION;
1544 } else if (devInfo != NULL && devInfo->linkkeyType != GAP_LINK_KEY_TYPE_UNKNOWN &&
1545 !GapDeviceLinkkeyIsAuthenticated(devInfo) && devInfo->actionReq != NULL &&
1546 devInfo->actionReq->needAuthentication && devInfo->actionReq->needMITM) {
1547 callback = false;
1548 }
1549
1550 if (g_authenticationCallback.callback.linkKeyReq) {
1551 if (callback) {
1552 g_authenticationCallback.callback.linkKeyReq(&addr, g_authenticationCallback.context);
1553 } else {
1554 GapLinkKeyRequestNegativeReply(&addr);
1555 }
1556 } else {
1557 GapLinkKeyRequestNegativeReply(&addr);
1558 }
1559 }
1560
GapLinkKeyRequestReplyComplete(const HciLinkKeyRequestReplyReturnParam * param)1561 void GapLinkKeyRequestReplyComplete(const HciLinkKeyRequestReplyReturnParam *param)
1562 {
1563 if (param->status != HCI_SUCCESS) {
1564 BtAddr addr;
1565 GapChangeHCIAddr(&addr, ¶m->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1566 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr.addr), param->status);
1567 GapLinkKeyRequestNegativeReply(&addr);
1568 }
1569 }
1570
GapLinkKeyRequestNegativeReplyComplete(const HciLinkKeyRequestNegativeReplyReturnParam * param)1571 void GapLinkKeyRequestNegativeReplyComplete(const HciLinkKeyRequestNegativeReplyReturnParam *param)
1572 {
1573 if (param->status != HCI_SUCCESS) {
1574 LOG_WARN("%{public}s:" BT_ADDR_FMT "Failed:%02x", __FUNCTION__, BT_ADDR_FMT_OUTPUT(param->bdAddr.raw), param->status);
1575 }
1576 }
1577
GAP_LinkKeyRsp(const BtAddr * addr,uint8_t accept,const uint8_t linkKey[GAP_LINKKEY_SIZE],uint8_t keyType)1578 int GAP_LinkKeyRsp(const BtAddr *addr, uint8_t accept, const uint8_t linkKey[GAP_LINKKEY_SIZE], uint8_t keyType)
1579 {
1580 int ret;
1581
1582 if (GapIsBredrEnable() == false) {
1583 return GAP_ERR_NOT_ENABLE;
1584 }
1585
1586 if (addr == NULL || linkKey == NULL) {
1587 return GAP_ERR_INVAL_PARAM;
1588 }
1589
1590 LOG_INFO("%{public}s:" BT_ADDR_FMT " %hhu, keyType[%hhu]", __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr->addr), accept, keyType);
1591
1592 if (accept == GAP_ACCEPT) {
1593 ConnectionInfoBlock *connectionInfoBlock = GapGetConnectionInfoBlock();
1594 DeviceInfo *devInfo = NULL;
1595 devInfo = ListForEachData(connectionInfoBlock->devicelist, GapFindConnectionDeviceByAddr, (void *)addr);
1596 if (devInfo != NULL) {
1597 devInfo->linkkeyType = keyType;
1598 }
1599 ret = GapLinkKeyRequestReply(addr, linkKey);
1600 } else if (accept == GAP_NOT_ACCEPT) {
1601 ret = GapLinkKeyRequestNegativeReply(addr);
1602 } else {
1603 ret = GAP_ERR_INVAL_PARAM;
1604 }
1605
1606 return ret;
1607 }
1608
GapOnSimplePairingComplete(const HciSimplePairingCompleteEventParam * eventParam)1609 NO_SANITIZE("cfi") void GapOnSimplePairingComplete(const HciSimplePairingCompleteEventParam *eventParam)
1610 {
1611 LOG_INFO("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(eventParam->bdAddr.raw));
1612 BtAddr addr;
1613
1614 GapChangeHCIAddr(&addr, &eventParam->bdAddr, BT_PUBLIC_DEVICE_ADDRESS);
1615
1616 if (g_authenticationCallback.callback.simplePairComplete) {
1617 g_authenticationCallback.callback.simplePairComplete(
1618 &addr, eventParam->status, g_authenticationCallback.context);
1619 }
1620 }
1621
GapOnEncryptionChangeEvent(const HciEncryptionChangeEventParam * eventParam)1622 NO_SANITIZE("cfi") void GapOnEncryptionChangeEvent(const HciEncryptionChangeEventParam *eventParam)
1623 {
1624 BtAddr addr = {0};
1625 GapEncryptionChangeCallback callback = NULL;
1626 void *context = NULL;
1627
1628 ConnectionInfoBlock *connectionInfoBlock = GapGetConnectionInfoBlock();
1629 DeviceInfo *devInfo = NULL;
1630 devInfo = ListForEachData(
1631 connectionInfoBlock->devicelist, GapFindConnectionDeviceByHandle, (void *)&eventParam->connectionHandle);
1632 if (devInfo != NULL) {
1633 (void)memcpy_s(&addr, sizeof(BtAddr), &devInfo->addr, sizeof(BtAddr));
1634 if (eventParam->status == HCI_SUCCESS) {
1635 if (eventParam->encryptionEnabled) {
1636 devInfo->isEncryption = true;
1637 } else {
1638 devInfo->isEncryption = false;
1639 }
1640 GapUpdateSecurityRequest(devInfo, GAP_SEC_EVENT_ENC_SUCCESS, eventParam->status);
1641 } else {
1642 GapUpdateSecurityRequest(devInfo, GAP_SEC_EVENT_ENC_FAILED, eventParam->status);
1643 }
1644 if (devInfo->status == GAP_DEV_SEC_STATUS_ACTION && devInfo->encryptionStatus == GAP_ENC_STATUS_ACTION) {
1645 devInfo->actionReq = NULL;
1646 devInfo->status = GAP_DEV_SEC_STATUS_WAIT_DISC;
1647 devInfo->encryptionStatus = GAP_ENC_STATUS_IDLE;
1648 }
1649 }
1650
1651 GapRequestSecurityProcess();
1652
1653 EncryptionBlock *encryptionBlock = GapGetEncryptionBlock();
1654 if (encryptionBlock->status == GAP_SET_ENCRYPTION_STATUS_SETTING) {
1655 callback = encryptionBlock->callback;
1656 context = encryptionBlock->context;
1657 }
1658
1659 LOG_DEBUG("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr.addr));
1660
1661 if (devInfo != NULL) {
1662 if (g_authenticationCallback.callback.encryptionChangeCallback) {
1663 g_authenticationCallback.callback.encryptionChangeCallback(&addr,
1664 eventParam->encryptionEnabled ? GAP_ENCRYPTION_ON : GAP_ENCRYPTION_OFF,
1665 g_authenticationCallback.context);
1666 }
1667 if (callback) {
1668 callback(&addr, eventParam->encryptionEnabled ? GAP_ENCRYPTION_ON : GAP_ENCRYPTION_OFF, context);
1669 }
1670 }
1671
1672 GapLeEncryptionComplete(eventParam->connectionHandle, eventParam->status);
1673 }
1674
GapOnEncryptionKeyRefreshComplete(const HciEncryptionKeyRefreshCompleteEventParam * eventParam)1675 void GapOnEncryptionKeyRefreshComplete(const HciEncryptionKeyRefreshCompleteEventParam *eventParam)
1676 {
1677 BtAddr addr = {0};
1678
1679 ConnectionInfoBlock *connectionInfoBlock = GapGetConnectionInfoBlock();
1680 DeviceInfo *devInfo = NULL;
1681 devInfo = ListForEachData(
1682 connectionInfoBlock->devicelist, GapFindConnectionDeviceByHandle, (void *)&eventParam->connectionHandle);
1683 if (devInfo != NULL) {
1684 (void)memcpy_s(&addr, sizeof(BtAddr), &devInfo->addr, sizeof(BtAddr));
1685 if (eventParam->status == HCI_SUCCESS) {
1686 GapUpdateSecurityRequest(devInfo, GAP_SEC_EVENT_ENC_SUCCESS, eventParam->status);
1687 } else {
1688 GapUpdateSecurityRequest(devInfo, GAP_SEC_EVENT_ENC_FAILED, eventParam->status);
1689 }
1690 if (devInfo->status == GAP_DEV_SEC_STATUS_ACTION && devInfo->encryptionStatus == GAP_ENC_STATUS_ACTION) {
1691 devInfo->actionReq = NULL;
1692 devInfo->status = GAP_DEV_SEC_STATUS_WAIT_DISC;
1693 devInfo->encryptionStatus = GAP_ENC_STATUS_IDLE;
1694 }
1695 }
1696 GapRequestSecurityProcess();
1697 LOG_DEBUG("%{public}s:" BT_ADDR_FMT, __FUNCTION__, BT_ADDR_FMT_OUTPUT(addr.addr));
1698 }
1699
GapSetConnectionEncryption(uint16_t handle,uint8_t encryptionEnable)1700 static int GapSetConnectionEncryption(uint16_t handle, uint8_t encryptionEnable)
1701 {
1702 HciSetConnectionEncryptionParam hciCmdParam = {
1703 .connectionHandle = handle,
1704 .encryptionEnable = encryptionEnable,
1705 };
1706
1707 return HCI_SetConnectionEncryption(&hciCmdParam);
1708 }
1709