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

feat(binding-coap): add support for client certificates #184

Draft
wants to merge 7 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
2 changes: 2 additions & 0 deletions lib/binding_coap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/// [spec link]: https://www.w3.org/TR/wot-binding-templates/
library binding_coap;

export "package:coap/coap.dart" show ClientCertificate, ClientPrivateKey;

export "src/binding_coap/coap_client_factory.dart";
export "src/binding_coap/coap_config.dart";
export "src/binding_coap/coap_server.dart";
31 changes: 19 additions & 12 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class _InternalCoapConfig extends CoapConfigDefault {
dtlsCiphers = coapConfig.dtlsCiphers,
dtlsVerify = coapConfig.dtlsVerify,
dtlsWithTrustedRoots = coapConfig.dtlsWithTrustedRoots,
rootCertificates = coapConfig.rootCertificates;
rootCertificates = coapConfig.rootCertificates,
clientCertificate = coapConfig.clientCertificate,
clientPrivateKey = coapConfig.clientPrivateKey,
_verifyPrivateKey = coapConfig.verifyPrivateKey;

@override
final int preferredBlockSize;
Expand All @@ -42,6 +45,17 @@ class _InternalCoapConfig extends CoapConfigDefault {

@override
final List<Uint8List> rootCertificates;

@override
final coap.ClientCertificate? clientCertificate;

@override
final coap.ClientPrivateKey? clientPrivateKey;

final bool _verifyPrivateKey;

@override
bool get verifyPrivateKey => _verifyPrivateKey;
}

coap.PskCredentialsCallback? _createPskCallback(
Expand Down Expand Up @@ -161,6 +175,7 @@ final class CoapClient extends ProtocolClient
form,
pskCredentialsCallback: _pskCredentialsCallback,
),
initTimeout: _coapConfig?.initTimeout ?? const Duration(seconds: 10),
);

final request = await _createRequest(
Expand Down Expand Up @@ -421,22 +436,14 @@ final class CoapClient extends ProtocolClient
accept: form.accept,
);

final subprotocol = form.coapSubprotocol ?? operationType.subprotocol;

final coapClient = coap.CoapClient(
form.resolvedHref,
config: _InternalCoapConfig(_coapConfig ?? const CoapConfig()),
);

if (subprotocol == CoapSubprotocol.observe) {
final observeClientRelation = await coapClient.observe(request);
observeClientRelation.listen(handleResponse);
return CoapSubscription(coapClient, observeClientRelation, complete);
}

final response = await coapClient.send(request);
handleResponse(response);
return CoapSubscription(coapClient, null, complete);
final observeClientRelation = await coapClient.observe(request);
observeClientRelation.listen(handleResponse);
return CoapSubscription(coapClient, observeClientRelation, complete);
}

@override
Expand Down
8 changes: 1 addition & 7 deletions lib/src/binding_coap/coap_client_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import "../../core.dart";

import "coap_client.dart";
import "coap_config.dart";
import "coap_definitions.dart";

/// A [ProtocolClientFactory] that produces CoAP clients.
final class CoapClientFactory implements ProtocolClientFactory {
Expand Down Expand Up @@ -56,11 +55,6 @@ final class CoapClientFactory implements ProtocolClientFactory {
OperationType.unsubscribeevent,
];

if (observeOperations.contains(operationType)) {
return CoapSubprotocol.tryParse(subprotocol ?? "") ==
CoapSubprotocol.observe;
}

return subprotocol == null;
return observeOperations.contains(operationType);
}
}
17 changes: 17 additions & 0 deletions lib/src/binding_coap/coap_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import "dart:typed_data";

import "package:coap/coap.dart";
import "package:meta/meta.dart";

/// Allows for configuring the behavior of CoAP clients and servers.
Expand All @@ -22,8 +23,14 @@ class CoapConfig {
this.rootCertificates = const [],
this.dtlsWithTrustedRoots = true,
this.dtlsVerify = true,
this.clientCertificate,
this.clientPrivateKey,
this.verifyPrivateKey = false,
this.initTimeout,
});

final Duration? initTimeout;

/// Whether certificates should be verified by OpenSSL.
final bool dtlsVerify;

Expand Down Expand Up @@ -57,4 +64,14 @@ class CoapConfig {
///
/// Defaults to 60 seconds.
final Duration multicastDiscoveryTimeout;

/// Name of a file referring to a client certificate used with DTLS PKI mode.
final ClientCertificate? clientCertificate;

/// Name of a file referring to a private client key used with DTLS PKI mode.
final ClientPrivateKey? clientPrivateKey;

/// Whether the private key of the client certificate should be verified when
/// creating the DTLS context.
final bool verifyPrivateKey;
}
17 changes: 0 additions & 17 deletions lib/src/binding_coap/coap_definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,3 @@ enum CoapRequestMethod {
static CoapRequestMethod? fromString(String stringValue) =>
_registry[stringValue];
}

/// Enumeration of available CoAP subprotocols.
enum CoapSubprotocol {
/// Subprotocol for observing CoAP resources.
observe,
;

/// Tries to match the given [subprotocol] string to one of the known
/// [CoapSubprotocol.values].
static CoapSubprotocol? tryParse(String subprotocol) {
if (subprotocol == "cov:observe") {
return CoapSubprotocol.observe;
}

return null;
}
}
25 changes: 0 additions & 25 deletions lib/src/binding_coap/coap_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ extension CoapFormExtension on AugmentedForm {
bool get usesAutoScheme =>
securityDefinitions.whereType<AutoSecurityScheme>().isNotEmpty;

/// Get the [CoapSubprotocol] for this [AugmentedForm], if one is set.
CoapSubprotocol? get coapSubprotocol {
if (subprotocol == coapPrefixMapping.expandCurieString("observe")) {
return CoapSubprotocol.observe;
}

return null;
}

/// The Content-Format for CoAP request and response payloads.
CoapMediaType get contentFormat {
final formDefinition = _obtainVocabularyTerm<int>("contentFormat");
Expand Down Expand Up @@ -160,22 +151,6 @@ extension OperationTypeExtension on OperationType {
return CoapRequestMethod.get;
}
}

/// Determines the [CoapSubprotocol] (if any) for this [OperationType].
///
/// The only supported subprotocol at the moment is `observe`.
CoapSubprotocol? get subprotocol {
if ([
OperationType.subscribeevent,
OperationType.unsubscribeevent,
OperationType.observeproperty,
OperationType.unobserveproperty,
].contains(this)) {
return CoapSubprotocol.observe;
}

return null;
}
}

/// Extension for easily extracting the [content] from a [CoapResponse].
Expand Down
10 changes: 10 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ dependencies:
typed_data: ^1.3.2
uri: ^1.0.0
uuid: ^4.2.1

dependency_overrides:
coap:
git:
url: https://github.com/namib-project/coap
ref: client-certificate-support
dtls2:
git:
url: https://github.com/JKRhb/dtls2
ref: client-certificates
Loading