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