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