chkr1011 / CoAPnet

CoAPnet is a high performance .NET library for CoAP based communication. It provides a CoAP client and a CoAP server. It also has DTLS support out of the box.
MIT License
70 stars 15 forks source link

Tradfri Pre Shared Key Generation #8

Closed DubbyMcDubs closed 3 years ago

DubbyMcDubs commented 3 years ago

Hi,

Great library! You have done an amazing amount of work.

One question, in the example for using IKEA Tradfri (https://github.com/chkr1011/CoAPnet/wiki/CoAP-Client-Examples), you mention the following link to create the pre shared key (https://github.com/home-assistant/core/issues/10252). I have tried to do this using the functions available in your library, trying various permutations of parameters etc, however I cannot get it to come back with a result due to timeouts. Is the basic functionality there in the DTLS libs to be able to string something together, or does there need to be additional work done in the WithDtlsTransportLayer function?

Kind Regards.

DubbyMcDubs commented 3 years ago

Solved. For anyone looking at doing the same thing, use the following code. The hint in getting it to work came from this line of code in another project (https://github.com/eriklupander/tradfri-go/blob/master/main.go#L170)

To create a PSK:

using (var LCoapClient = new CoapFactory().CreateClient())
{
    var LConnectOptions = new CoapClientConnectOptionsBuilder()
        .WithHost("INSERT IP ADDRESS HERE")
        .WithPort(Convert.ToInt32("INSERT PORT HERE")
        .WithDtlsTransportLayer(o => o.WithPreSharedKey("Client_identity", "INSERT SECURITY CODE ON GATEWAY HERE"))
        .Build();

    LCoapClient.ConnectAsync(LConnectOptions, CancellationToken.None).Wait();

    var LRequest = new CoapRequestBuilder()
            .WithMethod(CoapRequestMethod.Post)
            .WithPath("15011/9063")
            .WithPayload("{\"9090\":\"" + "INSERT YOUR CUSTOM CLIENT ID HERE" + "\"}")
            .Build();

    var LResponse = LCoapClient.RequestAsync(LRequest, CancellationToken.None).Result;
   //...use LResponse.Payload to get the new PSK
}
chkr1011 commented 3 years ago

Hi, thank you for your feedback! OK so you found it already 😄 I will close this ticket. If you have more questions please let me know.

Best regards Christian