]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/t1_enc.c
Transfer the functionality from ssl3_read_n to the new record layer
[thirdparty/openssl.git] / ssl / t1_enc.c
CommitLineData
846e33c7 1/*
fecb3aae 2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
c80149d9 3 * Copyright 2005 Nokia. All rights reserved.
82b0bf0b 4 *
2c18d164 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
82b0bf0b 9 */
846e33c7 10
58964a49 11#include <stdio.h>
706457b7
DMSP
12#include "ssl_local.h"
13#include "record/record_local.h"
50ec7505
BP
14#include "internal/ktls.h"
15#include "internal/cryptlib.h"
3c27208f 16#include <openssl/comp.h>
ec577822 17#include <openssl/evp.h>
b7d60e76 18#include <openssl/kdf.h>
637f374a 19#include <openssl/rand.h>
50ec7505 20#include <openssl/obj_mac.h>
ce3b1bb4 21#include <openssl/core_names.h>
49b26f54 22#include <openssl/trace.h>
58964a49 23
b7d60e76 24/* seed1 through seed5 are concatenated */
38b051a1 25static int tls1_PRF(SSL_CONNECTION *s,
6db6bc5a
MC
26 const void *seed1, size_t seed1_len,
27 const void *seed2, size_t seed2_len,
28 const void *seed3, size_t seed3_len,
29 const void *seed4, size_t seed4_len,
30 const void *seed5, size_t seed5_len,
31 const unsigned char *sec, size_t slen,
d4d2f3a4 32 unsigned char *out, size_t olen, int fatal)
0f113f3e 33{
28ba2541 34 const EVP_MD *md = ssl_prf_md(s);
ce3b1bb4 35 EVP_KDF *kdf;
32495464 36 EVP_KDF_CTX *kctx = NULL;
ce3b1bb4 37 OSSL_PARAM params[8], *p = params;
7e56c626 38 const char *mdname;
0f113f3e 39
28ba2541 40 if (md == NULL) {
668f6f08 41 /* Should never happen */
d4d2f3a4 42 if (fatal)
c48ffbcc 43 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
d4d2f3a4 44 else
6849b73c 45 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
28ba2541 46 return 0;
668f6f08 47 }
38b051a1
TM
48 kdf = EVP_KDF_fetch(SSL_CONNECTION_GET_CTX(s)->libctx,
49 OSSL_KDF_NAME_TLS1_PRF,
50 SSL_CONNECTION_GET_CTX(s)->propq);
ce3b1bb4
P
51 if (kdf == NULL)
52 goto err;
660c5344 53 kctx = EVP_KDF_CTX_new(kdf);
ce3b1bb4
P
54 EVP_KDF_free(kdf);
55 if (kctx == NULL)
b7d60e76 56 goto err;
ed576acd 57 mdname = EVP_MD_get0_name(md);
ce3b1bb4 58 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
8b6ffd40 59 (char *)mdname, 0);
ce3b1bb4
P
60 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,
61 (unsigned char *)sec,
62 (size_t)slen);
63 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
64 (void *)seed1, (size_t)seed1_len);
65 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
66 (void *)seed2, (size_t)seed2_len);
67 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
68 (void *)seed3, (size_t)seed3_len);
69 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
70 (void *)seed4, (size_t)seed4_len);
71 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
72 (void *)seed5, (size_t)seed5_len);
73 *p = OSSL_PARAM_construct_end();
5cceedb5 74 if (EVP_KDF_derive(kctx, out, olen, params)) {
660c5344 75 EVP_KDF_CTX_free(kctx);
ce3b1bb4 76 return 1;
d4d2f3a4 77 }
b7d60e76 78
a230b26e 79 err:
ce3b1bb4 80 if (fatal)
c48ffbcc 81 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
ce3b1bb4 82 else
6849b73c 83 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
660c5344 84 EVP_KDF_CTX_free(kctx);
ce3b1bb4 85 return 0;
81025661 86}
0f113f3e 87
38b051a1
TM
88static int tls1_generate_key_block(SSL_CONNECTION *s, unsigned char *km,
89 size_t num)
0f113f3e
MC
90{
91 int ret;
d4d2f3a4
MC
92
93 /* Calls SSLfatal() as required */
28ba2541 94 ret = tls1_PRF(s,
0f113f3e 95 TLS_MD_KEY_EXPANSION_CONST,
555cbb32
TS
96 TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3.server_random,
97 SSL3_RANDOM_SIZE, s->s3.client_random, SSL3_RANDOM_SIZE,
0f113f3e 98 NULL, 0, NULL, 0, s->session->master_key,
d4d2f3a4 99 s->session->master_key_length, km, num, 1);
55a9a16f 100
0f113f3e
MC
101 return ret;
102}
58964a49 103
38b051a1 104int tls_provider_set_tls_params(SSL_CONNECTION *s, EVP_CIPHER_CTX *ctx,
b5588178
MC
105 const EVP_CIPHER *ciph,
106 const EVP_MD *md)
107{
108 /*
109 * Provided cipher, the TLS padding/MAC removal is performed provider
110 * side so we need to tell the ctx about our TLS version and mac size
111 */
112 OSSL_PARAM params[3], *pprm = params;
113 size_t macsize = 0;
114 int imacsize = -1;
115
ed576acd 116 if ((EVP_CIPHER_get_flags(ciph) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0
b5588178
MC
117 /*
118 * We look at s->ext.use_etm instead of SSL_READ_ETM() or
119 * SSL_WRITE_ETM() because this test applies to both reading
120 * and writing.
121 */
122 && !s->ext.use_etm)
ed576acd 123 imacsize = EVP_MD_get_size(md);
b5588178
MC
124 if (imacsize >= 0)
125 macsize = (size_t)imacsize;
126
127 *pprm++ = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
128 &s->version);
129 *pprm++ = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE,
130 &macsize);
131 *pprm = OSSL_PARAM_construct_end();
132
133 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
c48ffbcc 134 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
b5588178
MC
135 return 0;
136 }
137
138 return 1;
139}
140
62f27ab9
MM
141
142static int tls_iv_length_within_key_block(const EVP_CIPHER *c)
143{
144 /* If GCM/CCM mode only part of IV comes from PRF */
ed576acd 145 if (EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE)
62f27ab9 146 return EVP_GCM_TLS_FIXED_IV_LEN;
ed576acd 147 else if (EVP_CIPHER_get_mode(c) == EVP_CIPH_CCM_MODE)
62f27ab9
MM
148 return EVP_CCM_TLS_FIXED_IV_LEN;
149 else
ed576acd 150 return EVP_CIPHER_get_iv_length(c);
62f27ab9
MM
151}
152
38b051a1 153int tls1_change_cipher_state(SSL_CONNECTION *s, int which)
0f113f3e 154{
0f113f3e 155 unsigned char *p, *mac_secret;
0f113f3e 156 unsigned char *ms, *key, *iv;
0f113f3e
MC
157 EVP_CIPHER_CTX *dd;
158 const EVP_CIPHER *c;
09b6c2ef 159#ifndef OPENSSL_NO_COMP
0f113f3e 160 const SSL_COMP *comp;
09b6c2ef 161#endif
0f113f3e
MC
162 const EVP_MD *m;
163 int mac_type;
b43d1cbb 164 size_t *mac_secret_size;
0f113f3e
MC
165 EVP_MD_CTX *mac_ctx;
166 EVP_PKEY *mac_key;
b43d1cbb 167 size_t n, i, j, k, cl;
0f113f3e 168 int reuse_dd = 0;
50ec7505 169#ifndef OPENSSL_NO_KTLS
c34ca13a 170 ktls_crypto_info_t crypto_info;
4ffccf6c 171 void *rl_sequence;
2111f5c2 172 BIO *bio;
50ec7505 173#endif
0f113f3e 174
e2d5742b
MC
175#if 0
176 if ((which & SSL3_CC_READ) != 0) {
177 /*
178 * TODO(RECLAYER): This is probably not the right place to free this. The
179 * OSSL_RECORD_METHOD might have changed since we created the rl object.
180 * We should free it at the point that we update the OSSL_RECORD_METHOD.
181 * But for now this will do.
182 */
183 s->rrlmethod->free(s->rrl);
184 s->rrl = s->rrlmethod->new_record_layer(s->version, s->server,
185 OSSL_RECORD_DIRECTION_READ,
186 0, 0, 0, NULL, s->rbio, NULL,
187 NULL, NULL, NULL);
188 }
189#endif
190
191 if (s->rrl == NULL) {
192 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
193 goto err;
194 }
195
555cbb32
TS
196 c = s->s3.tmp.new_sym_enc;
197 m = s->s3.tmp.new_hash;
198 mac_type = s->s3.tmp.new_mac_pkey_type;
09b6c2ef 199#ifndef OPENSSL_NO_COMP
555cbb32 200 comp = s->s3.tmp.new_compression;
09b6c2ef 201#endif
58964a49 202
0f113f3e 203 if (which & SSL3_CC_READ) {
28a31a0a 204 if (s->ext.use_etm)
555cbb32 205 s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
28a31a0a 206 else
555cbb32 207 s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
28a31a0a 208
555cbb32 209 if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
0f113f3e
MC
210 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
211 else
212 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
213
5a5530a2
DB
214 if (s->s3.tmp.new_cipher->algorithm2 & TLS1_TLSTREE)
215 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_TLSTREE;
216 else
217 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_TLSTREE;
218
f63a17d6 219 if (s->enc_read_ctx != NULL) {
0f113f3e 220 reuse_dd = 1;
f63a17d6 221 } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) {
c48ffbcc 222 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
0f113f3e 223 goto err;
f63a17d6 224 } else {
0f113f3e 225 /*
f430ba31 226 * make sure it's initialised in case we exit later with an error
0f113f3e 227 */
846ec07d 228 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
f63a17d6 229 }
0f113f3e
MC
230 dd = s->enc_read_ctx;
231 mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
157af9be 232 if (mac_ctx == NULL) {
c48ffbcc 233 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
5f3d93e4 234 goto err;
157af9be 235 }
09b6c2ef 236#ifndef OPENSSL_NO_COMP
efa7dd64
RS
237 COMP_CTX_free(s->expand);
238 s->expand = NULL;
0f113f3e
MC
239 if (comp != NULL) {
240 s->expand = COMP_CTX_new(comp->method);
241 if (s->expand == NULL) {
f63a17d6 242 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
f63a17d6
MC
243 SSL_R_COMPRESSION_LIBRARY_ERROR);
244 goto err;
0f113f3e 245 }
0f113f3e 246 }
09b6c2ef 247#endif
0f113f3e 248 /*
d5d0a1cb 249 * this is done by dtls1_reset_seq_numbers for DTLS
0f113f3e 250 */
38b051a1 251 if (!SSL_CONNECTION_IS_DTLS(s))
de07f311 252 RECORD_LAYER_reset_read_sequence(&s->rlayer);
555cbb32
TS
253 mac_secret = &(s->s3.read_mac_secret[0]);
254 mac_secret_size = &(s->s3.read_mac_secret_size);
0f113f3e 255 } else {
7426cd34 256 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
28a31a0a 257 if (s->ext.use_etm)
555cbb32 258 s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
28a31a0a 259 else
555cbb32 260 s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
28a31a0a 261
555cbb32 262 if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
0f113f3e
MC
263 s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
264 else
265 s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
5a5530a2
DB
266
267 if (s->s3.tmp.new_cipher->algorithm2 & TLS1_TLSTREE)
268 s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_TLSTREE;
269 else
270 s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_TLSTREE;
38b051a1 271 if (s->enc_write_ctx != NULL && !SSL_CONNECTION_IS_DTLS(s)) {
0f113f3e 272 reuse_dd = 1;
f63a17d6 273 } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) {
c48ffbcc 274 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
0f113f3e 275 goto err;
f63a17d6 276 }
0f113f3e 277 dd = s->enc_write_ctx;
38b051a1 278 if (SSL_CONNECTION_IS_DTLS(s)) {
bfb0641f 279 mac_ctx = EVP_MD_CTX_new();
f63a17d6 280 if (mac_ctx == NULL) {
c48ffbcc 281 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
0f113f3e 282 goto err;
f63a17d6 283 }
0f113f3e 284 s->write_hash = mac_ctx;
5f3d93e4 285 } else {
0f113f3e 286 mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
f63a17d6 287 if (mac_ctx == NULL) {
c48ffbcc 288 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
5f3d93e4 289 goto err;
f63a17d6 290 }
5f3d93e4 291 }
09b6c2ef 292#ifndef OPENSSL_NO_COMP
efa7dd64
RS
293 COMP_CTX_free(s->compress);
294 s->compress = NULL;
0f113f3e
MC
295 if (comp != NULL) {
296 s->compress = COMP_CTX_new(comp->method);
297 if (s->compress == NULL) {
f63a17d6 298 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
c48ffbcc 299 SSL_R_COMPRESSION_LIBRARY_ERROR);
f63a17d6 300 goto err;
0f113f3e
MC
301 }
302 }
09b6c2ef 303#endif
0f113f3e 304 /*
d5d0a1cb 305 * this is done by dtls1_reset_seq_numbers for DTLS
0f113f3e 306 */
38b051a1 307 if (!SSL_CONNECTION_IS_DTLS(s))
de07f311 308 RECORD_LAYER_reset_write_sequence(&s->rlayer);
555cbb32
TS
309 mac_secret = &(s->s3.write_mac_secret[0]);
310 mac_secret_size = &(s->s3.write_mac_secret_size);
0f113f3e
MC
311 }
312
313 if (reuse_dd)
846ec07d 314 EVP_CIPHER_CTX_reset(dd);
0f113f3e 315
555cbb32
TS
316 p = s->s3.tmp.key_block;
317 i = *mac_secret_size = s->s3.tmp.new_mac_secret_size;
0f113f3e 318
ed576acd 319 cl = EVP_CIPHER_get_key_length(c);
361a1191 320 j = cl;
62f27ab9 321 k = tls_iv_length_within_key_block(c);
0f113f3e
MC
322 if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
323 (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
324 ms = &(p[0]);
325 n = i + i;
326 key = &(p[n]);
327 n += j + j;
328 iv = &(p[n]);
329 n += k + k;
0f113f3e
MC
330 } else {
331 n = i;
332 ms = &(p[n]);
333 n += i + j;
334 key = &(p[n]);
335 n += j + k;
336 iv = &(p[n]);
337 n += k;
0f113f3e
MC
338 }
339
555cbb32 340 if (n > s->s3.tmp.key_block_length) {
c48ffbcc 341 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 342 goto err;
0f113f3e
MC
343 }
344
345 memcpy(mac_secret, ms, i);
346
ed576acd 347 if (!(EVP_CIPHER_get_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
38b051a1
TM
348 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
349
6f0bd6ca 350 if (mac_type == EVP_PKEY_HMAC) {
38b051a1
TM
351 mac_key = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
352 sctx->propq, mac_secret,
d8652be0 353 *mac_secret_size);
6f0bd6ca
MC
354 } else {
355 /*
356 * If its not HMAC then the only other types of MAC we support are
357 * the GOST MACs, so we need to use the old style way of creating
358 * a MAC key.
359 */
360 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, mac_secret,
361 (int)*mac_secret_size);
362 }
5f3d93e4 363 if (mac_key == NULL
ed576acd 364 || EVP_DigestSignInit_ex(mac_ctx, NULL, EVP_MD_get0_name(m),
38b051a1 365 sctx->libctx, sctx->propq, mac_key,
d38b6ae9 366 NULL) <= 0) {
5f3d93e4 367 EVP_PKEY_free(mac_key);
c48ffbcc 368 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 369 goto err;
5f3d93e4 370 }
0f113f3e
MC
371 EVP_PKEY_free(mac_key);
372 }
49b26f54
RL
373
374 OSSL_TRACE_BEGIN(TLS) {
375 BIO_printf(trc_out, "which = %04X, mac key:\n", which);
376 BIO_dump_indent(trc_out, ms, i, 4);
377 } OSSL_TRACE_END(TLS);
0f113f3e 378
ed576acd 379 if (EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE) {
eadf70d2 380 if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
d649c51a
PH
381 || EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,
382 iv) <= 0) {
c48ffbcc 383 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 384 goto err;
eadf70d2 385 }
ed576acd 386 } else if (EVP_CIPHER_get_mode(c) == EVP_CIPH_CCM_MODE) {
3d3701ea 387 int taglen;
555cbb32 388 if (s->s3.tmp.
a230b26e 389 new_cipher->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
ec07b1d8 390 taglen = EVP_CCM8_TLS_TAG_LEN;
3d3701ea 391 else
ec07b1d8 392 taglen = EVP_CCM_TLS_TAG_LEN;
e75c5a79 393 if (!EVP_CipherInit_ex(dd, c, NULL, NULL, NULL, (which & SSL3_CC_WRITE))
d649c51a
PH
394 || (EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL) <= 0)
395 || (EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_TAG, taglen, NULL) <= 0)
396 || (EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_CCM_SET_IV_FIXED, (int)k, iv) <= 0)
e75c5a79 397 || !EVP_CipherInit_ex(dd, NULL, NULL, key, NULL, -1)) {
c48ffbcc 398 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 399 goto err;
e75c5a79 400 }
eadf70d2
MC
401 } else {
402 if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
c48ffbcc 403 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 404 goto err;
eadf70d2
MC
405 }
406 }
0f113f3e 407 /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
ed576acd
TM
408 if ((EVP_CIPHER_get_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)
409 && *mac_secret_size
d649c51a
PH
410 && EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
411 (int)*mac_secret_size, mac_secret) <= 0) {
c48ffbcc 412 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 413 goto err;
eadf70d2 414 }
ed576acd 415 if (EVP_CIPHER_get0_provider(c) != NULL
b5588178
MC
416 && !tls_provider_set_tls_params(s, dd, c, m)) {
417 /* SSLfatal already called */
418 goto err;
524cb684 419 }
b5588178 420
50ec7505 421#ifndef OPENSSL_NO_KTLS
a3a54179 422 if (s->compress || (s->options & SSL_OP_ENABLE_KTLS) == 0)
50ec7505
BP
423 goto skip_ktls;
424
425 /* ktls supports only the maximum fragment size */
426 if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
427 goto skip_ktls;
428
4ffccf6c 429 /* check that cipher is supported */
3e582606 430 if (!ktls_check_supported_cipher(s, c, dd))
50ec7505
BP
431 goto skip_ktls;
432
c35e921f
BP
433 if (which & SSL3_CC_WRITE)
434 bio = s->wbio;
435 else
436 bio = s->rbio;
437
438 if (!ossl_assert(bio != NULL)) {
c48ffbcc 439 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
50ec7505
BP
440 goto err;
441 }
442
443 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
c35e921f
BP
444 if (which & SSL3_CC_WRITE) {
445 if (BIO_flush(bio) <= 0)
446 goto skip_ktls;
447 }
50ec7505
BP
448
449 /* ktls doesn't support renegotiation */
c35e921f
BP
450 if ((BIO_get_ktls_send(s->wbio) && (which & SSL3_CC_WRITE)) ||
451 (BIO_get_ktls_recv(s->rbio) && (which & SSL3_CC_READ))) {
c48ffbcc 452 SSLfatal(s, SSL_AD_NO_RENEGOTIATION, ERR_R_INTERNAL_ERROR);
50ec7505
BP
453 goto err;
454 }
455
c35e921f 456 if (which & SSL3_CC_WRITE)
4ffccf6c 457 rl_sequence = RECORD_LAYER_get_write_sequence(&s->rlayer);
c35e921f 458 else
4ffccf6c
VF
459 rl_sequence = RECORD_LAYER_get_read_sequence(&s->rlayer);
460
85773128
JB
461 if (!ktls_configure_crypto(s, c, dd, rl_sequence, &crypto_info,
462 which & SSL3_CC_WRITE, iv, key, ms,
463 *mac_secret_size))
4ffccf6c 464 goto skip_ktls;
c35e921f 465
50ec7505 466 /* ktls works with user provided buffers directly */
c35e921f
BP
467 if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) {
468 if (which & SSL3_CC_WRITE)
469 ssl3_release_write_buffer(s);
38b051a1 470 SSL_set_options(SSL_CONNECTION_GET_SSL(s), SSL_OP_NO_RENEGOTIATION);
50ec7505
BP
471 }
472
473 skip_ktls:
474#endif /* OPENSSL_NO_KTLS */
7426cd34 475 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
1cf218bc 476
49b26f54
RL
477 OSSL_TRACE_BEGIN(TLS) {
478 BIO_printf(trc_out, "which = %04X, key:\n", which);
ed576acd 479 BIO_dump_indent(trc_out, key, EVP_CIPHER_get_key_length(c), 4);
49b26f54
RL
480 BIO_printf(trc_out, "iv:\n");
481 BIO_dump_indent(trc_out, iv, k, 4);
482 } OSSL_TRACE_END(TLS);
58964a49 483
208fb891 484 return 1;
0f113f3e 485 err:
26a7d938 486 return 0;
0f113f3e 487}
58964a49 488
38b051a1 489int tls1_setup_key_block(SSL_CONNECTION *s)
0f113f3e 490{
b7d60e76 491 unsigned char *p;
0f113f3e
MC
492 const EVP_CIPHER *c;
493 const EVP_MD *hash;
0f113f3e 494 SSL_COMP *comp;
8c1a5343
MC
495 int mac_type = NID_undef;
496 size_t num, mac_secret_size = 0;
0f113f3e 497 int ret = 0;
58964a49 498
555cbb32 499 if (s->s3.tmp.key_block_length != 0)
208fb891 500 return 1;
0f113f3e 501
38b051a1
TM
502 if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s), s->session, &c, &hash,
503 &mac_type, &mac_secret_size, &comp,
504 s->ext.use_etm)) {
5a2d0ef3
RL
505 /* Error is already recorded */
506 SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
26a7d938 507 return 0;
0f113f3e
MC
508 }
509
c8f6c28a 510 ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
555cbb32 511 s->s3.tmp.new_sym_enc = c;
c8f6c28a 512 ssl_evp_md_free(s->s3.tmp.new_hash);
555cbb32
TS
513 s->s3.tmp.new_hash = hash;
514 s->s3.tmp.new_mac_pkey_type = mac_type;
515 s->s3.tmp.new_mac_secret_size = mac_secret_size;
ed576acd
TM
516 num = mac_secret_size + EVP_CIPHER_get_key_length(c)
517 + tls_iv_length_within_key_block(c);
0f113f3e
MC
518 num *= 2;
519
520 ssl3_cleanup_key_block(s);
521
b7d60e76 522 if ((p = OPENSSL_malloc(num)) == NULL) {
c48ffbcc 523 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
524 goto err;
525 }
526
555cbb32
TS
527 s->s3.tmp.key_block_length = num;
528 s->s3.tmp.key_block = p;
0f113f3e 529
49b26f54 530 OSSL_TRACE_BEGIN(TLS) {
234261f3 531 BIO_printf(trc_out, "key block length: %zu\n", num);
49b26f54 532 BIO_printf(trc_out, "client random\n");
555cbb32 533 BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
49b26f54 534 BIO_printf(trc_out, "server random\n");
555cbb32 535 BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
49b26f54
RL
536 BIO_printf(trc_out, "master key\n");
537 BIO_dump_indent(trc_out,
538 s->session->master_key,
539 s->session->master_key_length, 4);
540 } OSSL_TRACE_END(TLS);
541
d4d2f3a4
MC
542 if (!tls1_generate_key_block(s, p, num)) {
543 /* SSLfatal() already called */
0f113f3e 544 goto err;
d4d2f3a4 545 }
49b26f54
RL
546
547 OSSL_TRACE_BEGIN(TLS) {
548 BIO_printf(trc_out, "key block\n");
549 BIO_dump_indent(trc_out, p, num, 4);
550 } OSSL_TRACE_END(TLS);
58964a49 551
0f113f3e 552 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
38b051a1 553 && SSL_CONNECTION_GET_SSL(s)->method->version <= TLS1_VERSION) {
0f113f3e
MC
554 /*
555 * enable vulnerability countermeasure for CBC ciphers with known-IV
556 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
557 */
555cbb32 558 s->s3.need_empty_fragments = 1;
0f113f3e
MC
559
560 if (s->session->cipher != NULL) {
561 if (s->session->cipher->algorithm_enc == SSL_eNULL)
555cbb32 562 s->s3.need_empty_fragments = 0;
0f113f3e 563
0f113f3e 564 if (s->session->cipher->algorithm_enc == SSL_RC4)
555cbb32 565 s->s3.need_empty_fragments = 0;
0f113f3e
MC
566 }
567 }
568
569 ret = 1;
570 err:
26a7d938 571 return ret;
0f113f3e 572}
58964a49 573
38b051a1
TM
574size_t tls1_final_finish_mac(SSL_CONNECTION *s, const char *str,
575 size_t slen, unsigned char *out)
0f113f3e 576{
8c1a5343 577 size_t hashlen;
28ba2541 578 unsigned char hash[EVP_MAX_MD_SIZE];
5a5530a2
DB
579 size_t finished_size = TLS1_FINISH_MAC_LENGTH;
580
581 if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kGOST18)
582 finished_size = 32;
0f113f3e 583
d4d2f3a4
MC
584 if (!ssl3_digest_cached_records(s, 0)) {
585 /* SSLfatal() already called */
124037fd 586 return 0;
d4d2f3a4 587 }
0f113f3e 588
d4d2f3a4
MC
589 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
590 /* SSLfatal() already called */
48fbcbac 591 return 0;
d4d2f3a4 592 }
0f113f3e 593
b7d60e76 594 if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
0f113f3e 595 s->session->master_key, s->session->master_key_length,
5a5530a2 596 out, finished_size, 1)) {
d4d2f3a4 597 /* SSLfatal() already called */
0f113f3e 598 return 0;
d4d2f3a4 599 }
c9dd49a7 600 OPENSSL_cleanse(hash, hashlen);
5a5530a2 601 return finished_size;
0f113f3e 602}
58964a49 603
38b051a1
TM
604int tls1_generate_master_secret(SSL_CONNECTION *s, unsigned char *out,
605 unsigned char *p, size_t len,
606 size_t *secret_size)
0f113f3e 607{
329114f9 608 if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
0cfb0e75 609 unsigned char hash[EVP_MAX_MD_SIZE * 2];
8c1a5343 610 size_t hashlen;
a230b26e 611 /*
79c44b4e 612 * Digest cached records keeping record buffer (if present): this won't
a230b26e
EK
613 * affect client auth because we're freezing the buffer at the same
614 * point (after client key exchange and before certificate verify)
124037fd 615 */
f63a17d6
MC
616 if (!ssl3_digest_cached_records(s, 1)
617 || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
618 /* SSLfatal() already called */
8c1a5343 619 return 0;
f63a17d6 620 }
49b26f54
RL
621 OSSL_TRACE_BEGIN(TLS) {
622 BIO_printf(trc_out, "Handshake hashes:\n");
623 BIO_dump(trc_out, (char *)hash, hashlen);
624 } OSSL_TRACE_END(TLS);
d4d2f3a4
MC
625 if (!tls1_PRF(s,
626 TLS_MD_EXTENDED_MASTER_SECRET_CONST,
627 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
628 hash, hashlen,
629 NULL, 0,
630 NULL, 0,
631 NULL, 0, p, len, out,
632 SSL3_MASTER_SECRET_SIZE, 1)) {
633 /* SSLfatal() already called */
634 return 0;
635 }
0cfb0e75
DSH
636 OPENSSL_cleanse(hash, hashlen);
637 } else {
d4d2f3a4
MC
638 if (!tls1_PRF(s,
639 TLS_MD_MASTER_SECRET_CONST,
640 TLS_MD_MASTER_SECRET_CONST_SIZE,
555cbb32 641 s->s3.client_random, SSL3_RANDOM_SIZE,
d4d2f3a4 642 NULL, 0,
555cbb32 643 s->s3.server_random, SSL3_RANDOM_SIZE,
d4d2f3a4
MC
644 NULL, 0, p, len, out,
645 SSL3_MASTER_SECRET_SIZE, 1)) {
646 /* SSLfatal() already called */
647 return 0;
648 }
0cfb0e75 649 }
49b26f54
RL
650
651 OSSL_TRACE_BEGIN(TLS) {
652 BIO_printf(trc_out, "Premaster Secret:\n");
653 BIO_dump_indent(trc_out, p, len, 4);
654 BIO_printf(trc_out, "Client Random:\n");
555cbb32 655 BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
49b26f54 656 BIO_printf(trc_out, "Server Random:\n");
555cbb32 657 BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
49b26f54
RL
658 BIO_printf(trc_out, "Master Secret:\n");
659 BIO_dump_indent(trc_out,
660 s->session->master_key,
661 SSL3_MASTER_SECRET_SIZE, 4);
662 } OSSL_TRACE_END(TLS);
761772d7 663
8c1a5343
MC
664 *secret_size = SSL3_MASTER_SECRET_SIZE;
665 return 1;
0f113f3e 666}
58964a49 667
38b051a1
TM
668int tls1_export_keying_material(SSL_CONNECTION *s, unsigned char *out,
669 size_t olen, const char *label, size_t llen,
0f113f3e
MC
670 const unsigned char *context,
671 size_t contextlen, int use_context)
672{
0f113f3e 673 unsigned char *val = NULL;
1c8a527c 674 size_t vallen = 0, currentvalpos;
0f113f3e 675 int rv;
e0af0405 676
0f113f3e
MC
677 /*
678 * construct PRF arguments we construct the PRF argument ourself rather
679 * than passing separate values into the TLS PRF to ensure that the
680 * concatenation of values does not create a prohibited label.
681 */
682 vallen = llen + SSL3_RANDOM_SIZE * 2;
683 if (use_context) {
684 vallen += 2 + contextlen;
685 }
686
687 val = OPENSSL_malloc(vallen);
688 if (val == NULL)
689 goto err2;
690 currentvalpos = 0;
691 memcpy(val + currentvalpos, (unsigned char *)label, llen);
692 currentvalpos += llen;
555cbb32 693 memcpy(val + currentvalpos, s->s3.client_random, SSL3_RANDOM_SIZE);
0f113f3e 694 currentvalpos += SSL3_RANDOM_SIZE;
555cbb32 695 memcpy(val + currentvalpos, s->s3.server_random, SSL3_RANDOM_SIZE);
0f113f3e
MC
696 currentvalpos += SSL3_RANDOM_SIZE;
697
698 if (use_context) {
699 val[currentvalpos] = (contextlen >> 8) & 0xff;
700 currentvalpos++;
701 val[currentvalpos] = contextlen & 0xff;
702 currentvalpos++;
703 if ((contextlen > 0) || (context != NULL)) {
704 memcpy(val + currentvalpos, context, contextlen);
705 }
706 }
707
708 /*
709 * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
710 * label len) = 15, so size of val > max(prohibited label len) = 15 and
711 * the comparisons won't have buffer overflow
712 */
713 if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
714 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
715 goto err1;
716 if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
717 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
718 goto err1;
719 if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
720 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
721 goto err1;
0cfb0e75
DSH
722 if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
723 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
724 goto err1;
0f113f3e
MC
725 if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
726 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
727 goto err1;
728
28ba2541 729 rv = tls1_PRF(s,
0f113f3e
MC
730 val, vallen,
731 NULL, 0,
732 NULL, 0,
733 NULL, 0,
734 NULL, 0,
735 s->session->master_key, s->session->master_key_length,
d4d2f3a4 736 out, olen, 0);
e0af0405 737
0f113f3e
MC
738 goto ret;
739 err1:
6849b73c 740 ERR_raise(ERR_LIB_SSL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
0f113f3e
MC
741 rv = 0;
742 goto ret;
743 err2:
6849b73c 744 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
745 rv = 0;
746 ret:
05c7b163 747 OPENSSL_clear_free(val, vallen);
26a7d938 748 return rv;
0f113f3e 749}
e0af0405 750
6b691a5c 751int tls1_alert_code(int code)
0f113f3e
MC
752{
753 switch (code) {
754 case SSL_AD_CLOSE_NOTIFY:
26a7d938 755 return SSL3_AD_CLOSE_NOTIFY;
0f113f3e 756 case SSL_AD_UNEXPECTED_MESSAGE:
26a7d938 757 return SSL3_AD_UNEXPECTED_MESSAGE;
0f113f3e 758 case SSL_AD_BAD_RECORD_MAC:
26a7d938 759 return SSL3_AD_BAD_RECORD_MAC;
0f113f3e 760 case SSL_AD_DECRYPTION_FAILED:
26a7d938 761 return TLS1_AD_DECRYPTION_FAILED;
0f113f3e 762 case SSL_AD_RECORD_OVERFLOW:
26a7d938 763 return TLS1_AD_RECORD_OVERFLOW;
0f113f3e 764 case SSL_AD_DECOMPRESSION_FAILURE:
26a7d938 765 return SSL3_AD_DECOMPRESSION_FAILURE;
0f113f3e 766 case SSL_AD_HANDSHAKE_FAILURE:
26a7d938 767 return SSL3_AD_HANDSHAKE_FAILURE;
0f113f3e 768 case SSL_AD_NO_CERTIFICATE:
26a7d938 769 return -1;
0f113f3e 770 case SSL_AD_BAD_CERTIFICATE:
26a7d938 771 return SSL3_AD_BAD_CERTIFICATE;
0f113f3e 772 case SSL_AD_UNSUPPORTED_CERTIFICATE:
26a7d938 773 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
0f113f3e 774 case SSL_AD_CERTIFICATE_REVOKED:
26a7d938 775 return SSL3_AD_CERTIFICATE_REVOKED;
0f113f3e 776 case SSL_AD_CERTIFICATE_EXPIRED:
26a7d938 777 return SSL3_AD_CERTIFICATE_EXPIRED;
0f113f3e 778 case SSL_AD_CERTIFICATE_UNKNOWN:
26a7d938 779 return SSL3_AD_CERTIFICATE_UNKNOWN;
0f113f3e 780 case SSL_AD_ILLEGAL_PARAMETER:
26a7d938 781 return SSL3_AD_ILLEGAL_PARAMETER;
0f113f3e 782 case SSL_AD_UNKNOWN_CA:
26a7d938 783 return TLS1_AD_UNKNOWN_CA;
0f113f3e 784 case SSL_AD_ACCESS_DENIED:
26a7d938 785 return TLS1_AD_ACCESS_DENIED;
0f113f3e 786 case SSL_AD_DECODE_ERROR:
26a7d938 787 return TLS1_AD_DECODE_ERROR;
0f113f3e 788 case SSL_AD_DECRYPT_ERROR:
26a7d938 789 return TLS1_AD_DECRYPT_ERROR;
0f113f3e 790 case SSL_AD_EXPORT_RESTRICTION:
26a7d938 791 return TLS1_AD_EXPORT_RESTRICTION;
0f113f3e 792 case SSL_AD_PROTOCOL_VERSION:
26a7d938 793 return TLS1_AD_PROTOCOL_VERSION;
0f113f3e 794 case SSL_AD_INSUFFICIENT_SECURITY:
26a7d938 795 return TLS1_AD_INSUFFICIENT_SECURITY;
0f113f3e 796 case SSL_AD_INTERNAL_ERROR:
26a7d938 797 return TLS1_AD_INTERNAL_ERROR;
0f113f3e 798 case SSL_AD_USER_CANCELLED:
26a7d938 799 return TLS1_AD_USER_CANCELLED;
0f113f3e 800 case SSL_AD_NO_RENEGOTIATION:
26a7d938 801 return TLS1_AD_NO_RENEGOTIATION;
0f113f3e 802 case SSL_AD_UNSUPPORTED_EXTENSION:
26a7d938 803 return TLS1_AD_UNSUPPORTED_EXTENSION;
0f113f3e 804 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
26a7d938 805 return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
0f113f3e 806 case SSL_AD_UNRECOGNIZED_NAME:
26a7d938 807 return TLS1_AD_UNRECOGNIZED_NAME;
0f113f3e 808 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
26a7d938 809 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
0f113f3e 810 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
26a7d938 811 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
0f113f3e 812 case SSL_AD_UNKNOWN_PSK_IDENTITY:
26a7d938 813 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
0f113f3e 814 case SSL_AD_INAPPROPRIATE_FALLBACK:
26a7d938 815 return TLS1_AD_INAPPROPRIATE_FALLBACK;
06217867 816 case SSL_AD_NO_APPLICATION_PROTOCOL:
26a7d938 817 return TLS1_AD_NO_APPLICATION_PROTOCOL;
42c28b63
MC
818 case SSL_AD_CERTIFICATE_REQUIRED:
819 return SSL_AD_HANDSHAKE_FAILURE;
1376708c
BK
820 case TLS13_AD_MISSING_EXTENSION:
821 return SSL_AD_HANDSHAKE_FAILURE;
0f113f3e 822 default:
26a7d938 823 return -1;
0f113f3e
MC
824 }
825}