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