For certificate-based security, you must provide a CertificateProvider to supply your credentials and a CertificateVerifier to verify the peer's certificates.
Key Components:
CertificateProvider: Use SingleCertificateProvider for simple setups or KeyManagerCertificateProvider to support multiple certificates (e.g., for SNI or different algorithms).CertificateVerifier: Use StaticCertificateVerifier for basic verification.SslContextUtil: A utility to help load credentials from files or key-stores.
Anonymous Clients (Californium 4.0+):
In version 4.0+, a server can authenticate itself via certificate while the client remains anonymous in the DTLS handshake. In this case, the application must authorize the client using an ApplicationAuthorizer (available via CoapEndpoint or Exchange).
// Load credentials using SslContextUtil
Credentials serverCredentials = SslContextUtil.loadCredentials(...);
Credentials serverTrusts = SslContextUtil.loadCredentials(...);
DtlsConnectorConfig.Builder builder = DtlsConnectorConfig.builder(configuration);
builder.setAddress(new InetSocketAddress(5684));
// Set up the identity provider
SingleCertificateProvider certificate = new SingleCertificateProvider(
serverCredentials.getPrivateKey(),
serverCredentials.getCertificateChain()
);
builder.setCertificateIdentityProvider(certificate);
// Set up the verifier
CertificateVerifier trust = StaticCertificateVerifier.builder()
.setTrustedCertificates(serverTrusts.getTrustedCertificates)
.build();
builder.setCertificateVerifier(trust);
DTLSConnector connector = new DTLSConnector(builder.build());