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 @@ -181,6 +181,10 @@ public static LwM2mSingleResource newUnsignedIntegerResource(int id, Long value)
throw new LwM2mNodeException("Invalid value : positive value expected for UNSIGNED_INTEGER");
}

public static LwM2mSingleResource newULongArray(int id, ULong[] value) {
return new LwM2mSingleResource(id, value, Type.OPAQUE);
}

sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
/**
* {@inheritDoc}
*/
Expand All @@ -206,23 +210,23 @@ public Object getValue() {
}

/**
* @exception NoSuchElementException use {@link #getValue()} instead.
* @throws NoSuchElementException use {@link #getValue()} instead.
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
*/
@Override
public Object getValue(int id) {
throw new NoSuchElementException("There is no 'values' on single resources, use getValue() instead.");
}

/**
* @exception NoSuchElementException use {@link #getValue()} instead.
* @throws NoSuchElementException use {@link #getValue()} instead.
*/
@Override
public LwM2mResourceInstance getInstance(int id) {
throw new NoSuchElementException("There is no 'instance' on single resources, use getValue() instead.");
}

/**
* @exception NoSuchElementException use {@link #getValue()} instead.
* @throws NoSuchElementException use {@link #getValue()} instead.
*/
@Override
public Map<Integer, LwM2mResourceInstance> getInstances() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,24 @@ public class BootstrapConfig {
*/
public Map<Integer, OscoreObject> oscore = new HashMap<>();

/** Server Configuration (object 1) as defined in LWM2M 1.0.x TS. */
@Override
public String toString() {
return String.format("BootstrapConfig [servers=%s, security=%s, acls=%s, oscore=%s]", servers, security, acls,
oscore);
}

/**
* 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 +303,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 +352,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 +373,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,9 +480,36 @@ public String toString() {
}
}

@Override
public String toString() {
return String.format("BootstrapConfig [servers=%s, security=%s, acls=%s, oscore=%s]", servers, security, acls,
oscore);
public class CipherSuiteId {
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved

private final byte firstByte;
private final byte secondByte;

public CipherSuiteId(byte firstByte, byte secondByte) {
this.firstByte = firstByte;
this.secondByte = secondByte;
}

/**
* The IANA TLS ciphersuite registry is maintained at
* https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml. As an example, the
* TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string "0xC0,0xA8"
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
*/

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.


}

/**
* As an example, the TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string
* "0xC0,0xA8". To form an integer value the two values are concatenated. In this example, the value is 0xc0a8
* or 49320.
*/
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.

}
}
}
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