Contiki-NG
Loading...
Searching...
No Matches
edhoc-server.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, RISE Research Institutes of Sweden AB
3 * Copyright (c) 2020, Industrial Systems Institute (ISI), Patras, Greece
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32/**
33 * \file
34 * EDHOC server API [RFC9528] with CoAP Block-Wise Transfer [RFC7959]
35 * \author
36 * Lidia Pocero <pocero@isi.gr>, Peter A Jonsson, Rikard Höglund, Marco Tiloca
37 * Christos Koulamas <cklm@isi.gr>
38 */
39
40/**
41 * \addtogroup edhoc
42 * @{
43 */
44
45#ifndef EDHOC_SERVER_H_
46#define EDHOC_SERVER_H_
47
48#include "coap-engine.h"
49#include "edhoc.h"
50#include "coap-timer.h"
51#include "coap-block1.h"
52#include "edhoc-exporter.h"
53
54/**
55 * \brief Time limit value for EDHOC protocol completion
56 */
57#ifdef EDHOC_CONF_TIMEOUT
58#define SERV_TIMEOUT_VAL EDHOC_CONF_TIMEOUT
59#else
60#define SERV_TIMEOUT_VAL 10000
61#endif
62
63/* EDHOC process states */
64#define SERV_HANDSHAKE_COMPLETE 1
65#define SERV_HANDSHAKE_RESET 2
66
67/**
68 * \brief CoAP resource
69 */
70extern coap_resource_t res_edhoc;
71
72/**
73 * \brief EDHOC Server Struct
74 */
75typedef struct edhoc_server {
76 uint16_t con_num;
77 uint8_t state;
78 bool rx_msg1;
79 bool rx_msg3;
80 uip_ipaddr_t con_ipaddr;
82
83/**
84 * \brief EDHOC server Application data struct
85 */
86typedef struct edhoc_server_ad {
87 char ad_1[EDHOC_MAX_AD_SZ];
88 uint8_t ad_1_sz;
89 char ad_2[EDHOC_MAX_AD_SZ];
90 uint8_t ad_2_sz;
91 char ad_3[EDHOC_MAX_AD_SZ];
92 uint8_t ad_3_sz;
94
95/**
96 * \brief EDHOC server data event struct
97 */
98typedef struct ecc_data_event {
99 uint8_t val;
102
103/**
104 * \brief Activate the EDHOC CoAP Resource
105 *
106 * Activate the EDHOC well-known CoAP Resource at the Uri-Path defined
107 * in the WELL_KNOW macro.
108 */
109void edhoc_server_init(void);
110
111/**
112 * \brief Create a new EDHOC context for a new EDHOC protocol session
113 * \retval Non-zero if the authentication credentials for the EDHOC server exist in the key-storage
114 * and the EDHOC server starts correctly.
115 *
116 * This function retrieves the DH-static authentication key pair of the Server from the edhoc-key-storage.
117 * The authentication keys must be established in the EDHOC key storage before running the EDHOC protocol.
118 * Creates a new EDHOC context and generates the DH-ephemeral key for the specific session.
119 * A new EDHOC protocol session must be created for each new EDHOC client connection attempt.
120 */
121uint8_t edhoc_server_start(void);
122
123/**
124 * \brief Reset the EDHOC handshake state for a new client connection
125 * \retval Non-zero if the authentication credentials for the EDHOC server exist in the key-storage
126 * and the EDHOC server resets correctly.
127 *
128 * Resets the EDHOC handshake state to prepare for a new client handshake.
129 * This clears the current session state while keeping the server running.
130 * Must be called after completing or aborting a handshake before accepting the next client.
131 */
132uint8_t edhoc_server_reset_handshake(void);
133
134/**
135 * \brief Check if an EDHOC server session has finished
136 * \param ev Process event
137 * \param data Process data
138 * \retval SERV_HANDSHAKE_COMPLETE (non-zero) if the session finished successfully with an EDHOC client
139 * \retval SERV_HANDSHAKE_RESET (non-zero) if the handshake was reset
140 * \retval 0 if the event is not from the EDHOC server or the handshake has not finished yet
141 *
142 * This function checks the events triggered from the EDHOC server protocol, reporting both
143 * handshake completion (SERV_HANDSHAKE_COMPLETE) and handshake reset (SERV_HANDSHAKE_RESET).
144 * A non-zero return does not by itself imply success, so callers must distinguish the two values.
145 */
146int8_t edhoc_server_callback(process_event_t ev, void *data);
147
148/**
149 * \brief Close the EDHOC context
150 *
151 * This function must be called after the Security Context is exported to free the
152 * allocated memory.
153 */
154void edhoc_server_close(void);
155
156/**
157 * \brief Run the EDHOC Responder role process
158 * \param req The request CoAP message received
159 * \param res The response CoAP message to send back
160 * \param ser The EDHOC server struct
161 * \param msg A pointer to the buffer with the received message
162 * \param len The received message length
163 *
164 * This function must be called from a CoAP POST handler to run the EDHOC protocol Responder
165 * role. EDHOC messages 1 and 3 are transferred in POST requests and EDHOC message 2
166 * is transferred in 2.04 (Changed) responses.
167 */
168void edhoc_server_process(coap_message_t *req, coap_message_t *res, edhoc_server_t *ser, uint8_t *msg, size_t len);
169
170/**
171 * \brief Set the Application Data to be carried in EDHOC message 2
172 * \param buf A pointer to a buffer that contains the Application data to be copied
173 * \param buf_sz The Application data length
174 *
175 * This function sets the Application data to be carried in EDHOC message 2.
176 */
177void edhoc_server_set_ad_2(const void *buf, uint8_t buf_sz);
178
179/**
180 * \brief Get the Application Data received in EDHOC message 1
181 * \param buf A pointer to a buffer to copy the Application data
182 * \return ad_sz The Application data length
183 *
184 * This function copies the Application data from the received EDHOC message 1 to the buffer.
185 */
186uint8_t edhoc_server_get_ad_1(char *buf);
187
188/**
189 * \brief Get the Application Data received in EDHOC message 3
190 * \param buf A pointer to a buffer to copy the Application data
191 * \return ad_sz The Application data length
192 *
193 * This function copies the Application data from the received EDHOC message 3 to the buffer.
194 */
195uint8_t edhoc_server_get_ad_3(char *buf);
196
197#endif /* EDHOC_SERVER_H_ */
198/** @} */
CoAP module for block 1 handling.
CoAP engine implementation.
CoAP timer API.
EDHOC Exporter API - Interface to derive application security contexts from the EDHOC shared secret.
An implementation of Ephemeral Diffie-Hellman Over COSE (EDHOC) (RFC9528).
void edhoc_server_process(coap_message_t *req, coap_message_t *res, edhoc_server_t *ser, uint8_t *msg, size_t len)
Run the EDHOC Responder role process.
int8_t edhoc_server_callback(process_event_t ev, void *data)
Check if an EDHOC server session has finished.
void edhoc_server_close(void)
Close the EDHOC context.
void edhoc_server_set_ad_2(const void *buf, uint8_t buf_sz)
Set the Application Data to be carried in EDHOC message 2.
#define EDHOC_MAX_AD_SZ
The max length of the Application Data.
struct ecc_data_event ecc_data_event_t
EDHOC server data event struct.
struct edhoc_server edhoc_server_t
EDHOC Server Struct.
uint8_t edhoc_server_get_ad_3(char *buf)
Get the Application Data received in EDHOC message 3.
void edhoc_server_init(void)
Activate the EDHOC CoAP Resource.
coap_resource_t res_edhoc
CoAP resource.
uint8_t edhoc_server_reset_handshake(void)
Reset the EDHOC handshake state for a new client connection.
uint8_t edhoc_server_start(void)
Create a new EDHOC context for a new EDHOC protocol session.
struct edhoc_server_ad edhoc_server_ad_t
EDHOC server Application data struct.
uint8_t edhoc_server_get_ad_1(char *buf)
Get the Application Data received in EDHOC message 1.
EDHOC server data event struct.
EDHOC server Application data struct.
EDHOC Server Struct.