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
13 /**
14 * @file coap_notls.c
15 * @brief NoTLS specific interface functions
16 */
17
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
coap_dtls_is_supported(void)23 coap_dtls_is_supported(void) {
24 return 0;
25 }
26
27 int
coap_tls_is_supported(void)28 coap_tls_is_supported(void) {
29 return 0;
30 }
31
32 /*
33 * return 0 failed
34 * 1 passed
35 */
36 int
coap_dtls_psk_is_supported(void)37 coap_dtls_psk_is_supported(void) {
38 return 0;
39 }
40
41 /*
42 * return 0 failed
43 * 1 passed
44 */
45 int
coap_dtls_pki_is_supported(void)46 coap_dtls_pki_is_supported(void) {
47 return 0;
48 }
49
50 /*
51 * return 0 failed
52 * 1 passed
53 */
54 int
coap_dtls_pkcs11_is_supported(void)55 coap_dtls_pkcs11_is_supported(void) {
56 return 0;
57 }
58
59 /*
60 * return 0 failed
61 * 1 passed
62 */
63 int
coap_dtls_rpk_is_supported(void)64 coap_dtls_rpk_is_supported(void) {
65 return 0;
66 }
67
68 coap_tls_version_t *
coap_get_tls_library_version(void)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
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)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
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)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
coap_dtls_context_set_cpsk(coap_context_t * ctx COAP_UNUSED,coap_dtls_cpsk_t * setup_data COAP_UNUSED)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
coap_dtls_context_set_spsk(coap_context_t * ctx COAP_UNUSED,coap_dtls_spsk_t * setup_data COAP_UNUSED)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
coap_dtls_context_check_keys_enabled(coap_context_t * ctx COAP_UNUSED)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
coap_dtls_startup(void)118 coap_dtls_startup(void) {
119 }
120
121 void *
coap_dtls_get_tls(const coap_session_t * c_session COAP_UNUSED,coap_tls_library_t * tls_lib)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
coap_dtls_shutdown(void)130 coap_dtls_shutdown(void) {
131 coap_dtls_set_log_level(COAP_LOG_EMERG);
132 }
133
134 void
coap_dtls_set_log_level(coap_log_t level)135 coap_dtls_set_log_level(coap_log_t level) {
136 dtls_log_level = level;
137 }
138
139 coap_log_t
coap_dtls_get_log_level(void)140 coap_dtls_get_log_level(void) {
141 return dtls_log_level;
142 }
143
144 void *
coap_dtls_new_context(coap_context_t * coap_context COAP_UNUSED)145 coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED) {
146 return NULL;
147 }
148
149 void
coap_dtls_free_context(void * handle COAP_UNUSED)150 coap_dtls_free_context(void *handle COAP_UNUSED) {
151 }
152
153 #if COAP_SERVER_SUPPORT
154 void *
coap_dtls_new_server_session(coap_session_t * session COAP_UNUSED)155 coap_dtls_new_server_session(coap_session_t *session COAP_UNUSED) {
156 return NULL;
157 }
158 #endif /* COAP_SERVER_SUPPORT */
159
160 #if COAP_CLIENT_SUPPORT
161 void *
coap_dtls_new_client_session(coap_session_t * session COAP_UNUSED)162 coap_dtls_new_client_session(coap_session_t *session COAP_UNUSED) {
163 return NULL;
164 }
165 #endif /* COAP_CLIENT_SUPPORT */
166
167 void
coap_dtls_free_session(coap_session_t * coap_session COAP_UNUSED)168 coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED) {
169 }
170
171 void
coap_dtls_session_update_mtu(coap_session_t * session COAP_UNUSED)172 coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED) {
173 }
174
175 ssize_t
coap_dtls_send(coap_session_t * session COAP_UNUSED,const uint8_t * data COAP_UNUSED,size_t data_len COAP_UNUSED)176 coap_dtls_send(coap_session_t *session COAP_UNUSED,
177 const uint8_t *data COAP_UNUSED,
178 size_t data_len COAP_UNUSED) {
179 return -1;
180 }
181
182 int
coap_dtls_is_context_timeout(void)183 coap_dtls_is_context_timeout(void) {
184 return 1;
185 }
186
187 coap_tick_t
coap_dtls_get_context_timeout(void * dtls_context COAP_UNUSED)188 coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED) {
189 return 0;
190 }
191
192 coap_tick_t
coap_dtls_get_timeout(coap_session_t * session COAP_UNUSED,coap_tick_t now COAP_UNUSED)193 coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED) {
194 return 0;
195 }
196
197 /*
198 * return 1 timed out
199 * 0 still timing out
200 */
201 int
coap_dtls_handle_timeout(coap_session_t * session COAP_UNUSED)202 coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED) {
203 return 0;
204 }
205
206 int
coap_dtls_receive(coap_session_t * session COAP_UNUSED,const uint8_t * data COAP_UNUSED,size_t data_len COAP_UNUSED)207 coap_dtls_receive(coap_session_t *session COAP_UNUSED,
208 const uint8_t *data COAP_UNUSED,
209 size_t data_len COAP_UNUSED
210 ) {
211 return -1;
212 }
213
214 #if COAP_SERVER_SUPPORT
215 int
coap_dtls_hello(coap_session_t * session COAP_UNUSED,const uint8_t * data COAP_UNUSED,size_t data_len COAP_UNUSED)216 coap_dtls_hello(coap_session_t *session COAP_UNUSED,
217 const uint8_t *data COAP_UNUSED,
218 size_t data_len COAP_UNUSED
219 ) {
220 return 0;
221 }
222 #endif /* COAP_SERVER_SUPPORT */
223
224 unsigned int
coap_dtls_get_overhead(coap_session_t * session COAP_UNUSED)225 coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED) {
226 return 0;
227 }
228
229 #if COAP_CLIENT_SUPPORT
230 void *
coap_tls_new_client_session(coap_session_t * session COAP_UNUSED)231 coap_tls_new_client_session(coap_session_t *session COAP_UNUSED) {
232 return NULL;
233 }
234 #endif /* COAP_CLIENT_SUPPORT */
235
236 #if COAP_SERVER_SUPPORT
237 void *
coap_tls_new_server_session(coap_session_t * session COAP_UNUSED)238 coap_tls_new_server_session(coap_session_t *session COAP_UNUSED) {
239 return NULL;
240 }
241 #endif /* COAP_SERVER_SUPPORT */
242
243 void
coap_tls_free_session(coap_session_t * coap_session COAP_UNUSED)244 coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED) {
245 }
246
247 /*
248 * strm
249 * return +ve Number of bytes written.
250 * -1 Error (error in errno).
251 */
252 ssize_t
coap_tls_write(coap_session_t * session COAP_UNUSED,const uint8_t * data COAP_UNUSED,size_t data_len COAP_UNUSED)253 coap_tls_write(coap_session_t *session COAP_UNUSED,
254 const uint8_t *data COAP_UNUSED,
255 size_t data_len COAP_UNUSED) {
256 return -1;
257 }
258
259 /*
260 * strm
261 * return >=0 Number of bytes read.
262 * -1 Error (error in errno).
263 */
264 ssize_t
coap_tls_read(coap_session_t * session COAP_UNUSED,uint8_t * data COAP_UNUSED,size_t data_len COAP_UNUSED)265 coap_tls_read(coap_session_t *session COAP_UNUSED,
266 uint8_t *data COAP_UNUSED,
267 size_t data_len COAP_UNUSED) {
268 return -1;
269 }
270
271 #if COAP_SERVER_SUPPORT
272 typedef struct coap_local_hash_t {
273 size_t ofs;
274 coap_key_t key[8]; /* 32 bytes in total */
275 } coap_local_hash_t;
276
277 coap_digest_ctx_t *
coap_digest_setup(void)278 coap_digest_setup(void) {
279 coap_key_t *digest_ctx = coap_malloc_type(COAP_DIGEST_CTX, sizeof(coap_local_hash_t));
280
281 if (digest_ctx) {
282 memset(digest_ctx, 0, sizeof(coap_local_hash_t));
283 }
284
285 return digest_ctx;
286 }
287
288 void
coap_digest_free(coap_digest_ctx_t * digest_ctx)289 coap_digest_free(coap_digest_ctx_t *digest_ctx) {
290 coap_free_type(COAP_DIGEST_CTX, digest_ctx);
291 }
292
293 int
coap_digest_update(coap_digest_ctx_t * digest_ctx,const uint8_t * data,size_t data_len)294 coap_digest_update(coap_digest_ctx_t *digest_ctx,
295 const uint8_t *data,
296 size_t data_len) {
297 coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx;
298
299 coap_hash(data, data_len, local->key[local->ofs]);
300
301 local->ofs = (local->ofs + 1) % 7;
302 return 1;
303 }
304
305 int
coap_digest_final(coap_digest_ctx_t * digest_ctx,coap_digest_t * digest_buffer)306 coap_digest_final(coap_digest_ctx_t *digest_ctx,
307 coap_digest_t *digest_buffer) {
308 coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx;
309
310 memcpy(digest_buffer, local->key, sizeof(coap_digest_t));
311
312 coap_digest_free(digest_ctx);
313 return 1;
314 }
315 #endif /* COAP_SERVER_SUPPORT */
316
317 #if COAP_WS_SUPPORT
318 int
coap_crypto_hash(cose_alg_t alg,const coap_bin_const_t * data,coap_bin_const_t ** hash)319 coap_crypto_hash(cose_alg_t alg,
320 const coap_bin_const_t *data,
321 coap_bin_const_t **hash) {
322 (void)alg;
323 (void)data;
324 (void)hash;
325 return 0;
326 }
327 #endif /* COAP_WS_SUPPORT */
328
329 #if COAP_OSCORE_SUPPORT
330
331 int
coap_oscore_is_supported(void)332 coap_oscore_is_supported(void) {
333 return 0;
334 }
335
336 int
coap_crypto_check_cipher_alg(cose_alg_t alg)337 coap_crypto_check_cipher_alg(cose_alg_t alg) {
338 (void)alg;
339 return 0;
340 }
341
342 int
coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg)343 coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg) {
344 (void)hkdf_alg;
345 return 0;
346 }
347
348 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)349 coap_crypto_aead_encrypt(const coap_crypto_param_t *params,
350 coap_bin_const_t *data,
351 coap_bin_const_t *aad,
352 uint8_t *result,
353 size_t *max_result_len) {
354 (void)params;
355 (void)data;
356 (void)aad;
357 (void)result;
358 *max_result_len = 0;
359 return 0;
360 }
361
362 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)363 coap_crypto_aead_decrypt(const coap_crypto_param_t *params,
364 coap_bin_const_t *data,
365 coap_bin_const_t *aad,
366 uint8_t *result,
367 size_t *max_result_len) {
368 (void)params;
369 (void)data;
370 (void)aad;
371 (void)result;
372 *max_result_len = 0;
373 return 0;
374 }
375
376 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)377 coap_crypto_hmac(cose_hmac_alg_t hmac_alg,
378 coap_bin_const_t *key,
379 coap_bin_const_t *data,
380 coap_bin_const_t **hmac) {
381 (void)hmac_alg;
382 (void)key;
383 (void)data;
384 (void)hmac;
385 return 0;
386 }
387
388 #endif /* COAP_OSCORE_SUPPORT */
389
390 #else /* !COAP_WITH_LIBTINYDTLS && !COAP_WITH_LIBOPENSSL && !COAP_WITH_LIBGNUTLS */
391
392 #ifdef __clang__
393 /* Make compilers happy that do not like empty modules. As this function is
394 * never used, we ignore -Wunused-function at the end of compiling this file
395 */
396 #pragma GCC diagnostic ignored "-Wunused-function"
397 #endif
398 static inline void
dummy(void)399 dummy(void) {
400 }
401
402 #endif /* !COAP_WITH_LIBTINYDTLS && !COAP_WITH_LIBOPENSSL && !COAP_WITH_LIBGNUTLS && !COAP_WITH_LIBMBEDTLS */
403