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 @@ -71,35 +71,27 @@ public class BootstrapConfig {
*/
public ContentFormat contentFormat = null;

/**
* List of LWM2M path to delete.
*/
/** List of LWM2M path to delete. */
public List<String> toDelete = new ArrayList<>();

/**
* Map indexed by Server Instance Id. Key is the Server Instance to write.
*/
/** Map indexed by Server Instance Id. Key is the Server Instance to write. */
public Map<Integer, ServerConfig> servers = new HashMap<>();

/**
* Map indexed by Security Instance Id. Key is the Server Instance to write.
*/
/** Map indexed by Security Instance Id. Key is the Server Instance to write. */
public Map<Integer, ServerSecurity> security = new HashMap<>();

/**
* Map indexed by ACL Instance Id. Key is the ACL Instance to write.
*/
/** Map indexed by ACL Instance Id. Key is the ACL Instance to write. */
public Map<Integer, ACLConfig> acls = new HashMap<>();

/**
* Map indexed by OSCORE Object Instance Id. Key is the OSCORE Object Instance to write.
*/
/** Map indexed by OSCORE Object Instance Id. Key is the OSCORE Object Instance to write. */
public Map<Integer, OscoreObject> oscore = new HashMap<>();

/** 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). */
public int lifetime = 86400;
Expand Down Expand Up @@ -291,7 +283,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 +332,7 @@ public static class ServerSecurity {
* <p>
* Since Security v1.1
*/
public ULong cipherSuite = null;
public List<CipherSuiteId> cipherSuite = null;

@Override
public String toString() {
Expand Down Expand Up @@ -465,6 +456,55 @@ public String toString() {
}
}

public static class CipherSuiteId {

private final byte firstByte;
private final byte secondByte;

/**
* Ciphersuite is created with 2 bytes. Possible values are described in the
* <a href="https://iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4"> registry</a>.
*
* @param firstByte first byte of ciphersuite (for example 0xC0)
* @param secondByte second byte of ciphersuite (for example 0xA8)
*/
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". This format
* is used by Security Object, resource 16.
*
* @param valueFromSecurityObject Integer representing ciphersuite id
*/
public CipherSuiteId(ULong valueFromSecurityObject) {
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
if (valueFromSecurityObject.intValue() < 0 || valueFromSecurityObject.intValue() > 65535)
throw new IllegalArgumentException("ULong value have to be between <0, 65535>");
this.firstByte = (byte) ((valueFromSecurityObject.intValue() >> 8) & 0xFF);
this.secondByte = (byte) ((valueFromSecurityObject.intValue()) & 0xFF);
}

/**
* 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) << 8) | Byte.toUnsignedInt(secondByte));
}

/**
* @return String representing hex value of concatenated two bytes.
*/
@Override
public String toString() {
return String.format("%x,%x", firstByte, secondByte);
}
}

@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
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2016 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Bartosz Stolarczyk
* Orange Polska S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server.bootstrap;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.eclipse.leshan.core.util.Hex;
import org.eclipse.leshan.core.util.datatype.ULong;
import org.junit.jupiter.api.Test;

class BootstrapConfigTest {

@Test
public void CipherSuiteId_encode_from_two_bytes() {
// Create 2 bytes
byte[] decoded = Hex.decodeHex("C0A8".toCharArray());
Byte firstByte = decoded[0];
Byte secoundByte = decoded[1];

// Create CipherSuiteId with two bytes
BootstrapConfig.CipherSuiteId cipherSuiteId = new BootstrapConfig.CipherSuiteId(firstByte, secoundByte);

// Assert if bytes were correctly phrased
assertEquals("c0,a8", cipherSuiteId.toString());
}

@Test
public void CipherSuiteId_encode_from_ULong() {
// Create CipherSuiteId with ULong
BootstrapConfig.CipherSuiteId cipherSuiteId = new BootstrapConfig.CipherSuiteId(ULong.valueOf(49320));

// Assert if ULong was correctly phrased
assertEquals("c0,a8", cipherSuiteId.toString());
}

@Test
public void getValueForSecurityObject() {
// Create example ULong
ULong testValue = ULong.valueOf(49320);

// Create cipherSuiteId from ULong
BootstrapConfig.CipherSuiteId cipherSuiteId = new BootstrapConfig.CipherSuiteId(testValue);

// Check if getValueForSecurityObject() returns input ULong
assertEquals(testValue, cipherSuiteId.getValueForSecurityObject());
}

@Test
public void is_error_thrown_for_too_big_values() {
// Create ULong with value bigger than 65535
ULong testValue = ULong.valueOf(65536);

// Try to create CipherSuiteId with ULong outside of range
try {
new BootstrapConfig.CipherSuiteId(testValue);
assert (false);
} catch (IllegalArgumentException e) {
// If IllegalArgumentException is caught pass the test
assert (true);
}
}
}