> For the complete documentation index, see [llms.txt](https://docs.akenza.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.akenza.io/device-management/your-data-flow/device-connector/coap/dtls-pre-shared-keys.md).

# DTLS Pre-Shared Keys

Authenticate CoAP devices over DTLS with a per-device pre-shared key

With DTLS pre-shared keys (PSK), each device shares a symmetric key with akenza. The device proves that it knows the key during the DTLS handshake, and the connection is encrypted with keys derived from it. No certificates are needed, which keeps the handshake small for constrained devices.

* **PSK identity:** the device ID of the device in akenza. The device sends it in the DTLS handshake.
* **Key:** one key per device, stored encrypted in akenza. akenza generates a 32-byte key, or you supply your own key of 16 to 64 bytes.

## Setup

### 1. Create the device connector

Create a CoAP device connector and select **DTLS with Pre-Shared Keys** as device authentication (Create Data Flow > Start with empty Data Flow > + Device Connector > CoAP > New CoAP Device Connector).

### 2. Create the device and its key

Create a device on a data flow with this device connector. The device ID is the PSK identity, so set it to the identifier the device sends in the handshake (e.g. its serial number or IMEI).

Add the key in the **Device Credentials** section, either in the device creation wizard or later on the device details tab:

* **Generate key** (default): akenza generates a random 32-byte key.
* **Use my own key:** enter a key of 16 to 64 bytes, as hex or base64. Use this for keys that were provisioned on the device at the factory. A value with hex digits only is read as hex.

Optionally set a validity date. The device can no longer connect with the key after this date.

{% hint style="warning" %}
The key is shown only once, in hex and base64, right after the credential is created. akenza can't display it again. Store it in your provisioning system before you confirm with **I've stored the key**. If the key gets lost, delete the credential and create a new one.
{% endhint %}

### 3. Provision the device

Configure the device with:

| Setting      | Value                                          |
| ------------ | ---------------------------------------------- |
| Endpoint     | `coaps://coap.akenza.io:5684`                  |
| PSK identity | The device ID                                  |
| PSK          | The key from step 2                            |
| Cipher suite | See [#cipher-suites](#cipher-suites "mention") |

Store the key securely on the device, e.g. in a secure element or a hardware-backed keystore, not in plain-text application files.

## Sending uplinks

<mark style="color:green;">`POST`</mark> `coaps://coap.akenza.io:5684/v3/capture`

The device is identified by the PSK identity, so the `secret` and `deviceId` query parameters are not needed. If `deviceId` is passed, it must match the PSK identity. Content formats, the `timestamp` and `topic` query parameters and the responses are the same as for plain CoAP (see [CoAP](https://docs.akenza.io/device-management/your-data-flow/device-connector/coap.md)).

Downlinks work the same way as with client certificates: the device pulls commands with `GET /v3/commands` over the DTLS connection, or receives them piggy-backed on an uplink.

## Cipher suites

akenza offers the recommended PSK and ECDHE\_PSK cipher suites, including:

| Cipher suite                              | Notes                                                                                                                                    |
| ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `TLS_PSK_WITH_AES_128_CCM_8`              | Mandatory to implement for CoAP ([RFC 7252 §9.1.3.1](https://datatracker.ietf.org/doc/html/rfc7252#section-9.1.3.1)), smallest handshake |
| `TLS_ECDHE_PSK_WITH_AES_128_CCM_8_SHA256` | Adds forward secrecy: a leaked key does not decrypt previously recorded traffic                                                          |

Use `TLS_ECDHE_PSK_WITH_AES_128_CCM_8_SHA256` if the device can afford the additional elliptic curve computation.

With PSK cipher suites, akenza does not present a server certificate. The device knows it talks to akenza because only akenza knows the key.

DTLS Connection IDs are supported as for certificates, see [CoAP](https://docs.akenza.io/device-management/your-data-flow/device-connector/coap.md#dtls-with-connection-id-cid).

## Key rotation and revocation

A device has only one active key. To rotate it, delete the credential on the device details tab, create a new one and provision the device with the new key. The device can't connect between the deletion and the re-provisioning.

Deleting the credential also revokes existing DTLS sessions: the next request of the device is answered with `4.01 Unauthorized`, and new handshakes fail.

## Troubleshooting

| Symptom                                | Cause                                                                                                                                                                                                                      |
| -------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| The DTLS handshake fails               | Unknown PSK identity, wrong key, no active key (deleted or expired), or the device's connector does not use **DTLS with Pre-Shared Keys**                                                                                  |
| `4.01 Unauthorized` after a handshake  | The key was deleted or expired after the session was established, the connector's auth type was changed, or `deviceId` does not match the PSK identity                                                                     |
| The server never answers the handshake | The device's first handshake message (ClientHello) is larger than one UDP datagram and is split in two. akenza drops split ClientHello messages. Offer fewer cipher suites and extensions, e.g. only the PSK cipher suites |

## Test the connection

### With OpenSSL

The following script checks the key and sends one uplink with `openssl s_client`. It offers only `TLS_PSK_WITH_AES_128_CCM_8`, which keeps the handshake small:

```bash
DEVICE_ID="B6.EC.9C.9C.6E.E2"   # <- the device ID in akenza
PSK_HEX="<key in hex>"          # <- the key shown when the credential was created
PAYLOAD='{"sensor":"temp","value":23.5}'

# CoAP POST /v3/capture with Content-Format application/json (50), followed by the payload
{ printf '\x41\x02\x12\x34\x01\xb2v3\x07capture\x11\x32\xff'; printf '%s' "${PAYLOAD}"; } > request.bin

(sleep 3; cat request.bin; sleep 5) | openssl s_client -dtls1_2 -quiet \
  -connect coap.akenza.io:5684 \
  -psk_identity "${DEVICE_ID}" \
  -psk "${PSK_HEX}" \
  -cipher 'PSK-AES128-CCM8:@SECLEVEL=0' \
  | xxd
```

A successful uplink returns a response starting with the bytes `61 41` (`ACK`, `2.01 Created`), followed by a payload like `{"id":"<uuid>","message":"ok"}`.

To check only the handshake, run the `openssl s_client` command without `-quiet` and without input. A working handshake prints `Cipher is PSK-AES128-CCM8`.

### With libcoap

You can also use `coap-client` from [libcoap](https://libcoap.net/install.html). Prefix a hex key with `0x`:

```bash
# Install via: 'brew install libcoap' or 'apt install libcoap2-bin'
DEVICE_ID="B6.EC.9C.9C.6E.E2"   # <- the device ID in akenza
PSK_HEX="<key in hex>"          # <- the key shown when the credential was created

coap-client -m post \
  -e '{"sensor":"temp","value":23.5}' \
  -t "application/json" \
  -u "${DEVICE_ID}" \
  -k "0x${PSK_HEX}" \
  "coaps://coap.akenza.io:5684/v3/capture"
```

A successful uplink returns `2.01 Created` with a payload like `{"message":"ok","id":"<uuid>"}`.

{% hint style="warning" %}
`coap-client` builds that use OpenSSL 4 (e.g. the current Homebrew package) don't work with akenza. Their ClientHello is too large for one UDP datagram and gets split in two, which akenza drops. The request times out with `cannot send CoAP pdu`. Check the DTLS library of your build with `coap-client -h | grep "TLS Library"`. If it uses OpenSSL 4, test with OpenSSL as described above.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.akenza.io/device-management/your-data-flow/device-connector/coap/dtls-pre-shared-keys.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
