Skip to content

Commit 7ff1eed

Browse files
committed
net: sockets: tls: Add new option to retrieve cert verification result
Add new TLS socket option, TLS_CERT_VERIFY_RESULT, to obtain the certificate verification result from the most recent handshake on the socket. The option works if TLS_PEER_VERIFY_OPTIONAL was set on the socket, in which case the handshake may succeed even if certificate verification fails. Signed-off-by: Robert Lubos <[email protected]>
1 parent 2fc54ae commit 7ff1eed

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

include/zephyr/net/socket.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ extern "C" {
237237
* will take place in consecutive send()/recv() call.
238238
*/
239239
#define TLS_DTLS_HANDSHAKE_ON_CONNECT 18
240+
/** Read-only socket option to obtain the result of the certificate verification
241+
* from the most recent handshake if TLS_PEER_VERIFY_OPTIONAL was set on the
242+
* socket.
243+
* The option accepts a pointer to 32-bit unsigned integer, holding the
244+
* verification result on return.The result of 0 indicates that verification
245+
* was successful, otherwise the verification result is indicated by a set of
246+
* flags. For mbed TLS backend, the flags are defined in "X509 Verify codes"
247+
* section of x509.h header.
248+
*/
249+
#define TLS_CERT_VERIFY_RESULT 19
240250

241251
/* Valid values for @ref TLS_PEER_VERIFY option */
242252
#define TLS_PEER_VERIFY_NONE 0 /**< Peer verification disabled. */

subsys/net/lib/sockets/sockets_tls.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,18 @@ static int tls_opt_session_cache_get(struct tls_context *context,
19451945
return 0;
19461946
}
19471947

1948+
static int tls_opt_cert_verify_result_get(struct tls_context *context,
1949+
void *optval, socklen_t *optlen)
1950+
{
1951+
if (*optlen != sizeof(uint32_t)) {
1952+
return -EINVAL;
1953+
}
1954+
1955+
*(uint32_t *)optval = mbedtls_ssl_get_verify_result(&context->ssl);
1956+
1957+
return 0;
1958+
}
1959+
19481960
static int tls_opt_session_cache_purge_set(struct tls_context *context,
19491961
const void *optval, socklen_t optlen)
19501962
{
@@ -3493,6 +3505,10 @@ int ztls_getsockopt_ctx(struct tls_context *ctx, int level, int optname,
34933505
err = tls_opt_session_cache_get(ctx, optval, optlen);
34943506
break;
34953507

3508+
case TLS_CERT_VERIFY_RESULT:
3509+
err = tls_opt_cert_verify_result_get(ctx, optval, optlen);
3510+
break;
3511+
34963512
#if defined(CONFIG_NET_SOCKETS_ENABLE_DTLS)
34973513
case TLS_DTLS_HANDSHAKE_TIMEOUT_MIN:
34983514
err = tls_opt_dtls_handshake_timeout_get(ctx, optval,

0 commit comments

Comments
 (0)