Skip to content

Commit 0a93ff4

Browse files
committed
[nrf fromtree] net: lib: tls_credentials: return size required
If either no buffer is provided or the size of it is too small, return the required length. Signed-off-by: Pete Skeggs <[email protected]> (cherry picked from commit 6ec5729)
1 parent ba1d06f commit 0a93ff4

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

include/zephyr/net/tls_credentials.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ int tls_credential_add(sec_tag_t tag, enum tls_credential_type type,
107107
* @retval -EACCES Access to the TLS credential subsystem was denied.
108108
* @retval -ENOENT Requested TLS credential was not found.
109109
* @retval -EFBIG Requested TLS credential does not fit in the buffer provided.
110+
* Check *credlen for size required.
110111
*/
111112
int tls_credential_get(sec_tag_t tag, enum tls_credential_type type,
112113
void *cred, size_t *credlen);

subsys/net/lib/tls_credentials/tls_credentials.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
#include "tls_internal.h"
1212
#include "tls_credentials_digest_raw.h"
1313

14+
#include <zephyr/logging/log.h>
15+
16+
LOG_MODULE_DECLARE(tls_credentials,
17+
CONFIG_TLS_CREDENTIALS_LOG_LEVEL);
18+
1419
/* Global pool of credentials shared among TLS contexts. */
1520
static struct tls_credential credentials[CONFIG_TLS_MAX_CREDENTIALS_NUMBER];
1621

@@ -158,11 +163,18 @@ int tls_credential_get(sec_tag_t tag, enum tls_credential_type type,
158163
credential = credential_get(tag, type);
159164
if (credential == NULL) {
160165
ret = -ENOENT;
166+
*credlen = 0;
161167
goto exit;
162168
}
163169

164170
if (credential->len > *credlen) {
165171
ret = -EFBIG;
172+
LOG_DBG("Not enough room in the credential buffer to "
173+
"retrieve credential with sectag %d and type %d. "
174+
"Increase TLS_CREDENTIALS_SHELL_MAX_CRED_LEN "
175+
">= %d.\n",
176+
tag, (int)type, (int)credential->len);
177+
*credlen = credential->len;
166178
goto exit;
167179
}
168180

0 commit comments

Comments
 (0)