| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_prf.h" |
| 17 |
#include "tls/s2n_tls13_handshake.h" |
| 18 |
#include "utils/s2n_result.h" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#define S2N_APPLICATION_SECRET S2N_MASTER_SECRET |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#define K_send(conn, secret_type) RESULT_GUARD(s2n_tls13_key_schedule_set_key(conn, secret_type, (conn)->mode)) |
| 31 |
#define K_recv(conn, secret_type) RESULT_GUARD(s2n_tls13_key_schedule_set_key(conn, secret_type, S2N_PEER_MODE((conn)->mode))) |
| 32 |
|
| 33 |
static const struct s2n_blob s2n_zero_length_context = { 0 }; |
| 34 |
|
| 35 |
static S2N_RESULT s2n_zero_sequence_number(struct s2n_connection *conn, s2n_mode mode) |
| 36 |
{ |
| 37 |
RESULT_ENSURE_REF(conn); |
| 38 |
RESULT_ENSURE_REF(conn->secure); |
| 39 |
struct s2n_blob sequence_number = { 0 }; |
| 40 |
RESULT_GUARD(s2n_connection_get_sequence_number(conn, mode, &sequence_number)); |
| 41 |
RESULT_GUARD_POSIX(s2n_blob_zero(&sequence_number)); |
| 42 |
return S2N_RESULT_OK; |
| 43 |
} |
| 44 |
|
| 45 |
static S2N_RESULT s2n_tls13_key_schedule_get_keying_material( |
| 46 |
struct s2n_connection *conn, s2n_extract_secret_type_t secret_type, |
| 47 |
s2n_mode mode, struct s2n_blob *iv, struct s2n_blob *key) |
| 48 |
{ |
| 49 |
RESULT_ENSURE_REF(conn); |
| 50 |
RESULT_ENSURE_REF(iv); |
| 51 |
RESULT_ENSURE_REF(key); |
| 52 |
RESULT_ENSURE_REF(conn->secure); |
| 53 |
|
| 54 |
const struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite; |
| 55 |
RESULT_ENSURE_REF(cipher_suite); |
| 56 |
|
| 57 |
const struct s2n_cipher *cipher = NULL; |
| 58 |
RESULT_GUARD(s2n_connection_get_secure_cipher(conn, &cipher)); |
| 59 |
RESULT_ENSURE_REF(cipher); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
struct s2n_blob secret = { 0 }; |
| 69 |
uint8_t secret_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 }; |
| 70 |
RESULT_GUARD_POSIX(s2n_blob_init(&secret, secret_bytes, S2N_TLS13_SECRET_MAX_LEN)); |
| 71 |
RESULT_GUARD(s2n_tls13_secrets_get(conn, secret_type, mode, &secret)); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
const struct s2n_blob *key_purpose = &s2n_tls13_label_traffic_secret_key; |
| 79 |
const struct s2n_blob *iv_purpose = &s2n_tls13_label_traffic_secret_iv; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
const uint32_t key_size = cipher->key_material_size; |
| 87 |
const uint32_t iv_size = S2N_TLS13_FIXED_IV_LEN; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
s2n_hmac_algorithm hmac_alg = cipher_suite->prf_alg; |
| 95 |
DEFER_CLEANUP(struct s2n_hmac_state hmac = { 0 }, s2n_hmac_free); |
| 96 |
RESULT_GUARD_POSIX(s2n_hmac_new(&hmac)); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
RESULT_ENSURE_LTE(key_size, key->size); |
| 107 |
key->size = key_size; |
| 108 |
RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&hmac, hmac_alg, |
| 109 |
&secret, key_purpose, &s2n_zero_length_context, key)); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
RESULT_ENSURE_LTE(iv_size, iv->size); |
| 115 |
iv->size = iv_size; |
| 116 |
RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&hmac, hmac_alg, |
| 117 |
&secret, iv_purpose, &s2n_zero_length_context, iv)); |
| 118 |
|
| 119 |
return S2N_RESULT_OK; |
| 120 |
} |
| 121 |
|
| 122 |
S2N_RESULT s2n_tls13_key_schedule_set_key(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type, s2n_mode mode) |
| 123 |
{ |
| 124 |
RESULT_ENSURE_REF(conn); |
| 125 |
RESULT_ENSURE_REF(conn->secure); |
| 126 |
|
| 127 |
uint8_t *implicit_iv_data = NULL; |
| 128 |
struct s2n_session_key *session_key = NULL; |
| 129 |
uint8_t key_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 }; |
| 130 |
if (mode == S2N_CLIENT) { |
| 131 |
implicit_iv_data = conn->secure->client_implicit_iv; |
| 132 |
session_key = &conn->secure->client_key; |
| 133 |
conn->client = conn->secure; |
| 134 |
} else { |
| 135 |
implicit_iv_data = conn->secure->server_implicit_iv; |
| 136 |
session_key = &conn->secure->server_key; |
| 137 |
conn->server = conn->secure; |
| 138 |
} |
| 139 |
|
| 140 |
struct s2n_blob iv = { 0 }; |
| 141 |
struct s2n_blob key = { 0 }; |
| 142 |
RESULT_GUARD_POSIX(s2n_blob_init(&iv, implicit_iv_data, S2N_TLS13_FIXED_IV_LEN)); |
| 143 |
RESULT_GUARD_POSIX(s2n_blob_init(&key, key_bytes, sizeof(key_bytes))); |
| 144 |
RESULT_GUARD(s2n_tls13_key_schedule_get_keying_material( |
| 145 |
conn, secret_type, mode, &iv, &key)); |
| 146 |
|
| 147 |
const struct s2n_cipher *cipher = NULL; |
| 148 |
RESULT_GUARD(s2n_connection_get_secure_cipher(conn, &cipher)); |
| 149 |
RESULT_ENSURE_REF(cipher); |
| 150 |
|
| 151 |
bool is_sending_secret = (mode == conn->mode); |
| 152 |
if (is_sending_secret) { |
| 153 |
RESULT_GUARD(cipher->set_encryption_key(session_key, &key)); |
| 154 |
} else { |
| 155 |
RESULT_GUARD(cipher->set_decryption_key(session_key, &key)); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
RESULT_GUARD(s2n_zero_sequence_number(conn, mode)); |
| 166 |
|
| 167 |
return S2N_RESULT_OK; |
| 168 |
} |
| 169 |
|
| 170 |
static S2N_RESULT s2n_client_key_schedule(struct s2n_connection *conn) |
| 171 |
{ |
| 172 |
RESULT_ENSURE_REF(conn); |
| 173 |
|
| 174 |
message_type_t message_type = s2n_conn_get_current_message_type(conn); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
if (message_type == CLIENT_HELLO |
| 191 |
&& conn->early_data_state == S2N_EARLY_DATA_REQUESTED) { |
| 192 |
K_send(conn, S2N_EARLY_SECRET); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
if (message_type == SERVER_HELLO) { |
| 202 |
K_recv(conn, S2N_HANDSHAKE_SECRET); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if ((message_type == SERVER_FINISHED && !WITH_EARLY_DATA(conn)) |
| 226 |
|| (message_type == END_OF_EARLY_DATA)) { |
| 227 |
K_send(conn, S2N_HANDSHAKE_SECRET); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
if (message_type == CLIENT_FINISHED) { |
| 236 |
K_send(conn, S2N_APPLICATION_SECRET); |
| 237 |
K_recv(conn, S2N_APPLICATION_SECRET); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
return S2N_RESULT_OK; |
| 245 |
} |
| 246 |
|
| 247 |
static S2N_RESULT s2n_server_key_schedule(struct s2n_connection *conn) |
| 248 |
{ |
| 249 |
RESULT_ENSURE_REF(conn); |
| 250 |
|
| 251 |
message_type_t message_type = s2n_conn_get_current_message_type(conn); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
if (message_type == SERVER_HELLO) { |
| 266 |
K_send(conn, S2N_HANDSHAKE_SECRET); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
if (message_type == SERVER_FINISHED) { |
| 277 |
K_send(conn, S2N_APPLICATION_SECRET); |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
if (WITH_EARLY_DATA(conn)) { |
| 288 |
K_recv(conn, S2N_EARLY_SECRET); |
| 289 |
} else { |
| 290 |
K_recv(conn, S2N_HANDSHAKE_SECRET); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
if (message_type == END_OF_EARLY_DATA) { |
| 301 |
K_recv(conn, S2N_HANDSHAKE_SECRET); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
if (message_type == CLIENT_FINISHED) { |
| 323 |
K_recv(conn, S2N_APPLICATION_SECRET); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
return S2N_RESULT_OK; |
| 331 |
} |
| 332 |
|
| 333 |
s2n_result (*key_schedules[])(struct s2n_connection *) = { |
| 334 |
[S2N_CLIENT] = &s2n_client_key_schedule, |
| 335 |
[S2N_SERVER] = &s2n_server_key_schedule, |
| 336 |
}; |
| 337 |
|
| 338 |
S2N_RESULT s2n_tls13_key_schedule_update(struct s2n_connection *conn) |
| 339 |
{ |
| 340 |
RESULT_ENSURE_REF(conn); |
| 341 |
if (s2n_connection_get_protocol_version(conn) < S2N_TLS13) { |
| 342 |
return S2N_RESULT_OK; |
| 343 |
} |
| 344 |
RESULT_ENSURE_REF(key_schedules[conn->mode]); |
| 345 |
RESULT_GUARD(key_schedules[conn->mode](conn)); |
| 346 |
return S2N_RESULT_OK; |
| 347 |
} |
| 348 |
|
| 349 |
S2N_RESULT s2n_tls13_key_schedule_reset(struct s2n_connection *conn) |
| 350 |
{ |
| 351 |
RESULT_ENSURE_REF(conn); |
| 352 |
RESULT_ENSURE_REF(conn->initial); |
| 353 |
conn->client = conn->initial; |
| 354 |
conn->server = conn->initial; |
| 355 |
conn->secrets.extract_secret_type = S2N_NONE_SECRET; |
| 356 |
return S2N_RESULT_OK; |
| 357 |
} |
| 358 |
|
| 359 |
S2N_RESULT s2n_tls13_key_schedule_generate_key_material(struct s2n_connection *conn, |
| 360 |
s2n_mode sender, struct s2n_key_material *key_material) |
| 361 |
{ |
| 362 |
RESULT_GUARD(s2n_key_material_init(key_material, conn)); |
| 363 |
if (sender == S2N_CLIENT) { |
| 364 |
RESULT_GUARD(s2n_tls13_key_schedule_get_keying_material(conn, S2N_MASTER_SECRET, |
| 365 |
sender, &key_material->client_iv, &key_material->client_key)); |
| 366 |
} else { |
| 367 |
RESULT_GUARD(s2n_tls13_key_schedule_get_keying_material(conn, S2N_MASTER_SECRET, |
| 368 |
sender, &key_material->server_iv, &key_material->server_key)); |
| 369 |
} |
| 370 |
return S2N_RESULT_OK; |
| 371 |
} |