Search in sources :

Example 11 with NetworkConfig

use of org.eclipse.californium.core.network.config.NetworkConfig in project leshan by eclipse.

the class SecureIntegrationTestHelper method createRPKClient.

// TODO implement RPK support for client
public void createRPKClient() {
    ObjectsInitializer initializer = new ObjectsInitializer();
    initializer.setInstancesForObject(LwM2mId.SECURITY, Security.rpk("coaps://" + server.getSecuredAddress().getHostString() + ":" + server.getSecuredAddress().getPort(), 12345, clientPublicKey.getEncoded(), clientPrivateKey.getEncoded(), serverPublicKey.getEncoded()));
    initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
    initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U"));
    List<LwM2mObjectEnabler> objects = initializer.createMandatory();
    objects.add(initializer.create(2));
    InetSocketAddress clientAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
    DtlsConnectorConfig.Builder config = new DtlsConnectorConfig.Builder().setAddress(clientAddress);
    // TODO we should read the config from the security object
    // TODO no way to provide a dynamic config with the current scandium API
    config.setIdentity(clientPrivateKey, clientPublicKey);
    CoapServer coapServer = new CoapServer();
    CoapEndpoint.CoapEndpointBuilder coapBuilder = new CoapEndpoint.CoapEndpointBuilder();
    coapBuilder.setConnector(new DTLSConnector(config.build()));
    coapBuilder.setNetworkConfig(new NetworkConfig());
    coapServer.addEndpoint(coapBuilder.build());
    LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
    builder.setLocalAddress(clientAddress.getHostString(), clientAddress.getPort());
    builder.setObjects(objects);
    client = builder.build();
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) CoapServer(org.eclipse.californium.core.CoapServer) Server(org.eclipse.leshan.client.object.Server) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) Device(org.eclipse.leshan.client.object.Device) InetSocketAddress(java.net.InetSocketAddress) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) NetworkConfig(org.eclipse.californium.core.network.config.NetworkConfig) CoapServer(org.eclipse.californium.core.CoapServer) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint)

Example 12 with NetworkConfig

use of org.eclipse.californium.core.network.config.NetworkConfig in project hono by eclipse.

the class ConfigBasedCoapEndpointFactory method newDefaultNetworkConfig.

private NetworkConfig newDefaultNetworkConfig() {
    final NetworkConfig networkConfig = new NetworkConfig();
    networkConfig.setInt(Keys.PROTOCOL_STAGE_THREAD_COUNT, config.getCoapThreads());
    networkConfig.setInt(Keys.NETWORK_STAGE_RECEIVER_THREAD_COUNT, config.getConnectorThreads());
    networkConfig.setInt(Keys.NETWORK_STAGE_SENDER_THREAD_COUNT, config.getConnectorThreads());
    networkConfig.setInt(Keys.MAX_RESOURCE_BODY_SIZE, config.getMaxPayloadSize());
    networkConfig.setInt(Keys.EXCHANGE_LIFETIME, config.getExchangeLifetime());
    networkConfig.setBoolean(Keys.USE_MESSAGE_OFFLOADING, config.isMessageOffloadingEnabled());
    networkConfig.setString(Keys.DEDUPLICATOR, Keys.DEDUPLICATOR_PEERS_MARK_AND_SWEEP);
    final int maxConnections = config.getMaxConnections();
    if (maxConnections == 0) {
        final MemoryBasedConnectionLimitStrategy limits = new MemoryBasedConnectionLimitStrategy(MINIMAL_MEMORY, MEMORY_PER_CONNECTION);
        networkConfig.setInt(Keys.MAX_ACTIVE_PEERS, limits.getRecommendedLimit());
    } else {
        networkConfig.setInt(Keys.MAX_ACTIVE_PEERS, maxConnections);
    }
    return networkConfig;
}
Also used : MemoryBasedConnectionLimitStrategy(org.eclipse.hono.adapter.limiting.MemoryBasedConnectionLimitStrategy) NetworkConfig(org.eclipse.californium.core.network.config.NetworkConfig) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint)

Example 13 with NetworkConfig

use of org.eclipse.californium.core.network.config.NetworkConfig in project hono by eclipse.

the class ConfigBasedCoapEndpointFactory method createSecureEndpoint.

private Future<Endpoint> createSecureEndpoint(final int port, final NetworkConfig networkConfig) {
    if (deviceResolver == null) {
        return Future.failedFuture(new IllegalStateException("infoSupplier property must be set for secure endpoint"));
    }
    if (pskStore == null) {
        return Future.failedFuture(new IllegalStateException("pskStore property must be set for secure endpoint"));
    }
    LOG.info("creating secure endpoint");
    final DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder();
    // prevent session resumption
    dtlsConfig.setNoServerSessionId(true);
    dtlsConfig.setServerOnly(true);
    dtlsConfig.setRecommendedCipherSuitesOnly(true);
    dtlsConfig.setClientAuthenticationRequired(true);
    dtlsConfig.setAddress(new InetSocketAddress(config.getBindAddress(), port));
    dtlsConfig.setApplicationLevelInfoSupplier(deviceResolver);
    dtlsConfig.setAdvancedPskStore(pskStore);
    dtlsConfig.setRetransmissionTimeout(config.getDtlsRetransmissionTimeout());
    dtlsConfig.setMaxConnections(networkConfig.getInt(Keys.MAX_ACTIVE_PEERS));
    dtlsConfig.setSniEnabled(true);
    addIdentity(dtlsConfig);
    try {
        final DtlsConnectorConfig dtlsConnectorConfig = dtlsConfig.build();
        if (LOG.isInfoEnabled()) {
            final String ciphers = dtlsConnectorConfig.getSupportedCipherSuites().stream().map(cipher -> cipher.name()).collect(Collectors.joining(", "));
            LOG.info("creating secure endpoint supporting ciphers: {}", ciphers);
        }
        final DTLSConnector dtlsConnector = new DTLSConnector(dtlsConnectorConfig);
        final CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
        builder.setNetworkConfig(networkConfig);
        builder.setConnector(dtlsConnector);
        builder.setObservationStore(observationStore);
        return Future.succeededFuture(builder.build());
    } catch (final IllegalStateException ex) {
        LOG.warn("failed to create secure endpoint", ex);
        return Future.failedFuture(ex);
    }
}
Also used : AdvancedPskStore(org.eclipse.californium.scandium.dtls.pskstore.AdvancedPskStore) ObservationStore(org.eclipse.californium.core.observe.ObservationStore) LoggerFactory(org.slf4j.LoggerFactory) KeyLoader(org.eclipse.hono.config.KeyLoader) Constants(org.eclipse.hono.util.Constants) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Keys(org.eclipse.californium.core.network.config.NetworkConfig.Keys) NetworkConfig(org.eclipse.californium.core.network.config.NetworkConfig) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) ByteArrayInputStream(java.io.ByteArrayInputStream) DeviceInfoSupplier(org.eclipse.hono.adapter.coap.DeviceInfoSupplier) CoapEndpointFactory(org.eclipse.hono.adapter.coap.CoapEndpointFactory) NewAdvancedCertificateVerifier(org.eclipse.californium.scandium.dtls.x509.NewAdvancedCertificateVerifier) Strings(org.eclipse.hono.util.Strings) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) CoapAdapterProperties(org.eclipse.hono.adapter.coap.CoapAdapterProperties) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Objects(java.util.Objects) Certificate(java.security.cert.Certificate) MemoryBasedConnectionLimitStrategy(org.eclipse.hono.adapter.limiting.MemoryBasedConnectionLimitStrategy) PrivateKey(java.security.PrivateKey) CoAP(org.eclipse.californium.core.coap.CoAP) Endpoint(org.eclipse.californium.core.network.Endpoint) Optional(java.util.Optional) ApplicationLevelInfoSupplier(org.eclipse.californium.scandium.auth.ApplicationLevelInfoSupplier) InputStream(java.io.InputStream) InetSocketAddress(java.net.InetSocketAddress) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector)

Example 14 with NetworkConfig

use of org.eclipse.californium.core.network.config.NetworkConfig in project hono by eclipse.

the class ConfigBasedCoapEndpointFactory method loadNetworkConfig.

/**
 * Loads Californium configuration properties from a file.
 *
 * @param fileName The absolute path to the properties file.
 * @param networkConfig The configuration to apply the properties to.
 * @return The updated configuration.
 */
protected Future<NetworkConfig> loadNetworkConfig(final String fileName, final NetworkConfig networkConfig) {
    final Promise<NetworkConfig> result = Promise.promise();
    if (!Strings.isNullOrEmpty(fileName)) {
        vertx.fileSystem().readFile(fileName, readAttempt -> {
            if (readAttempt.succeeded()) {
                try (InputStream is = new ByteArrayInputStream(readAttempt.result().getBytes())) {
                    networkConfig.load(is);
                    result.complete(networkConfig);
                } catch (final IOException e) {
                    LOG.warn("error malformed NetworkConfig properties [{}]", fileName);
                    result.fail(e);
                }
            } else {
                LOG.warn("error reading NetworkConfig file [{}]", fileName, readAttempt.cause());
                result.fail(readAttempt.cause());
            }
        });
    } else {
        result.complete(networkConfig);
    }
    return result.future();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NetworkConfig(org.eclipse.californium.core.network.config.NetworkConfig) IOException(java.io.IOException)

Aggregations

NetworkConfig (org.eclipse.californium.core.network.config.NetworkConfig)14 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)4 File (java.io.File)3 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 CoapServer (org.eclipse.californium.core.CoapServer)3 DTLSConnector (org.eclipse.californium.scandium.DTLSConnector)3 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)3 LeshanClientBuilder (org.eclipse.leshan.client.californium.LeshanClientBuilder)3 Server (org.eclipse.leshan.client.object.Server)3 LwM2mObjectEnabler (org.eclipse.leshan.client.resource.LwM2mObjectEnabler)3 ObjectsInitializer (org.eclipse.leshan.client.resource.ObjectsInitializer)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 PrivateKey (java.security.PrivateKey)2 Certificate (java.security.cert.Certificate)2 Endpoint (org.eclipse.californium.core.network.Endpoint)2 MemoryBasedConnectionLimitStrategy (org.eclipse.hono.adapter.limiting.MemoryBasedConnectionLimitStrategy)2 Server (org.eclipse.jetty.server.Server)2 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)2