Skip to content

Commit

Permalink
Add connection ID support to demos
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Apr 13, 2021
1 parent da6d32e commit d33516e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.californium.core.network.config.NetworkConfig.Keys;
import org.eclipse.californium.elements.util.SslContextUtil;
import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
import org.eclipse.californium.scandium.dtls.SingleNodeConnectionIdGenerator;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
Expand Down Expand Up @@ -111,7 +112,13 @@ public static void main(String[] args) {
options.addOption("m", "modelsfolder", true, "A folder which contains object models in OMA DDF(.xml) format.");
options.addOption("cfg", "configfile", true,
"Set the filename for the configuration.\nDefault: " + JSONFileBootstrapStore.DEFAULT_FILE + ".");
options.addOption("oc", "activate support of old/deprecated cipher suites." + RPKChapter);
options.addOption("oc", "activate support of old/deprecated cipher suites.");
options.addOption("cid", true, "Control usage of DTLS connection ID." //
+ "\n - 'on' to activate Connection ID support (same as -cid 6)" //
+ "\n - 'off' to deactivate it" //
+ "\n - Positive value define the size in byte of CID generated."
+ "\n - 0 value means we accept to use CID but will not generated one for foreign peer."
+ "\n (Default: on)" + RPKChapter);
options.addOption("pubk", true,
"The path to your server public key file.\n The public Key should be in SubjectPublicKeyInfo format (DER encoding).");
options.addOption("prik", true,
Expand Down Expand Up @@ -233,6 +240,20 @@ public static void main(String[] args) {
configFilename = JSONFileBootstrapStore.DEFAULT_FILE;
}

// Get CID config
String cidOption = cl.getOptionValue("cid");
Integer cid = 6;
if (cidOption != null) {
if ("off".equals(cidOption)) {
cid = null;
} else if ("on".equals(cidOption)) {
// we keep default value
} else {
cid = Integer.parseInt(cidOption);
cid = cid < 0 ? null : cid;
}
}

// get RPK info
PublicKey publicKey = null;
PrivateKey privateKey = null;
Expand Down Expand Up @@ -312,7 +333,7 @@ public static void main(String[] args) {
try {
createAndStartServer(webAddress, webPort, localAddress, localPort, secureLocalAddress, secureLocalPort,
modelsFolderPath, configFilename, cl.hasOption("oc"), publicKey, privateKey, certificate,
trustStore);
trustStore, cid);
} catch (BindException e) {
System.err.println(String
.format("Web port %s is already in use, you can change it using the 'webport' option.", webPort));
Expand All @@ -325,7 +346,7 @@ public static void main(String[] args) {
public static void createAndStartServer(String webAddress, int webPort, String localAddress, Integer localPort,
String secureLocalAddress, Integer secureLocalPort, String modelsFolderPath, String configFilename,
boolean supportDeprecatedCiphers, PublicKey publicKey, PrivateKey privateKey, X509Certificate[] certificate,
List<Certificate> trustStore) throws Exception {
List<Certificate> trustStore, Integer cid) throws Exception {
// Create Models
List<ObjectModel> models = ObjectLoader.loadDefault();
if (modelsFolderPath != null) {
Expand All @@ -342,6 +363,9 @@ public static void createAndStartServer(String webAddress, int webPort, String l
// Create DTLS Config
DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder();
dtlsConfig.setRecommendedCipherSuitesOnly(!supportDeprecatedCiphers);
if (cid != null) {
dtlsConfig.setConnectionIdGenerator(new SingleNodeConnectionIdGenerator(cid));
}

// Create credentials;
X509Certificate[] serverCertificateChain = null;
Expand Down
Expand Up @@ -61,6 +61,7 @@
import org.eclipse.californium.scandium.dtls.ServerHandshaker;
import org.eclipse.californium.scandium.dtls.SessionAdapter;
import org.eclipse.californium.scandium.dtls.SessionId;
import org.eclipse.californium.scandium.dtls.SingleNodeConnectionIdGenerator;
import org.eclipse.californium.scandium.dtls.cipher.CipherSuite;
import org.eclipse.leshan.client.californium.LeshanClient;
import org.eclipse.leshan.client.californium.LeshanClientBuilder;
Expand Down Expand Up @@ -192,6 +193,13 @@ public static void main(final String[] args) {
c.hasArgs();
options.addOption(c.build());
options.addOption("oc", "activate support of old/deprecated cipher suites.");
options.addOption("cid", true, "Control usage of DTLS connection ID." //
+ "\n - 'on' to activate Connection ID support (same as -cid 0)" //
+ "\n - 'off' to deactivate it" //
+ "\n - Positive value define the size in byte of CID generated."
+ "\n - 0 value means we accept to use CID but will not generated one for foreign peer."
+ "\n (Default: off)");

Builder aa = Option.builder("aa");
aa.desc("Use additional attributes at registration time, syntax is \n -aa attrName1=attrValue1 attrName2=\\\"attrValue2\\\" ...");
aa.hasArgs();
Expand Down Expand Up @@ -418,6 +426,20 @@ public static void main(final String[] args) {
serverURI = "coap://localhost:" + LwM2m.DEFAULT_COAP_PORT;
}

// Get CID config
String cidOption = cl.getOptionValue("cid");
Integer cid = null;
if (cidOption != null) {
if ("off".equals(cidOption)) {
cid = null;
} else if ("on".equals(cidOption)) {
cid = 0;
} else {
cid = Integer.parseInt(cidOption);
cid = cid < 0 ? null : cid;
}
}

// get PSK info
byte[] pskIdentity = null;
byte[] pskKey = null;
Expand Down Expand Up @@ -596,7 +618,7 @@ public static void main(final String[] args) {
bsAdditionalAttributes, lifetime, communicationPeriod, serverURI, pskIdentity, pskKey,
clientPrivateKey, clientPublicKey, serverPublicKey, clientCertificate, serverCertificate,
trustStore, certificateUsage, latitude, longitude, scaleFactor, cl.hasOption("ocf"),
cl.hasOption("oc"), cl.hasOption("r"), cl.hasOption("f"), modelsFolderPath, ciphers);
cl.hasOption("oc"), cl.hasOption("r"), cl.hasOption("f"), modelsFolderPath, ciphers, cid);
} catch (Exception e) {
System.err.println("Unable to create and start client ...");
e.printStackTrace();
Expand All @@ -611,7 +633,8 @@ public static void createAndStartClient(String endpoint, String localAddress, in
X509Certificate clientCertificate, X509Certificate serverCertificate, List<Certificate> trustStore,
CertificateUsage certificateUsage, Float latitude, Float longitude, float scaleFactor,
boolean supportOldFormat, boolean supportDeprecatedCiphers, boolean reconnectOnUpdate,
boolean forceFullhandshake, String modelsFolderPath, List<CipherSuite> ciphers) throws Exception {
boolean forceFullhandshake, String modelsFolderPath, List<CipherSuite> ciphers, Integer cid)
throws Exception {

locationInstance = new MyLocation(latitude, longitude, scaleFactor);

Expand Down Expand Up @@ -680,6 +703,9 @@ public static void createAndStartClient(String endpoint, String localAddress, in
if (ciphers != null) {
dtlsConfig.setSupportedCipherSuites(ciphers);
}
if (cid != null) {
dtlsConfig.setConnectionIdGenerator(new SingleNodeConnectionIdGenerator(cid));
}

// Configure Registration Engine
DefaultRegistrationEngineFactory engineFactory = new DefaultRegistrationEngineFactory();
Expand Down
Expand Up @@ -48,6 +48,7 @@
import org.eclipse.californium.core.network.config.NetworkConfig.Keys;
import org.eclipse.californium.elements.util.SslContextUtil;
import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
import org.eclipse.californium.scandium.dtls.SingleNodeConnectionIdGenerator;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
Expand Down Expand Up @@ -152,6 +153,12 @@ public static void main(String[] args) {
options.addOption("wp", "webport", true, "Set the HTTP port for web server.\nDefault: 8080.");
options.addOption("m", "modelsfolder", true, "A folder which contains object models in OMA DDF(.xml) format.");
options.addOption("oc", "activate support of old/deprecated cipher suites.");
options.addOption("cid", true, "Control usage of DTLS connection ID." //
+ "\n - 'on' to activate Connection ID support (same as -cid 6)" //
+ "\n - 'off' to deactivate it" //
+ "\n - Positive value define the size in byte of CID generated."
+ "\n - 0 value means we accept to use CID but will not generated one for foreign peer."
+ "\n (Default: on)");
options.addOption("r", "redis", true,
"Use redis to store registration and securityInfo. \nThe URL of the redis server should be given using this format : 'redis://:password@hostname:port/db_number'\nExample without DB and password: 'redis://localhost:6379'\nDefault: redis is not used.");
options.addOption("mdns", "publishDNSSdServices", false,
Expand Down Expand Up @@ -280,6 +287,20 @@ public static void main(String[] args) {
// Get models folder
String modelsFolderPath = cl.getOptionValue("m");

// Get CID config
String cidOption = cl.getOptionValue("cid");
Integer cid = 6;
if (cidOption != null) {
if ("off".equals(cidOption)) {
cid = null;
} else if ("on".equals(cidOption)) {
// we keep default value
} else {
cid = Integer.parseInt(cidOption);
cid = cid < 0 ? null : cid;
}
}

// get the Redis hostname:port
String redisUrl = cl.getOptionValue("r");

Expand Down Expand Up @@ -373,7 +394,7 @@ public static void main(String[] args) {
createAndStartServer(webAddress, webPort, localAddress, localPort, secureLocalAddress, secureLocalPort,
modelsFolderPath, redisUrl, publicKey, privateKey, certificate, trustStore, keyStorePath,
keyStoreType, keyStorePass, keyStoreAlias, keyStoreAliasPass, publishDNSSdServices,
cl.hasOption("oc"));
cl.hasOption("oc"), cid);
} catch (BindException e) {
System.err.println(
String.format("Web port %s is already used, you could change it using 'webport' option.", webPort));
Expand All @@ -387,7 +408,8 @@ public static void createAndStartServer(String webAddress, int webPort, String l
String secureLocalAddress, Integer secureLocalPort, String modelsFolderPath, String redisUrl,
PublicKey publicKey, PrivateKey privateKey, X509Certificate[] certificate, List<Certificate> trustStore,
String keyStorePath, String keyStoreType, String keyStorePass, String keyStoreAlias,
String keyStoreAliasPass, Boolean publishDNSSdServices, boolean supportDeprecatedCiphers) throws Exception {
String keyStoreAliasPass, Boolean publishDNSSdServices, boolean supportDeprecatedCiphers, Integer cid)
throws Exception {
// Prepare LWM2M server
LeshanServerBuilder builder = new LeshanServerBuilder();
builder.setEncoder(new DefaultLwM2mNodeEncoder());
Expand Down Expand Up @@ -423,6 +445,9 @@ public static void createAndStartServer(String webAddress, int webPort, String l
// Create DTLS Config
DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder();
dtlsConfig.setRecommendedCipherSuitesOnly(!supportDeprecatedCiphers);
if (cid != null) {
dtlsConfig.setConnectionIdGenerator(new SingleNodeConnectionIdGenerator(cid));
}

X509Certificate[] serverCertificateChain = null;
if (certificate != null) {
Expand Down

0 comments on commit d33516e

Please sign in to comment.