Search in sources :

Example 1 with AddressEndpointContext

use of org.eclipse.californium.elements.AddressEndpointContext in project leshan by eclipse.

the class RegistrationTest method register_with_invalid_request.

@Test
public void register_with_invalid_request() throws InterruptedException, IOException {
    // Check registration
    helper.assertClientNotRegisterered();
    // create a register request without the list of supported object
    Request coapRequest = new Request(Code.POST);
    coapRequest.setDestinationContext(new AddressEndpointContext(helper.server.getUnsecuredAddress()));
    coapRequest.getOptions().setContentFormat(ContentFormat.LINK.getCode());
    coapRequest.getOptions().addUriPath("rd");
    coapRequest.getOptions().addUriQuery("ep=" + helper.currentEndpointIdentifier);
    // send request
    CoapEndpoint.CoapEndpointBuilder builder = new CoapEndpoint.CoapEndpointBuilder();
    builder.setInetSocketAddress(new InetSocketAddress(0));
    CoapEndpoint coapEndpoint = builder.build();
    coapEndpoint.start();
    coapEndpoint.sendRequest(coapRequest);
    // check response
    Response response = coapRequest.waitForResponse(1000);
    assertEquals(response.getCode(), org.eclipse.californium.core.coap.CoAP.ResponseCode.BAD_REQUEST);
    coapEndpoint.stop();
}
Also used : Response(org.eclipse.californium.core.coap.Response) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) RegisterResponse(org.eclipse.leshan.core.response.RegisterResponse) InetSocketAddress(java.net.InetSocketAddress) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest) Request(org.eclipse.californium.core.coap.Request) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Test(org.junit.Test)

Example 2 with AddressEndpointContext

use of org.eclipse.californium.elements.AddressEndpointContext in project leshan by eclipse.

the class ObserveTest method sendNotification.

private void sendNotification(Connector connector, byte[] payload, Response firstCoapResponse, int contentFormat) {
    // create observe response
    Response response = new Response(org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT);
    response.setType(Type.NON);
    response.setPayload(payload);
    response.setMID(firstCoapResponse.getMID() + 1);
    response.setToken(firstCoapResponse.getToken());
    OptionSet options = new OptionSet().setContentFormat(contentFormat).setObserve(firstCoapResponse.getOptions().getObserve() + 1);
    response.setOptions(options);
    EndpointContext context = new AddressEndpointContext(helper.server.getUnsecuredAddress().getAddress(), helper.server.getUnsecuredAddress().getPort());
    response.setDestinationContext(context);
    // serialize response
    UdpDataSerializer serializer = new UdpDataSerializer();
    RawData data = serializer.serializeResponse(response);
    // send it
    connector.send(data);
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) RawData(org.eclipse.californium.elements.RawData) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) OptionSet(org.eclipse.californium.core.coap.OptionSet) UdpDataSerializer(org.eclipse.californium.core.network.serialization.UdpDataSerializer)

Example 3 with AddressEndpointContext

use of org.eclipse.californium.elements.AddressEndpointContext in project leshan by eclipse.

the class SecurityTest method dont_sent_request_if_identity_change.

@Test
public void dont_sent_request_if_identity_change() throws NonUniqueSecurityInfoException, InterruptedException, IOException {
    // Create PSK server & start it
    // default server support PSK
    helper.createServer();
    helper.server.start();
    // Create PSK Client
    helper.createPSKClient();
    // Add client credentials to the server
    helper.getSecurityStore().add(SecurityInfo.newPreSharedKeyInfo(helper.getCurrentEndpoint(), GOOD_PSK_ID, GOOD_PSK_KEY));
    // Check client is not registered
    helper.assertClientNotRegisterered();
    // Start it and wait for registration
    helper.client.start();
    helper.waitForRegistration(1);
    // Check client is well registered
    helper.assertClientRegisterered();
    // Ensure we can send a read request
    helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 1));
    // Pause the client
    // helper.client.stop(false);
    // Add new credential to the server
    helper.getSecurityStore().add(SecurityInfo.newPreSharedKeyInfo(GOOD_ENDPOINT, "anotherPSK", GOOD_PSK_KEY));
    // Get connector
    Endpoint endpoint = helper.client.getCoapServer().getEndpoint(helper.client.getSecuredAddress());
    DTLSConnector connector = (DTLSConnector) ((CoapEndpoint) endpoint).getConnector();
    // Clear DTLS session to force new handshake
    connector.clearConnectionState();
    // Change PSK idea
    helper.setNewPsk(helper.client, "anotherPSK");
    // restart connector
    connector.start();
    // send and empty message to force a new handshake with new credentials
    SimpleMessageCallback callback = new SimpleMessageCallback();
    connector.send(RawData.outbound(new byte[0], new AddressEndpointContext(helper.server.getSecuredAddress()), callback, false));
    // Wait until new handshake DTLS is done
    EndpointContext endpointContext = callback.getEndpointContext(1000);
    assertEquals(endpointContext.getPeerIdentity().getName(), "anotherPSK");
    // Try to send a read request this should failed with an SendFailedException.
    try {
        helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 1), 1000);
        fail("send must failed");
    } catch (SendFailedException e) {
        assertTrue("must be caused by an EndpointMismatchException", e.getCause() instanceof EndpointMismatchException);
    } finally {
        connector.stop();
        helper.client.destroy(false);
        helper.client = null;
    }
}
Also used : SendFailedException(org.eclipse.leshan.core.request.exception.SendFailedException) EndpointMismatchException(org.eclipse.californium.elements.EndpointMismatchException) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) SimpleMessageCallback(org.eclipse.californium.elements.util.SimpleMessageCallback) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 4 with AddressEndpointContext

use of org.eclipse.californium.elements.AddressEndpointContext in project leshan by eclipse.

the class EndpointContextSerDes method deserialize.

public static EndpointContext deserialize(JsonObject peer) {
    String address = peer.get(KEY_ADDRESS).asString();
    int port = peer.get(KEY_PORT).asInt();
    InetSocketAddress socketAddress = new InetSocketAddress(address, port);
    Principal principal = null;
    JsonValue value = peer.get(KEY_ID);
    if (value != null) {
        principal = new PreSharedKeyIdentity(value.asString());
    } else if ((value = peer.get(KEY_RPK)) != null) {
        try {
            byte[] rpk = Hex.decodeHex(value.asString().toCharArray());
            X509EncodedKeySpec spec = new X509EncodedKeySpec(rpk);
            PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(spec);
            principal = new RawPublicKeyIdentity(publicKey);
        } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
            throw new IllegalStateException("Invalid security info content", e);
        }
    } else if ((value = peer.get(KEY_DN)) != null) {
        principal = new X500Principal(value.asString());
    }
    EndpointContext endpointContext;
    value = peer.get(KEY_ATTRIBUTES);
    if (value == null) {
        endpointContext = new AddressEndpointContext(socketAddress, principal);
    } else {
        int index = 0;
        String[] attributes = new String[value.asObject().size() * 2];
        for (Member member : value.asObject()) {
            attributes[index++] = member.getName();
            attributes[index++] = member.getValue().asString();
        }
        endpointContext = new MapBasedEndpointContext(socketAddress, principal, attributes);
    }
    return endpointContext;
}
Also used : AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) MapBasedEndpointContext(org.eclipse.californium.elements.MapBasedEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) InetSocketAddress(java.net.InetSocketAddress) PublicKey(java.security.PublicKey) RawPublicKeyIdentity(org.eclipse.californium.elements.auth.RawPublicKeyIdentity) JsonValue(com.eclipsesource.json.JsonValue) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) MapBasedEndpointContext(org.eclipse.californium.elements.MapBasedEndpointContext) X500Principal(javax.security.auth.x500.X500Principal) PreSharedKeyIdentity(org.eclipse.californium.elements.auth.PreSharedKeyIdentity) Member(com.eclipsesource.json.JsonObject.Member) X500Principal(javax.security.auth.x500.X500Principal) Principal(java.security.Principal)

Aggregations

AddressEndpointContext (org.eclipse.californium.elements.AddressEndpointContext)4 EndpointContext (org.eclipse.californium.elements.EndpointContext)3 InetSocketAddress (java.net.InetSocketAddress)2 Response (org.eclipse.californium.core.coap.Response)2 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)2 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)2 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)2 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)2 Test (org.junit.Test)2 Member (com.eclipsesource.json.JsonObject.Member)1 JsonValue (com.eclipsesource.json.JsonValue)1 Principal (java.security.Principal)1 PublicKey (java.security.PublicKey)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 X500Principal (javax.security.auth.x500.X500Principal)1 OptionSet (org.eclipse.californium.core.coap.OptionSet)1 Request (org.eclipse.californium.core.coap.Request)1 Endpoint (org.eclipse.californium.core.network.Endpoint)1 UdpDataSerializer (org.eclipse.californium.core.network.serialization.UdpDataSerializer)1 EndpointMismatchException (org.eclipse.californium.elements.EndpointMismatchException)1