Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2000-2018 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2012-2018 Nikos Mavrogiannopoulos |
4 | | * Copyright (C) 2018 Red Hat, Inc. |
5 | | * |
6 | | * Author: Nikos Mavrogiannopoulos |
7 | | * |
8 | | * This file is part of GnuTLS. |
9 | | * |
10 | | * The GnuTLS is free software; you can redistribute it and/or |
11 | | * modify it under the terms of the GNU Lesser General Public License |
12 | | * as published by the Free Software Foundation; either version 2.1 of |
13 | | * the License, or (at your option) any later version. |
14 | | * |
15 | | * This library is distributed in the hope that it will be useful, but |
16 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | | * Lesser General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU Lesser General Public License |
21 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
22 | | * |
23 | | */ |
24 | | |
25 | | /* Functions that are record layer specific, are included in this file. |
26 | | */ |
27 | | |
28 | | /* allocate this many bytes more when encrypting or decrypting, to |
29 | | * compensate for broken backends such as cryptodev. |
30 | | */ |
31 | 0 | #define CIPHER_SLACK_SIZE 32 |
32 | | |
33 | | #include "gnutls_int.h" |
34 | | #include "errors.h" |
35 | | #include "debug.h" |
36 | | #include "cipher.h" |
37 | | #include "buffers.h" |
38 | | #include "mbuffers.h" |
39 | | #include "handshake.h" |
40 | | #include "hash_int.h" |
41 | | #include "cipher_int.h" |
42 | | #include "algorithms.h" |
43 | | #include "db.h" |
44 | | #include "auth.h" |
45 | | #include "num.h" |
46 | | #include "record.h" |
47 | | #include "datum.h" |
48 | | #include "constate.h" |
49 | | #include "tls13/key_update.h" |
50 | | #include "ext/heartbeat.h" |
51 | | #include "state.h" |
52 | | #include "dtls.h" |
53 | | #include "dh.h" |
54 | | #include "random.h" |
55 | | #include <xsize.h> |
56 | | #include "locks.h" |
57 | | #include "system/ktls.h" |
58 | | #include <intprops.h> |
59 | | |
60 | | struct tls_record_st { |
61 | | uint16_t header_size; |
62 | | uint8_t version[2]; |
63 | | uint64_t sequence; /* DTLS */ |
64 | | uint16_t length; |
65 | | uint16_t packet_size; /* header_size + length */ |
66 | | content_type_t type; |
67 | | uint16_t epoch; /* valid in DTLS only */ |
68 | | #ifdef ENABLE_SSL2 |
69 | | unsigned v2 : 1; /* whether an SSLv2 client hello */ |
70 | | #endif |
71 | | /* the data */ |
72 | | }; |
73 | | |
74 | | /** |
75 | | * gnutls_record_disable_padding: |
76 | | * @session: is a #gnutls_session_t type. |
77 | | * |
78 | | * Used to disabled padding in TLS 1.0 and above. Normally you do not |
79 | | * need to use this function, but there are buggy clients that |
80 | | * complain if a server pads the encrypted data. This of course will |
81 | | * disable protection against statistical attacks on the data. |
82 | | * |
83 | | * This function is defunct since 3.1.7. Random padding is disabled |
84 | | * by default unless requested using gnutls_record_send_range(). |
85 | | * |
86 | | **/ |
87 | | void gnutls_record_disable_padding(gnutls_session_t session) |
88 | 0 | { |
89 | 0 | return; |
90 | 0 | } |
91 | | |
92 | | /** |
93 | | * gnutls_transport_set_ptr: |
94 | | * @session: is a #gnutls_session_t type. |
95 | | * @ptr: is the value. |
96 | | * |
97 | | * Used to set the first argument of the transport function (for push |
98 | | * and pull callbacks). In berkeley style sockets this function will set the |
99 | | * connection descriptor. |
100 | | * |
101 | | **/ |
102 | | void gnutls_transport_set_ptr(gnutls_session_t session, |
103 | | gnutls_transport_ptr_t ptr) |
104 | 0 | { |
105 | 0 | session->internals.transport_recv_ptr = ptr; |
106 | 0 | session->internals.transport_send_ptr = ptr; |
107 | 0 | } |
108 | | |
109 | | /** |
110 | | * gnutls_transport_set_ptr2: |
111 | | * @session: is a #gnutls_session_t type. |
112 | | * @recv_ptr: is the value for the pull function |
113 | | * @send_ptr: is the value for the push function |
114 | | * |
115 | | * Used to set the first argument of the transport function (for push |
116 | | * and pull callbacks). In berkeley style sockets this function will set the |
117 | | * connection descriptor. With this function you can use two different |
118 | | * pointers for receiving and sending. |
119 | | **/ |
120 | | void gnutls_transport_set_ptr2(gnutls_session_t session, |
121 | | gnutls_transport_ptr_t recv_ptr, |
122 | | gnutls_transport_ptr_t send_ptr) |
123 | 0 | { |
124 | 0 | session->internals.transport_send_ptr = send_ptr; |
125 | 0 | session->internals.transport_recv_ptr = recv_ptr; |
126 | 0 | } |
127 | | |
128 | | /** |
129 | | * gnutls_transport_set_int2: |
130 | | * @session: is a #gnutls_session_t type. |
131 | | * @recv_fd: is socket descriptor for the pull function |
132 | | * @send_fd: is socket descriptor for the push function |
133 | | * |
134 | | * This function sets the first argument of the transport functions, |
135 | | * such as send() and recv() for the default callbacks using the |
136 | | * system's socket API. With this function you can set two different |
137 | | * descriptors for receiving and sending. |
138 | | * |
139 | | * This function is equivalent to calling gnutls_transport_set_ptr2() |
140 | | * with the descriptors, but requires no casts. |
141 | | * |
142 | | * Since: 3.1.9 |
143 | | **/ |
144 | | void gnutls_transport_set_int2(gnutls_session_t session, int recv_fd, |
145 | | int send_fd) |
146 | 0 | { |
147 | 0 | session->internals.transport_send_ptr = |
148 | 0 | (gnutls_transport_ptr_t)(long)send_fd; |
149 | 0 | session->internals.transport_recv_ptr = |
150 | 0 | (gnutls_transport_ptr_t)(long)recv_fd; |
151 | 0 | } |
152 | | |
153 | | #if 0 |
154 | | /* this will be a macro */ |
155 | | /** |
156 | | * gnutls_transport_set_int: |
157 | | * @session: is a #gnutls_session_t type. |
158 | | * @fd: is the socket descriptor for the connection. |
159 | | * |
160 | | * This function sets the first argument of the transport function, such |
161 | | * as send() and recv() for the default callbacks using the |
162 | | * system's socket API. |
163 | | * |
164 | | * This function is equivalent to calling gnutls_transport_set_ptr() |
165 | | * with the descriptor, but requires no casts. |
166 | | * |
167 | | * Since: 3.1.9 |
168 | | * |
169 | | **/ |
170 | | void gnutls_transport_set_int(gnutls_session_t session, int fd) |
171 | | { |
172 | | session->internals.transport_recv_ptr = |
173 | | (gnutls_transport_ptr_t) (long)fd; |
174 | | session->internals.transport_send_ptr = |
175 | | (gnutls_transport_ptr_t) (long)fd; |
176 | | } |
177 | | #endif |
178 | | |
179 | | /** |
180 | | * gnutls_transport_get_ptr: |
181 | | * @session: is a #gnutls_session_t type. |
182 | | * |
183 | | * Used to get the first argument of the transport function (like |
184 | | * PUSH and PULL). This must have been set using |
185 | | * gnutls_transport_set_ptr(). |
186 | | * |
187 | | * Returns: The first argument of the transport function. |
188 | | **/ |
189 | | gnutls_transport_ptr_t gnutls_transport_get_ptr(gnutls_session_t session) |
190 | 0 | { |
191 | 0 | return session->internals.transport_recv_ptr; |
192 | 0 | } |
193 | | |
194 | | /** |
195 | | * gnutls_transport_get_ptr2: |
196 | | * @session: is a #gnutls_session_t type. |
197 | | * @recv_ptr: will hold the value for the pull function |
198 | | * @send_ptr: will hold the value for the push function |
199 | | * |
200 | | * Used to get the arguments of the transport functions (like PUSH |
201 | | * and PULL). These should have been set using |
202 | | * gnutls_transport_set_ptr2(). |
203 | | **/ |
204 | | void gnutls_transport_get_ptr2(gnutls_session_t session, |
205 | | gnutls_transport_ptr_t *recv_ptr, |
206 | | gnutls_transport_ptr_t *send_ptr) |
207 | 0 | { |
208 | 0 | *recv_ptr = session->internals.transport_recv_ptr; |
209 | 0 | *send_ptr = session->internals.transport_send_ptr; |
210 | 0 | } |
211 | | |
212 | | /** |
213 | | * gnutls_transport_get_int2: |
214 | | * @session: is a #gnutls_session_t type. |
215 | | * @recv_int: will hold the value for the pull function |
216 | | * @send_int: will hold the value for the push function |
217 | | * |
218 | | * Used to get the arguments of the transport functions (like PUSH |
219 | | * and PULL). These should have been set using |
220 | | * gnutls_transport_set_int2(). |
221 | | * |
222 | | * Since: 3.1.9 |
223 | | **/ |
224 | | void gnutls_transport_get_int2(gnutls_session_t session, int *recv_int, |
225 | | int *send_int) |
226 | 0 | { |
227 | 0 | *recv_int = (long)session->internals.transport_recv_ptr; |
228 | 0 | *send_int = (long)session->internals.transport_send_ptr; |
229 | 0 | } |
230 | | |
231 | | /** |
232 | | * gnutls_transport_get_int: |
233 | | * @session: is a #gnutls_session_t type. |
234 | | * |
235 | | * Used to get the first argument of the transport function (like |
236 | | * PUSH and PULL). This must have been set using |
237 | | * gnutls_transport_set_int(). |
238 | | * |
239 | | * Returns: The first argument of the transport function. |
240 | | * |
241 | | * Since: 3.1.9 |
242 | | **/ |
243 | | int gnutls_transport_get_int(gnutls_session_t session) |
244 | 0 | { |
245 | 0 | return (long)session->internals.transport_recv_ptr; |
246 | 0 | } |
247 | | |
248 | | /** |
249 | | * gnutls_bye: |
250 | | * @session: is a #gnutls_session_t type. |
251 | | * @how: is an integer |
252 | | * |
253 | | * Terminates the current TLS/SSL connection. The connection should |
254 | | * have been initiated using gnutls_handshake(). @how should be one |
255 | | * of %GNUTLS_SHUT_RDWR, %GNUTLS_SHUT_WR. |
256 | | * |
257 | | * In case of %GNUTLS_SHUT_RDWR the TLS session gets |
258 | | * terminated and further receives and sends will be disallowed. If |
259 | | * the return value is zero you may continue using the underlying |
260 | | * transport layer. %GNUTLS_SHUT_RDWR sends an alert containing a close |
261 | | * request and waits for the peer to reply with the same message. |
262 | | * |
263 | | * In case of %GNUTLS_SHUT_WR the TLS session gets terminated |
264 | | * and further sends will be disallowed. In order to reuse the |
265 | | * connection you should wait for an EOF from the peer. |
266 | | * %GNUTLS_SHUT_WR sends an alert containing a close request. |
267 | | * |
268 | | * Note that not all implementations will properly terminate a TLS |
269 | | * connection. Some of them, usually for performance reasons, will |
270 | | * terminate only the underlying transport layer, and thus not |
271 | | * distinguishing between a malicious party prematurely terminating |
272 | | * the connection and normal termination. |
273 | | * |
274 | | * This function may also return %GNUTLS_E_AGAIN or |
275 | | * %GNUTLS_E_INTERRUPTED; cf. gnutls_record_get_direction(). |
276 | | * |
277 | | * Returns: %GNUTLS_E_SUCCESS on success, or an error code, see |
278 | | * function documentation for entire semantics. |
279 | | **/ |
280 | | int gnutls_bye(gnutls_session_t session, gnutls_close_request_t how) |
281 | 0 | { |
282 | 0 | int ret = 0; |
283 | |
|
284 | 0 | switch (BYE_STATE) { |
285 | 0 | case BYE_STATE0: |
286 | 0 | if (!IS_KTLS_ENABLED(session, GNUTLS_KTLS_SEND)) |
287 | 0 | ret = _gnutls_io_write_flush(session); |
288 | 0 | BYE_STATE = BYE_STATE0; |
289 | 0 | if (ret < 0) { |
290 | 0 | gnutls_assert(); |
291 | 0 | return ret; |
292 | 0 | } |
293 | 0 | FALLTHROUGH; |
294 | 0 | case BYE_STATE1: |
295 | 0 | ret = gnutls_alert_send(session, GNUTLS_AL_WARNING, |
296 | 0 | GNUTLS_A_CLOSE_NOTIFY); |
297 | 0 | BYE_STATE = BYE_STATE1; |
298 | 0 | if (ret < 0) { |
299 | 0 | gnutls_assert(); |
300 | 0 | return ret; |
301 | 0 | } |
302 | 0 | FALLTHROUGH; |
303 | 0 | case BYE_STATE2: |
304 | 0 | BYE_STATE = BYE_STATE2; |
305 | 0 | if (how == GNUTLS_SHUT_RDWR) { |
306 | 0 | if (IS_KTLS_ENABLED(session, GNUTLS_KTLS_SEND)) { |
307 | 0 | do { |
308 | 0 | ret = _gnutls_ktls_recv_int( |
309 | 0 | session, GNUTLS_ALERT, NULL, 0); |
310 | 0 | } while (ret == GNUTLS_E_GOT_APPLICATION_DATA); |
311 | 0 | } else { |
312 | 0 | do { |
313 | 0 | ret = _gnutls_recv_int( |
314 | 0 | session, GNUTLS_ALERT, NULL, 0, |
315 | 0 | NULL, |
316 | 0 | session->internals |
317 | 0 | .record_timeout_ms); |
318 | 0 | } while (ret == GNUTLS_E_GOT_APPLICATION_DATA); |
319 | 0 | } |
320 | |
|
321 | 0 | if (ret >= 0) |
322 | 0 | session->internals.may_not_read = 1; |
323 | |
|
324 | 0 | if (ret < 0) { |
325 | 0 | gnutls_assert(); |
326 | 0 | return ret; |
327 | 0 | } |
328 | 0 | } |
329 | 0 | BYE_STATE = BYE_STATE2; |
330 | |
|
331 | 0 | break; |
332 | 0 | default: |
333 | 0 | gnutls_assert(); |
334 | 0 | return GNUTLS_E_INTERNAL_ERROR; |
335 | 0 | } |
336 | | |
337 | 0 | BYE_STATE = BYE_STATE0; |
338 | |
|
339 | 0 | session->internals.may_not_write = 1; |
340 | 0 | return 0; |
341 | 0 | } |
342 | | |
343 | | inline static void session_unresumable(gnutls_session_t session) |
344 | 0 | { |
345 | 0 | session->internals.resumable = false; |
346 | 0 | } |
347 | | |
348 | | /* returns 0 if session is valid |
349 | | */ |
350 | | inline static int session_is_valid(gnutls_session_t session) |
351 | 0 | { |
352 | 0 | if (session->internals.invalid_connection != 0) |
353 | 0 | return GNUTLS_E_INVALID_SESSION; |
354 | | |
355 | 0 | return 0; |
356 | 0 | } |
357 | | |
358 | | /* Copies the record version into the headers. The |
359 | | * version must have 2 bytes at least. |
360 | | */ |
361 | | inline static int copy_record_version(gnutls_session_t session, |
362 | | gnutls_handshake_description_t htype, |
363 | | uint8_t version[2]) |
364 | 0 | { |
365 | 0 | const version_entry_st *lver; |
366 | |
|
367 | 0 | lver = get_version(session); |
368 | 0 | if (session->internals.initial_negotiation_completed || |
369 | 0 | htype != GNUTLS_HANDSHAKE_CLIENT_HELLO || |
370 | 0 | (session->internals.hsk_flags & HSK_HRR_RECEIVED) || |
371 | 0 | session->internals.default_record_version[0] == 0) { |
372 | 0 | if (unlikely(lver == NULL)) |
373 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
374 | | |
375 | 0 | if (lver->tls13_sem) { |
376 | 0 | version[0] = 0x03; |
377 | 0 | version[1] = 0x03; |
378 | 0 | } else { |
379 | 0 | version[0] = lver->major; |
380 | 0 | version[1] = lver->minor; |
381 | 0 | } |
382 | 0 | } else { |
383 | 0 | version[0] = session->internals.default_record_version[0]; |
384 | 0 | version[1] = session->internals.default_record_version[1]; |
385 | 0 | } |
386 | | |
387 | 0 | return 0; |
388 | 0 | } |
389 | | |
390 | | /* Increments the sequence value |
391 | | */ |
392 | | inline static int sequence_increment(gnutls_session_t session, uint64_t *value) |
393 | 0 | { |
394 | 0 | uint64_t snmax = UINT64_C(0xffffffffffffffff); |
395 | |
|
396 | 0 | if (IS_DTLS(session)) { |
397 | 0 | uint64_t mask; |
398 | |
|
399 | 0 | snmax = UINT64_C(0xffffffffffff); |
400 | 0 | mask = snmax; |
401 | |
|
402 | 0 | if ((*value & mask) == snmax) |
403 | 0 | return -1; |
404 | | |
405 | 0 | *value = ((*value & mask) + 1) | (*value & ~mask); |
406 | 0 | } else { |
407 | 0 | if (*value == snmax) |
408 | 0 | return -1; |
409 | | |
410 | 0 | (*value)++; |
411 | 0 | } |
412 | | |
413 | 0 | return 0; |
414 | 0 | } |
415 | | |
416 | | /* This function behaves exactly like write(). The only difference is |
417 | | * that it accepts, the gnutls_session_t and the content_type_t of data to |
418 | | * send (if called by the user the Content is specific) |
419 | | * It is intended to transfer data, under the current session. |
420 | | * |
421 | | * @type: The content type to send |
422 | | * @htype: If this is a handshake message then the handshake type |
423 | | * @epoch_rel: %EPOCH_READ_* or %EPOCH_WRITE_* |
424 | | * @data: the data to be sent |
425 | | * @data_size: the size of the @data |
426 | | * @min_pad: the minimum required padding |
427 | | * @mflags: zero or %MBUFFER_FLUSH |
428 | | * |
429 | | * Oct 30 2001: Removed capability to send data more than MAX_RECORD_SIZE. |
430 | | * This makes the function much easier to read, and more error resistant |
431 | | * (there were cases were the old function could mess everything up). |
432 | | * --nmav |
433 | | * |
434 | | * This function may accept a NULL pointer for data, and 0 for size, if |
435 | | * and only if the previous send was interrupted for some reason. |
436 | | * |
437 | | */ |
438 | | ssize_t _gnutls_send_tlen_int(gnutls_session_t session, content_type_t type, |
439 | | gnutls_handshake_description_t htype, |
440 | | unsigned int epoch_rel, const void *_data, |
441 | | size_t data_size, size_t min_pad, |
442 | | unsigned int mflags) |
443 | 0 | { |
444 | 0 | mbuffer_st *bufel; |
445 | 0 | ssize_t cipher_size; |
446 | 0 | int retval, ret; |
447 | 0 | int send_data_size; |
448 | 0 | uint8_t *headers; |
449 | 0 | int header_size; |
450 | 0 | const uint8_t *data = _data; |
451 | 0 | record_parameters_st *record_params; |
452 | 0 | size_t max_send_size; |
453 | 0 | record_state_st *record_state; |
454 | 0 | const version_entry_st *vers = get_version(session); |
455 | |
|
456 | 0 | ret = _gnutls_epoch_get(session, epoch_rel, &record_params); |
457 | 0 | if (ret < 0) |
458 | 0 | return gnutls_assert_val(ret); |
459 | | |
460 | | /* Safeguard against processing data with an incomplete cipher state. */ |
461 | 0 | if (!record_params->initialized) |
462 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
463 | | |
464 | 0 | record_state = &record_params->write; |
465 | | |
466 | | /* Do not allow null pointer if the send buffer is empty. |
467 | | * If the previous send was interrupted then a null pointer is |
468 | | * ok, and means to resume. |
469 | | */ |
470 | 0 | if (session->internals.record_send_buffer.byte_length == 0 && |
471 | 0 | (data_size == 0 && _data == NULL)) { |
472 | 0 | gnutls_assert(); |
473 | 0 | return GNUTLS_E_INVALID_REQUEST; |
474 | 0 | } |
475 | | |
476 | 0 | if (type != GNUTLS_ALERT) /* alert messages are sent anyway */ |
477 | 0 | if (session_is_valid(session) || |
478 | 0 | session->internals.may_not_write != 0) { |
479 | 0 | gnutls_assert(); |
480 | 0 | return GNUTLS_E_INVALID_SESSION; |
481 | 0 | } |
482 | | |
483 | 0 | max_send_size = max_record_send_size(session); |
484 | |
|
485 | 0 | if (data_size > max_send_size) { |
486 | 0 | if (IS_DTLS(session)) |
487 | 0 | return gnutls_assert_val(GNUTLS_E_LARGE_PACKET); |
488 | | |
489 | 0 | send_data_size = max_send_size; |
490 | 0 | } else |
491 | 0 | send_data_size = data_size; |
492 | | |
493 | | /* Only encrypt if we don't have data to send |
494 | | * from the previous run. - probably interrupted. |
495 | | */ |
496 | 0 | if (mflags != 0 && |
497 | 0 | session->internals.record_send_buffer.byte_length > 0) { |
498 | 0 | ret = _gnutls_io_write_flush(session); |
499 | 0 | if (ret > 0) |
500 | 0 | cipher_size = ret; |
501 | 0 | else |
502 | 0 | cipher_size = 0; |
503 | |
|
504 | 0 | retval = session->internals.record_send_buffer_user_size; |
505 | 0 | } else { |
506 | 0 | if (unlikely((send_data_size == 0 && min_pad == 0))) |
507 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
508 | | |
509 | | /* now proceed to packet encryption |
510 | | */ |
511 | 0 | cipher_size = max_record_send_size(session) + |
512 | 0 | MAX_RECORD_SEND_OVERHEAD(session); |
513 | |
|
514 | 0 | bufel = _mbuffer_alloc_align16( |
515 | 0 | cipher_size + CIPHER_SLACK_SIZE, |
516 | 0 | get_total_headers2(session, record_params)); |
517 | 0 | if (bufel == NULL) |
518 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
519 | | |
520 | 0 | headers = _mbuffer_get_uhead_ptr(bufel); |
521 | 0 | if (vers->tls13_sem && |
522 | 0 | record_params->cipher->id != GNUTLS_CIPHER_NULL) |
523 | 0 | headers[0] = GNUTLS_APPLICATION_DATA; |
524 | 0 | else |
525 | 0 | headers[0] = type; |
526 | | |
527 | | /* Use the default record version, if it is |
528 | | * set. */ |
529 | 0 | ret = copy_record_version(session, htype, &headers[1]); |
530 | 0 | if (ret < 0) |
531 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
532 | | |
533 | | /* Adjust header length and add sequence for DTLS */ |
534 | 0 | if (IS_DTLS(session)) |
535 | 0 | _gnutls_write_uint64(record_state->sequence_number, |
536 | 0 | &headers[3]); |
537 | |
|
538 | 0 | _gnutls_record_log( |
539 | 0 | "REC[%p]: Preparing Packet %s(%d) with length: %d and min pad: %d\n", |
540 | 0 | session, _gnutls_packet2str(type), type, (int)data_size, |
541 | 0 | (int)min_pad); |
542 | |
|
543 | 0 | header_size = RECORD_HEADER_SIZE(session); |
544 | 0 | _mbuffer_set_udata_size(bufel, cipher_size); |
545 | 0 | _mbuffer_set_uhead_size(bufel, header_size); |
546 | |
|
547 | 0 | ret = _gnutls_encrypt(session, data, send_data_size, min_pad, |
548 | 0 | bufel, type, record_params); |
549 | 0 | if (ret <= 0) { |
550 | 0 | gnutls_assert(); |
551 | 0 | if (ret == 0) |
552 | 0 | ret = GNUTLS_E_ENCRYPTION_FAILED; |
553 | 0 | gnutls_free(bufel); |
554 | 0 | return ret; /* error */ |
555 | 0 | } |
556 | | |
557 | 0 | cipher_size = _mbuffer_get_udata_size(bufel); |
558 | 0 | retval = send_data_size; |
559 | 0 | session->internals.record_send_buffer_user_size = |
560 | 0 | send_data_size; |
561 | | |
562 | | /* increase sequence number |
563 | | */ |
564 | 0 | if (sequence_increment(session, |
565 | 0 | &record_state->sequence_number) != 0) { |
566 | 0 | session_invalidate(session); |
567 | 0 | gnutls_free(bufel); |
568 | 0 | return gnutls_assert_val(GNUTLS_E_RECORD_LIMIT_REACHED); |
569 | 0 | } |
570 | | |
571 | 0 | ret = _gnutls_io_write_buffered(session, bufel, mflags); |
572 | 0 | } |
573 | | |
574 | 0 | if (ret != cipher_size) { |
575 | | /* If we have sent any data then just return |
576 | | * the error value. Do not invalidate the session. |
577 | | */ |
578 | 0 | if (ret < 0 && gnutls_error_is_fatal(ret) == 0) |
579 | 0 | return gnutls_assert_val(ret); |
580 | | |
581 | 0 | if (ret > 0) |
582 | 0 | ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
583 | |
|
584 | 0 | session_unresumable(session); |
585 | 0 | session->internals.may_not_write = 1; |
586 | 0 | return gnutls_assert_val(ret); |
587 | 0 | } |
588 | | |
589 | 0 | session->internals.record_send_buffer_user_size = 0; |
590 | |
|
591 | 0 | _gnutls_record_log( |
592 | 0 | "REC[%p]: Sent Packet[%ld] %s(%d) in epoch %d and length: %d\n", |
593 | 0 | session, (unsigned long)(record_state->sequence_number), |
594 | 0 | _gnutls_packet2str(type), type, (int)record_params->epoch, |
595 | 0 | (int)cipher_size); |
596 | |
|
597 | 0 | if (vers->tls13_sem && |
598 | 0 | !(session->internals.flags & GNUTLS_NO_AUTO_REKEY) && |
599 | 0 | !(record_params->cipher->flags & GNUTLS_CIPHER_FLAG_NO_REKEY)) { |
600 | 0 | if (unlikely((record_state->sequence_number & |
601 | 0 | UINT64_C(0xffffff)) == UINT64_C(0xfffffd))) { |
602 | | /* After we have sent 2^24 messages, mark the session |
603 | | * as needing a key update. */ |
604 | 0 | session->internals.rsend_state = |
605 | 0 | RECORD_SEND_KEY_UPDATE_1; |
606 | 0 | } |
607 | 0 | } |
608 | |
|
609 | 0 | return retval; |
610 | 0 | } |
611 | | |
612 | | inline static int check_recv_type(gnutls_session_t session, |
613 | | content_type_t recv_type) |
614 | 0 | { |
615 | 0 | switch (recv_type) { |
616 | 0 | case GNUTLS_CHANGE_CIPHER_SPEC: |
617 | 0 | case GNUTLS_ALERT: |
618 | 0 | case GNUTLS_HANDSHAKE: |
619 | 0 | case GNUTLS_HEARTBEAT: |
620 | 0 | case GNUTLS_APPLICATION_DATA: |
621 | 0 | return 0; |
622 | 0 | default: |
623 | 0 | gnutls_assert(); |
624 | 0 | _gnutls_audit_log(session, |
625 | 0 | "Received record packet of unknown type %u\n", |
626 | 0 | (unsigned int)recv_type); |
627 | 0 | return GNUTLS_E_UNEXPECTED_PACKET; |
628 | 0 | } |
629 | 0 | } |
630 | | |
631 | | /* Checks if there are pending data in the record buffers. If there are |
632 | | * then it copies the data. |
633 | | */ |
634 | | static int get_data_from_buffers(gnutls_session_t session, content_type_t type, |
635 | | uint8_t *data, int data_size, void *seq) |
636 | 0 | { |
637 | 0 | if ((type == GNUTLS_APPLICATION_DATA || type == GNUTLS_HANDSHAKE || |
638 | 0 | type == GNUTLS_CHANGE_CIPHER_SPEC) && |
639 | 0 | _gnutls_record_buffer_get_size(session) > 0) { |
640 | 0 | int ret; |
641 | 0 | ret = _gnutls_record_buffer_get(type, session, data, data_size, |
642 | 0 | seq); |
643 | 0 | if (ret < 0) { |
644 | 0 | if (IS_DTLS(session)) { |
645 | 0 | if (ret == GNUTLS_E_UNEXPECTED_PACKET) { |
646 | 0 | ret = GNUTLS_E_AGAIN; |
647 | 0 | } |
648 | 0 | } |
649 | 0 | gnutls_assert(); |
650 | 0 | return ret; |
651 | 0 | } |
652 | | |
653 | 0 | return ret; |
654 | 0 | } |
655 | | |
656 | 0 | return 0; |
657 | 0 | } |
658 | | |
659 | | /* Checks and retrieves any pending data in the application data record buffers. |
660 | | */ |
661 | | static int get_packet_from_buffers(gnutls_session_t session, |
662 | | content_type_t type, gnutls_packet_t *packet) |
663 | 0 | { |
664 | 0 | if (_gnutls_record_buffer_get_size(session) > 0) { |
665 | 0 | int ret; |
666 | 0 | ret = _gnutls_record_buffer_get_packet(type, session, packet); |
667 | 0 | if (ret < 0) { |
668 | 0 | if (IS_DTLS(session)) { |
669 | 0 | if (ret == GNUTLS_E_UNEXPECTED_PACKET) { |
670 | 0 | ret = GNUTLS_E_AGAIN; |
671 | 0 | } |
672 | 0 | } |
673 | 0 | gnutls_assert(); |
674 | 0 | return ret; |
675 | 0 | } |
676 | | |
677 | 0 | return ret; |
678 | 0 | } |
679 | | |
680 | 0 | *packet = NULL; |
681 | 0 | return 0; |
682 | 0 | } |
683 | | |
684 | | /* Here we check if the advertised version is the one we |
685 | | * negotiated in the handshake. |
686 | | */ |
687 | | inline static int record_check_version(gnutls_session_t session, |
688 | | gnutls_handshake_description_t htype, |
689 | | uint8_t version[2]) |
690 | 0 | { |
691 | 0 | const version_entry_st *vers = get_version(session); |
692 | 0 | int diff = 0; |
693 | |
|
694 | 0 | if (vers->tls13_sem) { |
695 | | /* TLS 1.3 requires version to be 0x0303 */ |
696 | 0 | if (version[0] != 0x03 || version[1] != 0x03) |
697 | 0 | diff = 1; |
698 | 0 | } else { |
699 | 0 | if (vers->major != version[0] || vers->minor != version[1]) |
700 | 0 | diff = 1; |
701 | 0 | } |
702 | |
|
703 | 0 | if (!IS_DTLS(session)) { |
704 | 0 | if (htype == GNUTLS_HANDSHAKE_CLIENT_HELLO || |
705 | 0 | htype == GNUTLS_HANDSHAKE_HELLO_RETRY_REQUEST || |
706 | 0 | htype == GNUTLS_HANDSHAKE_SERVER_HELLO) { |
707 | 0 | if (version[0] != 3) { |
708 | 0 | gnutls_assert(); |
709 | 0 | _gnutls_record_log( |
710 | 0 | "REC[%p]: INVALID VERSION PACKET: (%d) %d.%d\n", |
711 | 0 | session, htype, version[0], version[1]); |
712 | 0 | return GNUTLS_E_UNSUPPORTED_VERSION_PACKET; |
713 | 0 | } |
714 | 0 | } else if (diff != 0) { |
715 | | /* Reject record packets that have a different version than the |
716 | | * one negotiated. Note that this version is not protected by any |
717 | | * mac. I don't really think that this check serves any purpose. |
718 | | */ |
719 | 0 | gnutls_assert(); |
720 | 0 | _gnutls_record_log( |
721 | 0 | "REC[%p]: INVALID VERSION PACKET: (%d) %d.%d\n", |
722 | 0 | session, htype, version[0], version[1]); |
723 | |
|
724 | 0 | return GNUTLS_E_UNSUPPORTED_VERSION_PACKET; |
725 | 0 | } |
726 | 0 | } else { /* DTLS */ |
727 | | |
728 | | /* In DTLS the only information we have here is whether we |
729 | | * expect a handshake message or not. |
730 | | */ |
731 | 0 | if (htype == (gnutls_handshake_description_t)-1) { |
732 | 0 | if (diff) { |
733 | | /* Reject record packets that have a different version than the |
734 | | * one negotiated. Note that this version is not protected by any |
735 | | * mac. I don't really think that this check serves any purpose. |
736 | | */ |
737 | 0 | gnutls_assert(); |
738 | 0 | _gnutls_record_log( |
739 | 0 | "REC[%p]: INVALID VERSION PACKET: (%d) %d.%d\n", |
740 | 0 | session, htype, version[0], version[1]); |
741 | |
|
742 | 0 | return GNUTLS_E_UNSUPPORTED_VERSION_PACKET; |
743 | 0 | } |
744 | 0 | } else if (vers->id > GNUTLS_DTLS1_0 && version[0] > 254) { |
745 | 0 | gnutls_assert(); |
746 | 0 | _gnutls_record_log( |
747 | 0 | "REC[%p]: INVALID DTLS VERSION PACKET: (%d) %d.%d\n", |
748 | 0 | session, htype, version[0], version[1]); |
749 | 0 | return GNUTLS_E_UNSUPPORTED_VERSION_PACKET; |
750 | 0 | } else if (vers->id == GNUTLS_DTLS0_9 && version[0] > 1) { |
751 | 0 | gnutls_assert(); |
752 | 0 | _gnutls_record_log( |
753 | 0 | "REC[%p]: INVALID DTLS VERSION PACKET: (%d) %d.%d\n", |
754 | 0 | session, htype, version[0], version[1]); |
755 | 0 | return GNUTLS_E_UNSUPPORTED_VERSION_PACKET; |
756 | 0 | } |
757 | 0 | } |
758 | | |
759 | 0 | return 0; |
760 | 0 | } |
761 | | |
762 | | static int recv_hello_request(gnutls_session_t session, void *data, |
763 | | uint32_t data_size) |
764 | 0 | { |
765 | 0 | uint8_t type; |
766 | |
|
767 | 0 | if (session->security_parameters.entity == GNUTLS_SERVER) |
768 | 0 | return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET); |
769 | | |
770 | 0 | if (data_size < 1) |
771 | 0 | return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH); |
772 | | |
773 | 0 | if (session->internals.handshake_in_progress) |
774 | 0 | return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET); |
775 | | |
776 | 0 | type = ((uint8_t *)data)[0]; |
777 | 0 | if (type == GNUTLS_HANDSHAKE_HELLO_REQUEST) { |
778 | 0 | if (IS_DTLS(session)) |
779 | 0 | session->internals.dtls.hsk_read_seq++; |
780 | 0 | if (session->internals.flags & GNUTLS_AUTO_REAUTH) { |
781 | 0 | session->internals.recv_state = RECV_STATE_REHANDSHAKE; |
782 | 0 | return GNUTLS_E_AGAIN; |
783 | 0 | } else { |
784 | 0 | return GNUTLS_E_REHANDSHAKE; |
785 | 0 | } |
786 | 0 | } else { |
787 | 0 | gnutls_assert(); |
788 | 0 | return GNUTLS_E_UNEXPECTED_PACKET; |
789 | 0 | } |
790 | 0 | } |
791 | | |
792 | | /* This function will check if the received record type is |
793 | | * the one we actually expect and adds it to the proper |
794 | | * buffer. The bufel will be deinitialized after calling |
795 | | * this function, even if it fails. |
796 | | */ |
797 | | static int record_add_to_buffers(gnutls_session_t session, |
798 | | struct tls_record_st *recv, |
799 | | content_type_t type, |
800 | | gnutls_handshake_description_t htype, |
801 | | uint64_t seq, mbuffer_st *bufel) |
802 | 0 | { |
803 | 0 | int ret; |
804 | 0 | const version_entry_st *ver = get_version(session); |
805 | |
|
806 | 0 | if ((recv->type == type) && |
807 | 0 | (type == GNUTLS_APPLICATION_DATA || |
808 | 0 | type == GNUTLS_CHANGE_CIPHER_SPEC || type == GNUTLS_HANDSHAKE)) { |
809 | 0 | if (bufel->msg.size == 0) { |
810 | 0 | if (type == GNUTLS_APPLICATION_DATA) { |
811 | | /* this is needed to distinguish an empty |
812 | | * message and EOF */ |
813 | 0 | ret = GNUTLS_E_AGAIN; |
814 | 0 | goto cleanup; |
815 | 0 | } else { |
816 | 0 | ret = gnutls_assert_val( |
817 | 0 | GNUTLS_E_UNEXPECTED_PACKET); |
818 | 0 | goto unexpected_packet; |
819 | 0 | } |
820 | 0 | } |
821 | | |
822 | | /* application data cannot be inserted between (async) handshake |
823 | | * messages */ |
824 | 0 | if (type == GNUTLS_APPLICATION_DATA && |
825 | 0 | (session->internals.handshake_recv_buffer_size != 0 || |
826 | 0 | session->internals.handshake_header_recv_buffer.length != |
827 | 0 | 0)) { |
828 | 0 | ret = gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET); |
829 | 0 | goto unexpected_packet; |
830 | 0 | } |
831 | | |
832 | | /* if the CCS has value other than 1 abort the connection, |
833 | | * unless old DTLS is negotiated, where CCS includes a sequence |
834 | | * number */ |
835 | 0 | if (type == GNUTLS_CHANGE_CIPHER_SPEC && |
836 | 0 | !(ver && ver->id == GNUTLS_DTLS0_9) && |
837 | 0 | (bufel->msg.size != 1 || bufel->msg.data[0] != 1)) { |
838 | 0 | ret = gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET); |
839 | 0 | goto unexpected_packet; |
840 | 0 | } |
841 | | |
842 | 0 | _gnutls_record_buffer_put(session, type, seq, bufel); |
843 | | |
844 | | /* if we received application data as expected then we |
845 | | * deactivate the async timer */ |
846 | 0 | _dtls_async_timer_delete(session); |
847 | 0 | } else { |
848 | | /* if the expected type is different than the received |
849 | | */ |
850 | 0 | switch (recv->type) { |
851 | 0 | case GNUTLS_ALERT: |
852 | 0 | if (bufel->msg.size < 2) { |
853 | 0 | ret = gnutls_assert_val( |
854 | 0 | GNUTLS_E_UNEXPECTED_PACKET); |
855 | 0 | goto unexpected_packet; |
856 | 0 | } |
857 | | |
858 | 0 | _gnutls_record_log( |
859 | 0 | "REC[%p]: Alert[%d|%d] - %s - was received\n", |
860 | 0 | session, bufel->msg.data[0], bufel->msg.data[1], |
861 | 0 | gnutls_alert_get_name((int)bufel->msg.data[1])); |
862 | |
|
863 | 0 | if (!session->internals.initial_negotiation_completed && |
864 | 0 | session->internals.handshake_in_progress && |
865 | 0 | STATE == STATE0) { /* handshake hasn't started */ |
866 | 0 | ret = gnutls_assert_val( |
867 | 0 | GNUTLS_E_UNEXPECTED_PACKET); |
868 | 0 | goto unexpected_packet; |
869 | 0 | } |
870 | | |
871 | 0 | session->internals.last_alert = bufel->msg.data[1]; |
872 | | |
873 | | /* if close notify is received and |
874 | | * the alert is not fatal |
875 | | */ |
876 | 0 | if (bufel->msg.data[1] == GNUTLS_A_CLOSE_NOTIFY && |
877 | 0 | bufel->msg.data[0] != GNUTLS_AL_FATAL) { |
878 | | /* If we have been expecting for an alert do |
879 | | */ |
880 | 0 | session->internals.read_eof = 1; |
881 | 0 | ret = GNUTLS_E_SESSION_EOF; |
882 | 0 | goto cleanup; |
883 | 0 | } else { |
884 | | /* if the alert is FATAL or WARNING |
885 | | * return the appropriate message |
886 | | */ |
887 | 0 | gnutls_assert(); |
888 | 0 | ret = GNUTLS_E_WARNING_ALERT_RECEIVED; |
889 | 0 | if ((ver && ver->tls13_sem) || |
890 | 0 | bufel->msg.data[0] == GNUTLS_AL_FATAL) { |
891 | 0 | session_unresumable(session); |
892 | 0 | session_invalidate(session); |
893 | 0 | ret = gnutls_assert_val( |
894 | 0 | GNUTLS_E_FATAL_ALERT_RECEIVED); |
895 | 0 | } |
896 | 0 | goto cleanup; |
897 | 0 | } |
898 | 0 | break; |
899 | | |
900 | 0 | case GNUTLS_CHANGE_CIPHER_SPEC: |
901 | 0 | if (!(IS_DTLS(session))) { |
902 | 0 | ret = gnutls_assert_val( |
903 | 0 | GNUTLS_E_UNEXPECTED_PACKET); |
904 | 0 | goto cleanup; |
905 | 0 | } |
906 | | |
907 | | /* if the CCS has value other than 1 abort the |
908 | | * connection, unless old DTLS is negotiated, where CCS |
909 | | * includes a sequence number */ |
910 | 0 | if (!(ver && ver->id == GNUTLS_DTLS0_9) && |
911 | 0 | (bufel->msg.size != 1 || bufel->msg.data[0] != 1)) { |
912 | 0 | ret = gnutls_assert_val( |
913 | 0 | GNUTLS_E_UNEXPECTED_PACKET); |
914 | 0 | goto unexpected_packet; |
915 | 0 | } |
916 | | |
917 | 0 | _gnutls_record_buffer_put(session, recv->type, seq, |
918 | 0 | bufel); |
919 | |
|
920 | 0 | break; |
921 | | |
922 | | #ifdef ENABLE_HEARTBEAT |
923 | | case GNUTLS_HEARTBEAT: |
924 | | ret = _gnutls_heartbeat_handle(session, bufel); |
925 | | goto cleanup; |
926 | | #endif |
927 | | |
928 | 0 | case GNUTLS_APPLICATION_DATA: |
929 | 0 | if (session->internals.initial_negotiation_completed == |
930 | 0 | 0) { |
931 | 0 | ret = gnutls_assert_val( |
932 | 0 | GNUTLS_E_UNEXPECTED_PACKET); |
933 | 0 | goto unexpected_packet; |
934 | 0 | } |
935 | | |
936 | | /* In TLS1.3 post-handshake authentication allow application |
937 | | * data error code. */ |
938 | 0 | if ((ver && ver->tls13_sem) && |
939 | 0 | type == GNUTLS_HANDSHAKE && |
940 | 0 | htype == GNUTLS_HANDSHAKE_CERTIFICATE_PKT && |
941 | 0 | session->internals.initial_negotiation_completed) { |
942 | 0 | _gnutls_record_buffer_put(session, recv->type, |
943 | 0 | seq, bufel); |
944 | 0 | return gnutls_assert_val( |
945 | 0 | GNUTLS_E_GOT_APPLICATION_DATA); |
946 | 0 | } |
947 | | |
948 | | /* The got_application data is only returned |
949 | | * if expecting client hello (for rehandshake |
950 | | * reasons). Otherwise it is an unexpected packet |
951 | | */ |
952 | 0 | if (type == GNUTLS_ALERT || |
953 | 0 | ((htype == GNUTLS_HANDSHAKE_SERVER_HELLO || |
954 | 0 | htype == GNUTLS_HANDSHAKE_CLIENT_HELLO || |
955 | 0 | htype == GNUTLS_HANDSHAKE_HELLO_RETRY_REQUEST) && |
956 | 0 | type == GNUTLS_HANDSHAKE)) { |
957 | | /* even if data is unexpected put it into the buffer */ |
958 | 0 | _gnutls_record_buffer_put(session, recv->type, |
959 | 0 | seq, bufel); |
960 | 0 | return gnutls_assert_val( |
961 | 0 | GNUTLS_E_GOT_APPLICATION_DATA); |
962 | 0 | } else { |
963 | 0 | ret = gnutls_assert_val( |
964 | 0 | GNUTLS_E_UNEXPECTED_PACKET); |
965 | 0 | goto unexpected_packet; |
966 | 0 | } |
967 | | |
968 | 0 | break; |
969 | | |
970 | 0 | case GNUTLS_HANDSHAKE: |
971 | | /* In DTLS we might receive a handshake replay from the peer to indicate |
972 | | * the our last TLS handshake messages were not received. |
973 | | */ |
974 | 0 | if (IS_DTLS(session)) { |
975 | 0 | if (type == GNUTLS_CHANGE_CIPHER_SPEC) { |
976 | 0 | ret = gnutls_assert_val( |
977 | 0 | GNUTLS_E_UNEXPECTED_PACKET); |
978 | 0 | goto unexpected_packet; |
979 | 0 | } |
980 | | |
981 | 0 | if (_dtls_is_async(session) && |
982 | 0 | _dtls_async_timer_active(session)) { |
983 | 0 | if (session->security_parameters.entity == |
984 | 0 | GNUTLS_SERVER && |
985 | 0 | bufel->htype == |
986 | 0 | GNUTLS_HANDSHAKE_CLIENT_HELLO) { |
987 | | /* client requested rehandshake. Delete the timer */ |
988 | 0 | _dtls_async_timer_delete( |
989 | 0 | session); |
990 | 0 | } else { |
991 | 0 | session->internals.recv_state = |
992 | 0 | RECV_STATE_DTLS_RETRANSMIT; |
993 | 0 | ret = _dtls_retransmit(session); |
994 | 0 | if (ret == 0) { |
995 | 0 | session->internals |
996 | 0 | .recv_state = |
997 | 0 | RECV_STATE_0; |
998 | 0 | ret = gnutls_assert_val( |
999 | 0 | GNUTLS_E_AGAIN); |
1000 | 0 | goto unexpected_packet; |
1001 | 0 | } |
1002 | 0 | goto cleanup; |
1003 | 0 | } |
1004 | 0 | } |
1005 | 0 | } |
1006 | | |
1007 | | /* retrieve async handshake messages */ |
1008 | 0 | if (ver && ver->tls13_sem) { |
1009 | 0 | _gnutls_record_buffer_put(session, recv->type, |
1010 | 0 | seq, bufel); |
1011 | |
|
1012 | 0 | ret = _gnutls13_recv_async_handshake(session); |
1013 | 0 | if (ret < 0) |
1014 | 0 | return gnutls_assert_val(ret); |
1015 | | |
1016 | | /* bufel is now accounted */ |
1017 | 0 | return GNUTLS_E_AGAIN; |
1018 | 0 | } |
1019 | | |
1020 | | /* This is legal if HELLO_REQUEST is received - and we are a client. |
1021 | | * If we are a server, a client may initiate a renegotiation at any time. |
1022 | | */ |
1023 | 0 | if (session->security_parameters.entity == |
1024 | 0 | GNUTLS_SERVER && |
1025 | 0 | session->internals.handshake_in_progress == 0 && |
1026 | 0 | bufel->htype == GNUTLS_HANDSHAKE_CLIENT_HELLO) { |
1027 | 0 | gnutls_assert(); |
1028 | 0 | _gnutls_record_buffer_put(session, recv->type, |
1029 | 0 | seq, bufel); |
1030 | 0 | return GNUTLS_E_REHANDSHAKE; |
1031 | 0 | } |
1032 | | |
1033 | | /* If we are already in a handshake then a Hello |
1034 | | * Request is illegal. But here we don't really care |
1035 | | * since this message will never make it up here. |
1036 | | */ |
1037 | | |
1038 | | /* So we accept it, if it is a Hello. If not, this will |
1039 | | * fail and trigger flight retransmissions after some time. */ |
1040 | 0 | ret = recv_hello_request(session, bufel->msg.data, |
1041 | 0 | bufel->msg.size); |
1042 | 0 | goto unexpected_packet; |
1043 | 0 | default: |
1044 | |
|
1045 | 0 | _gnutls_record_log( |
1046 | 0 | "REC[%p]: Received unexpected packet %d (%s) expecting %d (%s)\n", |
1047 | 0 | session, recv->type, |
1048 | 0 | _gnutls_packet2str(recv->type), type, |
1049 | 0 | _gnutls_packet2str(type)); |
1050 | |
|
1051 | 0 | gnutls_assert(); |
1052 | 0 | ret = GNUTLS_E_UNEXPECTED_PACKET; |
1053 | 0 | goto unexpected_packet; |
1054 | 0 | } |
1055 | 0 | } |
1056 | | |
1057 | 0 | return 0; |
1058 | | |
1059 | 0 | unexpected_packet: |
1060 | |
|
1061 | 0 | if (IS_DTLS(session) && ret != GNUTLS_E_REHANDSHAKE) { |
1062 | 0 | _mbuffer_xfree(&bufel); |
1063 | 0 | RETURN_DTLS_EAGAIN_OR_TIMEOUT(session, ret); |
1064 | 0 | } |
1065 | | |
1066 | 0 | cleanup: |
1067 | 0 | _mbuffer_xfree(&bufel); |
1068 | 0 | return ret; |
1069 | 0 | } |
1070 | | |
1071 | | /* Checks the record headers and returns the length, version and |
1072 | | * content type. |
1073 | | */ |
1074 | | static void record_read_headers(gnutls_session_t session, |
1075 | | uint8_t headers[MAX_RECORD_HEADER_SIZE], |
1076 | | content_type_t type, |
1077 | | gnutls_handshake_description_t htype, |
1078 | | struct tls_record_st *record) |
1079 | 0 | { |
1080 | | /* Read the first two bytes to determine if this is a |
1081 | | * version 2 message |
1082 | | */ |
1083 | |
|
1084 | 0 | #ifdef ENABLE_SSL2 |
1085 | 0 | if (htype == GNUTLS_HANDSHAKE_CLIENT_HELLO && |
1086 | 0 | type == GNUTLS_HANDSHAKE && headers[0] > 127 && |
1087 | 0 | !(IS_DTLS(session))) { |
1088 | | /* if msb set and expecting handshake message |
1089 | | * it should be SSL 2 hello |
1090 | | */ |
1091 | 0 | record->version[0] = 3; /* assume SSL 3.0 */ |
1092 | 0 | record->version[1] = 0; |
1093 | |
|
1094 | 0 | record->length = (((headers[0] & 0x7f) << 8)) | headers[1]; |
1095 | | |
1096 | | /* SSL 2.0 headers */ |
1097 | 0 | record->header_size = record->packet_size = 2; |
1098 | 0 | record->type = |
1099 | 0 | GNUTLS_HANDSHAKE; /* we accept only v2 client hello |
1100 | | */ |
1101 | | |
1102 | | /* in order to assist the handshake protocol. |
1103 | | * V2 compatibility is a mess. |
1104 | | */ |
1105 | 0 | record->v2 = 1; |
1106 | 0 | record->epoch = 0; |
1107 | 0 | memset(&record->sequence, 0, sizeof(record->sequence)); |
1108 | |
|
1109 | 0 | _gnutls_record_log( |
1110 | 0 | "REC[%p]: SSL 2.0 %s packet received. Length: %d\n", |
1111 | 0 | session, _gnutls_packet2str(record->type), |
1112 | 0 | record->length); |
1113 | |
|
1114 | 0 | } else |
1115 | 0 | #endif |
1116 | 0 | { |
1117 | | /* dtls version 1.0 and TLS version 1.x */ |
1118 | 0 | #ifdef ENABLE_SSL2 |
1119 | 0 | record->v2 = 0; |
1120 | 0 | #endif |
1121 | |
|
1122 | 0 | record->type = headers[0]; |
1123 | 0 | record->version[0] = headers[1]; |
1124 | 0 | record->version[1] = headers[2]; |
1125 | |
|
1126 | 0 | if (IS_DTLS(session)) { |
1127 | 0 | record->sequence = _gnutls_read_uint64(&headers[3]); |
1128 | 0 | record->length = _gnutls_read_uint16(&headers[11]); |
1129 | 0 | record->epoch = record->sequence >> 48; |
1130 | 0 | } else { |
1131 | 0 | memset(&record->sequence, 0, sizeof(record->sequence)); |
1132 | 0 | record->length = _gnutls_read_uint16(&headers[3]); |
1133 | 0 | record->epoch = session->security_parameters.epoch_read; |
1134 | 0 | } |
1135 | |
|
1136 | 0 | _gnutls_record_log( |
1137 | 0 | "REC[%p]: SSL %d.%d %s packet received. Epoch %d, length: %d\n", |
1138 | 0 | session, (int)record->version[0], |
1139 | 0 | (int)record->version[1], |
1140 | 0 | _gnutls_packet2str(record->type), (int)record->epoch, |
1141 | 0 | record->length); |
1142 | 0 | } |
1143 | |
|
1144 | 0 | record->packet_size += record->length; |
1145 | 0 | } |
1146 | | |
1147 | | static int recv_headers(gnutls_session_t session, |
1148 | | record_parameters_st *record_params, |
1149 | | content_type_t type, |
1150 | | gnutls_handshake_description_t htype, |
1151 | | struct tls_record_st *record, unsigned int *ms) |
1152 | 0 | { |
1153 | 0 | int ret; |
1154 | 0 | gnutls_datum_t raw; /* raw headers */ |
1155 | | /* Read the headers. |
1156 | | */ |
1157 | 0 | record->header_size = record->packet_size = RECORD_HEADER_SIZE(session); |
1158 | |
|
1159 | 0 | ret = _gnutls_io_read_buffered(session, record->header_size, -1, ms); |
1160 | 0 | if (ret != record->header_size) { |
1161 | 0 | if (ret < 0 && gnutls_error_is_fatal(ret) == 0) |
1162 | 0 | return ret; |
1163 | | |
1164 | 0 | if (ret > 0) |
1165 | 0 | ret = GNUTLS_E_UNEXPECTED_PACKET_LENGTH; |
1166 | 0 | else if (ret == 0) |
1167 | 0 | ret = GNUTLS_E_PREMATURE_TERMINATION; |
1168 | |
|
1169 | 0 | return gnutls_assert_val(ret); |
1170 | 0 | } |
1171 | | |
1172 | 0 | ret = _mbuffer_linearize_align16(&session->internals.record_recv_buffer, |
1173 | 0 | get_total_headers2(session, |
1174 | 0 | record_params)); |
1175 | 0 | if (ret < 0) |
1176 | 0 | return gnutls_assert_val(ret); |
1177 | | |
1178 | 0 | _mbuffer_head_get_first(&session->internals.record_recv_buffer, &raw); |
1179 | 0 | if (raw.size < RECORD_HEADER_SIZE(session)) |
1180 | 0 | return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH); |
1181 | | |
1182 | 0 | record_read_headers(session, raw.data, type, htype, record); |
1183 | | |
1184 | | /* Check if the DTLS epoch is valid */ |
1185 | 0 | if (IS_DTLS(session)) { |
1186 | 0 | if (_gnutls_epoch_is_valid(session, record->epoch) == 0) { |
1187 | 0 | _gnutls_audit_log( |
1188 | 0 | session, |
1189 | 0 | "Discarded message[%lu] with invalid epoch %u.\n", |
1190 | 0 | (unsigned long)record->sequence, |
1191 | 0 | (unsigned int)(record->sequence >> 48)); |
1192 | 0 | gnutls_assert(); |
1193 | | /* doesn't matter, just a fatal error */ |
1194 | 0 | return GNUTLS_E_UNEXPECTED_PACKET_LENGTH; |
1195 | 0 | } |
1196 | 0 | } |
1197 | | |
1198 | | /* Here we check if the Type of the received packet is |
1199 | | * ok. |
1200 | | */ |
1201 | 0 | if ((ret = check_recv_type(session, record->type)) < 0) |
1202 | 0 | return gnutls_assert_val(ret); |
1203 | | |
1204 | | /* Here we check if the advertised version is the one we |
1205 | | * negotiated in the handshake. |
1206 | | */ |
1207 | 0 | if ((ret = record_check_version(session, htype, record->version)) < 0) |
1208 | 0 | return gnutls_assert_val(ret); |
1209 | | |
1210 | 0 | if (record->length == 0 || |
1211 | 0 | record->length > max_record_recv_size(session)) { |
1212 | 0 | _gnutls_audit_log( |
1213 | 0 | session, |
1214 | 0 | "Received packet with illegal length: %u (max: %u)\n", |
1215 | 0 | (unsigned int)record->length, |
1216 | 0 | (unsigned)max_record_recv_size(session)); |
1217 | |
|
1218 | 0 | if (record->length == 0) { |
1219 | | /* Empty, unencrypted records are always unexpected. */ |
1220 | 0 | if (record_params->cipher->id == GNUTLS_CIPHER_NULL) |
1221 | 0 | return gnutls_assert_val( |
1222 | 0 | GNUTLS_E_UNEXPECTED_PACKET); |
1223 | | |
1224 | 0 | return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED); |
1225 | 0 | } |
1226 | 0 | return gnutls_assert_val(GNUTLS_E_RECORD_OVERFLOW); |
1227 | 0 | } |
1228 | | |
1229 | 0 | _gnutls_record_log("REC[%p]: Expected Packet %s(%d)\n", session, |
1230 | 0 | _gnutls_packet2str(type), type); |
1231 | 0 | _gnutls_record_log("REC[%p]: Received Packet %s(%d) with length: %d\n", |
1232 | 0 | session, _gnutls_packet2str(record->type), |
1233 | 0 | record->type, record->length); |
1234 | |
|
1235 | 0 | return 0; |
1236 | 0 | } |
1237 | | |
1238 | | /* @ms: is the number of milliseconds to wait for data. Use zero for indefinite. |
1239 | | * |
1240 | | * This will receive record layer packets and add them to |
1241 | | * application_data_buffer and handshake_data_buffer. |
1242 | | * |
1243 | | * If the htype is not -1 then handshake timeouts |
1244 | | * will be enforced. |
1245 | | */ |
1246 | | ssize_t _gnutls_recv_in_buffers(gnutls_session_t session, content_type_t type, |
1247 | | gnutls_handshake_description_t htype, |
1248 | | unsigned int ms) |
1249 | 0 | { |
1250 | 0 | uint64_t packet_sequence; |
1251 | 0 | gnutls_datum_t ciphertext; |
1252 | 0 | mbuffer_st *bufel = NULL, *decrypted = NULL; |
1253 | 0 | gnutls_datum_t t; |
1254 | 0 | int ret; |
1255 | 0 | unsigned int n_retries = 0; |
1256 | 0 | record_parameters_st *record_params; |
1257 | 0 | record_state_st *record_state; |
1258 | 0 | struct tls_record_st record; |
1259 | 0 | const version_entry_st *vers = get_version(session); |
1260 | |
|
1261 | 0 | begin: |
1262 | |
|
1263 | 0 | if (n_retries > DEFAULT_MAX_EMPTY_RECORDS) { |
1264 | 0 | gnutls_assert(); |
1265 | 0 | return GNUTLS_E_TOO_MANY_EMPTY_PACKETS; |
1266 | 0 | } |
1267 | | |
1268 | 0 | if (session->internals.read_eof != 0) { |
1269 | | /* if we have already read an EOF |
1270 | | */ |
1271 | 0 | return 0; |
1272 | 0 | } else if (session_is_valid(session) != 0 || |
1273 | 0 | session->internals.may_not_read != 0) |
1274 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_SESSION); |
1275 | | |
1276 | | /* get the record state parameters */ |
1277 | 0 | ret = _gnutls_epoch_get(session, EPOCH_READ_CURRENT, &record_params); |
1278 | 0 | if (ret < 0) |
1279 | 0 | return gnutls_assert_val(ret); |
1280 | | |
1281 | | /* Safeguard against processing data with an incomplete cipher state. */ |
1282 | 0 | if (!record_params->initialized) |
1283 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
1284 | | |
1285 | 0 | record_state = &record_params->read; |
1286 | | |
1287 | | /* receive headers */ |
1288 | 0 | ret = recv_headers( |
1289 | 0 | session, record_params, type, htype, &record, |
1290 | 0 | (!(session->internals.flags & GNUTLS_NONBLOCK)) ? &ms : 0); |
1291 | 0 | if (ret < 0) { |
1292 | 0 | ret = gnutls_assert_val_fatal(ret); |
1293 | 0 | goto recv_error; |
1294 | 0 | } |
1295 | | |
1296 | 0 | if (IS_DTLS(session)) |
1297 | 0 | packet_sequence = record.sequence; |
1298 | 0 | else |
1299 | 0 | packet_sequence = record_state->sequence_number; |
1300 | | |
1301 | | /* Read the packet data and insert it to record_recv_buffer. |
1302 | | */ |
1303 | 0 | ret = _gnutls_io_read_buffered( |
1304 | 0 | session, record.packet_size, record.type, |
1305 | 0 | (!(session->internals.flags & GNUTLS_NONBLOCK)) ? &ms : 0); |
1306 | 0 | if (ret != record.packet_size) { |
1307 | 0 | gnutls_assert(); |
1308 | 0 | goto recv_error; |
1309 | 0 | } |
1310 | | |
1311 | | /* ok now we are sure that we have read all the data - so |
1312 | | * move on ! |
1313 | | */ |
1314 | 0 | ret = _mbuffer_linearize_align16(&session->internals.record_recv_buffer, |
1315 | 0 | get_total_headers2(session, |
1316 | 0 | record_params)); |
1317 | 0 | if (ret < 0) |
1318 | 0 | return gnutls_assert_val(ret); |
1319 | | |
1320 | 0 | bufel = _mbuffer_head_get_first(&session->internals.record_recv_buffer, |
1321 | 0 | NULL); |
1322 | 0 | if (bufel == NULL) |
1323 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
1324 | | |
1325 | | /* ignore CCS if TLS 1.3 is negotiated */ |
1326 | 0 | if (record.type == GNUTLS_CHANGE_CIPHER_SPEC && vers && |
1327 | 0 | vers->tls13_sem) { |
1328 | | /* if the CCS has arrived after Finished, abort the |
1329 | | * connection */ |
1330 | 0 | if (!session->internals.handshake_in_progress) { |
1331 | 0 | return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET); |
1332 | 0 | } |
1333 | 0 | _gnutls_read_log("discarding change cipher spec in TLS1.3\n"); |
1334 | | /* we use the same mechanism to retry as when |
1335 | | * receiving multiple empty TLS packets */ |
1336 | 0 | bufel = _mbuffer_head_pop_first( |
1337 | 0 | &session->internals.record_recv_buffer); |
1338 | 0 | _mbuffer_xfree(&bufel); |
1339 | 0 | n_retries++; |
1340 | 0 | goto begin; |
1341 | 0 | } |
1342 | | |
1343 | | /* We allocate the maximum possible to allow few compressed bytes to expand to a |
1344 | | * full record. Moreover we add space for any pad and the MAC (in case |
1345 | | * they are encrypted). |
1346 | | */ |
1347 | 0 | ret = max_decrypted_size(session) + MAX_PAD_SIZE + MAX_HASH_SIZE; |
1348 | 0 | decrypted = _mbuffer_alloc_align16(ret, 0); |
1349 | 0 | if (decrypted == NULL) |
1350 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
1351 | | |
1352 | 0 | _mbuffer_set_udata_size(decrypted, ret); |
1353 | 0 | ciphertext.data = |
1354 | 0 | (uint8_t *)_mbuffer_get_udata_ptr(bufel) + record.header_size; |
1355 | 0 | ciphertext.size = record.length; |
1356 | | |
1357 | | /* decrypt the data we got. |
1358 | | */ |
1359 | 0 | t.data = _mbuffer_get_udata_ptr(decrypted); |
1360 | 0 | t.size = _mbuffer_get_udata_size(decrypted); |
1361 | 0 | ret = _gnutls_decrypt(session, &ciphertext, &t, &record.type, |
1362 | 0 | record_params, packet_sequence); |
1363 | 0 | if (ret >= 0) |
1364 | 0 | _mbuffer_set_udata_size(decrypted, ret); |
1365 | |
|
1366 | 0 | _mbuffer_head_remove_bytes(&session->internals.record_recv_buffer, |
1367 | 0 | record.header_size + record.length); |
1368 | |
|
1369 | 0 | if (session->security_parameters.entity == GNUTLS_SERVER && |
1370 | 0 | session->internals.hsk_flags & HSK_EARLY_DATA_IN_FLIGHT) { |
1371 | 0 | if (session->internals.hsk_flags & HSK_EARLY_DATA_ACCEPTED) { |
1372 | 0 | if (ret < 0 || |
1373 | | /* early data must always be encrypted, treat it |
1374 | | * as decryption failure if otherwise */ |
1375 | 0 | record_params->cipher->id == GNUTLS_CIPHER_NULL) { |
1376 | 0 | _gnutls_record_log( |
1377 | 0 | "REC[%p]: failed to decrypt early data, in epoch %d\n", |
1378 | 0 | session, record_params->epoch); |
1379 | 0 | ret = GNUTLS_E_DECRYPTION_FAILED; |
1380 | 0 | goto sanity_check_error; |
1381 | 0 | } else if (record.type == GNUTLS_APPLICATION_DATA) { |
1382 | 0 | size_t decrypted_length = |
1383 | 0 | _mbuffer_get_udata_size(decrypted); |
1384 | 0 | _gnutls_record_log( |
1385 | 0 | "REC[%p]: decrypted early data with length: %d, in epoch %d\n", |
1386 | 0 | session, (int)decrypted_length, |
1387 | 0 | record_params->epoch); |
1388 | 0 | if (decrypted_length > |
1389 | 0 | session->security_parameters |
1390 | 0 | .max_early_data_size - |
1391 | 0 | session->internals |
1392 | 0 | .early_data_received) { |
1393 | 0 | _gnutls_record_log( |
1394 | 0 | "REC[%p]: max_early_data_size exceeded\n", |
1395 | 0 | session); |
1396 | 0 | ret = GNUTLS_E_UNEXPECTED_PACKET; |
1397 | 0 | goto sanity_check_error; |
1398 | 0 | } |
1399 | | |
1400 | 0 | _mbuffer_enqueue( |
1401 | 0 | &session->internals |
1402 | 0 | .early_data_recv_buffer, |
1403 | 0 | decrypted); |
1404 | 0 | session->internals.early_data_received += |
1405 | 0 | decrypted_length; |
1406 | | |
1407 | | /* Increase sequence number. We do both for TLS and DTLS, since in |
1408 | | * DTLS we also rely on that number (roughly) since it may get reported |
1409 | | * to application via gnutls_record_get_state(). |
1410 | | */ |
1411 | 0 | if (sequence_increment( |
1412 | 0 | session, |
1413 | 0 | &record_state->sequence_number) != |
1414 | 0 | 0) { |
1415 | 0 | session_invalidate(session); |
1416 | 0 | gnutls_assert(); |
1417 | 0 | ret = GNUTLS_E_RECORD_LIMIT_REACHED; |
1418 | 0 | goto sanity_check_error; |
1419 | 0 | } |
1420 | | |
1421 | | /* decrypted is now accounted */ |
1422 | 0 | return GNUTLS_E_AGAIN; |
1423 | 0 | } |
1424 | 0 | } else { |
1425 | | /* We do not accept early data: skip decryption |
1426 | | * failure up to max_early_data_size. Otherwise, |
1427 | | * if the record is properly decrypted, treat it as |
1428 | | * the start of client's second flight. |
1429 | | */ |
1430 | 0 | if (record.type == GNUTLS_APPLICATION_DATA && |
1431 | 0 | (ret < 0 || |
1432 | | /* early data must always be encrypted, treat it |
1433 | | * as decryption failure if otherwise */ |
1434 | 0 | record_params->cipher->id == GNUTLS_CIPHER_NULL)) { |
1435 | 0 | if (record.length > |
1436 | 0 | session->security_parameters |
1437 | 0 | .max_early_data_size - |
1438 | 0 | session->internals |
1439 | 0 | .early_data_received) { |
1440 | 0 | _gnutls_record_log( |
1441 | 0 | "REC[%p]: max_early_data_size exceeded\n", |
1442 | 0 | session); |
1443 | 0 | ret = GNUTLS_E_UNEXPECTED_PACKET; |
1444 | 0 | goto sanity_check_error; |
1445 | 0 | } |
1446 | | |
1447 | 0 | _gnutls_record_log( |
1448 | 0 | "REC[%p]: Discarded early data[%lu] due to invalid decryption, length: %u\n", |
1449 | 0 | session, (unsigned long)packet_sequence, |
1450 | 0 | (unsigned int)record.length); |
1451 | 0 | session->internals.early_data_received += |
1452 | 0 | record.length; |
1453 | | /* silently discard received data */ |
1454 | 0 | _mbuffer_xfree(&decrypted); |
1455 | 0 | return gnutls_assert_val(GNUTLS_E_AGAIN); |
1456 | 0 | } else { |
1457 | 0 | session->internals.hsk_flags &= |
1458 | 0 | ~HSK_EARLY_DATA_IN_FLIGHT; |
1459 | 0 | } |
1460 | 0 | } |
1461 | 0 | } |
1462 | | |
1463 | 0 | if (ret < 0) { |
1464 | 0 | gnutls_assert(); |
1465 | 0 | _gnutls_audit_log( |
1466 | 0 | session, |
1467 | 0 | "Discarded message[%lu] due to invalid decryption\n", |
1468 | 0 | (unsigned long)packet_sequence); |
1469 | 0 | goto sanity_check_error; |
1470 | 0 | } |
1471 | | |
1472 | 0 | if (IS_DTLS(session)) { |
1473 | | /* check for duplicates. We check after the message |
1474 | | * is processed and authenticated to avoid someone |
1475 | | * messing with our windows. */ |
1476 | 0 | if (likely(!(session->internals.flags & |
1477 | 0 | GNUTLS_NO_REPLAY_PROTECTION))) { |
1478 | 0 | ret = _dtls_record_check(record_params, |
1479 | 0 | packet_sequence); |
1480 | 0 | if (ret < 0) { |
1481 | 0 | _gnutls_record_log( |
1482 | 0 | "REC[%p]: Discarded duplicate message[%u.%lu]: %s\n", |
1483 | 0 | session, |
1484 | 0 | (unsigned int)(record.sequence >> 48), |
1485 | 0 | (unsigned long)(packet_sequence), |
1486 | 0 | _gnutls_packet2str(record.type)); |
1487 | 0 | goto sanity_check_error; |
1488 | 0 | } |
1489 | 0 | } |
1490 | | |
1491 | 0 | _gnutls_record_log( |
1492 | 0 | "REC[%p]: Decrypted Packet[%u.%lu] %s(%d) with length: %d\n", |
1493 | 0 | session, (unsigned int)(record.sequence >> 48), |
1494 | 0 | (unsigned long)packet_sequence, |
1495 | 0 | _gnutls_packet2str(record.type), record.type, |
1496 | 0 | (int)_mbuffer_get_udata_size(decrypted)); |
1497 | | |
1498 | | /* store the last valid sequence number. We don't use that internally but |
1499 | | * callers of gnutls_record_get_state() could take advantage of it. */ |
1500 | 0 | record_state->sequence_number = record.sequence; |
1501 | 0 | } else { |
1502 | 0 | _gnutls_record_log( |
1503 | 0 | "REC[%p]: Decrypted Packet[%lu] %s(%d) with length: %d\n", |
1504 | 0 | session, (unsigned long)packet_sequence, |
1505 | 0 | _gnutls_packet2str(record.type), record.type, |
1506 | 0 | (int)_mbuffer_get_udata_size(decrypted)); |
1507 | 0 | } |
1508 | | |
1509 | | /* Increase sequence number. We do both for TLS and DTLS, since in |
1510 | | * DTLS we also rely on that number (roughly) since it may get reported |
1511 | | * to application via gnutls_record_get_state(). |
1512 | | */ |
1513 | 0 | if (sequence_increment(session, &record_state->sequence_number) != 0) { |
1514 | 0 | session_invalidate(session); |
1515 | 0 | gnutls_assert(); |
1516 | 0 | ret = GNUTLS_E_RECORD_LIMIT_REACHED; |
1517 | 0 | goto sanity_check_error; |
1518 | 0 | } |
1519 | | |
1520 | | /* (originally for) TLS 1.0 CBC protection. |
1521 | | * Actually this code is called if we just received |
1522 | | * an empty packet. An empty TLS packet is usually |
1523 | | * sent to protect some vulnerabilities in the CBC mode. |
1524 | | * In that case we go to the beginning and start reading |
1525 | | * the next packet. |
1526 | | */ |
1527 | 0 | if (_mbuffer_get_udata_size(decrypted) == 0 && |
1528 | | /* Under TLS 1.3, there are only AEAD ciphers and this |
1529 | | * logic is meaningless. Moreover, the implementation need |
1530 | | * to send correct alert upon receiving empty messages in |
1531 | | * certain occasions. Skip this and leave |
1532 | | * record_add_to_buffers() to handle the empty |
1533 | | * messages. */ |
1534 | 0 | !(vers && vers->tls13_sem)) { |
1535 | 0 | _mbuffer_xfree(&decrypted); |
1536 | 0 | n_retries++; |
1537 | 0 | goto begin; |
1538 | 0 | } |
1539 | | |
1540 | 0 | if (_mbuffer_get_udata_size(decrypted) > max_decrypted_size(session)) { |
1541 | 0 | _gnutls_audit_log(session, |
1542 | 0 | "Received packet with illegal length: %u\n", |
1543 | 0 | (unsigned int)ret); |
1544 | |
|
1545 | 0 | ret = gnutls_assert_val(GNUTLS_E_RECORD_OVERFLOW); |
1546 | 0 | goto sanity_check_error; |
1547 | 0 | } |
1548 | | |
1549 | 0 | #ifdef ENABLE_SSL2 |
1550 | 0 | if (record.v2) { |
1551 | 0 | decrypted->htype = GNUTLS_HANDSHAKE_CLIENT_HELLO_V2; |
1552 | 0 | } else |
1553 | 0 | #endif |
1554 | 0 | { |
1555 | 0 | uint8_t *p = _mbuffer_get_udata_ptr(decrypted); |
1556 | 0 | decrypted->htype = p[0]; |
1557 | 0 | } |
1558 | |
|
1559 | 0 | ret = record_add_to_buffers(session, &record, type, htype, |
1560 | 0 | packet_sequence, decrypted); |
1561 | | |
1562 | | /* decrypted is now either deinitialized or buffered somewhere else */ |
1563 | |
|
1564 | 0 | if (ret < 0) |
1565 | 0 | return gnutls_assert_val(ret); |
1566 | | |
1567 | 0 | return ret; |
1568 | | |
1569 | 0 | discard: |
1570 | 0 | session->internals.dtls.packets_dropped++; |
1571 | | |
1572 | | /* discard the whole received fragment. */ |
1573 | 0 | bufel = _mbuffer_head_pop_first(&session->internals.record_recv_buffer); |
1574 | 0 | _mbuffer_xfree(&bufel); |
1575 | 0 | return gnutls_assert_val(GNUTLS_E_AGAIN); |
1576 | | |
1577 | 0 | sanity_check_error: |
1578 | 0 | if (IS_DTLS(session)) { |
1579 | 0 | session->internals.dtls.packets_dropped++; |
1580 | 0 | ret = gnutls_assert_val(GNUTLS_E_AGAIN); |
1581 | 0 | goto cleanup; |
1582 | 0 | } |
1583 | | |
1584 | 0 | session_unresumable(session); |
1585 | 0 | session_invalidate(session); |
1586 | |
|
1587 | 0 | cleanup: |
1588 | 0 | _mbuffer_xfree(&decrypted); |
1589 | 0 | return ret; |
1590 | | |
1591 | 0 | recv_error: |
1592 | 0 | if (ret < 0 && |
1593 | 0 | (gnutls_error_is_fatal(ret) == 0 || ret == GNUTLS_E_TIMEDOUT)) |
1594 | 0 | return ret; |
1595 | | |
1596 | 0 | if (type == GNUTLS_ALERT) { /* we were expecting close notify */ |
1597 | 0 | session_invalidate(session); |
1598 | 0 | gnutls_assert(); |
1599 | 0 | return 0; |
1600 | 0 | } |
1601 | | |
1602 | 0 | if (IS_DTLS(session) && (ret == GNUTLS_E_DECRYPTION_FAILED || |
1603 | 0 | ret == GNUTLS_E_UNSUPPORTED_VERSION_PACKET || |
1604 | 0 | ret == GNUTLS_E_UNEXPECTED_PACKET_LENGTH || |
1605 | 0 | ret == GNUTLS_E_RECORD_OVERFLOW || |
1606 | 0 | ret == GNUTLS_E_UNEXPECTED_PACKET || |
1607 | 0 | ret == GNUTLS_E_ERROR_IN_FINISHED_PACKET || |
1608 | 0 | ret == GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET)) { |
1609 | 0 | goto discard; |
1610 | 0 | } |
1611 | | |
1612 | 0 | session_invalidate(session); |
1613 | 0 | session_unresumable(session); |
1614 | |
|
1615 | 0 | if (ret == 0) |
1616 | 0 | return GNUTLS_E_UNEXPECTED_PACKET_LENGTH; |
1617 | 0 | else |
1618 | 0 | return ret; |
1619 | 0 | } |
1620 | | |
1621 | | /* Returns a value greater than zero (>= 0) if buffers should be checked |
1622 | | * for data. */ |
1623 | | static ssize_t check_session_status(gnutls_session_t session, unsigned ms) |
1624 | 0 | { |
1625 | 0 | int ret; |
1626 | |
|
1627 | 0 | if (session->internals.read_eof != 0) { |
1628 | | /* if we have already read an EOF |
1629 | | */ |
1630 | 0 | return 0; |
1631 | 0 | } else if (session_is_valid(session) != 0 || |
1632 | 0 | session->internals.may_not_read != 0) { |
1633 | 0 | gnutls_assert(); |
1634 | 0 | return GNUTLS_E_INVALID_SESSION; |
1635 | 0 | } |
1636 | | |
1637 | 0 | switch (session->internals.recv_state) { |
1638 | 0 | case RECV_STATE_REAUTH: |
1639 | 0 | session->internals.recv_state = RECV_STATE_0; |
1640 | |
|
1641 | 0 | ret = gnutls_reauth(session, 0); |
1642 | 0 | if (ret < 0) { |
1643 | | /* a temp or fatal error, make sure we reset the state |
1644 | | * so we can resume on temp errors */ |
1645 | 0 | session->internals.recv_state = RECV_STATE_REAUTH; |
1646 | 0 | return gnutls_assert_val(ret); |
1647 | 0 | } |
1648 | | |
1649 | 0 | return 1; |
1650 | 0 | case RECV_STATE_REHANDSHAKE: |
1651 | 0 | session->internals.recv_state = RECV_STATE_0; |
1652 | |
|
1653 | 0 | ret = gnutls_handshake(session); |
1654 | 0 | if (ret < 0) { |
1655 | | /* a temp or fatal error, make sure we reset the state |
1656 | | * so we can resume on temp errors */ |
1657 | 0 | session->internals.recv_state = RECV_STATE_REHANDSHAKE; |
1658 | 0 | return gnutls_assert_val(ret); |
1659 | 0 | } |
1660 | | |
1661 | 0 | return 1; |
1662 | 0 | case RECV_STATE_ASYNC_HANDSHAKE: |
1663 | 0 | ret = _gnutls_recv_in_buffers(session, GNUTLS_HANDSHAKE, -1, |
1664 | 0 | ms); |
1665 | 0 | if (ret < 0 && ret != GNUTLS_E_SESSION_EOF) |
1666 | 0 | return gnutls_assert_val(ret); |
1667 | | |
1668 | 0 | ret = _gnutls13_recv_async_handshake(session); |
1669 | 0 | if (ret < 0) |
1670 | 0 | return gnutls_assert_val(ret); |
1671 | | |
1672 | 0 | return GNUTLS_E_AGAIN; |
1673 | 0 | case RECV_STATE_EARLY_START_HANDLING: |
1674 | 0 | case RECV_STATE_FALSE_START_HANDLING: |
1675 | 0 | return 1; |
1676 | 0 | case RECV_STATE_FALSE_START: |
1677 | | /* if false start is not complete we always expect for handshake packets |
1678 | | * prior to anything else. */ |
1679 | 0 | if (session->security_parameters.entity != GNUTLS_CLIENT || |
1680 | 0 | !(session->internals.flags & GNUTLS_ENABLE_FALSE_START)) |
1681 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
1682 | | |
1683 | | /* Attempt to complete handshake - we only need to receive */ |
1684 | 0 | session->internals.recv_state = RECV_STATE_FALSE_START_HANDLING; |
1685 | 0 | ret = gnutls_handshake(session); |
1686 | 0 | if (ret < 0) { |
1687 | | /* a temp or fatal error, make sure we reset the state |
1688 | | * so we can resume on temp errors */ |
1689 | 0 | session->internals.recv_state = RECV_STATE_FALSE_START; |
1690 | 0 | return gnutls_assert_val(ret); |
1691 | 0 | } |
1692 | | |
1693 | 0 | session->internals.recv_state = RECV_STATE_0; |
1694 | 0 | return 1; |
1695 | 0 | case RECV_STATE_EARLY_START: |
1696 | | /* if early start is not complete we always expect for handshake packets |
1697 | | * prior to anything else. */ |
1698 | 0 | if (session->security_parameters.entity != GNUTLS_SERVER || |
1699 | 0 | !(session->internals.flags & GNUTLS_ENABLE_EARLY_START)) |
1700 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
1701 | | |
1702 | | /* Attempt to complete handshake - we only need to receive */ |
1703 | 0 | session->internals.recv_state = RECV_STATE_EARLY_START_HANDLING; |
1704 | 0 | ret = gnutls_handshake(session); |
1705 | 0 | if (ret < 0) { |
1706 | | /* a temp or fatal error, make sure we reset the state |
1707 | | * so we can resume on temp errors */ |
1708 | 0 | session->internals.recv_state = RECV_STATE_EARLY_START; |
1709 | 0 | return gnutls_assert_val(ret); |
1710 | 0 | } |
1711 | | |
1712 | 0 | session->internals.recv_state = RECV_STATE_0; |
1713 | 0 | return 1; |
1714 | 0 | case RECV_STATE_DTLS_RETRANSMIT: |
1715 | 0 | ret = _dtls_retransmit(session); |
1716 | 0 | if (ret < 0) |
1717 | 0 | return gnutls_assert_val(ret); |
1718 | | |
1719 | 0 | session->internals.recv_state = RECV_STATE_0; |
1720 | |
|
1721 | 0 | FALLTHROUGH; |
1722 | 0 | case RECV_STATE_0: |
1723 | |
|
1724 | 0 | _dtls_async_timer_check(session); |
1725 | 0 | return 1; |
1726 | 0 | default: |
1727 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
1728 | 0 | } |
1729 | 0 | } |
1730 | | |
1731 | | /* This function behaves exactly like read(). The only difference is |
1732 | | * that it accepts the gnutls_session_t and the content_type_t of data to |
1733 | | * receive (if called by the user the Content is Userdata only) |
1734 | | * It is intended to receive data, under the current session. |
1735 | | */ |
1736 | | ssize_t _gnutls_recv_int(gnutls_session_t session, content_type_t type, |
1737 | | uint8_t *data, size_t data_size, void *seq, |
1738 | | unsigned int ms) |
1739 | 0 | { |
1740 | 0 | int ret; |
1741 | |
|
1742 | 0 | if ((type != GNUTLS_ALERT && type != GNUTLS_HEARTBEAT) && |
1743 | 0 | (data_size == 0 || data == NULL)) |
1744 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1745 | | |
1746 | 0 | ret = check_session_status(session, ms); |
1747 | 0 | if (ret <= 0) |
1748 | 0 | return ret; |
1749 | | |
1750 | | /* If we have enough data in the cache do not bother receiving |
1751 | | * a new packet. (in order to flush the cache) |
1752 | | */ |
1753 | 0 | ret = get_data_from_buffers(session, type, data, data_size, seq); |
1754 | 0 | if (ret != 0) |
1755 | 0 | return ret; |
1756 | | |
1757 | 0 | ret = _gnutls_recv_in_buffers(session, type, -1, ms); |
1758 | 0 | if (ret < 0 && ret != GNUTLS_E_SESSION_EOF) |
1759 | 0 | return gnutls_assert_val(ret); |
1760 | | |
1761 | 0 | return get_data_from_buffers(session, type, data, data_size, seq); |
1762 | 0 | } |
1763 | | |
1764 | | /** |
1765 | | * gnutls_packet_get: |
1766 | | * @packet: is a #gnutls_packet_t type. |
1767 | | * @data: will contain the data present in the @packet structure (may be %NULL) |
1768 | | * @sequence: the 8-bytes of the packet sequence number (may be %NULL) |
1769 | | * |
1770 | | * This function returns the data and sequence number associated with |
1771 | | * the received packet. |
1772 | | * |
1773 | | * Since: 3.3.5 |
1774 | | **/ |
1775 | | |
1776 | | void gnutls_packet_get(gnutls_packet_t packet, gnutls_datum_t *data, |
1777 | | unsigned char *sequence) |
1778 | 0 | { |
1779 | 0 | if (unlikely(packet == NULL)) { |
1780 | 0 | gnutls_assert(); |
1781 | 0 | if (data) { |
1782 | 0 | data->data = NULL; |
1783 | 0 | data->size = 0; |
1784 | 0 | return; |
1785 | 0 | } |
1786 | 0 | } |
1787 | | |
1788 | 0 | assert(packet != NULL); |
1789 | | |
1790 | 0 | if (sequence) { |
1791 | 0 | _gnutls_write_uint64(packet->record_sequence, sequence); |
1792 | 0 | } |
1793 | |
|
1794 | 0 | if (data) { |
1795 | 0 | data->size = packet->msg.size - packet->mark; |
1796 | 0 | data->data = packet->msg.data + packet->mark; |
1797 | 0 | } |
1798 | 0 | } |
1799 | | |
1800 | | /** |
1801 | | * gnutls_packet_deinit: |
1802 | | * @packet: is a pointer to a #gnutls_packet_st structure. |
1803 | | * |
1804 | | * This function will deinitialize all data associated with |
1805 | | * the received packet. |
1806 | | * |
1807 | | * Since: 3.3.5 |
1808 | | **/ |
1809 | | void gnutls_packet_deinit(gnutls_packet_t packet) |
1810 | 0 | { |
1811 | 0 | gnutls_free(packet); |
1812 | 0 | } |
1813 | | |
1814 | | /** |
1815 | | * gnutls_record_discard_queued: |
1816 | | * @session: is a #gnutls_session_t type. |
1817 | | * |
1818 | | * This function discards all queued to be sent packets in a DTLS session. |
1819 | | * These are the packets queued after an interrupted gnutls_record_send(). |
1820 | | * |
1821 | | * This function can only be used with transports where send() is |
1822 | | * an all-or-nothing operation (e.g., UDP). When partial writes are allowed |
1823 | | * this function will cause session errors. |
1824 | | * |
1825 | | * Returns: The number of bytes discarded. |
1826 | | * |
1827 | | * Since: 3.4.0 |
1828 | | **/ |
1829 | | size_t gnutls_record_discard_queued(gnutls_session_t session) |
1830 | 0 | { |
1831 | 0 | size_t ret = session->internals.record_send_buffer.byte_length; |
1832 | 0 | _mbuffer_head_clear(&session->internals.record_send_buffer); |
1833 | 0 | return ret; |
1834 | 0 | } |
1835 | | |
1836 | | /** |
1837 | | * gnutls_record_recv_packet: |
1838 | | * @session: is a #gnutls_session_t type. |
1839 | | * @packet: the structure that will hold the packet data |
1840 | | * |
1841 | | * This is a lower-level function than gnutls_record_recv() and allows |
1842 | | * to directly receive the whole decrypted packet. That avoids a |
1843 | | * memory copy, and is intended to be used by applications seeking high |
1844 | | * performance. |
1845 | | * |
1846 | | * The received packet is accessed using gnutls_packet_get() and |
1847 | | * must be deinitialized using gnutls_packet_deinit(). The returned |
1848 | | * packet will be %NULL if the return value is zero (EOF). |
1849 | | * |
1850 | | * Returns: The number of bytes received and zero on EOF (for stream |
1851 | | * connections). A negative error code is returned in case of an error. |
1852 | | * |
1853 | | * Since: 3.3.5 |
1854 | | **/ |
1855 | | ssize_t gnutls_record_recv_packet(gnutls_session_t session, |
1856 | | gnutls_packet_t *packet) |
1857 | 0 | { |
1858 | 0 | int ret; |
1859 | |
|
1860 | 0 | if (packet == NULL) |
1861 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1862 | | |
1863 | 0 | ret = check_session_status(session, |
1864 | 0 | session->internals.record_timeout_ms); |
1865 | 0 | if (ret <= 0) |
1866 | 0 | return ret; |
1867 | | |
1868 | 0 | ret = get_packet_from_buffers(session, GNUTLS_APPLICATION_DATA, packet); |
1869 | 0 | if (ret != 0) |
1870 | 0 | return ret; |
1871 | | |
1872 | 0 | ret = _gnutls_recv_in_buffers(session, GNUTLS_APPLICATION_DATA, -1, |
1873 | 0 | session->internals.record_timeout_ms); |
1874 | 0 | if (ret < 0 && ret != GNUTLS_E_SESSION_EOF) |
1875 | 0 | return gnutls_assert_val(ret); |
1876 | | |
1877 | 0 | return get_packet_from_buffers(session, GNUTLS_APPLICATION_DATA, |
1878 | 0 | packet); |
1879 | 0 | } |
1880 | | |
1881 | | static ssize_t append_data_to_corked(gnutls_session_t session, const void *data, |
1882 | | size_t data_size) |
1883 | 0 | { |
1884 | 0 | int ret; |
1885 | |
|
1886 | 0 | if (IS_DTLS(session)) { |
1887 | 0 | if (data_size + |
1888 | 0 | session->internals.record_presend_buffer.length > |
1889 | 0 | gnutls_dtls_get_data_mtu(session)) { |
1890 | 0 | return gnutls_assert_val(GNUTLS_E_LARGE_PACKET); |
1891 | 0 | } |
1892 | 0 | } |
1893 | | |
1894 | 0 | ret = _gnutls_buffer_append_data( |
1895 | 0 | &session->internals.record_presend_buffer, data, data_size); |
1896 | 0 | if (ret < 0) |
1897 | 0 | return gnutls_assert_val(ret); |
1898 | | |
1899 | 0 | return data_size; |
1900 | 0 | } |
1901 | | |
1902 | | /** |
1903 | | * gnutls_record_send: |
1904 | | * @session: is a #gnutls_session_t type. |
1905 | | * @data: contains the data to send |
1906 | | * @data_size: is the length of the data |
1907 | | * |
1908 | | * This function has the similar semantics with send(). The only |
1909 | | * difference is that it accepts a GnuTLS session, and uses different |
1910 | | * error codes. |
1911 | | * Note that if the send buffer is full, send() will block this |
1912 | | * function. See the send() documentation for more information. |
1913 | | * |
1914 | | * You can replace the default push function which is send(), by using |
1915 | | * gnutls_transport_set_push_function(). |
1916 | | * |
1917 | | * If the EINTR is returned by the internal push function |
1918 | | * then %GNUTLS_E_INTERRUPTED will be returned. If |
1919 | | * %GNUTLS_E_INTERRUPTED or %GNUTLS_E_AGAIN is returned, you must |
1920 | | * call this function again with the exact same parameters, or provide a |
1921 | | * %NULL pointer for @data and 0 for @data_size, in order to write the |
1922 | | * same data as before. If you wish to discard the previous data instead |
1923 | | * of retrying, you must call gnutls_record_discard_queued() before |
1924 | | * calling this function with different parameters. Note that the latter |
1925 | | * works only on special transports (e.g., UDP). |
1926 | | * cf. gnutls_record_get_direction(). |
1927 | | * |
1928 | | * Note that in DTLS this function will return the %GNUTLS_E_LARGE_PACKET |
1929 | | * error code if the send data exceed the data MTU value - as returned |
1930 | | * by gnutls_dtls_get_data_mtu(). The errno value EMSGSIZE |
1931 | | * also maps to %GNUTLS_E_LARGE_PACKET. |
1932 | | * Note that since 3.2.13 this function can be called under cork in DTLS |
1933 | | * mode, and will refuse to send data over the MTU size by returning |
1934 | | * %GNUTLS_E_LARGE_PACKET. |
1935 | | * |
1936 | | * Returns: The number of bytes sent, or a negative error code. The |
1937 | | * number of bytes sent might be less than @data_size. The maximum |
1938 | | * number of bytes this function can send in a single call depends |
1939 | | * on the negotiated maximum record size. |
1940 | | **/ |
1941 | | ssize_t gnutls_record_send(gnutls_session_t session, const void *data, |
1942 | | size_t data_size) |
1943 | 0 | { |
1944 | 0 | return gnutls_record_send2(session, data, data_size, 0, 0); |
1945 | 0 | } |
1946 | | |
1947 | | /** |
1948 | | * gnutls_record_send2: |
1949 | | * @session: is a #gnutls_session_t type. |
1950 | | * @data: contains the data to send |
1951 | | * @data_size: is the length of the data |
1952 | | * @pad: padding to be added to the record |
1953 | | * @flags: must be zero |
1954 | | * |
1955 | | * This function is identical to gnutls_record_send() except that it |
1956 | | * takes an extra argument to specify padding to be added the record. |
1957 | | * To determine the maximum size of padding, use |
1958 | | * gnutls_record_get_max_size() and gnutls_record_overhead_size(). |
1959 | | * |
1960 | | * Note that in order for GnuTLS to provide constant time processing |
1961 | | * of padding and data in TLS1.3, the flag %GNUTLS_SAFE_PADDING_CHECK |
1962 | | * must be used in gnutls_init(). |
1963 | | * |
1964 | | * Returns: The number of bytes sent, or a negative error code. The |
1965 | | * number of bytes sent might be less than @data_size. The maximum |
1966 | | * number of bytes this function can send in a single call depends |
1967 | | * on the negotiated maximum record size. |
1968 | | * |
1969 | | * Since: 3.6.3 |
1970 | | **/ |
1971 | | ssize_t gnutls_record_send2(gnutls_session_t session, const void *data, |
1972 | | size_t data_size, size_t pad, unsigned flags) |
1973 | 0 | { |
1974 | 0 | const version_entry_st *vers = get_version(session); |
1975 | 0 | size_t max_pad = 0; |
1976 | 0 | int ret; |
1977 | |
|
1978 | 0 | if (unlikely(!session->internals.initial_negotiation_completed)) { |
1979 | | /* this is to protect buggy applications from sending unencrypted |
1980 | | * data. We allow sending however, if we are in false or early start |
1981 | | * handshake state. */ |
1982 | 0 | gnutls_mutex_lock(&session->internals.post_negotiation_lock); |
1983 | | |
1984 | | /* we intentionally re-check the initial_negotation_completed variable |
1985 | | * to avoid locking during normal operation of gnutls_record_send2() */ |
1986 | 0 | if (!session->internals.initial_negotiation_completed && |
1987 | 0 | session->internals.recv_state != RECV_STATE_FALSE_START && |
1988 | 0 | session->internals.recv_state != |
1989 | 0 | RECV_STATE_FALSE_START_HANDLING && |
1990 | 0 | session->internals.recv_state != RECV_STATE_EARLY_START && |
1991 | 0 | session->internals.recv_state != |
1992 | 0 | RECV_STATE_EARLY_START_HANDLING && |
1993 | 0 | !(session->internals.hsk_flags & |
1994 | 0 | HSK_EARLY_DATA_IN_FLIGHT)) { |
1995 | 0 | gnutls_mutex_unlock( |
1996 | 0 | &session->internals.post_negotiation_lock); |
1997 | 0 | return gnutls_assert_val( |
1998 | 0 | GNUTLS_E_UNAVAILABLE_DURING_HANDSHAKE); |
1999 | 0 | } |
2000 | 0 | gnutls_mutex_unlock(&session->internals.post_negotiation_lock); |
2001 | 0 | } |
2002 | | |
2003 | 0 | if (unlikely(!vers)) |
2004 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
2005 | | |
2006 | 0 | if (vers->tls13_sem) |
2007 | 0 | max_pad = gnutls_record_get_max_size(session) - |
2008 | 0 | gnutls_record_overhead_size(session); |
2009 | |
|
2010 | 0 | if (pad > max_pad) |
2011 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
2012 | | |
2013 | 0 | switch (session->internals.rsend_state) { |
2014 | 0 | case RECORD_SEND_NORMAL: |
2015 | 0 | if (IS_KTLS_ENABLED(session, GNUTLS_KTLS_SEND)) { |
2016 | 0 | return _gnutls_ktls_send(session, data, data_size); |
2017 | 0 | } else { |
2018 | 0 | return _gnutls_send_tlen_int(session, |
2019 | 0 | GNUTLS_APPLICATION_DATA, |
2020 | 0 | -1, EPOCH_WRITE_CURRENT, |
2021 | 0 | data, data_size, pad, |
2022 | 0 | MBUFFER_FLUSH); |
2023 | 0 | } |
2024 | 0 | case RECORD_SEND_CORKED: |
2025 | 0 | case RECORD_SEND_CORKED_TO_KU: |
2026 | 0 | return append_data_to_corked(session, data, data_size); |
2027 | 0 | case RECORD_SEND_KEY_UPDATE_1: |
2028 | 0 | _gnutls_buffer_reset( |
2029 | 0 | &session->internals.record_key_update_buffer); |
2030 | |
|
2031 | 0 | ret = _gnutls_buffer_append_data( |
2032 | 0 | &session->internals.record_key_update_buffer, data, |
2033 | 0 | data_size); |
2034 | 0 | if (ret < 0) |
2035 | 0 | return gnutls_assert_val(ret); |
2036 | | |
2037 | 0 | session->internals.rsend_state = RECORD_SEND_KEY_UPDATE_2; |
2038 | 0 | FALLTHROUGH; |
2039 | 0 | case RECORD_SEND_KEY_UPDATE_2: |
2040 | 0 | ret = gnutls_session_key_update(session, 0); |
2041 | 0 | if (ret < 0) |
2042 | 0 | return gnutls_assert_val(ret); |
2043 | | |
2044 | 0 | session->internals.rsend_state = RECORD_SEND_KEY_UPDATE_3; |
2045 | 0 | FALLTHROUGH; |
2046 | 0 | case RECORD_SEND_KEY_UPDATE_3: |
2047 | 0 | if (IS_KTLS_ENABLED(session, GNUTLS_KTLS_SEND)) { |
2048 | 0 | return _gnutls_ktls_send( |
2049 | 0 | session, |
2050 | 0 | session->internals.record_key_update_buffer.data, |
2051 | 0 | session->internals.record_key_update_buffer |
2052 | 0 | .length); |
2053 | 0 | } else { |
2054 | 0 | ret = _gnutls_send_int( |
2055 | 0 | session, GNUTLS_APPLICATION_DATA, -1, |
2056 | 0 | EPOCH_WRITE_CURRENT, |
2057 | 0 | session->internals.record_key_update_buffer.data, |
2058 | 0 | session->internals.record_key_update_buffer |
2059 | 0 | .length, |
2060 | 0 | MBUFFER_FLUSH); |
2061 | 0 | } |
2062 | 0 | _gnutls_buffer_clear( |
2063 | 0 | &session->internals.record_key_update_buffer); |
2064 | 0 | session->internals.rsend_state = RECORD_SEND_NORMAL; |
2065 | 0 | if (ret < 0) |
2066 | 0 | gnutls_assert(); |
2067 | |
|
2068 | 0 | return ret; |
2069 | 0 | default: |
2070 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
2071 | 0 | } |
2072 | 0 | } |
2073 | | |
2074 | | /** |
2075 | | * gnutls_record_send_early_data: |
2076 | | * @session: is a #gnutls_session_t type. |
2077 | | * @data: contains the data to send |
2078 | | * @data_size: is the length of the data |
2079 | | * |
2080 | | * This function can be used by a client to send data early in the |
2081 | | * handshake processes when resuming a session. This is used to |
2082 | | * implement a zero-roundtrip (0-RTT) mode. It has the same semantics |
2083 | | * as gnutls_record_send(). |
2084 | | * |
2085 | | * There may be a limit to the amount of data sent as early data. Use |
2086 | | * gnutls_record_get_max_early_data_size() to check the limit. If the |
2087 | | * limit exceeds, this function returns |
2088 | | * %GNUTLS_E_RECORD_LIMIT_REACHED. |
2089 | | * |
2090 | | * Returns: The number of bytes sent, or a negative error code. The |
2091 | | * number of bytes sent might be less than @data_size. The maximum |
2092 | | * number of bytes this function can send in a single call depends |
2093 | | * on the negotiated maximum record size. |
2094 | | * |
2095 | | * Since: 3.6.5 |
2096 | | **/ |
2097 | | ssize_t gnutls_record_send_early_data(gnutls_session_t session, |
2098 | | const void *data, size_t data_size) |
2099 | 0 | { |
2100 | 0 | int ret; |
2101 | |
|
2102 | 0 | if (session->security_parameters.entity != GNUTLS_CLIENT) |
2103 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
2104 | | |
2105 | 0 | if (data_size == 0) { |
2106 | 0 | return 0; |
2107 | 0 | } |
2108 | | |
2109 | 0 | if (xsum(session->internals.early_data_presend_buffer.length, |
2110 | 0 | data_size) > session->security_parameters.max_early_data_size) |
2111 | 0 | return gnutls_assert_val(GNUTLS_E_RECORD_LIMIT_REACHED); |
2112 | | |
2113 | 0 | ret = _gnutls_buffer_append_data( |
2114 | 0 | &session->internals.early_data_presend_buffer, data, data_size); |
2115 | 0 | if (ret < 0) |
2116 | 0 | return gnutls_assert_val(ret); |
2117 | | |
2118 | 0 | session->internals.flags |= GNUTLS_ENABLE_EARLY_DATA; |
2119 | |
|
2120 | 0 | return ret; |
2121 | 0 | } |
2122 | | |
2123 | | /** |
2124 | | * gnutls_record_send_file: |
2125 | | * @session: is a #gnutls_session_t type. |
2126 | | * @fd: file descriptor from which to read data. |
2127 | | * @offset: Is relative to file offset, denotes the starting location for |
2128 | | * reading. after function returns, it point to position following |
2129 | | * last read byte. |
2130 | | * @count: is the length of the data in bytes to be read from file and send. |
2131 | | * |
2132 | | * This function sends data from @fd. If KTLS (kernel TLS) is enabled, it will |
2133 | | * use the sendfile() system call to avoid overhead of copying data between user |
2134 | | * space and the kernel. Otherwise, this functionality is merely emulated by |
2135 | | * calling read() and gnutls_record_send(). If this implementation is |
2136 | | * suboptimal, check whether KTLS is enabled using |
2137 | | * gnutls_transport_is_ktls_enabled(). |
2138 | | * |
2139 | | * If @offset is NULL then file offset is incremented by number of bytes send, |
2140 | | * otherwise file offset remains unchanged. |
2141 | | * |
2142 | | * Returns: The number of bytes sent, or a negative error code. |
2143 | | **/ |
2144 | | ssize_t gnutls_record_send_file(gnutls_session_t session, int fd, off_t *offset, |
2145 | | size_t count) |
2146 | 0 | { |
2147 | 0 | ssize_t ret; |
2148 | 0 | size_t buf_len; |
2149 | 0 | size_t sent = 0; |
2150 | 0 | uint8_t *buf; |
2151 | 0 | off_t saved_offset = 0; |
2152 | |
|
2153 | 0 | if (IS_KTLS_ENABLED(session, GNUTLS_KTLS_SEND)) { |
2154 | 0 | return _gnutls_ktls_send_file(session, fd, offset, count); |
2155 | 0 | } |
2156 | | |
2157 | 0 | if (offset != NULL) { |
2158 | 0 | saved_offset = lseek(fd, 0, SEEK_CUR); |
2159 | 0 | if (saved_offset == (off_t)-1) { |
2160 | 0 | return GNUTLS_E_FILE_ERROR; |
2161 | 0 | } |
2162 | 0 | if (lseek(fd, *offset, SEEK_CUR) == -1) { |
2163 | 0 | return GNUTLS_E_FILE_ERROR; |
2164 | 0 | } |
2165 | 0 | } |
2166 | | |
2167 | 0 | buf_len = MIN(count, MAX(max_record_send_size(session), 512)); |
2168 | |
|
2169 | 0 | buf = gnutls_malloc(buf_len); |
2170 | 0 | if (buf == NULL) { |
2171 | 0 | gnutls_assert(); |
2172 | 0 | ret = GNUTLS_E_MEMORY_ERROR; |
2173 | 0 | goto end; |
2174 | 0 | } |
2175 | | |
2176 | 0 | while (sent < count) { |
2177 | 0 | ret = read(fd, buf, MIN(buf_len, count - sent)); |
2178 | 0 | if (ret == 0) { |
2179 | 0 | break; |
2180 | 0 | } else if (ret == -1) { |
2181 | 0 | if (errno == EAGAIN) { |
2182 | 0 | ret = GNUTLS_E_AGAIN; |
2183 | 0 | goto end; |
2184 | 0 | } |
2185 | 0 | ret = GNUTLS_E_FILE_ERROR; |
2186 | 0 | goto end; |
2187 | 0 | } |
2188 | | |
2189 | 0 | ret = gnutls_record_send(session, buf, ret); |
2190 | 0 | if (ret < 0) { |
2191 | 0 | goto end; |
2192 | 0 | } |
2193 | 0 | if (INT_ADD_OVERFLOW(sent, ret)) { |
2194 | 0 | gnutls_assert(); |
2195 | 0 | ret = GNUTLS_E_RECORD_OVERFLOW; |
2196 | 0 | goto end; |
2197 | 0 | } |
2198 | 0 | sent += ret; |
2199 | 0 | } |
2200 | | |
2201 | 0 | ret = sent; |
2202 | |
|
2203 | 0 | end: |
2204 | 0 | if (offset != NULL) { |
2205 | 0 | if (likely(!INT_ADD_OVERFLOW(*offset, sent))) { |
2206 | 0 | *offset += sent; |
2207 | 0 | } else { |
2208 | 0 | gnutls_assert(); |
2209 | 0 | ret = GNUTLS_E_RECORD_OVERFLOW; |
2210 | 0 | } |
2211 | 0 | lseek(fd, saved_offset, SEEK_SET); |
2212 | 0 | } |
2213 | 0 | gnutls_free(buf); |
2214 | 0 | return ret; |
2215 | 0 | } |
2216 | | |
2217 | | /** |
2218 | | * gnutls_record_recv_early_data: |
2219 | | * @session: is a #gnutls_session_t type. |
2220 | | * @data: the buffer that the data will be read into |
2221 | | * @data_size: the number of requested bytes |
2222 | | * |
2223 | | * This function can be used by a server to retrieve data sent early |
2224 | | * in the handshake processes when resuming a session. This is used |
2225 | | * to implement a zero-roundtrip (0-RTT) mode. It has the same |
2226 | | * semantics as gnutls_record_recv(). |
2227 | | * |
2228 | | * This function can be called either in a handshake hook, or after |
2229 | | * the handshake is complete. |
2230 | | * |
2231 | | * Returns: The number of bytes received and zero when early data |
2232 | | * reading is complete. A negative error code is returned in case of |
2233 | | * an error. If no early data is received during the handshake, this |
2234 | | * function returns %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE. The |
2235 | | * number of bytes received might be less than the requested |
2236 | | * @data_size. |
2237 | | * |
2238 | | * Since: 3.6.5 |
2239 | | **/ |
2240 | | ssize_t gnutls_record_recv_early_data(gnutls_session_t session, void *data, |
2241 | | size_t data_size) |
2242 | 0 | { |
2243 | 0 | mbuffer_st *bufel; |
2244 | 0 | gnutls_datum_t msg; |
2245 | 0 | size_t length; |
2246 | |
|
2247 | 0 | if (session->security_parameters.entity != GNUTLS_SERVER) |
2248 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
2249 | | |
2250 | 0 | bufel = _mbuffer_head_get_first( |
2251 | 0 | &session->internals.early_data_recv_buffer, &msg); |
2252 | 0 | if (bufel == NULL) |
2253 | 0 | return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE); |
2254 | | |
2255 | 0 | length = MIN(msg.size, data_size); |
2256 | 0 | memcpy(data, msg.data, length); |
2257 | 0 | _mbuffer_head_remove_bytes(&session->internals.early_data_recv_buffer, |
2258 | 0 | length); |
2259 | |
|
2260 | 0 | return length; |
2261 | 0 | } |
2262 | | |
2263 | | /** |
2264 | | * gnutls_record_cork: |
2265 | | * @session: is a #gnutls_session_t type. |
2266 | | * |
2267 | | * If called, gnutls_record_send() will no longer send any records. |
2268 | | * Any sent records will be cached until gnutls_record_uncork() is called. |
2269 | | * |
2270 | | * This function is safe to use with DTLS after GnuTLS 3.3.0. |
2271 | | * |
2272 | | * Since: 3.1.9 |
2273 | | **/ |
2274 | | void gnutls_record_cork(gnutls_session_t session) |
2275 | 0 | { |
2276 | 0 | session->internals.rsend_state = RECORD_SEND_CORKED; |
2277 | 0 | } |
2278 | | |
2279 | | /** |
2280 | | * gnutls_record_uncork: |
2281 | | * @session: is a #gnutls_session_t type. |
2282 | | * @flags: Could be zero or %GNUTLS_RECORD_WAIT |
2283 | | * |
2284 | | * This resets the effect of gnutls_record_cork(), and flushes any pending |
2285 | | * data. If the %GNUTLS_RECORD_WAIT flag is specified then this |
2286 | | * function will block until the data is sent or a fatal error |
2287 | | * occurs (i.e., the function will retry on %GNUTLS_E_AGAIN and |
2288 | | * %GNUTLS_E_INTERRUPTED). |
2289 | | * |
2290 | | * If the flag %GNUTLS_RECORD_WAIT is not specified and the function |
2291 | | * is interrupted then the %GNUTLS_E_AGAIN or %GNUTLS_E_INTERRUPTED |
2292 | | * errors will be returned. To obtain the data left in the corked |
2293 | | * buffer use gnutls_record_check_corked(). |
2294 | | * |
2295 | | * Returns: On success the number of transmitted data is returned, or |
2296 | | * otherwise a negative error code. |
2297 | | * |
2298 | | * Since: 3.1.9 |
2299 | | **/ |
2300 | | int gnutls_record_uncork(gnutls_session_t session, unsigned int flags) |
2301 | 0 | { |
2302 | 0 | int ret; |
2303 | 0 | ssize_t total = 0; |
2304 | 0 | record_send_state_t orig_state = session->internals.rsend_state; |
2305 | |
|
2306 | 0 | if (orig_state == RECORD_SEND_CORKED) |
2307 | 0 | session->internals.rsend_state = RECORD_SEND_NORMAL; |
2308 | 0 | else if (orig_state == RECORD_SEND_CORKED_TO_KU) |
2309 | 0 | session->internals.rsend_state = RECORD_SEND_KEY_UPDATE_1; |
2310 | 0 | else |
2311 | 0 | return 0; /* nothing to be done */ |
2312 | | |
2313 | 0 | while (session->internals.record_presend_buffer.length > 0) { |
2314 | 0 | if (flags == GNUTLS_RECORD_WAIT) { |
2315 | 0 | do { |
2316 | 0 | ret = gnutls_record_send( |
2317 | 0 | session, |
2318 | 0 | session->internals.record_presend_buffer |
2319 | 0 | .data, |
2320 | 0 | session->internals.record_presend_buffer |
2321 | 0 | .length); |
2322 | 0 | } while (ret < 0 && (ret == GNUTLS_E_AGAIN || |
2323 | 0 | ret == GNUTLS_E_INTERRUPTED)); |
2324 | 0 | } else { |
2325 | 0 | ret = gnutls_record_send( |
2326 | 0 | session, |
2327 | 0 | session->internals.record_presend_buffer.data, |
2328 | 0 | session->internals.record_presend_buffer.length); |
2329 | 0 | } |
2330 | 0 | if (ret < 0) |
2331 | 0 | goto fail; |
2332 | | |
2333 | 0 | session->internals.record_presend_buffer.data += ret; |
2334 | 0 | session->internals.record_presend_buffer.length -= ret; |
2335 | 0 | total += ret; |
2336 | 0 | } |
2337 | | |
2338 | 0 | return total; |
2339 | | |
2340 | 0 | fail: |
2341 | 0 | session->internals.rsend_state = orig_state; |
2342 | 0 | return ret; |
2343 | 0 | } |
2344 | | |
2345 | | /** |
2346 | | * gnutls_record_recv: |
2347 | | * @session: is a #gnutls_session_t type. |
2348 | | * @data: the buffer that the data will be read into |
2349 | | * @data_size: the number of requested bytes |
2350 | | * |
2351 | | * This function has the similar semantics with recv(). The only |
2352 | | * difference is that it accepts a GnuTLS session, and uses different |
2353 | | * error codes. |
2354 | | * In the special case that the peer requests a renegotiation, the |
2355 | | * caller will receive an error code of %GNUTLS_E_REHANDSHAKE. In case |
2356 | | * of a client, this message may be simply ignored, replied with an alert |
2357 | | * %GNUTLS_A_NO_RENEGOTIATION, or replied with a new handshake, |
2358 | | * depending on the client's will. A server receiving this error code |
2359 | | * can only initiate a new handshake or terminate the session. |
2360 | | * |
2361 | | * If %EINTR is returned by the internal pull function (the default |
2362 | | * is recv()) then %GNUTLS_E_INTERRUPTED will be returned. If |
2363 | | * %GNUTLS_E_INTERRUPTED or %GNUTLS_E_AGAIN is returned, you must |
2364 | | * call this function again to get the data. See also |
2365 | | * gnutls_record_get_direction(). |
2366 | | * |
2367 | | * Returns: The number of bytes received and zero on EOF (for stream |
2368 | | * connections). A negative error code is returned in case of an error. |
2369 | | * The number of bytes received might be less than the requested @data_size. |
2370 | | **/ |
2371 | | ssize_t gnutls_record_recv(gnutls_session_t session, void *data, |
2372 | | size_t data_size) |
2373 | 0 | { |
2374 | 0 | if (unlikely(!session->internals.initial_negotiation_completed)) { |
2375 | | /* this is to protect buggy applications from sending unencrypted |
2376 | | * data. We allow sending however, if we are in false start handshake |
2377 | | * state. */ |
2378 | 0 | if (session->internals.recv_state != RECV_STATE_FALSE_START && |
2379 | 0 | session->internals.recv_state != RECV_STATE_EARLY_START) |
2380 | 0 | return gnutls_assert_val( |
2381 | 0 | GNUTLS_E_UNAVAILABLE_DURING_HANDSHAKE); |
2382 | 0 | } |
2383 | | |
2384 | 0 | if (IS_KTLS_ENABLED(session, GNUTLS_KTLS_RECV)) { |
2385 | 0 | return _gnutls_ktls_recv(session, data, data_size); |
2386 | 0 | } else { |
2387 | 0 | return _gnutls_recv_int(session, GNUTLS_APPLICATION_DATA, data, |
2388 | 0 | data_size, NULL, |
2389 | 0 | session->internals.record_timeout_ms); |
2390 | 0 | } |
2391 | 0 | } |
2392 | | |
2393 | | /** |
2394 | | * gnutls_record_recv_seq: |
2395 | | * @session: is a #gnutls_session_t type. |
2396 | | * @data: the buffer that the data will be read into |
2397 | | * @data_size: the number of requested bytes |
2398 | | * @seq: is the packet's 64-bit sequence number. Should have space for 8 bytes. |
2399 | | * |
2400 | | * This function is the same as gnutls_record_recv(), except that |
2401 | | * it returns in addition to data, the sequence number of the data. |
2402 | | * This is useful in DTLS where record packets might be received |
2403 | | * out-of-order. The returned 8-byte sequence number is an |
2404 | | * integer in big-endian format and should be |
2405 | | * treated as a unique message identification. |
2406 | | * |
2407 | | * Returns: The number of bytes received and zero on EOF. A negative |
2408 | | * error code is returned in case of an error. The number of bytes |
2409 | | * received might be less than @data_size. |
2410 | | * |
2411 | | * Since: 3.0 |
2412 | | **/ |
2413 | | ssize_t gnutls_record_recv_seq(gnutls_session_t session, void *data, |
2414 | | size_t data_size, unsigned char *seq) |
2415 | 0 | { |
2416 | 0 | return _gnutls_recv_int(session, GNUTLS_APPLICATION_DATA, data, |
2417 | 0 | data_size, seq, |
2418 | 0 | session->internals.record_timeout_ms); |
2419 | 0 | } |
2420 | | |
2421 | | /** |
2422 | | * gnutls_record_set_timeout: |
2423 | | * @session: is a #gnutls_session_t type. |
2424 | | * @ms: is a timeout value in milliseconds |
2425 | | * |
2426 | | * This function sets the receive timeout for the record layer |
2427 | | * to the provided value. Use an @ms value of zero to disable |
2428 | | * timeout (the default), or %GNUTLS_INDEFINITE_TIMEOUT, to |
2429 | | * set an indefinite timeout. |
2430 | | * |
2431 | | * This function requires to set a pull timeout callback. See |
2432 | | * gnutls_transport_set_pull_timeout_function(). |
2433 | | * |
2434 | | * Since: 3.1.7 |
2435 | | **/ |
2436 | | void gnutls_record_set_timeout(gnutls_session_t session, unsigned int ms) |
2437 | 0 | { |
2438 | 0 | session->internals.record_timeout_ms = ms; |
2439 | 0 | } |
2440 | | |
2441 | | /** |
2442 | | * gnutls_handshake_write: |
2443 | | * @session: is a #gnutls_session_t type. |
2444 | | * @level: the current encryption level for reading a handshake message |
2445 | | * @data: the (const) handshake data to be processed |
2446 | | * @data_size: the size of data |
2447 | | * |
2448 | | * This function processes a handshake message in the encryption level |
2449 | | * specified with @level. Prior to calling this function, a handshake |
2450 | | * read callback must be set on @session. Use |
2451 | | * gnutls_handshake_set_read_function() to do this. |
2452 | | * |
2453 | | * Since: 3.7.0 |
2454 | | */ |
2455 | | int gnutls_handshake_write(gnutls_session_t session, |
2456 | | gnutls_record_encryption_level_t level, |
2457 | | const void *data, size_t data_size) |
2458 | 0 | { |
2459 | 0 | record_parameters_st *record_params; |
2460 | 0 | record_state_st *record_state; |
2461 | 0 | mbuffer_st *bufel; |
2462 | 0 | uint8_t *p; |
2463 | 0 | int ret; |
2464 | | |
2465 | | /* DTLS is not supported */ |
2466 | 0 | if (IS_DTLS(session)) |
2467 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
2468 | | |
2469 | | /* Nothing to do */ |
2470 | 0 | if (data_size == 0) |
2471 | 0 | return gnutls_assert_val(0); |
2472 | | |
2473 | | /* When using this, the outgoing handshake messages should |
2474 | | * also be handled manually unless KTLS is enabled exclusively |
2475 | | * in GNUTLS_KTLS_RECV mode in which case the outgoing messages |
2476 | | * are handled by GnuTLS. |
2477 | | */ |
2478 | 0 | if (!session->internals.h_read_func && |
2479 | 0 | !IS_KTLS_ENABLED(session, GNUTLS_KTLS_RECV)) |
2480 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
2481 | | |
2482 | 0 | if (session->internals.initial_negotiation_completed) { |
2483 | 0 | const version_entry_st *vers = get_version(session); |
2484 | 0 | if (unlikely(vers == NULL || !vers->tls13_sem)) |
2485 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
2486 | 0 | } |
2487 | | |
2488 | 0 | ret = _gnutls_epoch_get(session, EPOCH_READ_CURRENT, &record_params); |
2489 | 0 | if (ret < 0) |
2490 | 0 | return gnutls_assert_val(ret); |
2491 | | |
2492 | 0 | record_state = &record_params->read; |
2493 | 0 | if (record_state->level > level) |
2494 | 0 | return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED); |
2495 | | |
2496 | 0 | bufel = _mbuffer_alloc_align16(data_size, 0); |
2497 | 0 | if (bufel == NULL) |
2498 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
2499 | | |
2500 | 0 | memcpy(_mbuffer_get_udata_ptr(bufel), data, data_size); |
2501 | 0 | _mbuffer_set_udata_size(bufel, data_size); |
2502 | 0 | p = _mbuffer_get_udata_ptr(bufel); |
2503 | 0 | bufel->htype = p[0]; |
2504 | |
|
2505 | 0 | if (sequence_increment(session, &record_state->sequence_number) != 0) { |
2506 | 0 | _mbuffer_xfree(&bufel); |
2507 | 0 | return gnutls_assert_val(GNUTLS_E_RECORD_LIMIT_REACHED); |
2508 | 0 | } |
2509 | | |
2510 | 0 | _gnutls_record_buffer_put(session, GNUTLS_HANDSHAKE, |
2511 | 0 | record_state->sequence_number, bufel); |
2512 | |
|
2513 | 0 | if (session->internals.initial_negotiation_completed) |
2514 | 0 | return _gnutls13_recv_async_handshake(session); |
2515 | | |
2516 | 0 | return 0; |
2517 | 0 | } |