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