Skip to content

Commit

Permalink
Merge pull request #3 from pan-/palsm-interface-improvement
Browse files Browse the repository at this point in the history
Palsm interface improvement
  • Loading branch information
paul-szczepanek-arm authored Jan 16, 2018
2 parents f93ed10 + f71490b commit cc41857
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 128 deletions.
213 changes: 104 additions & 109 deletions features/FEATURE_BLE/ble/SecurityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,108 @@ class SecurityManager {
typedef FunctionPointerWithContext<const SecurityManager *> SecurityManagerShutdownCallback_t;
typedef CallChainOfFunctionPointersWithContext<const SecurityManager *> SecurityManagerShutdownCallbackChain_t;

public:
/* subclass to override handlers */
class SecurityManagerEventHandler {
public:
SecurityManagerEventHandler() {};
virtual ~SecurityManagerEventHandler() {};

virtual void securitySetupInitiated(connection_handle_t handle, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) {
(void)handle;
(void)allowBonding;
(void)requireMITM;
(void)iocaps;
};
virtual void securitySetupCompleted(connection_handle_t handle, SecurityManager::SecurityCompletionStatus_t status) {
(void)handle;
(void)status;
};
virtual void linkSecured(connection_handle_t handle, SecurityManager::SecurityMode_t securityMode) {
(void)handle;
(void)securityMode;
};
virtual void securityContextStored(connection_handle_t handle) {
(void)handle;
}
virtual void passkeyDisplay(connection_handle_t handle, const SecurityManager::Passkey_t passkey) {
(void)handle;
(void)passkey;
};
virtual void validMicTimeout(connection_handle_t handle) {
(void)handle;
};
virtual void linkKeyFailure(connection_handle_t handle) {
(void)handle;
};
virtual void keypressNotification(connection_handle_t handle, SecurityManager::Keypress_t keypress) {
(void)handle;
(void)keypress;
};
virtual void legacyPairingOobRequest(connection_handle_t handle) {
(void)handle;
};
virtual void oobRequest(connection_handle_t handle) {
(void)handle;
};
virtual void pinRequest(connection_handle_t handle) {
(void)handle;
};
virtual void passkeyRequest(connection_handle_t handle) {
(void)handle;
};
virtual void confirmationRequest(connection_handle_t handle) {
(void)handle;
};
virtual void acceptPairingRequest(connection_handle_t handle) {
(void)handle;
};
};

private:
/* legacy compatibility with old callbacks (from both sides, so combination of new and old works) */
class LegacySecurityManagerEventHandler : public SecurityManagerEventHandler {
public:
LegacySecurityManagerEventHandler() :
securitySetupInitiatedCallback(),
securitySetupCompletedCallback(),
linkSecuredCallback(),
securityContextStoredCallback(),
passkeyDisplayCallback() { };

void securitySetupInitiated(connection_handle_t handle, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) {
if (securitySetupInitiatedCallback) {
securitySetupInitiatedCallback(handle, allowBonding, requireMITM, iocaps);
}
};
void securitySetupCompleted(connection_handle_t handle, SecurityManager::SecurityCompletionStatus_t status) {
if (securitySetupCompletedCallback) {
securitySetupCompletedCallback(handle, status);
}
};
void linkSecured(connection_handle_t handle, SecurityManager::SecurityMode_t securityMode) {
if (linkSecuredCallback) {
linkSecuredCallback(handle, securityMode);
}
};
void securityContextStored(connection_handle_t handle) {
if (securityContextStoredCallback) {
securityContextStoredCallback(handle);
}
}
void passkeyDisplay(connection_handle_t handle, const SecurityManager::Passkey_t passkey) {
if (passkeyDisplayCallback) {
passkeyDisplayCallback(handle, passkey);
}
};

SecurityManager::SecuritySetupInitiatedCallback_t securitySetupInitiatedCallback;
SecurityManager::SecuritySetupCompletedCallback_t securitySetupCompletedCallback;
SecurityManager::LinkSecuredCallback_t linkSecuredCallback;
SecurityManager::HandleSpecificEvent_t securityContextStoredCallback;
SecurityManager::PasskeyDisplayCallback_t passkeyDisplayCallback;
};

/*
* The following functions are meant to be overridden in the platform-specific sub-class.
*/
Expand Down Expand Up @@ -157,7 +259,7 @@ class SecurityManager {
shutdownCallChain.clear();
if (eventHandler != &defaultEventHandler) {
delete eventHandler;
eventHandler = defaultEventHandler;
eventHandler = &defaultEventHandler;
}

return BLE_ERROR_NONE;
Expand Down Expand Up @@ -219,14 +321,6 @@ class SecurityManager {
// Security settings
//

virtual ble_error_t setPinCode(uint8_t pinLength, uint8_t * pinCode, bool isStatic = false) {
(void) pinLength;
(void) pinCode;
(void) isStatic;

return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */
}

virtual ble_error_t setPasskey(const Passkey_t passkey) {
(void) passkey;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */
Expand Down Expand Up @@ -493,107 +587,8 @@ class SecurityManager {
SecurityManagerEventHandler* eventHandler;
SecurityManagerShutdownCallbackChain_t shutdownCallChain;

protected:
LegacySecurityManagerEventHandler defaultEventHandler;
};

/* subclass to override handlers */
class SecurityManagerEventHandler {
public:
SecurityManagerEventHandler() {};
virtual ~SecurityManagerEventHandler() {};

virtual void securitySetupInitiated(connection_handle_t handle, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) {
(void)handle;
(void)allowBonding;
(void)requireMITM;
(void)iocaps;
};
virtual void securitySetupCompleted(connection_handle_t handle, SecurityManager::SecurityCompletionStatus_t status) {
(void)handle;
(void)status;
};
virtual void linkSecured(connection_handle_t handle, SecurityManager::SecurityMode_t securityMode) {
(void)handle;
(void)securityMode;
};
virtual void securityContextStored(connection_handle_t handle) {
(void)handle;
}
virtual void passkeyDisplay(connection_handle_t handle, const SecurityManager::Passkey_t passkey) {
(void)handle;
(void)passkey;
};
virtual void validMicTimeout(connection_handle_t handle) {
(void)handle;
};
virtual void linkKeyFailure(connection_handle_t handle) {
(void)handle;
};
virtual void keypressNotification(connection_handle_t handle, SecurityManager::Keypress_t keypress) {
(void)handle;
(void)keypress;
};
virtual void legacyPairingOobRequest(connection_handle_t handle) {
(void)handle;
};
virtual void oobRequest(connection_handle_t handle) {
(void)handle;
};
virtual void pinRequest(connection_handle_t handle) {
(void)handle;
};
virtual void passkeyRequest(connection_handle_t handle) {
(void)handle;
};
virtual void confirmationRequest(connection_handle_t handle) {
(void)handle;
};
virtual void acceptPairingRequest(connection_handle_t handle) {
(void)handle;
};
};

/* legacy compatibility with old callbacks (from both sides, so combination of new and old works) */
class LegacySecurityManagerEventHandler : public SecurityManagerEventHandler {
public:
LegacySecurityManagerEventHandler() :
securitySetupInitiatedCallback(),
securitySetupCompletedCallback(),
linkSecuredCallback(),
securityContextStoredCallback(),
passkeyDisplayCallback() { };

void securitySetupInitiated(connection_handle_t handle, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) {
if (securitySetupInitiatedCallback) {
securitySetupInitiatedCallback(handle, allowBonding, requireMITM, iocaps);
}
};
void securitySetupCompleted(connection_handle_t handle, SecurityManager::SecurityCompletionStatus_t status) {
if (securitySetupCompletedCallback) {
securitySetupCompletedCallback(handle, status);
}
};
void linkSecured(connection_handle_t handle, SecurityManager::SecurityMode_t securityMode) {
if (linkSecuredCallback) {
linkSecuredCallback(handle, securityMode);
}
};
void securityContextStored(connection_handle_t handle) {
if (securityContextStoredCallback) {
securityContextStoredCallback(handle);
}
}
void passkeyDisplay(connection_handle_t handle, const SecurityManager::Passkey_t passkey) {
if (passkeyDisplayCallback) {
passkeyDisplayCallback(handle, passkey);
}
};
private:
SecurityManager::SecuritySetupInitiatedCallback_t securitySetupInitiatedCallback;
SecurityManager::SecuritySetupCompletedCallback_t securitySetupCompletedCallback;
SecurityManager::LinkSecuredCallback_t linkSecuredCallback;
SecurityManager::HandleSpecificEvent_t securityContextStoredCallback;
SecurityManager::PasskeyDisplayCallback_t passkeyDisplayCallback;
};

#endif /*__SECURITY_MANAGER_H__*/
16 changes: 9 additions & 7 deletions features/FEATURE_BLE/ble/pal/PalSecurityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,14 @@ struct pairing_failure_t : SafeEnum<pairing_failure_t, uint8_t> {
pairing_failure_t(type value) : SafeEnum<pairing_failure_t, uint8_t>(value) { }
};


using SecurityManager::IO_CAPS_NONE;
using SecurityManager::SecurityCompletionStatus_t;
using SecurityManager::SecurityMode_t;
using SecurityManager::LinkSecurityStatus_t;
using SecurityManager::Keypress_t;
typedef SecurityManager::SecurityCompletionStatus_t SecurityCompletionStatus_t;
typedef SecurityManager::SecurityMode_t SecurityMode_t;
typedef SecurityManager::LinkSecurityStatus_t LinkSecurityStatus_t;
typedef SecurityManager::Keypress_t Keypress_t;

/* please use typedef for porting not the types directly */

typedef uint8_t passkey_t[6];
typedef uint8_t passkey_t[3];
typedef uint8_t oob_data_t[16];

typedef uint8_t irk_t[16];
Expand Down Expand Up @@ -261,6 +259,10 @@ class SecurityManagerEventHandler {

/**
* Adaptation layer of the Security Manager.
*
*
* FIXME: ADD API in the pal to set default passkey!
*
*/
class SecurityManager : private mbed::NonCopyable<SecurityManager> {
public:
Expand Down
24 changes: 12 additions & 12 deletions features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ using ble::pal::csrk_t;
using ble::pal::ltk_t;
using ble::pal::ediv_t;
using ble::pal::rand_t;
using SecurityManager::SecurityIOCapabilities_t;
typedef SecurityManager::SecurityIOCapabilities_t SecurityIOCapabilities_t;

static const uint8_t NUMBER_OFFSET = '0';

Expand Down Expand Up @@ -120,7 +120,7 @@ class GenericSecurityManager : public SecurityManager,
bondable = initBondable;
mitm = initMITM;
iocaps = initIocaps;
passkey = initPasskey;
memcpy(passkey, initPasskey, sizeof(Passkey_t));

return BLE_ERROR_NONE;
}
Expand Down Expand Up @@ -179,17 +179,15 @@ class GenericSecurityManager : public SecurityManager,
// Security settings
//

ble_error_t setPinCode(uint8_t pinLength, uint8_t *pinCode,
bool isStatic = false) {
return pal.set_pin_code(pinLength, pinCode, isStatic);
}

ble_error_t setPasskey(const Passkey_t passkeyASCI, bool isStatic = false) {
// FIXME: ADD API in the pal to set default passkey!
#if 0
uint32_t passkey = 0;
for (int i = 0, m = 1; i < 6; ++i, m *= 10) {
passkey += (passkeyASCI[i] - NUMBER_OFFSET) * m;
}
return pal.set_passkey(passkey);
#endif
return BLE_ERROR_NOT_IMPLEMENTED;
}

ble_error_t setAuthenticationTimeout(connection_handle_t handle,
Expand All @@ -201,7 +199,7 @@ class GenericSecurityManager : public SecurityManager,
uint32_t *timeout_in_ms) {
uint16_t timeout_in_10ms;
ble_error_t status = pal.get_authentication_timeout(handle, timeout_in_10ms);
timeout_in_ms = 10 * timeout_in_10ms;
*timeout_in_ms = 10 * timeout_in_10ms;
return status;
}

Expand Down Expand Up @@ -297,7 +295,9 @@ class GenericSecurityManager : public SecurityManager,
}

virtual ble_error_t passkeyEntered(Gap::Handle_t handle, Passkey_t passkey) {
return pal.passkey_entered(handle, passkey);
// FIXME: convert to passkey_t (3 bytes instead of 6)
//return pal.passkey_request_reply(handle, passkey);
return BLE_ERROR_NOT_IMPLEMENTED;
}

virtual ble_error_t sendKeypressNotification(Gap::Handle_t handle, Keypress_t keypress) {
Expand All @@ -308,7 +308,7 @@ class GenericSecurityManager : public SecurityManager,
// Event handler
//

void setSecurityManagerEventHandler(::SecurityManagerEventHandler* handler) {
void setSecurityManagerEventHandler(::SecurityManager::SecurityManagerEventHandler* handler) {
SecurityManager::setSecurityManagerEventHandler(handler);
if (handler) {
_app_event_handler = handler;
Expand Down Expand Up @@ -459,7 +459,7 @@ class GenericSecurityManager : public SecurityManager,

private:
/* handler is always a valid pointer */
::SecurityManagerEventHandler *_app_event_handler;
::SecurityManager::SecurityManagerEventHandler *_app_event_handler;
};


Expand Down

0 comments on commit cc41857

Please sign in to comment.