From e66b2aea83046f41ef501f70051b4e8fe773a3df Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 29 Apr 2020 09:49:28 +0100 Subject: [PATCH] DTLS: Fix identity length checking src/coap_tinydtls.c: Pass the correct identity size to coap_get_session_client_psk() --- src/coap_tinydtls.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index bc82b9e7..b56385dd 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -229,9 +229,10 @@ get_psk_info(struct dtls_context_t *dtls_context, } identity_length = 0; + /* result_length is max size of the returned identity */ psk_len = coap_context->get_client_psk(coap_session, (const uint8_t*)id, id_len, (uint8_t*)result, &identity_length, result_length, psk, sizeof(psk)); if (!psk_len) { - coap_log(LOG_WARNING, "no PSK identity for given realm\n"); + coap_log(LOG_WARNING, "no PSK identity for given realm or buffer too small\n"); fatal_error = DTLS_ALERT_CLOSE_NOTIFY; goto error; } @@ -242,17 +243,13 @@ get_psk_info(struct dtls_context_t *dtls_context, if (!coap_context || !coap_context->get_client_psk) goto error; identity_length = 0; - psk_len = coap_context->get_client_psk(coap_session, (const uint8_t*)id, id_len, (uint8_t*)result, &identity_length, result_length, psk, sizeof(psk)); + /* Use psk[] as a scratch area for returning the unused identity */ + psk_len = coap_context->get_client_psk(coap_session, (const uint8_t*)id, id_len, psk, &identity_length, sizeof(psk), result, result_length); if (!psk_len) { - coap_log(LOG_WARNING, "no pre-shared key for given realm\n"); + coap_log(LOG_WARNING, "no pre-shared key for given realm or buffer too small\n"); fatal_error = DTLS_ALERT_CLOSE_NOTIFY; goto error; } - if (psk_len > result_length) { - coap_log(LOG_WARNING, "cannot set psk -- buffer too small\n"); - goto error; - } - memcpy(result, psk, psk_len); return (int)psk_len; } if (coap_context->get_server_psk) {