Skip to content

Commit

Permalink
Warn if client has several security instance with the psk identity.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Sep 12, 2017
1 parent 35c0a64 commit 9c714dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public LeshanClientBuilder setLocalSecureAddress(String hostname, int port) {
}

/**
* <p>
* Sets the list of objects enablers
* </p>
* Warning : The Security ObjectEnabler should not contains 2 or more entries with the same identity. This is not a
* LWM2M specification constraint but an implementation limitation.
*/
public LeshanClientBuilder setObjects(List<? extends LwM2mObjectEnabler> objectEnablers) {
this.objectEnablers = objectEnablers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public class SecurityObjectPskStore implements PskStore {

private final LwM2mObjectEnabler securityEnabler;

/**
* Warning : The securityEnabler should not contains 2 or more entries with the same identity. This is not a LWM2M
* specification constraint but an implementation limitation.
*/
public SecurityObjectPskStore(LwM2mObjectEnabler securityEnabler) {
this.securityEnabler = securityEnabler;
}
Expand All @@ -52,17 +56,28 @@ public byte[] getKey(String identity) {
if (identity == null)
return null;

byte[] res = null;

LwM2mObject securities = (LwM2mObject) securityEnabler.read(SYSTEM, new ReadRequest(SECURITY)).getContent();
for (LwM2mObjectInstance security : securities.getInstances().values()) {
long securityMode = (long) security.getResource(SEC_SECURITY_MODE).getValue();
if (securityMode == SecurityMode.PSK.code) // psk
{
byte[] pskIdentity = (byte[]) security.getResource(SEC_PUBKEY_IDENTITY).getValue();
if (Arrays.equals(identity.getBytes(), pskIdentity))
return (byte[]) security.getResource(SEC_SECRET_KEY).getValue();
if (Arrays.equals(identity.getBytes(), pskIdentity)) {
if (res == null) {
// we continue to check if the is duplication
res = (byte[]) security.getResource(SEC_SECRET_KEY).getValue();
} else {
LOG.warn("There is several security object instance with the same psk identity : '{}'",
identity);
// we find 1 duplication and warn for it no need to continue.
return res;
}
}
}
}
return null;
return res;
}

@Override
Expand Down

0 comments on commit 9c714dc

Please sign in to comment.