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