Skip to content

Commit

Permalink
print proper logs messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed Aug 17, 2023
1 parent 1597367 commit c06a36c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
6 changes: 4 additions & 2 deletions security/oc_certs.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,12 @@ oc_certs_parse_CN_buffer_for_UUID(mbedtls_asn1_buf val, char *buffer,
if (uuid_prefix_len == 0 ||
val.len - uuid_prefix_len <
OC_UUID_LEN - 1) { // -1 because val is not nul-terminated
#if OC_ERR_IS_ENABLED
#if OC_DBG_IS_ENABLED
oc_string_t cn;
oc_new_string(&cn, uuid_CN, val.len);
OC_ERR("invalid Common Name field (tag:%d val:%s)", val.tag, oc_string(cn));
OC_DBG("Common Name field (tag:%d val:%s) is not in format " UUID_PREFIX
":<UUID string>",
val.tag, oc_string(cn));
oc_free_string(&cn);
#endif /* OC_ERR_IS_ENABLED */
return false;
Expand Down
49 changes: 45 additions & 4 deletions security/oc_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@
#include <mbedtls/debug.h>
#include <mbedtls/error.h>
#include <mbedtls/platform.h>
#endif /* OC_DEBUG */
#else /* OC_DEBUG */
static const char *
mbedtls_strerror(int ret, char *buf, size_t buflen)
{
snprintf(buf, buflen, "MBEDTLS_ERR(%d)", ret);
return 0;
}
#endif /* !OC_DEBUG */

#include <stdarg.h>
#include <stdint.h>
Expand Down Expand Up @@ -1347,9 +1354,38 @@ oc_tls_configure_end_entity_cert_chain(mbedtls_ssl_config *conf, size_t device,
}
cert = cert->next;
}

if (!cert || mbedtls_ssl_conf_own_cert(conf, &cert->cert, &cert->pk) != 0) {
OC_WRN("error configuring identity cert");
if (!cert) {
#if OC_WRN_IS_ENABLED
char credid_str[16];
memset(credid_str, 0, sizeof(credid_str));
if (credid == -1) {
strncpy(credid_str, "any", sizeof(credid_str));
} else {
snprintf(credid_str, sizeof(credid_str), "%d", credid);
}
OC_WRN(
"cannot set client %s certificate(selected %s): certificate not found",
credusage == OC_CREDUSAGE_MFG_CERT ? "manufacturer" : "identity",
credid_str);
#endif /* OC_WRN_IS_ENABLED */
return -1;
}
int err = mbedtls_ssl_conf_own_cert(conf, &cert->cert, &cert->pk);
if (err != 0) {
#if OC_WRN_IS_ENABLED
char credid_str[16];
memset(credid_str, 0, sizeof(credid_str));
if (credid == -1) {
strncpy(credid_str, "any", sizeof(credid_str));
} else {
snprintf(credid_str, sizeof(credid_str), "%d", credid);
}
char buf[128];
memset(buf, 0, sizeof(buf));
OC_WRN("cannot set client %s certificate(selected %s): %s",
credusage == OC_CREDUSAGE_MFG_CERT ? "manufacturer" : "identity",
credid_str, mbedtls_strerror(err, buf, sizeof(buf)));
#endif /* OC_WRN_IS_ENABLED */
return -1;
}

Expand All @@ -1368,6 +1404,11 @@ static int
oc_tls_load_identity_cert_chain(mbedtls_ssl_config *conf, size_t device,
int credid)
{
if (credid < -1) {
// could be set when the application wants to use manufacturer certificate
// instead of identity certificate
return -1;
}
OC_DBG("loading identity cert chain");
return oc_tls_configure_end_entity_cert_chain(
conf, device, OC_CREDUSAGE_IDENTITY_CERT, credid);
Expand Down

0 comments on commit c06a36c

Please sign in to comment.