Fix KTLS compilation error
[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 #    ifndef TLS_RX
94 #     define TLS_RX                  2
95 #    endif
96
97 /*
98  * When successful, this socket option doesn't change the behaviour of the
99  * TCP socket, except changing the TCP setsockopt handler to enable the
100  * processing of SOL_TLS socket options. All other functionality remains the
101  * same.
102  */
103 static ossl_inline int ktls_enable(int fd)
104 {
105     return setsockopt(fd, SOL_TCP, TCP_ULP, "tls", sizeof("tls")) ? 0 : 1;
106 }
107
108 /*
109  * The TLS_TX socket option changes the send/sendmsg handlers of the TCP socket.
110  * If successful, then data sent using this socket will be encrypted and
111  * encapsulated in TLS records using the crypto_info provided here.
112  * The TLS_RX socket option changes the recv/recvmsg handlers of the TCP socket.
113  * If successful, then data received using this socket will be decrypted,
114  * authenticated and decapsulated using the crypto_info provided here.
115  */
116 static ossl_inline int ktls_start(int fd,
117                                   struct tls12_crypto_info_aes_gcm_128
118                                   *crypto_info, size_t len, int is_tx)
119 {
120     return setsockopt(fd, SOL_TLS, is_tx ? TLS_TX : TLS_RX,
121                       crypto_info, sizeof(*crypto_info)) ? 0 : 1;
122 }
123
124 /*
125  * Send a TLS record using the crypto_info provided in ktls_start and use
126  * record_type instead of the default SSL3_RT_APPLICATION_DATA.
127  * When the socket is non-blocking, then this call either returns EAGAIN or
128  * the entire record is pushed to TCP. It is impossible to send a partial
129  * record using this control message.
130  */
131 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
132                                               const void *data, size_t length)
133 {
134     struct msghdr msg;
135     int cmsg_len = sizeof(record_type);
136     struct cmsghdr *cmsg;
137     union {
138         struct cmsghdr hdr;
139         char buf[CMSG_SPACE(sizeof(unsigned char))];
140     } cmsgbuf;
141     struct iovec msg_iov;       /* Vector of data to send/receive into */
142
143     memset(&msg, 0, sizeof(msg));
144     msg.msg_control = cmsgbuf.buf;
145     msg.msg_controllen = sizeof(cmsgbuf.buf);
146     cmsg = CMSG_FIRSTHDR(&msg);
147     cmsg->cmsg_level = SOL_TLS;
148     cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
149     cmsg->cmsg_len = CMSG_LEN(cmsg_len);
150     *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
151     msg.msg_controllen = cmsg->cmsg_len;
152
153     msg_iov.iov_base = (void *)data;
154     msg_iov.iov_len = length;
155     msg.msg_iov = &msg_iov;
156     msg.msg_iovlen = 1;
157
158     return sendmsg(fd, &msg, 0);
159 }
160
161 #    define K_MIN1_RX  17
162 #    if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1_RX, K_MIN2)
163
164 #     ifndef PEDANTIC
165 #      warning "KTLS requires Kernel Headers >= 4.17.0 for receiving"
166 #      warning "Skipping Compilation of KTLS receive data path"
167 #     endif
168
169 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
170 {
171     return -1;
172 }
173
174 #    else
175
176 /*
177  * Receive a TLS record using the crypto_info provided in ktls_start.
178  * The kernel strips the TLS record header, IV and authentication tag,
179  * returning only the plaintext data or an error on failure.
180  * We add the TLS record header here to satisfy routines in rec_layer_s3.c
181  */
182 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
183 {
184     struct msghdr msg;
185     struct cmsghdr *cmsg;
186     union {
187         struct cmsghdr hdr;
188         char buf[CMSG_SPACE(sizeof(unsigned char))];
189     } cmsgbuf;
190     struct iovec msg_iov;
191     int ret;
192     unsigned char *p = data;
193     const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
194
195     if (length < prepend_length + EVP_GCM_TLS_TAG_LEN) {
196         errno = EINVAL;
197         return -1;
198     }
199
200     memset(&msg, 0, sizeof(msg));
201     msg.msg_control = cmsgbuf.buf;
202     msg.msg_controllen = sizeof(cmsgbuf.buf);
203
204     msg_iov.iov_base = p + prepend_length;
205     msg_iov.iov_len = length - prepend_length - EVP_GCM_TLS_TAG_LEN;
206     msg.msg_iov = &msg_iov;
207     msg.msg_iovlen = 1;
208
209     ret = recvmsg(fd, &msg, 0);
210     if (ret < 0)
211         return ret;
212
213     if (msg.msg_controllen > 0) {
214         cmsg = CMSG_FIRSTHDR(&msg);
215         if (cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
216             p[0] = *((unsigned char *)CMSG_DATA(cmsg));
217             p[1] = TLS1_2_VERSION_MAJOR;
218             p[2] = TLS1_2_VERSION_MINOR;
219             /* returned length is limited to msg_iov.iov_len above */
220             p[3] = (ret >> 8) & 0xff;
221             p[4] = ret & 0xff;
222             ret += prepend_length;
223         }
224     }
225
226     return ret;
227 }
228
229 #    endif
230 #   endif
231 #  endif
232 # endif
233 #endif