@@ -752,6 +752,47 @@ static void smp_sign_info_sent(struct bt_conn *conn, void *user_data, int err)
752752#endif /* CONFIG_BT_SIGNING */
753753
754754#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+
755796static void sc_derive_link_key (struct bt_smp * smp )
756797{
757798 /* 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)
764805
765806 /* TODO handle errors? */
766807
808+ if (!ltk_derive_link_key_allowed (smp )) {
809+ LOG_DBG ("LK cannot be derived by LTK" );
810+ return ;
811+ }
812+
767813 /*
768814 * At this point remote device identity is known so we can use
769815 * destination address here
0 commit comments