@@ -752,6 +752,47 @@ static void smp_sign_info_sent(struct bt_conn *conn, void *user_data, int err)
752
752
#endif /* CONFIG_BT_SIGNING */
753
753
754
754
#if defined(CONFIG_BT_CLASSIC )
755
+ static bool ltk_derive_link_key_allowed (struct bt_smp * smp )
756
+ {
757
+ struct bt_conn * conn ;
758
+ struct bt_keys_link_key * link_key ;
759
+ struct bt_keys * keys ;
760
+
761
+ if (!smp -> chan .chan .conn ) {
762
+ return false;
763
+ }
764
+
765
+ conn = smp -> chan .chan .conn ;
766
+ keys = conn -> le .keys ;
767
+ if (keys == NULL ) {
768
+ return false;
769
+ }
770
+
771
+ /* Check whether it is has been bonded */
772
+ link_key = bt_keys_find_link_key (& conn -> le .dst .a );
773
+ if (link_key == NULL ) {
774
+ return true;
775
+ }
776
+
777
+ if (link_key -> flags & BT_LINK_KEY_DEBUG ) {
778
+ LOG_DBG ("Debug LK can be overwrote" );
779
+ return true;
780
+ }
781
+
782
+ if ((link_key -> flags & BT_LINK_KEY_AUTHENTICATED ) &&
783
+ ((keys -> flags & BT_KEYS_AUTHENTICATED ) == 0 )) {
784
+ LOG_DBG ("Stronger LK (MITM) cannot be overwrote by weaker LTK" );
785
+ return false;
786
+ }
787
+
788
+ if ((link_key -> flags & BT_LINK_KEY_SC ) && ((keys -> flags & BT_KEYS_SC ) == 0 )) {
789
+ LOG_DBG ("Stronger LK (SC) cannot be overwrote by weaker LTK" );
790
+ return false;
791
+ }
792
+
793
+ return true;
794
+ }
795
+
755
796
static void sc_derive_link_key (struct bt_smp * smp )
756
797
{
757
798
/* constants as specified in Core Spec Vol.3 Part H 2.4.2.4 */
@@ -764,6 +805,11 @@ static void sc_derive_link_key(struct bt_smp *smp)
764
805
765
806
/* TODO handle errors? */
766
807
808
+ if (!ltk_derive_link_key_allowed (smp )) {
809
+ LOG_DBG ("LK cannot be derived by LTK" );
810
+ return ;
811
+ }
812
+
767
813
/*
768
814
* At this point remote device identity is known so we can use
769
815
* destination address here
0 commit comments