summaryrefslogtreecommitdiff
path: root/system/gd/security/internal/security_manager_impl.cc
blob: 4e21bc9c62ff41fb3ccb995a52ba61b5cde3b08c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
/*
 *
 *  Copyright 2019 The Android Open Source Project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
#include "security_manager_impl.h"

#include <iostream>

#include "common/bind.h"
#include "crypto_toolbox/crypto_toolbox.h"
#include "hci/address_with_type.h"
#include "os/log.h"
#include "os/rand.h"
#include "security/initial_informations.h"
#include "security/internal/security_manager_impl.h"
#include "security/pairing_handler_le.h"
#include "security/security_manager.h"
#include "security/ui.h"

namespace bluetooth {
namespace security {
namespace internal {

void SecurityManagerImpl::DispatchPairingHandler(
    std::shared_ptr<record::SecurityRecord> record,
    bool locally_initiated,
    hci::IoCapability io_capability,
    hci::AuthenticationRequirements auth_requirements,
    pairing::OobData remote_p192_oob_data,
    pairing::OobData remote_p256_oob_data) {
  common::OnceCallback<void(hci::Address, PairingResultOrFailure)> callback =
      common::BindOnce(&SecurityManagerImpl::OnPairingHandlerComplete, common::Unretained(this));
  auto entry = pairing_handler_map_.find(record->GetPseudoAddress()->GetAddress());
  if (entry != pairing_handler_map_.end()) {
    LOG_WARN("Device already has a pairing handler, and is in the middle of pairing!");
    return;
  }
  std::shared_ptr<pairing::PairingHandler> pairing_handler = nullptr;
  switch (record->GetPseudoAddress()->GetAddressType()) {
    case hci::AddressType::PUBLIC_DEVICE_ADDRESS: {
      pairing_handler = std::make_shared<security::pairing::ClassicPairingHandler>(
          security_manager_channel_,
          record,
          security_handler_,
          std::move(callback),
          user_interface_,
          user_interface_handler_,
          record->GetPseudoAddress()->ToString(),
          name_db_module_);
      break;
    }
    default:
      ASSERT_LOG(false, "Pairing type %hhu not implemented!", record->GetPseudoAddress()->GetAddressType());
  }
  auto new_entry = std::pair<hci::Address, std::shared_ptr<pairing::PairingHandler>>(
      record->GetPseudoAddress()->GetAddress(), pairing_handler);
  pairing_handler_map_.insert(std::move(new_entry));
  pairing_handler->Initiate(
      locally_initiated, io_capability, auth_requirements, remote_p192_oob_data, remote_p256_oob_data);
}

void SecurityManagerImpl::Init() {
  security_manager_channel_->SetChannelListener(this);
  security_manager_channel_->SendCommand(hci::WriteSimplePairingModeBuilder::Create(hci::Enable::ENABLED));
  security_manager_channel_->SendCommand(hci::WriteSecureConnectionsHostSupportBuilder::Create(hci::Enable::ENABLED));

  ASSERT_LOG(storage_module_ != nullptr, "Storage module must not be null!");
  security_database_.LoadRecordsFromStorage();

  storage::AdapterConfig adapter_config = storage_module_->GetAdapterConfig();
  if (!adapter_config.GetLeIdentityResolvingKey()) {
    auto mutation = storage_module_->Modify();
    mutation.Add(adapter_config.SetLeIdentityResolvingKey(bluetooth::os::GenerateRandom<16>()));
    mutation.Commit();
  }

  Address controllerAddress = controller_->GetMacAddress();
  if (!adapter_config.GetAddress() || adapter_config.GetAddress().value() != controllerAddress) {
    auto mutation = storage_module_->Modify();
    mutation.Add(adapter_config.SetAddress(controllerAddress));
    mutation.Commit();
  }

  local_identity_address_ =
      hci::AddressWithType(adapter_config.GetAddress().value(), hci::AddressType::PUBLIC_DEVICE_ADDRESS);
  local_identity_resolving_key_ = adapter_config.GetLeIdentityResolvingKey().value().bytes;

  hci::LeAddressManager::AddressPolicy address_policy = hci::LeAddressManager::AddressPolicy::USE_RESOLVABLE_ADDRESS;
  hci::AddressWithType address_with_type(hci::Address{}, hci::AddressType::RANDOM_DEVICE_ADDRESS);

  /* 7 minutes minimum, 15 minutes maximum for random address refreshing */
  auto minimum_rotation_time = std::chrono::minutes(7);
  auto maximum_rotation_time = std::chrono::minutes(15);

  acl_manager_->SetPrivacyPolicyForInitiatorAddress(
      address_policy, address_with_type, minimum_rotation_time, maximum_rotation_time);
}

void SecurityManagerImpl::CreateBond(hci::AddressWithType device) {
  this->CreateBondOutOfBand(device, pairing::OobData(), pairing::OobData());
}

void SecurityManagerImpl::CreateBondOutOfBand(
    hci::AddressWithType device, pairing::OobData remote_p192_oob_data, pairing::OobData remote_p256_oob_data) {
  auto record = security_database_.FindOrCreate(device);
  if (record->IsPaired()) {
    // Bonded means we saved it, but the caller doesn't care
    // Bonded will always mean paired
    NotifyDeviceBonded(device);
  } else {
    if (!record->IsPairing()) {
      // Dispatch pairing handler, if we are calling create we are the initiator
      LOG_WARN("Dispatch #1");
      DispatchPairingHandler(
          record,
          true,
          this->local_io_capability_,
          this->local_authentication_requirements_,
          remote_p192_oob_data,
          remote_p256_oob_data);
    }
  }
}

void SecurityManagerImpl::CreateBondLe(hci::AddressWithType address) {
  auto record = security_database_.FindOrCreate(address);
  if (record->IsPaired()) {
    NotifyDeviceBondFailed(address, PairingFailure("Already bonded"));
    return;
  }

  pending_le_pairing_.address_ = address;

  LeFixedChannelEntry* stored_chan = FindStoredLeChannel(address);
  if (stored_chan) {
    // We are already connected
    ConnectionIsReadyStartPairing(stored_chan);
    return;
  }

  l2cap_manager_le_->ConnectServices(
      address, common::BindOnce(&SecurityManagerImpl::OnConnectionFailureLe, common::Unretained(this)),
      security_handler_);
}

void SecurityManagerImpl::CancelBond(hci::AddressWithType device) {
  auto entry = pairing_handler_map_.find(device.GetAddress());
  if (entry != pairing_handler_map_.end()) {
    auto cancel_me = entry->second;
    pairing_handler_map_.erase(entry);
    cancel_me->Cancel();
  }

  auto record = security_database_.FindOrCreate(device);
  record->CancelPairing();

  WipeLePairingHandler();
}

void SecurityManagerImpl::RemoveBond(hci::AddressWithType device) {
  CancelBond(device);
  security_manager_channel_->Disconnect(device.GetAddress());
  security_database_.Remove(device);
  security_manager_channel_->SendCommand(hci::DeleteStoredLinkKeyBuilder::Create(
      device.GetAddress(), hci::DeleteStoredLinkKeyDeleteAllFlag::SPECIFIED_BD_ADDR));
  NotifyDeviceUnbonded(device);
}

void SecurityManagerImpl::SetUserInterfaceHandler(UI* user_interface, os::Handler* handler) {
  if (user_interface_ != nullptr || user_interface_handler_ != nullptr) {
    LOG_ALWAYS_FATAL("Listener has already been registered!");
  }
  user_interface_ = user_interface;
  user_interface_handler_ = handler;
}

// TODO(jpawlowski): remove once we have config file abstraction in cert tests
void SecurityManagerImpl::SetLeInitiatorAddressPolicyForTest(
    hci::LeAddressManager::AddressPolicy address_policy,
    hci::AddressWithType fixed_address,
    crypto_toolbox::Octet16 rotation_irk,
    std::chrono::milliseconds minimum_rotation_time,
    std::chrono::milliseconds maximum_rotation_time) {
  acl_manager_->SetPrivacyPolicyForInitiatorAddressForTest(
      address_policy, fixed_address, rotation_irk, minimum_rotation_time, maximum_rotation_time);
}

void SecurityManagerImpl::RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler) {
  for (auto it = listeners_.begin(); it != listeners_.end(); ++it) {
    if (it->first == listener) {
      LOG_ALWAYS_FATAL("Listener has already been registered!");
    }
  }

  listeners_.push_back({listener, handler});
}

void SecurityManagerImpl::UnregisterCallbackListener(ISecurityManagerListener* listener) {
  for (auto it = listeners_.begin(); it != listeners_.end(); ++it) {
    if (it->first == listener) {
      listeners_.erase(it);
      return;
    }
  }

  LOG_ALWAYS_FATAL("Listener has not been registered!");
}

void SecurityManagerImpl::NotifyDeviceBonded(hci::AddressWithType device) {
  for (auto& iter : listeners_) {
    iter.second->Post(common::Bind(&ISecurityManagerListener::OnDeviceBonded, common::Unretained(iter.first), device));
  }
}

void SecurityManagerImpl::NotifyDeviceBondFailed(hci::AddressWithType device, PairingFailure status) {
  for (auto& iter : listeners_) {
    iter.second->Post(
        common::Bind(&ISecurityManagerListener::OnDeviceBondFailed, common::Unretained(iter.first), device, status));
  }
}

void SecurityManagerImpl::NotifyDeviceUnbonded(hci::AddressWithType device) {
  for (auto& iter : listeners_) {
    iter.second->Post(
        common::Bind(&ISecurityManagerListener::OnDeviceUnbonded, common::Unretained(iter.first), device));
  }
  acl_manager_->RemoveDeviceFromConnectList(device);
}

void SecurityManagerImpl::NotifyEncryptionStateChanged(hci::EncryptionChangeView encryption_change_view) {
  for (auto& iter : listeners_) {
    iter.second->Post(common::Bind(&ISecurityManagerListener::OnEncryptionStateChanged, common::Unretained(iter.first),
                                   encryption_change_view));
  }
}

template <class T>
void SecurityManagerImpl::HandleEvent(T packet) {
  ASSERT(packet.IsValid());
  auto entry = pairing_handler_map_.find(packet.GetBdAddr());

  if (entry == pairing_handler_map_.end()) {
    auto bd_addr = packet.GetBdAddr();
    auto event_code = packet.GetEventCode();

    if (event_code != hci::EventCode::LINK_KEY_REQUEST && event_code != hci::EventCode::PIN_CODE_REQUEST &&
        event_code != hci::EventCode::IO_CAPABILITY_RESPONSE) {
      LOG_ERROR("No classic pairing handler for device '%s' ready for command %s ", bd_addr.ToString().c_str(),
                hci::EventCodeText(event_code).c_str());
      return;
    }

    auto device = storage_module_->GetDeviceByClassicMacAddress(bd_addr);

    auto record =
        security_database_.FindOrCreate(hci::AddressWithType{bd_addr, hci::AddressType::PUBLIC_DEVICE_ADDRESS});
    LOG_WARN("Dispatch #2");
    DispatchPairingHandler(
        record,
        false,
        this->local_io_capability_,
        this->local_authentication_requirements_,
        pairing::OobData(),
        pairing::OobData());
    entry = pairing_handler_map_.find(bd_addr);
  }
  entry->second->OnReceive(packet);
}

void SecurityManagerImpl::OnHciEventReceived(hci::EventView packet) {
  auto event = hci::EventView::Create(packet);
  ASSERT_LOG(event.IsValid(), "Received invalid packet");
  const hci::EventCode code = event.GetEventCode();
  switch (code) {
    case hci::EventCode::PIN_CODE_REQUEST:
      HandleEvent<hci::PinCodeRequestView>(hci::PinCodeRequestView::Create(event));
      break;
    case hci::EventCode::LINK_KEY_REQUEST:
      HandleEvent(hci::LinkKeyRequestView::Create(event));
      break;
    case hci::EventCode::LINK_KEY_NOTIFICATION:
      HandleEvent(hci::LinkKeyNotificationView::Create(event));
      break;
    case hci::EventCode::IO_CAPABILITY_REQUEST:
      HandleEvent(hci::IoCapabilityRequestView::Create(event));
      break;
    case hci::EventCode::IO_CAPABILITY_RESPONSE:
      HandleEvent(hci::IoCapabilityResponseView::Create(event));
      break;
    case hci::EventCode::SIMPLE_PAIRING_COMPLETE:
      HandleEvent(hci::SimplePairingCompleteView::Create(event));
      break;
    case hci::EventCode::REMOTE_OOB_DATA_REQUEST:
      HandleEvent(hci::RemoteOobDataRequestView::Create(event));
      break;
    case hci::EventCode::USER_PASSKEY_NOTIFICATION:
      HandleEvent(hci::UserPasskeyNotificationView::Create(event));
      break;
    case hci::EventCode::KEYPRESS_NOTIFICATION:
      HandleEvent(hci::KeypressNotificationView::Create(event));
      break;
    case hci::EventCode::USER_CONFIRMATION_REQUEST:
      HandleEvent(hci::UserConfirmationRequestView::Create(event));
      break;
    case hci::EventCode::USER_PASSKEY_REQUEST:
      HandleEvent(hci::UserPasskeyRequestView::Create(event));
      break;
    case hci::EventCode::REMOTE_HOST_SUPPORTED_FEATURES_NOTIFICATION:
      LOG_INFO("Unhandled event: %s", hci::EventCodeText(code).c_str());
      break;

    case hci::EventCode::ENCRYPTION_CHANGE: {
      EncryptionChangeView encryption_change_view = EncryptionChangeView::Create(event);
      if (!encryption_change_view.IsValid()) {
        LOG_ERROR("Invalid EncryptionChange packet received");
        return;
      }
      if (encryption_change_view.GetConnectionHandle() == pending_le_pairing_.connection_handle_) {
        pending_le_pairing_.handler_->OnHciEvent(event);
        return;
      }
      NotifyEncryptionStateChanged(encryption_change_view);
      break;
    }

    default:
      ASSERT_LOG(false, "Cannot handle received packet: %s", hci::EventCodeText(code).c_str());
      break;
  }
}

void SecurityManagerImpl::OnConnectionClosed(hci::Address address) {
  auto entry = pairing_handler_map_.find(address);
  if (entry != pairing_handler_map_.end()) {
    LOG_INFO("Cancelling pairing handler for '%s'", address.ToString().c_str());
    entry->second->Cancel();
  }
  auto record = security_database_.FindOrCreate(hci::AddressWithType(address, hci::AddressType::PUBLIC_DEVICE_ADDRESS));
  if (record->IsTemporary()) {
    security_database_.Remove(hci::AddressWithType(address, hci::AddressType::PUBLIC_DEVICE_ADDRESS));
  }
  if (this->facade_disconnect_callback_) {
    this->security_handler_->Call(
        *this->facade_disconnect_callback_, hci::AddressWithType(address, hci::AddressType::PUBLIC_DEVICE_ADDRESS));
  }
}

void SecurityManagerImpl::OnHciLeEvent(hci::LeMetaEventView event) {
  hci::SubeventCode code = event.GetSubeventCode();

  if (code == hci::SubeventCode::LONG_TERM_KEY_REQUEST) {
    hci::LeLongTermKeyRequestView le_long_term_key_request_view = hci::LeLongTermKeyRequestView::Create(event);
    if (!le_long_term_key_request_view.IsValid()) {
      LOG_ERROR("Invalid LeLongTermKeyRequestView packet received");
      return;
    }

    if (le_long_term_key_request_view.GetConnectionHandle() == pending_le_pairing_.connection_handle_) {
      pending_le_pairing_.handler_->OnHciLeEvent(event);
      return;
    }

    LOG_INFO("Unhandled HCI LE security event, code %s", hci::SubeventCodeText(code).c_str());
    return;
  }

  // hci::SubeventCode::READ_LOCAL_P256_PUBLIC_KEY_COMPLETE,
  // hci::SubeventCode::GENERATE_DHKEY_COMPLETE,
  LOG_ERROR("Unhandled HCI LE security event, code %s", hci::SubeventCodeText(code).c_str());
}

void SecurityManagerImpl::OnPairingPromptAccepted(const bluetooth::hci::AddressWithType& address, bool confirmed) {
  auto entry = pairing_handler_map_.find(address.GetAddress());
  if (entry != pairing_handler_map_.end()) {
    entry->second->OnPairingPromptAccepted(address, confirmed);
  } else {
    if (pending_le_pairing_.address_ == address) {
      pending_le_pairing_.handler_->OnUiAction(PairingEvent::UI_ACTION_TYPE::PAIRING_ACCEPTED, confirmed);
    }
  }
}

void SecurityManagerImpl::OnConfirmYesNo(const bluetooth::hci::AddressWithType& address, bool confirmed) {
  auto entry = pairing_handler_map_.find(address.GetAddress());
  if (entry != pairing_handler_map_.end()) {
    entry->second->OnConfirmYesNo(address, confirmed);
  } else {
    if (pending_le_pairing_.address_ == address) {
      pending_le_pairing_.handler_->OnUiAction(PairingEvent::UI_ACTION_TYPE::CONFIRM_YESNO, confirmed);
    }
  }
}

void SecurityManagerImpl::OnPasskeyEntry(const bluetooth::hci::AddressWithType& address, uint32_t passkey) {
  auto entry = pairing_handler_map_.find(address.GetAddress());
  if (entry != pairing_handler_map_.end()) {
    entry->second->OnPasskeyEntry(address, passkey);
  } else {
    if (pending_le_pairing_.address_ == address) {
      pending_le_pairing_.handler_->OnUiAction(PairingEvent::UI_ACTION_TYPE::PASSKEY, passkey);
    }
  }
}

void SecurityManagerImpl::OnPinEntry(const bluetooth::hci::AddressWithType& address, std::vector<uint8_t> pin) {
  auto entry = pairing_handler_map_.find(address.GetAddress());
  if (entry != pairing_handler_map_.end()) {
    LOG_INFO("PIN for %s", address.ToString().c_str());
    entry->second->OnPinEntry(address, pin);
  } else {
    LOG_WARN("No handler found for PIN for %s", address.ToString().c_str());
    // TODO(jpawlowski): Implement LE version
  }
}

void SecurityManagerImpl::OnPairingHandlerComplete(hci::Address address, PairingResultOrFailure status) {
  auto entry = pairing_handler_map_.find(address);
  if (entry != pairing_handler_map_.end()) {
    pairing_handler_map_.erase(entry);
    security_manager_channel_->Release(address);
  }
  auto remote = hci::AddressWithType(address, hci::AddressType::PUBLIC_DEVICE_ADDRESS);
  if (!std::holds_alternative<PairingFailure>(status)) {
    NotifyDeviceBonded(remote);
  } else {
    NotifyDeviceBondFailed(remote, std::get<PairingFailure>(status));
  }
  auto record = this->security_database_.FindOrCreate(remote);
  record->CancelPairing();
  security_database_.SaveRecordsToStorage();
  // Only call update link if we need to
  auto policy_callback_entry = enforce_security_policy_callback_map_.find(remote);
  if (policy_callback_entry != enforce_security_policy_callback_map_.end()) {
    UpdateLinkSecurityCondition(remote);
  }
}

void SecurityManagerImpl::OnL2capRegistrationCompleteLe(
    l2cap::le::FixedChannelManager::RegistrationResult result,
    std::unique_ptr<l2cap::le::FixedChannelService> le_smp_service) {
  ASSERT_LOG(result == bluetooth::l2cap::le::FixedChannelManager::RegistrationResult::SUCCESS,
             "Failed to register to LE SMP Fixed Channel Service");
}

LeFixedChannelEntry* SecurityManagerImpl::FindStoredLeChannel(const hci::AddressWithType& device) {
  for (LeFixedChannelEntry& storage : all_channels_) {
    if (storage.channel_->GetDevice() == device) {
      return &storage;
    }
  }
  return nullptr;
}

bool SecurityManagerImpl::EraseStoredLeChannel(const hci::AddressWithType& device) {
  for (auto it = all_channels_.begin(); it != all_channels_.end(); it++) {
    if (it->channel_->GetDevice() == device) {
      all_channels_.erase(it);
      return true;
    }
  }
  return false;
}

void SecurityManagerImpl::OnSmpCommandLe(hci::AddressWithType device) {
  LeFixedChannelEntry* stored_chan = FindStoredLeChannel(device);
  if (!stored_chan) {
    LOG_ALWAYS_FATAL("Received SMP command for unknown channel");
    return;
  }

  std::unique_ptr<l2cap::le::FixedChannel>& channel = stored_chan->channel_;

  auto packet = channel->GetQueueUpEnd()->TryDequeue();
  if (!packet) {
    LOG_ERROR("Received dequeue, but no data ready...");
    return;
  }

  // Pending pairing - pass the data to the handler
  auto temp_cmd_view = CommandView::Create(*packet);
  if (pending_le_pairing_.address_ == device) {
    pending_le_pairing_.handler_->OnCommandView(temp_cmd_view);
    return;
  }

  // no pending pairing attempt
  if (!temp_cmd_view.IsValid()) {
    LOG_ERROR("Invalid Command packet");
    return;
  }

  if (temp_cmd_view.GetCode() == Code::SECURITY_REQUEST) {
    // TODO: either start encryption or pairing
    LOG_WARN("Unhandled security request!!!");
    return;
  }

  auto my_role = channel->GetLinkOptions()->GetRole();
  if (temp_cmd_view.GetCode() == Code::PAIRING_REQUEST && my_role == hci::Role::PERIPHERAL) {
    // TODO: if (pending_le_pairing_) { do not start another }

    LOG_INFO("start of security request handling!");

    stored_chan->channel_->Acquire();

    PairingRequestView pairing_request = PairingRequestView::Create(temp_cmd_view);
    auto& enqueue_buffer = stored_chan->enqueue_buffer_;

    std::optional<InitialInformations::out_of_band_data> remote_oob_data = std::nullopt;
    if (remote_oob_data_address_.has_value() && remote_oob_data_address_.value() == channel->GetDevice())
      remote_oob_data = InitialInformations::out_of_band_data{.le_sc_c = remote_oob_data_le_sc_c_.value(),
                                                              .le_sc_r = remote_oob_data_le_sc_r_.value()};

    // TODO: this doesn't have to be a unique ptr, if there is a way to properly std::move it into place where it's
    // stored
    pending_le_pairing_.connection_handle_ = channel->GetLinkOptions()->GetHandle();
    InitialInformations initial_informations{
        .my_role = my_role,
        .my_connection_address = channel->GetLinkOptions()->GetLocalAddress(),
        .my_identity_address = local_identity_address_,
        .my_identity_resolving_key = local_identity_resolving_key_,
        /*TODO: properly obtain capabilities from device-specific storage*/
        .myPairingCapabilities = {.io_capability = local_le_io_capability_,
                                  .oob_data_flag = local_le_oob_data_present_,
                                  .auth_req = local_le_auth_req_,
                                  .maximum_encryption_key_size = local_maximum_encryption_key_size_,
                                  .initiator_key_distribution = 0x07,
                                  .responder_key_distribution = 0x07},
        .remotely_initiated = true,
        .connection_handle = channel->GetLinkOptions()->GetHandle(),
        .remote_connection_address = channel->GetDevice(),
        .remote_name = "TODO: grab proper device name in sec mgr",
        /* contains pairing request, if the pairing was remotely initiated */
        .pairing_request = pairing_request,
        .remote_oob_data = remote_oob_data,
        .my_oob_data = local_le_oob_data_,
        /* Used by Pairing Handler to present user with requests*/
        .user_interface = user_interface_,
        .user_interface_handler = user_interface_handler_,

        /* HCI interface to use */
        .le_security_interface = hci_security_interface_le_,
        .proper_l2cap_interface = enqueue_buffer.get(),
        .l2cap_handler = security_handler_,
        /* Callback to execute once the Pairing process is finished */
        // TODO: make it an common::OnceCallback ?
        .OnPairingFinished = std::bind(&SecurityManagerImpl::OnPairingFinished, this, std::placeholders::_1),
    };
    pending_le_pairing_.address_ = device;
    pending_le_pairing_.handler_ = std::make_unique<PairingHandlerLe>(PairingHandlerLe::PHASE1, initial_informations);
  }
}

void SecurityManagerImpl::OnConnectionOpenLe(std::unique_ptr<l2cap::le::FixedChannel> channel_param) {
  auto enqueue_buffer_temp =
      std::make_unique<os::EnqueueBuffer<packet::BasePacketBuilder>>(channel_param->GetQueueUpEnd());

  all_channels_.push_back({std::move(channel_param), std::move(enqueue_buffer_temp)});
  auto& stored_channel = all_channels_.back();
  auto& channel = stored_channel.channel_;

  channel->RegisterOnCloseCallback(
      security_handler_,
      common::BindOnce(&SecurityManagerImpl::OnConnectionClosedLe, common::Unretained(this), channel->GetDevice()));
  channel->GetQueueUpEnd()->RegisterDequeue(
      security_handler_,
      common::Bind(&SecurityManagerImpl::OnSmpCommandLe, common::Unretained(this), channel->GetDevice()));

  if (pending_le_pairing_.address_ != channel->GetDevice()) {
    return;
  }

  ConnectionIsReadyStartPairing(&stored_channel);
}

void SecurityManagerImpl::ConnectionIsReadyStartPairing(LeFixedChannelEntry* stored_channel) {
  auto& channel = stored_channel->channel_;
  auto& enqueue_buffer = stored_channel->enqueue_buffer_;

  stored_channel->channel_->Acquire();

  std::optional<InitialInformations::out_of_band_data> remote_oob_data = std::nullopt;
  if (remote_oob_data_address_.has_value() && remote_oob_data_address_.value() == channel->GetDevice())
    remote_oob_data = InitialInformations::out_of_band_data{.le_sc_c = remote_oob_data_le_sc_c_.value(),
                                                            .le_sc_r = remote_oob_data_le_sc_r_.value()};

  // TODO: this doesn't have to be a unique ptr, if there is a way to properly std::move it into place where it's stored
  pending_le_pairing_.connection_handle_ = channel->GetLinkOptions()->GetHandle();
  InitialInformations initial_informations{
      .my_role = channel->GetLinkOptions()->GetRole(),
      .my_connection_address = channel->GetLinkOptions()->GetLocalAddress(),
      .my_identity_address = local_identity_address_,
      .my_identity_resolving_key = local_identity_resolving_key_,
      /*TODO: properly obtain capabilities from device-specific storage*/
      .myPairingCapabilities = {.io_capability = local_le_io_capability_,
                                .oob_data_flag = local_le_oob_data_present_,
                                .auth_req = local_le_auth_req_,
                                .maximum_encryption_key_size = local_maximum_encryption_key_size_,
                                .initiator_key_distribution = 0x07,
                                .responder_key_distribution = 0x07},
      .remotely_initiated = false,
      .connection_handle = channel->GetLinkOptions()->GetHandle(),
      .remote_connection_address = channel->GetDevice(),
      .remote_name = "TODO: grab proper device name in sec mgr",
      /* contains pairing request, if the pairing was remotely initiated */
      .pairing_request = std::nullopt,  // TODO: handle remotely initiated pairing in SecurityManager properly
      .remote_oob_data = remote_oob_data,
      .my_oob_data = local_le_oob_data_,
      /* Used by Pairing Handler to present user with requests*/
      .user_interface = user_interface_,
      .user_interface_handler = user_interface_handler_,

      /* HCI interface to use */
      .le_security_interface = hci_security_interface_le_,
      .proper_l2cap_interface = enqueue_buffer.get(),
      .l2cap_handler = security_handler_,
      /* Callback to execute once the Pairing process is finished */
      // TODO: make it an common::OnceCallback ?
      .OnPairingFinished = std::bind(&SecurityManagerImpl::OnPairingFinished, this, std::placeholders::_1),
  };
  pending_le_pairing_.handler_ = std::make_unique<PairingHandlerLe>(PairingHandlerLe::PHASE1, initial_informations);
}

void SecurityManagerImpl::OnConnectionClosedLe(hci::AddressWithType address, hci::ErrorCode error_code) {
  if (pending_le_pairing_.address_ != address) {
    LeFixedChannelEntry* stored_chan = FindStoredLeChannel(address);
    if (!stored_chan) {
      LOG_ALWAYS_FATAL("Received connection closed for unknown channel");
      return;
    }
    stored_chan->channel_->GetQueueUpEnd()->UnregisterDequeue();
    stored_chan->enqueue_buffer_.reset();
    EraseStoredLeChannel(address);
    return;
  }
  pending_le_pairing_.handler_->SendExitSignal();
  NotifyDeviceBondFailed(address, PairingFailure("Connection closed"));
}

void SecurityManagerImpl::OnConnectionFailureLe(bluetooth::l2cap::le::FixedChannelManager::ConnectionResult result) {
  if (result.connection_result_code ==
      bluetooth::l2cap::le::FixedChannelManager::ConnectionResultCode::FAIL_ALL_SERVICES_HAVE_CHANNEL) {
    // TODO: already connected
  }

  // This callback is invoked only for devices we attempted to connect to.
  NotifyDeviceBondFailed(pending_le_pairing_.address_, PairingFailure("Connection establishment failed"));
}

SecurityManagerImpl::SecurityManagerImpl(
    os::Handler* security_handler,
    l2cap::le::L2capLeModule* l2cap_le_module,
    channel::SecurityManagerChannel* security_manager_channel,
    hci::HciLayer* hci_layer,
    hci::AclManager* acl_manager,
    hci::Controller* controller,
    storage::StorageModule* storage_module,
    neighbor::NameDbModule* name_db_module)
    : security_handler_(security_handler),
      l2cap_le_module_(l2cap_le_module),
      l2cap_manager_le_(l2cap_le_module_->GetFixedChannelManager()),
      hci_security_interface_le_(
          hci_layer->GetLeSecurityInterface(security_handler_->BindOn(this, &SecurityManagerImpl::OnHciLeEvent))),
      security_manager_channel_(security_manager_channel),
      acl_manager_(acl_manager),
      controller_(controller),
      storage_module_(storage_module),
      security_record_storage_(storage_module, security_handler),
      security_database_(security_record_storage_),
      name_db_module_(name_db_module) {
  Init();

  l2cap_manager_le_->RegisterService(
      bluetooth::l2cap::kSmpCid,
      common::BindOnce(&SecurityManagerImpl::OnL2capRegistrationCompleteLe, common::Unretained(this)),
      common::Bind(&SecurityManagerImpl::OnConnectionOpenLe, common::Unretained(this)), security_handler_);
}

void SecurityManagerImpl::OnPairingFinished(security::PairingResultOrFailure pairing_result) {
  LOG_INFO(" ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Received pairing result");

  LeFixedChannelEntry* stored_chan = FindStoredLeChannel(pending_le_pairing_.address_);
  if (stored_chan) {
    stored_chan->channel_->Release();
  }

  if (std::holds_alternative<PairingFailure>(pairing_result)) {
    PairingFailure failure = std::get<PairingFailure>(pairing_result);
    LOG_INFO(" ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ failure message: %s",
             failure.message.c_str());
    if (stored_chan) {
      NotifyDeviceBondFailed(stored_chan->channel_->GetDevice(), failure);
    }
    return;
  }

  auto result = std::get<PairingResult>(pairing_result);
  LOG_INFO("Pairing with %s was successful", result.connection_address.ToString().c_str());

  // TODO: ensure that the security level is not weaker than what we already have.
  auto record = this->security_database_.FindOrCreate(result.connection_address);
  record->identity_address_ = result.distributed_keys.remote_identity_address;
  record->remote_ltk = result.distributed_keys.remote_ltk;
  record->key_size = result.key_size;
  record->security_level = result.security_level;
  record->remote_ediv = result.distributed_keys.remote_ediv;
  record->remote_rand = result.distributed_keys.remote_rand;
  record->remote_irk = result.distributed_keys.remote_irk;
  record->remote_signature_key = result.distributed_keys.remote_signature_key;
  if (result.distributed_keys.remote_link_key)
    record->SetLinkKey(*result.distributed_keys.remote_link_key, hci::KeyType::AUTHENTICATED_P256);
  security_database_.SaveRecordsToStorage();

  NotifyDeviceBonded(result.connection_address);
  // We also notify bond complete using identity address. That's what old stack used to do.
  if (result.distributed_keys.remote_identity_address)
    NotifyDeviceBonded(*result.distributed_keys.remote_identity_address);

  security_handler_->CallOn(this, &SecurityManagerImpl::WipeLePairingHandler);
}

void SecurityManagerImpl::WipeLePairingHandler() {
  pending_le_pairing_.handler_.reset();
  pending_le_pairing_.connection_handle_ = 0;
  pending_le_pairing_.address_ = hci::AddressWithType();
}

// Facade Configuration API functions
void SecurityManagerImpl::SetDisconnectCallback(FacadeDisconnectCallback callback) {
  this->facade_disconnect_callback_ = std::make_optional<FacadeDisconnectCallback>(callback);
}

void SecurityManagerImpl::SetIoCapability(hci::IoCapability io_capability) {
  this->local_io_capability_ = io_capability;
}

void SecurityManagerImpl::SetLeIoCapability(security::IoCapability io_capability) {
  this->local_le_io_capability_ = io_capability;
}

void SecurityManagerImpl::SetLeAuthRequirements(uint8_t auth_req) {
  this->local_le_auth_req_ = auth_req;
}

void SecurityManagerImpl::SetLeMaximumEncryptionKeySize(uint8_t maximum_encryption_key_size) {
  this->local_maximum_encryption_key_size_ = maximum_encryption_key_size;
}

void SecurityManagerImpl::SetLeOobDataPresent(OobDataFlag data_present) {
  this->local_le_oob_data_present_ = data_present;
}

void SecurityManagerImpl::GetOutOfBandData(channel::SecurityCommandStatusCallback callback) {
  this->security_manager_channel_->SendCommand(
      hci::ReadLocalOobDataBuilder::Create(), std::forward<channel::SecurityCommandStatusCallback>(callback));
}

void SecurityManagerImpl::GetLeOutOfBandData(
    std::array<uint8_t, 16>* confirmation_value, std::array<uint8_t, 16>* random_value) {
  local_le_oob_data_ = std::make_optional<MyOobData>(PairingHandlerLe::GenerateOobData());
  *confirmation_value = local_le_oob_data_.value().c;
  *random_value = local_le_oob_data_.value().r;
}

void SecurityManagerImpl::SetOutOfBandData(
    hci::AddressWithType remote_address,
    std::array<uint8_t, 16> confirmation_value,
    std::array<uint8_t, 16> random_value) {
  remote_oob_data_address_ = remote_address;
  remote_oob_data_le_sc_c_ = confirmation_value;
  remote_oob_data_le_sc_r_ = random_value;
}

void SecurityManagerImpl::SetAuthenticationRequirements(hci::AuthenticationRequirements authentication_requirements) {
  this->local_authentication_requirements_ = authentication_requirements;
}

void SecurityManagerImpl::InternalEnforceSecurityPolicy(
    hci::AddressWithType remote,
    l2cap::classic::SecurityPolicy policy,
    l2cap::classic::SecurityEnforcementInterface::ResultCallback result_callback) {
  if (IsSecurityRequirementSatisfied(remote, policy)) {
    // Notify client immediately if already satisfied
    std::move(result_callback).Invoke(true);
    return;
  }

  // At this point we don't meet the security requirements; must pair
  auto record = this->security_database_.FindOrCreate(remote);
  hci::AuthenticationRequirements authentication_requirements = kDefaultAuthenticationRequirements;
  enforce_security_policy_callback_map_[remote] = {policy, std::move(result_callback)};

  switch (policy) {
    case l2cap::classic::SecurityPolicy::BEST:
    case l2cap::classic::SecurityPolicy::AUTHENTICATED_ENCRYPTED_TRANSPORT:
      // Force MITM requirement locally
      authentication_requirements = hci::AuthenticationRequirements::GENERAL_BONDING_MITM_PROTECTION;
      break;
    case l2cap::classic::SecurityPolicy::ENCRYPTED_TRANSPORT:
      authentication_requirements = hci::AuthenticationRequirements::GENERAL_BONDING;
      break;
    default:
      // I could hear the voice of Myles, "This should be an ASSERT!"
      ASSERT_LOG(false, "Unreachable code path");
      return;
  }

  LOG_WARN("Dispatch #3");
  DispatchPairingHandler(
      record,
      true,
      this->local_io_capability_,
      std::as_const(authentication_requirements),
      pairing::OobData(),
      pairing::OobData());
}

void SecurityManagerImpl::UpdateLinkSecurityCondition(hci::AddressWithType remote) {
  auto entry = enforce_security_policy_callback_map_.find(remote);
  if (entry == enforce_security_policy_callback_map_.end()) {
    LOG_ERROR("No L2CAP security policy callback pending for %s", remote.ToString().c_str());
    return;
  }
  std::move(entry->second.callback_).Invoke(IsSecurityRequirementSatisfied(remote, entry->second.policy_));
  enforce_security_policy_callback_map_.erase(entry);
}

bool SecurityManagerImpl::IsSecurityRequirementSatisfied(
    hci::AddressWithType remote, l2cap::classic::SecurityPolicy policy) {
  auto record = security_database_.FindOrCreate(remote);
  switch (policy) {
    case l2cap::classic::SecurityPolicy::BEST:
    case l2cap::classic::SecurityPolicy::AUTHENTICATED_ENCRYPTED_TRANSPORT:
      return (record->IsPaired() && record->IsAuthenticated());
    case l2cap::classic::SecurityPolicy::ENCRYPTED_TRANSPORT:
      return record->IsPaired();
    default:
      return true;
  }
}

void SecurityManagerImpl::EnforceSecurityPolicy(
    hci::AddressWithType remote,
    l2cap::classic::SecurityPolicy policy,
    l2cap::classic::SecurityEnforcementInterface::ResultCallback result_callback) {
  LOG_INFO("Attempting to enforce security policy");
  auto record = security_database_.FindOrCreate(remote);
  if (!record->IsPairing()) {
    this->InternalEnforceSecurityPolicy(remote, policy, std::move(result_callback));
  }
}

void SecurityManagerImpl::EnforceLeSecurityPolicy(
    hci::AddressWithType remote, l2cap::le::SecurityPolicy policy,
    l2cap::le::SecurityEnforcementInterface::ResultCallback result_callback) {
  bool result = false;
  // TODO(jpawlowski): Implement for LE
  switch (policy) {
    case l2cap::le::SecurityPolicy::BEST:
      break;
    case l2cap::le::SecurityPolicy::AUTHENTICATED_ENCRYPTED_TRANSPORT:
      break;
    case l2cap::le::SecurityPolicy::ENCRYPTED_TRANSPORT:
      break;
    case l2cap::le::SecurityPolicy::NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK:
      result = true;
      break;
    case l2cap::le::SecurityPolicy::_NOT_FOR_YOU__AUTHENTICATED_PAIRING_WITH_128_BIT_KEY:
      break;
    case l2cap::le::SecurityPolicy::_NOT_FOR_YOU__AUTHORIZATION:
      break;
  }
  result_callback.Invoke(result);
}
}  // namespace internal
}  // namespace security
}  // namespace bluetooth