libcoap 4.3.2rc2

coap_notls.c
Go to the documentation of this file.
1/*
2 * coap_notls.c -- Stub Datagram Transport Layer Support for libcoap
3 *
4 * Copyright (C) 2016 Olaf Bergmann <bergmann@tzi.org>
5 * Copyright (C) 2021-2023 Jon Shallow <supjps-libcoap@jpshallow.com>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 *
9 * This file is part of the CoAP library libcoap. Please see README for terms
10 * of use.
11 */
12
18#include "coap3/coap_internal.h"
19
20#if !defined(COAP_WITH_LIBTINYDTLS) && !defined(COAP_WITH_LIBOPENSSL) && !defined(COAP_WITH_LIBGNUTLS) && !defined(COAP_WITH_LIBMBEDTLS)
21
22 int
23 coap_dtls_is_supported(void) {
24 return 0;
25}
26
27 int
28 coap_tls_is_supported(void) {
29 return 0;
30}
31
32/*
33 * return 0 failed
34 * 1 passed
35 */
36 int
37 coap_dtls_psk_is_supported(void) {
38 return 0;
39}
40
41/*
42 * return 0 failed
43 * 1 passed
44 */
45 int
46 coap_dtls_pki_is_supported(void) {
47 return 0;
48}
49
50/*
51 * return 0 failed
52 * 1 passed
53 */
54 int
55 coap_dtls_pkcs11_is_supported(void) {
56 return 0;
57}
58
59/*
60 * return 0 failed
61 * 1 passed
62 */
63 int
64 coap_dtls_rpk_is_supported(void) {
65 return 0;
66}
67
68 coap_tls_version_t *
69 coap_get_tls_library_version(void) {
70 static coap_tls_version_t version;
71 version.version = 0;
72 version.type = COAP_TLS_LIBRARY_NOTLS;
73 return &version;
74}
75
76 int
77 coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED,
78 const coap_dtls_pki_t *setup_data COAP_UNUSED,
79 const coap_dtls_role_t role COAP_UNUSED
80 ) {
81 return 0;
82}
83
84 int
85 coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED,
86 const char *ca_file COAP_UNUSED,
87 const char *ca_path COAP_UNUSED
88 ) {
89 return 0;
90}
91
92#if COAP_CLIENT_SUPPORT
93 int
94 coap_dtls_context_set_cpsk(coap_context_t *ctx COAP_UNUSED,
95 coap_dtls_cpsk_t *setup_data COAP_UNUSED
96 ) {
97 return 0;
98}
99#endif /* COAP_CLIENT_SUPPORT */
100
101#if COAP_SERVER_SUPPORT
102 int
103 coap_dtls_context_set_spsk(coap_context_t *ctx COAP_UNUSED,
104 coap_dtls_spsk_t *setup_data COAP_UNUSED
105 ) {
106 return 0;
107}
108#endif /* COAP_SERVER_SUPPORT */
109
110 int
111 coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED) {
112 return 0;
113}
114
115 static coap_log_t dtls_log_level = COAP_LOG_EMERG;
116
117 void
118 coap_dtls_startup(void) {
119}
120
121 void *
122 coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED,
123 coap_tls_library_t *tls_lib) {
124 if (tls_lib)
125 *tls_lib = COAP_TLS_LIBRARY_NOTLS;
126 return NULL;
127}
128
129 void
130 coap_dtls_shutdown(void) {
131}
132
133 void
134 coap_dtls_set_log_level(coap_log_t level) {
135 dtls_log_level = level;
136}
137
138 coap_log_t
139 coap_dtls_get_log_level(void) {
140 return dtls_log_level;
141}
142
143 void *
144 coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED) {
145 return NULL;
146}
147
148 void
149 coap_dtls_free_context(void *handle COAP_UNUSED) {
150}
151
152#if COAP_SERVER_SUPPORT
153 void *
154 coap_dtls_new_server_session(coap_session_t *session COAP_UNUSED) {
155 return NULL;
156}
157#endif /* COAP_SERVER_SUPPORT */
158
159#if COAP_CLIENT_SUPPORT
160 void *
161 coap_dtls_new_client_session(coap_session_t *session COAP_UNUSED) {
162 return NULL;
163}
164#endif /* COAP_CLIENT_SUPPORT */
165
166 void
167 coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED) {
168}
169
170 void
171 coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED) {
172}
173
174ssize_t
175 coap_dtls_send(coap_session_t *session COAP_UNUSED,
176 const uint8_t *data COAP_UNUSED,
177 size_t data_len COAP_UNUSED) {
178 return -1;
179}
180
181 int
182 coap_dtls_is_context_timeout(void) {
183 return 1;
184}
185
186 coap_tick_t
187 coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED) {
188 return 0;
189}
190
191 coap_tick_t
192 coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED) {
193 return 0;
194}
195
196/*
197 * return 1 timed out
198 * 0 still timing out
199 */
200 int
201 coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED) {
202 return 0;
203}
204
205 int
206 coap_dtls_receive(coap_session_t *session COAP_UNUSED,
207 const uint8_t *data COAP_UNUSED,
208 size_t data_len COAP_UNUSED
209 ) {
210 return -1;
211}
212
213#if COAP_SERVER_SUPPORT
214 int
215 coap_dtls_hello(coap_session_t *session COAP_UNUSED,
216 const uint8_t *data COAP_UNUSED,
217 size_t data_len COAP_UNUSED
218 ) {
219 return 0;
220}
221#endif /* COAP_SERVER_SUPPORT */
222
223 unsigned int
224 coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED) {
225 return 0;
226}
227
228#if COAP_CLIENT_SUPPORT
229 void *
230 coap_tls_new_client_session(coap_session_t *session COAP_UNUSED) {
231 return NULL;
232}
233#endif /* COAP_CLIENT_SUPPORT */
234
235#if COAP_SERVER_SUPPORT
236 void *
237 coap_tls_new_server_session(coap_session_t *session COAP_UNUSED) {
238 return NULL;
239}
240#endif /* COAP_SERVER_SUPPORT */
241
242 void
243 coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED) {
244}
245
246/*
247 * strm
248 * return +ve Number of bytes written.
249 * -1 Error (error in errno).
250 */
251ssize_t
252 coap_tls_write(coap_session_t *session COAP_UNUSED,
253 const uint8_t *data COAP_UNUSED,
254 size_t data_len COAP_UNUSED) {
255 return -1;
256}
257
258/*
259 * strm
260 * return >=0 Number of bytes read.
261 * -1 Error (error in errno).
262 */
263ssize_t
264 coap_tls_read(coap_session_t *session COAP_UNUSED,
265 uint8_t *data COAP_UNUSED,
266 size_t data_len COAP_UNUSED) {
267 return -1;
268}
269
270#if COAP_SERVER_SUPPORT
271 typedef struct coap_local_hash_t {
272 size_t ofs;
273 coap_key_t key[8]; /* 32 bytes in total */
274} coap_local_hash_t;
275
276 coap_digest_ctx_t *
277 coap_digest_setup(void) {
278 coap_key_t *digest_ctx = coap_malloc_type(COAP_DIGEST_CTX, sizeof(coap_local_hash_t));
279
280 if (digest_ctx) {
281 memset(digest_ctx, 0, sizeof(coap_local_hash_t));
282 }
283
284 return digest_ctx;
285}
286
287 void
288 coap_digest_free(coap_digest_ctx_t *digest_ctx) {
289 coap_free_type(COAP_DIGEST_CTX, digest_ctx);
290}
291
292 int
293 coap_digest_update(coap_digest_ctx_t *digest_ctx,
294 const uint8_t *data,
295 size_t data_len) {
296 coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx;
297
298 coap_hash(data, data_len, local->key[local->ofs]);
299
300 local->ofs = (local->ofs + 1) % 7;
301 return 1;
302}
303
304 int
305 coap_digest_final(coap_digest_ctx_t *digest_ctx,
306 coap_digest_t *digest_buffer) {
307 coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx;
308
309 memcpy(digest_buffer, local->key, sizeof(coap_digest_t));
310
311 coap_digest_free(digest_ctx);
312 return 1;
313}
314#endif /* COAP_SERVER_SUPPORT */
315
316#if COAP_WS_SUPPORT
317 int
318 coap_crypto_hash(cose_alg_t alg,
319 const coap_bin_const_t *data,
320 coap_bin_const_t **hash) {
321 (void)alg;
322 (void)data;
323 (void)hash;
324 return 0;
325}
326#endif /* COAP_WS_SUPPORT */
327
328#if COAP_OSCORE_SUPPORT
329
330 int
331 coap_oscore_is_supported(void) {
332 return 0;
333}
334
335 int
336 coap_crypto_check_cipher_alg(cose_alg_t alg) {
337 (void)alg;
338 return 0;
339}
340
341 int
342 coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg) {
343 (void)hkdf_alg;
344 return 0;
345}
346
347 int
348 coap_crypto_aead_encrypt(const coap_crypto_param_t *params,
349 coap_bin_const_t *data,
350 coap_bin_const_t *aad,
351 uint8_t *result,
352 size_t *max_result_len) {
353 (void)params;
354 (void)data;
355 (void)aad;
356 (void)result;
357 *max_result_len = 0;
358 return 0;
359}
360
361 int
362 coap_crypto_aead_decrypt(const coap_crypto_param_t *params,
363 coap_bin_const_t *data,
364 coap_bin_const_t *aad,
365 uint8_t *result,
366 size_t *max_result_len) {
367 (void)params;
368 (void)data;
369 (void)aad;
370 (void)result;
371 *max_result_len = 0;
372 return 0;
373}
374
375 int
376 coap_crypto_hmac(cose_hmac_alg_t hmac_alg,
377 coap_bin_const_t *key,
378 coap_bin_const_t *data,
379 coap_bin_const_t **hmac) {
380 (void)hmac_alg;
381 (void)key;
382 (void)data;
383 (void)hmac;
384 return 0;
385}
386
387#endif /* COAP_OSCORE_SUPPORT */
388
389#else /* !COAP_WITH_LIBTINYDTLS && !COAP_WITH_LIBOPENSSL && !COAP_WITH_LIBGNUTLS */
390
391#ifdef __clang__
392/* Make compilers happy that do not like empty modules. As this function is
393 * never used, we ignore -Wunused-function at the end of compiling this file
394 */
395#pragma GCC diagnostic ignored "-Wunused-function"
396#endif
397 static inline void
398 dummy(void) {
399}
400
401#endif /* !COAP_WITH_LIBTINYDTLS && !COAP_WITH_LIBOPENSSL && !COAP_WITH_LIBGNUTLS && !COAP_WITH_LIBMBEDTLS */
coap_key_t
unsigned char coap_key_t[4]
Definition: coap_hashkey_internal.h:24
coap_hash
#define coap_hash(String, Length, Result)
Definition: coap_hashkey_internal.h:38
coap_internal.h
Pulls together all the internal only header files.
COAP_DIGEST_CTX
@ COAP_DIGEST_CTX
Definition: coap_mem.h:57
coap_malloc_type
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
coap_free_type
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_dtls_context_set_pki
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
Definition: coap_notls.c:77
coap_dtls_get_timeout
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
Definition: coap_notls.c:192
coap_tls_read
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition: coap_notls.c:264
coap_dtls_get_context_timeout
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
Definition: coap_notls.c:187
coap_dtls_receive
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition: coap_notls.c:206
coap_dtls_get_tls
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition: coap_notls.c:122
coap_dtls_get_overhead
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition: coap_notls.c:224
dtls_log_level
static coap_log_t dtls_log_level
Definition: coap_notls.c:115
coap_dtls_context_check_keys_enabled
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition: coap_notls.c:111
coap_dtls_send
ssize_t coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition: coap_notls.c:175
coap_tls_write
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition: coap_notls.c:252
coap_dtls_session_update_mtu
void coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED)
Definition: coap_notls.c:171
coap_dtls_context_set_pki_root_cas
int coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, const char *ca_file COAP_UNUSED, const char *ca_path COAP_UNUSED)
Definition: coap_notls.c:85
coap_dtls_handle_timeout
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
Definition: coap_notls.c:201
coap_dtls_free_context
void coap_dtls_free_context(void *handle COAP_UNUSED)
Definition: coap_notls.c:149
coap_dtls_free_session
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition: coap_notls.c:167
coap_dtls_new_context
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
Definition: coap_notls.c:144
coap_tls_free_session
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition: coap_notls.c:243
dummy
COAP_STATIC_INLINE void dummy(void)
Definition: coap_time.c:139
coap_digest_free
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
coap_digest_final
int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer)
Finalize the coap_digest information into the provided digest_buffer.
coap_digest_update
int coap_digest_update(coap_digest_ctx_t *digest_ctx, const uint8_t *data, size_t data_len)
Update the coap_digest information with the next chunk of data.
coap_digest_ctx_t
void coap_digest_ctx_t
Definition: coap_cache_internal.h:61
coap_digest_setup
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:144
coap_crypto_hmac
int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, coap_bin_const_t *data, coap_bin_const_t **hmac)
Create a HMAC hash of the provided data.
coap_crypto_aead_decrypt
int coap_crypto_aead_decrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Decrypt the provided encrypted data into plaintext.
coap_crypto_aead_encrypt
int coap_crypto_aead_encrypt(const coap_crypto_param_t *params, coap_bin_const_t *data, coap_bin_const_t *aad, uint8_t *result, size_t *max_result_len)
Encrypt the provided plaintext data.
coap_crypto_hash
int coap_crypto_hash(cose_alg_t alg, const coap_bin_const_t *data, coap_bin_const_t **hash)
Create a hash of the provided data.
coap_crypto_check_hkdf_alg
int coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg)
Check whether the defined hkdf algorithm is supported by the underlying crypto library.
coap_crypto_check_cipher_alg
int coap_crypto_check_cipher_alg(cose_alg_t alg)
Check whether the defined cipher algorithm is supported by the underlying crypto library.
coap_tls_new_server_session
void * coap_tls_new_server_session(coap_session_t *coap_session)
Create a TLS new server-side session.
coap_dtls_startup
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
Definition: coap_notls.c:118
coap_dtls_new_client_session
void * coap_dtls_new_client_session(coap_session_t *coap_session)
Create a new client-side session.
coap_dtls_new_server_session
void * coap_dtls_new_server_session(coap_session_t *coap_session)
Create a new DTLS server-side session.
coap_dtls_hello
int coap_dtls_hello(coap_session_t *coap_session, const uint8_t *data, size_t data_len)
Handling client HELLO messages from a new candiate peer.
coap_dtls_is_context_timeout
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
Definition: coap_notls.c:182
coap_dtls_context_set_cpsk
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
coap_dtls_context_set_spsk
int coap_dtls_context_set_spsk(coap_context_t *coap_context, coap_dtls_spsk_t *setup_data)
Set the DTLS context's default server PSK information.
coap_dtls_shutdown
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
Definition: coap_notls.c:130
coap_tls_new_client_session
void * coap_tls_new_client_session(coap_session_t *coap_session)
Create a new TLS client-side session.
coap_get_tls_library_version
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
Definition: coap_notls.c:69
coap_dtls_psk_is_supported
int coap_dtls_psk_is_supported(void)
Check whether (D)TLS PSK is available.
Definition: coap_notls.c:37
coap_dtls_role_t
coap_dtls_role_t
Definition: coap_dtls.h:43
coap_tls_is_supported
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition: coap_notls.c:28
coap_dtls_is_supported
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition: coap_notls.c:23
coap_dtls_pki_is_supported
int coap_dtls_pki_is_supported(void)
Check whether (D)TLS PKI is available.
Definition: coap_notls.c:46
coap_dtls_rpk_is_supported
int coap_dtls_rpk_is_supported(void)
Check whether (D)TLS RPK is available.
Definition: coap_notls.c:64
coap_tls_library_t
coap_tls_library_t
Definition: coap_dtls.h:92
coap_dtls_pkcs11_is_supported
int coap_dtls_pkcs11_is_supported(void)
Check whether (D)TLS PKCS11 is available.
Definition: coap_notls.c:55
COAP_TLS_LIBRARY_NOTLS
@ COAP_TLS_LIBRARY_NOTLS
No DTLS library.
Definition: coap_dtls.h:93
coap_log_t
coap_log_t
Logging type.
Definition: coap_debug.h:50
coap_dtls_get_log_level
coap_log_t coap_dtls_get_log_level(void)
Get the current (D)TLS logging.
Definition: coap_notls.c:139
coap_dtls_set_log_level
void coap_dtls_set_log_level(coap_log_t level)
Sets the (D)TLS logging level to the specified level.
Definition: coap_notls.c:134
COAP_LOG_EMERG
@ COAP_LOG_EMERG
Definition: coap_debug.h:51
cose_hkdf_alg_t
cose_hkdf_alg_t
Definition: oscore_cose.h:165
cose_hmac_alg_t
cose_hmac_alg_t
Definition: oscore_cose.h:157
cose_alg_t
cose_alg_t
Definition: oscore_cose.h:126
coap_oscore_is_supported
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
Definition: coap_oscore.c:2104
COAP_UNUSED
#define COAP_UNUSED
Definition: libcoap.h:68
coap_bin_const_t
CoAP binary data definition with const data.
Definition: coap_str.h:64
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition: coap_net_internal.h:49
coap_crypto_param_t
The common structure that holds the Crypto information.
Definition: coap_crypto_internal.h:59
coap_digest_t
Definition: coap_cache_internal.h:33
coap_dtls_cpsk_t
The structure used for defining the Client PSK setup data to be used.
Definition: coap_dtls.h:378
coap_dtls_pki_t
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:284
coap_dtls_spsk_t
The structure used for defining the Server PSK setup data to be used.
Definition: coap_dtls.h:465
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition: coap_session_internal.h:68
coap_tls_version_t
The structure used for returning the underlying (D)TLS library information.
Definition: coap_dtls.h:104
coap_tls_version_t::type
coap_tls_library_t type
Library type.
Definition: coap_dtls.h:106
coap_tls_version_t::version
uint64_t version
(D)TLS runtime Library Version
Definition: coap_dtls.h:105