/* * 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 #include #include #include #include "error/s2n_errno.h" #include "tls/s2n_cipher_suites.h" #include "tls/s2n_connection.h" #include "tls/s2n_prf.h" #include "stuffer/s2n_stuffer.h" #include "crypto/s2n_hmac.h" #include "crypto/s2n_hash.h" #include "crypto/s2n_openssl.h" #include "crypto/s2n_fips.h" #include "utils/s2n_safety.h" #include "utils/s2n_blob.h" #include "utils/s2n_mem.h" static int s2n_sslv3_prf(struct s2n_prf_working_space *ws, struct s2n_blob *secret, struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out) { struct s2n_hash_state *md5 = &ws->ssl3.md5; struct s2n_hash_state *sha1 = &ws->ssl3.sha1; uint32_t outputlen = out->size; uint8_t *output = out->data; uint8_t iteration = 1; uint8_t A = 'A'; while (outputlen) { POSIX_GUARD(s2n_hash_reset(sha1)); for (int i = 0; i < iteration; i++) { POSIX_GUARD(s2n_hash_update(sha1, &A, 1)); } POSIX_GUARD(s2n_hash_update(sha1, secret->data, secret->size)); POSIX_GUARD(s2n_hash_update(sha1, seed_a->data, seed_a->size)); if (seed_b) { POSIX_GUARD(s2n_hash_update(sha1, seed_b->data, seed_b->size)); if (seed_c) { POSIX_GUARD(s2n_hash_update(sha1, seed_c->data, seed_c->size)); } } POSIX_GUARD(s2n_hash_digest(sha1, ws->ssl3.sha1_digest, sizeof(ws->ssl3.sha1_digest))); POSIX_GUARD(s2n_hash_reset(md5)); POSIX_GUARD(s2n_hash_update(md5, secret->data, secret->size)); POSIX_GUARD(s2n_hash_update(md5, ws->ssl3.sha1_digest, sizeof(ws->ssl3.sha1_digest))); POSIX_GUARD(s2n_hash_digest(md5, ws->ssl3.md5_digest, sizeof(ws->ssl3.md5_digest))); uint32_t bytes_to_copy = MIN(outputlen, sizeof(ws->ssl3.md5_digest)); POSIX_CHECKED_MEMCPY(output, ws->ssl3.md5_digest, bytes_to_copy); outputlen -= bytes_to_copy; output += bytes_to_copy; /* Increment the letter */ A++; iteration++; } POSIX_GUARD(s2n_hash_reset(md5)); POSIX_GUARD(s2n_hash_reset(sha1)); return 0; } #if !defined(OPENSSL_IS_BORINGSSL) && !defined(OPENSSL_IS_AWSLC) static int s2n_evp_hmac_p_hash_new(struct s2n_prf_working_space *ws) { POSIX_ENSURE_REF(ws->tls.p_hash.evp_hmac.evp_digest.ctx = S2N_EVP_MD_CTX_NEW()); return 0; } static int s2n_evp_hmac_p_hash_digest_init(struct s2n_prf_working_space *ws) { POSIX_ENSURE_REF(ws->tls.p_hash.evp_hmac.evp_digest.md); POSIX_ENSURE_REF(ws->tls.p_hash.evp_hmac.evp_digest.ctx); POSIX_ENSURE_REF(ws->tls.p_hash.evp_hmac.mac_key); /* Ignore the MD5 check when in FIPS mode to comply with the TLS 1.0 RFC */ if (s2n_is_in_fips_mode()) { POSIX_GUARD(s2n_digest_allow_md5_for_fips(&ws->tls.p_hash.evp_hmac.evp_digest)); } POSIX_GUARD_OSSL(EVP_DigestSignInit(ws->tls.p_hash.evp_hmac.evp_digest.ctx, NULL, ws->tls.p_hash.evp_hmac.evp_digest.md, NULL, ws->tls.p_hash.evp_hmac.mac_key), S2N_ERR_P_HASH_INIT_FAILED); return 0; } static int s2n_evp_hmac_p_hash_init(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret) { /* Initialize the message digest */ switch (alg) { case S2N_HMAC_SSLv3_MD5: case S2N_HMAC_MD5: ws->tls.p_hash.evp_hmac.evp_digest.md = EVP_md5(); break; case S2N_HMAC_SSLv3_SHA1: case S2N_HMAC_SHA1: ws->tls.p_hash.evp_hmac.evp_digest.md = EVP_sha1(); break; case S2N_HMAC_SHA224: ws->tls.p_hash.evp_hmac.evp_digest.md = EVP_sha224(); break; case S2N_HMAC_SHA256: ws->tls.p_hash.evp_hmac.evp_digest.md = EVP_sha256(); break; case S2N_HMAC_SHA384: ws->tls.p_hash.evp_hmac.evp_digest.md = EVP_sha384(); break; case S2N_HMAC_SHA512: ws->tls.p_hash.evp_hmac.evp_digest.md = EVP_sha512(); break; default: POSIX_BAIL(S2N_ERR_P_HASH_INVALID_ALGORITHM); } /* Initialize the mac key using the provided secret */ POSIX_ENSURE_REF(ws->tls.p_hash.evp_hmac.mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret->data, secret->size)); /* Initialize the message digest context with the above message digest and mac key */ return s2n_evp_hmac_p_hash_digest_init(ws); } static int s2n_evp_hmac_p_hash_update(struct s2n_prf_working_space *ws, const void *data, uint32_t size) { POSIX_GUARD_OSSL(EVP_DigestSignUpdate(ws->tls.p_hash.evp_hmac.evp_digest.ctx, data, (size_t)size), S2N_ERR_P_HASH_UPDATE_FAILED); return 0; } static int s2n_evp_hmac_p_hash_digest(struct s2n_prf_working_space *ws, void *digest, uint32_t size) { /* EVP_DigestSign API's require size_t data structures */ size_t digest_size = size; POSIX_GUARD_OSSL(EVP_DigestSignFinal(ws->tls.p_hash.evp_hmac.evp_digest.ctx, (unsigned char *)digest, &digest_size), S2N_ERR_P_HASH_FINAL_FAILED); return 0; } static int s2n_evp_hmac_p_hash_wipe(struct s2n_prf_working_space *ws) { POSIX_GUARD_OSSL(S2N_EVP_MD_CTX_RESET(ws->tls.p_hash.evp_hmac.evp_digest.ctx), S2N_ERR_P_HASH_WIPE_FAILED); return 0; } static int s2n_evp_hmac_p_hash_reset(struct s2n_prf_working_space *ws) { POSIX_GUARD(s2n_evp_hmac_p_hash_wipe(ws)); return s2n_evp_hmac_p_hash_digest_init(ws); } static int s2n_evp_hmac_p_hash_cleanup(struct s2n_prf_working_space *ws) { /* Prepare the workspace md_ctx for the next p_hash */ POSIX_GUARD(s2n_evp_hmac_p_hash_wipe(ws)); /* Free mac key - PKEYs cannot be reused */ POSIX_ENSURE_REF(ws->tls.p_hash.evp_hmac.mac_key); EVP_PKEY_free(ws->tls.p_hash.evp_hmac.mac_key); ws->tls.p_hash.evp_hmac.mac_key = NULL; return 0; } static int s2n_evp_hmac_p_hash_free(struct s2n_prf_working_space *ws) { POSIX_ENSURE_REF(ws->tls.p_hash.evp_hmac.evp_digest.ctx); S2N_EVP_MD_CTX_FREE(ws->tls.p_hash.evp_hmac.evp_digest.ctx); ws->tls.p_hash.evp_hmac.evp_digest.ctx = NULL; return 0; } static const struct s2n_p_hash_hmac s2n_evp_hmac = { .alloc = &s2n_evp_hmac_p_hash_new, .init = &s2n_evp_hmac_p_hash_init, .update = &s2n_evp_hmac_p_hash_update, .final = &s2n_evp_hmac_p_hash_digest, .reset = &s2n_evp_hmac_p_hash_reset, .cleanup = &s2n_evp_hmac_p_hash_cleanup, .free = &s2n_evp_hmac_p_hash_free, }; #endif /* !defined(OPENSSL_IS_BORINGSSL) && !defined(OPENSSL_IS_AWSLC) */ static int s2n_hmac_p_hash_new(struct s2n_prf_working_space *ws) { POSIX_GUARD(s2n_hmac_new(&ws->tls.p_hash.s2n_hmac)); return s2n_hmac_init(&ws->tls.p_hash.s2n_hmac, S2N_HMAC_NONE, NULL, 0); } static int s2n_hmac_p_hash_init(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret) { return s2n_hmac_init(&ws->tls.p_hash.s2n_hmac, alg, secret->data, secret->size); } static int s2n_hmac_p_hash_update(struct s2n_prf_working_space *ws, const void *data, uint32_t size) { return s2n_hmac_update(&ws->tls.p_hash.s2n_hmac, data, size); } static int s2n_hmac_p_hash_digest(struct s2n_prf_working_space *ws, void *digest, uint32_t size) { return s2n_hmac_digest(&ws->tls.p_hash.s2n_hmac, digest, size); } static int s2n_hmac_p_hash_reset(struct s2n_prf_working_space *ws) { return s2n_hmac_reset(&ws->tls.p_hash.s2n_hmac); } static int s2n_hmac_p_hash_cleanup(struct s2n_prf_working_space *ws) { return s2n_hmac_p_hash_reset(ws); } static int s2n_hmac_p_hash_free(struct s2n_prf_working_space *ws) { return s2n_hmac_free(&ws->tls.p_hash.s2n_hmac); } static const struct s2n_p_hash_hmac s2n_hmac = { .alloc = &s2n_hmac_p_hash_new, .init = &s2n_hmac_p_hash_init, .update = &s2n_hmac_p_hash_update, .final = &s2n_hmac_p_hash_digest, .reset = &s2n_hmac_p_hash_reset, .cleanup = &s2n_hmac_p_hash_cleanup, .free = &s2n_hmac_p_hash_free, }; static int s2n_p_hash(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret, struct s2n_blob *label, struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out) { uint8_t digest_size; POSIX_GUARD(s2n_hmac_digest_size(alg, &digest_size)); const struct s2n_p_hash_hmac *hmac = ws->tls.p_hash_hmac_impl; /* First compute hmac(secret + A(0)) */ POSIX_GUARD(hmac->init(ws, alg, secret)); POSIX_GUARD(hmac->update(ws, label->data, label->size)); POSIX_GUARD(hmac->update(ws, seed_a->data, seed_a->size)); if (seed_b) { POSIX_GUARD(hmac->update(ws, seed_b->data, seed_b->size)); if (seed_c) { POSIX_GUARD(hmac->update(ws, seed_c->data, seed_c->size)); } } POSIX_GUARD(hmac->final(ws, ws->tls.digest0, digest_size)); uint32_t outputlen = out->size; uint8_t *output = out->data; while (outputlen) { /* Now compute hmac(secret + A(N - 1) + seed) */ POSIX_GUARD(hmac->reset(ws)); POSIX_GUARD(hmac->update(ws, ws->tls.digest0, digest_size)); /* Add the label + seed and compute this round's A */ POSIX_GUARD(hmac->update(ws, label->data, label->size)); POSIX_GUARD(hmac->update(ws, seed_a->data, seed_a->size)); if (seed_b) { POSIX_GUARD(hmac->update(ws, seed_b->data, seed_b->size)); if (seed_c) { POSIX_GUARD(hmac->update(ws, seed_c->data, seed_c->size)); } } POSIX_GUARD(hmac->final(ws, ws->tls.digest1, digest_size)); uint32_t bytes_to_xor = MIN(outputlen, digest_size); for (uint32_t i = 0; i < bytes_to_xor; i++) { *output ^= ws->tls.digest1[i]; output++; outputlen--; } /* Stash a digest of A(N), in A(N), for the next round */ POSIX_GUARD(hmac->reset(ws)); POSIX_GUARD(hmac->update(ws, ws->tls.digest0, digest_size)); POSIX_GUARD(hmac->final(ws, ws->tls.digest0, digest_size)); } POSIX_GUARD(hmac->cleanup(ws)); return 0; } const struct s2n_p_hash_hmac *s2n_get_hmac_implementation() { #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC) return &s2n_hmac; #else return s2n_is_in_fips_mode() ? &s2n_evp_hmac : &s2n_hmac; #endif } int s2n_prf_new(struct s2n_connection *conn) { /* Set p_hash_hmac_impl on initial prf creation. * When in FIPS mode, the EVP API's must be used for the p_hash HMAC. */ conn->prf_space.tls.p_hash_hmac_impl = s2n_get_hmac_implementation(); return conn->prf_space.tls.p_hash_hmac_impl->alloc(&conn->prf_space); } int s2n_prf_free(struct s2n_connection *conn) { /* Ensure that p_hash_hmac_impl is set, as it may have been reset for prf_space on s2n_connection_wipe. * When in FIPS mode, the EVP API's must be used for the p_hash HMAC. */ conn->prf_space.tls.p_hash_hmac_impl = s2n_get_hmac_implementation(); return conn->prf_space.tls.p_hash_hmac_impl->free(&conn->prf_space); } static int s2n_prf(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *label, struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out) { /* seed_a is always required, seed_b is optional, if seed_c is provided seed_b must also be provided */ S2N_ERROR_IF(seed_a == NULL, S2N_ERR_PRF_INVALID_SEED); S2N_ERROR_IF(seed_b == NULL && seed_c != NULL, S2N_ERR_PRF_INVALID_SEED); if (conn->actual_protocol_version == S2N_SSLv3) { return s2n_sslv3_prf(&conn->prf_space, secret, seed_a, seed_b, seed_c, out); } /* We zero the out blob because p_hash works by XOR'ing with the existing * buffer. This is a little convoluted but means we can avoid dynamic memory * allocation. When we call p_hash once (in the TLS1.2 case) it will produce * the right values. When we call it twice in the regular case, the two * outputs will be XORd just ass the TLS 1.0 and 1.1 RFCs require. */ POSIX_GUARD(s2n_blob_zero(out)); /* Ensure that p_hash_hmac_impl is set, as it may have been reset for prf_space on s2n_connection_wipe. * When in FIPS mode, the EVP API's must be used for the p_hash HMAC. */ conn->prf_space.tls.p_hash_hmac_impl = s2n_get_hmac_implementation(); if (conn->actual_protocol_version == S2N_TLS12) { return s2n_p_hash(&conn->prf_space, conn->secure.cipher_suite->prf_alg, secret, label, seed_a, seed_b, seed_c, out); } struct s2n_blob half_secret = {.data = secret->data,.size = (secret->size + 1) / 2 }; POSIX_GUARD(s2n_p_hash(&conn->prf_space, S2N_HMAC_MD5, &half_secret, label, seed_a, seed_b, seed_c, out)); half_secret.data += secret->size - half_secret.size; POSIX_GUARD(s2n_p_hash(&conn->prf_space, S2N_HMAC_SHA1, &half_secret, label, seed_a, seed_b, seed_c, out)); return 0; } int s2n_tls_prf_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret) { struct s2n_blob client_random = {.size = sizeof(conn->secrets.client_random), .data = conn->secrets.client_random}; struct s2n_blob server_random = {.size = sizeof(conn->secrets.server_random), .data = conn->secrets.server_random}; struct s2n_blob master_secret = {.size = sizeof(conn->secrets.master_secret), .data = conn->secrets.master_secret}; uint8_t master_secret_label[] = "master secret"; struct s2n_blob label = {.size = sizeof(master_secret_label) - 1, .data = master_secret_label}; return s2n_prf(conn, premaster_secret, &label, &client_random, &server_random, NULL, &master_secret); } int s2n_hybrid_prf_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret) { struct s2n_blob client_random = {.size = sizeof(conn->secrets.client_random), .data = conn->secrets.client_random}; struct s2n_blob server_random = {.size = sizeof(conn->secrets.server_random), .data = conn->secrets.server_random}; struct s2n_blob master_secret = {.size = sizeof(conn->secrets.master_secret), .data = conn->secrets.master_secret}; uint8_t master_secret_label[] = "hybrid master secret"; struct s2n_blob label = {.size = sizeof(master_secret_label) - 1, .data = master_secret_label}; return s2n_prf(conn, premaster_secret, &label, &client_random, &server_random, &conn->kex_params.client_key_exchange_message, &master_secret); } /** *= https://tools.ietf.org/rfc/rfc7627#section-4 *# When the extended master secret extension is negotiated in a full *# handshake, the "master_secret" is computed as *# *# master_secret = PRF(pre_master_secret, "extended master secret", *# session_hash) *# [0..47]; */ S2N_RESULT s2n_tls_prf_extended_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret, struct s2n_blob *session_hash) { struct s2n_blob extended_master_secret = {.size = sizeof(conn->secrets.master_secret), .data = conn->secrets.master_secret}; uint8_t extended_master_secret_label[] = "extended master secret"; /* Subtract one from the label size to remove the "\0" */ struct s2n_blob label = {.size = sizeof(extended_master_secret_label) - 1, .data = extended_master_secret_label}; RESULT_GUARD_POSIX(s2n_prf(conn, premaster_secret, &label, session_hash, NULL, NULL, &extended_master_secret)); return S2N_RESULT_OK; } static int s2n_sslv3_finished(struct s2n_connection *conn, uint8_t prefix[4], struct s2n_hash_state *hash_workspace, uint8_t * out) { uint8_t xorpad1[48] = { 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36 }; uint8_t xorpad2[48] = { 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c }; uint8_t *md5_digest = out; uint8_t *sha_digest = out + MD5_DIGEST_LENGTH; POSIX_ENSURE_LTE(MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, sizeof(conn->handshake.client_finished)); struct s2n_hash_state *md5 = hash_workspace; POSIX_GUARD(s2n_hash_copy(md5, &conn->handshake.md5)); POSIX_GUARD(s2n_hash_update(md5, prefix, 4)); POSIX_GUARD(s2n_hash_update(md5, conn->secrets.master_secret, sizeof(conn->secrets.master_secret))); POSIX_GUARD(s2n_hash_update(md5, xorpad1, 48)); POSIX_GUARD(s2n_hash_digest(md5, md5_digest, MD5_DIGEST_LENGTH)); POSIX_GUARD(s2n_hash_reset(md5)); POSIX_GUARD(s2n_hash_update(md5, conn->secrets.master_secret, sizeof(conn->secrets.master_secret))); POSIX_GUARD(s2n_hash_update(md5, xorpad2, 48)); POSIX_GUARD(s2n_hash_update(md5, md5_digest, MD5_DIGEST_LENGTH)); POSIX_GUARD(s2n_hash_digest(md5, md5_digest, MD5_DIGEST_LENGTH)); POSIX_GUARD(s2n_hash_reset(md5)); struct s2n_hash_state *sha1 = hash_workspace; POSIX_GUARD(s2n_hash_copy(sha1, &conn->handshake.sha1)); POSIX_GUARD(s2n_hash_update(sha1, prefix, 4)); POSIX_GUARD(s2n_hash_update(sha1, conn->secrets.master_secret, sizeof(conn->secrets.master_secret))); POSIX_GUARD(s2n_hash_update(sha1, xorpad1, 40)); POSIX_GUARD(s2n_hash_digest(sha1, sha_digest, SHA_DIGEST_LENGTH)); POSIX_GUARD(s2n_hash_reset(sha1)); POSIX_GUARD(s2n_hash_update(sha1, conn->secrets.master_secret, sizeof(conn->secrets.master_secret))); POSIX_GUARD(s2n_hash_update(sha1, xorpad2, 40)); POSIX_GUARD(s2n_hash_update(sha1, sha_digest, SHA_DIGEST_LENGTH)); POSIX_GUARD(s2n_hash_digest(sha1, sha_digest, SHA_DIGEST_LENGTH)); POSIX_GUARD(s2n_hash_reset(sha1)); return 0; } static int s2n_sslv3_client_finished(struct s2n_connection *conn) { uint8_t prefix[4] = { 0x43, 0x4c, 0x4e, 0x54 }; POSIX_ENSURE_LTE(MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, sizeof(conn->handshake.client_finished)); return s2n_sslv3_finished(conn, prefix, &conn->hash_workspace, conn->handshake.client_finished); } static int s2n_sslv3_server_finished(struct s2n_connection *conn) { uint8_t prefix[4] = { 0x53, 0x52, 0x56, 0x52 }; POSIX_ENSURE_LTE(MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, sizeof(conn->handshake.server_finished)); return s2n_sslv3_finished(conn, prefix, &conn->hash_workspace, conn->handshake.server_finished); } int s2n_prf_client_finished(struct s2n_connection *conn) { struct s2n_blob master_secret, md5, sha; uint8_t md5_digest[MD5_DIGEST_LENGTH]; uint8_t sha_digest[SHA384_DIGEST_LENGTH]; uint8_t client_finished_label[] = "client finished"; struct s2n_blob client_finished = {0}; struct s2n_blob label = {0}; if (conn->actual_protocol_version == S2N_SSLv3) { return s2n_sslv3_client_finished(conn); } client_finished.data = conn->handshake.client_finished; client_finished.size = S2N_TLS_FINISHED_LEN; label.data = client_finished_label; label.size = sizeof(client_finished_label) - 1; master_secret.data = conn->secrets.master_secret; master_secret.size = sizeof(conn->secrets.master_secret); if (conn->actual_protocol_version == S2N_TLS12) { switch (conn->secure.cipher_suite->prf_alg) { case S2N_HMAC_SHA256: POSIX_GUARD(s2n_hash_copy(&conn->hash_workspace, &conn->handshake.sha256)); POSIX_GUARD(s2n_hash_digest(&conn->hash_workspace, sha_digest, SHA256_DIGEST_LENGTH)); sha.size = SHA256_DIGEST_LENGTH; break; case S2N_HMAC_SHA384: POSIX_GUARD(s2n_hash_copy(&conn->hash_workspace, &conn->handshake.sha384)); POSIX_GUARD(s2n_hash_digest(&conn->hash_workspace, sha_digest, SHA384_DIGEST_LENGTH)); sha.size = SHA384_DIGEST_LENGTH; break; default: POSIX_BAIL(S2N_ERR_PRF_INVALID_ALGORITHM); } sha.data = sha_digest; return s2n_prf(conn, &master_secret, &label, &sha, NULL, NULL, &client_finished); } POSIX_GUARD(s2n_hash_copy(&conn->hash_workspace, &conn->handshake.md5)); POSIX_GUARD(s2n_hash_digest(&conn->hash_workspace, md5_digest, MD5_DIGEST_LENGTH)); md5.data = md5_digest; md5.size = MD5_DIGEST_LENGTH; POSIX_GUARD(s2n_hash_copy(&conn->hash_workspace, &conn->handshake.sha1)); POSIX_GUARD(s2n_hash_digest(&conn->hash_workspace, sha_digest, SHA_DIGEST_LENGTH)); sha.data = sha_digest; sha.size = SHA_DIGEST_LENGTH; return s2n_prf(conn, &master_secret, &label, &md5, &sha, NULL, &client_finished); } int s2n_prf_server_finished(struct s2n_connection *conn) { struct s2n_blob master_secret, md5, sha; uint8_t md5_digest[MD5_DIGEST_LENGTH]; uint8_t sha_digest[SHA384_DIGEST_LENGTH]; uint8_t server_finished_label[] = "server finished"; struct s2n_blob server_finished = {0}; struct s2n_blob label = {0}; if (conn->actual_protocol_version == S2N_SSLv3) { return s2n_sslv3_server_finished(conn); } server_finished.data = conn->handshake.server_finished; server_finished.size = S2N_TLS_FINISHED_LEN; label.data = server_finished_label; label.size = sizeof(server_finished_label) - 1; master_secret.data = conn->secrets.master_secret; master_secret.size = sizeof(conn->secrets.master_secret); if (conn->actual_protocol_version == S2N_TLS12) { switch (conn->secure.cipher_suite->prf_alg) { case S2N_HMAC_SHA256: POSIX_GUARD(s2n_hash_copy(&conn->hash_workspace, &conn->handshake.sha256)); POSIX_GUARD(s2n_hash_digest(&conn->hash_workspace, sha_digest, SHA256_DIGEST_LENGTH)); sha.size = SHA256_DIGEST_LENGTH; break; case S2N_HMAC_SHA384: POSIX_GUARD(s2n_hash_copy(&conn->hash_workspace, &conn->handshake.sha384)); POSIX_GUARD(s2n_hash_digest(&conn->hash_workspace, sha_digest, SHA384_DIGEST_LENGTH)); sha.size = SHA384_DIGEST_LENGTH; break; default: POSIX_BAIL(S2N_ERR_PRF_INVALID_ALGORITHM); } sha.data = sha_digest; return s2n_prf(conn, &master_secret, &label, &sha, NULL, NULL, &server_finished); } POSIX_GUARD(s2n_hash_copy(&conn->hash_workspace, &conn->handshake.md5)); POSIX_GUARD(s2n_hash_digest(&conn->hash_workspace, md5_digest, MD5_DIGEST_LENGTH)); md5.data = md5_digest; md5.size = MD5_DIGEST_LENGTH; POSIX_GUARD(s2n_hash_copy(&conn->hash_workspace, &conn->handshake.sha1)); POSIX_GUARD(s2n_hash_digest(&conn->hash_workspace, sha_digest, SHA_DIGEST_LENGTH)); sha.data = sha_digest; sha.size = SHA_DIGEST_LENGTH; return s2n_prf(conn, &master_secret, &label, &md5, &sha, NULL, &server_finished); } static int s2n_prf_make_client_key(struct s2n_connection *conn, struct s2n_stuffer *key_material) { struct s2n_blob client_key = {0}; client_key.size = conn->secure.cipher_suite->record_alg->cipher->key_material_size; client_key.data = s2n_stuffer_raw_read(key_material, client_key.size); POSIX_ENSURE_REF(client_key.data); if (conn->mode == S2N_CLIENT) { POSIX_GUARD(conn->secure.cipher_suite->record_alg->cipher->set_encryption_key(&conn->secure.client_key, &client_key)); } else { POSIX_GUARD(conn->secure.cipher_suite->record_alg->cipher->set_decryption_key(&conn->secure.client_key, &client_key)); } return 0; } static int s2n_prf_make_server_key(struct s2n_connection *conn, struct s2n_stuffer *key_material) { struct s2n_blob server_key = {0}; server_key.size = conn->secure.cipher_suite->record_alg->cipher->key_material_size; server_key.data = s2n_stuffer_raw_read(key_material, server_key.size); POSIX_ENSURE_REF(server_key.data); if (conn->mode == S2N_SERVER) { POSIX_GUARD(conn->secure.cipher_suite->record_alg->cipher->set_encryption_key(&conn->secure.server_key, &server_key)); } else { POSIX_GUARD(conn->secure.cipher_suite->record_alg->cipher->set_decryption_key(&conn->secure.server_key, &server_key)); } return 0; } int s2n_prf_key_expansion(struct s2n_connection *conn) { struct s2n_blob client_random = {.data = conn->secrets.client_random,.size = sizeof(conn->secrets.client_random) }; struct s2n_blob server_random = {.data = conn->secrets.server_random,.size = sizeof(conn->secrets.server_random) }; struct s2n_blob master_secret = {.data = conn->secrets.master_secret,.size = sizeof(conn->secrets.master_secret) }; struct s2n_blob label, out; uint8_t key_expansion_label[] = "key expansion"; uint8_t key_block[S2N_MAX_KEY_BLOCK_LEN]; label.data = key_expansion_label; label.size = sizeof(key_expansion_label) - 1; POSIX_GUARD(s2n_blob_init(&out, key_block, sizeof(key_block))); struct s2n_stuffer key_material = {0}; POSIX_GUARD(s2n_prf(conn, &master_secret, &label, &server_random, &client_random, NULL, &out)); POSIX_GUARD(s2n_stuffer_init(&key_material, &out)); POSIX_GUARD(s2n_stuffer_write(&key_material, &out)); POSIX_ENSURE(conn->secure.cipher_suite->available, S2N_ERR_PRF_INVALID_ALGORITHM); POSIX_GUARD(conn->secure.cipher_suite->record_alg->cipher->init(&conn->secure.client_key)); POSIX_GUARD(conn->secure.cipher_suite->record_alg->cipher->init(&conn->secure.server_key)); /* Check that we have a valid MAC and key size */ uint8_t mac_size; if (conn->secure.cipher_suite->record_alg->cipher->type == S2N_COMPOSITE) { mac_size = conn->secure.cipher_suite->record_alg->cipher->io.comp.mac_key_size; } else { POSIX_GUARD(s2n_hmac_digest_size(conn->secure.cipher_suite->record_alg->hmac_alg, &mac_size)); } /* Seed the client MAC */ uint8_t *client_mac_write_key = s2n_stuffer_raw_read(&key_material, mac_size); POSIX_ENSURE_REF(client_mac_write_key); POSIX_GUARD(s2n_hmac_reset(&conn->secure.client_record_mac)); POSIX_GUARD(s2n_hmac_init(&conn->secure.client_record_mac, conn->secure.cipher_suite->record_alg->hmac_alg, client_mac_write_key, mac_size)); /* Seed the server MAC */ uint8_t *server_mac_write_key = s2n_stuffer_raw_read(&key_material, mac_size); POSIX_ENSURE_REF(server_mac_write_key); POSIX_GUARD(s2n_hmac_reset(&conn->secure.server_record_mac)); POSIX_GUARD(s2n_hmac_init(&conn->secure.server_record_mac, conn->secure.cipher_suite->record_alg->hmac_alg, server_mac_write_key, mac_size)); /* Make the client key */ POSIX_GUARD(s2n_prf_make_client_key(conn, &key_material)); /* Make the server key */ POSIX_GUARD(s2n_prf_make_server_key(conn, &key_material)); /* Composite CBC does MAC inside the cipher, pass it the MAC key. * Must happen after setting encryption/decryption keys. */ if (conn->secure.cipher_suite->record_alg->cipher->type == S2N_COMPOSITE) { POSIX_GUARD(conn->secure.cipher_suite->record_alg->cipher->io.comp.set_mac_write_key(&conn->secure.server_key, server_mac_write_key, mac_size)); POSIX_GUARD(conn->secure.cipher_suite->record_alg->cipher->io.comp.set_mac_write_key(&conn->secure.client_key, client_mac_write_key, mac_size)); } /* TLS >= 1.1 has no implicit IVs for non AEAD ciphers */ if (conn->actual_protocol_version > S2N_TLS10 && conn->secure.cipher_suite->record_alg->cipher->type != S2N_AEAD) { return 0; } uint32_t implicit_iv_size = 0; switch (conn->secure.cipher_suite->record_alg->cipher->type) { case S2N_AEAD: implicit_iv_size = conn->secure.cipher_suite->record_alg->cipher->io.aead.fixed_iv_size; break; case S2N_CBC: implicit_iv_size = conn->secure.cipher_suite->record_alg->cipher->io.cbc.block_size; break; case S2N_COMPOSITE: implicit_iv_size = conn->secure.cipher_suite->record_alg->cipher->io.comp.block_size; break; /* No-op for stream ciphers */ default: break; } struct s2n_blob client_implicit_iv = {.data = conn->secure.client_implicit_iv,.size = implicit_iv_size }; struct s2n_blob server_implicit_iv = {.data = conn->secure.server_implicit_iv,.size = implicit_iv_size }; POSIX_GUARD(s2n_stuffer_read(&key_material, &client_implicit_iv)); POSIX_GUARD(s2n_stuffer_read(&key_material, &server_implicit_iv)); return 0; }