Skip to content

Commit 2a53a5f

Browse files
committed
add Pairing setIOCaps() and setMITM()
1 parent 7b43260 commit 2a53a5f

File tree

2 files changed

+69
-35
lines changed

2 files changed

+69
-35
lines changed

libraries/Bluefruit52Lib/src/BLEPairing.cpp

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
//--------------------------------------------------------------------+
2828
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
2929
//--------------------------------------------------------------------+
30-
#define EDIV_INVALID 0xFFFF
30+
#ifdef NRF_CRYPTOCELL
31+
#define LESC_SUPPORTED 1
32+
#else
33+
#define LESC_SUPPORTED 0
34+
#endif
3135

3236
//------------- IMPLEMENTATION -------------//
3337

@@ -46,26 +50,24 @@ static void swap_endian(uint8_t data[], uint32_t nbytes)
4650
}
4751
}
4852

49-
BLEPairing::BLEPairing(void)
53+
// default is Just Work
54+
static const ble_gap_sec_params_t _sec_param_default =
5055
{
51-
_sec_param = ((ble_gap_sec_params_t)
52-
{
53-
.bond = 1,
54-
.mitm = 0,
55-
.lesc = 0,
56-
.keypress = 0,
57-
.io_caps = BLE_GAP_IO_CAPS_NONE,
58-
.oob = 0,
59-
.min_key_size = 7,
60-
.max_key_size = 16,
61-
.kdist_own = { .enc = 1, .id = 1},
62-
.kdist_peer = { .enc = 1, .id = 1},
63-
});
64-
65-
#ifdef NRF_CRYPTOCELL
66-
// _sec_param.lesc = 1; // enable LESC if CryptoCell is present
67-
#endif
56+
.bond = 1,
57+
.mitm = 0,
58+
.lesc = LESC_SUPPORTED,
59+
.keypress = 0,
60+
.io_caps = BLE_GAP_IO_CAPS_NONE,
61+
.oob = 0,
62+
.min_key_size = 7,
63+
.max_key_size = 16,
64+
.kdist_own = { .enc = 1, .id = 1},
65+
.kdist_peer = { .enc = 1, .id = 1}
66+
};
6867

68+
BLEPairing::BLEPairing(void)
69+
{
70+
_sec_param = _sec_param_default;
6971
_passkey_cb = NULL;
7072
_complete_cb = NULL;
7173
}
@@ -100,6 +102,39 @@ bool BLEPairing::begin(void)
100102
return true;
101103
}
102104

105+
void BLEPairing::setIOCaps(bool display, bool keyboard, bool yes_no)
106+
{
107+
uint8_t io_caps = BLE_GAP_IO_CAPS_NONE;
108+
109+
if (display)
110+
{
111+
if (keyboard)
112+
{
113+
io_caps = BLE_GAP_IO_CAPS_KEYBOARD_DISPLAY;
114+
}
115+
else if (yes_no)
116+
{
117+
io_caps = BLE_GAP_IO_CAPS_DISPLAY_YESNO;
118+
}else
119+
{
120+
io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
121+
}
122+
}else
123+
{
124+
if (keyboard) io_caps = BLE_GAP_IO_CAPS_KEYBOARD_ONLY;
125+
}
126+
127+
_sec_param.io_caps = io_caps;
128+
129+
// also set Man in the middle protection if we have some IO caps
130+
if (io_caps != BLE_GAP_IO_CAPS_NONE) _sec_param.mitm = 1;
131+
}
132+
133+
void BLEPairing::setMITM(bool enabled)
134+
{
135+
_sec_param.mitm = (enabled ? 1 : 0);
136+
}
137+
103138
/* Resolvable Address = Hash (24 bit) | Random (24 bit)
104139
* in which
105140
* - Hash = AES(random) using IRK
@@ -136,16 +171,13 @@ bool BLEPairing::resolveAddress(ble_gap_addr_t const * p_addr, ble_gap_irk_t con
136171
// Use Legacy SC static Passkey
137172
bool BLEPairing::setPIN(const char* pin)
138173
{
139-
// back to open mode
140174
if (pin == NULL)
141175
{
142-
_sec_param.bond = 1;
143-
_sec_param.mitm = 0;
144-
_sec_param.lesc = 0; // TODO NRF_CRYPTOCELL
145-
_sec_param.io_caps = BLE_GAP_IO_CAPS_NONE;
176+
// back to default mode
177+
_sec_param = _sec_param_default;
146178
}else
147179
{
148-
VERIFY ( strlen(pin) == BLE_GAP_PASSKEY_LEN );
180+
VERIFY(strlen(pin) == BLE_GAP_PASSKEY_LEN);
149181

150182
// Static Passkey requires using
151183
// - Legacy SC
@@ -158,7 +190,7 @@ bool BLEPairing::setPIN(const char* pin)
158190

159191
ble_opt_t opt;
160192
opt.gap_opt.passkey.p_passkey = (const uint8_t*) pin;
161-
VERIFY_STATUS( sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opt), false);
193+
VERIFY_STATUS(sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opt), false);
162194
}
163195

164196
return true;
@@ -171,20 +203,14 @@ bool BLEPairing::setPasskeyCallback(pair_passkey_cb_t fp)
171203

172204
if ( fp == NULL )
173205
{
174-
// TODO callback clear
206+
// back to default mode
207+
_sec_param = _sec_param_default;
175208
}else
176209
{
177210
_sec_param.bond = 1;
178211
_sec_param.mitm = 1;
179-
180-
// TODO NRF_CRYPTOCELL
181-
// _sec_param.lesc = 0;
182-
_sec_param.lesc = 1;
212+
_sec_param.lesc = LESC_SUPPORTED;
183213
_sec_param.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
184-
185-
ble_opt_t opt;
186-
opt.gap_opt.passkey.p_passkey = NULL; // generate Passkey randomly
187-
VERIFY_STATUS( sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opt), false);
188214
}
189215

190216
return true;

libraries/Bluefruit52Lib/src/BLEPairing.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,21 @@ class BLEPairing
4545
// Use static Passkey (Legacy SC)
4646
bool setPIN(const char* pin);
4747

48+
// Set IO capacities
49+
void setIOCaps(bool display, bool keyboard, bool yes_no);
50+
51+
// Enable/Disable Man in the middle protection
52+
void setMITM(bool enabled);
53+
4854
// resolve address with IRK to see if it matches
4955
bool resolveAddress(ble_gap_addr_t const * p_addr, ble_gap_irk_t const * irk);
5056

5157
//------------- Callbacks -------------//
5258
bool setPasskeyCallback(pair_passkey_cb_t fp);
5359
void setCompleteCallback(pair_complete_cb_t fp);
5460

61+
62+
5563
/*------------------------------------------------------------------*/
5664
/* INTERNAL USAGE ONLY
5765
* Although declare as public, it is meant to be invoked by internal

0 commit comments

Comments
 (0)