Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regenerate bindings, adjust wrapper, add CI workflow #8

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
submodules: true
- uses: dart-lang/setup-dart@v1

- name: Install dependencies
Expand All @@ -23,6 +28,38 @@ jobs:
- name: Analyze project source
run: dart analyze

- name: Recompile tinydtls binary for Linux
if: matrix.os == 'ubuntu-latest'
run: |
cd third_party/tinydtls
./autogen.sh
./configure
make
cd ../..
mv third_party/tinydtls/libtinydtls.so libtinydtls.so

- name: Recompile tinydtls binary for Windows
if: matrix.os == 'windows-latest'
run: |
cd third_party/tinydtls
cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS=true .
make
cd ../..
mv third_party/tinydtls/libtinydtls.dll tinydtls.dll

- name: Recompile tinydtls binary for macOS
if: matrix.os == 'macos-latest'
run: |
# Remove the signature of the dart binary
# See https://github.com/dart-lang/sdk/issues/38314
codesign --remove-signature $DART_HOME/bin/dart

cd third_party/tinydtls
cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS=true .
make
cd ../..
cp third_party/tinydtls/*.dylib .

- name: Run tests with coverage
run: dart test --coverage="coverage"

Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "third_party/tinydtls"]
path = third_party/tinydtls
url = https://github.com/eclipse/tinydtls.git
branch = develop
branch = main
34 changes: 18 additions & 16 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import 'types.dart';
import 'util.dart';

int _handleWrite(Pointer<dtls_context_t> context, Pointer<session_t> session,
Pointer<ffi.Uint8> dataAddress, int dataLength) {
final data = dataAddress.asTypedList(dataLength).buffer.asUint8List();
Pointer<ffi.UnsignedChar> dataAddress, int dataLength) {
final data =
dataAddress.cast<Uint8>().asTypedList(dataLength).buffer.asUint8List();
final address = addressFromSession(session);
final port = portFromSession(session);

Expand All @@ -33,14 +34,14 @@ int _handleWrite(Pointer<dtls_context_t> context, Pointer<session_t> session,
}

int _handleRead(Pointer<dtls_context_t> context, Pointer<session_t> session,
Pointer<ffi.Uint8> dataAddress, int dataLength) {
Pointer<ffi.UnsignedChar> dataAddress, int dataLength) {
final connection = DtlsClientConnection._connections[context.address];

if (connection == null) {
return errorCode;
}

final data = dataAddress.asTypedList(dataLength);
final data = dataAddress.cast<Uint8>().asTypedList(dataLength);
final address = addressFromSession(session);
final port = portFromSession(session);

Expand All @@ -67,9 +68,9 @@ int _retrievePskInfo(
Pointer<dtls_context_t> context,
Pointer<session_t> session,
int type,
Pointer<ffi.Uint8> id,
Pointer<ffi.UnsignedChar> id,
int idLen,
Pointer<ffi.Uint8> result,
Pointer<ffi.UnsignedChar> result,
int resultLength,
) {
final connection = DtlsClientConnection._connections[context.address];
Expand All @@ -85,19 +86,19 @@ int _retrievePskInfo(
throw StateError("Expected a defined pskCallback.");
}

final idBytes = id.asTypedList(idLen);
final idBytes = id.cast<Uint8>().asTypedList(idLen);

switch (type) {
case dtls_credentials_type_t.DTLS_PSK_IDENTITY:
final pskCredentials = pskCallback(idBytes);
final _identity = pskCredentials.identity;
final identity = pskCredentials.identity;

if (resultLength < _identity.length) {
if (resultLength < identity.length) {
return createFatalError(dtls_alert_t.DTLS_ALERT_INTERNAL_ERROR);
}

final identityBytes = _identity;
result.asTypedList(resultLength).setAll(0, identityBytes);
final identityBytes = identity;
result.cast<Uint8>().asTypedList(resultLength).setAll(0, identityBytes);
connection._pskCredentials = pskCredentials;
return identityBytes.lengthInBytes;
case dtls_credentials_type_t.DTLS_PSK_KEY:
Expand All @@ -111,7 +112,7 @@ int _retrievePskInfo(
return createFatalError(dtls_alert_t.DTLS_ALERT_ILLEGAL_PARAMETER);
}

result.asTypedList(resultLength).setAll(0, psk);
result.cast<Uint8>().asTypedList(resultLength).setAll(0, psk);
return psk.lengthInBytes;
}
case dtls_credentials_type_t.DTLS_PSK_HINT:
Expand Down Expand Up @@ -140,8 +141,8 @@ int _retrieveEcdsaInfo(
int _verifyEcdsaKey(
ffi.Pointer<dtls_context_t> context,
ffi.Pointer<session_t> session,
ffi.Pointer<ffi.Uint8> publicKeyX,
ffi.Pointer<ffi.Uint8> publicKeyY,
ffi.Pointer<ffi.UnsignedChar> publicKeyX,
ffi.Pointer<ffi.UnsignedChar> publicKeyY,
int keySize) {
return success;
}
Expand Down Expand Up @@ -371,7 +372,8 @@ class DtlsClient {
int _send(List<int> data, Pointer<dtls_context_t> context,
Pointer<session_t> session) {
buffer.asTypedList(data.length).setAll(0, data);
final result = _tinyDtls.dtls_write(context, session, buffer, data.length);
final result =
_tinyDtls.dtls_write(context, session, buffer.cast(), data.length);

if (result < 0) {
throw DtlsException("Error sending DTLS message");
Expand All @@ -383,7 +385,7 @@ class DtlsClient {
void _receive(Uint8List data, Pointer<dtls_context_t> context,
Pointer<session_t> session) {
buffer.asTypedList(data.length).setAll(0, data);
_tinyDtls.dtls_handle_message(context, session, buffer, data.length);
_tinyDtls.dtls_handle_message(context, session, buffer.cast(), data.length);
}

/// Closes this [DtlsClient].
Expand Down
48 changes: 29 additions & 19 deletions lib/src/ecdsa_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ enum EcdsaCurve {

/// A component (private key, or x or y coordinate of the public key) of an
/// [EcdsaKeys] object.
class _KeyComponent {
final _ArgumentType _argumentType;
class KeyComponent {
final KeyComponentType _argumentType;

/// The list of bytes representing this component of the [EcdsaKeys].
final Uint8List byteArray;
Expand All @@ -42,7 +42,8 @@ class _KeyComponent {

final EcdsaCurve _ecdsaCurve;

_KeyComponent(this._argumentType, this.byteArray, this._ecdsaCurve) {
/// Constructor.
KeyComponent(this._argumentType, this.byteArray, this._ecdsaCurve) {
_verify();
}

Expand All @@ -51,11 +52,24 @@ class _KeyComponent {
throw _EcdsaValidationError(_ecdsaCurve, _argumentType, length);
}
}

Pointer<UnsignedChar> _createKeyPointer() {
final keySize = _ecdsaCurve.byteLength;
final pointer = malloc<Uint8>(keySize)
..asTypedList(keySize).setAll(0, byteArray);
return Pointer.fromAddress(pointer.address);
}
}

enum _ArgumentType {
/// The three different types of an ECDSA [KeyComponent].
enum KeyComponentType {
/// The x coordinate of the public key.
x,

/// The y coordinate of the public key.
y,

/// The private key.
private;

@override
Expand All @@ -71,7 +85,7 @@ enum _ArgumentType {
class _EcdsaValidationError extends ArgumentError {
final EcdsaCurve _ecdsaCurve;

final _ArgumentType _argumentType;
final KeyComponentType _argumentType;

final int _actualByteLength;

Expand All @@ -92,13 +106,13 @@ class EcdsaKeys {
final EcdsaCurve ecdsaCurve;

/// The private key.
final _KeyComponent privateKey;
final KeyComponent privateKey;

/// The x coordinate of the public key.
final _KeyComponent publicKeyX;
final KeyComponent publicKeyX;

/// The y coordinate of the public key.
final _KeyComponent publicKeyY;
final KeyComponent publicKeyY;

/// Constructor.
EcdsaKeys(
Expand All @@ -107,24 +121,20 @@ class EcdsaKeys {
required Uint8List publicKeyX,
required Uint8List publicKeyY,
}) : privateKey =
_KeyComponent(_ArgumentType.private, privateKey, ecdsaCurve),
publicKeyX = _KeyComponent(_ArgumentType.x, publicKeyX, ecdsaCurve),
publicKeyY = _KeyComponent(_ArgumentType.y, publicKeyY, ecdsaCurve);
KeyComponent(KeyComponentType.private, privateKey, ecdsaCurve),
publicKeyX = KeyComponent(KeyComponentType.x, publicKeyX, ecdsaCurve),
publicKeyY = KeyComponent(KeyComponentType.y, publicKeyY, ecdsaCurve);
}

/// Convert [EcdsaKeys] object to a Dart ffi [Pointer].
Pointer<dtls_ecdsa_key_t> ecdsaKeysToPointer(EcdsaKeys ecdsaKeys) {
final keySize = ecdsaKeys.ecdsaCurve.byteLength;
final ecdsaKeyStruct = malloc<dtls_ecdsa_key_t>();

ecdsaKeyStruct.ref
..priv_key = malloc<Uint8>(keySize)
..pub_key_x = malloc<Uint8>(keySize)
..pub_key_y = malloc<Uint8>(keySize)
..curve = ecdsaKeys.ecdsaCurve._internalEnumValue
..priv_key.asTypedList(keySize).setAll(0, ecdsaKeys.privateKey.byteArray)
..pub_key_x.asTypedList(keySize).setAll(0, ecdsaKeys.publicKeyX.byteArray)
..pub_key_y.asTypedList(keySize).setAll(0, ecdsaKeys.publicKeyY.byteArray);
..priv_key = ecdsaKeys.privateKey._createKeyPointer()
..pub_key_x = ecdsaKeys.publicKeyX._createKeyPointer()
..pub_key_y = ecdsaKeys.publicKeyY._createKeyPointer()
..curve = ecdsaKeys.ecdsaCurve._internalEnumValue;

return ecdsaKeyStruct;
}
Expand Down
Loading