Lines Matching refs:ssl

57 static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl);
63 void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs) in mbedtls_ssl_set_timer() argument
65 if (ssl->f_set_timer == NULL) { in mbedtls_ssl_set_timer()
70 ssl->f_set_timer(ssl->p_timer, millisecs / 4, millisecs); in mbedtls_ssl_set_timer()
76 int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl) in mbedtls_ssl_check_timer() argument
78 if (ssl->f_get_timer == NULL) { in mbedtls_ssl_check_timer()
82 if (ssl->f_get_timer(ssl->p_timer) == 2) { in mbedtls_ssl_check_timer()
91 static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
96 int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl, in mbedtls_ssl_check_record() argument
107 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) { in mbedtls_ssl_check_record()
115 ret = ssl_parse_record_header(ssl, buf, buflen, &rec); in mbedtls_ssl_check_record()
121 if (ssl->transform_in != NULL) { in mbedtls_ssl_check_record()
122 ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, &rec); in mbedtls_ssl_check_record()
153 static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
155 static void ssl_free_buffered_record(mbedtls_ssl_context *ssl);
157 static int ssl_load_buffered_message(mbedtls_ssl_context *ssl);
159 static int ssl_load_buffered_record(mbedtls_ssl_context *ssl);
161 static int ssl_buffer_message(mbedtls_ssl_context *ssl);
163 static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
166 static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl);
168 static size_t ssl_get_maximum_datagram_size(mbedtls_ssl_context const *ssl) in ssl_get_maximum_datagram_size() argument
170 size_t mtu = mbedtls_ssl_get_current_mtu(ssl); in ssl_get_maximum_datagram_size()
172 size_t out_buf_len = ssl->out_buf_len; in ssl_get_maximum_datagram_size()
185 static int ssl_get_remaining_space_in_datagram(mbedtls_ssl_context const *ssl) in ssl_get_remaining_space_in_datagram() argument
187 size_t const bytes_written = ssl->out_left; in ssl_get_remaining_space_in_datagram()
188 size_t const mtu = ssl_get_maximum_datagram_size(ssl); in ssl_get_remaining_space_in_datagram()
201 static int ssl_get_remaining_payload_in_datagram(mbedtls_ssl_context const *ssl) in ssl_get_remaining_payload_in_datagram() argument
208 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl); in ssl_get_remaining_payload_in_datagram()
223 if (max_len <= ssl->out_left) { in ssl_get_remaining_payload_in_datagram()
227 max_len -= ssl->out_left; in ssl_get_remaining_payload_in_datagram()
230 ret = ssl_get_remaining_space_in_datagram(ssl); in ssl_get_remaining_payload_in_datagram()
236 ret = mbedtls_ssl_get_record_expansion(ssl); in ssl_get_remaining_payload_in_datagram()
259 static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl) in ssl_double_retransmit_timeout() argument
263 if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) { in ssl_double_retransmit_timeout()
273 if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) { in ssl_double_retransmit_timeout()
274 ssl->handshake->mtu = 508; in ssl_double_retransmit_timeout()
275 MBEDTLS_SSL_DEBUG_MSG(2, ("mtu autoreduction to %d bytes", ssl->handshake->mtu)); in ssl_double_retransmit_timeout()
278 new_timeout = 2 * ssl->handshake->retransmit_timeout; in ssl_double_retransmit_timeout()
281 if (new_timeout < ssl->handshake->retransmit_timeout || in ssl_double_retransmit_timeout()
282 new_timeout > ssl->conf->hs_timeout_max) { in ssl_double_retransmit_timeout()
283 new_timeout = ssl->conf->hs_timeout_max; in ssl_double_retransmit_timeout()
286 ssl->handshake->retransmit_timeout = new_timeout; in ssl_double_retransmit_timeout()
288 (unsigned long) ssl->handshake->retransmit_timeout)); in ssl_double_retransmit_timeout()
293 static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl) in ssl_reset_retransmit_timeout() argument
295 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; in ssl_reset_retransmit_timeout()
297 (unsigned long) ssl->handshake->retransmit_timeout)); in ssl_reset_retransmit_timeout()
681 int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl, in mbedtls_ssl_encrypt_buf() argument
703 ssl = NULL; /* make sure we don't use it except for debug */ in mbedtls_ssl_encrypt_buf()
704 ((void) ssl); in mbedtls_ssl_encrypt_buf()
1261 int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl, in mbedtls_ssl_decrypt_buf() argument
1287 ssl = NULL; /* make sure we don't use it except for debug */ in mbedtls_ssl_decrypt_buf()
1288 ((void) ssl); in mbedtls_ssl_decrypt_buf()
1916 int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want) in mbedtls_ssl_fetch_input() argument
1921 size_t in_buf_len = ssl->in_buf_len; in mbedtls_ssl_fetch_input()
1928 if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) { in mbedtls_ssl_fetch_input()
1933 if (nb_want > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) { in mbedtls_ssl_fetch_input()
1939 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_fetch_input()
1952 if (ssl->next_record_offset != 0) { in mbedtls_ssl_fetch_input()
1953 if (ssl->in_left < ssl->next_record_offset) { in mbedtls_ssl_fetch_input()
1958 ssl->in_left -= ssl->next_record_offset; in mbedtls_ssl_fetch_input()
1960 if (ssl->in_left != 0) { in mbedtls_ssl_fetch_input()
1963 ssl->next_record_offset)); in mbedtls_ssl_fetch_input()
1964 memmove(ssl->in_hdr, in mbedtls_ssl_fetch_input()
1965 ssl->in_hdr + ssl->next_record_offset, in mbedtls_ssl_fetch_input()
1966 ssl->in_left); in mbedtls_ssl_fetch_input()
1969 ssl->next_record_offset = 0; in mbedtls_ssl_fetch_input()
1974 ssl->in_left, nb_want)); in mbedtls_ssl_fetch_input()
1979 if (nb_want <= ssl->in_left) { in mbedtls_ssl_fetch_input()
1989 if (ssl->in_left != 0) { in mbedtls_ssl_fetch_input()
1999 if (mbedtls_ssl_check_timer(ssl) != 0) { in mbedtls_ssl_fetch_input()
2003 len = in_buf_len - (ssl->in_hdr - ssl->in_buf); in mbedtls_ssl_fetch_input()
2005 if (mbedtls_ssl_is_handshake_over(ssl) == 0) { in mbedtls_ssl_fetch_input()
2006 timeout = ssl->handshake->retransmit_timeout; in mbedtls_ssl_fetch_input()
2008 timeout = ssl->conf->read_timeout; in mbedtls_ssl_fetch_input()
2013 if (ssl->f_recv_timeout != NULL) { in mbedtls_ssl_fetch_input()
2014 ret = ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len, in mbedtls_ssl_fetch_input()
2017 ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len); in mbedtls_ssl_fetch_input()
2029 mbedtls_ssl_set_timer(ssl, 0); in mbedtls_ssl_fetch_input()
2031 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { in mbedtls_ssl_fetch_input()
2032 if (ssl_double_retransmit_timeout(ssl) != 0) { in mbedtls_ssl_fetch_input()
2037 if ((ret = mbedtls_ssl_resend(ssl)) != 0) { in mbedtls_ssl_fetch_input()
2045 else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && in mbedtls_ssl_fetch_input()
2046 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { in mbedtls_ssl_fetch_input()
2047 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) { in mbedtls_ssl_fetch_input()
2062 ssl->in_left = ret; in mbedtls_ssl_fetch_input()
2068 ssl->in_left, nb_want)); in mbedtls_ssl_fetch_input()
2070 while (ssl->in_left < nb_want) { in mbedtls_ssl_fetch_input()
2071 len = nb_want - ssl->in_left; in mbedtls_ssl_fetch_input()
2073 if (mbedtls_ssl_check_timer(ssl) != 0) { in mbedtls_ssl_fetch_input()
2076 if (ssl->f_recv_timeout != NULL) { in mbedtls_ssl_fetch_input()
2077 ret = ssl->f_recv_timeout(ssl->p_bio, in mbedtls_ssl_fetch_input()
2078 ssl->in_hdr + ssl->in_left, len, in mbedtls_ssl_fetch_input()
2079 ssl->conf->read_timeout); in mbedtls_ssl_fetch_input()
2081 ret = ssl->f_recv(ssl->p_bio, in mbedtls_ssl_fetch_input()
2082 ssl->in_hdr + ssl->in_left, len); in mbedtls_ssl_fetch_input()
2088 ssl->in_left, nb_want)); in mbedtls_ssl_fetch_input()
2107 ssl->in_left += ret; in mbedtls_ssl_fetch_input()
2119 int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl) in mbedtls_ssl_flush_output() argument
2126 if (ssl->f_send == NULL) { in mbedtls_ssl_flush_output()
2132 if (ssl->out_left == 0) { in mbedtls_ssl_flush_output()
2137 while (ssl->out_left > 0) { in mbedtls_ssl_flush_output()
2140 mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left)); in mbedtls_ssl_flush_output()
2142 buf = ssl->out_hdr - ssl->out_left; in mbedtls_ssl_flush_output()
2143 ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left); in mbedtls_ssl_flush_output()
2151 if ((size_t) ret > ssl->out_left) { in mbedtls_ssl_flush_output()
2155 ret, ssl->out_left)); in mbedtls_ssl_flush_output()
2159 ssl->out_left -= ret; in mbedtls_ssl_flush_output()
2163 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_flush_output()
2164 ssl->out_hdr = ssl->out_buf; in mbedtls_ssl_flush_output()
2168 ssl->out_hdr = ssl->out_buf + 8; in mbedtls_ssl_flush_output()
2170 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); in mbedtls_ssl_flush_output()
2185 static int ssl_flight_append(mbedtls_ssl_context *ssl) in ssl_flight_append() argument
2190 ssl->out_msg, ssl->out_msglen); in ssl_flight_append()
2199 if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) { in ssl_flight_append()
2201 ssl->out_msglen)); in ssl_flight_append()
2207 memcpy(msg->p, ssl->out_msg, ssl->out_msglen); in ssl_flight_append()
2208 msg->len = ssl->out_msglen; in ssl_flight_append()
2209 msg->type = ssl->out_msgtype; in ssl_flight_append()
2213 if (ssl->handshake->flight == NULL) { in ssl_flight_append()
2214 ssl->handshake->flight = msg; in ssl_flight_append()
2216 mbedtls_ssl_flight_item *cur = ssl->handshake->flight; in ssl_flight_append()
2249 static int ssl_swap_epochs(mbedtls_ssl_context *ssl) in ssl_swap_epochs() argument
2254 if (ssl->transform_out == ssl->handshake->alt_transform_out) { in ssl_swap_epochs()
2262 tmp_transform = ssl->transform_out; in ssl_swap_epochs()
2263 ssl->transform_out = ssl->handshake->alt_transform_out; in ssl_swap_epochs()
2264 ssl->handshake->alt_transform_out = tmp_transform; in ssl_swap_epochs()
2267 memcpy(tmp_out_ctr, ssl->cur_out_ctr, sizeof(tmp_out_ctr)); in ssl_swap_epochs()
2268 memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, in ssl_swap_epochs()
2269 sizeof(ssl->cur_out_ctr)); in ssl_swap_epochs()
2270 memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr, in ssl_swap_epochs()
2271 sizeof(ssl->handshake->alt_out_ctr)); in ssl_swap_epochs()
2274 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); in ssl_swap_epochs()
2282 int mbedtls_ssl_resend(mbedtls_ssl_context *ssl) in mbedtls_ssl_resend() argument
2288 ret = mbedtls_ssl_flight_transmit(ssl); in mbedtls_ssl_resend()
2302 int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl) in mbedtls_ssl_flight_transmit() argument
2307 if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) { in mbedtls_ssl_flight_transmit()
2310 ssl->handshake->cur_msg = ssl->handshake->flight; in mbedtls_ssl_flight_transmit()
2311 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; in mbedtls_ssl_flight_transmit()
2312 ret = ssl_swap_epochs(ssl); in mbedtls_ssl_flight_transmit()
2317 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; in mbedtls_ssl_flight_transmit()
2320 while (ssl->handshake->cur_msg != NULL) { in mbedtls_ssl_flight_transmit()
2322 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; in mbedtls_ssl_flight_transmit()
2328 int const force_flush = ssl->disable_datagram_packing == 1 ? in mbedtls_ssl_flight_transmit()
2334 if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) { in mbedtls_ssl_flight_transmit()
2336 ret = ssl_swap_epochs(ssl); in mbedtls_ssl_flight_transmit()
2342 ret = ssl_get_remaining_payload_in_datagram(ssl); in mbedtls_ssl_flight_transmit()
2351 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { in mbedtls_ssl_flight_transmit()
2358 memcpy(ssl->out_msg, cur->p, cur->len); in mbedtls_ssl_flight_transmit()
2359 ssl->out_msglen = cur->len; in mbedtls_ssl_flight_transmit()
2360 ssl->out_msgtype = cur->type; in mbedtls_ssl_flight_transmit()
2363 ssl->handshake->cur_msg_p += cur->len; in mbedtls_ssl_flight_transmit()
2365 const unsigned char * const p = ssl->handshake->cur_msg_p; in mbedtls_ssl_flight_transmit()
2373 ret = ssl_swap_epochs(ssl); in mbedtls_ssl_flight_transmit()
2379 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { in mbedtls_ssl_flight_transmit()
2399 memcpy(ssl->out_msg, cur->p, 6); in mbedtls_ssl_flight_transmit()
2401 ssl->out_msg[6] = MBEDTLS_BYTE_2(frag_off); in mbedtls_ssl_flight_transmit()
2402 ssl->out_msg[7] = MBEDTLS_BYTE_1(frag_off); in mbedtls_ssl_flight_transmit()
2403 ssl->out_msg[8] = MBEDTLS_BYTE_0(frag_off); in mbedtls_ssl_flight_transmit()
2405 ssl->out_msg[9] = MBEDTLS_BYTE_2(cur_hs_frag_len); in mbedtls_ssl_flight_transmit()
2406 ssl->out_msg[10] = MBEDTLS_BYTE_1(cur_hs_frag_len); in mbedtls_ssl_flight_transmit()
2407 ssl->out_msg[11] = MBEDTLS_BYTE_0(cur_hs_frag_len); in mbedtls_ssl_flight_transmit()
2409 MBEDTLS_SSL_DEBUG_BUF(3, "handshake header", ssl->out_msg, 12); in mbedtls_ssl_flight_transmit()
2412 memcpy(ssl->out_msg + 12, p, cur_hs_frag_len); in mbedtls_ssl_flight_transmit()
2413 ssl->out_msglen = cur_hs_frag_len + 12; in mbedtls_ssl_flight_transmit()
2414 ssl->out_msgtype = cur->type; in mbedtls_ssl_flight_transmit()
2417 ssl->handshake->cur_msg_p += cur_hs_frag_len; in mbedtls_ssl_flight_transmit()
2421 if (ssl->handshake->cur_msg_p >= cur->p + cur->len) { in mbedtls_ssl_flight_transmit()
2423 ssl->handshake->cur_msg = cur->next; in mbedtls_ssl_flight_transmit()
2424 ssl->handshake->cur_msg_p = cur->next->p + 12; in mbedtls_ssl_flight_transmit()
2426 ssl->handshake->cur_msg = NULL; in mbedtls_ssl_flight_transmit()
2427 ssl->handshake->cur_msg_p = NULL; in mbedtls_ssl_flight_transmit()
2432 if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) { in mbedtls_ssl_flight_transmit()
2438 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { in mbedtls_ssl_flight_transmit()
2443 if (mbedtls_ssl_is_handshake_over(ssl) == 1) { in mbedtls_ssl_flight_transmit()
2444 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; in mbedtls_ssl_flight_transmit()
2446 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; in mbedtls_ssl_flight_transmit()
2447 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout); in mbedtls_ssl_flight_transmit()
2458 void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl) in mbedtls_ssl_recv_flight_completed() argument
2461 mbedtls_ssl_flight_free(ssl->handshake->flight); in mbedtls_ssl_recv_flight_completed()
2462 ssl->handshake->flight = NULL; in mbedtls_ssl_recv_flight_completed()
2463 ssl->handshake->cur_msg = NULL; in mbedtls_ssl_recv_flight_completed()
2466 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; in mbedtls_ssl_recv_flight_completed()
2469 ssl->handshake->buffering.seen_ccs = 0; in mbedtls_ssl_recv_flight_completed()
2472 mbedtls_ssl_buffering_free(ssl); in mbedtls_ssl_recv_flight_completed()
2475 mbedtls_ssl_set_timer(ssl, 0); in mbedtls_ssl_recv_flight_completed()
2477 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && in mbedtls_ssl_recv_flight_completed()
2478 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) { in mbedtls_ssl_recv_flight_completed()
2479 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; in mbedtls_ssl_recv_flight_completed()
2481 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; in mbedtls_ssl_recv_flight_completed()
2488 void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl) in mbedtls_ssl_send_flight_completed() argument
2490 ssl_reset_retransmit_timeout(ssl); in mbedtls_ssl_send_flight_completed()
2491 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout); in mbedtls_ssl_send_flight_completed()
2493 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && in mbedtls_ssl_send_flight_completed()
2494 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) { in mbedtls_ssl_send_flight_completed()
2495 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; in mbedtls_ssl_send_flight_completed()
2497 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; in mbedtls_ssl_send_flight_completed()
2505 int mbedtls_ssl_start_handshake_msg(mbedtls_ssl_context *ssl, unsigned hs_type, in mbedtls_ssl_start_handshake_msg() argument
2515 *buf = ssl->out_msg + 4; in mbedtls_ssl_start_handshake_msg()
2518 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; in mbedtls_ssl_start_handshake_msg()
2519 ssl->out_msg[0] = hs_type; in mbedtls_ssl_start_handshake_msg()
2546 int mbedtls_ssl_write_handshake_msg_ext(mbedtls_ssl_context *ssl, in mbedtls_ssl_write_handshake_msg_ext() argument
2551 const size_t hs_len = ssl->out_msglen - 4; in mbedtls_ssl_write_handshake_msg_ext()
2552 const unsigned char hs_type = ssl->out_msg[0]; in mbedtls_ssl_write_handshake_msg_ext()
2559 if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && in mbedtls_ssl_write_handshake_msg_ext()
2560 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { in mbedtls_ssl_write_handshake_msg_ext()
2567 if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && in mbedtls_ssl_write_handshake_msg_ext()
2569 ssl->handshake == NULL) { in mbedtls_ssl_write_handshake_msg_ext()
2575 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && in mbedtls_ssl_write_handshake_msg_ext()
2576 ssl->handshake != NULL && in mbedtls_ssl_write_handshake_msg_ext()
2577 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { in mbedtls_ssl_write_handshake_msg_ext()
2591 if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) { in mbedtls_ssl_write_handshake_msg_ext()
2595 ssl->out_msglen, in mbedtls_ssl_write_handshake_msg_ext()
2603 if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { in mbedtls_ssl_write_handshake_msg_ext()
2604 ssl->out_msg[1] = MBEDTLS_BYTE_2(hs_len); in mbedtls_ssl_write_handshake_msg_ext()
2605 ssl->out_msg[2] = MBEDTLS_BYTE_1(hs_len); in mbedtls_ssl_write_handshake_msg_ext()
2606 ssl->out_msg[3] = MBEDTLS_BYTE_0(hs_len); in mbedtls_ssl_write_handshake_msg_ext()
2616 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_write_handshake_msg_ext()
2618 if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) { in mbedtls_ssl_write_handshake_msg_ext()
2627 memmove(ssl->out_msg + 12, ssl->out_msg + 4, hs_len); in mbedtls_ssl_write_handshake_msg_ext()
2628 ssl->out_msglen += 8; in mbedtls_ssl_write_handshake_msg_ext()
2632 MBEDTLS_PUT_UINT16_BE(ssl->handshake->out_msg_seq, ssl->out_msg, 4); in mbedtls_ssl_write_handshake_msg_ext()
2633 ++(ssl->handshake->out_msg_seq); in mbedtls_ssl_write_handshake_msg_ext()
2635 ssl->out_msg[4] = 0; in mbedtls_ssl_write_handshake_msg_ext()
2636 ssl->out_msg[5] = 0; in mbedtls_ssl_write_handshake_msg_ext()
2641 memset(ssl->out_msg + 6, 0x00, 3); in mbedtls_ssl_write_handshake_msg_ext()
2642 memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3); in mbedtls_ssl_write_handshake_msg_ext()
2648 ret = ssl->handshake->update_checksum(ssl, ssl->out_msg, in mbedtls_ssl_write_handshake_msg_ext()
2649 ssl->out_msglen); in mbedtls_ssl_write_handshake_msg_ext()
2659 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && in mbedtls_ssl_write_handshake_msg_ext()
2660 !(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && in mbedtls_ssl_write_handshake_msg_ext()
2662 if ((ret = ssl_flight_append(ssl)) != 0) { in mbedtls_ssl_write_handshake_msg_ext()
2669 if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) { in mbedtls_ssl_write_handshake_msg_ext()
2680 int mbedtls_ssl_finish_handshake_msg(mbedtls_ssl_context *ssl, in mbedtls_ssl_finish_handshake_msg() argument
2689 ssl->out_msglen = msg_with_header_len; in mbedtls_ssl_finish_handshake_msg()
2690 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_handshake_msg_ext(ssl, 0, 0)); in mbedtls_ssl_finish_handshake_msg()
2708 int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, int force_flush) in mbedtls_ssl_write_record() argument
2711 size_t len = ssl->out_msglen; in mbedtls_ssl_write_record()
2720 size_t out_buf_len = ssl->out_buf_len; in mbedtls_ssl_write_record()
2726 mbedtls_ssl_protocol_version tls_ver = ssl->tls_version; in mbedtls_ssl_write_record()
2734 mbedtls_ssl_write_version(ssl->out_hdr + 1, ssl->conf->transport, in mbedtls_ssl_write_record()
2737 memcpy(ssl->out_ctr, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); in mbedtls_ssl_write_record()
2738 MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0); in mbedtls_ssl_write_record()
2740 if (ssl->transform_out != NULL) { in mbedtls_ssl_write_record()
2743 rec.buf = ssl->out_iv; in mbedtls_ssl_write_record()
2744 rec.buf_len = out_buf_len - (ssl->out_iv - ssl->out_buf); in mbedtls_ssl_write_record()
2745 rec.data_len = ssl->out_msglen; in mbedtls_ssl_write_record()
2746 rec.data_offset = ssl->out_msg - rec.buf; in mbedtls_ssl_write_record()
2748 memcpy(&rec.ctr[0], ssl->out_ctr, sizeof(rec.ctr)); in mbedtls_ssl_write_record()
2749 mbedtls_ssl_write_version(rec.ver, ssl->conf->transport, tls_ver); in mbedtls_ssl_write_record()
2750 rec.type = ssl->out_msgtype; in mbedtls_ssl_write_record()
2757 if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec, in mbedtls_ssl_write_record()
2758 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { in mbedtls_ssl_write_record()
2769 ssl->out_msgtype = rec.type; in mbedtls_ssl_write_record()
2771 memcpy(ssl->out_cid, rec.cid, rec.cid_len); in mbedtls_ssl_write_record()
2773 ssl->out_msglen = len = rec.data_len; in mbedtls_ssl_write_record()
2774 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0); in mbedtls_ssl_write_record()
2777 protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl); in mbedtls_ssl_write_record()
2782 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_write_record()
2783 ret = ssl_get_remaining_space_in_datagram(ssl); in mbedtls_ssl_write_record()
2796 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; in mbedtls_ssl_write_record()
2800 ssl->out_hdr[0], ssl->out_hdr[1], in mbedtls_ssl_write_record()
2801 ssl->out_hdr[2], len)); in mbedtls_ssl_write_record()
2804 ssl->out_hdr, protected_record_size); in mbedtls_ssl_write_record()
2806 ssl->out_left += protected_record_size; in mbedtls_ssl_write_record()
2807 ssl->out_hdr += protected_record_size; in mbedtls_ssl_write_record()
2808 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); in mbedtls_ssl_write_record()
2810 for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) { in mbedtls_ssl_write_record()
2811 if (++ssl->cur_out_ctr[i - 1] != 0) { in mbedtls_ssl_write_record()
2817 if (i == mbedtls_ssl_ep_len(ssl)) { in mbedtls_ssl_write_record()
2824 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && in mbedtls_ssl_write_record()
2827 ret = ssl_get_remaining_payload_in_datagram(ssl); in mbedtls_ssl_write_record()
2846 (ret = mbedtls_ssl_flush_output(ssl)) != 0) { in mbedtls_ssl_write_record()
2859 static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl) in ssl_hs_is_proper_fragment() argument
2861 if (ssl->in_msglen < ssl->in_hslen || in ssl_hs_is_proper_fragment()
2862 memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 || in ssl_hs_is_proper_fragment()
2863 memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) { in ssl_hs_is_proper_fragment()
2869 static uint32_t ssl_get_hs_frag_len(mbedtls_ssl_context const *ssl) in ssl_get_hs_frag_len() argument
2871 return (ssl->in_msg[9] << 16) | in ssl_get_hs_frag_len()
2872 (ssl->in_msg[10] << 8) | in ssl_get_hs_frag_len()
2873 ssl->in_msg[11]; in ssl_get_hs_frag_len()
2876 static uint32_t ssl_get_hs_frag_off(mbedtls_ssl_context const *ssl) in ssl_get_hs_frag_off() argument
2878 return (ssl->in_msg[6] << 16) | in ssl_get_hs_frag_off()
2879 (ssl->in_msg[7] << 8) | in ssl_get_hs_frag_off()
2880 ssl->in_msg[8]; in ssl_get_hs_frag_off()
2884 static int ssl_check_hs_header(mbedtls_ssl_context const *ssl) in ssl_check_hs_header() argument
2888 msg_len = ssl_get_hs_total_len(ssl); in ssl_check_hs_header()
2889 frag_off = ssl_get_hs_frag_off(ssl); in ssl_check_hs_header()
2890 frag_len = ssl_get_hs_frag_len(ssl); in ssl_check_hs_header()
2900 if (frag_len + 12 > ssl->in_msglen) { in ssl_check_hs_header()
2991 static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl) in ssl_get_hs_total_len() argument
2993 return (ssl->in_msg[1] << 16) | in ssl_get_hs_total_len()
2994 (ssl->in_msg[2] << 8) | in ssl_get_hs_total_len()
2995 ssl->in_msg[3]; in ssl_get_hs_total_len()
2998 int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl) in mbedtls_ssl_prepare_handshake_record() argument
3000 if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) { in mbedtls_ssl_prepare_handshake_record()
3002 ssl->in_msglen)); in mbedtls_ssl_prepare_handshake_record()
3006 ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl); in mbedtls_ssl_prepare_handshake_record()
3011 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen)); in mbedtls_ssl_prepare_handshake_record()
3014 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_prepare_handshake_record()
3016 unsigned int recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5]; in mbedtls_ssl_prepare_handshake_record()
3018 if (ssl_check_hs_header(ssl) != 0) { in mbedtls_ssl_prepare_handshake_record()
3023 if (ssl->handshake != NULL && in mbedtls_ssl_prepare_handshake_record()
3024 ((mbedtls_ssl_is_handshake_over(ssl) == 0 && in mbedtls_ssl_prepare_handshake_record()
3025 recv_msg_seq != ssl->handshake->in_msg_seq) || in mbedtls_ssl_prepare_handshake_record()
3026 (mbedtls_ssl_is_handshake_over(ssl) == 1 && in mbedtls_ssl_prepare_handshake_record()
3027 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) { in mbedtls_ssl_prepare_handshake_record()
3028 if (recv_msg_seq > ssl->handshake->in_msg_seq) { in mbedtls_ssl_prepare_handshake_record()
3033 ssl->handshake->in_msg_seq)); in mbedtls_ssl_prepare_handshake_record()
3040 if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 && in mbedtls_ssl_prepare_handshake_record()
3041 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) { in mbedtls_ssl_prepare_handshake_record()
3045 ssl->handshake->in_flight_start_seq)); in mbedtls_ssl_prepare_handshake_record()
3047 if ((ret = mbedtls_ssl_resend(ssl)) != 0) { in mbedtls_ssl_prepare_handshake_record()
3055 ssl->handshake->in_msg_seq)); in mbedtls_ssl_prepare_handshake_record()
3066 if (ssl_hs_is_proper_fragment(ssl) == 1) { in mbedtls_ssl_prepare_handshake_record()
3073 if (ssl->in_msglen < ssl->in_hslen) { in mbedtls_ssl_prepare_handshake_record()
3081 int mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl) in mbedtls_ssl_update_handshake_status() argument
3084 mbedtls_ssl_handshake_params * const hs = ssl->handshake; in mbedtls_ssl_update_handshake_status()
3086 if (mbedtls_ssl_is_handshake_over(ssl) == 0 && hs != NULL) { in mbedtls_ssl_update_handshake_status()
3087 ret = ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen); in mbedtls_ssl_update_handshake_status()
3096 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && in mbedtls_ssl_update_handshake_status()
3097 ssl->handshake != NULL) { in mbedtls_ssl_update_handshake_status()
3109 ssl_buffering_free_slot(ssl, 0); in mbedtls_ssl_update_handshake_status()
3136 void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl) in mbedtls_ssl_dtls_replay_reset() argument
3138 ssl->in_window_top = 0; in mbedtls_ssl_dtls_replay_reset()
3139 ssl->in_window = 0; in mbedtls_ssl_dtls_replay_reset()
3153 static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, uint8_t *record_in_ctr) in mbedtls_ssl_dtls_record_replay_check() argument
3159 original_in_ctr = ssl->in_ctr; in mbedtls_ssl_dtls_record_replay_check()
3162 ssl->in_ctr = record_in_ctr; in mbedtls_ssl_dtls_record_replay_check()
3164 ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl); in mbedtls_ssl_dtls_record_replay_check()
3167 ssl->in_ctr = original_in_ctr; in mbedtls_ssl_dtls_record_replay_check()
3175 int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl) in mbedtls_ssl_dtls_replay_check() argument
3177 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2); in mbedtls_ssl_dtls_replay_check()
3180 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) { in mbedtls_ssl_dtls_replay_check()
3184 if (rec_seqnum > ssl->in_window_top) { in mbedtls_ssl_dtls_replay_check()
3188 bit = ssl->in_window_top - rec_seqnum; in mbedtls_ssl_dtls_replay_check()
3194 if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) { in mbedtls_ssl_dtls_replay_check()
3204 void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl) in mbedtls_ssl_dtls_replay_update() argument
3206 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2); in mbedtls_ssl_dtls_replay_update()
3208 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) { in mbedtls_ssl_dtls_replay_update()
3212 if (rec_seqnum > ssl->in_window_top) { in mbedtls_ssl_dtls_replay_update()
3214 uint64_t shift = rec_seqnum - ssl->in_window_top; in mbedtls_ssl_dtls_replay_update()
3217 ssl->in_window = 1; in mbedtls_ssl_dtls_replay_update()
3219 ssl->in_window <<= shift; in mbedtls_ssl_dtls_replay_update()
3220 ssl->in_window |= 1; in mbedtls_ssl_dtls_replay_update()
3223 ssl->in_window_top = rec_seqnum; in mbedtls_ssl_dtls_replay_update()
3226 uint64_t bit = ssl->in_window_top - rec_seqnum; in mbedtls_ssl_dtls_replay_update()
3229 ssl->in_window |= (uint64_t) 1 << bit; in mbedtls_ssl_dtls_replay_update()
3250 mbedtls_ssl_context *ssl, in mbedtls_ssl_check_dtls_clihlo_cookie() argument
3324 if (ssl->conf->f_cookie_check(ssl->conf->p_cookie, in mbedtls_ssl_check_dtls_clihlo_cookie()
3363 if (ssl->conf->f_cookie_write(ssl->conf->p_cookie, in mbedtls_ssl_check_dtls_clihlo_cookie()
3404 static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl) in ssl_handle_possible_reconnect() argument
3409 if (ssl->conf->f_cookie_write == NULL || in ssl_handle_possible_reconnect()
3410 ssl->conf->f_cookie_check == NULL) { in ssl_handle_possible_reconnect()
3419 ssl, in ssl_handle_possible_reconnect()
3420 ssl->cli_id, ssl->cli_id_len, in ssl_handle_possible_reconnect()
3421 ssl->in_buf, ssl->in_left, in ssl_handle_possible_reconnect()
3422 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len); in ssl_handle_possible_reconnect()
3430 ssl->out_buf, len); in ssl_handle_possible_reconnect()
3434 send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len); in ssl_handle_possible_reconnect()
3443 if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) { in ssl_handle_possible_reconnect()
3488 static int ssl_parse_record_header(mbedtls_ssl_context const *ssl, in ssl_parse_record_header() argument
3523 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_parse_record_header()
3550 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && in ssl_parse_record_header()
3551 ssl->conf->cid_len != 0 && in ssl_parse_record_header()
3568 rec_hdr_cid_len = ssl->conf->cid_len; in ssl_parse_record_header()
3600 ssl->conf->transport); in ssl_parse_record_header()
3602 if (tls_version > ssl->conf->max_tls_version) { in ssl_parse_record_header()
3605 (unsigned) ssl->conf->max_tls_version)); in ssl_parse_record_header()
3614 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_parse_record_header()
3622 memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len); in ssl_parse_record_header()
3658 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_parse_record_header()
3675 if (rec_epoch != ssl->in_epoch) { in ssl_parse_record_header()
3678 ssl->in_epoch, (unsigned long) rec_epoch)); in ssl_parse_record_header()
3682 if (rec_epoch == (unsigned) ssl->in_epoch + 1) { in ssl_parse_record_header()
3692 else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl, in ssl_parse_record_header()
3707 static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl) in ssl_check_client_reconnect() argument
3709 unsigned int rec_epoch = (ssl->in_ctr[0] << 8) | ssl->in_ctr[1]; in ssl_check_client_reconnect()
3718 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && in ssl_check_client_reconnect()
3719 mbedtls_ssl_is_handshake_over(ssl) == 1 && in ssl_check_client_reconnect()
3720 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && in ssl_check_client_reconnect()
3721 ssl->in_left > 13 && in ssl_check_client_reconnect()
3722 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) { in ssl_check_client_reconnect()
3725 return ssl_handle_possible_reconnect(ssl); in ssl_check_client_reconnect()
3736 static int ssl_prepare_record_content(mbedtls_ssl_context *ssl, in ssl_prepare_record_content() argument
3750 if (ssl->transform_in != NULL && in ssl_prepare_record_content()
3751 ssl->transform_in->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { in ssl_prepare_record_content()
3758 if (!done && ssl->transform_in != NULL) { in ssl_prepare_record_content()
3761 if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, in ssl_prepare_record_content()
3767 ssl->conf->ignore_unexpected_cid in ssl_prepare_record_content()
3801 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 in ssl_prepare_record_content()
3804 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype)); in ssl_prepare_record_content()
3809 ssl->nb_zero++; in ssl_prepare_record_content()
3815 if (ssl->nb_zero > 3) { in ssl_prepare_record_content()
3824 ssl->nb_zero = 0; in ssl_prepare_record_content()
3828 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_prepare_record_content()
3835 i > mbedtls_ssl_ep_len(ssl); i--) { in ssl_prepare_record_content()
3836 if (++ssl->in_ctr[i - 1] != 0) { in ssl_prepare_record_content()
3842 if (i == mbedtls_ssl_ep_len(ssl)) { in ssl_prepare_record_content()
3851 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_prepare_record_content()
3852 mbedtls_ssl_dtls_replay_update(ssl); in ssl_prepare_record_content()
3876 static int ssl_consume_current_message(mbedtls_ssl_context *ssl);
3878 static int ssl_get_next_record(mbedtls_ssl_context *ssl);
3880 static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl);
3882 int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl, in mbedtls_ssl_read_record() argument
3889 if (ssl->keep_current_message == 0) { in mbedtls_ssl_read_record()
3892 ret = ssl_consume_current_message(ssl); in mbedtls_ssl_read_record()
3897 if (ssl_record_is_in_progress(ssl) == 0) { in mbedtls_ssl_read_record()
3903 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && in mbedtls_ssl_read_record()
3904 ssl_next_record_is_in_datagram(ssl) == 0) { in mbedtls_ssl_read_record()
3905 if (ssl_load_buffered_message(ssl) == 0) { in mbedtls_ssl_read_record()
3912 ret = ssl_get_next_record(ssl); in mbedtls_ssl_read_record()
3924 ret = mbedtls_ssl_handle_message_type(ssl); in mbedtls_ssl_read_record()
3929 ret = ssl_buffer_message(ssl); in mbedtls_ssl_read_record()
3946 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && in mbedtls_ssl_read_record()
3948 ret = mbedtls_ssl_update_handshake_status(ssl); in mbedtls_ssl_read_record()
3956 ssl->keep_current_message = 0; in mbedtls_ssl_read_record()
3966 static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl) in ssl_next_record_is_in_datagram() argument
3968 if (ssl->in_left > ssl->next_record_offset) { in ssl_next_record_is_in_datagram()
3976 static int ssl_load_buffered_message(mbedtls_ssl_context *ssl) in ssl_load_buffered_message() argument
3978 mbedtls_ssl_handshake_params * const hs = ssl->handshake; in ssl_load_buffered_message()
3988 if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || in ssl_load_buffered_message()
3989 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) { in ssl_load_buffered_message()
3999 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; in ssl_load_buffered_message()
4000 ssl->in_msglen = 1; in ssl_load_buffered_message()
4001 ssl->in_msg[0] = 1; in ssl_load_buffered_message()
4004 ssl->in_left = 0; in ssl_load_buffered_message()
4005 ssl->next_record_offset = 0; in ssl_load_buffered_message()
4046 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; in ssl_load_buffered_message()
4047 ssl->in_hslen = msg_len + 12; in ssl_load_buffered_message()
4048 ssl->in_msglen = msg_len + 12; in ssl_load_buffered_message()
4049 memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen); in ssl_load_buffered_message()
4067 static int ssl_buffer_make_space(mbedtls_ssl_context *ssl, in ssl_buffer_make_space() argument
4071 mbedtls_ssl_handshake_params * const hs = ssl->handshake; in ssl_buffer_make_space()
4076 ssl_free_buffered_record(ssl); in ssl_buffer_make_space()
4095 ssl_buffering_free_slot(ssl, (uint8_t) offset); in ssl_buffer_make_space()
4109 static int ssl_buffer_message(mbedtls_ssl_context *ssl) in ssl_buffer_message() argument
4112 mbedtls_ssl_handshake_params * const hs = ssl->handshake; in ssl_buffer_message()
4120 switch (ssl->in_msgtype) { in ssl_buffer_message()
4130 unsigned recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5]; in ssl_buffer_message()
4132 size_t msg_len = ssl->in_hslen - 12; in ssl_buffer_message()
4136 if (recv_msg_seq < ssl->handshake->in_msg_seq) { in ssl_buffer_message()
4141 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; in ssl_buffer_message()
4147 recv_msg_seq, ssl->handshake->in_msg_seq, in ssl_buffer_message()
4148 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - in ssl_buffer_message()
4164 (ssl_hs_is_proper_fragment(ssl) == 1); in ssl_buffer_message()
4213 if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) { in ssl_buffer_message()
4246 memcpy(hs_buf->data, ssl->in_msg, 6); in ssl_buffer_message()
4255 if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) { in ssl_buffer_message()
4272 frag_off = ssl_get_hs_frag_off(ssl); in ssl_buffer_message()
4273 frag_len = ssl_get_hs_frag_len(ssl); in ssl_buffer_message()
4278 memcpy(msg + frag_off, ssl->in_msg + 12, frag_len); in ssl_buffer_message()
4309 static int ssl_consume_current_message(mbedtls_ssl_context *ssl) in ssl_consume_current_message() argument
4334 if (ssl->in_hslen != 0) { in ssl_consume_current_message()
4338 if (ssl->in_offt != NULL) { in ssl_consume_current_message()
4364 if (ssl->in_hslen < ssl->in_msglen) { in ssl_consume_current_message()
4365 ssl->in_msglen -= ssl->in_hslen; in ssl_consume_current_message()
4366 memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen, in ssl_consume_current_message()
4367 ssl->in_msglen); in ssl_consume_current_message()
4370 ssl->in_msg, ssl->in_msglen); in ssl_consume_current_message()
4372 ssl->in_msglen = 0; in ssl_consume_current_message()
4375 ssl->in_hslen = 0; in ssl_consume_current_message()
4378 else if (ssl->in_offt != NULL) { in ssl_consume_current_message()
4383 ssl->in_msglen = 0; in ssl_consume_current_message()
4390 static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl) in ssl_record_is_in_progress() argument
4392 if (ssl->in_msglen > 0) { in ssl_record_is_in_progress()
4401 static void ssl_free_buffered_record(mbedtls_ssl_context *ssl) in ssl_free_buffered_record() argument
4403 mbedtls_ssl_handshake_params * const hs = ssl->handshake; in ssl_free_buffered_record()
4418 static int ssl_load_buffered_record(mbedtls_ssl_context *ssl) in ssl_load_buffered_record() argument
4420 mbedtls_ssl_handshake_params * const hs = ssl->handshake; in ssl_load_buffered_record()
4425 size_t in_buf_len = ssl->in_buf_len; in ssl_load_buffered_record()
4429 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_load_buffered_record()
4447 if (ssl_next_record_is_in_datagram(ssl) == 1) { in ssl_load_buffered_record()
4453 if (rec_epoch != ssl->in_epoch) { in ssl_load_buffered_record()
4461 if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) { in ssl_load_buffered_record()
4466 memcpy(ssl->in_hdr, rec, rec_len); in ssl_load_buffered_record()
4467 ssl->in_left = rec_len; in ssl_load_buffered_record()
4468 ssl->next_record_offset = 0; in ssl_load_buffered_record()
4470 ssl_free_buffered_record(ssl); in ssl_load_buffered_record()
4478 static int ssl_buffer_future_record(mbedtls_ssl_context *ssl, in ssl_buffer_future_record() argument
4481 mbedtls_ssl_handshake_params * const hs = ssl->handshake; in ssl_buffer_future_record()
4513 ssl->in_epoch + 1U)); in ssl_buffer_future_record()
4518 hs->buffering.future_record.epoch = ssl->in_epoch + 1; in ssl_buffer_future_record()
4538 static int ssl_get_next_record(mbedtls_ssl_context *ssl) in ssl_get_next_record() argument
4550 ret = ssl_load_buffered_record(ssl); in ssl_get_next_record()
4559 ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl)); in ssl_get_next_record()
4565 ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec); in ssl_get_next_record()
4568 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_get_next_record()
4570 ret = ssl_buffer_future_record(ssl, &rec); in ssl_get_next_record()
4584 mbedtls_ssl_update_in_pointers(ssl); in ssl_get_next_record()
4587 ssl->in_msgtype = rec.type; in ssl_get_next_record()
4589 ssl->in_len = ssl->in_cid + rec.cid_len; in ssl_get_next_record()
4591 ssl->in_iv = ssl->in_msg = ssl->in_len + 2; in ssl_get_next_record()
4592 ssl->in_msglen = rec.data_len; in ssl_get_next_record()
4594 ret = ssl_check_client_reconnect(ssl); in ssl_get_next_record()
4602 ssl->next_record_offset = rec.buf_len; in ssl_get_next_record()
4608 ssl->next_record_offset = 0; in ssl_get_next_record()
4609 ssl->in_left = 0; in ssl_get_next_record()
4625 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_get_next_record()
4627 ssl->next_record_offset = rec.buf_len; in ssl_get_next_record()
4628 if (ssl->next_record_offset < ssl->in_left) { in ssl_get_next_record()
4637 ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len); in ssl_get_next_record()
4643 ssl->in_left = 0; in ssl_get_next_record()
4650 if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) { in ssl_get_next_record()
4652 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_get_next_record()
4658 if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || in ssl_get_next_record()
4659 ssl->state == MBEDTLS_SSL_SERVER_FINISHED) { in ssl_get_next_record()
4662 mbedtls_ssl_send_alert_message(ssl, in ssl_get_next_record()
4670 if (ssl->conf->badmac_limit != 0 && in ssl_get_next_record()
4671 ++ssl->badmac_seen >= ssl->conf->badmac_limit) { in ssl_get_next_record()
4679 ssl->next_record_offset = 0; in ssl_get_next_record()
4680 ssl->in_left = 0; in ssl_get_next_record()
4693 mbedtls_ssl_send_alert_message(ssl, in ssl_get_next_record()
4706 mbedtls_ssl_update_in_pointers(ssl); in ssl_get_next_record()
4708 ssl->in_len = ssl->in_cid + rec.cid_len; in ssl_get_next_record()
4710 ssl->in_iv = ssl->in_len + 2; in ssl_get_next_record()
4714 ssl->in_msgtype = rec.type; in ssl_get_next_record()
4719 ssl->in_hdr[0] = rec.type; in ssl_get_next_record()
4720 ssl->in_msg = rec.buf + rec.data_offset; in ssl_get_next_record()
4721 ssl->in_msglen = rec.data_len; in ssl_get_next_record()
4722 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0); in ssl_get_next_record()
4727 int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl) in mbedtls_ssl_handle_message_type() argument
4734 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { in mbedtls_ssl_handle_message_type()
4735 if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) { in mbedtls_ssl_handle_message_type()
4740 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { in mbedtls_ssl_handle_message_type()
4741 if (ssl->in_msglen != 1) { in mbedtls_ssl_handle_message_type()
4743 ssl->in_msglen)); in mbedtls_ssl_handle_message_type()
4747 if (ssl->in_msg[0] != 1) { in mbedtls_ssl_handle_message_type()
4749 ssl->in_msg[0])); in mbedtls_ssl_handle_message_type()
4754 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && in mbedtls_ssl_handle_message_type()
4755 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && in mbedtls_ssl_handle_message_type()
4756 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) { in mbedtls_ssl_handle_message_type()
4757 if (ssl->handshake == NULL) { in mbedtls_ssl_handle_message_type()
4768 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { in mbedtls_ssl_handle_message_type()
4782 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) { in mbedtls_ssl_handle_message_type()
4783 if (ssl->in_msglen != 2) { in mbedtls_ssl_handle_message_type()
4788 ssl->in_msglen)); in mbedtls_ssl_handle_message_type()
4793 ssl->in_msg[0], ssl->in_msg[1])); in mbedtls_ssl_handle_message_type()
4798 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) { in mbedtls_ssl_handle_message_type()
4800 ssl->in_msg[1])); in mbedtls_ssl_handle_message_type()
4804 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && in mbedtls_ssl_handle_message_type()
4805 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) { in mbedtls_ssl_handle_message_type()
4811 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && in mbedtls_ssl_handle_message_type()
4812 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) { in mbedtls_ssl_handle_message_type()
4823 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_handle_message_type()
4826 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && in mbedtls_ssl_handle_message_type()
4827 mbedtls_ssl_is_handshake_over(ssl) == 0 in mbedtls_ssl_handle_message_type()
4829 && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && in mbedtls_ssl_handle_message_type()
4830 ssl->state == MBEDTLS_SSL_SERVER_HELLO) in mbedtls_ssl_handle_message_type()
4837 if (ssl->handshake != NULL && in mbedtls_ssl_handle_message_type()
4838 mbedtls_ssl_is_handshake_over(ssl) == 1) { in mbedtls_ssl_handle_message_type()
4839 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); in mbedtls_ssl_handle_message_type()
4847 int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl) in mbedtls_ssl_send_fatal_handshake_failure() argument
4849 return mbedtls_ssl_send_alert_message(ssl, in mbedtls_ssl_send_fatal_handshake_failure()
4854 int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl, in mbedtls_ssl_send_alert_message() argument
4860 if (ssl == NULL || ssl->conf == NULL) { in mbedtls_ssl_send_alert_message()
4864 if (ssl->out_left != 0) { in mbedtls_ssl_send_alert_message()
4865 return mbedtls_ssl_flush_output(ssl); in mbedtls_ssl_send_alert_message()
4871 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; in mbedtls_ssl_send_alert_message()
4872 ssl->out_msglen = 2; in mbedtls_ssl_send_alert_message()
4873 ssl->out_msg[0] = level; in mbedtls_ssl_send_alert_message()
4874 ssl->out_msg[1] = message; in mbedtls_ssl_send_alert_message()
4876 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { in mbedtls_ssl_send_alert_message()
4885 int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl) in mbedtls_ssl_write_change_cipher_spec() argument
4891 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; in mbedtls_ssl_write_change_cipher_spec()
4892 ssl->out_msglen = 1; in mbedtls_ssl_write_change_cipher_spec()
4893 ssl->out_msg[0] = 1; in mbedtls_ssl_write_change_cipher_spec()
4895 ssl->state++; in mbedtls_ssl_write_change_cipher_spec()
4897 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { in mbedtls_ssl_write_change_cipher_spec()
4907 int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl) in mbedtls_ssl_parse_change_cipher_spec() argument
4913 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { in mbedtls_ssl_parse_change_cipher_spec()
4918 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { in mbedtls_ssl_parse_change_cipher_spec()
4920 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, in mbedtls_ssl_parse_change_cipher_spec()
4934 ssl->transform_in = ssl->transform_negotiate; in mbedtls_ssl_parse_change_cipher_spec()
4936 ssl->session_in = ssl->session_negotiate; in mbedtls_ssl_parse_change_cipher_spec()
4939 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_parse_change_cipher_spec()
4941 mbedtls_ssl_dtls_replay_reset(ssl); in mbedtls_ssl_parse_change_cipher_spec()
4945 if (++ssl->in_epoch == 0) { in mbedtls_ssl_parse_change_cipher_spec()
4953 memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); in mbedtls_ssl_parse_change_cipher_spec()
4955 mbedtls_ssl_update_in_pointers(ssl); in mbedtls_ssl_parse_change_cipher_spec()
4957 ssl->state++; in mbedtls_ssl_parse_change_cipher_spec()
4978 void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl, in mbedtls_ssl_update_out_pointers() argument
4982 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_update_out_pointers()
4983 ssl->out_ctr = ssl->out_hdr + 3; in mbedtls_ssl_update_out_pointers()
4985 ssl->out_cid = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; in mbedtls_ssl_update_out_pointers()
4986 ssl->out_len = ssl->out_cid; in mbedtls_ssl_update_out_pointers()
4988 ssl->out_len += transform->out_cid_len; in mbedtls_ssl_update_out_pointers()
4991 ssl->out_len = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; in mbedtls_ssl_update_out_pointers()
4993 ssl->out_iv = ssl->out_len + 2; in mbedtls_ssl_update_out_pointers()
4997 ssl->out_len = ssl->out_hdr + 3; in mbedtls_ssl_update_out_pointers()
4999 ssl->out_cid = ssl->out_len; in mbedtls_ssl_update_out_pointers()
5001 ssl->out_iv = ssl->out_hdr + 5; in mbedtls_ssl_update_out_pointers()
5004 ssl->out_msg = ssl->out_iv; in mbedtls_ssl_update_out_pointers()
5007 ssl->out_msg += ssl_transform_get_explicit_iv_len(transform); in mbedtls_ssl_update_out_pointers()
5019 void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl) in mbedtls_ssl_update_in_pointers() argument
5032 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_update_in_pointers()
5037 ssl->in_ctr = ssl->in_hdr + 3; in mbedtls_ssl_update_in_pointers()
5039 ssl->in_cid = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; in mbedtls_ssl_update_in_pointers()
5040 ssl->in_len = ssl->in_cid; /* Default: no CID */ in mbedtls_ssl_update_in_pointers()
5042 ssl->in_len = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; in mbedtls_ssl_update_in_pointers()
5044 ssl->in_iv = ssl->in_len + 2; in mbedtls_ssl_update_in_pointers()
5048 ssl->in_ctr = ssl->in_hdr - MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; in mbedtls_ssl_update_in_pointers()
5049 ssl->in_len = ssl->in_hdr + 3; in mbedtls_ssl_update_in_pointers()
5051 ssl->in_cid = ssl->in_len; in mbedtls_ssl_update_in_pointers()
5053 ssl->in_iv = ssl->in_hdr + 5; in mbedtls_ssl_update_in_pointers()
5057 ssl->in_msg = ssl->in_iv; in mbedtls_ssl_update_in_pointers()
5064 void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl) in mbedtls_ssl_reset_in_out_pointers() argument
5068 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_reset_in_out_pointers()
5069 ssl->out_hdr = ssl->out_buf; in mbedtls_ssl_reset_in_out_pointers()
5070 ssl->in_hdr = ssl->in_buf; in mbedtls_ssl_reset_in_out_pointers()
5074 ssl->out_ctr = ssl->out_buf; in mbedtls_ssl_reset_in_out_pointers()
5075 ssl->out_hdr = ssl->out_buf + 8; in mbedtls_ssl_reset_in_out_pointers()
5076 ssl->in_hdr = ssl->in_buf + 8; in mbedtls_ssl_reset_in_out_pointers()
5080 mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */); in mbedtls_ssl_reset_in_out_pointers()
5081 mbedtls_ssl_update_in_pointers(ssl); in mbedtls_ssl_reset_in_out_pointers()
5087 size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl) in mbedtls_ssl_get_bytes_avail() argument
5089 return ssl->in_offt == NULL ? 0 : ssl->in_msglen; in mbedtls_ssl_get_bytes_avail()
5092 int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl) in mbedtls_ssl_check_pending() argument
5099 if (ssl->keep_current_message == 1) { in mbedtls_ssl_check_pending()
5109 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && in mbedtls_ssl_check_pending()
5110 ssl->in_left > ssl->next_record_offset) { in mbedtls_ssl_check_pending()
5120 if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) { in mbedtls_ssl_check_pending()
5129 if (ssl->in_offt != NULL) { in mbedtls_ssl_check_pending()
5145 int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl) in mbedtls_ssl_get_record_expansion() argument
5148 const mbedtls_ssl_transform *transform = ssl->transform_out; in mbedtls_ssl_get_record_expansion()
5155 size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl); in mbedtls_ssl_get_record_expansion()
5243 static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl) in ssl_check_ctr_renegotiate() argument
5245 size_t ep_len = mbedtls_ssl_ep_len(ssl); in ssl_check_ctr_renegotiate()
5249 if (mbedtls_ssl_is_handshake_over(ssl) == 0 || in ssl_check_ctr_renegotiate()
5250 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || in ssl_check_ctr_renegotiate()
5251 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) { in ssl_check_ctr_renegotiate()
5255 in_ctr_cmp = memcmp(ssl->in_ctr + ep_len, in ssl_check_ctr_renegotiate()
5256 &ssl->conf->renego_period[ep_len], in ssl_check_ctr_renegotiate()
5258 out_ctr_cmp = memcmp(&ssl->cur_out_ctr[ep_len], in ssl_check_ctr_renegotiate()
5259 &ssl->conf->renego_period[ep_len], in ssl_check_ctr_renegotiate()
5260 sizeof(ssl->cur_out_ctr) - ep_len); in ssl_check_ctr_renegotiate()
5267 return mbedtls_ssl_renegotiate(ssl); in ssl_check_ctr_renegotiate()
5275 static int ssl_tls13_check_new_session_ticket(mbedtls_ssl_context *ssl) in ssl_tls13_check_new_session_ticket() argument
5278 if ((ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl)) || in ssl_tls13_check_new_session_ticket()
5279 (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET)) { in ssl_tls13_check_new_session_ticket()
5283 ssl->keep_current_message = 1; in ssl_tls13_check_new_session_ticket()
5286 mbedtls_ssl_handshake_set_state(ssl, in ssl_tls13_check_new_session_ticket()
5294 static int ssl_tls13_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl) in ssl_tls13_handle_hs_message_post_handshake() argument
5300 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { in ssl_tls13_handle_hs_message_post_handshake()
5301 int ret = ssl_tls13_check_new_session_ticket(ssl); in ssl_tls13_handle_hs_message_post_handshake()
5324 static int ssl_tls12_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl) in ssl_tls12_handle_hs_message_post_handshake() argument
5335 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && in ssl_tls12_handle_hs_message_post_handshake()
5336 (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || in ssl_tls12_handle_hs_message_post_handshake()
5337 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) { in ssl_tls12_handle_hs_message_post_handshake()
5342 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_tls12_handle_hs_message_post_handshake()
5351 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && in ssl_tls12_handle_hs_message_post_handshake()
5352 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) { in ssl_tls12_handle_hs_message_post_handshake()
5357 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_tls12_handle_hs_message_post_handshake()
5367 if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || in ssl_tls12_handle_hs_message_post_handshake()
5368 (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && in ssl_tls12_handle_hs_message_post_handshake()
5369 ssl->conf->allow_legacy_renegotiation == in ssl_tls12_handle_hs_message_post_handshake()
5377 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && in ssl_tls12_handle_hs_message_post_handshake()
5378 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { in ssl_tls12_handle_hs_message_post_handshake()
5379 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; in ssl_tls12_handle_hs_message_post_handshake()
5382 ret = mbedtls_ssl_start_renegotiation(ssl); in ssl_tls12_handle_hs_message_post_handshake()
5398 if ((ret = mbedtls_ssl_send_alert_message(ssl, in ssl_tls12_handle_hs_message_post_handshake()
5410 static int ssl_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl) in ssl_handle_hs_message_post_handshake() argument
5414 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { in ssl_handle_hs_message_post_handshake()
5415 return ssl_tls13_handle_hs_message_post_handshake(ssl); in ssl_handle_hs_message_post_handshake()
5420 if (ssl->tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) { in ssl_handle_hs_message_post_handshake()
5421 return ssl_tls12_handle_hs_message_post_handshake(ssl); in ssl_handle_hs_message_post_handshake()
5432 int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len) in mbedtls_ssl_read() argument
5437 if (ssl == NULL || ssl->conf == NULL) { in mbedtls_ssl_read()
5444 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in mbedtls_ssl_read()
5445 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { in mbedtls_ssl_read()
5449 if (ssl->handshake != NULL && in mbedtls_ssl_read()
5450 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { in mbedtls_ssl_read()
5451 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { in mbedtls_ssl_read()
5471 ret = ssl_check_ctr_renegotiate(ssl); in mbedtls_ssl_read()
5479 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { in mbedtls_ssl_read()
5480 ret = mbedtls_ssl_handshake(ssl); in mbedtls_ssl_read()
5489 while (ssl->in_offt == NULL) { in mbedtls_ssl_read()
5491 if (ssl->f_get_timer != NULL && in mbedtls_ssl_read()
5492 ssl->f_get_timer(ssl->p_timer) == -1) { in mbedtls_ssl_read()
5493 mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout); in mbedtls_ssl_read()
5496 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { in mbedtls_ssl_read()
5505 if (ssl->in_msglen == 0 && in mbedtls_ssl_read()
5506 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) { in mbedtls_ssl_read()
5510 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { in mbedtls_ssl_read()
5520 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { in mbedtls_ssl_read()
5521 ret = ssl_handle_hs_message_post_handshake(ssl); in mbedtls_ssl_read()
5552 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { in mbedtls_ssl_read()
5553 if (ssl->conf->renego_max_records >= 0) { in mbedtls_ssl_read()
5554 if (++ssl->renego_records_seen > ssl->conf->renego_max_records) { in mbedtls_ssl_read()
5564 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) { in mbedtls_ssl_read()
5569 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) { in mbedtls_ssl_read()
5574 ssl->in_offt = ssl->in_msg; in mbedtls_ssl_read()
5578 if (mbedtls_ssl_is_handshake_over(ssl) == 1) { in mbedtls_ssl_read()
5579 mbedtls_ssl_set_timer(ssl, 0); in mbedtls_ssl_read()
5587 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && in mbedtls_ssl_read()
5588 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { in mbedtls_ssl_read()
5589 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) { in mbedtls_ssl_read()
5599 n = (len < ssl->in_msglen) in mbedtls_ssl_read()
5600 ? len : ssl->in_msglen; in mbedtls_ssl_read()
5603 memcpy(buf, ssl->in_offt, n); in mbedtls_ssl_read()
5604 ssl->in_msglen -= n; in mbedtls_ssl_read()
5609 mbedtls_platform_zeroize(ssl->in_offt, n); in mbedtls_ssl_read()
5611 if (ssl->in_msglen == 0) { in mbedtls_ssl_read()
5613 ssl->in_offt = NULL; in mbedtls_ssl_read()
5614 ssl->keep_current_message = 0; in mbedtls_ssl_read()
5617 ssl->in_offt += n; in mbedtls_ssl_read()
5638 static int ssl_write_real(mbedtls_ssl_context *ssl, in ssl_write_real() argument
5641 int ret = mbedtls_ssl_get_max_out_record_payload(ssl); in ssl_write_real()
5651 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { in ssl_write_real()
5662 if (ssl->out_left != 0) { in ssl_write_real()
5669 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { in ssl_write_real()
5679 ssl->out_msglen = len; in ssl_write_real()
5680 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; in ssl_write_real()
5682 memcpy(ssl->out_msg, buf, len); in ssl_write_real()
5685 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { in ssl_write_real()
5697 int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len) in mbedtls_ssl_write() argument
5703 if (ssl == NULL || ssl->conf == NULL) { in mbedtls_ssl_write()
5708 if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) { in mbedtls_ssl_write()
5714 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { in mbedtls_ssl_write()
5715 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { in mbedtls_ssl_write()
5721 ret = ssl_write_real(ssl, buf, len); in mbedtls_ssl_write()
5731 int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl) in mbedtls_ssl_close_notify() argument
5735 if (ssl == NULL || ssl->conf == NULL) { in mbedtls_ssl_close_notify()
5741 if (mbedtls_ssl_is_handshake_over(ssl) == 1) { in mbedtls_ssl_close_notify()
5742 if ((ret = mbedtls_ssl_send_alert_message(ssl, in mbedtls_ssl_close_notify()
5782 void mbedtls_ssl_set_inbound_transform(mbedtls_ssl_context *ssl, in mbedtls_ssl_set_inbound_transform() argument
5785 ssl->transform_in = transform; in mbedtls_ssl_set_inbound_transform()
5786 memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); in mbedtls_ssl_set_inbound_transform()
5789 void mbedtls_ssl_set_outbound_transform(mbedtls_ssl_context *ssl, in mbedtls_ssl_set_outbound_transform() argument
5792 ssl->transform_out = transform; in mbedtls_ssl_set_outbound_transform()
5793 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); in mbedtls_ssl_set_outbound_transform()
5798 void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl) in mbedtls_ssl_buffering_free() argument
5801 mbedtls_ssl_handshake_params * const hs = ssl->handshake; in mbedtls_ssl_buffering_free()
5807 ssl_free_buffered_record(ssl); in mbedtls_ssl_buffering_free()
5810 ssl_buffering_free_slot(ssl, offset); in mbedtls_ssl_buffering_free()
5814 static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl, in ssl_buffering_free_slot() argument
5817 mbedtls_ssl_handshake_params * const hs = ssl->handshake; in ssl_buffering_free_slot()
5878 int mbedtls_ssl_handle_pending_alert(mbedtls_ssl_context *ssl) in mbedtls_ssl_handle_pending_alert() argument
5883 if (ssl->send_alert == 0) { in mbedtls_ssl_handle_pending_alert()
5887 ret = mbedtls_ssl_send_alert_message(ssl, in mbedtls_ssl_handle_pending_alert()
5889 ssl->alert_type); in mbedtls_ssl_handle_pending_alert()
5895 ssl->send_alert = 0; in mbedtls_ssl_handle_pending_alert()
5902 return ssl->alert_reason; in mbedtls_ssl_handle_pending_alert()
5908 void mbedtls_ssl_pend_fatal_alert(mbedtls_ssl_context *ssl, in mbedtls_ssl_pend_fatal_alert() argument
5912 ssl->send_alert = 1; in mbedtls_ssl_pend_fatal_alert()
5913 ssl->alert_type = alert_type; in mbedtls_ssl_pend_fatal_alert()
5914 ssl->alert_reason = alert_reason; in mbedtls_ssl_pend_fatal_alert()