Skip to content

Commit e6b2d21

Browse files
committed
Allow build without SSL
NS_USE_EXTERNAL_MBED_TLS now controls whether we attempt to include mbedTLS header files at all, and after including them, we check whether SSL/TLS is enabled. If not, we provide non-secure operation only.
1 parent ac8ddaf commit e6b2d21

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

source/coap_connection_handler.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,11 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
224224
if( !is_secure ){
225225
this->listen_socket = socket_open(SOCKET_UDP, listen_port, recv_sckt_msg);
226226
}else{
227+
#ifdef COAP_SECURITY_AVAILABLE
227228
this->listen_socket = socket_open(SOCKET_UDP, listen_port, secure_recv_sckt_msg);
229+
#else
230+
tr_err("Secure CoAP unavailable - SSL library not configured, possibly due to lack of entropy source");
231+
#endif
228232
}
229233
// Socket create failed
230234
if(this->listen_socket < 0){

source/coap_security_handler.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
#include <time.h>
77
#include <stdlib.h>
88

9+
#include "coap_security_handler.h"
10+
11+
#ifdef COAP_SECURITY_AVAILABLE
12+
913
#include "mbedtls/sha256.h"
1014
#include "mbedtls/error.h"
1115
#include "mbedtls/platform.h"
1216
#include "mbedtls/ssl_cookie.h"
17+
#include "mbedtls/entropy.h"
1318
#include "mbedtls/entropy_poll.h"
14-
#include "mbedtls/ssl.h"
19+
#include "mbedtls/ctr_drbg.h"
20+
#include "mbedtls/ssl_ciphersuites.h"
21+
1522
#include "ns_trace.h"
1623
#include "nsdynmemLIB.h"
1724
#include "coap_connection_handler.h"
18-
#include "coap_security_handler.h"
1925
#include "randLIB.h"
20-
#include "mbedtls/ssl_ciphersuites.h"
21-
#include "socket_api.h"
2226

2327
struct coap_security_s {
2428
mbedtls_ssl_config _conf;
@@ -620,3 +624,5 @@ int entropy_poll( void *ctx, unsigned char *output, size_t len,
620624
ns_dyn_mem_free(c);
621625
return( 0 );
622626
}
627+
628+
#endif // COAP_SECURITY_AVAILABLE

source/include/coap_security_handler.h

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
#include <stddef.h>
2222
#include <inttypes.h>
2323
#include <stdbool.h>
24-
#include "mbedtls/platform.h"
24+
25+
#ifdef NS_USE_EXTERNAL_MBED_TLS
2526
#include "mbedtls/ssl.h"
26-
#include "mbedtls/sha256.h"
27-
#include "mbedtls/entropy.h"
28-
#include "mbedtls/ctr_drbg.h"
27+
#ifdef MBEDTLS_SSL_TLS_C
28+
#define COAP_SECURITY_AVAILABLE
29+
#endif
30+
#endif
2931

3032
#define COOKIE_SIMPLE_LEN 8
3133
typedef struct simple_cookie {
@@ -68,6 +70,8 @@ typedef struct {
6870

6971
typedef struct coap_security_s coap_security_t;
7072

73+
#ifdef COAP_SECURITY_AVAILABLE
74+
7175
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle,
7276
SecureConnectionMode mode,
7377
send_cb *send_cb,
@@ -93,4 +97,26 @@ bool coap_security_handler_is_started(const coap_security_t *sec);
9397

9498
const void *coap_security_handler_keyblock(const coap_security_t *sec);
9599

100+
#else
101+
102+
/* Dummy definitions, including needed error codes */
103+
#define MBEDTLS_ERR_SSL_TIMEOUT (-1)
104+
#define MBEDTLS_ERR_SSL_WANT_READ (-2)
105+
#define MBEDTLS_ERR_SSL_WANT_WRITE (-3)
106+
#define MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE (-4)
107+
108+
#define coap_security_create(socket_id, timer_id, handle, \
109+
mode, send_cb, receive_cb, start_timer_cb, timer_status_cb) ((coap_security_t *) 0)
110+
#define coap_security_destroy(sec) ((void) 0)
111+
#define coap_security_handler_connect(sec, is_server, sock_mode, keys) (-1)
112+
#define coap_security_handler_connect_non_blocking(sec, is_server, sock_mode, keys, timeout_min, timeout_max) (-1)
113+
#define coap_security_handler_continue_connecting(sec) (-1)
114+
#define coap_security_handler_send_message(sec, message, len) (-1)
115+
#define coap_security_send_close_alert(sec) (-1)
116+
#define coap_security_handler_read(sec, buffer, len) (-1)
117+
#define coap_security_handler_is_started(sec) false
118+
#define coap_security_handler_keyblock(sec) ((void *) 0)
119+
120+
#endif /* COAP_SECURITY_AVAILABLE */
121+
96122
#endif

0 commit comments

Comments
 (0)