Search in sources :

Example 16 with Configuration

use of org.eclipse.californium.elements.config.Configuration in project leshan by eclipse.

the class SecureIntegrationTestHelper method createX509CertClient.

public void createX509CertClient(X509Certificate[] clientCertificate, PrivateKey privatekey, List<Certificate> clientTrustStore, X509Certificate serverCertificate, CertificateUsage certificateUsage) throws CertificateEncodingException {
    /* Make sure there is only 1 certificate in chain before client certificate chains are supported */
    assert (clientCertificate.length == 1);
    ObjectsInitializer initializer = new TestObjectsInitializer();
    initializer.setInstancesForObject(LwM2mId.SECURITY, Security.x509("coaps://" + server.getSecuredAddress().getHostString() + ":" + server.getSecuredAddress().getPort(), 12345, clientCertificate[0].getEncoded(), privatekey.getEncoded(), serverCertificate.getEncoded(), certificateUsage.code));
    initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME));
    initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345"));
    initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, DummyInstanceEnabler.class);
    List<LwM2mObjectEnabler> objects = initializer.createAll();
    InetSocketAddress clientAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
    Configuration configuration = LeshanClientBuilder.createDefaultCoapConfiguration();
    LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
    builder.setLocalAddress(clientAddress.getHostString(), clientAddress.getPort());
    builder.setTrustStore(clientTrustStore);
    Builder dtlsConfig = DtlsConnectorConfig.builder(configuration);
    dtlsConfig.set(DtlsConfig.DTLS_ROLE, DtlsRole.CLIENT_ONLY);
    builder.setDtlsConfig(dtlsConfig);
    builder.setObjects(objects);
    client = builder.build();
    setupClientMonitoring();
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) Server(org.eclipse.leshan.client.object.Server) Configuration(org.eclipse.californium.elements.config.Configuration) 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) LeshanServerBuilder(org.eclipse.leshan.server.californium.LeshanServerBuilder) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) Builder(org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder)

Example 17 with Configuration

use of org.eclipse.californium.elements.config.Configuration in project leshan by eclipse.

the class SecureIntegrationTestHelper method createPSKClient.

public void createPSKClient(boolean queueMode) {
    ObjectsInitializer initializer = new TestObjectsInitializer();
    initializer.setInstancesForObject(LwM2mId.SECURITY, Security.psk("coaps://" + server.getSecuredAddress().getHostString() + ":" + server.getSecuredAddress().getPort(), 12345, GOOD_PSK_ID.getBytes(StandardCharsets.UTF_8), GOOD_PSK_KEY));
    initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME));
    initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345"));
    initializer.setDummyInstancesForObject(LwM2mId.ACCESS_CONTROL);
    List<LwM2mObjectEnabler> objects = initializer.createAll();
    InetSocketAddress clientAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
    Configuration configuration = LeshanClientBuilder.createDefaultCoapConfiguration();
    LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
    builder.setRegistrationEngineFactory(new DefaultRegistrationEngineFactory().setQueueMode(queueMode));
    builder.setLocalAddress(clientAddress.getHostString(), clientAddress.getPort());
    builder.setObjects(objects);
    builder.setDtlsConfig(DtlsConnectorConfig.builder(configuration).setAsList(DtlsConfig.DTLS_CIPHER_SUITES, CipherSuite.TLS_PSK_WITH_AES_128_CCM_8));
    // set an editable PSK store for tests
    builder.setEndpointFactory(new EndpointFactory() {

        @Override
        public CoapEndpoint createUnsecuredEndpoint(InetSocketAddress address, Configuration coapConfig, ObservationStore store) {
            CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
            builder.setInetSocketAddress(address);
            builder.setConfiguration(coapConfig);
            return builder.build();
        }

        @Override
        public CoapEndpoint createSecuredEndpoint(DtlsConnectorConfig dtlsConfig, Configuration coapConfig, ObservationStore store) {
            CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
            Builder dtlsConfigBuilder = DtlsConnectorConfig.builder(dtlsConfig);
            // tricks to be able to change psk information on the fly
            AdvancedPskStore pskStore = dtlsConfig.getAdvancedPskStore();
            if (pskStore != null) {
                PskPublicInformation identity = pskStore.getIdentity(null, null);
                SecretKey key = pskStore.requestPskSecretResult(ConnectionId.EMPTY, null, identity, null, null, null, false).getSecret();
                singlePSKStore = new SinglePSKStore(identity, key);
                dtlsConfigBuilder.setAdvancedPskStore(singlePSKStore);
            }
            builder.setConnector(new DTLSConnector(dtlsConfigBuilder.build()));
            builder.setConfiguration(coapConfig);
            return builder.build();
        }
    });
    // create client;
    client = builder.build();
    setupClientMonitoring();
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) DefaultRegistrationEngineFactory(org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory) Server(org.eclipse.leshan.client.object.Server) Configuration(org.eclipse.californium.elements.config.Configuration) 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) EndpointFactory(org.eclipse.leshan.core.californium.EndpointFactory) LeshanServerBuilder(org.eclipse.leshan.server.californium.LeshanServerBuilder) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) Builder(org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder) ObservationStore(org.eclipse.californium.core.observe.ObservationStore) PskPublicInformation(org.eclipse.californium.scandium.dtls.PskPublicInformation) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) SecretKey(javax.crypto.SecretKey) AdvancedPskStore(org.eclipse.californium.scandium.dtls.pskstore.AdvancedPskStore) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint)

Example 18 with Configuration

use of org.eclipse.californium.elements.config.Configuration in project thingsboard by thingsboard.

the class DefaultCoapServerService method createCoapServer.

private CoapServer createCoapServer() throws UnknownHostException {
    Configuration networkConfig = new Configuration();
    networkConfig.set(CoapConfig.BLOCKWISE_STRICT_BLOCK2_OPTION, true);
    networkConfig.set(CoapConfig.BLOCKWISE_ENTITY_TOO_LARGE_AUTO_FAILOVER, true);
    networkConfig.set(CoapConfig.BLOCKWISE_STATUS_LIFETIME, DEFAULT_BLOCKWISE_STATUS_LIFETIME_IN_SECONDS, TimeUnit.SECONDS);
    networkConfig.set(CoapConfig.MAX_RESOURCE_BODY_SIZE, 256 * 1024 * 1024);
    networkConfig.set(CoapConfig.RESPONSE_MATCHING, CoapConfig.MatcherMode.RELAXED);
    networkConfig.set(CoapConfig.PREFERRED_BLOCK_SIZE, 1024);
    networkConfig.set(CoapConfig.MAX_MESSAGE_SIZE, 1024);
    networkConfig.set(CoapConfig.MAX_RETRANSMIT, 4);
    networkConfig.set(CoapConfig.COAP_PORT, coapServerContext.getPort());
    server = new CoapServer(networkConfig);
    CoapEndpoint.Builder noSecCoapEndpointBuilder = new CoapEndpoint.Builder();
    InetAddress addr = InetAddress.getByName(coapServerContext.getHost());
    InetSocketAddress sockAddr = new InetSocketAddress(addr, coapServerContext.getPort());
    noSecCoapEndpointBuilder.setInetSocketAddress(sockAddr);
    noSecCoapEndpointBuilder.setConfiguration(networkConfig);
    CoapEndpoint noSecCoapEndpoint = noSecCoapEndpointBuilder.build();
    server.addEndpoint(noSecCoapEndpoint);
    if (isDtlsEnabled()) {
        CoapEndpoint.Builder dtlsCoapEndpointBuilder = new CoapEndpoint.Builder();
        TbCoapDtlsSettings dtlsSettings = coapServerContext.getDtlsSettings();
        DtlsConnectorConfig dtlsConnectorConfig = dtlsSettings.dtlsConnectorConfig(networkConfig);
        networkConfig.set(CoapConfig.COAP_SECURE_PORT, dtlsConnectorConfig.getAddress().getPort());
        dtlsCoapEndpointBuilder.setConfiguration(networkConfig);
        DTLSConnector connector = new DTLSConnector(dtlsConnectorConfig);
        dtlsCoapEndpointBuilder.setConnector(connector);
        CoapEndpoint dtlsCoapEndpoint = dtlsCoapEndpointBuilder.build();
        server.addEndpoint(dtlsCoapEndpoint);
        tbDtlsCertificateVerifier = (TbCoapDtlsCertificateVerifier) dtlsConnectorConfig.getAdvancedCertificateVerifier();
        dtlsSessionsExecutor = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName(getClass().getSimpleName()));
        dtlsSessionsExecutor.scheduleAtFixedRate(this::evictTimeoutSessions, new Random().nextInt((int) getDtlsSessionReportTimeout()), getDtlsSessionReportTimeout(), TimeUnit.MILLISECONDS);
    }
    Resource root = server.getRoot();
    TbCoapServerMessageDeliverer messageDeliverer = new TbCoapServerMessageDeliverer(root);
    server.setMessageDeliverer(messageDeliverer);
    server.start();
    return server;
}
Also used : Configuration(org.eclipse.californium.elements.config.Configuration) InetSocketAddress(java.net.InetSocketAddress) Resource(org.eclipse.californium.core.server.resources.Resource) CoapServer(org.eclipse.californium.core.CoapServer) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) Random(java.util.Random) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) InetAddress(java.net.InetAddress)

Example 19 with Configuration

use of org.eclipse.californium.elements.config.Configuration in project thingsboard by thingsboard.

the class SecureClientX509 method main.

public static void main(String[] args) throws URISyntaxException {
    System.out.println("Usage: java -cp ... org.thingsboard.server.transport.coap.client.SecureClientX509 " + "host port keyStoreUriPath keyStoreAlias trustedAliasPattern clientKeys sharedKeys");
    String host = args[0];
    int port = Integer.parseInt(args[1]);
    String clientKeys = args[6];
    String sharedKeys = args[7];
    String keyStoreUriPath = args[2];
    String keyStoreAlias = args[3];
    String trustedAliasPattern = args[4];
    String keyStorePassword = args[5];
    DtlsConnectorConfig.Builder builder = new DtlsConnectorConfig.Builder(new Configuration());
    setupCredentials(builder, keyStoreUriPath, keyStoreAlias, trustedAliasPattern, keyStorePassword);
    DTLSConnector dtlsConnector = new DTLSConnector(builder.build());
    SecureClientX509 client = new SecureClientX509(dtlsConnector, host, port, clientKeys, sharedKeys);
    client.test();
}
Also used : Configuration(org.eclipse.californium.elements.config.Configuration) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector)

Example 20 with Configuration

use of org.eclipse.californium.elements.config.Configuration in project californium by eclipse.

the class LibCoapClientMbedTlsInteroperabilityTest method start.

@Before
public void start() {
    processUtil.setTag(name.getName());
    // mbedtls v2.27 still supports only the deprecated MAC calculation.
    // Ensure/adjust the extension id in mbedtls - include/mbedtls/ssl.h
    // for compatibility to 53
    // 
    // #define MBEDTLS_TLS_EXT_CID                        53
    // 
    // For libcoap enable the passive use of CID in src/coap_mbedtls.c,
    // coap_dtls_new_mbedtls_env, before mbedtls_ssl_set_bio with
    // 
    // mbedtls_ssl_set_cid(&m_env->ssl, MBEDTLS_SSL_CID_ENABLED, NULL, 0);
    builder = DtlsConnectorConfig.builder(new Configuration()).set(DtlsConfig.DTLS_SUPPORT_DEPRECATED_CID, true);
}
Also used : Configuration(org.eclipse.californium.elements.config.Configuration) Before(org.junit.Before)

Aggregations

Configuration (org.eclipse.californium.elements.config.Configuration)152 Test (org.junit.Test)55 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)52 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)44 CipherSuite (org.eclipse.californium.scandium.dtls.cipher.CipherSuite)30 CoapServer (org.eclipse.californium.core.CoapServer)24 InetSocketAddress (java.net.InetSocketAddress)21 Before (org.junit.Before)19 DTLSConnector (org.eclipse.californium.scandium.DTLSConnector)16 Endpoint (org.eclipse.californium.core.network.Endpoint)14 CoapResource (org.eclipse.californium.core.CoapResource)7 MessageTracer (org.eclipse.californium.core.network.interceptors.MessageTracer)7 Response (org.eclipse.californium.core.coap.Response)6 Exchange (org.eclipse.californium.core.network.Exchange)6 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)6 AdvancedSinglePskStore (org.eclipse.californium.scandium.dtls.pskstore.AdvancedSinglePskStore)6 InetAddress (java.net.InetAddress)5 CoapClient (org.eclipse.californium.core.CoapClient)5 Request (org.eclipse.californium.core.coap.Request)5 Builder (org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder)5