Skip to content

Commit 39d0e38

Browse files
chuckleverkuba-moo
authored andcommitted
net/handshake: Add helpers for parsing incoming TLS Alerts
Kernel TLS consumers can replace common TLS Alert parsing code with these helpers. Signed-off-by: Chuck Lever <[email protected]> Link: https://lore.kernel.org/r/169047942074.5241.13791647439480672048.stgit@oracle-102.nfsv4bat.org Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 5dd5ad6 commit 39d0e38

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

include/net/handshake.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ int tls_server_hello_psk(const struct tls_handshake_args *args, gfp_t flags);
4242
bool tls_handshake_cancel(struct sock *sk);
4343
void tls_handshake_close(struct socket *sock);
4444

45+
u8 tls_get_record_type(const struct sock *sk, const struct cmsghdr *msg);
46+
void tls_alert_recv(const struct sock *sk, const struct msghdr *msg,
47+
u8 *level, u8 *description);
48+
4549
#endif /* _NET_HANDSHAKE_H */

net/handshake/alert.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,45 @@ int tls_alert_send(struct socket *sock, u8 level, u8 description)
5959
ret = sock_sendmsg(sock, &msg);
6060
return ret < 0 ? ret : 0;
6161
}
62+
63+
/**
64+
* tls_get_record_type - Look for TLS RECORD_TYPE information
65+
* @sk: socket (for IP address information)
66+
* @cmsg: incoming message to be parsed
67+
*
68+
* Returns zero or a TLS_RECORD_TYPE value.
69+
*/
70+
u8 tls_get_record_type(const struct sock *sk, const struct cmsghdr *cmsg)
71+
{
72+
u8 record_type;
73+
74+
if (cmsg->cmsg_level != SOL_TLS)
75+
return 0;
76+
if (cmsg->cmsg_type != TLS_GET_RECORD_TYPE)
77+
return 0;
78+
79+
record_type = *((u8 *)CMSG_DATA(cmsg));
80+
return record_type;
81+
}
82+
EXPORT_SYMBOL(tls_get_record_type);
83+
84+
/**
85+
* tls_alert_recv - Parse TLS Alert messages
86+
* @sk: socket (for IP address information)
87+
* @msg: incoming message to be parsed
88+
* @level: OUT - TLS AlertLevel value
89+
* @description: OUT - TLS AlertDescription value
90+
*
91+
*/
92+
void tls_alert_recv(const struct sock *sk, const struct msghdr *msg,
93+
u8 *level, u8 *description)
94+
{
95+
const struct kvec *iov;
96+
u8 *data;
97+
98+
iov = msg->msg_iter.kvec;
99+
data = iov->iov_base;
100+
*level = data[0];
101+
*description = data[1];
102+
}
103+
EXPORT_SYMBOL(tls_alert_recv);

0 commit comments

Comments
 (0)