5495a8de5199721134fb221a48632b0c02621d45
[openssl.git] / include / internal / ktls.h
1 /*
2  * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef OPENSSL_NO_KTLS
11 # ifndef HEADER_INTERNAL_KTLS
12 #  define HEADER_INTERNAL_KTLS
13
14 #  if defined(OPENSSL_SYS_LINUX)
15 #   include <linux/version.h>
16
17 #   define K_MAJ   4
18 #   define K_MIN1  13
19 #   define K_MIN2  0
20 #   if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2)
21
22 #    ifndef PEDANTIC
23 #     warning "KTLS requires Kernel Headers >= 4.13.0"
24 #     warning "Skipping Compilation of KTLS"
25 #    endif
26
27 #    define TLS_TX                  1
28 #    define TLS_RX                  2
29
30 #    define TLS_CIPHER_AES_GCM_128                          51
31 #    define TLS_CIPHER_AES_GCM_128_IV_SIZE                  8
32 #    define TLS_CIPHER_AES_GCM_128_KEY_SIZE                 16
33 #    define TLS_CIPHER_AES_GCM_128_SALT_SIZE                4
34 #    define TLS_CIPHER_AES_GCM_128_TAG_SIZE                 16
35 #    define TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE             8
36
37 #    define TLS_SET_RECORD_TYPE     1
38
39 struct tls_crypto_info {
40     unsigned short version;
41     unsigned short cipher_type;
42 };
43
44 struct tls12_crypto_info_aes_gcm_128 {
45     struct tls_crypto_info info;
46     unsigned char iv[TLS_CIPHER_AES_GCM_128_IV_SIZE];
47     unsigned char key[TLS_CIPHER_AES_GCM_128_KEY_SIZE];
48     unsigned char salt[TLS_CIPHER_AES_GCM_128_SALT_SIZE];
49     unsigned char rec_seq[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
50 };
51
52 /* Dummy functions here */
53 static ossl_inline int ktls_enable(int fd)
54 {
55     return 0;
56 }
57
58 static ossl_inline int ktls_start(int fd,
59                                   struct tls12_crypto_info_aes_gcm_128
60                                   *crypto_info, size_t len, int is_tx)
61 {
62     return 0;
63 }
64
65 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
66                                               const void *data, size_t length)
67 {
68     return -1;
69 }
70
71 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
72 {
73     return -1;
74 }
75
76 #   else                        /* KERNEL_VERSION */
77
78 #    include <netinet/tcp.h>
79 #    include <linux/tls.h>
80 #    include <linux/socket.h>
81 #    include "openssl/ssl3.h"
82 #    include "openssl/tls1.h"
83 #    include "openssl/evp.h"
84
85 #    ifndef SOL_TLS
86 #     define SOL_TLS 282
87 #    endif
88
89 #    ifndef TCP_ULP
90 #     define TCP_ULP 31
91 #    endif
92
93 /*
94  * When successful, this socket option doesn't change the behaviour of the
95  * TCP socket, except changing the TCP setsockopt handler to enable the
96  * processing of SOL_TLS socket options. All other functionality remains the
97  * same.
98  */
99 static ossl_inline int ktls_enable(int fd)
100 {
101     return setsockopt(fd, SOL_TCP, TCP_ULP, "tls", sizeof("tls")) ? 0 : 1;
102 }
103
104 /*
105  * The TLS_TX socket option changes the send/sendmsg handlers of the TCP socket.
106  * If successful, then data sent using this socket will be encrypted and
107  * encapsulated in TLS records using the crypto_info provided here.
108  * The TLS_RX socket option changes the recv/recvmsg handlers of the TCP socket.
109  * If successful, then data received using this socket will be decrypted,
110  * authenticated and decapsulated using the crypto_info provided here.
111  */
112 static ossl_inline int ktls_start(int fd,
113                                   struct tls12_crypto_info_aes_gcm_128
114                                   *crypto_info, size_t len, int is_tx)
115 {
116     return setsockopt(fd, SOL_TLS, is_tx ? TLS_TX : TLS_RX,
117                       crypto_info, sizeof(*crypto_info)) ? 0 : 1;
118 }
119
120 /*
121  * Send a TLS record using the crypto_info provided in ktls_start and use
122  * record_type instead of the default SSL3_RT_APPLICATION_DATA.
123  * When the socket is non-blocking, then this call either returns EAGAIN or
124  * the entire record is pushed to TCP. It is impossible to send a partial
125  * record using this control message.
126  */
127 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
128                                               const void *data, size_t length)
129 {
130     struct msghdr msg;
131     int cmsg_len = sizeof(record_type);
132     struct cmsghdr *cmsg;
133     union {
134         struct cmsghdr hdr;
135         char buf[CMSG_SPACE(sizeof(unsigned char))];
136     } cmsgbuf;
137     struct iovec msg_iov;       /* Vector of data to send/receive into */
138
139     memset(&msg, 0, sizeof(msg));
140     msg.msg_control = cmsgbuf.buf;
141     msg.msg_controllen = sizeof(cmsgbuf.buf);
142     cmsg = CMSG_FIRSTHDR(&msg);
143     cmsg->cmsg_level = SOL_TLS;
144     cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
145     cmsg->cmsg_len = CMSG_LEN(cmsg_len);
146     *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
147     msg.msg_controllen = cmsg->cmsg_len;
148
149     msg_iov.iov_base = (void *)data;
150     msg_iov.iov_len = length;
151     msg.msg_iov = &msg_iov;
152     msg.msg_iovlen = 1;
153
154     return sendmsg(fd, &msg, 0);
155 }
156
157 #    define K_MIN1_RX  17
158 #    if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1_RX, K_MIN2)
159
160 #     ifndef PEDANTIC
161 #      warning "KTLS requires Kernel Headers >= 4.17.0 for receiving"
162 #      warning "Skipping Compilation of KTLS receive data path"
163 #     endif
164
165 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
166 {
167     return -1;
168 }
169
170 #    else
171
172 /*
173  * Receive a TLS record using the crypto_info provided in ktls_start.
174  * The kernel strips the TLS record header, IV and authentication tag,
175  * returning only the plaintext data or an error on failure.
176  * We add the TLS record header here to satisfy routines in rec_layer_s3.c
177  */
178 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
179 {
180     struct msghdr msg;
181     struct cmsghdr *cmsg;
182     union {
183         struct cmsghdr hdr;
184         char buf[CMSG_SPACE(sizeof(unsigned char))];
185     } cmsgbuf;
186     struct iovec msg_iov;
187     int ret;
188     unsigned char *p = data;
189     const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
190
191     if (length < prepend_length + EVP_GCM_TLS_TAG_LEN) {
192         errno = EINVAL;
193         return -1;
194     }
195
196     memset(&msg, 0, sizeof(msg));
197     msg.msg_control = cmsgbuf.buf;
198     msg.msg_controllen = sizeof(cmsgbuf.buf);
199
200     msg_iov.iov_base = p + prepend_length;
201     msg_iov.iov_len = length - prepend_length - EVP_GCM_TLS_TAG_LEN;
202     msg.msg_iov = &msg_iov;
203     msg.msg_iovlen = 1;
204
205     ret = recvmsg(fd, &msg, 0);
206     if (ret < 0)
207         return ret;
208
209     if (msg.msg_controllen > 0) {
210         cmsg = CMSG_FIRSTHDR(&msg);
211         if (cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
212             p[0] = *((unsigned char *)CMSG_DATA(cmsg));
213             p[1] = TLS1_2_VERSION_MAJOR;
214             p[2] = TLS1_2_VERSION_MINOR;
215             /* returned length is limited to msg_iov.iov_len above */
216             p[3] = (ret >> 8) & 0xff;
217             p[4] = ret & 0xff;
218             ret += prepend_length;
219         }
220     }
221
222     return ret;
223 }
224
225 #    endif
226 #   endif
227 #  endif
228 # endif
229 #endif