]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
4333b89f | 2 | * Copyright 2005-2021 The OpenSSL Project Authors. All Rights Reserved. |
0f113f3e | 3 | * |
2c18d164 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
36d16f8e BL |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <errno.h> | |
706457b7 | 12 | #include "../ssl_local.h" |
36d16f8e BL |
13 | #include <openssl/evp.h> |
14 | #include <openssl/buffer.h> | |
706457b7 | 15 | #include "record_local.h" |
0d345f0e | 16 | #include "internal/packet.h" |
67dc995e | 17 | #include "internal/cryptlib.h" |
40f37188 MC |
18 | |
19 | int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl) | |
20 | { | |
21 | DTLS_RECORD_LAYER *d; | |
0485d540 | 22 | |
fe1128dc | 23 | if ((d = OPENSSL_malloc(sizeof(*d))) == NULL) { |
6849b73c | 24 | ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); |
26a7d938 | 25 | return 0; |
fe1128dc | 26 | } |
40f37188 MC |
27 | |
28 | rl->d = d; | |
5fb6f80c | 29 | |
24a1e2f2 | 30 | d->buffered_app_data.q = pqueue_new(); |
cb2ce7ab | 31 | |
eddb067e | 32 | if (d->buffered_app_data.q == NULL) { |
cb2ce7ab MC |
33 | OPENSSL_free(d); |
34 | rl->d = NULL; | |
26a7d938 | 35 | return 0; |
cb2ce7ab | 36 | } |
40f37188 MC |
37 | |
38 | return 1; | |
39 | } | |
40 | ||
41 | void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl) | |
42 | { | |
d0afb30e MC |
43 | if (rl->d == NULL) |
44 | return; | |
45 | ||
cb2ce7ab | 46 | DTLS_RECORD_LAYER_clear(rl); |
24a1e2f2 | 47 | pqueue_free(rl->d->buffered_app_data.q); |
40f37188 MC |
48 | OPENSSL_free(rl->d); |
49 | rl->d = NULL; | |
50 | } | |
51 | ||
52 | void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl) | |
53 | { | |
54 | DTLS_RECORD_LAYER *d; | |
cb2ce7ab | 55 | pitem *item = NULL; |
eddb067e | 56 | TLS_RECORD *rec; |
cf2cede4 | 57 | pqueue *buffered_app_data; |
cb2ce7ab | 58 | |
40f37188 | 59 | d = rl->d; |
0485d540 | 60 | |
24a1e2f2 | 61 | while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) { |
eddb067e | 62 | rec = (TLS_RECORD *)item->data; |
163b8016 | 63 | if (rl->s->options & SSL_OP_CLEANSE_PLAINTEXT) |
eddb067e MC |
64 | OPENSSL_cleanse(rec->data, rec->length); |
65 | OPENSSL_free(rec->data); | |
24a1e2f2 MC |
66 | OPENSSL_free(item->data); |
67 | pitem_free(item); | |
68 | } | |
69 | ||
24a1e2f2 | 70 | buffered_app_data = d->buffered_app_data.q; |
b4faea50 | 71 | memset(d, 0, sizeof(*d)); |
24a1e2f2 | 72 | d->buffered_app_data.q = buffered_app_data; |
40f37188 MC |
73 | } |
74 | ||
3bb8f87d MC |
75 | void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e) |
76 | { | |
77 | if (e == rl->d->w_epoch - 1) { | |
78 | memcpy(rl->d->curr_write_sequence, | |
a230b26e | 79 | rl->write_sequence, sizeof(rl->write_sequence)); |
3bb8f87d | 80 | memcpy(rl->write_sequence, |
a230b26e | 81 | rl->d->last_write_sequence, sizeof(rl->write_sequence)); |
3bb8f87d MC |
82 | } else if (e == rl->d->w_epoch + 1) { |
83 | memcpy(rl->d->last_write_sequence, | |
a230b26e | 84 | rl->write_sequence, sizeof(unsigned char[8])); |
3bb8f87d | 85 | memcpy(rl->write_sequence, |
a230b26e | 86 | rl->d->curr_write_sequence, sizeof(rl->write_sequence)); |
3bb8f87d MC |
87 | } |
88 | rl->d->w_epoch = e; | |
89 | } | |
90 | ||
e3d0dae7 MC |
91 | void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq) |
92 | { | |
93 | memcpy(rl->write_sequence, seq, SEQ_NUM_SIZE); | |
94 | } | |
95 | ||
eddb067e | 96 | int dtls_buffer_record(SSL_CONNECTION *s, TLS_RECORD *rec) |
0f113f3e | 97 | { |
eddb067e | 98 | TLS_RECORD *rdata; |
0f113f3e | 99 | pitem *item; |
eddb067e | 100 | record_pqueue *queue = &(s->rlayer.d->buffered_app_data); |
0f113f3e MC |
101 | |
102 | /* Limit the size of the queue to prevent DOS attacks */ | |
103 | if (pqueue_size(queue->q) >= 100) | |
104 | return 0; | |
105 | ||
eddb067e MC |
106 | /* We don't buffer partially read records */ |
107 | if (!ossl_assert(rec->off == 0)) | |
108 | return -1; | |
109 | ||
b4faea50 | 110 | rdata = OPENSSL_malloc(sizeof(*rdata)); |
eddb067e | 111 | item = pitem_new(rec->seq_num, rdata); |
0f113f3e | 112 | if (rdata == NULL || item == NULL) { |
b548a1f1 | 113 | OPENSSL_free(rdata); |
25aaa98a | 114 | pitem_free(item); |
c48ffbcc | 115 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
06c6a2b4 | 116 | return -1; |
0f113f3e MC |
117 | } |
118 | ||
eddb067e MC |
119 | *rdata = *rec; |
120 | /* | |
121 | * We will release the record from the record layer soon, so we take a copy | |
122 | * now. Copying data isn't good - but this should be infrequent so we | |
123 | * accept it here. | |
124 | */ | |
125 | rdata->data = OPENSSL_memdup(rec->data, rec->length); | |
126 | if (rdata->data == NULL) { | |
127 | OPENSSL_free(rdata); | |
128 | pitem_free(item); | |
129 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); | |
130 | return -1; | |
131 | } | |
132 | /* | |
133 | * We use a NULL rechandle to indicate that the data field has been | |
134 | * allocated by us. | |
135 | */ | |
136 | rdata->rechandle = NULL; | |
0f113f3e MC |
137 | |
138 | item->data = rdata; | |
36d16f8e | 139 | |
7e159e01 | 140 | #ifndef OPENSSL_NO_SCTP |
0f113f3e | 141 | /* Store bio_dgram_sctp_rcvinfo struct */ |
38b051a1 TM |
142 | if (BIO_dgram_is_sctp(SSL_get_rbio(ssl)) && |
143 | (SSL_get_state(ssl) == TLS_ST_SR_FINISHED | |
144 | || SSL_get_state(ssl) == TLS_ST_CR_FINISHED)) { | |
145 | BIO_ctrl(SSL_get_rbio(ssl), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO, | |
0f113f3e MC |
146 | sizeof(rdata->recordinfo), &rdata->recordinfo); |
147 | } | |
7e159e01 DSH |
148 | #endif |
149 | ||
0f113f3e | 150 | if (pqueue_insert(queue->q, item) == NULL) { |
840facc3 | 151 | /* Must be a duplicate so ignore it */ |
eddb067e | 152 | OPENSSL_free(rdata->data); |
0f113f3e MC |
153 | OPENSSL_free(rdata); |
154 | pitem_free(item); | |
0f113f3e | 155 | } |
36d16f8e | 156 | |
208fb891 | 157 | return 1; |
0f113f3e MC |
158 | } |
159 | ||
eddb067e MC |
160 | /* Unbuffer a previously buffered TLS_RECORD structure if any */ |
161 | static void dtls_unbuffer_record(SSL_CONNECTION *s) | |
0f113f3e | 162 | { |
eddb067e | 163 | TLS_RECORD *rdata; |
36d16f8e BL |
164 | pitem *item; |
165 | ||
eddb067e MC |
166 | /* If we already have records to handle then do nothing */ |
167 | if (s->rlayer.curr_rec < s->rlayer.num_recs) | |
168 | return; | |
1fb9fdc3 | 169 | |
eddb067e MC |
170 | item = pqueue_pop(s->rlayer.d->buffered_app_data.q); |
171 | if (item != NULL) { | |
172 | rdata = (TLS_RECORD *)item->data; | |
0f113f3e | 173 | |
eddb067e MC |
174 | s->rlayer.tlsrecs[0] = *rdata; |
175 | s->rlayer.num_recs = 1; | |
176 | s->rlayer.curr_rec = 0; | |
738ad946 | 177 | |
1fb9fdc3 | 178 | #ifndef OPENSSL_NO_SCTP |
eddb067e MC |
179 | /* Restore bio_dgram_sctp_rcvinfo struct */ |
180 | if (BIO_dgram_is_sctp(SSL_get_rbio(s))) { | |
181 | DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data; | |
182 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO, | |
183 | sizeof(rdata->recordinfo), &rdata->recordinfo); | |
184 | } | |
1fb9fdc3 | 185 | #endif |
1fb9fdc3 | 186 | |
eddb067e MC |
187 | /* Set proper sequence number for mac calculation */ |
188 | memcpy(&(s->rlayer.read_sequence[2]), &(rdata->seq_num[2]), 6); | |
1fb9fdc3 | 189 | |
eddb067e MC |
190 | OPENSSL_free(item->data); |
191 | pitem_free(item); | |
0f113f3e | 192 | } |
0f113f3e | 193 | } |
36d16f8e | 194 | |
1d97c843 TH |
195 | /*- |
196 | * Return up to 'len' payload bytes received in 'type' records. | |
36d16f8e BL |
197 | * 'type' is one of the following: |
198 | * | |
199 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) | |
200 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) | |
201 | * - 0 (during a shutdown, no data has to be returned) | |
202 | * | |
203 | * If we don't have stored data to work from, read a SSL/TLS record first | |
204 | * (possibly multiple records if we still don't have anything to return). | |
205 | * | |
206 | * This function must handle any surprises the peer may have for us, such as | |
c69f2adf MC |
207 | * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec |
208 | * messages are treated as if they were handshake messages *if* the |recd_type| | |
209 | * argument is non NULL. | |
36d16f8e BL |
210 | * Also if record payloads contain fragments too small to process, we store |
211 | * them until there is enough for the respective protocol (the record protocol | |
212 | * may use arbitrary fragmentation and even interleaving): | |
213 | * Change cipher spec protocol | |
214 | * just 1 byte needed, no need for keeping anything stored | |
215 | * Alert protocol | |
216 | * 2 bytes needed (AlertLevel, AlertDescription) | |
217 | * Handshake protocol | |
218 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have | |
219 | * to detect unexpected Client Hello and Hello Request messages | |
220 | * here, anything else is handled by higher layers | |
221 | * Application data protocol | |
222 | * none of our business | |
223 | */ | |
657da85e | 224 | int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf, |
02ba18a6 | 225 | size_t len, int peek, size_t *readbytes) |
0f113f3e | 226 | { |
eddb067e | 227 | int i, j, ret; |
bd990e25 | 228 | size_t n; |
eddb067e | 229 | TLS_RECORD *rr; |
0f113f3e | 230 | void (*cb) (const SSL *ssl, int type2, int val) = NULL; |
38b051a1 TM |
231 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
232 | ||
233 | if (sc == NULL) | |
234 | return -1; | |
0f113f3e | 235 | |
0f113f3e MC |
236 | if ((type && (type != SSL3_RT_APPLICATION_DATA) && |
237 | (type != SSL3_RT_HANDSHAKE)) || | |
238 | (peek && (type != SSL3_RT_APPLICATION_DATA))) { | |
38b051a1 | 239 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
0f113f3e MC |
240 | return -1; |
241 | } | |
242 | ||
38b051a1 | 243 | if (!ossl_statem_get_in_handshake(sc) && SSL_in_init(s)) { |
0f113f3e | 244 | /* type == SSL3_RT_APPLICATION_DATA */ |
38b051a1 | 245 | i = sc->handshake_func(s); |
c2853382 | 246 | /* SSLfatal() already called if appropriate */ |
0f113f3e | 247 | if (i < 0) |
eda75751 | 248 | return i; |
c2853382 | 249 | if (i == 0) |
eda75751 | 250 | return -1; |
0f113f3e MC |
251 | } |
252 | ||
253 | start: | |
38b051a1 | 254 | sc->rwstate = SSL_NOTHING; |
0f113f3e | 255 | |
0f113f3e MC |
256 | /* |
257 | * We are not handshaking and have no data yet, so process data buffered | |
258 | * during the last handshake in advance, if any. | |
259 | */ | |
eddb067e MC |
260 | if (SSL_is_init_finished(s)) |
261 | dtls_unbuffer_record(sc); | |
0f113f3e MC |
262 | |
263 | /* Check for timeout */ | |
38b051a1 | 264 | if (dtls1_handle_timeout(sc) > 0) { |
0f113f3e | 265 | goto start; |
38b051a1 | 266 | } else if (ossl_statem_in_error(sc)) { |
d273b60b MC |
267 | /* dtls1_handle_timeout() has failed with a fatal error */ |
268 | return -1; | |
269 | } | |
0f113f3e MC |
270 | |
271 | /* get new packet if necessary */ | |
eddb067e MC |
272 | if (sc->rlayer.curr_rec >= sc->rlayer.num_recs) { |
273 | sc->rlayer.curr_rec = sc->rlayer.num_recs = 0; | |
274 | do { | |
275 | rr = &sc->rlayer.tlsrecs[sc->rlayer.num_recs]; | |
276 | ||
eddb067e MC |
277 | ret = HANDLE_RLAYER_RETURN(sc, |
278 | sc->rrlmethod->read_record(sc->rrl, &rr->rechandle, | |
279 | &rr->version, &rr->type, | |
280 | &rr->data, &rr->length, | |
281 | &rr->epoch, rr->seq_num)); | |
282 | if (ret <= 0) { | |
283 | ret = dtls1_read_failed(sc, ret); | |
284 | /* | |
285 | * Anything other than a timeout is an error. SSLfatal() already | |
286 | * called if appropriate. | |
287 | */ | |
288 | if (ret <= 0) | |
289 | return ret; | |
290 | else | |
291 | goto start; | |
292 | } | |
293 | rr->off = 0; | |
294 | sc->rlayer.num_recs++; | |
295 | } while (sc->rrlmethod->processed_read_pending(sc->rrl) | |
296 | && sc->rlayer.num_recs < SSL_MAX_PIPELINES); | |
0f113f3e | 297 | } |
eddb067e | 298 | rr = &sc->rlayer.tlsrecs[sc->rlayer.curr_rec]; |
0f113f3e | 299 | |
af58be76 MC |
300 | /* |
301 | * Reset the count of consecutive warning alerts if we've got a non-empty | |
302 | * record that isn't an alert. | |
303 | */ | |
eddb067e | 304 | if (rr->type != SSL3_RT_ALERT && rr->length != 0) |
38b051a1 | 305 | sc->rlayer.alert_count = 0; |
af58be76 | 306 | |
0f113f3e MC |
307 | /* we now have a packet which can be read and processed */ |
308 | ||
38b051a1 | 309 | if (sc->s3.change_cipher_spec /* set when we receive ChangeCipherSpec, |
555cbb32 | 310 | * reset by ssl3_get_finished */ |
eddb067e | 311 | && (rr->type != SSL3_RT_HANDSHAKE)) { |
0f113f3e MC |
312 | /* |
313 | * We now have application data between CCS and Finished. Most likely | |
314 | * the packets were reordered on their way, so buffer the application | |
315 | * data for later processing rather than dropping the connection. | |
316 | */ | |
eddb067e | 317 | if (dtls_buffer_record(sc, rr) < 0) { |
c2853382 | 318 | /* SSLfatal() already called */ |
0f113f3e MC |
319 | return -1; |
320 | } | |
eddb067e | 321 | ssl_release_record(sc, rr); |
0f113f3e MC |
322 | goto start; |
323 | } | |
324 | ||
325 | /* | |
326 | * If the other end has shut down, throw anything we read away (even in | |
327 | * 'peek' mode) | |
328 | */ | |
38b051a1 | 329 | if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) { |
eddb067e | 330 | ssl_release_record(sc, rr); |
38b051a1 | 331 | sc->rwstate = SSL_NOTHING; |
eda75751 | 332 | return 0; |
0f113f3e MC |
333 | } |
334 | ||
eddb067e MC |
335 | if (type == rr->type |
336 | || (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC | |
a230b26e | 337 | && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) { |
c69f2adf MC |
338 | /* |
339 | * SSL3_RT_APPLICATION_DATA or | |
340 | * SSL3_RT_HANDSHAKE or | |
341 | * SSL3_RT_CHANGE_CIPHER_SPEC | |
342 | */ | |
0f113f3e MC |
343 | /* |
344 | * make sure that we are not getting application data when we are | |
345 | * doing a handshake for the first time | |
346 | */ | |
347 | if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && | |
38b051a1 TM |
348 | (sc->enc_read_ctx == NULL)) { |
349 | SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, | |
c2853382 MC |
350 | SSL_R_APP_DATA_IN_HANDSHAKE); |
351 | return -1; | |
0f113f3e | 352 | } |
7e159e01 | 353 | |
c69f2adf | 354 | if (recvd_type != NULL) |
eddb067e | 355 | *recvd_type = rr->type; |
c69f2adf | 356 | |
66fab923 MC |
357 | if (len == 0) { |
358 | /* | |
eddb067e | 359 | * Release a zero length record. This ensures multiple calls to |
66fab923 MC |
360 | * SSL_read() with a zero length buffer will eventually cause |
361 | * SSL_pending() to report data as being available. | |
362 | */ | |
eddb067e MC |
363 | if (rr->length == 0) |
364 | ssl_release_record(sc, rr); | |
eda75751 | 365 | return 0; |
66fab923 | 366 | } |
0f113f3e | 367 | |
eddb067e MC |
368 | if (len > rr->length) |
369 | n = rr->length; | |
0f113f3e | 370 | else |
eda75751 | 371 | n = len; |
0f113f3e | 372 | |
eddb067e | 373 | memcpy(buf, &(rr->data[rr->off]), n); |
66fab923 | 374 | if (peek) { |
eddb067e MC |
375 | if (rr->length == 0) |
376 | ssl_release_record(sc, rr); | |
66fab923 | 377 | } else { |
38b051a1 | 378 | if (sc->options & SSL_OP_CLEANSE_PLAINTEXT) |
eddb067e MC |
379 | OPENSSL_cleanse(&(rr->data[rr->off]), n); |
380 | rr->length -= n; | |
381 | rr->off += n; | |
8bbf7ef6 | 382 | if (rr->length == 0) |
eddb067e | 383 | ssl_release_record(sc, rr); |
0f113f3e | 384 | } |
7e159e01 | 385 | #ifndef OPENSSL_NO_SCTP |
0f113f3e MC |
386 | /* |
387 | * We might had to delay a close_notify alert because of reordered | |
388 | * app data. If there was an alert and there is no message to read | |
389 | * anymore, finally set shutdown. | |
390 | */ | |
391 | if (BIO_dgram_is_sctp(SSL_get_rbio(s)) && | |
38b051a1 | 392 | sc->d1->shutdown_received |
639e5760 | 393 | && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)) <= 0) { |
38b051a1 | 394 | sc->shutdown |= SSL_RECEIVED_SHUTDOWN; |
eda75751 | 395 | return 0; |
0f113f3e MC |
396 | } |
397 | #endif | |
02ba18a6 | 398 | *readbytes = n; |
eda75751 | 399 | return 1; |
0f113f3e MC |
400 | } |
401 | ||
402 | /* | |
403 | * If we get here, then type != rr->type; if we have a handshake message, | |
404 | * then it was unexpected (Hello Request or Client Hello). | |
405 | */ | |
406 | ||
eddb067e | 407 | if (rr->type == SSL3_RT_ALERT) { |
bd990e25 | 408 | unsigned int alert_level, alert_descr; |
eddb067e | 409 | unsigned char *alert_bytes = rr->data + rr->off; |
bd990e25 | 410 | PACKET alert; |
0f113f3e | 411 | |
eddb067e | 412 | if (!PACKET_buf_init(&alert, alert_bytes, rr->length) |
bd990e25 MC |
413 | || !PACKET_get_1(&alert, &alert_level) |
414 | || !PACKET_get_1(&alert, &alert_descr) | |
415 | || PACKET_remaining(&alert) != 0) { | |
38b051a1 | 416 | SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT); |
c2853382 | 417 | return -1; |
0f113f3e MC |
418 | } |
419 | ||
38b051a1 TM |
420 | if (sc->msg_callback) |
421 | sc->msg_callback(0, sc->version, SSL3_RT_ALERT, alert_bytes, 2, s, | |
422 | sc->msg_callback_arg); | |
0f113f3e | 423 | |
38b051a1 TM |
424 | if (sc->info_callback != NULL) |
425 | cb = sc->info_callback; | |
0f113f3e MC |
426 | else if (s->ctx->info_callback != NULL) |
427 | cb = s->ctx->info_callback; | |
428 | ||
429 | if (cb != NULL) { | |
430 | j = (alert_level << 8) | alert_descr; | |
431 | cb(s, SSL_CB_READ_ALERT, j); | |
432 | } | |
433 | ||
fd865cad | 434 | if (alert_level == SSL3_AL_WARNING) { |
38b051a1 | 435 | sc->s3.warn_alert = alert_descr; |
eddb067e | 436 | ssl_release_record(sc, rr); |
af58be76 | 437 | |
38b051a1 TM |
438 | sc->rlayer.alert_count++; |
439 | if (sc->rlayer.alert_count == MAX_WARN_ALERT_COUNT) { | |
440 | SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, | |
c2853382 MC |
441 | SSL_R_TOO_MANY_WARN_ALERTS); |
442 | return -1; | |
af58be76 MC |
443 | } |
444 | ||
0f113f3e | 445 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { |
7e159e01 | 446 | #ifndef OPENSSL_NO_SCTP |
0f113f3e MC |
447 | /* |
448 | * With SCTP and streams the socket may deliver app data | |
449 | * after a close_notify alert. We have to check this first so | |
450 | * that nothing gets discarded. | |
451 | */ | |
452 | if (BIO_dgram_is_sctp(SSL_get_rbio(s)) && | |
639e5760 | 453 | BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)) > 0) { |
38b051a1 TM |
454 | sc->d1->shutdown_received = 1; |
455 | sc->rwstate = SSL_READING; | |
0f113f3e MC |
456 | BIO_clear_retry_flags(SSL_get_rbio(s)); |
457 | BIO_set_retry_read(SSL_get_rbio(s)); | |
458 | return -1; | |
459 | } | |
7e159e01 | 460 | #endif |
38b051a1 | 461 | sc->shutdown |= SSL_RECEIVED_SHUTDOWN; |
eda75751 | 462 | return 0; |
0f113f3e | 463 | } |
fd865cad | 464 | } else if (alert_level == SSL3_AL_FATAL) { |
38b051a1 TM |
465 | sc->rwstate = SSL_NOTHING; |
466 | sc->s3.fatal_alert = alert_descr; | |
467 | SSLfatal_data(sc, SSL_AD_NO_ALERT, | |
c48ffbcc RL |
468 | SSL_AD_REASON_OFFSET + alert_descr, |
469 | "SSL alert number %d", alert_descr); | |
38b051a1 | 470 | sc->shutdown |= SSL_RECEIVED_SHUTDOWN; |
eddb067e | 471 | ssl_release_record(sc, rr); |
38b051a1 | 472 | SSL_CTX_remove_session(sc->session_ctx, sc->session); |
eda75751 | 473 | return 0; |
0f113f3e | 474 | } else { |
38b051a1 | 475 | SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE); |
c2853382 | 476 | return -1; |
0f113f3e MC |
477 | } |
478 | ||
479 | goto start; | |
480 | } | |
481 | ||
38b051a1 | 482 | if (sc->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a |
0f113f3e | 483 | * shutdown */ |
38b051a1 | 484 | sc->rwstate = SSL_NOTHING; |
eddb067e | 485 | ssl_release_record(sc, rr); |
eda75751 | 486 | return 0; |
0f113f3e MC |
487 | } |
488 | ||
eddb067e | 489 | if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
0f113f3e MC |
490 | /* |
491 | * We can't process a CCS now, because previous handshake messages | |
492 | * are still missing, so just drop it. | |
493 | */ | |
eddb067e | 494 | ssl_release_record(sc, rr); |
0f113f3e MC |
495 | goto start; |
496 | } | |
497 | ||
498 | /* | |
499 | * Unexpected handshake message (Client Hello, or protocol violation) | |
500 | */ | |
eddb067e | 501 | if (rr->type == SSL3_RT_HANDSHAKE && !ossl_statem_get_in_handshake(sc)) { |
0f113f3e MC |
502 | struct hm_header_st msg_hdr; |
503 | ||
bd990e25 MC |
504 | /* |
505 | * This may just be a stale retransmit. Also sanity check that we have | |
506 | * at least enough record bytes for a message header | |
507 | */ | |
eddb067e MC |
508 | if (rr->epoch != sc->rlayer.d->r_epoch |
509 | || rr->length < DTLS1_HM_HEADER_LENGTH) { | |
510 | ssl_release_record(sc, rr); | |
0f113f3e MC |
511 | goto start; |
512 | } | |
513 | ||
bd990e25 MC |
514 | dtls1_get_message_header(rr->data, &msg_hdr); |
515 | ||
0f113f3e MC |
516 | /* |
517 | * If we are server, we may have a repeated FINISHED of the client | |
518 | * here, then retransmit our CCS and FINISHED. | |
519 | */ | |
520 | if (msg_hdr.type == SSL3_MT_FINISHED) { | |
38b051a1 | 521 | if (dtls1_check_timeout_num(sc) < 0) { |
c2853382 | 522 | /* SSLfatal) already called */ |
0f113f3e | 523 | return -1; |
c2853382 | 524 | } |
0f113f3e | 525 | |
38b051a1 | 526 | if (dtls1_retransmit_buffered_messages(sc) <= 0) { |
d273b60b | 527 | /* Fail if we encountered a fatal error */ |
38b051a1 | 528 | if (ossl_statem_in_error(sc)) |
d273b60b | 529 | return -1; |
d273b60b | 530 | } |
eddb067e | 531 | ssl_release_record(sc, rr); |
38b051a1 | 532 | if (!(sc->mode & SSL_MODE_AUTO_RETRY)) { |
81c9ebd9 | 533 | if (!sc->rrlmethod->unprocessed_read_pending(sc->rrl)) { |
ad962252 MC |
534 | /* no read-ahead left? */ |
535 | BIO *bio; | |
536 | ||
38b051a1 | 537 | sc->rwstate = SSL_READING; |
ad962252 MC |
538 | bio = SSL_get_rbio(s); |
539 | BIO_clear_retry_flags(bio); | |
540 | BIO_set_retry_read(bio); | |
541 | return -1; | |
542 | } | |
543 | } | |
0f113f3e MC |
544 | goto start; |
545 | } | |
546 | ||
c7f47786 MC |
547 | /* |
548 | * To get here we must be trying to read app data but found handshake | |
549 | * data. But if we're trying to read app data, and we're not in init | |
550 | * (which is tested for at the top of this function) then init must be | |
551 | * finished | |
552 | */ | |
b77f3ed1 | 553 | if (!ossl_assert(SSL_is_init_finished(s))) { |
38b051a1 | 554 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c2853382 | 555 | return -1; |
0f113f3e | 556 | } |
c7f47786 MC |
557 | |
558 | /* We found handshake data, so we're going back into init */ | |
38b051a1 | 559 | ossl_statem_set_in_init(sc, 1); |
c7f47786 | 560 | |
38b051a1 | 561 | i = sc->handshake_func(s); |
c2853382 | 562 | /* SSLfatal() called if appropriate */ |
0f113f3e | 563 | if (i < 0) |
eda75751 | 564 | return i; |
c2853382 | 565 | if (i == 0) |
eda75751 | 566 | return -1; |
0f113f3e | 567 | |
38b051a1 | 568 | if (!(sc->mode & SSL_MODE_AUTO_RETRY)) { |
81c9ebd9 | 569 | if (!sc->rrlmethod->unprocessed_read_pending(sc->rrl)) { |
28d59af8 | 570 | /* no read-ahead left? */ |
0f113f3e MC |
571 | BIO *bio; |
572 | /* | |
573 | * In the case where we try to read application data, but we | |
574 | * trigger an SSL handshake, we return -1 with the retry | |
575 | * option set. Otherwise renegotiation may cause nasty | |
576 | * problems in the blocking world | |
577 | */ | |
38b051a1 | 578 | sc->rwstate = SSL_READING; |
0f113f3e MC |
579 | bio = SSL_get_rbio(s); |
580 | BIO_clear_retry_flags(bio); | |
581 | BIO_set_retry_read(bio); | |
eda75751 | 582 | return -1; |
0f113f3e MC |
583 | } |
584 | } | |
585 | goto start; | |
586 | } | |
587 | ||
eddb067e | 588 | switch (rr->type) { |
0f113f3e | 589 | default: |
38b051a1 | 590 | SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD); |
c2853382 | 591 | return -1; |
0f113f3e MC |
592 | case SSL3_RT_CHANGE_CIPHER_SPEC: |
593 | case SSL3_RT_ALERT: | |
594 | case SSL3_RT_HANDSHAKE: | |
595 | /* | |
596 | * we already handled all of these, with the possible exception of | |
024f543c MC |
597 | * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but |
598 | * that should not happen when type != rr->type | |
0f113f3e | 599 | */ |
38b051a1 | 600 | SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR); |
c2853382 | 601 | return -1; |
0f113f3e MC |
602 | case SSL3_RT_APPLICATION_DATA: |
603 | /* | |
604 | * At this point, we were expecting handshake data, but have | |
605 | * application data. If the library was running inside ssl3_read() | |
606 | * (i.e. in_read_app_data is set) and it makes sense to read | |
607 | * application data at this point (session renegotiation not yet | |
608 | * started), we will indulge it. | |
609 | */ | |
38b051a1 TM |
610 | if (sc->s3.in_read_app_data && |
611 | (sc->s3.total_renegotiations != 0) && | |
612 | ossl_statem_app_data_allowed(sc)) { | |
613 | sc->s3.in_read_app_data = 2; | |
eda75751 | 614 | return -1; |
0f113f3e | 615 | } else { |
38b051a1 | 616 | SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD); |
c2853382 | 617 | return -1; |
0f113f3e MC |
618 | } |
619 | } | |
620 | /* not reached */ | |
0f113f3e MC |
621 | } |
622 | ||
0f113f3e MC |
623 | /* |
624 | * Call this to write data in records of type 'type' It will return <= 0 if | |
625 | * not all data has been sent or non-blocking IO. | |
36d16f8e | 626 | */ |
38b051a1 TM |
627 | int dtls1_write_bytes(SSL_CONNECTION *s, int type, const void *buf, |
628 | size_t len, size_t *written) | |
0f113f3e MC |
629 | { |
630 | int i; | |
631 | ||
42bd7a16 | 632 | if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) { |
c48ffbcc | 633 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
380a522f | 634 | return -1; |
42bd7a16 | 635 | } |
0f113f3e | 636 | s->rwstate = SSL_NOTHING; |
7ee8627f | 637 | i = do_dtls1_write(s, type, buf, len, 0, written); |
0f113f3e MC |
638 | return i; |
639 | } | |
640 | ||
38b051a1 | 641 | int do_dtls1_write(SSL_CONNECTION *sc, int type, const unsigned char *buf, |
7ee8627f | 642 | size_t len, int create_empty_fragment, size_t *written) |
0f113f3e MC |
643 | { |
644 | unsigned char *p, *pseq; | |
645 | int i, mac_size, clear = 0; | |
7ee8627f | 646 | size_t prefix_len = 0; |
0f113f3e | 647 | int eivlen; |
f482740f | 648 | SSL3_RECORD wr; |
0f113f3e MC |
649 | SSL3_BUFFER *wb; |
650 | SSL_SESSION *sess; | |
38b051a1 | 651 | SSL *s = SSL_CONNECTION_GET_SSL(sc); |
0f113f3e | 652 | |
38b051a1 | 653 | wb = &sc->rlayer.wbuf[0]; |
db9a32e7 | 654 | |
0f113f3e | 655 | /* |
70cae332 BK |
656 | * DTLS writes whole datagrams, so there can't be anything left in |
657 | * the buffer. | |
0f113f3e | 658 | */ |
380a522f | 659 | if (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) { |
38b051a1 | 660 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
380a522f | 661 | return 0; |
0f113f3e MC |
662 | } |
663 | ||
664 | /* If we have an alert to send, lets send it */ | |
38b051a1 | 665 | if (sc->s3.alert_dispatch) { |
0f113f3e MC |
666 | i = s->method->ssl_dispatch_alert(s); |
667 | if (i <= 0) | |
7ee8627f | 668 | return i; |
0f113f3e MC |
669 | /* if it went, fall through and send more stuff */ |
670 | } | |
671 | ||
672 | if (len == 0 && !create_empty_fragment) | |
673 | return 0; | |
674 | ||
38b051a1 TM |
675 | if (len > ssl_get_max_send_fragment(sc)) { |
676 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE); | |
aefb9256 MC |
677 | return 0; |
678 | } | |
679 | ||
38b051a1 | 680 | sess = sc->session; |
0f113f3e | 681 | |
f6c95e46 | 682 | if ((sess == NULL) |
38b051a1 TM |
683 | || (sc->enc_write_ctx == NULL) |
684 | || (EVP_MD_CTX_get0_md(sc->write_hash) == NULL)) | |
0f113f3e MC |
685 | clear = 1; |
686 | ||
687 | if (clear) | |
688 | mac_size = 0; | |
689 | else { | |
38b051a1 | 690 | mac_size = EVP_MD_CTX_get_size(sc->write_hash); |
5591a613 | 691 | if (mac_size < 0) { |
38b051a1 | 692 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, |
5591a613 MC |
693 | SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE); |
694 | return -1; | |
695 | } | |
0f113f3e MC |
696 | } |
697 | ||
747e1639 | 698 | p = SSL3_BUFFER_get_buf(wb) + prefix_len; |
0f113f3e MC |
699 | |
700 | /* write the header */ | |
701 | ||
702 | *(p++) = type & 0xff; | |
f482740f | 703 | SSL3_RECORD_set_type(&wr, type); |
0f113f3e MC |
704 | /* |
705 | * Special case: for hello verify request, client version 1.0 and we | |
706 | * haven't decided which version to use yet send back using version 1.0 | |
707 | * header: otherwise some clients will ignore it. | |
708 | */ | |
032924c4 | 709 | if (s->method->version == DTLS_ANY_VERSION && |
38b051a1 | 710 | sc->max_proto_version != DTLS1_BAD_VER) { |
0f113f3e MC |
711 | *(p++) = DTLS1_VERSION >> 8; |
712 | *(p++) = DTLS1_VERSION & 0xff; | |
713 | } else { | |
38b051a1 TM |
714 | *(p++) = sc->version >> 8; |
715 | *(p++) = sc->version & 0xff; | |
0f113f3e MC |
716 | } |
717 | ||
718 | /* field where we are to write out packet epoch, seq num and len */ | |
719 | pseq = p; | |
720 | p += 10; | |
721 | ||
722 | /* Explicit IV length, block ciphers appropriate version flag */ | |
38b051a1 TM |
723 | if (sc->enc_write_ctx) { |
724 | int mode = EVP_CIPHER_CTX_get_mode(sc->enc_write_ctx); | |
0f113f3e | 725 | if (mode == EVP_CIPH_CBC_MODE) { |
38b051a1 | 726 | eivlen = EVP_CIPHER_CTX_get_iv_length(sc->enc_write_ctx); |
83ab43da DB |
727 | if (eivlen < 0) { |
728 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); | |
729 | return -1; | |
730 | } | |
0f113f3e MC |
731 | if (eivlen <= 1) |
732 | eivlen = 0; | |
733 | } | |
734 | /* Need explicit part of IV for GCM mode */ | |
735 | else if (mode == EVP_CIPH_GCM_MODE) | |
736 | eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN; | |
e75c5a79 DSH |
737 | else if (mode == EVP_CIPH_CCM_MODE) |
738 | eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN; | |
0f113f3e MC |
739 | else |
740 | eivlen = 0; | |
741 | } else | |
742 | eivlen = 0; | |
743 | ||
744 | /* lets setup the record stuff. */ | |
f482740f | 745 | SSL3_RECORD_set_data(&wr, p + eivlen); /* make room for IV in case of CBC */ |
7ee8627f | 746 | SSL3_RECORD_set_length(&wr, len); |
f482740f | 747 | SSL3_RECORD_set_input(&wr, (unsigned char *)buf); |
0f113f3e MC |
748 | |
749 | /* | |
f482740f | 750 | * we now 'read' from wr.input, wr.length bytes into wr.data |
0f113f3e MC |
751 | */ |
752 | ||
753 | /* first we compress */ | |
38b051a1 TM |
754 | if (sc->compress != NULL) { |
755 | if (!ssl3_do_compress(sc, &wr)) { | |
756 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE); | |
5591a613 | 757 | return -1; |
0f113f3e MC |
758 | } |
759 | } else { | |
f482740f MC |
760 | memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr), |
761 | SSL3_RECORD_get_length(&wr)); | |
762 | SSL3_RECORD_reset_input(&wr); | |
0f113f3e | 763 | } |
36d16f8e | 764 | |
0f113f3e | 765 | /* |
f482740f MC |
766 | * we should still have the output to wr.data and the input from |
767 | * wr.input. Length should be wr.length. wr.data still points in the | |
0f113f3e MC |
768 | * wb->buf |
769 | */ | |
36d16f8e | 770 | |
38b051a1 TM |
771 | if (!SSL_WRITE_ETM(sc) && mac_size != 0) { |
772 | if (!s->method->ssl3_enc->mac(sc, &wr, | |
a14aa99b | 773 | &(p[SSL3_RECORD_get_length(&wr) + eivlen]), |
5591a613 | 774 | 1)) { |
38b051a1 | 775 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5591a613 MC |
776 | return -1; |
777 | } | |
f482740f | 778 | SSL3_RECORD_add_length(&wr, mac_size); |
0f113f3e | 779 | } |
36d16f8e | 780 | |
0f113f3e | 781 | /* this is true regardless of mac size */ |
f482740f MC |
782 | SSL3_RECORD_set_data(&wr, p); |
783 | SSL3_RECORD_reset_input(&wr); | |
36d16f8e | 784 | |
0f113f3e | 785 | if (eivlen) |
f482740f | 786 | SSL3_RECORD_add_length(&wr, eivlen); |
36d16f8e | 787 | |
38b051a1 TM |
788 | if (s->method->ssl3_enc->enc(sc, &wr, 1, 1, NULL, mac_size) < 1) { |
789 | if (!ossl_statem_in_error(sc)) { | |
790 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
921d84a0 | 791 | } |
5591a613 MC |
792 | return -1; |
793 | } | |
36d16f8e | 794 | |
38b051a1 TM |
795 | if (SSL_WRITE_ETM(sc) && mac_size != 0) { |
796 | if (!s->method->ssl3_enc->mac(sc, &wr, | |
5591a613 | 797 | &(p[SSL3_RECORD_get_length(&wr)]), 1)) { |
38b051a1 | 798 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5591a613 MC |
799 | return -1; |
800 | } | |
e23d5071 DW |
801 | SSL3_RECORD_add_length(&wr, mac_size); |
802 | } | |
803 | ||
0f113f3e | 804 | /* record length after mac and block padding */ |
36d16f8e | 805 | |
0f113f3e | 806 | /* there's only one epoch between handshake and app data */ |
36d16f8e | 807 | |
38b051a1 | 808 | s2n(sc->rlayer.d->w_epoch, pseq); |
36d16f8e | 809 | |
38b051a1 | 810 | memcpy(pseq, &(sc->rlayer.write_sequence[2]), 6); |
0f113f3e | 811 | pseq += 6; |
f482740f | 812 | s2n(SSL3_RECORD_get_length(&wr), pseq); |
36d16f8e | 813 | |
38b051a1 TM |
814 | if (sc->msg_callback) |
815 | sc->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH, | |
816 | DTLS1_RT_HEADER_LENGTH, s, sc->msg_callback_arg); | |
36d16f8e | 817 | |
0f113f3e | 818 | /* |
f482740f | 819 | * we should now have wr.data pointing to the encrypted data, which is |
0f113f3e MC |
820 | * wr->length long |
821 | */ | |
f482740f MC |
822 | SSL3_RECORD_set_type(&wr, type); /* not needed but helps for debugging */ |
823 | SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH); | |
36d16f8e | 824 | |
38b051a1 | 825 | ssl3_record_sequence_update(&(sc->rlayer.write_sequence[0])); |
36d16f8e | 826 | |
0f113f3e MC |
827 | if (create_empty_fragment) { |
828 | /* | |
829 | * we are in a recursive call; just return the length, don't write | |
830 | * out anything here | |
831 | */ | |
7ee8627f MC |
832 | *written = wr.length; |
833 | return 1; | |
0f113f3e | 834 | } |
36d16f8e | 835 | |
0f113f3e | 836 | /* now let's set up wb */ |
f482740f | 837 | SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr)); |
747e1639 | 838 | SSL3_BUFFER_set_offset(wb, 0); |
0f113f3e MC |
839 | |
840 | /* | |
841 | * memorize arguments so that ssl3_write_pending can detect bad write | |
842 | * retries later | |
843 | */ | |
38b051a1 TM |
844 | sc->rlayer.wpend_tot = len; |
845 | sc->rlayer.wpend_buf = buf; | |
846 | sc->rlayer.wpend_type = type; | |
847 | sc->rlayer.wpend_ret = len; | |
0f113f3e | 848 | |
c2853382 | 849 | /* we now just need to write the buffer. Calls SSLfatal() as required. */ |
38b051a1 | 850 | return ssl3_write_pending(sc, type, buf, len, written); |
0f113f3e | 851 | } |
36d16f8e | 852 | |
38b051a1 | 853 | void dtls1_reset_seq_numbers(SSL_CONNECTION *s, int rw) |
0f113f3e MC |
854 | { |
855 | unsigned char *seq; | |
de07f311 | 856 | unsigned int seq_bytes = sizeof(s->rlayer.read_sequence); |
0f113f3e MC |
857 | |
858 | if (rw & SSL3_CC_READ) { | |
de07f311 | 859 | seq = s->rlayer.read_sequence; |
78a39fe7 | 860 | s->rlayer.d->r_epoch++; |
16f8d4eb RS |
861 | memcpy(&s->rlayer.d->bitmap, &s->rlayer.d->next_bitmap, |
862 | sizeof(s->rlayer.d->bitmap)); | |
a230b26e | 863 | memset(&s->rlayer.d->next_bitmap, 0, sizeof(s->rlayer.d->next_bitmap)); |
5cb4d646 MC |
864 | |
865 | /* | |
866 | * We must not use any buffered messages received from the previous | |
867 | * epoch | |
868 | */ | |
869 | dtls1_clear_received_buffer(s); | |
0f113f3e | 870 | } else { |
de07f311 | 871 | seq = s->rlayer.write_sequence; |
3bb8f87d | 872 | memcpy(s->rlayer.d->last_write_sequence, seq, |
de07f311 | 873 | sizeof(s->rlayer.write_sequence)); |
78a39fe7 | 874 | s->rlayer.d->w_epoch++; |
0f113f3e MC |
875 | } |
876 | ||
16f8d4eb | 877 | memset(seq, 0, seq_bytes); |
0f113f3e | 878 | } |