Skip to content
This repository was archived by the owner on Apr 17, 2021. It is now read-only.

Commit 404dd15

Browse files
authored
Merge pull request #1 from boyvinall/master
fix Deserialise infinite loop for invalid record
2 parents 060f53b + 547e292 commit 404dd15

File tree

8 files changed

+82
-44
lines changed

8 files changed

+82
-44
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
7+
[Makefile]
8+
indent_style = tab

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.user
77
*.userosscache
88
*.sln.docstates
9+
.vscode
910

1011
# User-specific files (MonoDevelop/Xamarin Studio)
1112
*.userprefs
@@ -242,4 +243,4 @@ ModelManifest.xml
242243
.paket/paket.exe
243244

244245
# FAKE - F# Make
245-
.fake/
246+
.fake/

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DTLS_VERSION:=1.0.21
2+
3+
.PHONY: all
4+
all: src/DTLS.Net/bin/Release/DTLS.Net.$(DTLS_VERSION).nupkg
5+
6+
src/DTLS.Net/bin/Release/DTLS.Net.$(DTLS_VERSION).nupkg:
7+
docker run -v $(PWD):/app --entrypoint /app/pack.sh creatordev/dotnet-mono-base Release src/DTLS.Net
8+
9+
.PHONY: clean
10+
clean:
11+
rm -rf src/DTLS.Net/bin src/DTLS.Net/obj src/DTLS.Net/project.lock.json

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
## DTLS.Net
66

7-
[![License (3-Clause BSD)](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg?style=flat-square)](http://opensource.org/licenses/BSD-3-Clause)
7+
[![License (3-Clause BSD)](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg?style=flat-square)](http://opensource.org/licenses/BSD-3-Clause)
88

9-
DTLS.Net was developed for use in an [implementation](https://github.com/Creatordev/DeviceServer) of the Open Mobile Alliance's (OMA) Lightweight Machine to Machine protocol (LWM2M). For this reason it only supports the following cipher suites:
9+
DTLS.Net was developed for use in an [implementation](https://github.com/Creatordev/DeviceServer) of the Open Mobile Alliance's (OMA) Lightweight Machine to Machine protocol (LWM2M). For this reason it only supports the following cipher suites:
1010

1111
* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
1212
* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
@@ -16,38 +16,38 @@ DTLS.Net was developed for use in an [implementation](https://github.com/Creator
1616

1717
### Limitations
1818

19-
Since the client is only required to serve for the above project it has several inherent limitations:
19+
Since the client is only required to serve for the above project it has several inherent limitations:
2020

21-
1. No automatic retransmition
22-
2. No support for fragmentation of handshake packets
23-
3. Does not verify Server Certificates (against CA)
21+
1. No automatic retransmission
22+
2. No support for fragmentation of handshake packets
23+
3. Does not verify Server Certificates (against CA)
2424

25-
The server currently also has the following limitations:
25+
The server currently also has the following limitations:
2626

27-
1. Does not verify client Certificates
28-
2. No support for fragmentation of handshake packets
27+
1. Does not verify client Certificates
28+
2. No support for fragmentation of handshake packets
2929

3030
Hopefully over time these will be implemented, in the meantime we hope this is still useful.
3131

3232
----
3333

3434
### Contributing
3535

36-
We welcome all contributions to this project and we give credit where it's due. Anything from enhancing functionality to improving documentation and bug reporting - it's all good.
36+
We welcome all contributions to this project and we give credit where it's due. Anything from enhancing functionality to improving documentation and bug reporting - it's all good.
3737

38-
Find out more in the [contributor guide](CONTRIBUTING.md).
38+
Find out more in the [contributor guide](CONTRIBUTING.md).
3939

4040
### Credits
41-
42-
We would like to thank all of our current [contributors](CONTRIBUTORS).
41+
42+
We would like to thank all of our current [contributors](CONTRIBUTORS).
4343

4444

4545
----
4646

4747
### License information
4848

49-
* All code and documentation developed by Imagination Technologies Limited is licensed under the [BSD 3-clause license](LICENSE).
50-
* Bouncy Castle by The Legion of the Bouncy Castle is licensed under an [adaptation of the MIT X11 License](https://bouncycastle.org/csharp/licence.html).
49+
* All code and documentation developed by Imagination Technologies Limited is licensed under the [BSD 3-clause license](LICENSE).
50+
* Bouncy Castle by The Legion of the Bouncy Castle is licensed under an [adaptation of the MIT X11 License](https://bouncycastle.org/csharp/licence.html).
5151

5252

5353
----

pack.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
CONFIGURATION=$1
3+
shift
4+
5+
for PACKAGE in $*; do
6+
cd /app/$PACKAGE
7+
dotnet restore
8+
dotnet pack --configuration=$CONFIGURATION
9+
done

src/DTLS.Net/Client.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ 3. Neither the name of the copyright holder nor the names of its contributors ma
1212
products derived from this software without specific prior written permission.
1313
1414
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
15-
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
15+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1717
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
19-
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
18+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
19+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
2020
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2121
***********************************************************************************************************************/
2222

@@ -42,7 +42,7 @@ public class Client
4242
private static Version SupportedVersion = DTLSRecord.Version1_2;
4343

4444
private EndPoint _LocalEndPoint;
45-
private int _MaxPacketSize = 1440;
45+
private int _MaxPacketSize = 1440;
4646
private Socket _Socket;
4747
private List<TCipherSuite> _SupportedCipherSuites;
4848

@@ -130,7 +130,7 @@ private void ProcessHandshake(DTLSRecord record)
130130
{
131131

132132
int count = 0;
133-
while ((_Cipher == null) && (count < 50))
133+
while ((_Cipher == null) && (count < 500))
134134
{
135135
System.Threading.Thread.Sleep(10);
136136
count++;
@@ -616,7 +616,7 @@ private void SendHello(byte[] cookie)
616616

617617
clientHello.Extensions.Add(new Extension() { ExtensionType = TExtensionType.EncryptThenMAC});
618618
clientHello.Extensions.Add(new Extension() { ExtensionType = TExtensionType.ExtendedMasterSecret });
619-
619+
620620
EllipticCurvesExtension ellipticCurvesExtension = new EllipticCurvesExtension();
621621
for (int curve = 0; curve < (int)TEllipticCurve.secp521r1; curve++)
622622
{
@@ -769,7 +769,7 @@ private void StartReceive(Socket socket)
769769
socket.ReceiveFromAsync(parameters);
770770
}
771771

772-
public void SetVersion(Version version)
772+
public void SetVersion(Version version)
773773
{
774774
_Version = version;
775775
}

src/DTLS.Net/Records/DTLSRecord.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ All rights reserved.
44
55
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
66
following conditions are met:
7-
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8-
following disclaimer.
9-
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10-
following disclaimer in the documentation and/or other materials provided with the distribution.
11-
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
12-
products derived from this software without specific prior written permission.
7+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8+
following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10+
following disclaimer in the documentation and/or other materials provided with the distribution.
11+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
12+
products derived from this software without specific prior written permission.
1313
1414
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
15-
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
15+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1717
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
19-
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
18+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
19+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
2020
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2121
***********************************************************************************************************************/
2222

@@ -36,8 +36,8 @@ namespace DTLS
3636
internal class DTLSRecord
3737
{
3838
public static Version DefaultVersion = new Version(1, 0);
39-
public static Version Version1_0 = new Version(1, 0);
40-
public static Version Version1_2 = new Version(1, 2);
39+
public static Version Version1_0 = new Version(1, 0);
40+
public static Version Version1_2 = new Version(1, 2);
4141

4242
public const int RECORD_OVERHEAD = 13;
4343

@@ -58,7 +58,7 @@ internal class DTLSRecord
5858
// opaque fragment[DTLSPlaintext.length];
5959
//} DTLSPlaintext;
6060

61-
public TRecordType RecordType
61+
public TRecordType RecordType
6262
{
6363
get { return _RecordType; }
6464
set { _RecordType = value; }
@@ -85,7 +85,7 @@ public long SequenceNumber
8585
public byte[] Fragment
8686
{
8787
get { return _Fragment; }
88-
set
88+
set
8989
{
9090
_Fragment = value;
9191
if (_Fragment != null)
@@ -108,6 +108,7 @@ public static DTLSRecord Deserialise(Stream stream)
108108
{
109109
DTLSRecord result = new DTLSRecord();
110110
result._RecordType = (TRecordType)stream.ReadByte();
111+
// could check here for a valid type, and bail out if invalid
111112
result._Version = new Version(255 - stream.ReadByte(), 255 - stream.ReadByte());
112113
result._Epoch = NetworkByteOrderConverter.ToUInt16(stream);
113114
result._SequenceNumber = NetworkByteOrderConverter.ToInt48(stream);
@@ -116,10 +117,18 @@ public static DTLSRecord Deserialise(Stream stream)
116117
{
117118
result._Fragment = new byte[result._Length];
118119
int length = stream.Read(result._Fragment, 0, result._Length);
119-
while (length < result._Length)
120-
{
121-
length += stream.Read(result._Fragment, length, result._Length - length);
122-
}
120+
while (length < result._Length)
121+
{
122+
int bytesRead = stream.Read(result._Fragment, length, result._Length - length);
123+
if (bytesRead > 0)
124+
{
125+
length += bytesRead;
126+
}
127+
else
128+
{
129+
break;
130+
}
131+
}
123132
}
124133
return result;
125134
}

src/DTLS.Net/project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"version": "1.0.19-*",
2+
"version": "1.0.21-*",
33
"title": "DTLS.Net",
4-
"description": "DTLS.Net Class Library",
4+
"description": "DTLS.Net Class Library",
55
"authors": [ "Delme Thomas" ],
66
"packOptions": {
77
"owners": [ "Imagination Technologies Limited" ],

0 commit comments

Comments
 (0)