Skip to content

Commit 194525f

Browse files
authored
Merge pull request #39 from ARMmbed/no_ssl
Allow build without SSL
2 parents 9dfc6f9 + e6b2d21 commit 194525f

File tree

8 files changed

+157
-102
lines changed

8 files changed

+157
-102
lines changed

source/coap_connection_handler.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ typedef struct secure_session {
6464
coap_security_t *sec_handler; //owned
6565
internal_socket_t *parent; //not owned
6666

67+
uint8_t remote_address[16];
68+
uint16_t remote_port;
69+
6770
secure_timer_t timer;
6871

6972
session_state_t session_state;
@@ -72,7 +75,7 @@ typedef struct secure_session {
7275
} secure_session_t;
7376

7477
static NS_LIST_DEFINE(secure_session_list, secure_session_t, link);
75-
static int send_to_socket(int8_t socket_id, const uint8_t *address_ptr, uint16_t port, const uint8_t source_addr[static 16], const void *buf, size_t len);
78+
static int send_to_socket(int8_t socket_id, void *handle, const void *buf, size_t len);
7679
static int receive_from_socket(int8_t socket_id, unsigned char *buf, size_t len);
7780
static void start_timer(int8_t timer_id, uint32_t int_ms, uint32_t fin_ms);
7881
static int timer_status(int8_t timer_id);
@@ -146,8 +149,10 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
146149
timer_id++;
147150
}
148151
this->timer.id = timer_id;
152+
memcpy(this->remote_address, address_ptr, 16);
153+
this->remote_port = port;
149154

150-
this->sec_handler = coap_security_create(parent->listen_socket, this->timer.id, address_ptr, port, ECJPAKE,
155+
this->sec_handler = coap_security_create(parent->listen_socket, this->timer.id, this, ECJPAKE,
151156
&send_to_socket, &receive_from_socket, &start_timer, &timer_status);
152157
if( !this->sec_handler ){
153158
ns_dyn_mem_free(this);
@@ -178,8 +183,8 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui
178183
secure_session_t *this = NULL;
179184
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
180185
if( cur_ptr->sec_handler ){
181-
if (cur_ptr->parent == parent && cur_ptr->sec_handler->_remote_port == port &&
182-
memcmp(cur_ptr->sec_handler->_remote_address, address_ptr, 16) == 0) {
186+
if (cur_ptr->parent == parent && cur_ptr->remote_port == port &&
187+
memcmp(cur_ptr->remote_address, address_ptr, 16) == 0) {
183188
this = cur_ptr;
184189
// hack_save_remote_address(address_ptr, port);
185190
break;
@@ -219,7 +224,11 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
219224
if( !is_secure ){
220225
this->listen_socket = socket_open(SOCKET_UDP, listen_port, recv_sckt_msg);
221226
}else{
227+
#ifdef COAP_SECURITY_AVAILABLE
222228
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
223232
}
224233
// Socket create failed
225234
if(this->listen_socket < 0){
@@ -329,15 +338,16 @@ static int8_t send_to_real_socket(int8_t socket_id, const ns_address_t *address,
329338
return socket_sendmsg(socket_id, &msghdr, 0);
330339
}
331340

332-
static int send_to_socket(int8_t socket_id, const uint8_t *address_ptr, uint16_t port, const uint8_t source_addr[static 16], const void *buf, size_t len)
341+
static int send_to_socket(int8_t socket_id, void *handle, const void *buf, size_t len)
333342
{
343+
secure_session_t *session = handle;
334344
internal_socket_t *sock = int_socket_find_by_socket_id(socket_id);
335345
if(!sock){
336346
return -1;
337347
}
338348
if(!sock->real_socket){
339349
// Send to virtual socket cb
340-
int ret = sock->parent->_send_cb(sock->listen_socket, address_ptr, port, buf, len);
350+
int ret = sock->parent->_send_cb(sock->listen_socket, session->remote_address, session->remote_port, buf, len);
341351
if( ret < 0 )
342352
return ret;
343353
return len;
@@ -353,7 +363,7 @@ static int send_to_socket(int8_t socket_id, const uint8_t *address_ptr, uint16_t
353363
//For some reason socket_sendto returns 0 in success, while other socket impls return number of bytes sent!!!
354364
//TODO: check if address_ptr is valid and use that instead if it is
355365

356-
int8_t ret = send_to_real_socket(sock->listen_socket, &sock->dest_addr, source_addr, buf, len);
366+
int8_t ret = send_to_real_socket(sock->listen_socket, &sock->dest_addr, session->remote_address, buf, len);
357367
if (ret < 0) {
358368
return ret;
359369
}
@@ -536,8 +546,8 @@ static void secure_recv_sckt_msg(void *cb_res)
536546
}
537547
session->last_contact_time = coap_service_get_internal_timer_ticks();
538548
// Start handshake
539-
if (!session->sec_handler->_is_started) {
540-
uint8_t *pw = (uint8_t *)ns_dyn_mem_alloc(64);
549+
if (!coap_security_handler_is_started(session->sec_handler) ){
550+
uint8_t *pw = ns_dyn_mem_alloc(64);
541551
uint8_t pw_len;
542552
if( sock->parent->_get_password_cb && 0 == sock->parent->_get_password_cb(sock->listen_socket, src_address.address, src_address.identifier, pw, &pw_len)){
543553
//TODO: get_password_cb should support certs and PSK also
@@ -560,7 +570,7 @@ static void secure_recv_sckt_msg(void *cb_res)
560570
if( sock->parent->_security_done_cb ){
561571
sock->parent->_security_done_cb(sock->listen_socket, src_address.address,
562572
src_address.identifier,
563-
session->sec_handler->_keyblk.value);
573+
(void *)coap_security_handler_keyblock(session->sec_handler));
564574
}
565575
} else if (ret < 0){
566576
// error handling
@@ -641,8 +651,8 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
641651

642652
session->last_contact_time = coap_service_get_internal_timer_ticks();
643653

644-
if (!session->sec_handler->_is_started) {
645-
uint8_t *pw = (uint8_t *)ns_dyn_mem_alloc(64);
654+
if (!coap_security_handler_is_started(session->sec_handler)) {
655+
uint8_t *pw = ns_dyn_mem_alloc(64);
646656
uint8_t pw_len;
647657
if (sock->parent->_get_password_cb && 0 == sock->parent->_get_password_cb(sock->listen_socket, address, port, pw, &pw_len)) {
648658
//TODO: get_password_cb should support certs and PSK also
@@ -665,7 +675,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
665675
if( handler->_security_done_cb ){
666676
handler->_security_done_cb(sock->listen_socket,
667677
address, port,
668-
session->sec_handler->_keyblk.value);
678+
(void *)coap_security_handler_keyblock(session->sec_handler));
669679
}
670680
return 0;
671681
}
@@ -807,7 +817,7 @@ int coap_connection_handler_send_data(coap_conn_handler_t *handler, const ns_add
807817
memcpy( handler->socket->dest_addr.address, dest_addr->address, 16 );
808818
handler->socket->dest_addr.identifier = dest_addr->identifier;
809819
handler->socket->dest_addr.type = dest_addr->type;
810-
uint8_t *pw = (uint8_t *)ns_dyn_mem_alloc(64);
820+
uint8_t *pw = ns_dyn_mem_alloc(64);
811821
if (!pw) {
812822
//todo: free secure session?
813823
return -1;

source/coap_security_handler.c

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,54 @@
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"
26+
27+
struct coap_security_s {
28+
mbedtls_ssl_config _conf;
29+
mbedtls_ssl_context _ssl;
30+
31+
mbedtls_ctr_drbg_context _ctr_drbg;
32+
mbedtls_entropy_context _entropy;
33+
bool _is_started;
34+
simple_cookie_t _cookie;
35+
key_block_t _keyblk;
36+
37+
SecureConnectionMode _conn_mode;
38+
#if defined(MBEDTLS_X509_CRT_PARSE_C)
39+
mbedtls_x509_crt _cacert;
40+
mbedtls_x509_crt _owncert;
41+
#endif
42+
mbedtls_pk_context _pkey;
43+
44+
uint8_t _pw[64];
45+
uint8_t _pw_len;
46+
47+
bool _is_blocking;
48+
int8_t _socket_id;
49+
int8_t _timer_id;
50+
void *_handle;
51+
send_cb *_send_cb;
52+
receive_cb *_receive_cb;
53+
start_timer_cb *_start_timer_cb;
54+
timer_status_cb *_timer_status_cb;
55+
56+
};
2257

2358
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2459
const int ECJPAKE_SUITES[] = {
@@ -78,6 +113,16 @@ static int coap_security_handler_init(coap_security_t *sec){
78113
return 0;
79114
}
80115

116+
bool coap_security_handler_is_started(const coap_security_t *sec)
117+
{
118+
return sec->_is_started;
119+
}
120+
121+
const void *coap_security_handler_keyblock(const coap_security_t *sec)
122+
{
123+
return sec->_keyblk.value;
124+
}
125+
81126
static void coap_security_handler_reset(coap_security_t *sec){
82127
#if defined(MBEDTLS_X509_CRT_PARSE_C)
83128
mbedtls_x509_crt_free(&sec->_cacert);
@@ -93,13 +138,13 @@ static void coap_security_handler_reset(coap_security_t *sec){
93138
}
94139

95140

96-
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, const uint8_t *address_ptr, uint16_t port, SecureConnectionMode mode,
141+
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle, SecureConnectionMode mode,
97142
send_cb *socket_cb,
98143
receive_cb *receive_data_cb,
99144
start_timer_cb *timer_start_cb,
100145
timer_status_cb *timer_stat_cb)
101146
{
102-
if (!address_ptr || socket_cb == NULL || receive_data_cb == NULL || timer_start_cb == NULL || timer_stat_cb == NULL) {
147+
if (socket_cb == NULL || receive_data_cb == NULL || timer_start_cb == NULL || timer_stat_cb == NULL) {
103148
return NULL;
104149
}
105150
coap_security_t *this = ns_dyn_mem_alloc(sizeof(coap_security_t));
@@ -111,8 +156,7 @@ coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, const u
111156
ns_dyn_mem_free(this);
112157
return NULL;
113158
}
114-
this->_remote_port = port;
115-
memcpy(this->_remote_address, address_ptr, 16);
159+
this->_handle = handle;
116160
this->_conn_mode = mode;
117161
memset(this->_pw, 0, 64);
118162
this->_pw_len = 0;
@@ -552,7 +596,7 @@ static int get_timer(void *sec_obj)
552596

553597
int f_send( void *ctx, const unsigned char *buf, size_t len){
554598
coap_security_t *sec = (coap_security_t *)ctx;
555-
return sec->_send_cb(sec->_socket_id, sec->_remote_address, sec->_remote_port, ns_in6addr_any, buf, len);
599+
return sec->_send_cb(sec->_socket_id, sec->_handle, buf, len);
556600
}
557601

558602
int f_recv(void *ctx, unsigned char *buf, size_t len){
@@ -580,3 +624,5 @@ int entropy_poll( void *ctx, unsigned char *output, size_t len,
580624
ns_dyn_mem_free(c);
581625
return( 0 );
582626
}
627+
628+
#endif // COAP_SECURITY_AVAILABLE

source/include/coap_security_handler.h

Lines changed: 36 additions & 37 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 {
@@ -38,7 +40,7 @@ typedef struct key_block {
3840
unsigned char value[KEY_BLOCK_LEN];
3941
} key_block_t;
4042

41-
typedef int send_cb(int8_t socket_id, const uint8_t *address_ptr, uint16_t port, const uint8_t source_addr[static 16], const void *, size_t);
43+
typedef int send_cb(int8_t socket_id, void *handle, const void *buf, size_t);
4244
typedef int receive_cb(int8_t socket_id, unsigned char *, size_t);
4345
typedef void start_timer_cb(int8_t timer_id, uint32_t min, uint32_t fin);
4446
typedef int timer_status_cb(int8_t timer_id);
@@ -66,40 +68,11 @@ typedef struct {
6668
uint8_t _priv_len;
6769
} coap_security_keys_t;
6870

69-
typedef struct coap_security_s {
70-
mbedtls_ssl_config _conf;
71-
mbedtls_ssl_context _ssl;
72-
73-
mbedtls_ctr_drbg_context _ctr_drbg;
74-
mbedtls_entropy_context _entropy;
75-
bool _is_started;
76-
simple_cookie_t _cookie;
77-
key_block_t _keyblk;
78-
79-
SecureConnectionMode _conn_mode;
80-
#if defined(MBEDTLS_X509_CRT_PARSE_C)
81-
mbedtls_x509_crt _cacert;
82-
mbedtls_x509_crt _owncert;
83-
#endif
84-
mbedtls_pk_context _pkey;
85-
86-
uint8_t _remote_address[16];
87-
uint16_t _remote_port;
88-
89-
uint8_t _pw[64];
90-
uint8_t _pw_len;
71+
typedef struct coap_security_s coap_security_t;
9172

92-
bool _is_blocking;
93-
int8_t _socket_id;
94-
int8_t _timer_id;
95-
send_cb *_send_cb;
96-
receive_cb *_receive_cb;
97-
start_timer_cb *_start_timer_cb;
98-
timer_status_cb *_timer_status_cb;
73+
#ifdef COAP_SECURITY_AVAILABLE
9974

100-
} coap_security_t;
101-
102-
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, const uint8_t *address_ptr, uint16_t port,
75+
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle,
10376
SecureConnectionMode mode,
10477
send_cb *send_cb,
10578
receive_cb *receive_cb,
@@ -120,4 +93,30 @@ int coap_security_send_close_alert(coap_security_t *sec);
12093

12194
int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size_t len);
12295

96+
bool coap_security_handler_is_started(const coap_security_t *sec);
97+
98+
const void *coap_security_handler_keyblock(const coap_security_t *sec);
99+
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+
123122
#endif

0 commit comments

Comments
 (0)