]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/record/ssl3_record.c
Fix error in BIO_get_ktls_send() and BIO_get_ktls_recv()
[thirdparty/openssl.git] / ssl / record / ssl3_record.c
CommitLineData
846e33c7 1/*
48e5119a 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
258f8721 3 *
2c18d164 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
258f8721
MC
8 */
9
10#include "../ssl_locl.h"
68570797 11#include "internal/constant_time_locl.h"
49b26f54 12#include <openssl/trace.h>
02a36fda 13#include <openssl/rand.h>
c99c4c11 14#include "record_locl.h"
67dc995e 15#include "internal/cryptlib.h"
02a36fda
MC
16
17static const unsigned char ssl3_pad_1[48] = {
18 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
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};
25
26static const unsigned char ssl3_pad_2[48] = {
27 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
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};
258f8721 34
6b41b3f5
MC
35/*
36 * Clear the contents of an SSL3_RECORD but retain any memory allocated
37 */
5607b275 38void SSL3_RECORD_clear(SSL3_RECORD *r, size_t num_recs)
258f8721 39{
94777c9c 40 unsigned char *comp;
5607b275 41 size_t i;
6b41b3f5 42
94777c9c
MC
43 for (i = 0; i < num_recs; i++) {
44 comp = r[i].comp;
45
46 memset(&r[i], 0, sizeof(*r));
47 r[i].comp = comp;
48 }
258f8721
MC
49}
50
5607b275 51void SSL3_RECORD_release(SSL3_RECORD *r, size_t num_recs)
258f8721 52{
5607b275 53 size_t i;
94777c9c
MC
54
55 for (i = 0; i < num_recs; i++) {
56 OPENSSL_free(r[i].comp);
57 r[i].comp = NULL;
58 }
258f8721
MC
59}
60
258f8721
MC
61void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
62{
e5bf62f7 63 memcpy(r->seq_num, seq_num, SEQ_NUM_SIZE);
258f8721 64}
fe589e61 65
94777c9c
MC
66/*
67 * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
68 * for us in the buffer.
69 */
ea71906e 70static int ssl3_record_app_data_waiting(SSL *s)
94777c9c
MC
71{
72 SSL3_BUFFER *rbuf;
8e6d03ca 73 size_t left, len;
94777c9c
MC
74 unsigned char *p;
75
76 rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
77
78 p = SSL3_BUFFER_get_buf(rbuf);
79 if (p == NULL)
80 return 0;
81
82 left = SSL3_BUFFER_get_left(rbuf);
83
84 if (left < SSL3_RT_HEADER_LENGTH)
85 return 0;
86
87 p += SSL3_BUFFER_get_offset(rbuf);
88
89 /*
90 * We only check the type and record length, we will sanity check version
91 * etc later
92 */
93 if (*p != SSL3_RT_APPLICATION_DATA)
94 return 0;
95
96 p += 3;
97 n2s(p, len);
98
99 if (left < SSL3_RT_HEADER_LENGTH + len)
100 return 0;
101
102 return 1;
103}
104
196f2cbb 105int early_data_count_ok(SSL *s, size_t length, size_t overhead, int send)
70ef40a0 106{
4e8548e8 107 uint32_t max_early_data;
add8d0e9 108 SSL_SESSION *sess = s->session;
70ef40a0
MC
109
110 /*
7daf7156 111 * If we are a client then we always use the max_early_data from the
add8d0e9
MC
112 * session/psksession. Otherwise we go with the lowest out of the max early
113 * data set in the session and the configured max_early_data.
70ef40a0 114 */
add8d0e9
MC
115 if (!s->server && sess->ext.max_early_data == 0) {
116 if (!ossl_assert(s->psksession != NULL
117 && s->psksession->ext.max_early_data > 0)) {
196f2cbb
MC
118 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_EARLY_DATA_COUNT_OK,
119 ERR_R_INTERNAL_ERROR);
add8d0e9
MC
120 return 0;
121 }
122 sess = s->psksession;
123 }
4e8548e8
MC
124
125 if (!s->server)
add8d0e9 126 max_early_data = sess->ext.max_early_data;
4e8548e8
MC
127 else if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
128 max_early_data = s->recv_max_early_data;
129 else
130 max_early_data = s->recv_max_early_data < sess->ext.max_early_data
131 ? s->recv_max_early_data : sess->ext.max_early_data;
70ef40a0
MC
132
133 if (max_early_data == 0) {
196f2cbb
MC
134 SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
135 SSL_F_EARLY_DATA_COUNT_OK, SSL_R_TOO_MUCH_EARLY_DATA);
70ef40a0
MC
136 return 0;
137 }
138
139 /* If we are dealing with ciphertext we need to allow for the overhead */
140 max_early_data += overhead;
141
7daf7156 142 if (s->early_data_count + length > max_early_data) {
196f2cbb
MC
143 SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
144 SSL_F_EARLY_DATA_COUNT_OK, SSL_R_TOO_MUCH_EARLY_DATA);
70ef40a0
MC
145 return 0;
146 }
7daf7156 147 s->early_data_count += length;
70ef40a0
MC
148
149 return 1;
150}
151
fe589e61
MC
152/*
153 * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
154 * will be processed per call to ssl3_get_record. Without this limit an
155 * attacker could send empty records at a faster rate than we can process and
156 * cause ssl3_get_record to loop forever.
157 */
158#define MAX_EMPTY_RECORDS 32
159
32ec4153 160#define SSL2_RT_HEADER_LENGTH 2
fe589e61 161/*-
94777c9c 162 * Call this to get new input records.
fe589e61
MC
163 * It will return <= 0 if more data is needed, normally due to an error
164 * or non-blocking IO.
94777c9c
MC
165 * When it finishes, |numrpipes| records have been decoded. For each record 'i':
166 * rr[i].type - is the type of record
167 * rr[i].data, - data
168 * rr[i].length, - number of bytes
169 * Multiple records will only be returned if the record types are all
170 * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
171 * |max_pipelines|
fe589e61
MC
172 */
173/* used only by ssl3_read_bytes */
174int ssl3_get_record(SSL *s)
175{
196f2cbb 176 int enc_err, rret;
8e6d03ca
MC
177 int i;
178 size_t more, n;
88858868 179 SSL3_RECORD *rr, *thisrr;
94777c9c 180 SSL3_BUFFER *rbuf;
fe589e61
MC
181 SSL_SESSION *sess;
182 unsigned char *p;
183 unsigned char md[EVP_MAX_MD_SIZE];
6a149cee 184 unsigned int version;
72716e79
MC
185 size_t mac_size;
186 int imac_size;
187 size_t num_recs = 0, max_recs, j;
6a149cee 188 PACKET pkt, sslv2pkt;
70ef40a0 189 size_t first_rec_len;
c35e921f 190 int is_ktls_left;
fe589e61
MC
191
192 rr = RECORD_LAYER_get_rrec(&s->rlayer);
94777c9c 193 rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
c35e921f 194 is_ktls_left = (rbuf->left > 0);
94777c9c
MC
195 max_recs = s->max_pipelines;
196 if (max_recs == 0)
197 max_recs = 1;
fe589e61
MC
198 sess = s->session;
199
94777c9c 200 do {
88858868
MC
201 thisrr = &rr[num_recs];
202
94777c9c
MC
203 /* check if we have the header */
204 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
205 (RECORD_LAYER_get_packet_length(&s->rlayer)
206 < SSL3_RT_HEADER_LENGTH)) {
6a149cee
MC
207 size_t sslv2len;
208 unsigned int type;
209
8e6d03ca
MC
210 rret = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
211 SSL3_BUFFER_get_len(rbuf), 0,
212 num_recs == 0 ? 1 : 0, &n);
c35e921f 213 if (rret <= 0) {
3119ab3c 214#ifndef OPENSSL_NO_KTLS
c35e921f
BP
215 if (!BIO_get_ktls_recv(s->rbio))
216 return rret; /* error or non-blocking */
c35e921f
BP
217 switch (errno) {
218 case EBADMSG:
219 SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
220 SSL_F_SSL3_GET_RECORD,
221 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
222 break;
223 case EMSGSIZE:
224 SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
225 SSL_F_SSL3_GET_RECORD,
226 SSL_R_PACKET_LENGTH_TOO_LONG);
227 break;
228 case EINVAL:
229 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
230 SSL_F_SSL3_GET_RECORD,
231 SSL_R_WRONG_VERSION_NUMBER);
232 break;
233 default:
234 break;
235 }
c35e921f 236#endif
3119ab3c 237 return rret;
c35e921f 238 }
94777c9c
MC
239 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
240
241 p = RECORD_LAYER_get_packet(&s->rlayer);
6a149cee
MC
242 if (!PACKET_buf_init(&pkt, RECORD_LAYER_get_packet(&s->rlayer),
243 RECORD_LAYER_get_packet_length(&s->rlayer))) {
196f2cbb
MC
244 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
245 ERR_R_INTERNAL_ERROR);
246 return -1;
6a149cee
MC
247 }
248 sslv2pkt = pkt;
249 if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
250 || !PACKET_get_1(&sslv2pkt, &type)) {
196f2cbb
MC
251 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
252 ERR_R_INTERNAL_ERROR);
253 return -1;
6a149cee 254 }
94777c9c 255 /*
78fcddbb 256 * The first record received by the server may be a V2ClientHello.
94777c9c 257 */
78fcddbb 258 if (s->server && RECORD_LAYER_is_first_record(&s->rlayer)
6a149cee
MC
259 && (sslv2len & 0x8000) != 0
260 && (type == SSL2_MT_CLIENT_HELLO)) {
be9c8deb
MC
261 /*
262 * SSLv2 style record
263 *
264 * |num_recs| here will actually always be 0 because
265 * |num_recs > 0| only ever occurs when we are processing
266 * multiple app data records - which we know isn't the case here
267 * because it is an SSLv2ClientHello. We keep it using
268 * |num_recs| for the sake of consistency
269 */
88858868
MC
270 thisrr->type = SSL3_RT_HANDSHAKE;
271 thisrr->rec_version = SSL2_VERSION;
94777c9c 272
88858868 273 thisrr->length = sslv2len & 0x7fff;
94777c9c 274
88858868 275 if (thisrr->length > SSL3_BUFFER_get_len(rbuf)
a230b26e 276 - SSL2_RT_HEADER_LENGTH) {
196f2cbb
MC
277 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
278 SSL_R_PACKET_LENGTH_TOO_LONG);
279 return -1;
94777c9c 280 }
fe589e61 281
88858868 282 if (thisrr->length < MIN_SSL2_RECORD_LEN) {
196f2cbb
MC
283 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
284 SSL_R_LENGTH_TOO_SHORT);
285 return -1;
94777c9c
MC
286 }
287 } else {
288 /* SSLv3+ style record */
289 if (s->msg_callback)
290 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
291 s->msg_callback_arg);
292
293 /* Pull apart the header into the SSL3_RECORD */
6a149cee
MC
294 if (!PACKET_get_1(&pkt, &type)
295 || !PACKET_get_net_2(&pkt, &version)
88858868 296 || !PACKET_get_net_2_len(&pkt, &thisrr->length)) {
196f2cbb
MC
297 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
298 ERR_R_INTERNAL_ERROR);
299 return -1;
6a149cee 300 }
88858868
MC
301 thisrr->type = type;
302 thisrr->rec_version = version;
94777c9c 303
a2b97bdf 304 /*
3295d242
MC
305 * Lets check version. In TLSv1.3 we only check this field
306 * when encryption is occurring (see later check). For the
beb30941
MC
307 * ServerHello after an HRR we haven't actually selected TLSv1.3
308 * yet, but we still treat it as TLSv1.3, so we must check for
309 * that explicitly
a2b97bdf 310 */
657a43f6 311 if (!s->first_packet && !SSL_IS_TLS13(s)
fc7129dc 312 && s->hello_retry_request != SSL_HRR_PENDING
6a149cee 313 && version != (unsigned int)s->version) {
94777c9c
MC
314 if ((s->version & 0xFF00) == (version & 0xFF00)
315 && !s->enc_write_ctx && !s->write_hash) {
88858868 316 if (thisrr->type == SSL3_RT_ALERT) {
94777c9c
MC
317 /*
318 * The record is using an incorrect version number,
319 * but what we've got appears to be an alert. We
320 * haven't read the body yet to check whether its a
321 * fatal or not - but chances are it is. We probably
322 * shouldn't send a fatal alert back. We'll just
323 * end.
324 */
196f2cbb
MC
325 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
326 SSL_R_WRONG_VERSION_NUMBER);
327 return -1;
94777c9c 328 }
02db21df 329 /*
94777c9c 330 * Send back error using their minor version number :-)
02db21df 331 */
94777c9c 332 s->version = (unsigned short)version;
02db21df 333 }
196f2cbb
MC
334 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL3_GET_RECORD,
335 SSL_R_WRONG_VERSION_NUMBER);
336 return -1;
02db21df 337 }
32ec4153 338
94777c9c 339 if ((version >> 8) != SSL3_VERSION_MAJOR) {
a01c86a2 340 if (RECORD_LAYER_is_first_record(&s->rlayer)) {
94777c9c
MC
341 /* Go back to start of packet, look at the five bytes
342 * that we have. */
343 p = RECORD_LAYER_get_packet(&s->rlayer);
344 if (strncmp((char *)p, "GET ", 4) == 0 ||
345 strncmp((char *)p, "POST ", 5) == 0 ||
346 strncmp((char *)p, "HEAD ", 5) == 0 ||
347 strncmp((char *)p, "PUT ", 4) == 0) {
196f2cbb
MC
348 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
349 SSL_R_HTTP_REQUEST);
350 return -1;
94777c9c 351 } else if (strncmp((char *)p, "CONNE", 5) == 0) {
196f2cbb
MC
352 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
353 SSL_R_HTTPS_PROXY_REQUEST);
354 return -1;
94777c9c 355 }
a01c86a2
MC
356
357 /* Doesn't look like TLS - don't send an alert */
196f2cbb
MC
358 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
359 SSL_R_WRONG_VERSION_NUMBER);
360 return -1;
a01c86a2 361 } else {
196f2cbb
MC
362 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
363 SSL_F_SSL3_GET_RECORD,
364 SSL_R_WRONG_VERSION_NUMBER);
365 return -1;
124f6ff4
RJ
366 }
367 }
32ec4153 368
3295d242
MC
369 if (SSL_IS_TLS13(s) && s->enc_read_ctx != NULL) {
370 if (thisrr->type != SSL3_RT_APPLICATION_DATA
371 && (thisrr->type != SSL3_RT_CHANGE_CIPHER_SPEC
de9e884b
MC
372 || !SSL_IS_FIRST_HANDSHAKE(s))
373 && (thisrr->type != SSL3_RT_ALERT
374 || s->statem.enc_read_state
375 != ENC_READ_STATE_ALLOW_PLAIN_ALERTS)) {
3295d242
MC
376 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
377 SSL_F_SSL3_GET_RECORD, SSL_R_BAD_RECORD_TYPE);
378 return -1;
379 }
380 if (thisrr->rec_version != TLS1_2_VERSION) {
381 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
382 SSL_R_WRONG_VERSION_NUMBER);
383 return -1;
384 }
e60ce9c4
MC
385 }
386
88858868 387 if (thisrr->length >
a230b26e 388 SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
196f2cbb
MC
389 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
390 SSL_R_PACKET_LENGTH_TOO_LONG);
391 return -1;
94777c9c 392 }
32ec4153 393 }
94777c9c
MC
394
395 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
fe589e61
MC
396 }
397
43219695
MC
398 if (SSL_IS_TLS13(s)) {
399 if (thisrr->length > SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH) {
196f2cbb
MC
400 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
401 SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
402 return -1;
43219695
MC
403 }
404 } else {
405 size_t len = SSL3_RT_MAX_ENCRYPTED_LENGTH;
406
4f7b76bf
MC
407#ifndef OPENSSL_NO_COMP
408 /*
409 * If OPENSSL_NO_COMP is defined then SSL3_RT_MAX_ENCRYPTED_LENGTH
410 * does not include the compression overhead anyway.
411 */
43219695
MC
412 if (s->expand == NULL)
413 len -= SSL3_RT_MAX_COMPRESSED_OVERHEAD;
4f7b76bf 414#endif
43219695 415
c35e921f 416 if (thisrr->length > len && !BIO_get_ktls_recv(s->rbio)) {
196f2cbb
MC
417 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
418 SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
419 return -1;
43219695
MC
420 }
421 }
422
94777c9c
MC
423 /*
424 * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
425 * Calculate how much more data we need to read for the rest of the
426 * record
427 */
88858868
MC
428 if (thisrr->rec_version == SSL2_VERSION) {
429 more = thisrr->length + SSL2_RT_HEADER_LENGTH
94777c9c
MC
430 - SSL3_RT_HEADER_LENGTH;
431 } else {
88858868 432 more = thisrr->length;
94777c9c 433 }
c35e921f 434
8e6d03ca 435 if (more > 0) {
94777c9c 436 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
fe589e61 437
8e6d03ca
MC
438 rret = ssl3_read_n(s, more, more, 1, 0, &n);
439 if (rret <= 0)
440 return rret; /* error or non-blocking io */
94777c9c 441 }
32ec4153 442
94777c9c
MC
443 /* set state for later operations */
444 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
fe589e61 445
94777c9c 446 /*
88858868
MC
447 * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH
448 * + thisrr->length, or s->packet_length == SSL2_RT_HEADER_LENGTH
449 * + thisrr->length and we have that many bytes in s->packet
94777c9c 450 */
88858868
MC
451 if (thisrr->rec_version == SSL2_VERSION) {
452 thisrr->input =
94777c9c
MC
453 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
454 } else {
88858868 455 thisrr->input =
94777c9c
MC
456 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
457 }
fe589e61 458
94777c9c 459 /*
88858868
MC
460 * ok, we can now read from 's->packet' data into 'thisrr' thisrr->input
461 * points at thisrr->length bytes, which need to be copied into
462 * thisrr->data by either the decryption or by the decompression When
463 * the data is 'copied' into the thisrr->data buffer, thisrr->input will
464 * be pointed at the new buffer
94777c9c 465 */
fe589e61 466
94777c9c 467 /*
88858868
MC
468 * We now have - encrypted [ MAC [ compressed [ plain ] ] ]
469 * thisrr->length bytes of encrypted compressed stuff.
94777c9c 470 */
fe589e61 471
88858868
MC
472 /* decrypt in place in 'thisrr->input' */
473 thisrr->data = thisrr->input;
474 thisrr->orig_len = thisrr->length;
255cfeac
MC
475
476 /* Mark this record as not read by upper layers yet */
88858868 477 thisrr->read = 0;
255cfeac 478
94777c9c
MC
479 num_recs++;
480
481 /* we have pulled in a full packet so zero things */
482 RECORD_LAYER_reset_packet_length(&s->rlayer);
78fcddbb 483 RECORD_LAYER_clear_first_record(&s->rlayer);
de0717eb 484 } while (num_recs < max_recs
88858868 485 && thisrr->type == SSL3_RT_APPLICATION_DATA
94777c9c
MC
486 && SSL_USE_EXPLICIT_IV(s)
487 && s->enc_read_ctx != NULL
488 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
a230b26e 489 & EVP_CIPH_FLAG_PIPELINE)
ea71906e 490 && ssl3_record_app_data_waiting(s));
fe589e61 491
fdd92367
MC
492 if (num_recs == 1
493 && thisrr->type == SSL3_RT_CHANGE_CIPHER_SPEC
fc7129dc 494 && (SSL_IS_TLS13(s) || s->hello_retry_request != SSL_HRR_NONE)
fdd92367
MC
495 && SSL_IS_FIRST_HANDSHAKE(s)) {
496 /*
497 * CCS messages must be exactly 1 byte long, containing the value 0x01
498 */
499 if (thisrr->length != 1 || thisrr->data[0] != 0x01) {
500 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL3_GET_RECORD,
501 SSL_R_INVALID_CCS_MESSAGE);
502 return -1;
503 }
504 /*
505 * CCS messages are ignored in TLSv1.3. We treat it like an empty
506 * handshake record
507 */
508 thisrr->type = SSL3_RT_HANDSHAKE;
509 RECORD_LAYER_inc_empty_record_count(&s->rlayer);
510 if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
511 > MAX_EMPTY_RECORDS) {
512 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
513 SSL_R_UNEXPECTED_CCS_MESSAGE);
514 return -1;
515 }
516 thisrr->read = 1;
517 RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
518
519 return 1;
520 }
521
c35e921f
BP
522 /*
523 * KTLS reads full records. If there is any data left,
524 * then it is from before enabling ktls
525 */
526 if (BIO_get_ktls_recv(s->rbio) && !is_ktls_left)
527 goto skip_decryption;
528
fe589e61
MC
529 /*
530 * If in encrypt-then-mac mode calculate mac from encrypted record. All
531 * the details below are public so no timing details can leak.
532 */
28a31a0a 533 if (SSL_READ_ETM(s) && s->read_hash) {
fe589e61 534 unsigned char *mac;
72716e79
MC
535 /* TODO(size_t): convert this to do size_t properly */
536 imac_size = EVP_MD_CTX_size(s->read_hash);
b77f3ed1 537 if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
196f2cbb
MC
538 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
539 ERR_LIB_EVP);
540 return -1;
72716e79
MC
541 }
542 mac_size = (size_t)imac_size;
94777c9c 543 for (j = 0; j < num_recs; j++) {
88858868
MC
544 thisrr = &rr[j];
545
546 if (thisrr->length < mac_size) {
196f2cbb
MC
547 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
548 SSL_R_LENGTH_TOO_SHORT);
549 return -1;
94777c9c 550 }
88858868
MC
551 thisrr->length -= mac_size;
552 mac = thisrr->data + thisrr->length;
553 i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
a14aa99b 554 if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
196f2cbb 555 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
94777c9c 556 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
196f2cbb 557 return -1;
94777c9c 558 }
fe589e61
MC
559 }
560 }
561
70ef40a0
MC
562 first_rec_len = rr[0].length;
563
94777c9c 564 enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0);
e60ce9c4 565
fe589e61
MC
566 /*-
567 * enc_err is:
d781d247 568 * 0: (in non-constant time) if the record is publicly invalid.
fe589e61
MC
569 * 1: if the padding is valid
570 * -1: if the padding is invalid
571 */
572 if (enc_err == 0) {
921d84a0
MC
573 if (ossl_statem_in_error(s)) {
574 /* SSLfatal() already got called */
575 return -1;
576 }
0a87d0ac
MC
577 if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
578 /*
d781d247
MC
579 * Valid early_data that we cannot decrypt might fail here as
580 * publicly invalid. We treat it like an empty record.
0a87d0ac 581 */
70ef40a0 582
0a87d0ac 583 thisrr = &rr[0];
70ef40a0
MC
584
585 if (!early_data_count_ok(s, thisrr->length,
196f2cbb
MC
586 EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
587 /* SSLfatal() already called */
588 return -1;
589 }
70ef40a0 590
0a87d0ac
MC
591 thisrr->length = 0;
592 thisrr->read = 1;
593 RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
67f78ead 594 RECORD_LAYER_reset_read_sequence(&s->rlayer);
0a87d0ac
MC
595 return 1;
596 }
196f2cbb
MC
597 SSLfatal(s, SSL_AD_DECRYPTION_FAILED, SSL_F_SSL3_GET_RECORD,
598 SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
599 return -1;
fe589e61 600 }
49b26f54
RL
601 OSSL_TRACE_BEGIN(TLS) {
602 BIO_printf(trc_out, "dec %lu\n", (unsigned long)rr[0].length);
603 BIO_dump_indent(trc_out, rr[0].data, rr[0].length, 4);
604 } OSSL_TRACE_END(TLS);
fe589e61
MC
605
606 /* r->length is now the compressed data plus mac */
607 if ((sess != NULL) &&
608 (s->enc_read_ctx != NULL) &&
28a31a0a 609 (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)) {
fe589e61
MC
610 /* s->read_hash != NULL => mac_size != -1 */
611 unsigned char *mac = NULL;
612 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
94777c9c 613
fe589e61 614 mac_size = EVP_MD_CTX_size(s->read_hash);
380a522f 615 if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
196f2cbb
MC
616 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
617 ERR_R_INTERNAL_ERROR);
618 return -1;
380a522f 619 }
fe589e61 620
a230b26e 621 for (j = 0; j < num_recs; j++) {
88858868 622 thisrr = &rr[j];
fe589e61 623 /*
94777c9c
MC
624 * orig_len is the length of the record before any padding was
625 * removed. This is public information, as is the MAC in use,
626 * therefore we can safely process the record in a different amount
627 * of time if it's too short to possibly contain a MAC.
fe589e61 628 */
88858868 629 if (thisrr->orig_len < mac_size ||
94777c9c
MC
630 /* CBC records must have a padding length byte too. */
631 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
88858868 632 thisrr->orig_len < mac_size + 1)) {
196f2cbb
MC
633 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
634 SSL_R_LENGTH_TOO_SHORT);
635 return -1;
94777c9c 636 }
fe589e61 637
94777c9c
MC
638 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
639 /*
640 * We update the length so that the TLS header bytes can be
641 * constructed correctly but we need to extract the MAC in
642 * constant time from within the record, without leaking the
643 * contents of the padding bytes.
644 */
645 mac = mac_tmp;
380a522f 646 if (!ssl3_cbc_copy_mac(mac_tmp, thisrr, mac_size)) {
196f2cbb
MC
647 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
648 ERR_R_INTERNAL_ERROR);
649 return -1;
380a522f 650 }
88858868 651 thisrr->length -= mac_size;
94777c9c
MC
652 } else {
653 /*
654 * In this case there's no padding, so |rec->orig_len| equals
655 * |rec->length| and we checked that there's enough bytes for
656 * |mac_size| above.
657 */
88858868
MC
658 thisrr->length -= mac_size;
659 mac = &thisrr->data[thisrr->length];
94777c9c
MC
660 }
661
88858868 662 i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
a14aa99b 663 if (i == 0 || mac == NULL
94777c9c
MC
664 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
665 enc_err = -1;
88858868 666 if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
94777c9c
MC
667 enc_err = -1;
668 }
fe589e61
MC
669 }
670
671 if (enc_err < 0) {
921d84a0
MC
672 if (ossl_statem_in_error(s)) {
673 /* We already called SSLfatal() */
674 return -1;
675 }
d781d247
MC
676 if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
677 /*
678 * We assume this is unreadable early_data - we treat it like an
679 * empty record
680 */
70ef40a0
MC
681
682 /*
683 * The record length may have been modified by the mac check above
684 * so we use the previously saved value
685 */
686 if (!early_data_count_ok(s, first_rec_len,
196f2cbb
MC
687 EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
688 /* SSLfatal() already called */
689 return -1;
690 }
70ef40a0 691
d781d247
MC
692 thisrr = &rr[0];
693 thisrr->length = 0;
694 thisrr->read = 1;
695 RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
67f78ead 696 RECORD_LAYER_reset_read_sequence(&s->rlayer);
d781d247
MC
697 return 1;
698 }
fe589e61
MC
699 /*
700 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
701 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
702 * failure is directly visible from the ciphertext anyway, we should
703 * not reveal which kind of error occurred -- this might become
704 * visible to an attacker (e.g. via a logfile)
705 */
196f2cbb
MC
706 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
707 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
708 return -1;
fe589e61
MC
709 }
710
c35e921f
BP
711 skip_decryption:
712
94777c9c 713 for (j = 0; j < num_recs; j++) {
88858868
MC
714 thisrr = &rr[j];
715
716 /* thisrr->length is now just compressed */
94777c9c 717 if (s->expand != NULL) {
88858868 718 if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
196f2cbb
MC
719 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
720 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
721 return -1;
94777c9c 722 }
88858868 723 if (!ssl3_do_uncompress(s, thisrr)) {
196f2cbb
MC
724 SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE, SSL_F_SSL3_GET_RECORD,
725 SSL_R_BAD_DECOMPRESSION);
726 return -1;
94777c9c 727 }
fe589e61 728 }
94777c9c 729
de9e884b
MC
730 if (SSL_IS_TLS13(s)
731 && s->enc_read_ctx != NULL
732 && thisrr->type != SSL3_RT_ALERT) {
e60ce9c4
MC
733 size_t end;
734
3c544acc
MC
735 if (thisrr->length == 0
736 || thisrr->type != SSL3_RT_APPLICATION_DATA) {
196f2cbb
MC
737 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
738 SSL_R_BAD_RECORD_TYPE);
739 return -1;
e60ce9c4
MC
740 }
741
742 /* Strip trailing padding */
88858868
MC
743 for (end = thisrr->length - 1; end > 0 && thisrr->data[end] == 0;
744 end--)
e60ce9c4
MC
745 continue;
746
88858868
MC
747 thisrr->length = end;
748 thisrr->type = thisrr->data[end];
749 if (thisrr->type != SSL3_RT_APPLICATION_DATA
750 && thisrr->type != SSL3_RT_ALERT
751 && thisrr->type != SSL3_RT_HANDSHAKE) {
196f2cbb
MC
752 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
753 SSL_R_BAD_RECORD_TYPE);
754 return -1;
e60ce9c4 755 }
ad5100bc
MC
756 if (s->msg_callback)
757 s->msg_callback(0, s->version, SSL3_RT_INNER_CONTENT_TYPE,
758 &thisrr->data[end], 1, s, s->msg_callback_arg);
e60ce9c4
MC
759 }
760
9010b7bc
MC
761 /*
762 * TLSv1.3 alert and handshake records are required to be non-zero in
763 * length.
764 */
fc4c15fa
MC
765 if (SSL_IS_TLS13(s)
766 && (thisrr->type == SSL3_RT_HANDSHAKE
767 || thisrr->type == SSL3_RT_ALERT)
768 && thisrr->length == 0) {
196f2cbb
MC
769 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
770 SSL_R_BAD_LENGTH);
771 return -1;
fc4c15fa
MC
772 }
773
c35e921f 774 if (thisrr->length > SSL3_RT_MAX_PLAIN_LENGTH && !BIO_get_ktls_recv(s->rbio)) {
196f2cbb
MC
775 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
776 SSL_R_DATA_LENGTH_TOO_LONG);
777 return -1;
fe589e61 778 }
fe589e61 779
cf72c757
F
780 /* If received packet overflows current Max Fragment Length setting */
781 if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
c35e921f
BP
782 && thisrr->length > GET_MAX_FRAGMENT_LENGTH(s->session)
783 && !BIO_get_ktls_recv(s->rbio)) {
196f2cbb
MC
784 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
785 SSL_R_DATA_LENGTH_TOO_LONG);
786 return -1;
cf72c757
F
787 }
788
88858868 789 thisrr->off = 0;
94777c9c
MC
790 /*-
791 * So at this point the following is true
88858868
MC
792 * thisrr->type is the type of record
793 * thisrr->length == number of bytes in record
794 * thisrr->off == offset to first valid byte
795 * thisrr->data == where to take bytes from, increment after use :-).
94777c9c 796 */
fe589e61 797
94777c9c 798 /* just read a 0 length packet */
88858868 799 if (thisrr->length == 0) {
255cfeac
MC
800 RECORD_LAYER_inc_empty_record_count(&s->rlayer);
801 if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
a230b26e 802 > MAX_EMPTY_RECORDS) {
196f2cbb
MC
803 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
804 SSL_R_RECORD_TOO_SMALL);
805 return -1;
94777c9c 806 }
255cfeac
MC
807 } else {
808 RECORD_LAYER_reset_empty_record_count(&s->rlayer);
fe589e61 809 }
94777c9c 810 }
fe589e61 811
70ef40a0
MC
812 if (s->early_data_state == SSL_EARLY_DATA_READING) {
813 thisrr = &rr[0];
814 if (thisrr->type == SSL3_RT_APPLICATION_DATA
196f2cbb
MC
815 && !early_data_count_ok(s, thisrr->length, 0, 0)) {
816 /* SSLfatal already called */
817 return -1;
818 }
70ef40a0
MC
819 }
820
94777c9c
MC
821 RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
822 return 1;
fe589e61
MC
823}
824
94777c9c 825int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
fe589e61
MC
826{
827#ifndef OPENSSL_NO_COMP
828 int i;
fe589e61 829
0220fee4
MC
830 if (rr->comp == NULL) {
831 rr->comp = (unsigned char *)
832 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
833 }
834 if (rr->comp == NULL)
835 return 0;
836
eda75751 837 /* TODO(size_t): Convert this call */
fe589e61 838 i = COMP_expand_block(ssl->expand, rr->comp,
a230b26e 839 SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
fe589e61 840 if (i < 0)
0220fee4 841 return 0;
fe589e61
MC
842 else
843 rr->length = i;
844 rr->data = rr->comp;
845#endif
0220fee4 846 return 1;
fe589e61
MC
847}
848
d102d9df 849int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
fe589e61
MC
850{
851#ifndef OPENSSL_NO_COMP
852 int i;
fe589e61 853
eda75751 854 /* TODO(size_t): Convert this call */
fe589e61 855 i = COMP_compress_block(ssl->compress, wr->data,
c7c42022 856 (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
fe589e61
MC
857 wr->input, (int)wr->length);
858 if (i < 0)
26a7d938 859 return 0;
fe589e61
MC
860 else
861 wr->length = i;
862
863 wr->input = wr->data;
864#endif
208fb891 865 return 1;
fe589e61
MC
866}
867
02a36fda 868/*-
921d84a0
MC
869 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|. Will call
870 * SSLfatal() for internal errors, but not otherwise.
02a36fda
MC
871 *
872 * Returns:
873 * 0: (in non-constant time) if the record is publically invalid (i.e. too
874 * short etc).
875 * 1: if the record's padding is valid / the encryption was successful.
876 * -1: if the record's padding is invalid or, if sending, an internal error
877 * occurred.
878 */
aebe9e39 879int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int sending)
02a36fda
MC
880{
881 SSL3_RECORD *rec;
882 EVP_CIPHER_CTX *ds;
eda75751 883 size_t l, i;
72716e79
MC
884 size_t bs, mac_size = 0;
885 int imac_size;
02a36fda
MC
886 const EVP_CIPHER *enc;
887
d102d9df 888 rec = inrecs;
37205971
MC
889 /*
890 * We shouldn't ever be called with more than one record in the SSLv3 case
891 */
892 if (n_recs != 1)
893 return 0;
aebe9e39 894 if (sending) {
02a36fda 895 ds = s->enc_write_ctx;
02a36fda
MC
896 if (s->enc_write_ctx == NULL)
897 enc = NULL;
898 else
899 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
900 } else {
901 ds = s->enc_read_ctx;
02a36fda
MC
902 if (s->enc_read_ctx == NULL)
903 enc = NULL;
904 else
905 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
906 }
907
908 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
909 memmove(rec->data, rec->input, rec->length);
910 rec->input = rec->data;
911 } else {
912 l = rec->length;
eda75751 913 /* TODO(size_t): Convert this call */
846ec07d 914 bs = EVP_CIPHER_CTX_block_size(ds);
02a36fda
MC
915
916 /* COMPRESS */
917
aebe9e39 918 if ((bs != 1) && sending) {
c08d12ca 919 i = bs - (l % bs);
02a36fda
MC
920
921 /* we need to add 'i-1' padding bytes */
922 l += i;
923 /*
924 * the last of these zero bytes will be overwritten with the
925 * padding length.
926 */
927 memset(&rec->input[rec->length], 0, i);
928 rec->length += i;
c08d12ca 929 rec->input[l - 1] = (unsigned char)(i - 1);
02a36fda
MC
930 }
931
aebe9e39 932 if (!sending) {
02a36fda
MC
933 if (l == 0 || l % bs != 0)
934 return 0;
935 /* otherwise, rec->length >= bs */
936 }
937
eda75751 938 /* TODO(size_t): Convert this call */
c08d12ca 939 if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1)
02a36fda
MC
940 return -1;
941
72716e79
MC
942 if (EVP_MD_CTX_md(s->read_hash) != NULL) {
943 /* TODO(size_t): convert me */
944 imac_size = EVP_MD_CTX_size(s->read_hash);
921d84a0
MC
945 if (imac_size < 0) {
946 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_ENC,
947 ERR_R_INTERNAL_ERROR);
72716e79 948 return -1;
921d84a0 949 }
72716e79
MC
950 mac_size = (size_t)imac_size;
951 }
aebe9e39 952 if ((bs != 1) && !sending)
a773b52a 953 return ssl3_cbc_remove_padding(rec, bs, mac_size);
02a36fda 954 }
208fb891 955 return 1;
02a36fda
MC
956}
957
72716e79 958#define MAX_PADDING 256
02a36fda 959/*-
921d84a0
MC
960 * tls1_enc encrypts/decrypts |n_recs| in |recs|. Will call SSLfatal() for
961 * internal errors, but not otherwise.
02a36fda
MC
962 *
963 * Returns:
964 * 0: (in non-constant time) if the record is publically invalid (i.e. too
965 * short etc).
966 * 1: if the record's padding is valid / the encryption was successful.
967 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
968 * an internal error occurred.
969 */
aebe9e39 970int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
02a36fda 971{
02a36fda 972 EVP_CIPHER_CTX *ds;
d102d9df
MC
973 size_t reclen[SSL_MAX_PIPELINES];
974 unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
72716e79
MC
975 int i, pad = 0, ret, tmpr;
976 size_t bs, mac_size = 0, ctr, padnum, loop;
977 unsigned char padval;
978 int imac_size;
02a36fda
MC
979 const EVP_CIPHER *enc;
980
921d84a0
MC
981 if (n_recs == 0) {
982 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
983 ERR_R_INTERNAL_ERROR);
a3004c82 984 return 0;
921d84a0 985 }
a3004c82 986
aebe9e39 987 if (sending) {
02a36fda
MC
988 if (EVP_MD_CTX_md(s->write_hash)) {
989 int n = EVP_MD_CTX_size(s->write_hash);
380a522f 990 if (!ossl_assert(n >= 0)) {
921d84a0
MC
991 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
992 ERR_R_INTERNAL_ERROR);
380a522f
MC
993 return -1;
994 }
02a36fda
MC
995 }
996 ds = s->enc_write_ctx;
02a36fda
MC
997 if (s->enc_write_ctx == NULL)
998 enc = NULL;
999 else {
1000 int ivlen;
1001 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
1002 /* For TLSv1.1 and later explicit IV */
1003 if (SSL_USE_EXPLICIT_IV(s)
1004 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
1005 ivlen = EVP_CIPHER_iv_length(enc);
1006 else
1007 ivlen = 0;
1008 if (ivlen > 1) {
37205971 1009 for (ctr = 0; ctr < n_recs; ctr++) {
d102d9df
MC
1010 if (recs[ctr].data != recs[ctr].input) {
1011 /*
1012 * we can't write into the input stream: Can this ever
1013 * happen?? (steve)
1014 */
921d84a0
MC
1015 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1016 ERR_R_INTERNAL_ERROR);
d102d9df 1017 return -1;
16cfc2c9 1018 } else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
921d84a0
MC
1019 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1020 ERR_R_INTERNAL_ERROR);
d102d9df
MC
1021 return -1;
1022 }
1023 }
02a36fda
MC
1024 }
1025 }
1026 } else {
1027 if (EVP_MD_CTX_md(s->read_hash)) {
1028 int n = EVP_MD_CTX_size(s->read_hash);
380a522f 1029 if (!ossl_assert(n >= 0)) {
921d84a0
MC
1030 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1031 ERR_R_INTERNAL_ERROR);
380a522f
MC
1032 return -1;
1033 }
02a36fda
MC
1034 }
1035 ds = s->enc_read_ctx;
02a36fda
MC
1036 if (s->enc_read_ctx == NULL)
1037 enc = NULL;
1038 else
1039 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
1040 }
1041
02a36fda 1042 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
37205971 1043 for (ctr = 0; ctr < n_recs; ctr++) {
d102d9df
MC
1044 memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
1045 recs[ctr].input = recs[ctr].data;
1046 }
02a36fda
MC
1047 ret = 1;
1048 } else {
d102d9df
MC
1049 bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
1050
37205971 1051 if (n_recs > 1) {
e8aa8b6c 1052 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
a230b26e 1053 & EVP_CIPH_FLAG_PIPELINE)) {
d102d9df
MC
1054 /*
1055 * We shouldn't have been called with pipeline data if the
1056 * cipher doesn't support pipelining
1057 */
921d84a0
MC
1058 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1059 SSL_R_PIPELINE_FAILURE);
d102d9df
MC
1060 return -1;
1061 }
1062 }
37205971 1063 for (ctr = 0; ctr < n_recs; ctr++) {
d102d9df
MC
1064 reclen[ctr] = recs[ctr].length;
1065
1066 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
a230b26e 1067 & EVP_CIPH_FLAG_AEAD_CIPHER) {
d102d9df
MC
1068 unsigned char *seq;
1069
aebe9e39 1070 seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
d102d9df
MC
1071 : RECORD_LAYER_get_read_sequence(&s->rlayer);
1072
1073 if (SSL_IS_DTLS(s)) {
1074 /* DTLS does not support pipelining */
1075 unsigned char dtlsseq[9], *p = dtlsseq;
1076
aebe9e39 1077 s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
d102d9df
MC
1078 DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
1079 memcpy(p, &seq[2], 6);
1080 memcpy(buf[ctr], dtlsseq, 8);
1081 } else {
1082 memcpy(buf[ctr], seq, 8);
1083 for (i = 7; i >= 0; i--) { /* increment */
1084 ++seq[i];
1085 if (seq[i] != 0)
1086 break;
1087 }
1088 }
02a36fda 1089
d102d9df
MC
1090 buf[ctr][8] = recs[ctr].type;
1091 buf[ctr][9] = (unsigned char)(s->version >> 8);
1092 buf[ctr][10] = (unsigned char)(s->version);
c08d12ca
MC
1093 buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
1094 buf[ctr][12] = (unsigned char)(recs[ctr].length & 0xff);
d102d9df
MC
1095 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
1096 EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
921d84a0
MC
1097 if (pad <= 0) {
1098 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1099 ERR_R_INTERNAL_ERROR);
d102d9df 1100 return -1;
921d84a0 1101 }
d102d9df 1102
aebe9e39 1103 if (sending) {
d102d9df
MC
1104 reclen[ctr] += pad;
1105 recs[ctr].length += pad;
02a36fda 1106 }
02a36fda 1107
aebe9e39 1108 } else if ((bs != 1) && sending) {
c08d12ca 1109 padnum = bs - (reclen[ctr] % bs);
02a36fda 1110
d102d9df 1111 /* Add weird padding of upto 256 bytes */
02a36fda 1112
921d84a0
MC
1113 if (padnum > MAX_PADDING) {
1114 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1115 ERR_R_INTERNAL_ERROR);
72716e79 1116 return -1;
921d84a0 1117 }
72716e79 1118 /* we need to add 'padnum' padding bytes of value padval */
c08d12ca 1119 padval = (unsigned char)(padnum - 1);
72716e79
MC
1120 for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
1121 recs[ctr].input[loop] = padval;
1122 reclen[ctr] += padnum;
1123 recs[ctr].length += padnum;
d102d9df
MC
1124 }
1125
aebe9e39 1126 if (!sending) {
d102d9df
MC
1127 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0)
1128 return 0;
1129 }
02a36fda 1130 }
37205971 1131 if (n_recs > 1) {
d102d9df 1132 unsigned char *data[SSL_MAX_PIPELINES];
02a36fda 1133
d102d9df 1134 /* Set the output buffers */
e8aa8b6c 1135 for (ctr = 0; ctr < n_recs; ctr++) {
d102d9df
MC
1136 data[ctr] = recs[ctr].data;
1137 }
1138 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
c08d12ca 1139 (int)n_recs, data) <= 0) {
921d84a0
MC
1140 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1141 SSL_R_PIPELINE_FAILURE);
1142 return -1;
d102d9df
MC
1143 }
1144 /* Set the input buffers */
e8aa8b6c 1145 for (ctr = 0; ctr < n_recs; ctr++) {
d102d9df
MC
1146 data[ctr] = recs[ctr].input;
1147 }
1148 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
c08d12ca 1149 (int)n_recs, data) <= 0
d102d9df 1150 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
c08d12ca 1151 (int)n_recs, reclen) <= 0) {
921d84a0
MC
1152 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1153 SSL_R_PIPELINE_FAILURE);
d102d9df
MC
1154 return -1;
1155 }
02a36fda
MC
1156 }
1157
c08d12ca
MC
1158 /* TODO(size_t): Convert this call */
1159 tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
1160 (unsigned int)reclen[0]);
d102d9df 1161 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
a230b26e 1162 & EVP_CIPH_FLAG_CUSTOM_CIPHER)
72716e79
MC
1163 ? (tmpr < 0)
1164 : (tmpr == 0))
02a36fda 1165 return -1; /* AEAD can fail to verify MAC */
921d84a0 1166
aebe9e39 1167 if (sending == 0) {
e75c5a79 1168 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
37205971 1169 for (ctr = 0; ctr < n_recs; ctr++) {
d102d9df
MC
1170 recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1171 recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1172 recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
1173 }
e75c5a79 1174 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
37205971 1175 for (ctr = 0; ctr < n_recs; ctr++) {
d102d9df
MC
1176 recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1177 recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1178 recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
1179 }
e75c5a79 1180 }
02a36fda 1181 }
02a36fda
MC
1182
1183 ret = 1;
28a31a0a 1184 if (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL) {
72716e79 1185 imac_size = EVP_MD_CTX_size(s->read_hash);
921d84a0
MC
1186 if (imac_size < 0) {
1187 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1188 ERR_R_INTERNAL_ERROR);
72716e79 1189 return -1;
921d84a0 1190 }
72716e79
MC
1191 mac_size = (size_t)imac_size;
1192 }
aebe9e39 1193 if ((bs != 1) && !sending) {
94777c9c 1194 int tmpret;
37205971 1195 for (ctr = 0; ctr < n_recs; ctr++) {
94777c9c 1196 tmpret = tls1_cbc_remove_padding(s, &recs[ctr], bs, mac_size);
f9cf774c
MC
1197 /*
1198 * If tmpret == 0 then this means publicly invalid so we can
1199 * short circuit things here. Otherwise we must respect constant
1200 * time behaviour.
1201 */
1202 if (tmpret == 0)
1203 return 0;
1204 ret = constant_time_select_int(constant_time_eq_int(tmpret, 1),
1205 ret, -1);
94777c9c
MC
1206 }
1207 }
aebe9e39 1208 if (pad && !sending) {
37205971 1209 for (ctr = 0; ctr < n_recs; ctr++) {
94777c9c
MC
1210 recs[ctr].length -= pad;
1211 }
d102d9df 1212 }
02a36fda
MC
1213 }
1214 return ret;
1215}
1216
aebe9e39 1217int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
02a36fda 1218{
02a36fda 1219 unsigned char *mac_sec, *seq;
02a36fda
MC
1220 const EVP_MD_CTX *hash;
1221 unsigned char *p, rec_char;
1222 size_t md_size;
c08d12ca 1223 size_t npad;
02a36fda
MC
1224 int t;
1225
aebe9e39 1226 if (sending) {
02a36fda 1227 mac_sec = &(ssl->s3->write_mac_secret[0]);
de07f311 1228 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
02a36fda
MC
1229 hash = ssl->write_hash;
1230 } else {
02a36fda 1231 mac_sec = &(ssl->s3->read_mac_secret[0]);
de07f311 1232 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
02a36fda
MC
1233 hash = ssl->read_hash;
1234 }
1235
1236 t = EVP_MD_CTX_size(hash);
1237 if (t < 0)
a14aa99b 1238 return 0;
02a36fda
MC
1239 md_size = t;
1240 npad = (48 / md_size) * md_size;
1241
aebe9e39 1242 if (!sending &&
02a36fda
MC
1243 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1244 ssl3_cbc_record_digest_supported(hash)) {
1245 /*
1246 * This is a CBC-encrypted record. We must avoid leaking any
1247 * timing-side channel information about how many blocks of data we
1248 * are hashing because that gives an attacker a timing-oracle.
1249 */
1250
1251 /*-
1252 * npad is, at most, 48 bytes and that's with MD5:
1253 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
1254 *
1255 * With SHA-1 (the largest hash speced for SSLv3) the hash size
1256 * goes up 4, but npad goes down by 8, resulting in a smaller
1257 * total size.
1258 */
1259 unsigned char header[75];
c08d12ca 1260 size_t j = 0;
02a36fda
MC
1261 memcpy(header + j, mac_sec, md_size);
1262 j += md_size;
1263 memcpy(header + j, ssl3_pad_1, npad);
1264 j += npad;
1265 memcpy(header + j, seq, 8);
1266 j += 8;
1267 header[j++] = rec->type;
c08d12ca
MC
1268 header[j++] = (unsigned char)(rec->length >> 8);
1269 header[j++] = (unsigned char)(rec->length & 0xff);
02a36fda
MC
1270
1271 /* Final param == is SSLv3 */
5f3d93e4
MC
1272 if (ssl3_cbc_digest_record(hash,
1273 md, &md_size,
1274 header, rec->input,
1275 rec->length + md_size, rec->orig_len,
1276 mac_sec, md_size, 1) <= 0)
a14aa99b 1277 return 0;
02a36fda
MC
1278 } else {
1279 unsigned int md_size_u;
1280 /* Chop the digest off the end :-) */
bfb0641f 1281 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
6e59a892
RL
1282
1283 if (md_ctx == NULL)
a14aa99b 1284 return 0;
02a36fda 1285
02a36fda 1286 rec_char = rec->type;
02a36fda
MC
1287 p = md;
1288 s2n(rec->length, p);
6e59a892 1289 if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
a230b26e
EK
1290 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
1291 || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
1292 || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
1293 || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
1294 || EVP_DigestUpdate(md_ctx, md, 2) <= 0
1295 || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
1296 || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
1297 || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
1298 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
1299 || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
1300 || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
1301 || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
302d1697 1302 EVP_MD_CTX_free(md_ctx);
a14aa99b 1303 return 0;
5f3d93e4 1304 }
02a36fda 1305
bfb0641f 1306 EVP_MD_CTX_free(md_ctx);
02a36fda
MC
1307 }
1308
1309 ssl3_record_sequence_update(seq);
a14aa99b 1310 return 1;
02a36fda
MC
1311}
1312
aebe9e39 1313int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
02a36fda 1314{
02a36fda
MC
1315 unsigned char *seq;
1316 EVP_MD_CTX *hash;
1317 size_t md_size;
1318 int i;
6e59a892 1319 EVP_MD_CTX *hmac = NULL, *mac_ctx;
02a36fda 1320 unsigned char header[13];
aebe9e39 1321 int stream_mac = (sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
02a36fda
MC
1322 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
1323 int t;
1324
aebe9e39 1325 if (sending) {
de07f311 1326 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
02a36fda
MC
1327 hash = ssl->write_hash;
1328 } else {
de07f311 1329 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
02a36fda
MC
1330 hash = ssl->read_hash;
1331 }
1332
1333 t = EVP_MD_CTX_size(hash);
380a522f
MC
1334 if (!ossl_assert(t >= 0))
1335 return 0;
02a36fda
MC
1336 md_size = t;
1337
1338 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1339 if (stream_mac) {
1340 mac_ctx = hash;
1341 } else {
bfb0641f 1342 hmac = EVP_MD_CTX_new();
a080c3e8
BE
1343 if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash)) {
1344 EVP_MD_CTX_free(hmac);
a14aa99b 1345 return 0;
a080c3e8 1346 }
6e59a892 1347 mac_ctx = hmac;
02a36fda
MC
1348 }
1349
1350 if (SSL_IS_DTLS(ssl)) {
1351 unsigned char dtlsseq[8], *p = dtlsseq;
1352
aebe9e39 1353 s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
78a39fe7 1354 DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
02a36fda
MC
1355 memcpy(p, &seq[2], 6);
1356
1357 memcpy(header, dtlsseq, 8);
1358 } else
1359 memcpy(header, seq, 8);
1360
1361 header[8] = rec->type;
1362 header[9] = (unsigned char)(ssl->version >> 8);
1363 header[10] = (unsigned char)(ssl->version);
348240c6
MC
1364 header[11] = (unsigned char)(rec->length >> 8);
1365 header[12] = (unsigned char)(rec->length & 0xff);
02a36fda 1366
aebe9e39 1367 if (!sending && !SSL_READ_ETM(ssl) &&
02a36fda
MC
1368 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1369 ssl3_cbc_record_digest_supported(mac_ctx)) {
1370 /*
1371 * This is a CBC-encrypted record. We must avoid leaking any
1372 * timing-side channel information about how many blocks of data we
1373 * are hashing because that gives an attacker a timing-oracle.
1374 */
1375 /* Final param == not SSLv3 */
5f3d93e4
MC
1376 if (ssl3_cbc_digest_record(mac_ctx,
1377 md, &md_size,
1378 header, rec->input,
1379 rec->length + md_size, rec->orig_len,
1380 ssl->s3->read_mac_secret,
1381 ssl->s3->read_mac_secret_size, 0) <= 0) {
bfb0641f 1382 EVP_MD_CTX_free(hmac);
aabe3a35 1383 return 0;
5f3d93e4 1384 }
02a36fda 1385 } else {
eda75751 1386 /* TODO(size_t): Convert these calls */
5f3d93e4 1387 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
a230b26e
EK
1388 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
1389 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
bfb0641f 1390 EVP_MD_CTX_free(hmac);
a14aa99b 1391 return 0;
5f3d93e4 1392 }
02a36fda
MC
1393 }
1394
bfb0641f 1395 EVP_MD_CTX_free(hmac);
5f3d93e4 1396
49b26f54
RL
1397 OSSL_TRACE_BEGIN(TLS) {
1398 BIO_printf(trc_out, "seq:\n");
1399 BIO_dump_indent(trc_out, seq, 8, 4);
1400 BIO_printf(trc_out, "rec:\n");
1401 BIO_dump_indent(trc_out, rec->data, rec->length, 4);
1402 } OSSL_TRACE_END(TLS);
02a36fda
MC
1403
1404 if (!SSL_IS_DTLS(ssl)) {
1405 for (i = 7; i >= 0; i--) {
1406 ++seq[i];
1407 if (seq[i] != 0)
1408 break;
1409 }
1410 }
49b26f54
RL
1411 OSSL_TRACE_BEGIN(TLS) {
1412 BIO_printf(trc_out, "md:\n");
1413 BIO_dump_indent(trc_out, md, md_size, 4);
1414 } OSSL_TRACE_END(TLS);
a14aa99b 1415 return 1;
02a36fda
MC
1416}
1417
1418/*-
1419 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1420 * record in |rec| by updating |rec->length| in constant time.
1421 *
1422 * block_size: the block size of the cipher used to encrypt the record.
1423 * returns:
1424 * 0: (in non-constant time) if the record is publicly invalid.
1425 * 1: if the padding was valid
1426 * -1: otherwise.
1427 */
a773b52a 1428int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
72716e79 1429 size_t block_size, size_t mac_size)
02a36fda 1430{
72716e79 1431 size_t padding_length;
2688e7a0 1432 size_t good;
72716e79 1433 const size_t overhead = 1 /* padding length byte */ + mac_size;
02a36fda
MC
1434
1435 /*
1436 * These lengths are all public so we can test them in non-constant time.
1437 */
1438 if (overhead > rec->length)
1439 return 0;
1440
1441 padding_length = rec->data[rec->length - 1];
2688e7a0 1442 good = constant_time_ge_s(rec->length, padding_length + overhead);
02a36fda 1443 /* SSLv3 requires that the padding is minimal. */
2688e7a0 1444 good &= constant_time_ge_s(block_size, padding_length + 1);
02a36fda 1445 rec->length -= good & (padding_length + 1);
2688e7a0 1446 return constant_time_select_int_s(good, 1, -1);
02a36fda
MC
1447}
1448
1449/*-
1450 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1451 * record in |rec| in constant time and returns 1 if the padding is valid and
1452 * -1 otherwise. It also removes any explicit IV from the start of the record
1453 * without leaking any timing about whether there was enough space after the
1454 * padding was removed.
1455 *
1456 * block_size: the block size of the cipher used to encrypt the record.
1457 * returns:
1458 * 0: (in non-constant time) if the record is publicly invalid.
1459 * 1: if the padding was valid
1460 * -1: otherwise.
1461 */
1462int tls1_cbc_remove_padding(const SSL *s,
1463 SSL3_RECORD *rec,
72716e79 1464 size_t block_size, size_t mac_size)
02a36fda 1465{
2688e7a0 1466 size_t good;
72716e79
MC
1467 size_t padding_length, to_check, i;
1468 const size_t overhead = 1 /* padding length byte */ + mac_size;
02a36fda
MC
1469 /* Check if version requires explicit IV */
1470 if (SSL_USE_EXPLICIT_IV(s)) {
1471 /*
1472 * These lengths are all public so we can test them in non-constant
1473 * time.
1474 */
1475 if (overhead + block_size > rec->length)
1476 return 0;
1477 /* We can now safely skip explicit IV */
1478 rec->data += block_size;
1479 rec->input += block_size;
1480 rec->length -= block_size;
1481 rec->orig_len -= block_size;
1482 } else if (overhead > rec->length)
1483 return 0;
1484
1485 padding_length = rec->data[rec->length - 1];
1486
a230b26e
EK
1487 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) &
1488 EVP_CIPH_FLAG_AEAD_CIPHER) {
02a36fda
MC
1489 /* padding is already verified */
1490 rec->length -= padding_length + 1;
1491 return 1;
1492 }
1493
2688e7a0 1494 good = constant_time_ge_s(rec->length, overhead + padding_length);
02a36fda
MC
1495 /*
1496 * The padding consists of a length byte at the end of the record and
1497 * then that many bytes of padding, all with the same value as the length
1498 * byte. Thus, with the length byte included, there are i+1 bytes of
1499 * padding. We can't check just |padding_length+1| bytes because that
1500 * leaks decrypted information. Therefore we always have to check the
1501 * maximum amount of padding possible. (Again, the length of the record
1502 * is public information so we can use it.)
1503 */
eea8723c
AL
1504 to_check = 256; /* maximum amount of padding, inc length byte. */
1505 if (to_check > rec->length)
1506 to_check = rec->length;
02a36fda
MC
1507
1508 for (i = 0; i < to_check; i++) {
2688e7a0 1509 unsigned char mask = constant_time_ge_8_s(padding_length, i);
02a36fda
MC
1510 unsigned char b = rec->data[rec->length - 1 - i];
1511 /*
1512 * The final |padding_length+1| bytes should all have the value
1513 * |padding_length|. Therefore the XOR should be zero.
1514 */
1515 good &= ~(mask & (padding_length ^ b));
1516 }
1517
1518 /*
1519 * If any of the final |padding_length+1| bytes had the wrong value, one
1520 * or more of the lower eight bits of |good| will be cleared.
1521 */
2688e7a0 1522 good = constant_time_eq_s(0xff, good & 0xff);
02a36fda
MC
1523 rec->length -= good & (padding_length + 1);
1524
2688e7a0 1525 return constant_time_select_int_s(good, 1, -1);
02a36fda
MC
1526}
1527
1528/*-
1529 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1530 * constant time (independent of the concrete value of rec->length, which may
1531 * vary within a 256-byte window).
1532 *
1533 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1534 * this function.
1535 *
1536 * On entry:
1537 * rec->orig_len >= md_size
1538 * md_size <= EVP_MAX_MD_SIZE
1539 *
1540 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1541 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1542 * a single or pair of cache-lines, then the variable memory accesses don't
1543 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1544 * not multi-core and are not considered vulnerable to cache-timing attacks.
1545 */
1546#define CBC_MAC_ROTATE_IN_PLACE
1547
380a522f 1548int ssl3_cbc_copy_mac(unsigned char *out,
72716e79 1549 const SSL3_RECORD *rec, size_t md_size)
02a36fda
MC
1550{
1551#if defined(CBC_MAC_ROTATE_IN_PLACE)
1552 unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
1553 unsigned char *rotated_mac;
1554#else
1555 unsigned char rotated_mac[EVP_MAX_MD_SIZE];
1556#endif
1557
1558 /*
1559 * mac_end is the index of |rec->data| just after the end of the MAC.
1560 */
72716e79
MC
1561 size_t mac_end = rec->length;
1562 size_t mac_start = mac_end - md_size;
8f77fab8 1563 size_t in_mac;
02a36fda
MC
1564 /*
1565 * scan_start contains the number of bytes that we can ignore because the
1566 * MAC's position can only vary by 255 bytes.
1567 */
72716e79 1568 size_t scan_start = 0;
2688e7a0 1569 size_t i, j;
2688e7a0 1570 size_t rotate_offset;
02a36fda 1571
380a522f
MC
1572 if (!ossl_assert(rec->orig_len >= md_size
1573 && md_size <= EVP_MAX_MD_SIZE))
1574 return 0;
02a36fda
MC
1575
1576#if defined(CBC_MAC_ROTATE_IN_PLACE)
1577 rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
1578#endif
1579
1580 /* This information is public so it's safe to branch based on it. */
1581 if (rec->orig_len > md_size + 255 + 1)
1582 scan_start = rec->orig_len - (md_size + 255 + 1);
02a36fda 1583
8f77fab8
AP
1584 in_mac = 0;
1585 rotate_offset = 0;
02a36fda
MC
1586 memset(rotated_mac, 0, md_size);
1587 for (i = scan_start, j = 0; i < rec->orig_len; i++) {
8f77fab8
AP
1588 size_t mac_started = constant_time_eq_s(i, mac_start);
1589 size_t mac_ended = constant_time_lt_s(i, mac_end);
02a36fda 1590 unsigned char b = rec->data[i];
8f77fab8
AP
1591
1592 in_mac |= mac_started;
1593 in_mac &= mac_ended;
1594 rotate_offset |= j & mac_started;
1595 rotated_mac[j++] |= b & in_mac;
2688e7a0 1596 j &= constant_time_lt_s(j, md_size);
02a36fda
MC
1597 }
1598
1599 /* Now rotate the MAC */
1600#if defined(CBC_MAC_ROTATE_IN_PLACE)
1601 j = 0;
1602 for (i = 0; i < md_size; i++) {
1603 /* in case cache-line is 32 bytes, touch second line */
1604 ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
1605 out[j++] = rotated_mac[rotate_offset++];
2688e7a0 1606 rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
02a36fda
MC
1607 }
1608#else
1609 memset(out, 0, md_size);
1610 rotate_offset = md_size - rotate_offset;
2688e7a0 1611 rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
02a36fda
MC
1612 for (i = 0; i < md_size; i++) {
1613 for (j = 0; j < md_size; j++)
2688e7a0 1614 out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);
02a36fda 1615 rotate_offset++;
2688e7a0 1616 rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
02a36fda
MC
1617 }
1618#endif
380a522f
MC
1619
1620 return 1;
02a36fda
MC
1621}
1622
1fb9fdc3 1623int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
fe589e61 1624{
c2853382 1625 int i;
fe589e61
MC
1626 int enc_err;
1627 SSL_SESSION *sess;
1628 SSL3_RECORD *rr;
72716e79
MC
1629 int imac_size;
1630 size_t mac_size;
fe589e61
MC
1631 unsigned char md[EVP_MAX_MD_SIZE];
1632
1633 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1634 sess = s->session;
1635
1636 /*
1637 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1638 * and we have that many bytes in s->packet
1639 */
7a7048af 1640 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
fe589e61
MC
1641
1642 /*
1643 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1644 * at rr->length bytes, which need to be copied into rr->data by either
1645 * the decryption or by the decompression When the data is 'copied' into
1646 * the rr->data buffer, rr->input will be pointed at the new buffer
1647 */
1648
1649 /*
1650 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1651 * bytes of encrypted compressed stuff.
1652 */
1653
1654 /* check is not needed I believe */
1655 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
c2853382
MC
1656 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
1657 SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1658 return 0;
fe589e61
MC
1659 }
1660
1661 /* decrypt in place in 'rr->input' */
1662 rr->data = rr->input;
1663 rr->orig_len = rr->length;
1664
28a31a0a 1665 if (SSL_READ_ETM(s) && s->read_hash) {
e23d5071
DW
1666 unsigned char *mac;
1667 mac_size = EVP_MD_CTX_size(s->read_hash);
380a522f 1668 if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
c2853382
MC
1669 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
1670 ERR_R_INTERNAL_ERROR);
1671 return 0;
380a522f 1672 }
e23d5071 1673 if (rr->orig_len < mac_size) {
c2853382
MC
1674 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
1675 SSL_R_LENGTH_TOO_SHORT);
1676 return 0;
e23d5071
DW
1677 }
1678 rr->length -= mac_size;
1679 mac = rr->data + rr->length;
1680 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
a14aa99b 1681 if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
c2853382 1682 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_DTLS1_PROCESS_RECORD,
e23d5071 1683 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
c2853382 1684 return 0;
e23d5071
DW
1685 }
1686 }
1687
d102d9df 1688 enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
fe589e61
MC
1689 /*-
1690 * enc_err is:
1691 * 0: (in non-constant time) if the record is publically invalid.
1692 * 1: if the padding is valid
1693 * -1: if the padding is invalid
1694 */
1695 if (enc_err == 0) {
921d84a0
MC
1696 if (ossl_statem_in_error(s)) {
1697 /* SSLfatal() got called */
1698 return 0;
1699 }
fe589e61
MC
1700 /* For DTLS we simply ignore bad packets. */
1701 rr->length = 0;
7a7048af 1702 RECORD_LAYER_reset_packet_length(&s->rlayer);
c2853382 1703 return 0;
fe589e61 1704 }
49b26f54
RL
1705 OSSL_TRACE_BEGIN(TLS) {
1706 BIO_printf(trc_out, "dec %ld\n", rr->length);
1707 BIO_dump_indent(trc_out, rr->data, rr->length, 4);
1708 } OSSL_TRACE_END(TLS);
fe589e61
MC
1709
1710 /* r->length is now the compressed data plus mac */
28a31a0a 1711 if ((sess != NULL) && !SSL_READ_ETM(s) &&
fe589e61
MC
1712 (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
1713 /* s->read_hash != NULL => mac_size != -1 */
1714 unsigned char *mac = NULL;
1715 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
72716e79
MC
1716
1717 /* TODO(size_t): Convert this to do size_t properly */
1718 imac_size = EVP_MD_CTX_size(s->read_hash);
1719 if (imac_size < 0) {
c2853382
MC
1720 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
1721 ERR_LIB_EVP);
1722 return 0;
72716e79
MC
1723 }
1724 mac_size = (size_t)imac_size;
380a522f 1725 if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
c2853382
MC
1726 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
1727 ERR_R_INTERNAL_ERROR);
1728 return 0;
380a522f 1729 }
fe589e61
MC
1730
1731 /*
1732 * orig_len is the length of the record before any padding was
1733 * removed. This is public information, as is the MAC in use,
1734 * therefore we can safely process the record in a different amount
1735 * of time if it's too short to possibly contain a MAC.
1736 */
1737 if (rr->orig_len < mac_size ||
1738 /* CBC records must have a padding length byte too. */
1739 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1740 rr->orig_len < mac_size + 1)) {
c2853382
MC
1741 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
1742 SSL_R_LENGTH_TOO_SHORT);
1743 return 0;
fe589e61
MC
1744 }
1745
1746 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
1747 /*
1748 * We update the length so that the TLS header bytes can be
1749 * constructed correctly but we need to extract the MAC in
1750 * constant time from within the record, without leaking the
1751 * contents of the padding bytes.
1752 */
1753 mac = mac_tmp;
380a522f 1754 if (!ssl3_cbc_copy_mac(mac_tmp, rr, mac_size)) {
c2853382
MC
1755 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
1756 ERR_R_INTERNAL_ERROR);
1757 return 0;
380a522f 1758 }
fe589e61
MC
1759 rr->length -= mac_size;
1760 } else {
1761 /*
1762 * In this case there's no padding, so |rec->orig_len| equals
1763 * |rec->length| and we checked that there's enough bytes for
1764 * |mac_size| above.
1765 */
1766 rr->length -= mac_size;
1767 mac = &rr->data[rr->length];
1768 }
1769
d102d9df 1770 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
a14aa99b 1771 if (i == 0 || mac == NULL
72716e79 1772 || CRYPTO_memcmp(md, mac, mac_size) != 0)
fe589e61
MC
1773 enc_err = -1;
1774 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1775 enc_err = -1;
1776 }
1777
1778 if (enc_err < 0) {
1779 /* decryption failed, silently discard message */
1780 rr->length = 0;
7a7048af 1781 RECORD_LAYER_reset_packet_length(&s->rlayer);
c2853382 1782 return 0;
fe589e61
MC
1783 }
1784
1785 /* r->length is now just compressed */
1786 if (s->expand != NULL) {
1787 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
c2853382
MC
1788 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
1789 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1790 return 0;
fe589e61 1791 }
94777c9c 1792 if (!ssl3_do_uncompress(s, rr)) {
c2853382
MC
1793 SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE,
1794 SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
1795 return 0;
fe589e61
MC
1796 }
1797 }
1798
1799 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
c2853382
MC
1800 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
1801 SSL_R_DATA_LENGTH_TOO_LONG);
1802 return 0;
fe589e61
MC
1803 }
1804
1805 rr->off = 0;
1806 /*-
1807 * So at this point the following is true
1808 * ssl->s3->rrec.type is the type of record
1809 * ssl->s3->rrec.length == number of bytes in record
1810 * ssl->s3->rrec.off == offset to first valid byte
1811 * ssl->s3->rrec.data == where to take bytes from, increment
1812 * after use :-).
1813 */
1814
1815 /* we have pulled in a full packet so zero things */
7a7048af 1816 RECORD_LAYER_reset_packet_length(&s->rlayer);
1fb9fdc3
MC
1817
1818 /* Mark receipt of record. */
1819 dtls1_record_bitmap_update(s, bitmap);
1820
208fb891 1821 return 1;
fe589e61
MC
1822}
1823
fe589e61 1824/*
69687aa8 1825 * Retrieve a buffered record that belongs to the current epoch, i.e. processed
fe589e61
MC
1826 */
1827#define dtls1_get_processed_record(s) \
1828 dtls1_retrieve_buffered_record((s), \
cb2ce7ab 1829 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
fe589e61
MC
1830
1831/*-
1832 * Call this to get a new input record.
1833 * It will return <= 0 if more data is needed, normally due to an error
1834 * or non-blocking IO.
1835 * When it finishes, one packet has been decoded and can be found in
1836 * ssl->s3->rrec.type - is the type of record
1837 * ssl->s3->rrec.data, - data
1838 * ssl->s3->rrec.length, - number of bytes
1839 */
1840/* used only by dtls1_read_bytes */
1841int dtls1_get_record(SSL *s)
1842{
1843 int ssl_major, ssl_minor;
8e6d03ca
MC
1844 int rret;
1845 size_t more, n;
fe589e61
MC
1846 SSL3_RECORD *rr;
1847 unsigned char *p = NULL;
1848 unsigned short version;
1849 DTLS1_BITMAP *bitmap;
1850 unsigned int is_next_epoch;
1851
1852 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1853
738ad946 1854 again:
fe589e61
MC
1855 /*
1856 * The epoch may have changed. If so, process all the pending records.
1857 * This is a non-blocking operation.
1858 */
c2853382
MC
1859 if (!dtls1_process_buffered_records(s)) {
1860 /* SSLfatal() already called */
fe589e61 1861 return -1;
c2853382 1862 }
fe589e61
MC
1863
1864 /* if we're renegotiating, then there may be buffered records */
1865 if (dtls1_get_processed_record(s))
1866 return 1;
1867
1868 /* get something from the wire */
738ad946 1869
fe589e61 1870 /* check if we have the header */
295c3f41 1871 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
7a7048af 1872 (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
8e6d03ca
MC
1873 rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1874 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
fe589e61 1875 /* read timeout is handled by dtls1_read_bytes */
c2853382
MC
1876 if (rret <= 0) {
1877 /* SSLfatal() already called if appropriate */
8e6d03ca 1878 return rret; /* error or non-blocking */
c2853382 1879 }
fe589e61
MC
1880
1881 /* this packet contained a partial record, dump it */
a230b26e
EK
1882 if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
1883 DTLS1_RT_HEADER_LENGTH) {
7a7048af 1884 RECORD_LAYER_reset_packet_length(&s->rlayer);
fe589e61
MC
1885 goto again;
1886 }
1887
295c3f41 1888 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
fe589e61 1889
7a7048af 1890 p = RECORD_LAYER_get_packet(&s->rlayer);
fe589e61
MC
1891
1892 if (s->msg_callback)
1893 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1894 s, s->msg_callback_arg);
1895
1896 /* Pull apart the header into the DTLS1_RECORD */
1897 rr->type = *(p++);
1898 ssl_major = *(p++);
1899 ssl_minor = *(p++);
1900 version = (ssl_major << 8) | ssl_minor;
1901
1902 /* sequence number is 64 bits, with top 2 bytes = epoch */
1903 n2s(p, rr->epoch);
1904
de07f311 1905 memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
fe589e61
MC
1906 p += 6;
1907
1908 n2s(p, rr->length);
66fab923 1909 rr->read = 0;
fe589e61 1910
08455bc9
MC
1911 /*
1912 * Lets check the version. We tolerate alerts that don't have the exact
1913 * version number (e.g. because of protocol version errors)
1914 */
1915 if (!s->first_packet && rr->type != SSL3_RT_ALERT) {
fe589e61
MC
1916 if (version != s->version) {
1917 /* unexpected version, silently discard */
1918 rr->length = 0;
66fab923 1919 rr->read = 1;
7a7048af 1920 RECORD_LAYER_reset_packet_length(&s->rlayer);
fe589e61
MC
1921 goto again;
1922 }
1923 }
1924
1925 if ((version & 0xff00) != (s->version & 0xff00)) {
1926 /* wrong version, silently discard record */
1927 rr->length = 0;
66fab923 1928 rr->read = 1;
7a7048af 1929 RECORD_LAYER_reset_packet_length(&s->rlayer);
fe589e61
MC
1930 goto again;
1931 }
1932
1933 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1934 /* record too long, silently discard it */
1935 rr->length = 0;
66fab923 1936 rr->read = 1;
7a7048af 1937 RECORD_LAYER_reset_packet_length(&s->rlayer);
fe589e61
MC
1938 goto again;
1939 }
1940
cf72c757
F
1941 /* If received packet overflows own-client Max Fragment Length setting */
1942 if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
1943 && rr->length > GET_MAX_FRAGMENT_LENGTH(s->session)) {
1944 /* record too long, silently discard it */
1945 rr->length = 0;
66fab923 1946 rr->read = 1;
cf72c757
F
1947 RECORD_LAYER_reset_packet_length(&s->rlayer);
1948 goto again;
1949 }
1950
295c3f41 1951 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
fe589e61
MC
1952 }
1953
295c3f41 1954 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
fe589e61 1955
7a7048af
MC
1956 if (rr->length >
1957 RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
fe589e61 1958 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
8e6d03ca
MC
1959 more = rr->length;
1960 rret = ssl3_read_n(s, more, more, 1, 1, &n);
fe589e61 1961 /* this packet contained a partial record, dump it */
8e6d03ca 1962 if (rret <= 0 || n != more) {
c2853382
MC
1963 if (ossl_statem_in_error(s)) {
1964 /* ssl3_read_n() called SSLfatal() */
1965 return -1;
1966 }
fe589e61 1967 rr->length = 0;
66fab923 1968 rr->read = 1;
7a7048af 1969 RECORD_LAYER_reset_packet_length(&s->rlayer);
fe589e61
MC
1970 goto again;
1971 }
1972
1973 /*
1974 * now n == rr->length, and s->packet_length ==
1975 * DTLS1_RT_HEADER_LENGTH + rr->length
1976 */
1977 }
295c3f41
MC
1978 /* set state for later operations */
1979 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
fe589e61
MC
1980
1981 /* match epochs. NULL means the packet is dropped on the floor */
1982 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1983 if (bitmap == NULL) {
1984 rr->length = 0;
a230b26e 1985 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
fe589e61
MC
1986 goto again; /* get another record */
1987 }
1988#ifndef OPENSSL_NO_SCTP
1989 /* Only do replay check if no SCTP bio */
1990 if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1991#endif
912c89c5 1992 /* Check whether this is a repeat, or aged record. */
1fb9fdc3
MC
1993 /*
1994 * TODO: Does it make sense to have replay protection in epoch 0 where
1995 * we have no integrity negotiated yet?
1996 */
912c89c5 1997 if (!dtls1_record_replay_check(s, bitmap)) {
fe589e61 1998 rr->length = 0;
66fab923 1999 rr->read = 1;
7a7048af 2000 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
fe589e61
MC
2001 goto again; /* get another record */
2002 }
2003#ifndef OPENSSL_NO_SCTP
2004 }
2005#endif
2006
2007 /* just read a 0 length packet */
66fab923
MC
2008 if (rr->length == 0) {
2009 rr->read = 1;
fe589e61 2010 goto again;
66fab923 2011 }
fe589e61
MC
2012
2013 /*
2014 * If this record is from the next epoch (either HM or ALERT), and a
2015 * handshake is currently in progress, buffer it since it cannot be
912c89c5 2016 * processed at this time.
fe589e61
MC
2017 */
2018 if (is_next_epoch) {
024f543c 2019 if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
c2853382
MC
2020 if (dtls1_buffer_record (s,
2021 &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
2022 rr->seq_num) < 0) {
2023 /* SSLfatal() already called */
fe589e61 2024 return -1;
c2853382 2025 }
fe589e61
MC
2026 }
2027 rr->length = 0;
66fab923 2028 rr->read = 1;
7a7048af 2029 RECORD_LAYER_reset_packet_length(&s->rlayer);
fe589e61
MC
2030 goto again;
2031 }
2032
1fb9fdc3 2033 if (!dtls1_process_record(s, bitmap)) {
c2853382
MC
2034 if (ossl_statem_in_error(s)) {
2035 /* dtls1_process_record() called SSLfatal */
2036 return -1;
2037 }
fe589e61 2038 rr->length = 0;
66fab923 2039 rr->read = 1;
a230b26e 2040 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
fe589e61
MC
2041 goto again; /* get another record */
2042 }
fe589e61 2043
208fb891 2044 return 1;
fe589e61
MC
2045
2046}
079ef6bd
MC
2047
2048int dtls_buffer_listen_record(SSL *s, size_t len, unsigned char *seq, size_t off)
2049{
2050 SSL3_RECORD *rr;
2051
2052 rr = RECORD_LAYER_get_rrec(&s->rlayer);
2053 memset(rr, 0, sizeof(SSL3_RECORD));
2054
2055 rr->length = len;
2056 rr->type = SSL3_RT_HANDSHAKE;
2057 memcpy(rr->seq_num, seq, sizeof(rr->seq_num));
2058 rr->off = off;
2059
2060 s->rlayer.packet = RECORD_LAYER_get_rbuf(&s->rlayer)->buf;
2061 s->rlayer.packet_length = DTLS1_RT_HEADER_LENGTH + len;
2062 rr->data = s->rlayer.packet + DTLS1_RT_HEADER_LENGTH;
2063
2064 if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
2065 SSL3_RECORD_get_seq_num(s->rlayer.rrec)) <= 0) {
2066 /* SSLfatal() already called */
2067 return 0;
2068 }
2069
2070 return 1;
2071}