1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "tls/s2n_prf.h"
#include "tls/s2n_tls13_handshake.h"
#include "utils/s2n_result.h"
/* The state machine refers to the "master" secret as the "application" secret.
* Let's use that terminology here to match.
*/
#define S2N_APPLICATION_SECRET S2N_MASTER_SECRET
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A
*# The notation "K_{send,recv} = foo" means "set
*# the send/recv key to the given key".
*/
#define K_send(conn, secret_type) RESULT_GUARD(s2n_set_key(conn, secret_type, (conn)->mode))
#define K_recv(conn, secret_type) RESULT_GUARD(s2n_set_key(conn, secret_type, S2N_PEER_MODE((conn)->mode)))
static const struct s2n_blob s2n_zero_length_context = { 0 };
static S2N_RESULT s2n_zero_sequence_number(struct s2n_connection *conn, s2n_mode mode)
{
RESULT_ENSURE_REF(conn);
RESULT_ENSURE_REF(conn->secure);
struct s2n_blob sequence_number = { 0 };
RESULT_GUARD(s2n_connection_get_sequence_number(conn, mode, &sequence_number));
RESULT_GUARD_POSIX(s2n_blob_zero(&sequence_number));
return S2N_RESULT_OK;
}
static S2N_RESULT s2n_tls13_key_schedule_get_keying_material(
struct s2n_connection *conn, s2n_extract_secret_type_t secret_type,
s2n_mode mode, struct s2n_blob *iv, struct s2n_blob *key)
{
RESULT_ENSURE_REF(conn);
RESULT_ENSURE_REF(iv);
RESULT_ENSURE_REF(key);
RESULT_ENSURE_REF(conn->secure);
const struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite;
RESULT_ENSURE_REF(cipher_suite);
const struct s2n_cipher *cipher = NULL;
RESULT_GUARD(s2n_connection_get_secure_cipher(conn, &cipher));
RESULT_ENSURE_REF(cipher);
/**
*= https://tools.ietf.org/rfc/rfc8446#section-7.3
*# The traffic keying material is generated from the following input
*# values:
*#
*# - A secret value
**/
struct s2n_blob secret = { 0 };
uint8_t secret_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
RESULT_GUARD_POSIX(s2n_blob_init(&secret, secret_bytes, S2N_TLS13_SECRET_MAX_LEN));
RESULT_GUARD(s2n_tls13_secrets_get(conn, secret_type, mode, &secret));
/**
*= https://tools.ietf.org/rfc/rfc8446#section-7.3
*#
*# - A purpose value indicating the specific value being generated
**/
const struct s2n_blob *key_purpose = &s2n_tls13_label_traffic_secret_key;
const struct s2n_blob *iv_purpose = &s2n_tls13_label_traffic_secret_iv;
/**
*= https://tools.ietf.org/rfc/rfc8446#section-7.3
*#
*# - The length of the key being generated
**/
const uint32_t key_size = cipher->key_material_size;
const uint32_t iv_size = S2N_TLS13_FIXED_IV_LEN;
/*
* TODO: We should be able to reuse the prf_work_space rather
* than allocating a new HMAC every time.
* https://github.com/aws/s2n-tls/issues/3206
*/
s2n_hmac_algorithm hmac_alg = cipher_suite->prf_alg;
DEFER_CLEANUP(struct s2n_hmac_state hmac = { 0 }, s2n_hmac_free);
RESULT_GUARD_POSIX(s2n_hmac_new(&hmac));
/**
*= https://tools.ietf.org/rfc/rfc8446#section-7.3
*#
*# The traffic keying material is generated from an input traffic secret
*# value using:
*#
*# [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
**/
RESULT_ENSURE_LTE(key_size, key->size);
key->size = key_size;
RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&hmac, hmac_alg,
&secret, key_purpose, &s2n_zero_length_context, key));
/**
*= https://tools.ietf.org/rfc/rfc8446#section-7.3
*# [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)
**/
RESULT_ENSURE_LTE(iv_size, iv->size);
iv->size = iv_size;
RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&hmac, hmac_alg,
&secret, iv_purpose, &s2n_zero_length_context, iv));
return S2N_RESULT_OK;
}
static S2N_RESULT s2n_set_key(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type, s2n_mode mode)
{
RESULT_ENSURE_REF(conn);
RESULT_ENSURE_REF(conn->secure);
uint8_t *implicit_iv_data = NULL;
struct s2n_session_key *session_key = NULL;
uint8_t key_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
if (mode == S2N_CLIENT) {
implicit_iv_data = conn->secure->client_implicit_iv;
session_key = &conn->secure->client_key;
conn->client = conn->secure;
} else {
implicit_iv_data = conn->secure->server_implicit_iv;
session_key = &conn->secure->server_key;
conn->server = conn->secure;
}
struct s2n_blob iv = { 0 };
struct s2n_blob key = { 0 };
RESULT_GUARD_POSIX(s2n_blob_init(&iv, implicit_iv_data, S2N_TLS13_FIXED_IV_LEN));
RESULT_GUARD_POSIX(s2n_blob_init(&key, key_bytes, sizeof(key_bytes)));
RESULT_GUARD(s2n_tls13_key_schedule_get_keying_material(
conn, secret_type, mode, &iv, &key));
const struct s2n_cipher *cipher = NULL;
RESULT_GUARD(s2n_connection_get_secure_cipher(conn, &cipher));
RESULT_ENSURE_REF(cipher);
bool is_sending_secret = (mode == conn->mode);
if (is_sending_secret) {
RESULT_GUARD_POSIX(cipher->set_encryption_key(session_key, &key));
} else {
RESULT_GUARD_POSIX(cipher->set_decryption_key(session_key, &key));
}
/**
*= https://tools.ietf.org/rfc/rfc8446#section-5.3
*# Each sequence number is
*# set to zero at the beginning of a connection and whenever the key is
*# changed; the first record transmitted under a particular traffic key
*# MUST use sequence number 0.
*/
RESULT_GUARD(s2n_zero_sequence_number(conn, mode));
return S2N_RESULT_OK;
}
static S2N_RESULT s2n_client_key_schedule(struct s2n_connection *conn)
{
RESULT_ENSURE_REF(conn);
message_type_t message_type = s2n_conn_get_current_message_type(conn);
/**
* How client keys are set varies depending on early data state.
*
*= https://tools.ietf.org/rfc/rfc8446#appendix-A
*# Actions which are taken only in certain circumstances
*# are indicated in [].
*/
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.1
*# START <----+
*# Send ClientHello | | Recv HelloRetryRequest
*# [K_send = early data] | |
*/
if (message_type == CLIENT_HELLO
&& conn->early_data_state == S2N_EARLY_DATA_REQUESTED) {
K_send(conn, S2N_EARLY_SECRET);
}
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.1
*# v |
*# / WAIT_SH ----+
*# | | Recv ServerHello
*# | | K_recv = handshake
*/
if (message_type == SERVER_HELLO) {
K_recv(conn, S2N_HANDSHAKE_SECRET);
}
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.1
*# Can | V
*# send | WAIT_EE
*# early | | Recv EncryptedExtensions
*# data | +--------+--------+
*# | Using | | Using certificate
*# | PSK | v
*# | | WAIT_CERT_CR
*# | | Recv | | Recv CertificateRequest
*# | | Certificate | v
*# | | | WAIT_CERT
*# | | | | Recv Certificate
*# | | v v
*# | | WAIT_CV
*# | | | Recv CertificateVerify
*# | +> WAIT_FINISHED <+
*# | | Recv Finished
*# \ | [Send EndOfEarlyData]
*# | K_send = handshake
*/
if ((message_type == SERVER_FINISHED && !WITH_EARLY_DATA(conn))
|| (message_type == END_OF_EARLY_DATA)) {
K_send(conn, S2N_HANDSHAKE_SECRET);
}
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.1
*# | [Send Certificate [+ CertificateVerify]]
*# Can send | Send Finished
*# app data --> | K_send = K_recv = application
*/
if (message_type == CLIENT_FINISHED) {
K_send(conn, S2N_APPLICATION_SECRET);
K_recv(conn, S2N_APPLICATION_SECRET);
}
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.1
*# after here v
*# CONNECTED
*/
return S2N_RESULT_OK;
}
static S2N_RESULT s2n_server_key_schedule(struct s2n_connection *conn)
{
RESULT_ENSURE_REF(conn);
message_type_t message_type = s2n_conn_get_current_message_type(conn);
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
*# START <-----+
*# Recv ClientHello | | Send HelloRetryRequest
*# v |
*# RECVD_CH ----+
*# | Select parameters
*# v
*# NEGOTIATED
*# | Send ServerHello
*# | K_send = handshake
*/
if (message_type == SERVER_HELLO) {
K_send(conn, S2N_HANDSHAKE_SECRET);
}
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
*# | Send EncryptedExtensions
*# | [Send CertificateRequest]
*# Can send | [Send Certificate + CertificateVerify]
*# app data | Send Finished
*# after --> | K_send = application
*/
if (message_type == SERVER_FINISHED) {
K_send(conn, S2N_APPLICATION_SECRET);
/* clang-format off */
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
*# here +--------+--------+
*# No 0-RTT | | 0-RTT
*# | |
*# K_recv = handshake | | K_recv = early data
*/
/* clang-format on */
if (WITH_EARLY_DATA(conn)) {
K_recv(conn, S2N_EARLY_SECRET);
} else {
K_recv(conn, S2N_HANDSHAKE_SECRET);
}
}
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
*# [Skip decrypt errors] | +------> WAIT_EOED -+
*# | | Recv | | Recv EndOfEarlyData
*# | | early data | | K_recv = handshake
*# | +------------+ |
*/
if (message_type == END_OF_EARLY_DATA) {
K_recv(conn, S2N_HANDSHAKE_SECRET);
}
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
*# | |
*# +> WAIT_FLIGHT2 <--------+
*# |
*# +--------+--------+
*# No auth | | Client auth
*# | |
*# | v
*# | WAIT_CERT
*# | Recv | | Recv Certificate
*# | empty | v
*# | Certificate | WAIT_CV
*# | | | Recv
*# | v | CertificateVerify
*# +-> WAIT_FINISHED <---+
*# | Recv Finished
*# | K_recv = application
*/
if (message_type == CLIENT_FINISHED) {
K_recv(conn, S2N_APPLICATION_SECRET);
}
/**
*= https://tools.ietf.org/rfc/rfc8446#appendix-A.2
*# v
*# CONNECTED
*/
return S2N_RESULT_OK;
}
s2n_result (*key_schedules[])(struct s2n_connection *) = {
[S2N_CLIENT] = &s2n_client_key_schedule,
[S2N_SERVER] = &s2n_server_key_schedule,
};
S2N_RESULT s2n_tls13_key_schedule_update(struct s2n_connection *conn)
{
RESULT_ENSURE_REF(conn);
if (s2n_connection_get_protocol_version(conn) < S2N_TLS13) {
return S2N_RESULT_OK;
}
RESULT_ENSURE_REF(key_schedules[conn->mode]);
RESULT_GUARD(key_schedules[conn->mode](conn));
return S2N_RESULT_OK;
}
S2N_RESULT s2n_tls13_key_schedule_reset(struct s2n_connection *conn)
{
RESULT_ENSURE_REF(conn);
RESULT_ENSURE_REF(conn->initial);
conn->client = conn->initial;
conn->server = conn->initial;
conn->secrets.extract_secret_type = S2N_NONE_SECRET;
return S2N_RESULT_OK;
}
S2N_RESULT s2n_tls13_key_schedule_generate_key_material(struct s2n_connection *conn,
s2n_mode sender, struct s2n_key_material *key_material)
{
RESULT_GUARD(s2n_key_material_init(key_material, conn));
if (sender == S2N_CLIENT) {
RESULT_GUARD(s2n_tls13_key_schedule_get_keying_material(conn, S2N_MASTER_SECRET,
sender, &key_material->client_iv, &key_material->client_key));
} else {
RESULT_GUARD(s2n_tls13_key_schedule_get_keying_material(conn, S2N_MASTER_SECRET,
sender, &key_material->server_iv, &key_material->server_key));
}
return S2N_RESULT_OK;
}
|