Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing CiperSuiteId from single to multiple resource #1404

Closed
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,18 @@ public class BootstrapConfig {
*/
public Map<Integer, OscoreObject> oscore = new HashMap<>();

/** Server Configuration (object 1) as defined in LWM2M 1.0.x TS. */
/**
* Server Configuration (object 1) as defined in LWM2M 1.0.x TS.
*/
public static class ServerConfig {

/** Used as link to associate server Object Instance. */
/**
* Used as link to associate server Object Instance.
*/
public int shortId;
/** Specify the lifetime of the registration in seconds (see Section 5.3 Registration). */
/**
* Specify the lifetime of the registration in seconds (see Section 5.3 Registration).
*/
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
public int lifetime = 86400;
/**
* The default value the LwM2M Client should use for the Minimum Period of an Observation in the absence of this
Expand Down Expand Up @@ -291,7 +297,6 @@ public static class ServerSecurity {
/**
* The Object ID of the OSCORE Object Instance that holds the OSCORE configuration to be used by the LWM2M
* Client to the LWM2M Server associated with this Security object.
*
*/
public Integer oscoreSecurityMode;

Expand Down Expand Up @@ -341,7 +346,7 @@ public static class ServerSecurity {
* <p>
* Since Security v1.1
*/
public ULong cipherSuite = null;
public List<CipherSuiteId> cipherSuite = null;

@Override
public String toString() {
Expand All @@ -362,9 +367,13 @@ public String toString() {
*/
public static class ACLConfig {

/** The Object ID of the Object Instance for which ACL are applied. */
/**
* The Object ID of the Object Instance for which ACL are applied.
*/
public int objectId;
/** The Object instance ID of the Object Instance for which ACL are applied. */
/**
* The Object instance ID of the Object Instance for which ACL are applied.
*/
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
public int objectInstanceId;

/**
Expand Down Expand Up @@ -465,6 +474,44 @@ public String toString() {
}
}

public class CipherSuiteId {
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved

private final byte firstByte;
private final byte secondByte;

/**
* Ciphersuite is created with 2 bytes.
*
* @param firstByte first byte of ciphersuite (for example 0xC0)
* @param secondByte second byte of ciphersuite (for example 0xA8)
*/
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
public CipherSuiteId(byte firstByte, byte secondByte) {
this.firstByte = firstByte;
this.secondByte = secondByte;
}

/**
* Integer is split into 2 bytes for example 49320 (0xc0a8 in hex) will be split into "0xC0,0xA8".
*
* @param valueFromSecurityObject Integer representing ciphersuite id
*/
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
public CipherSuiteId(ULong valueFromSecurityObject) {
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
String binaryString = Long.toBinaryString(valueFromSecurityObject.longValue());
this.firstByte = (byte) Integer.parseInt(binaryString.substring(0, 8), 2);
this.secondByte = (byte) Integer.parseInt(binaryString.substring(9, 17), 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using String is maybe not the best idea.

Should be better to play with binary operator like :

  • shift operator : >>

You can create unit test to check your code.

We should also check that valueFromSecurityObject is a 16-bit unsigned integer.

}

/**
* Two bytes of ciphersuite id are concatenated into integer value. As an example bytes "0xC0,0xA8" will be
* concatenated into 0xc0a8 which in decimal notation is 49320.
*
* @return Integer number concatenated from 2 bytes.
*/
public ULong getValueForSecurityObject() {
return ULong.valueOf(Byte.toUnsignedInt(firstByte) * 256 + Byte.toUnsignedInt(secondByte));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using floating point number and multiplication are maybe not best idea.

Should be better to play with binary operator like :

  • bitMask operator : &
  • binary notation : 0b00000
  • shift operator : <<

You can create unit test to check your code.

}
}

@Override
public String toString() {
return String.format("BootstrapConfig [servers=%s, security=%s, acls=%s, oscore=%s]", servers, security, acls,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

Expand All @@ -36,6 +38,7 @@
import org.eclipse.leshan.core.request.BootstrapWriteRequest;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.core.util.datatype.ULong;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig.ACLConfig;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig.OscoreObject;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig;
Expand Down Expand Up @@ -79,8 +82,14 @@ public static LwM2mObjectInstance toSecurityInstance(int instanceId, ServerSecur
resources.add(LwM2mSingleResource.newStringResource(14, securityConfig.sni));
if (securityConfig.certificateUsage != null)
resources.add(LwM2mSingleResource.newUnsignedIntegerResource(15, securityConfig.certificateUsage.code));
if (securityConfig.cipherSuite != null)
resources.add(LwM2mSingleResource.newUnsignedIntegerResource(16, securityConfig.cipherSuite));
if (securityConfig.cipherSuite != null) {
Map<Integer, ULong> ciperSuiteULong = new HashMap<>();
int i = 0;
for (BootstrapConfig.CipherSuiteId cipherSuiteId : securityConfig.cipherSuite) {
ciperSuiteULong.put(i++, cipherSuiteId.getValueForSecurityObject());
}
resources.add(LwM2mMultipleResource.newUnsignedIntegerResource(16, ciperSuiteULong));
}
if (securityConfig.oscoreSecurityMode != null) {
resources.add(LwM2mSingleResource.newObjectLinkResource(17,
new ObjectLink(21, securityConfig.oscoreSecurityMode)));
Expand Down