| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "api/s2n.h" |
| 17 |
#include "tls/s2n_alerts.h" |
| 18 |
#include "tls/s2n_connection.h" |
| 19 |
#include "tls/s2n_tls.h" |
| 20 |
#include "utils/s2n_atomic.h" |
| 21 |
#include "utils/s2n_safety.h" |
| 22 |
|
| 23 |
static bool s2n_shutdown_expect_close_notify(struct s2n_connection *conn) |
| 24 |
{ |
| 25 |
|
| 26 |
if (s2n_atomic_flag_test(&conn->error_alert_received)) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
if (conn->writer_alert_out || conn->reader_alert_out) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
if (!s2n_handshake_is_complete(conn)) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
if (conn->quic_enabled) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
if (conn->delay) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
int s2n_shutdown_send(struct s2n_connection *conn, s2n_blocked_status *blocked) |
| 63 |
{ |
| 64 |
POSIX_ENSURE_REF(conn); |
| 65 |
POSIX_ENSURE_REF(blocked); |
| 66 |
*blocked = S2N_NOT_BLOCKED; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
if (conn->send == NULL && conn->recv == NULL) { |
| 73 |
return S2N_SUCCESS; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
s2n_atomic_flag_set(&conn->write_closed); |
| 78 |
POSIX_GUARD(s2n_flush(conn, blocked)); |
| 79 |
|
| 80 |
|
| 81 |
if (s2n_atomic_flag_test(&conn->error_alert_received)) { |
| 82 |
return S2N_SUCCESS; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
if (conn->alert_sent) { |
| 87 |
return S2N_SUCCESS; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
uint64_t elapsed = 0; |
| 95 |
POSIX_GUARD_RESULT(s2n_timer_elapsed(conn->config, &conn->write_timer, &elapsed)); |
| 96 |
S2N_ERROR_IF(elapsed < conn->delay, S2N_ERR_SHUTDOWN_PAUSED); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
POSIX_GUARD_RESULT(s2n_alerts_write_error_or_close_notify(conn)); |
| 104 |
POSIX_GUARD(s2n_flush(conn, blocked)); |
| 105 |
return S2N_SUCCESS; |
| 106 |
} |
| 107 |
|
| 108 |
int s2n_shutdown(struct s2n_connection *conn, s2n_blocked_status *blocked) |
| 109 |
{ |
| 110 |
POSIX_ENSURE_REF(conn); |
| 111 |
POSIX_ENSURE_REF(blocked); |
| 112 |
*blocked = S2N_NOT_BLOCKED; |
| 113 |
|
| 114 |
|
| 115 |
POSIX_GUARD(s2n_shutdown_send(conn, blocked)); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if (!s2n_shutdown_expect_close_notify(conn)) { |
| 121 |
POSIX_GUARD_RESULT(s2n_connection_set_closed(conn)); |
| 122 |
*blocked = S2N_NOT_BLOCKED; |
| 123 |
return S2N_SUCCESS; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
uint8_t record_type = 0; |
| 128 |
int isSSLv2 = false; |
| 129 |
*blocked = S2N_BLOCKED_ON_READ; |
| 130 |
while (!s2n_atomic_flag_test(&conn->close_notify_received)) { |
| 131 |
POSIX_GUARD(s2n_read_full_record(conn, &record_type, &isSSLv2)); |
| 132 |
POSIX_ENSURE(!isSSLv2, S2N_ERR_BAD_MESSAGE); |
| 133 |
if (record_type == TLS_ALERT) { |
| 134 |
POSIX_GUARD(s2n_process_alert_fragment(conn)); |
| 135 |
} |
| 136 |
POSIX_GUARD_RESULT(s2n_record_wipe(conn)); |
| 137 |
} |
| 138 |
|
| 139 |
*blocked = S2N_NOT_BLOCKED; |
| 140 |
return S2N_SUCCESS; |
| 141 |
} |