Skip to content

Commit

Permalink
Put in RPK and callbacks for supporting certificates (#48)
Browse files Browse the repository at this point in the history
Setup TLS to have callbacks so that one can look at things like ACE tokens being passed as the PSK identifier.
  • Loading branch information
jimsch committed Jun 16, 2018
1 parent 727780f commit ddea0e2
Show file tree
Hide file tree
Showing 11 changed files with 872 additions and 83 deletions.
5 changes: 3 additions & 2 deletions CoAP.NET/CoAP.NET45.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -109,6 +109,7 @@
<Compile Include="DTLS\DtlsServer.cs" />
<Compile Include="DTLS\DTLSSession.cs" />
<Compile Include="DTLS\QueueItem.cs" />
<Compile Include="DTLS\TlsEvent.cs" />
<Compile Include="EmptyMessage.cs" />
<Compile Include="EndPoint\Resources\RemoteResource.cs" />
<Compile Include="EndPoint\Resources\Resource.cs" />
Expand Down Expand Up @@ -205,4 +206,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
14 changes: 14 additions & 0 deletions CoAP.NET/DTLS/DTLSChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class DTLSChannel : IChannel
private UDPChannel _udpChannel;
private KeySet _serverKeys;
private KeySet _userKeys;


public DTLSChannel(KeySet serverKeys, KeySet userKeys) : this(serverKeys, userKeys, 0)
{
Expand Down Expand Up @@ -86,6 +87,8 @@ public Int32 ReceivePacketSize {

private Int32 _running;

public EventHandler<TlsEvent> TlsEventHandler;

public void Start()
{
if (System.Threading.Interlocked.CompareExchange(ref _running, 1, 0) > 0) {
Expand Down Expand Up @@ -164,6 +167,7 @@ public ISession GetSession(System.Net.EndPoint ep)

session = new DTLSSession(ipEndPoint, DataReceived, _serverKeys, _userKeys);
AddSession(session);
session.TlsEventHandler += MyTlsEventHandler;


session.Connect(_udpChannel);
Expand All @@ -175,6 +179,14 @@ public ISession GetSession(System.Net.EndPoint ep)
return session;
}

private void MyTlsEventHandler(Object o, TlsEvent e)
{
EventHandler<TlsEvent> handler = TlsEventHandler;
if (handler != null) {
handler(o, e);
}
}

public void Send(byte[] data, ISession sessionReceive, System.Net.EndPoint ep)
{
try {
Expand All @@ -184,6 +196,7 @@ public void Send(byte[] data, ISession sessionReceive, System.Net.EndPoint ep)
if (session == null) {

session = new DTLSSession(ipEP, DataReceived, _serverKeys, _userKeys);
session.TlsEventHandler += MyTlsEventHandler;
AddSession(session);
session.Connect(_udpChannel);
}
Expand Down Expand Up @@ -211,6 +224,7 @@ private void ReceiveData(Object sender, DataReceivedEventArgs e)
}

DTLSSession sessionNew = new DTLSSession((IPEndPoint) e.EndPoint, DataReceived, _serverKeys, _userKeys);
sessionNew.TlsEventHandler = MyTlsEventHandler;
_sessionList.Add(sessionNew);
new Thread(() => Accept(sessionNew, e.Data)).Start();
}
Expand Down
Loading

0 comments on commit ddea0e2

Please sign in to comment.