Skip to content

Commit

Permalink
Client x509 dtls (#58)
Browse files Browse the repository at this point in the history
* Test and get working X509 certs with DTLS

Still need to do TLS.

* Fixes for test error

* Let the start/stop run a bit longer
* Change test on null argument.
  • Loading branch information
jimsch authored Sep 17, 2019
1 parent 94c37f3 commit d5afdfc
Show file tree
Hide file tree
Showing 19 changed files with 547 additions and 319 deletions.
2 changes: 1 addition & 1 deletion CoAP.Example/CoAP.Client/ExampleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static void Main(String[] args)
if (payload != null) {
request.SetPayload(payload, MediaType.TextPlain);
}
if (oscoap != null) request.OscoapContext = oscoap;
if (oscoap != null) request.OscoreContext = oscoap;

// uncomment the next line if you want to specify a draft to use
// request.EndPoint = CoAP.Net.EndPointManager.Draft13;
Expand Down
208 changes: 111 additions & 97 deletions CoAP.NET/DTLS/DTLSClient.cs

Large diffs are not rendered by default.

34 changes: 26 additions & 8 deletions CoAP.NET/DTLS/DTLSClientChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ internal class DTLSClientChannel : IChannel
private Int32 _receivePacketSize;
private readonly int _port;
private UDPChannel _udpChannel;
private readonly OneKey _userKey;
#if SUPPORT_TLS_CWT
private readonly CWT _userCwt;
#endif
private KeySet CwtTrustKeySet { get; }
private readonly TlsKeyPair _userKey;
public KeySet CwtTrustKeySet { get; set; }

public EventHandler<TlsEvent> TlsEventHandler;

#if false
/// <summary>
/// Create a client only channel and use a randomly assigned port on
/// the client UDP port.
Expand All @@ -47,12 +45,19 @@ public DTLSClientChannel(OneKey userKey) : this(userKey, 0)
/// </summary>
/// <param name="userKey">Authentication Key</param>
/// <param name="port">client side UDP port</param>
public DTLSClientChannel(OneKey userKey, Int32 port)
public DTLSClientChannel(OneKey userKey, Intint32 port)
{
_port = port;
_userKey = userKey;
}

#endif
public DTLSClientChannel(TlsKeyPair userKey, int port)
{
_port = port;
_userKey = userKey ?? throw new ArgumentNullException(nameof(userKey));
}
#if false
#if SUPPORT_TLS_CWT
public DTLSClientChannel(CWT cwt, OneKey userKey, KeySet cwtTrustKeys, int port)
{
Expand All @@ -73,6 +78,19 @@ public DTLSClientChannel(OneKey userKey, System.Net.EndPoint ep)
_localEndPoint = ep;
_userKey = userKey;
}
#endif

/// <summary>
/// Create a client only channel and use a given endpoint
/// </summary>
/// <param name="userKey">Authentication Key</param>
/// <param name="ep">client side endpoint</param>
public DTLSClientChannel(TlsKeyPair userKey, System.Net.EndPoint ep)
{
_localEndPoint = ep;
_userKey = userKey ?? throw new ArgumentNullException(nameof(userKey));
}


/// <inheritdoc/>
public event EventHandler<DataReceivedEventArgs> DataReceived;
Expand Down Expand Up @@ -203,8 +221,8 @@ public ISession GetSession(System.Net.EndPoint ep)
// No session - create a new one.

#if SUPPORT_TLS_CWT
if (_userCwt != null) {
session = new DTLSSession(ipEndPoint, DataReceived, _userCwt, _userKey, CwtTrustKeySet);
if (CwtTrustKeySet != null) {
session = new DTLSSession(ipEndPoint, DataReceived, _userKey, CwtTrustKeySet);
}
else {
#endif
Expand Down
53 changes: 30 additions & 23 deletions CoAP.NET/DTLS/DTLSClientEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Com.AugustCellars.CoAP.Net;

using Com.AugustCellars.COSE;
using Org.BouncyCastle.Bcpg;
#if SUPPORT_TLS_CWT
using Com.AugustCellars.WebToken;
#endif
Expand All @@ -19,45 +20,29 @@ public class DTLSClientEndPoint : CoAPEndPoint
{
public EventHandler<TlsEvent> TlsEventHandler;

/// <summary>
/// Instantiates a new DTLS endpoint with the specific channel and configuration
/// </summary>
/// <param name="userKey">Authentication information</param>
public DTLSClientEndPoint(OneKey userKey) : this(userKey, 0, CoapConfig.Default)
{
}

#if SUPPORT_TLS_CWT
public DTLSClientEndPoint(CWT cwt, OneKey privKey, KeySet cwtTrustKeys) : this (new DTLSClientChannel(cwt, privKey, cwtTrustKeys, 0), CoapConfig.Default)
{
}
#endif

/// <summary>
/// Instantiates a new DTLS endpoint with the specific channel and configuration
/// </summary>
/// <param name="userKey">Authentication information</param>
/// <param name="config">Configuration info</param>
public DTLSClientEndPoint(OneKey userKey, ICoapConfig config) : this(userKey, 0, config)
{
}
{ }

/// <summary>
/// Instantiates a new DTLS endpoint with the specific channel and configuration
/// </summary>
/// <param name="userKey">Authentication information</param>
/// <param name="port">Client side port to use</param>
public DTLSClientEndPoint(OneKey userKey, Int32 port) : this(new DTLSClientChannel(userKey, port), CoapConfig.Default)
{
}
public DTLSClientEndPoint(OneKey userKey, int port=0) : this(userKey, port, CoapConfig.Default)
{ }

/// <summary>
/// Instantiates a new DTLS endpoint with the specific channel and configuration
/// </summary>
/// <param name="userKey">Authentication information</param>
/// <param name="port">Client side port to use</param>
/// <param name="config">Configuration info</param>
public DTLSClientEndPoint(OneKey userKey, Int32 port, ICoapConfig config) : this (new DTLSClientChannel(userKey, port), config)
public DTLSClientEndPoint(OneKey userKey, int port, ICoapConfig config) : this(new TlsKeyPair(userKey), port, config)
{ }

/// <summary>
Expand All @@ -75,9 +60,25 @@ public DTLSClientEndPoint(OneKey userKey, System.Net.EndPoint localEP) : this(us
/// <param name="userKey">Authentication information</param>
/// <param name="localEP">Client side endpoint to use</param>
/// <param name="config">Configuration info</param>
public DTLSClientEndPoint(OneKey userKey, System.Net.EndPoint localEP, ICoapConfig config) : this(new DTLSClientChannel(userKey, localEP), config)
{
}
public DTLSClientEndPoint(OneKey userKey, System.Net.EndPoint localEP, ICoapConfig config) : this(new TlsKeyPair(userKey), localEP, config)
{ }

public DTLSClientEndPoint(TlsKeyPair userKey, int port=0) : this(userKey, port, CoapConfig.Default)
{ }

public DTLSClientEndPoint(TlsKeyPair userKey, ICoapConfig config) : this(userKey, 0, config)
{ }

public DTLSClientEndPoint(TlsKeyPair userKey, int port, ICoapConfig config) : this (new DTLSClientChannel(userKey, port), config)
{ }


public DTLSClientEndPoint(TlsKeyPair userKey, System.Net.EndPoint localEndPoint) : this(userKey, localEndPoint, CoapConfig.Default)
{ }


public DTLSClientEndPoint(TlsKeyPair userKey, System.Net.EndPoint localEndPoint, ICoapConfig config) : this(new DTLSClientChannel(userKey, localEndPoint), config)
{ }

/// <summary>
/// Instantiates a new DTLS endpoint with the specific channel and configuration
Expand Down Expand Up @@ -122,5 +123,11 @@ private void OnTlsEvent(Object o, TlsEvent e)
}

}

public KeySet CwtTrustKeySet
{
get { return ((DTLSClientChannel) _channel).CwtTrustKeySet; }
set { ((DTLSClientChannel) _channel).CwtTrustKeySet = value; }
}
}
}
Loading

0 comments on commit d5afdfc

Please sign in to comment.