| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/extensions/s2n_server_renegotiation_info.h" |
| 17 |
|
| 18 |
#include "error/s2n_errno.h" |
| 19 |
#include "stuffer/s2n_stuffer.h" |
| 20 |
#include "tls/s2n_connection.h" |
| 21 |
#include "tls/s2n_tls.h" |
| 22 |
#include "tls/s2n_tls_parameters.h" |
| 23 |
#include "utils/s2n_safety.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
static bool s2n_renegotiation_info_should_send(struct s2n_connection *conn); |
| 37 |
static int s2n_renegotiation_info_send(struct s2n_connection *conn, struct s2n_stuffer *out); |
| 38 |
static int s2n_renegotiation_info_recv(struct s2n_connection *conn, struct s2n_stuffer *extension); |
| 39 |
static int s2n_renegotiation_info_if_missing(struct s2n_connection *conn); |
| 40 |
|
| 41 |
const s2n_extension_type s2n_server_renegotiation_info_extension = { |
| 42 |
.iana_value = TLS_EXTENSION_RENEGOTIATION_INFO, |
| 43 |
.send = s2n_renegotiation_info_send, |
| 44 |
.recv = s2n_renegotiation_info_recv, |
| 45 |
.should_send = s2n_renegotiation_info_should_send, |
| 46 |
.if_missing = s2n_renegotiation_info_if_missing, |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
.is_response = false, |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static bool s2n_renegotiation_info_should_send(struct s2n_connection *conn) |
| 71 |
{ |
| 72 |
return conn && conn->secure_renegotiation && s2n_connection_get_protocol_version(conn) < S2N_TLS13; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static int s2n_renegotiation_info_send_initial(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 82 |
{ |
| 83 |
POSIX_GUARD(s2n_stuffer_write_uint8(out, 0)); |
| 84 |
return S2N_SUCCESS; |
| 85 |
} |
| 86 |
|
| 87 |
static int s2n_renegotiation_info_send_renegotiation(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 88 |
{ |
| 89 |
POSIX_ENSURE_REF(conn); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
POSIX_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
POSIX_ENSURE(conn->secure_renegotiation, S2N_ERR_NO_RENEGOTIATION); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
const uint8_t verify_data_len = conn->handshake.finished_len; |
| 110 |
POSIX_ENSURE_GT(verify_data_len, 0); |
| 111 |
POSIX_GUARD(s2n_stuffer_write_uint8(out, verify_data_len * 2)); |
| 112 |
POSIX_GUARD(s2n_stuffer_write_bytes(out, conn->handshake.client_finished, verify_data_len)); |
| 113 |
POSIX_GUARD(s2n_stuffer_write_bytes(out, conn->handshake.server_finished, verify_data_len)); |
| 114 |
|
| 115 |
return S2N_SUCCESS; |
| 116 |
} |
| 117 |
|
| 118 |
static int s2n_renegotiation_info_send(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 119 |
{ |
| 120 |
if (s2n_handshake_is_renegotiation(conn)) { |
| 121 |
POSIX_GUARD(s2n_renegotiation_info_send_renegotiation(conn, out)); |
| 122 |
} else { |
| 123 |
POSIX_GUARD(s2n_renegotiation_info_send_initial(conn, out)); |
| 124 |
} |
| 125 |
return S2N_SUCCESS; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
static int s2n_renegotiation_info_recv_initial(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 134 |
{ |
| 135 |
POSIX_ENSURE_REF(conn); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
uint8_t renegotiated_connection_len = 0; |
| 144 |
POSIX_GUARD(s2n_stuffer_read_uint8(extension, &renegotiated_connection_len)); |
| 145 |
POSIX_ENSURE(s2n_stuffer_data_available(extension) == 0, S2N_ERR_NON_EMPTY_RENEGOTIATION_INFO); |
| 146 |
POSIX_ENSURE(renegotiated_connection_len == 0, S2N_ERR_NON_EMPTY_RENEGOTIATION_INFO); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
conn->secure_renegotiation = 1; |
| 153 |
return S2N_SUCCESS; |
| 154 |
} |
| 155 |
|
| 156 |
static int s2n_renegotiation_info_recv_renegotiation(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 157 |
{ |
| 158 |
POSIX_ENSURE_REF(conn); |
| 159 |
uint8_t verify_data_len = conn->handshake.finished_len; |
| 160 |
POSIX_ENSURE_GT(verify_data_len, 0); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
POSIX_ENSURE(conn->secure_renegotiation, S2N_ERR_NO_RENEGOTIATION); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
uint8_t renegotiated_connection_len = 0; |
| 179 |
POSIX_GUARD(s2n_stuffer_read_uint8(extension, &renegotiated_connection_len)); |
| 180 |
POSIX_ENSURE(verify_data_len * 2 == renegotiated_connection_len, S2N_ERR_BAD_MESSAGE); |
| 181 |
|
| 182 |
uint8_t *first_half = s2n_stuffer_raw_read(extension, verify_data_len); |
| 183 |
POSIX_ENSURE_REF(first_half); |
| 184 |
POSIX_ENSURE(s2n_constant_time_equals(first_half, conn->handshake.client_finished, verify_data_len), |
| 185 |
S2N_ERR_BAD_MESSAGE); |
| 186 |
|
| 187 |
uint8_t *second_half = s2n_stuffer_raw_read(extension, verify_data_len); |
| 188 |
POSIX_ENSURE_REF(second_half); |
| 189 |
POSIX_ENSURE(s2n_constant_time_equals(second_half, conn->handshake.server_finished, verify_data_len), |
| 190 |
S2N_ERR_BAD_MESSAGE); |
| 191 |
|
| 192 |
return S2N_SUCCESS; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
static int s2n_renegotiation_info_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 204 |
{ |
| 205 |
if (s2n_handshake_is_renegotiation(conn)) { |
| 206 |
POSIX_GUARD(s2n_renegotiation_info_recv_renegotiation(conn, extension)); |
| 207 |
} else { |
| 208 |
POSIX_GUARD(s2n_renegotiation_info_recv_initial(conn, extension)); |
| 209 |
} |
| 210 |
return S2N_SUCCESS; |
| 211 |
} |
| 212 |
|
| 213 |
static int s2n_renegotiation_info_if_missing(struct s2n_connection *conn) |
| 214 |
{ |
| 215 |
POSIX_ENSURE_REF(conn); |
| 216 |
if (s2n_handshake_is_renegotiation(conn)) { |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
POSIX_BAIL(S2N_ERR_NO_RENEGOTIATION); |
| 224 |
} else { |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
conn->secure_renegotiation = false; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
return S2N_SUCCESS; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
int s2n_recv_server_renegotiation_info_ext(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 256 |
{ |
| 257 |
return s2n_extension_recv(&s2n_server_renegotiation_info_extension, conn, extension); |
| 258 |
} |