5b3d48464c324dd92ab9d7645fd02ea68e29ae5a
[openssl.git] / ssl / record / ssl3_record.c
1 /*
2  * Copyright 1995-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 <assert.h>
11 #include "../ssl_local.h"
12 #include <openssl/trace.h>
13 #include <openssl/rand.h>
14 #include <openssl/core_names.h>
15 #include "record_local.h"
16 #include "internal/cryptlib.h"
17
18 static const unsigned char ssl3_pad_1[48] = {
19     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
20     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
21     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
22     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
23     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
24     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
25 };
26
27 static const unsigned char ssl3_pad_2[48] = {
28     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
29     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
30     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
31     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
32     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
33     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
34 };
35
36 /*
37  * Clear the contents of an SSL3_RECORD but retain any memory allocated
38  */
39 void SSL3_RECORD_clear(SSL3_RECORD *r, size_t num_recs)
40 {
41     unsigned char *comp;
42     size_t i;
43
44     for (i = 0; i < num_recs; i++) {
45         comp = r[i].comp;
46
47         memset(&r[i], 0, sizeof(*r));
48         r[i].comp = comp;
49     }
50 }
51
52 void SSL3_RECORD_release(SSL3_RECORD *r, size_t num_recs)
53 {
54     size_t i;
55
56     for (i = 0; i < num_recs; i++) {
57         OPENSSL_free(r[i].comp);
58         r[i].comp = NULL;
59     }
60 }
61
62 void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
63 {
64     memcpy(r->seq_num, seq_num, SEQ_NUM_SIZE);
65 }
66
67 uint32_t ossl_get_max_early_data(SSL_CONNECTION *s)
68 {
69     uint32_t max_early_data;
70     SSL_SESSION *sess = s->session;
71
72     /*
73      * If we are a client then we always use the max_early_data from the
74      * session/psksession. Otherwise we go with the lowest out of the max early
75      * data set in the session and the configured max_early_data.
76      */
77     if (!s->server && sess->ext.max_early_data == 0) {
78         if (!ossl_assert(s->psksession != NULL
79                          && s->psksession->ext.max_early_data > 0)) {
80             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
81             return 0;
82         }
83         sess = s->psksession;
84     }
85
86     if (!s->server)
87         max_early_data = sess->ext.max_early_data;
88     else if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
89         max_early_data = s->recv_max_early_data;
90     else
91         max_early_data = s->recv_max_early_data < sess->ext.max_early_data
92                          ? s->recv_max_early_data : sess->ext.max_early_data;
93
94     return max_early_data;
95 }
96
97 int ossl_early_data_count_ok(SSL_CONNECTION *s, size_t length, size_t overhead,
98                              int send)
99 {
100     uint32_t max_early_data;
101
102     max_early_data = ossl_get_max_early_data(s);
103
104     if (max_early_data == 0) {
105         SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
106                  SSL_R_TOO_MUCH_EARLY_DATA);
107         return 0;
108     }
109
110     /* If we are dealing with ciphertext we need to allow for the overhead */
111     max_early_data += overhead;
112
113     if (s->early_data_count + length > max_early_data) {
114         SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
115                  SSL_R_TOO_MUCH_EARLY_DATA);
116         return 0;
117     }
118     s->early_data_count += length;
119
120     return 1;
121 }
122
123 int ssl3_do_uncompress(SSL_CONNECTION *ssl, SSL3_RECORD *rr)
124 {
125 #ifndef OPENSSL_NO_COMP
126     int i;
127
128     if (rr->comp == NULL) {
129         rr->comp = (unsigned char *)
130             OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
131     }
132     if (rr->comp == NULL)
133         return 0;
134
135     i = COMP_expand_block(ssl->expand, rr->comp,
136                           SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
137     if (i < 0)
138         return 0;
139     else
140         rr->length = i;
141     rr->data = rr->comp;
142 #endif
143     return 1;
144 }
145
146 int ssl3_do_compress(SSL_CONNECTION *sc, SSL3_RECORD *wr)
147 {
148 #ifndef OPENSSL_NO_COMP
149     int i;
150
151     i = COMP_compress_block(sc->compress, wr->data,
152                             (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
153                             wr->input, (int)wr->length);
154     if (i < 0)
155         return 0;
156     else
157         wr->length = i;
158
159     wr->input = wr->data;
160 #endif
161     return 1;
162 }
163
164 /*-
165  * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|. Calls SSLfatal on
166  * internal error, but not otherwise. It is the responsibility of the caller to
167  * report a bad_record_mac
168  *
169  * Returns:
170  *    0: if the record is publicly invalid, or an internal error
171  *    1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
172  */
173 int ssl3_enc(SSL_CONNECTION *s, SSL3_RECORD *inrecs, size_t n_recs, int sending,
174              SSL_MAC_BUF *mac, size_t macsize)
175 {
176     SSL3_RECORD *rec;
177     EVP_CIPHER_CTX *ds;
178     size_t l, i;
179     size_t bs;
180     const EVP_CIPHER *enc;
181
182     rec = inrecs;
183     /*
184      * We shouldn't ever be called with more than one record in the SSLv3 case
185      */
186     if (n_recs != 1)
187         return 0;
188     if (sending) {
189         ds = s->enc_write_ctx;
190         if (s->enc_write_ctx == NULL)
191             enc = NULL;
192         else
193             enc = EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx);
194     } else {
195         ds = s->enc_read_ctx;
196         if (s->enc_read_ctx == NULL)
197             enc = NULL;
198         else
199             enc = EVP_CIPHER_CTX_get0_cipher(s->enc_read_ctx);
200     }
201
202     if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
203         memmove(rec->data, rec->input, rec->length);
204         rec->input = rec->data;
205     } else {
206         int provided = (EVP_CIPHER_get0_provider(enc) != NULL);
207
208         l = rec->length;
209         bs = EVP_CIPHER_CTX_get_block_size(ds);
210
211         /* COMPRESS */
212
213         if ((bs != 1) && sending && !provided) {
214             /*
215              * We only do this for legacy ciphers. Provided ciphers add the
216              * padding on the provider side.
217              */
218             i = bs - (l % bs);
219
220             /* we need to add 'i-1' padding bytes */
221             l += i;
222             /*
223              * the last of these zero bytes will be overwritten with the
224              * padding length.
225              */
226             memset(&rec->input[rec->length], 0, i);
227             rec->length += i;
228             rec->input[l - 1] = (unsigned char)(i - 1);
229         }
230
231         if (!sending) {
232             if (l == 0 || l % bs != 0) {
233                 /* Publicly invalid */
234                 return 0;
235             }
236             /* otherwise, rec->length >= bs */
237         }
238
239         if (EVP_CIPHER_get0_provider(enc) != NULL) {
240             int outlen;
241
242             if (!EVP_CipherUpdate(ds, rec->data, &outlen, rec->input,
243                                   (unsigned int)l))
244                 return 0;
245             rec->length = outlen;
246
247             if (!sending && mac != NULL) {
248                 /* Now get a pointer to the MAC */
249                 OSSL_PARAM params[2], *p = params;
250
251                 /* Get the MAC */
252                 mac->alloced = 0;
253
254                 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
255                                                       (void **)&mac->mac,
256                                                       macsize);
257                 *p = OSSL_PARAM_construct_end();
258
259                 if (!EVP_CIPHER_CTX_get_params(ds, params)) {
260                     /* Shouldn't normally happen */
261                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
262                     return 0;
263                 }
264             }
265         } else {
266             if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1) {
267                 /* Shouldn't happen */
268                 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, ERR_R_INTERNAL_ERROR);
269                 return 0;
270             }
271
272             if (!sending)
273                 return ssl3_cbc_remove_padding_and_mac(&rec->length,
274                                            rec->orig_len,
275                                            rec->data,
276                                            (mac != NULL) ? &mac->mac : NULL,
277                                            (mac != NULL) ? &mac->alloced : NULL,
278                                            bs,
279                                            macsize,
280                                            SSL_CONNECTION_GET_CTX(s)->libctx);
281         }
282     }
283     return 1;
284 }
285
286 #define MAX_PADDING 256
287 /*-
288  * tls1_enc encrypts/decrypts |n_recs| in |recs|. Calls SSLfatal on internal
289  * error, but not otherwise. It is the responsibility of the caller to report
290  * a bad_record_mac - if appropriate (DTLS just drops the record).
291  *
292  * Returns:
293  *    0: if the record is publicly invalid, or an internal error, or AEAD
294  *       decryption failed, or Encrypt-then-mac decryption failed.
295  *    1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
296  */
297 int tls1_enc(SSL_CONNECTION *s, SSL3_RECORD *recs, size_t n_recs, int sending,
298              SSL_MAC_BUF *macs, size_t macsize)
299 {
300     EVP_CIPHER_CTX *ds;
301     size_t reclen[SSL_MAX_PIPELINES];
302     unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
303     int i, pad = 0, tmpr;
304     size_t bs, ctr, padnum, loop;
305     unsigned char padval;
306     const EVP_CIPHER *enc;
307     int tlstree_enc = sending ? (s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
308                               : (s->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
309
310     if (n_recs == 0) {
311         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
312         return 0;
313     }
314
315     if (sending) {
316         if (EVP_MD_CTX_get0_md(s->write_hash)) {
317             int n = EVP_MD_CTX_get_size(s->write_hash);
318             if (!ossl_assert(n >= 0)) {
319                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
320                 return 0;
321             }
322         }
323         ds = s->enc_write_ctx;
324         if (s->enc_write_ctx == NULL)
325             enc = NULL;
326         else {
327             int ivlen;
328
329             enc = EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx);
330             /* For TLSv1.1 and later explicit IV */
331             if (SSL_USE_EXPLICIT_IV(s)
332                 && EVP_CIPHER_get_mode(enc) == EVP_CIPH_CBC_MODE)
333                 ivlen = EVP_CIPHER_get_iv_length(enc);
334             else
335                 ivlen = 0;
336             if (ivlen > 1) {
337                 for (ctr = 0; ctr < n_recs; ctr++) {
338                     if (recs[ctr].data != recs[ctr].input) {
339                         /*
340                          * we can't write into the input stream: Can this ever
341                          * happen?? (steve)
342                          */
343                         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
344                         return 0;
345                     } else if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
346                                              recs[ctr].input,
347                                              ivlen, 0) <= 0) {
348                         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
349                         return 0;
350                     }
351                 }
352             }
353         }
354     } else {
355         if (EVP_MD_CTX_get0_md(s->read_hash)) {
356             int n = EVP_MD_CTX_get_size(s->read_hash);
357             if (!ossl_assert(n >= 0)) {
358                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
359                 return 0;
360             }
361         }
362         ds = s->enc_read_ctx;
363         if (s->enc_read_ctx == NULL)
364             enc = NULL;
365         else
366             enc = EVP_CIPHER_CTX_get0_cipher(s->enc_read_ctx);
367     }
368
369     if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
370         for (ctr = 0; ctr < n_recs; ctr++) {
371             memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
372             recs[ctr].input = recs[ctr].data;
373         }
374     } else {
375         int provided = (EVP_CIPHER_get0_provider(enc) != NULL);
376
377         bs = EVP_CIPHER_get_block_size(EVP_CIPHER_CTX_get0_cipher(ds));
378
379         if (n_recs > 1) {
380             if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
381                   & EVP_CIPH_FLAG_PIPELINE) == 0) {
382                 /*
383                  * We shouldn't have been called with pipeline data if the
384                  * cipher doesn't support pipelining
385                  */
386                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
387                 return 0;
388             }
389         }
390         for (ctr = 0; ctr < n_recs; ctr++) {
391             reclen[ctr] = recs[ctr].length;
392
393             if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
394                         & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) {
395                 unsigned char *seq;
396
397                 seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
398                     : RECORD_LAYER_get_read_sequence(&s->rlayer);
399
400                 if (SSL_CONNECTION_IS_DTLS(s)) {
401                     /* DTLS does not support pipelining */
402                     unsigned char dtlsseq[8], *p = dtlsseq;
403
404                     s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
405                         DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
406                     memcpy(p, &seq[2], 6);
407                     memcpy(buf[ctr], dtlsseq, 8);
408                 } else {
409                     memcpy(buf[ctr], seq, 8);
410                     for (i = 7; i >= 0; i--) { /* increment */
411                         ++seq[i];
412                         if (seq[i] != 0)
413                             break;
414                     }
415                 }
416
417                 buf[ctr][8] = recs[ctr].type;
418                 buf[ctr][9] = (unsigned char)(s->version >> 8);
419                 buf[ctr][10] = (unsigned char)(s->version);
420                 buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
421                 buf[ctr][12] = (unsigned char)(recs[ctr].length & 0xff);
422                 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
423                                           EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
424                 if (pad <= 0) {
425                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
426                     return 0;
427                 }
428
429                 if (sending) {
430                     reclen[ctr] += pad;
431                     recs[ctr].length += pad;
432                 }
433
434             } else if ((bs != 1) && sending && !provided) {
435                 /*
436                  * We only do this for legacy ciphers. Provided ciphers add the
437                  * padding on the provider side.
438                  */
439                 padnum = bs - (reclen[ctr] % bs);
440
441                 /* Add weird padding of up to 256 bytes */
442
443                 if (padnum > MAX_PADDING) {
444                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
445                     return 0;
446                 }
447                 /* we need to add 'padnum' padding bytes of value padval */
448                 padval = (unsigned char)(padnum - 1);
449                 for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
450                     recs[ctr].input[loop] = padval;
451                 reclen[ctr] += padnum;
452                 recs[ctr].length += padnum;
453             }
454
455             if (!sending) {
456                 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0) {
457                     /* Publicly invalid */
458                     return 0;
459                 }
460             }
461         }
462         if (n_recs > 1) {
463             unsigned char *data[SSL_MAX_PIPELINES];
464
465             /* Set the output buffers */
466             for (ctr = 0; ctr < n_recs; ctr++) {
467                 data[ctr] = recs[ctr].data;
468             }
469             if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
470                                     (int)n_recs, data) <= 0) {
471                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
472                 return 0;
473             }
474             /* Set the input buffers */
475             for (ctr = 0; ctr < n_recs; ctr++) {
476                 data[ctr] = recs[ctr].input;
477             }
478             if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
479                                     (int)n_recs, data) <= 0
480                 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
481                                        (int)n_recs, reclen) <= 0) {
482                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
483                 return 0;
484             }
485         }
486
487         if (!SSL_CONNECTION_IS_DTLS(s) && tlstree_enc) {
488             unsigned char *seq;
489             int decrement_seq = 0;
490
491             /*
492              * When sending, seq is incremented after MAC calculation.
493              * So if we are in ETM mode, we use seq 'as is' in the ctrl-function.
494              * Otherwise we have to decrease it in the implementation
495              */
496             if (sending && !SSL_WRITE_ETM(s))
497                 decrement_seq = 1;
498
499             seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
500                           : RECORD_LAYER_get_read_sequence(&s->rlayer);
501             if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_TLSTREE, decrement_seq, seq) <= 0) {
502                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
503                 return 0;
504             }
505         }
506
507         if (provided) {
508             int outlen;
509
510             /* Provided cipher - we do not support pipelining on this path */
511             if (n_recs > 1)  {
512                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
513                 return 0;
514             }
515
516             if (!EVP_CipherUpdate(ds, recs[0].data, &outlen, recs[0].input,
517                                   (unsigned int)reclen[0]))
518                 return 0;
519             recs[0].length = outlen;
520
521             /*
522              * The length returned from EVP_CipherUpdate above is the actual
523              * payload length. We need to adjust the data/input ptr to skip over
524              * any explicit IV
525              */
526             if (!sending) {
527                 if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_GCM_MODE) {
528                         recs[0].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
529                         recs[0].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
530                 } else if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_CCM_MODE) {
531                         recs[0].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
532                         recs[0].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
533                 } else if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
534                     recs[0].data += bs;
535                     recs[0].input += bs;
536                     recs[0].orig_len -= bs;
537                 }
538
539                 /* Now get a pointer to the MAC (if applicable) */
540                 if (macs != NULL) {
541                     OSSL_PARAM params[2], *p = params;
542
543                     /* Get the MAC */
544                     macs[0].alloced = 0;
545
546                     *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
547                                                           (void **)&macs[0].mac,
548                                                           macsize);
549                     *p = OSSL_PARAM_construct_end();
550
551                     if (!EVP_CIPHER_CTX_get_params(ds, params)) {
552                         /* Shouldn't normally happen */
553                         SSLfatal(s, SSL_AD_INTERNAL_ERROR,
554                                  ERR_R_INTERNAL_ERROR);
555                         return 0;
556                     }
557                 }
558             }
559         } else {
560             /* Legacy cipher */
561
562             tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
563                               (unsigned int)reclen[0]);
564             if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
565                  & EVP_CIPH_FLAG_CUSTOM_CIPHER) != 0
566                 ? (tmpr < 0)
567                 : (tmpr == 0)) {
568                 /* AEAD can fail to verify MAC */
569                 return 0;
570             }
571
572             if (!sending) {
573                 for (ctr = 0; ctr < n_recs; ctr++) {
574                     /* Adjust the record to remove the explicit IV/MAC/Tag */
575                     if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_GCM_MODE) {
576                         recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
577                         recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
578                         recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
579                     } else if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_CCM_MODE) {
580                         recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
581                         recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
582                         recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
583                     } else if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
584                         if (recs[ctr].length < bs)
585                             return 0;
586                         recs[ctr].data += bs;
587                         recs[ctr].input += bs;
588                         recs[ctr].length -= bs;
589                         recs[ctr].orig_len -= bs;
590                     }
591
592                     /*
593                      * If using Mac-then-encrypt, then this will succeed but
594                      * with a random MAC if padding is invalid
595                      */
596                     if (!tls1_cbc_remove_padding_and_mac(&recs[ctr].length,
597                                          recs[ctr].orig_len,
598                                          recs[ctr].data,
599                                          (macs != NULL) ? &macs[ctr].mac : NULL,
600                                          (macs != NULL) ? &macs[ctr].alloced
601                                                         : NULL,
602                                          bs,
603                                          pad ? (size_t)pad : macsize,
604                                          (EVP_CIPHER_get_flags(enc)
605                                          & EVP_CIPH_FLAG_AEAD_CIPHER) != 0,
606                                          SSL_CONNECTION_GET_CTX(s)->libctx))
607                         return 0;
608                 }
609             }
610         }
611     }
612     return 1;
613 }
614
615 int n_ssl3_mac(SSL_CONNECTION *sc, SSL3_RECORD *rec, unsigned char *md,
616                int sending)
617 {
618     unsigned char *mac_sec, *seq;
619     const EVP_MD_CTX *hash;
620     unsigned char *p, rec_char;
621     size_t md_size;
622     size_t npad;
623     int t;
624     unsigned int md_size_u;
625     EVP_MD_CTX *md_ctx;
626
627     /*
628      * All read record layer operations should have been moved to the new
629      * record layer code
630      */
631     assert(sending);
632
633     mac_sec = &(sc->s3.write_mac_secret[0]);
634     seq = RECORD_LAYER_get_write_sequence(&sc->rlayer);
635     hash = sc->write_hash;
636
637     t = EVP_MD_CTX_get_size(hash);
638     if (t < 0)
639         return 0;
640     md_size = t;
641     npad = (48 / md_size) * md_size;
642
643     /* Chop the digest off the end :-) */
644     md_ctx = EVP_MD_CTX_new();
645
646     if (md_ctx == NULL)
647         return 0;
648
649     rec_char = rec->type;
650     p = md;
651     s2n(rec->length, p);
652     if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
653         || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
654         || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
655         || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
656         || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
657         || EVP_DigestUpdate(md_ctx, md, 2) <= 0
658         || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
659         || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
660         || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
661         || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
662         || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
663         || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
664         || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
665         EVP_MD_CTX_free(md_ctx);
666         return 0;
667     }
668
669     EVP_MD_CTX_free(md_ctx);
670
671     ssl3_record_sequence_update(seq);
672     return 1;
673 }
674
675 int tls1_mac_old(SSL_CONNECTION *sc, SSL3_RECORD *rec, unsigned char *md,
676                  int sending)
677 {
678     unsigned char *seq;
679     EVP_MD_CTX *hash;
680     size_t md_size;
681     int i;
682     EVP_MD_CTX *hmac = NULL, *mac_ctx;
683     unsigned char header[13];
684     int stream_mac = sending ? (sc->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
685                              : (sc->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM);
686     int tlstree_mac = sending ? (sc->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
687                               : (sc->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
688     int t;
689     int ret = 0;
690
691     /*
692      * All read record layer calls should have been moved to the new record
693      * layer.
694      */
695     assert(sending);
696
697     seq = RECORD_LAYER_get_write_sequence(&sc->rlayer);
698     hash = sc->write_hash;
699
700     t = EVP_MD_CTX_get_size(hash);
701     if (!ossl_assert(t >= 0))
702         return 0;
703     md_size = t;
704
705     /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
706     if (stream_mac) {
707         mac_ctx = hash;
708     } else {
709         hmac = EVP_MD_CTX_new();
710         if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash)) {
711             goto end;
712         }
713         mac_ctx = hmac;
714     }
715
716     if (!SSL_CONNECTION_IS_DTLS(sc) && tlstree_mac
717         && EVP_MD_CTX_ctrl(mac_ctx, EVP_MD_CTRL_TLSTREE, 0, seq) <= 0) {
718         goto end;
719     }
720
721     if (SSL_CONNECTION_IS_DTLS(sc)) {
722         unsigned char dtlsseq[8], *p = dtlsseq;
723
724         s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&sc->rlayer) :
725             DTLS_RECORD_LAYER_get_r_epoch(&sc->rlayer), p);
726         memcpy(p, &seq[2], 6);
727
728         memcpy(header, dtlsseq, 8);
729     } else
730         memcpy(header, seq, 8);
731
732     header[8] = rec->type;
733     header[9] = (unsigned char)(sc->version >> 8);
734     header[10] = (unsigned char)(sc->version);
735     header[11] = (unsigned char)(rec->length >> 8);
736     header[12] = (unsigned char)(rec->length & 0xff);
737
738     if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
739         || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
740         || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
741         goto end;
742     }
743
744     OSSL_TRACE_BEGIN(TLS) {
745         BIO_printf(trc_out, "seq:\n");
746         BIO_dump_indent(trc_out, seq, 8, 4);
747         BIO_printf(trc_out, "rec:\n");
748         BIO_dump_indent(trc_out, rec->data, rec->length, 4);
749     } OSSL_TRACE_END(TLS);
750
751     if (!SSL_CONNECTION_IS_DTLS(sc)) {
752         for (i = 7; i >= 0; i--) {
753             ++seq[i];
754             if (seq[i] != 0)
755                 break;
756         }
757     }
758     OSSL_TRACE_BEGIN(TLS) {
759         BIO_printf(trc_out, "md:\n");
760         BIO_dump_indent(trc_out, md, md_size, 4);
761     } OSSL_TRACE_END(TLS);
762     ret = 1;
763  end:
764     EVP_MD_CTX_free(hmac);
765     return ret;
766 }