use of org.openremote.agent.protocol.bluetooth.mesh.NetworkKey in project openremote by openremote.
the class BaseMeshNetwork method updateMeshKey.
private boolean updateMeshKey(final MeshKey key) {
if (key instanceof ApplicationKey) {
ApplicationKey appKey = null;
for (int i = 0; i < appKeys.size(); i++) {
final ApplicationKey tempKey = appKeys.get(i);
if (tempKey.getKeyIndex() == key.getKeyIndex()) {
appKey = (ApplicationKey) key;
appKeys.set(i, appKey);
break;
}
}
if (appKey != null) {
notifyAppKeyUpdated(appKey);
return true;
}
} else {
NetworkKey netKey = null;
for (int i = 0; i < netKeys.size(); i++) {
final NetworkKey tempKey = netKeys.get(i);
if (tempKey.getKeyIndex() == key.getKeyIndex()) {
netKey = (NetworkKey) key;
netKeys.set(i, netKey);
break;
}
}
if (netKey != null) {
netKey.setTimestamp(System.currentTimeMillis());
notifyNetKeyUpdated(netKey);
return true;
}
}
return false;
}
use of org.openremote.agent.protocol.bluetooth.mesh.NetworkKey in project openremote by openremote.
the class NetworkLayer method getK2Output.
/**
* Returns the master credentials {@link SecureUtils.K2Output}
*
* @param message Message
*/
private SecureUtils.K2Output getK2Output(final Message message) {
final NetworkKey networkKey;
if (message.getAkf() == APPLICATION_KEY_IDENTIFIER) {
networkKey = mNetworkLayerCallbacks.getPrimaryNetworkKey();
} else {
final int netKeyIndex = message.getApplicationKey().getBoundNetKeyIndex();
networkKey = mNetworkLayerCallbacks.getNetworkKey(netKeyIndex);
}
return networkKey.getTxDerivatives();
}
use of org.openremote.agent.protocol.bluetooth.mesh.NetworkKey in project openremote by openremote.
the class BaseMeshNetwork method distributeNetKey.
/**
* Distribute Net Key will start the key refresh procedure and return the newly updated key.
*
* <p>
* This process contains three phases.
* {@link NetworkKey#KEY_DISTRIBUTION} - Distribution of the new Keys {@link #distributeNetKey(NetworkKey, byte[])}.
* {@link NetworkKey#USING_NEW_KEYS} - Switching to the new keys {@link #switchToNewKey(NetworkKey)}.
* {@link NetworkKey#REVOKE_OLD_KEYS} - Revoking old keys {@link #revokeOldKey(NetworkKey)}.
* The new key is distributed to the provisioner node by setting the currently used key as the old key and setting the
* currently used key to the new key value. This will change the phase of the network key to{@link NetworkKey#KEY_DISTRIBUTION}.
* During this phase a node will transmit using the old key but may receive using both old and the new key. After a successful
* distribution to the provisioner, the user may start sending {@link ConfigNetKeyUpdate} messages to the respective nodes in the
* network that requires updating. In addition if the user wishes to update the AppKey call {@link #distributeAppKey(ApplicationKey, byte[])}
* to update the Application Key on the provisioner and then distribute it to other nodes by sending {@link ConfigAppKeyUpdate} to
* update an AppKey. However it shall be only successfully processed if the NetworkKey bound to the Application Key is in
* {@link NetworkKey#KEY_DISTRIBUTION} and the received app key value is different or when the received AppKey value is the same as
* previously received value. Also note that sending a ConfigNetKeyUpdate during {@link NetworkKey#NORMAL_OPERATION} will switch the
* phase to {@link NetworkKey#KEY_DISTRIBUTION}. Once distribution is completed, call {@link #switchToNewKey(NetworkKey)} and
* send {@link ConfigKeyRefreshPhaseSet} to other nodes.
* </p>
*
* @param networkKey Network key
* @param newNetKey 16-byte key
* @throws IllegalArgumentException the key value is already in use.
*/
public synchronized NetworkKey distributeNetKey(final NetworkKey networkKey, final byte[] newNetKey) throws IllegalArgumentException {
if (validateKey(newNetKey)) {
if (isNetKeyExists(newNetKey)) {
throw new IllegalArgumentException("Net key value is already in use.");
}
final int keyIndex = networkKey.getKeyIndex();
final NetworkKey netKey = getNetKey(keyIndex);
if (netKey.equals(networkKey)) {
if (netKey.distributeKey(newNetKey)) {
updateNodeKeyStatus(netKey);
if (updateMeshKey(netKey)) {
return netKey;
}
}
}
}
return null;
}
use of org.openremote.agent.protocol.bluetooth.mesh.NetworkKey in project openremote by openremote.
the class BaseMeshNetwork method updateNetKey.
/**
* Update a network key with the given 16-byte hexadecimal string in the mesh network.
*
* <p>
* Updating a NetworkKey's key value requires initiating a Key Refresh Procedure. A NetworkKey that's in use
* would require a Key Refresh Procedure to update it's key contents. However a NetworkKey that's not in could
* be updated without this procedure. If the key is in use, call {@link #distributeNetKey(NetworkKey, byte[])}
* to initiate the Key Refresh Procedure.
* </p>
*
* @param networkKey Network key
* @param newNetKey 16-byte hexadecimal string
* @throws IllegalArgumentException if the key is already in use
*/
public synchronized boolean updateNetKey(final NetworkKey networkKey, final String newNetKey) throws IllegalArgumentException {
if (MeshParserUtils.validateKeyInput(newNetKey)) {
final byte[] key = MeshParserUtils.toByteArray(newNetKey);
if (isNetKeyExists(key)) {
throw new IllegalArgumentException("Net key value is already in use.");
}
final int keyIndex = networkKey.getKeyIndex();
final NetworkKey netKey = getNetKey(keyIndex);
if (!isKeyInUse(netKey)) {
// This will return true only if the key index and the key are the same
if (netKey.equals(networkKey)) {
netKey.setKey(key);
return updateMeshKey(netKey);
} else {
return false;
}
} else {
throw new IllegalArgumentException("Unable to update a network key that's already in use. ");
}
}
return false;
}
use of org.openremote.agent.protocol.bluetooth.mesh.NetworkKey in project openremote by openremote.
the class BaseMeshNetwork method createNetworkKey.
/**
* Creates an Network key
*
* @return {@link NetworkKey}
* @throws IllegalArgumentException in case the generated application key already exists
*/
public synchronized NetworkKey createNetworkKey() throws IllegalArgumentException {
final NetworkKey key = new NetworkKey(getAvailableNetKeyIndex(), MeshParserUtils.toByteArray(SecureUtils.generateRandomNetworkKey()));
key.setMeshUuid(meshUUID);
return key;
}
Aggregations