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