Skip to content

Commit

Permalink
net/handshake: Add helpers for parsing incoming TLS Alerts
Browse files Browse the repository at this point in the history
Kernel TLS consumers can replace common TLS Alert parsing code with
these helpers.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
Link: https://lore.kernel.org/r/169047942074.5241.13791647439480672048.stgit@oracle-102.nfsv4bat.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Chuck Lever authored and Jakub Kicinski committed Jul 28, 2023
1 parent 5dd5ad6 commit 39d0e38
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/net/handshake.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ int tls_server_hello_psk(const struct tls_handshake_args *args, gfp_t flags);
bool tls_handshake_cancel(struct sock *sk);
void tls_handshake_close(struct socket *sock);

u8 tls_get_record_type(const struct sock *sk, const struct cmsghdr *msg);
void tls_alert_recv(const struct sock *sk, const struct msghdr *msg,
u8 *level, u8 *description);

#endif /* _NET_HANDSHAKE_H */
42 changes: 42 additions & 0 deletions net/handshake/alert.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,45 @@ int tls_alert_send(struct socket *sock, u8 level, u8 description)
ret = sock_sendmsg(sock, &msg);
return ret < 0 ? ret : 0;
}

/**
* tls_get_record_type - Look for TLS RECORD_TYPE information
* @sk: socket (for IP address information)
* @cmsg: incoming message to be parsed
*
* Returns zero or a TLS_RECORD_TYPE value.
*/
u8 tls_get_record_type(const struct sock *sk, const struct cmsghdr *cmsg)
{
u8 record_type;

if (cmsg->cmsg_level != SOL_TLS)
return 0;
if (cmsg->cmsg_type != TLS_GET_RECORD_TYPE)
return 0;

record_type = *((u8 *)CMSG_DATA(cmsg));
return record_type;
}
EXPORT_SYMBOL(tls_get_record_type);

/**
* tls_alert_recv - Parse TLS Alert messages
* @sk: socket (for IP address information)
* @msg: incoming message to be parsed
* @level: OUT - TLS AlertLevel value
* @description: OUT - TLS AlertDescription value
*
*/
void tls_alert_recv(const struct sock *sk, const struct msghdr *msg,
u8 *level, u8 *description)
{
const struct kvec *iov;
u8 *data;

iov = msg->msg_iter.kvec;
data = iov->iov_base;
*level = data[0];
*description = data[1];
}
EXPORT_SYMBOL(tls_alert_recv);

0 comments on commit 39d0e38

Please sign in to comment.