|
ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
|
Layer: L7 Application · Build flags: PC_ENABLE_COAP, PC_ENABLE_DTLS
This is CoAP secured. CoAP normally runs as plaintext UDP; on the open Internet you want coaps:// - CoAP inside a DTLS 1.3 session (RFC 7252 §9), the datagram counterpart to HTTPS. The same resources (/info, /led, /hello) are served, but every request and response travels encrypted and authenticated.
The resource table is transport-independent. You register resources exactly once with pc_coap_server_add_resource(); the plaintext server (pc_coap_server_begin) and this secure front-end both dispatch against the same table. Here we bind only the secure one:
One poll drives everything. coaps_server owns a small pool of DTLS connections keyed by peer address. pc_coaps_server_poll() - called every loop - routes each queued datagram to its connection, runs the handshake (or decrypts a CoAP request, answers it, and re-encrypts), fires the DTLS retransmission timer so a lost handshake flight is re-sent (RFC 9147 §5.8), and reaps idle connections:
The profile is TLS_AES_128_GCM_SHA256 + X25519 + an Ed25519 server certificate. A client that does not offer an X25519 key_share up front is answered with a HelloRetryRequest carrying an address-bound cookie and renegotiates the group, so any conformant DTLS 1.3 client interoperates.
The certificate in the sketch is a throwaway demo key - regenerate your own before using this for anything real. The header comment in CoapSecure.ino has the exact
opensslcommands (genpkey -algorithm ed25519→ raw seed + a self-signed DER leaf certificate).
Then, from a host with a DTLS 1.3 CoAP client (accept the demo certificate):
The verified reference client is the wolfSSL harness in test/servers/dtls_wolfssl, which completes the handshake and an application-data round trip against this same server core.
See also CoAP (plaintext), CoapObserve (server push), and CoapBlock (large transfers).