forked from obgm/libcoap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoap_notls.c
446 lines (377 loc) · 9.17 KB
/
coap_notls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* coap_notls.c -- Stub Datagram Transport Layer Support for libcoap
*
* Copyright (C) 2016 Olaf Bergmann <[email protected]>
* Copyright (C) 2021-2024 Jon Shallow <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_notls.c
* @brief NoTLS specific interface functions
*/
#include "coap3/coap_libcoap_build.h"
#if !defined(COAP_WITH_LIBOPENSSL)
int
coap_tls_engine_configure(coap_str_const_t *conf_mem) {
(void)conf_mem;
return 0;
}
int
coap_tls_engine_remove(void) {
return 0;
}
#endif /* ! COAP_WITH_LIBOPENSSL */
#if !defined(COAP_WITH_LIBTINYDTLS) && !defined(COAP_WITH_LIBOPENSSL) && !defined(COAP_WITH_LIBWOLFSSL) && !defined(COAP_WITH_LIBGNUTLS) && !defined(COAP_WITH_LIBMBEDTLS)
int
coap_dtls_is_supported(void) {
return 0;
}
int
coap_tls_is_supported(void) {
return 0;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_psk_is_supported(void) {
return 0;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_pki_is_supported(void) {
return 0;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_pkcs11_is_supported(void) {
return 0;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_rpk_is_supported(void) {
return 0;
}
/*
* return 0 failed
* 1 passed
*/
int
coap_dtls_cid_is_supported(void) {
return 0;
}
#if COAP_CLIENT_SUPPORT
int
coap_dtls_set_cid_tuple_change(coap_context_t *c_context, uint8_t every) {
(void)c_context;
(void)every;
return 0;
}
#endif /* COAP_CLIENT_SUPPORT */
coap_tls_version_t *
coap_get_tls_library_version(void) {
static coap_tls_version_t version;
version.version = 0;
version.type = COAP_TLS_LIBRARY_NOTLS;
return &version;
}
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
) {
return 0;
}
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
) {
return 0;
}
#if COAP_CLIENT_SUPPORT
int
coap_dtls_context_set_cpsk(coap_context_t *ctx COAP_UNUSED,
coap_dtls_cpsk_t *setup_data COAP_UNUSED
) {
return 0;
}
#endif /* COAP_CLIENT_SUPPORT */
#if COAP_SERVER_SUPPORT
int
coap_dtls_context_set_spsk(coap_context_t *ctx COAP_UNUSED,
coap_dtls_spsk_t *setup_data COAP_UNUSED
) {
return 0;
}
#endif /* COAP_SERVER_SUPPORT */
int
coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED) {
return 0;
}
static coap_log_t dtls_log_level = COAP_LOG_EMERG;
void
coap_dtls_startup(void) {
}
void *
coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED,
coap_tls_library_t *tls_lib) {
if (tls_lib)
*tls_lib = COAP_TLS_LIBRARY_NOTLS;
return NULL;
}
void
coap_dtls_shutdown(void) {
coap_dtls_set_log_level(COAP_LOG_EMERG);
}
void
coap_dtls_set_log_level(coap_log_t level) {
dtls_log_level = level;
}
coap_log_t
coap_dtls_get_log_level(void) {
return dtls_log_level;
}
void *
coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED) {
return NULL;
}
void
coap_dtls_free_context(void *handle COAP_UNUSED) {
}
#if COAP_SERVER_SUPPORT
void *
coap_dtls_new_server_session(coap_session_t *session COAP_UNUSED) {
return NULL;
}
#endif /* COAP_SERVER_SUPPORT */
#if COAP_CLIENT_SUPPORT
void *
coap_dtls_new_client_session(coap_session_t *session COAP_UNUSED) {
return NULL;
}
#endif /* COAP_CLIENT_SUPPORT */
void
coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED) {
}
void
coap_dtls_session_update_mtu(coap_session_t *session COAP_UNUSED) {
}
ssize_t
coap_dtls_send(coap_session_t *session COAP_UNUSED,
const uint8_t *data COAP_UNUSED,
size_t data_len COAP_UNUSED) {
return -1;
}
int
coap_dtls_is_context_timeout(void) {
return 1;
}
coap_tick_t
coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED) {
return 0;
}
coap_tick_t
coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED) {
return 0;
}
/*
* return 1 timed out
* 0 still timing out
*/
int
coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED) {
return 0;
}
int
coap_dtls_receive(coap_session_t *session COAP_UNUSED,
const uint8_t *data COAP_UNUSED,
size_t data_len COAP_UNUSED
) {
return -1;
}
#if COAP_SERVER_SUPPORT
int
coap_dtls_hello(coap_session_t *session COAP_UNUSED,
const uint8_t *data COAP_UNUSED,
size_t data_len COAP_UNUSED
) {
return 0;
}
#endif /* COAP_SERVER_SUPPORT */
unsigned int
coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED) {
return 0;
}
#if COAP_CLIENT_SUPPORT
void *
coap_tls_new_client_session(coap_session_t *session COAP_UNUSED) {
return NULL;
}
#endif /* COAP_CLIENT_SUPPORT */
#if COAP_SERVER_SUPPORT
void *
coap_tls_new_server_session(coap_session_t *session COAP_UNUSED) {
return NULL;
}
#endif /* COAP_SERVER_SUPPORT */
void
coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED) {
}
/*
* strm
* return +ve Number of bytes written.
* -1 Error (error in errno).
*/
ssize_t
coap_tls_write(coap_session_t *session COAP_UNUSED,
const uint8_t *data COAP_UNUSED,
size_t data_len COAP_UNUSED) {
return -1;
}
/*
* strm
* return >=0 Number of bytes read.
* -1 Error (error in errno).
*/
ssize_t
coap_tls_read(coap_session_t *session COAP_UNUSED,
uint8_t *data COAP_UNUSED,
size_t data_len COAP_UNUSED) {
return -1;
}
#if COAP_SERVER_SUPPORT
typedef struct coap_local_hash_t {
size_t ofs;
coap_key_t key[8]; /* 32 bytes in total */
} coap_local_hash_t;
coap_digest_ctx_t *
coap_digest_setup(void) {
coap_key_t *digest_ctx = coap_malloc_type(COAP_DIGEST_CTX, sizeof(coap_local_hash_t));
if (digest_ctx) {
memset(digest_ctx, 0, sizeof(coap_local_hash_t));
}
return digest_ctx;
}
void
coap_digest_free(coap_digest_ctx_t *digest_ctx) {
coap_free_type(COAP_DIGEST_CTX, digest_ctx);
}
int
coap_digest_update(coap_digest_ctx_t *digest_ctx,
const uint8_t *data,
size_t data_len) {
coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx;
coap_hash(data, data_len, local->key[local->ofs]);
local->ofs = (local->ofs + 1) % 7;
return 1;
}
int
coap_digest_final(coap_digest_ctx_t *digest_ctx,
coap_digest_t *digest_buffer) {
coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx;
memcpy(digest_buffer, local->key, sizeof(coap_digest_t));
coap_digest_free(digest_ctx);
return 1;
}
#endif /* COAP_SERVER_SUPPORT */
#if COAP_WS_SUPPORT
int
coap_crypto_hash(cose_alg_t alg,
const coap_bin_const_t *data,
coap_bin_const_t **hash) {
SHA1Context sha1_context;
coap_binary_t *dummy = NULL;
(void)alg;
SHA1Reset(&sha1_context);
if (SHA1Input(&sha1_context, data->s, data->length) != shaSuccess)
return 0;
dummy = coap_new_binary(SHA1HashSize);
if (!dummy)
return 0;
if (SHA1Result(&sha1_context, dummy->s) != shaSuccess) {
coap_delete_binary(dummy);
return 0;
}
*hash = (coap_bin_const_t *)(dummy);
return 1;
}
#endif /* COAP_WS_SUPPORT */
#if COAP_OSCORE_SUPPORT
int
coap_oscore_is_supported(void) {
return 0;
}
int
coap_crypto_check_cipher_alg(cose_alg_t alg) {
(void)alg;
return 0;
}
int
coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg) {
(void)hkdf_alg;
return 0;
}
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) {
(void)params;
(void)data;
(void)aad;
(void)result;
*max_result_len = 0;
return 0;
}
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) {
(void)params;
(void)data;
(void)aad;
(void)result;
*max_result_len = 0;
return 0;
}
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) {
(void)hmac_alg;
(void)key;
(void)data;
(void)hmac;
return 0;
}
#endif /* COAP_OSCORE_SUPPORT */
#else /* !COAP_WITH_LIBTINYDTLS && !COAP_WITH_LIBOPENSSL && !COAP_WITH_LIBWOLFSSL && !COAP_WITH_LIBGNUTLS */
#ifdef __clang__
/* Make compilers happy that do not like empty modules. As this function is
* never used, we ignore -Wunused-function at the end of compiling this file
*/
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
static inline void
dummy(void) {
}
#endif /* !COAP_WITH_LIBTINYDTLS && !COAP_WITH_LIBOPENSSL && !COAP_WITH_LIBWOLFSSL && !COAP_WITH_LIBGNUTLS && !COAP_WITH_LIBMBEDTLS */