doc: EVP_PKEY_get_utf8/octet_string_param() clarify NULL buffer behavior
[openssl.git] / ssl / ktls.c
1 /*
2  * Copyright 2018-2021 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 #if defined(__FreeBSD__)
14 # include "crypto/cryptodev.h"
15
16 /*-
17  * Check if a given cipher is supported by the KTLS interface.
18  * The kernel might still fail the setsockopt() if no suitable
19  * provider is found, but this checks if the socket option
20  * supports the cipher suite used at all.
21  */
22 int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c,
23                                 const EVP_CIPHER_CTX *dd)
24 {
25
26     switch (s->version) {
27     case TLS1_VERSION:
28     case TLS1_1_VERSION:
29     case TLS1_2_VERSION:
30     case TLS1_3_VERSION:
31         break;
32     default:
33         return 0;
34     }
35
36     switch (s->s3.tmp.new_cipher->algorithm_enc) {
37     case SSL_AES128GCM:
38     case SSL_AES256GCM:
39         return 1;
40     case SSL_AES128:
41     case SSL_AES256:
42         if (s->ext.use_etm)
43             return 0;
44         switch (s->s3.tmp.new_cipher->algorithm_mac) {
45         case SSL_SHA1:
46         case SSL_SHA256:
47         case SSL_SHA384:
48             return 1;
49         default:
50             return 0;
51         }
52     default:
53         return 0;
54     }
55 }
56
57 /* Function to configure kernel TLS structure */
58 int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd,
59                           void *rl_sequence, ktls_crypto_info_t *crypto_info,
60                           unsigned char **rec_seq, unsigned char *iv,
61                           unsigned char *key, unsigned char *mac_key,
62                           size_t mac_secret_size)
63 {
64     memset(crypto_info, 0, sizeof(*crypto_info));
65     switch (s->s3.tmp.new_cipher->algorithm_enc) {
66     case SSL_AES128GCM:
67     case SSL_AES256GCM:
68         crypto_info->cipher_algorithm = CRYPTO_AES_NIST_GCM_16;
69         if (s->version == TLS1_3_VERSION)
70             crypto_info->iv_len = EVP_CIPHER_CTX_get_iv_length(dd);
71         else
72             crypto_info->iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
73         break;
74     case SSL_AES128:
75     case SSL_AES256:
76         switch (s->s3.tmp.new_cipher->algorithm_mac) {
77         case SSL_SHA1:
78             crypto_info->auth_algorithm = CRYPTO_SHA1_HMAC;
79             break;
80         case SSL_SHA256:
81             crypto_info->auth_algorithm = CRYPTO_SHA2_256_HMAC;
82             break;
83         case SSL_SHA384:
84             crypto_info->auth_algorithm = CRYPTO_SHA2_384_HMAC;
85             break;
86         default:
87             return 0;
88         }
89         crypto_info->cipher_algorithm = CRYPTO_AES_CBC;
90         crypto_info->iv_len = EVP_CIPHER_get_iv_length(c);
91         crypto_info->auth_key = mac_key;
92         crypto_info->auth_key_len = mac_secret_size;
93         break;
94     default:
95         return 0;
96     }
97     crypto_info->cipher_key = key;
98     crypto_info->cipher_key_len = EVP_CIPHER_get_key_length(c);
99     crypto_info->iv = iv;
100     crypto_info->tls_vmajor = (s->version >> 8) & 0x000000ff;
101     crypto_info->tls_vminor = (s->version & 0x000000ff);
102 # ifdef TCP_RXTLS_ENABLE
103     memcpy(crypto_info->rec_seq, rl_sequence, sizeof(crypto_info->rec_seq));
104     if (rec_seq != NULL)
105         *rec_seq = crypto_info->rec_seq;
106 # else
107     if (rec_seq != NULL)
108         *rec_seq = NULL;
109 # endif
110     return 1;
111 };
112
113 #endif                         /* __FreeBSD__ */
114
115 #if defined(OPENSSL_SYS_LINUX)
116
117 /* Function to check supported ciphers in Linux */
118 int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c,
119                                 const EVP_CIPHER_CTX *dd)
120 {
121     switch (s->version) {
122     case TLS1_2_VERSION:
123     case TLS1_3_VERSION:
124         break;
125     default:
126         return 0;
127     }
128
129     /* check that cipher is AES_GCM_128, AES_GCM_256, AES_CCM_128 
130      * or Chacha20-Poly1305
131      */
132     switch (EVP_CIPHER_get_nid(c))
133     {
134 # ifdef OPENSSL_KTLS_AES_CCM_128
135     case NID_aes_128_ccm:
136         if (s->version == TLS_1_3_VERSION /* broken on 5.x kernels */
137             || EVP_CIPHER_CTX_get_tag_length(dd) != EVP_CCM_TLS_TAG_LEN)
138           return 0;
139 # endif
140 # ifdef OPENSSL_KTLS_AES_GCM_128
141         /* Fall through */
142     case NID_aes_128_gcm:
143 # endif
144 # ifdef OPENSSL_KTLS_AES_GCM_256
145     case NID_aes_256_gcm:
146 # endif
147 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
148     case NID_chacha20_poly1305:
149 # endif
150         return 1;
151     default:
152         return 0;
153     }
154 }
155
156 /* Function to configure kernel TLS structure */
157 int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd,
158                           void *rl_sequence, ktls_crypto_info_t *crypto_info,
159                           unsigned char **rec_seq, unsigned char *iv,
160                           unsigned char *key, unsigned char *mac_key,
161                           size_t mac_secret_size)
162 {
163     unsigned char geniv[12];
164     unsigned char *iiv = iv;
165
166     if (s->version == TLS1_2_VERSION &&
167         EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE) {
168         if (!EVP_CIPHER_CTX_get_updated_iv(dd, geniv,
169                                            EVP_GCM_TLS_FIXED_IV_LEN
170                                            + EVP_GCM_TLS_EXPLICIT_IV_LEN))
171             return 0;
172         iiv = geniv;
173     }
174
175     memset(crypto_info, 0, sizeof(*crypto_info));
176     switch (EVP_CIPHER_get_nid(c))
177     {
178 # ifdef OPENSSL_KTLS_AES_GCM_128
179     case NID_aes_128_gcm:
180         crypto_info->gcm128.info.cipher_type = TLS_CIPHER_AES_GCM_128;
181         crypto_info->gcm128.info.version = s->version;
182         crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm128);
183         memcpy(crypto_info->gcm128.iv, iiv + EVP_GCM_TLS_FIXED_IV_LEN,
184                TLS_CIPHER_AES_GCM_128_IV_SIZE);
185         memcpy(crypto_info->gcm128.salt, iiv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
186         memcpy(crypto_info->gcm128.key, key, EVP_CIPHER_get_key_length(c));
187         memcpy(crypto_info->gcm128.rec_seq, rl_sequence,
188                TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
189         if (rec_seq != NULL)
190             *rec_seq = crypto_info->gcm128.rec_seq;
191         return 1;
192 # endif
193 # ifdef OPENSSL_KTLS_AES_GCM_256
194     case NID_aes_256_gcm:
195         crypto_info->gcm256.info.cipher_type = TLS_CIPHER_AES_GCM_256;
196         crypto_info->gcm256.info.version = s->version;
197         crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm256);
198         memcpy(crypto_info->gcm256.iv, iiv + EVP_GCM_TLS_FIXED_IV_LEN,
199                TLS_CIPHER_AES_GCM_256_IV_SIZE);
200         memcpy(crypto_info->gcm256.salt, iiv, TLS_CIPHER_AES_GCM_256_SALT_SIZE);
201         memcpy(crypto_info->gcm256.key, key, EVP_CIPHER_get_key_length(c));
202         memcpy(crypto_info->gcm256.rec_seq, rl_sequence,
203                TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
204         if (rec_seq != NULL)
205             *rec_seq = crypto_info->gcm256.rec_seq;
206         return 1;
207 # endif
208 # ifdef OPENSSL_KTLS_AES_CCM_128
209     case NID_aes_128_ccm:
210         crypto_info->ccm128.info.cipher_type = TLS_CIPHER_AES_CCM_128;
211         crypto_info->ccm128.info.version = s->version;
212         crypto_info->tls_crypto_info_len = sizeof(crypto_info->ccm128);
213         memcpy(crypto_info->ccm128.iv, iiv + EVP_CCM_TLS_FIXED_IV_LEN,
214                TLS_CIPHER_AES_CCM_128_IV_SIZE);
215         memcpy(crypto_info->ccm128.salt, iiv, TLS_CIPHER_AES_CCM_128_SALT_SIZE);
216         memcpy(crypto_info->ccm128.key, key, EVP_CIPHER_get_key_length(c));
217         memcpy(crypto_info->ccm128.rec_seq, rl_sequence,
218                TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
219         if (rec_seq != NULL)
220             *rec_seq = crypto_info->ccm128.rec_seq;
221         return 1;
222 # endif
223 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
224     case NID_chacha20_poly1305:
225         crypto_info->chacha20poly1305.info.cipher_type = TLS_CIPHER_CHACHA20_POLY1305;
226         crypto_info->chacha20poly1305.info.version = s->version;
227         crypto_info->tls_crypto_info_len = sizeof(crypto_info->chacha20poly1305);
228         memcpy(crypto_info->chacha20poly1305.iv, iiv,
229                TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
230         memcpy(crypto_info->chacha20poly1305.key, key,
231                EVP_CIPHER_get_key_length(c));
232         memcpy(crypto_info->chacha20poly1305.rec_seq, rl_sequence,
233                TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
234         if (rec_seq != NULL)
235             *rec_seq = crypto_info->chacha20poly1305.rec_seq;
236         return 1;
237 # endif
238     default:
239         return 0;
240     }
241
242 }
243
244 #endif /* OPENSSL_SYS_LINUX */