shamblett / coap

A Coap package for dart
Other
16 stars 13 forks source link

Pure dart dtls #166

Open vincent-iQontrol opened 1 year ago

vincent-iQontrol commented 1 year ago

Is it possible to use just pure dart for DTLS instead of openssl like this:

` import 'dart:io'; import 'dart:async';

Future createDtlsConnection() async { // Create a SecurityContext object final securityContext = SecurityContext(); securityContext.setAlpnProtocols(['dtls1.2'], true); securityContext.setPSKIdentity("client_identity"); securityContext.setPSK("client_secret".codeUnits);

// Create a SecureSocket object
final socket = await SecureSocket.connect(
'dtls.example.com', 
8443, 
context: securityContext,
);

// Send and receive data over the socket
socket.write('Hello, DTLS!');
final response = await socket.read();
print(String.fromCharCodes(response));

} `

or

`import 'dart:io'; import 'dart:async';

Future createDtlsConnection() async { // Load the certificate and private key from files final certificate = await File('path/to/certificate.pem').readAsString(); final privateKey = await File('path/to/private_key.pem').readAsString(); final caCertificate = await File('path/to/ca_certificate.pem').readAsString();

// Create a SecurityContext object final securityContext = SecurityContext(); securityContext.useCertificateChainBytes(certificate.codeUnits); securityContext.usePrivateKeyBytes(privateKey.codeUnits); securityContext.setTrustedCertificatesBytes(caCertificate.codeUnits);

// Create a SecureSocket object final socket = await SecureSocket.connect( 'dtls.example.com', 8443, context: securityContext, );

// Send and receive data over the socket socket.write('Hello, DTLS!'); final response = await socket.read(); print(String.fromCharCodes(response)); }`

JKRhb commented 1 year ago

That is definitely the goal and would be very desirable! However, the SecureSocket class from dart:io currently only supports TLS, therefore the code you posted does not work at the moment :/ (Also see this issue: https://github.com/dart-lang/sdk/issues/43378) However, based on the foundations laid in the DTLS packages, maybe it will be possible to create an integration into the Dart SDK in the not too distant future :)