Skip to content

Commit 9a9085d

Browse files
author
Mika Leppänen
authored
Updated coap service to be compatible with mbed TLS 3.0 (ARMmbed#135)
Disabled key material export for now, needs to be updated later to new key export function if needed. Made the SSL state to refer to private state for now. Do not include config, instead include version.h that is present in both 2.0 and 3.0 and will include config and defines version macros.
1 parent bbe0173 commit 9a9085d

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

source/coap_security_handler.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323

2424
#ifdef COAP_SECURITY_AVAILABLE
2525

26+
#include "mbedtls/version.h"
2627
#include "mbedtls/sha256.h"
2728
#include "mbedtls/error.h"
2829
#include "mbedtls/platform.h"
2930
#include "mbedtls/ssl_cookie.h"
3031
#include "mbedtls/entropy.h"
31-
#include "mbedtls/entropy_poll.h"
3232
#include "mbedtls/ctr_drbg.h"
3333
#include "mbedtls/hmac_drbg.h"
3434
#include "mbedtls/ssl_ciphersuites.h"
@@ -310,6 +310,7 @@ static int simple_cookie_check(void *ctx,
310310

311311
/**** Key export function ****/
312312
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
313+
#if (MBEDTLS_VERSION_MAJOR < 3)
313314
static int export_key_block(void *ctx,
314315
const unsigned char *mk, const unsigned char *kb,
315316
size_t maclen, size_t keylen, size_t ivlen)
@@ -330,6 +331,7 @@ static int export_key_block(void *ctx,
330331
return 0;
331332
}
332333
#endif
334+
#endif
333335

334336
static int coap_security_handler_configure_keys(coap_security_t *sec, coap_security_keys_t keys, bool is_server)
335337
{
@@ -343,9 +345,15 @@ static int coap_security_handler_configure_keys(coap_security_t *sec, coap_secur
343345
break;
344346
}
345347

348+
#if (MBEDTLS_VERSION_MAJOR >= 3)
349+
if (mbedtls_pk_parse_key(&sec->_pkey, keys._priv_key, keys._priv_key_len, NULL, 0, DRBG_RANDOM, &sec->_drbg) < 0) {
350+
break;
351+
}
352+
#else
346353
if (mbedtls_pk_parse_key(&sec->_pkey, keys._priv_key, keys._priv_key_len, NULL, 0) < 0) {
347354
break;
348355
}
356+
#endif
349357

350358
if (0 != mbedtls_ssl_conf_own_cert(&sec->_conf, &sec->_owncert, &sec->_pkey)) {
351359
break;
@@ -378,10 +386,15 @@ static int coap_security_handler_configure_keys(coap_security_t *sec, coap_secur
378386
mbedtls_ssl_conf_ciphersuites(&sec->_conf, ECJPAKE_SUITES);
379387
#endif /* !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) */
380388

389+
#if (MBEDTLS_VERSION_MAJOR >= 3)
390+
tr_error("FATAL ERROR: support for mbedtls_ssl_set_export_keys_cb() not implemented");
391+
#else
381392
//NOTE: If thread starts supporting PSK in other modes, then this will be needed!
382393
mbedtls_ssl_conf_export_keys_cb(&sec->_conf,
383394
export_key_block,
384395
&sec->_keyblk);
396+
#endif
397+
385398
ret = 0;
386399
#endif
387400
break;
@@ -512,9 +525,15 @@ int coap_security_handler_continue_connecting(coap_security_t *sec)
512525
return ret;
513526
}
514527

528+
#if (MBEDTLS_VERSION_MAJOR >= 3)
529+
if (sec->_ssl.private_state == MBEDTLS_SSL_HANDSHAKE_OVER) {
530+
return 0;
531+
}
532+
#else
515533
if (sec->_ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER) {
516534
return 0;
517535
}
536+
#endif
518537
}
519538

520539
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {

source/include/coap_security_handler.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@
2020
#include "ns_types.h"
2121

2222
#ifdef NS_USE_EXTERNAL_MBED_TLS
23-
#if !defined(MBEDTLS_CONFIG_FILE)
24-
#include "mbedtls/config.h"
25-
#else
2623
// cppcheck-suppress preprocessorErrorDirective
27-
#include MBEDTLS_CONFIG_FILE
28-
#endif
24+
#include "mbedtls/version.h"
2925

3026
#if defined(MBEDTLS_SSL_TLS_C)
3127
#include "mbedtls/ssl.h"

test/coap-service/unittest/coap_security_handler/test_coap_security_handler.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ bool test_coap_security_handler_connect()
184184
}
185185

186186
mbedtls_stub.counter = 0;
187-
mbedtls_stub.retArray[5] = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
187+
mbedtls_stub.retArray[5] = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
188188

189189
if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) {
190190
return false;
@@ -230,9 +230,9 @@ bool test_coap_security_handler_continue_connecting()
230230
}
231231

232232
mbedtls_stub.counter = 0;
233-
mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
233+
mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
234234

235-
if (MBEDTLS_ERR_SSL_BAD_HS_FINISHED != coap_security_handler_continue_connecting(handle)) {
235+
if (MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL != coap_security_handler_continue_connecting(handle)) {
236236
return false;
237237
}
238238

test/coap-service/unittest/stub/mbedtls_stub.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
2727

2828
if (mbedtls_stub.retArray[mbedtls_stub.counter] == HANDSHAKE_FINISHED_VALUE ||
2929
mbedtls_stub.retArray[mbedtls_stub.counter] == HANDSHAKE_FINISHED_VALUE_RETURN_ZERO) {
30-
30+
#if (MBEDTLS_VERSION_MAJOR >= 3)
31+
ssl->private_state = MBEDTLS_SSL_HANDSHAKE_OVER;
32+
#else
3133
ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
34+
#endif
3235
if (mbedtls_stub.retArray[mbedtls_stub.counter] == HANDSHAKE_FINISHED_VALUE_RETURN_ZERO) {
3336
return 0;
3437
}
@@ -346,9 +349,16 @@ int mbedtls_entropy_add_source(mbedtls_entropy_context *a,
346349
}
347350

348351
//From pk.h
352+
#if (MBEDTLS_VERSION_MAJOR >= 3)
353+
int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
354+
const unsigned char *b, size_t c,
355+
const unsigned char *d, size_t e,
356+
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
357+
#else
349358
int mbedtls_pk_parse_key(mbedtls_pk_context *a,
350359
const unsigned char *b, size_t c,
351360
const unsigned char *d, size_t e)
361+
#endif
352362
{
353363
if (mbedtls_stub.useCounter) {
354364
return mbedtls_stub.retArray[mbedtls_stub.counter++];
@@ -396,6 +406,7 @@ void mbedtls_ssl_conf_dtls_cookies(mbedtls_ssl_config *conf,
396406
}
397407
}
398408

409+
#if (MBEDTLS_VERSION_MAJOR < 3)
399410
void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
400411
mbedtls_ssl_export_keys_t *f_export_keys,
401412
void *p_export_keys)
@@ -408,6 +419,7 @@ void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
408419
f_export_keys(p_export_keys, &value, "", 0, 20, 0); //success case
409420
}
410421
}
422+
#endif
411423

412424
int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
413425
{

0 commit comments

Comments
 (0)