Update copyright year
[openssl.git] / ssl / ktls.c
1 /*
2  * Copyright 2018-2022 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 #include "ssl_local.h"
11 #include "internal/ktls.h"
12
13 #ifndef OPENSSL_NO_KTLS_RX
14  /*
15   * Count the number of records that were not processed yet from record boundary.
16   *
17   * This function assumes that there are only fully formed records read in the
18   * record layer. If read_ahead is enabled, then this might be false and this
19   * function will fail.
20   */
21 static int count_unprocessed_records(SSL *s)
22 {
23     SSL3_BUFFER *rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
24     PACKET pkt, subpkt;
25     int count = 0;
26
27     if (!PACKET_buf_init(&pkt, rbuf->buf + rbuf->offset, rbuf->left))
28         return -1;
29
30     while (PACKET_remaining(&pkt) > 0) {
31         /* Skip record type and version */
32         if (!PACKET_forward(&pkt, 3))
33             return -1;
34
35         /* Read until next record */
36         if (!PACKET_get_length_prefixed_2(&pkt, &subpkt))
37             return -1;
38
39         count += 1;
40     }
41
42     return count;
43 }
44
45 /*
46  * The kernel cannot offload receive if a partial TLS record has been read.
47  * Check the read buffer for unprocessed records.  If the buffer contains a
48  * partial record, fail and return 0.  Otherwise, update the sequence
49  * number at *rec_seq for the count of unprocessed records and return 1.
50  */
51 static int check_rx_read_ahead(SSL *s, unsigned char *rec_seq)
52 {
53     int bit, count_unprocessed;
54
55     count_unprocessed = count_unprocessed_records(s);
56     if (count_unprocessed < 0)
57         return 0;
58
59     /* increment the crypto_info record sequence */
60     while (count_unprocessed) {
61         for (bit = 7; bit >= 0; bit--) { /* increment */
62             ++rec_seq[bit];
63             if (rec_seq[bit] != 0)
64                 break;
65         }
66         count_unprocessed--;
67
68     }
69
70     return 1;
71 }
72 #endif
73
74 #if defined(__FreeBSD__)
75 # include "crypto/cryptodev.h"
76
77 /*-
78  * Check if a given cipher is supported by the KTLS interface.
79  * The kernel might still fail the setsockopt() if no suitable
80  * provider is found, but this checks if the socket option
81  * supports the cipher suite used at all.
82  */
83 int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c,
84                                 const EVP_CIPHER_CTX *dd)
85 {
86
87     switch (s->version) {
88     case TLS1_VERSION:
89     case TLS1_1_VERSION:
90     case TLS1_2_VERSION:
91     case TLS1_3_VERSION:
92         break;
93     default:
94         return 0;
95     }
96
97     switch (s->s3.tmp.new_cipher->algorithm_enc) {
98     case SSL_AES128GCM:
99     case SSL_AES256GCM:
100         return 1;
101 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
102     case SSL_CHACHA20POLY1305:
103         return 1;
104 # endif
105     case SSL_AES128:
106     case SSL_AES256:
107         if (s->ext.use_etm)
108             return 0;
109         switch (s->s3.tmp.new_cipher->algorithm_mac) {
110         case SSL_SHA1:
111         case SSL_SHA256:
112         case SSL_SHA384:
113             return 1;
114         default:
115             return 0;
116         }
117     default:
118         return 0;
119     }
120 }
121
122 /* Function to configure kernel TLS structure */
123 int ktls_configure_crypto(SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd,
124                           void *rl_sequence, ktls_crypto_info_t *crypto_info,
125                           int is_tx, unsigned char *iv,
126                           unsigned char *key, unsigned char *mac_key,
127                           size_t mac_secret_size)
128 {
129     memset(crypto_info, 0, sizeof(*crypto_info));
130     switch (s->s3.tmp.new_cipher->algorithm_enc) {
131     case SSL_AES128GCM:
132     case SSL_AES256GCM:
133         crypto_info->cipher_algorithm = CRYPTO_AES_NIST_GCM_16;
134         if (s->version == TLS1_3_VERSION)
135             crypto_info->iv_len = EVP_CIPHER_CTX_get_iv_length(dd);
136         else
137             crypto_info->iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
138         break;
139 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
140     case SSL_CHACHA20POLY1305:
141         crypto_info->cipher_algorithm = CRYPTO_CHACHA20_POLY1305;
142         crypto_info->iv_len = EVP_CIPHER_CTX_get_iv_length(dd);
143         break;
144 # endif
145     case SSL_AES128:
146     case SSL_AES256:
147         switch (s->s3.tmp.new_cipher->algorithm_mac) {
148         case SSL_SHA1:
149             crypto_info->auth_algorithm = CRYPTO_SHA1_HMAC;
150             break;
151         case SSL_SHA256:
152             crypto_info->auth_algorithm = CRYPTO_SHA2_256_HMAC;
153             break;
154         case SSL_SHA384:
155             crypto_info->auth_algorithm = CRYPTO_SHA2_384_HMAC;
156             break;
157         default:
158             return 0;
159         }
160         crypto_info->cipher_algorithm = CRYPTO_AES_CBC;
161         crypto_info->iv_len = EVP_CIPHER_get_iv_length(c);
162         crypto_info->auth_key = mac_key;
163         crypto_info->auth_key_len = mac_secret_size;
164         break;
165     default:
166         return 0;
167     }
168     crypto_info->cipher_key = key;
169     crypto_info->cipher_key_len = EVP_CIPHER_get_key_length(c);
170     crypto_info->iv = iv;
171     crypto_info->tls_vmajor = (s->version >> 8) & 0x000000ff;
172     crypto_info->tls_vminor = (s->version & 0x000000ff);
173 # ifdef TCP_RXTLS_ENABLE
174     memcpy(crypto_info->rec_seq, rl_sequence, sizeof(crypto_info->rec_seq));
175     if (!is_tx && !check_rx_read_ahead(s, crypto_info->rec_seq))
176         return 0;
177 # else
178     if (!is_tx)
179         return 0;
180 # endif
181     return 1;
182 };
183
184 #endif                         /* __FreeBSD__ */
185
186 #if defined(OPENSSL_SYS_LINUX)
187
188 /* Function to check supported ciphers in Linux */
189 int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c,
190                                 const EVP_CIPHER_CTX *dd)
191 {
192     switch (s->version) {
193     case TLS1_2_VERSION:
194     case TLS1_3_VERSION:
195         break;
196     default:
197         return 0;
198     }
199
200     /* check that cipher is AES_GCM_128, AES_GCM_256, AES_CCM_128 
201      * or Chacha20-Poly1305
202      */
203 # ifdef OPENSSL_KTLS_AES_CCM_128
204     if (EVP_CIPHER_is_a(c, "AES-128-CCM")) {
205         if (s->version == TLS_1_3_VERSION /* broken on 5.x kernels */
206             || EVP_CIPHER_CTX_get_tag_length(dd) != EVP_CCM_TLS_TAG_LEN)
207             return 0;
208         return 1;
209     } else
210 # endif
211     if (0
212 # ifdef OPENSSL_KTLS_AES_GCM_128
213         || EVP_CIPHER_is_a(c, "AES-128-GCM")
214 # endif
215 # ifdef OPENSSL_KTLS_AES_GCM_256
216         || EVP_CIPHER_is_a(c, "AES-256-GCM")
217 # endif
218 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
219         || EVP_CIPHER_is_a(c, "ChaCha20-Poly1305")
220 # endif
221         ) {
222         return 1;
223     }
224     return 0;
225 }
226
227 /* Function to configure kernel TLS structure */
228 int ktls_configure_crypto(SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd,
229                           void *rl_sequence, ktls_crypto_info_t *crypto_info,
230                           int is_tx, unsigned char *iv,
231                           unsigned char *key, unsigned char *mac_key,
232                           size_t mac_secret_size)
233 {
234     unsigned char geniv[12];
235     unsigned char *iiv = iv;
236
237 # ifdef OPENSSL_NO_KTLS_RX
238     if (!is_tx)
239         return 0;
240 # endif
241
242     if (s->version == TLS1_2_VERSION &&
243         EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE) {
244         if (!EVP_CIPHER_CTX_get_updated_iv(dd, geniv,
245                                            EVP_GCM_TLS_FIXED_IV_LEN
246                                            + EVP_GCM_TLS_EXPLICIT_IV_LEN))
247             return 0;
248         iiv = geniv;
249     }
250
251     memset(crypto_info, 0, sizeof(*crypto_info));
252     switch (EVP_CIPHER_get_nid(c))
253     {
254 # ifdef OPENSSL_KTLS_AES_GCM_128
255     case NID_aes_128_gcm:
256         crypto_info->gcm128.info.cipher_type = TLS_CIPHER_AES_GCM_128;
257         crypto_info->gcm128.info.version = s->version;
258         crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm128);
259         memcpy(crypto_info->gcm128.iv, iiv + EVP_GCM_TLS_FIXED_IV_LEN,
260                TLS_CIPHER_AES_GCM_128_IV_SIZE);
261         memcpy(crypto_info->gcm128.salt, iiv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
262         memcpy(crypto_info->gcm128.key, key, EVP_CIPHER_get_key_length(c));
263         memcpy(crypto_info->gcm128.rec_seq, rl_sequence,
264                TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
265         if (!is_tx && !check_rx_read_ahead(s, crypto_info->gcm128.rec_seq))
266             return 0;
267         return 1;
268 # endif
269 # ifdef OPENSSL_KTLS_AES_GCM_256
270     case NID_aes_256_gcm:
271         crypto_info->gcm256.info.cipher_type = TLS_CIPHER_AES_GCM_256;
272         crypto_info->gcm256.info.version = s->version;
273         crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm256);
274         memcpy(crypto_info->gcm256.iv, iiv + EVP_GCM_TLS_FIXED_IV_LEN,
275                TLS_CIPHER_AES_GCM_256_IV_SIZE);
276         memcpy(crypto_info->gcm256.salt, iiv, TLS_CIPHER_AES_GCM_256_SALT_SIZE);
277         memcpy(crypto_info->gcm256.key, key, EVP_CIPHER_get_key_length(c));
278         memcpy(crypto_info->gcm256.rec_seq, rl_sequence,
279                TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
280         if (!is_tx && !check_rx_read_ahead(s, crypto_info->gcm256.rec_seq))
281             return 0;
282         return 1;
283 # endif
284 # ifdef OPENSSL_KTLS_AES_CCM_128
285     case NID_aes_128_ccm:
286         crypto_info->ccm128.info.cipher_type = TLS_CIPHER_AES_CCM_128;
287         crypto_info->ccm128.info.version = s->version;
288         crypto_info->tls_crypto_info_len = sizeof(crypto_info->ccm128);
289         memcpy(crypto_info->ccm128.iv, iiv + EVP_CCM_TLS_FIXED_IV_LEN,
290                TLS_CIPHER_AES_CCM_128_IV_SIZE);
291         memcpy(crypto_info->ccm128.salt, iiv, TLS_CIPHER_AES_CCM_128_SALT_SIZE);
292         memcpy(crypto_info->ccm128.key, key, EVP_CIPHER_get_key_length(c));
293         memcpy(crypto_info->ccm128.rec_seq, rl_sequence,
294                TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
295         if (!is_tx && !check_rx_read_ahead(s, crypto_info->ccm128.rec_seq))
296             return 0;
297         return 1;
298 # endif
299 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
300     case NID_chacha20_poly1305:
301         crypto_info->chacha20poly1305.info.cipher_type = TLS_CIPHER_CHACHA20_POLY1305;
302         crypto_info->chacha20poly1305.info.version = s->version;
303         crypto_info->tls_crypto_info_len = sizeof(crypto_info->chacha20poly1305);
304         memcpy(crypto_info->chacha20poly1305.iv, iiv,
305                TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
306         memcpy(crypto_info->chacha20poly1305.key, key,
307                EVP_CIPHER_get_key_length(c));
308         memcpy(crypto_info->chacha20poly1305.rec_seq, rl_sequence,
309                TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
310         if (!is_tx
311                 && !check_rx_read_ahead(s,
312                                         crypto_info->chacha20poly1305.rec_seq))
313             return 0;
314         return 1;
315 # endif
316     default:
317         return 0;
318     }
319
320 }
321
322 #endif /* OPENSSL_SYS_LINUX */