| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright 2005 Nokia. All rights reserved. |
| 4 |
// |
| 5 |
// The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 6 |
// Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 7 |
// support (see RFC 4279) to OpenSSL. |
| 8 |
// |
| 9 |
// SPDX-License-Identifier: Apache-2.0 |
| 10 |
|
| 11 |
#include <openssl/ssl.h> |
| 12 |
|
| 13 |
#include <assert.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include <utility> |
| 17 |
|
| 18 |
#include <openssl/err.h> |
| 19 |
#include <openssl/evp.h> |
| 20 |
#include <openssl/hmac.h> |
| 21 |
#include <openssl/kdf.h> |
| 22 |
#include <openssl/md5.h> |
| 23 |
#include <openssl/mem.h> |
| 24 |
#include <openssl/nid.h> |
| 25 |
#include <openssl/rand.h> |
| 26 |
|
| 27 |
#include "../crypto/internal.h" |
| 28 |
#include "internal.h" |
| 29 |
|
| 30 |
|
| 31 |
BSSL_NAMESPACE_BEGIN |
| 32 |
|
| 33 |
bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out, |
| 34 |
Span<const uint8_t> secret, Span<const char> label, |
| 35 |
Span<const uint8_t> seed1, Span<const uint8_t> seed2) { |
| 36 |
return 1 == CRYPTO_tls1_prf(digest, out.data(), out.size(), secret.data(), |
| 37 |
secret.size(), label.data(), label.size(), |
| 38 |
seed1.data(), seed1.size(), seed2.data(), |
| 39 |
seed2.size()); |
| 40 |
} |
| 41 |
|
| 42 |
static bool get_key_block_lengths(const SSL *ssl, size_t *out_mac_secret_len, |
| 43 |
size_t *out_key_len, size_t *out_iv_len, |
| 44 |
const SSL_CIPHER *cipher) { |
| 45 |
const EVP_AEAD *aead = NULL; |
| 46 |
if (!ssl_cipher_get_evp_aead(&aead, out_mac_secret_len, out_iv_len, cipher, |
| 47 |
ssl_protocol_version(ssl), SSL_is_dtls(ssl))) { |
| 48 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
*out_key_len = EVP_AEAD_key_length(aead); |
| 53 |
if (*out_mac_secret_len > 0) { |
| 54 |
// For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the |
| 55 |
// key length reported by |EVP_AEAD_key_length| will include the MAC key |
| 56 |
// bytes and initial implicit IV. |
| 57 |
if (*out_key_len < *out_mac_secret_len + *out_iv_len) { |
| 58 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 59 |
return false; |
| 60 |
} |
| 61 |
*out_key_len -= *out_mac_secret_len + *out_iv_len; |
| 62 |
} |
| 63 |
|
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
static bool generate_key_block(const SSL *ssl, Span<uint8_t> out, |
| 68 |
const SSL_SESSION *session) { |
| 69 |
auto secret = MakeConstSpan(session->secret, session->secret_length); |
| 70 |
static const char kLabel[] = "key expansion"; |
| 71 |
auto label = MakeConstSpan(kLabel, sizeof(kLabel) - 1); |
| 72 |
|
| 73 |
const EVP_MD *digest = ssl_session_get_digest(session); |
| 74 |
// Note this function assumes that |session|'s key material corresponds to |
| 75 |
// |ssl->s3->client_random| and |ssl->s3->server_random|. |
| 76 |
return tls1_prf(digest, out, secret, label, ssl->s3->server_random, |
| 77 |
ssl->s3->client_random); |
| 78 |
} |
| 79 |
|
| 80 |
bool tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction, |
| 81 |
Array<uint8_t> *key_block_cache, |
| 82 |
const SSL_SESSION *session, |
| 83 |
Span<const uint8_t> iv_override) { |
| 84 |
size_t mac_secret_len, key_len, iv_len; |
| 85 |
if (!get_key_block_lengths(ssl, &mac_secret_len, &key_len, &iv_len, |
| 86 |
session->cipher)) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
// Ensure that |key_block_cache| is set up. |
| 91 |
const size_t key_block_size = 2 * (mac_secret_len + key_len + iv_len); |
| 92 |
if (key_block_cache->empty()) { |
| 93 |
if (!key_block_cache->Init(key_block_size) || |
| 94 |
!generate_key_block(ssl, MakeSpan(*key_block_cache), session)) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
} |
| 98 |
assert(key_block_cache->size() == key_block_size); |
| 99 |
|
| 100 |
Span<const uint8_t> key_block = *key_block_cache; |
| 101 |
Span<const uint8_t> mac_secret, key, iv; |
| 102 |
if (direction == (ssl->server ? evp_aead_open : evp_aead_seal)) { |
| 103 |
// Use the client write (server read) keys. |
| 104 |
mac_secret = key_block.subspan(0, mac_secret_len); |
| 105 |
key = key_block.subspan(2 * mac_secret_len, key_len); |
| 106 |
iv = key_block.subspan(2 * mac_secret_len + 2 * key_len, iv_len); |
| 107 |
} else { |
| 108 |
// Use the server write (client read) keys. |
| 109 |
mac_secret = key_block.subspan(mac_secret_len, mac_secret_len); |
| 110 |
key = key_block.subspan(2 * mac_secret_len + key_len, key_len); |
| 111 |
iv = key_block.subspan(2 * mac_secret_len + 2 * key_len + iv_len, iv_len); |
| 112 |
} |
| 113 |
|
| 114 |
if (!iv_override.empty()) { |
| 115 |
if (iv_override.size() != iv_len) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
iv = iv_override; |
| 119 |
} |
| 120 |
|
| 121 |
UniquePtr<SSLAEADContext> aead_ctx = |
| 122 |
SSLAEADContext::Create(direction, ssl->version, SSL_is_dtls(ssl), |
| 123 |
session->cipher, key, mac_secret, iv); |
| 124 |
if (!aead_ctx) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
if (direction == evp_aead_open) { |
| 129 |
return ssl->method->set_read_state(ssl, ssl_encryption_application, |
| 130 |
std::move(aead_ctx), |
| 131 |
/*secret_for_quic=*/{}); |
| 132 |
} |
| 133 |
|
| 134 |
return ssl->method->set_write_state(ssl, ssl_encryption_application, |
| 135 |
std::move(aead_ctx), |
| 136 |
/*secret_for_quic=*/{}); |
| 137 |
} |
| 138 |
|
| 139 |
bool tls1_change_cipher_state(SSL_HANDSHAKE *hs, |
| 140 |
evp_aead_direction_t direction) { |
| 141 |
return tls1_configure_aead(hs->ssl, direction, &hs->key_block, |
| 142 |
ssl_handshake_session(hs), {}); |
| 143 |
} |
| 144 |
|
| 145 |
int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out, |
| 146 |
Span<const uint8_t> premaster) { |
| 147 |
static const char kMasterSecretLabel[] = "master secret"; |
| 148 |
static const char kExtendedMasterSecretLabel[] = "extended master secret"; |
| 149 |
|
| 150 |
const SSL *ssl = hs->ssl; |
| 151 |
auto out_span = MakeSpan(out, SSL3_MASTER_SECRET_SIZE); |
| 152 |
if (hs->extended_master_secret) { |
| 153 |
auto label = MakeConstSpan(kExtendedMasterSecretLabel, |
| 154 |
sizeof(kExtendedMasterSecretLabel) - 1); |
| 155 |
uint8_t digests[EVP_MAX_MD_SIZE]; |
| 156 |
size_t digests_len; |
| 157 |
if (!hs->transcript.GetHash(digests, &digests_len) || |
| 158 |
!tls1_prf(hs->transcript.Digest(), out_span, premaster, label, |
| 159 |
MakeConstSpan(digests, digests_len), {})) { |
| 160 |
return 0; |
| 161 |
} |
| 162 |
} else { |
| 163 |
auto label = |
| 164 |
MakeConstSpan(kMasterSecretLabel, sizeof(kMasterSecretLabel) - 1); |
| 165 |
if (!tls1_prf(hs->transcript.Digest(), out_span, premaster, label, |
| 166 |
ssl->s3->client_random, ssl->s3->server_random)) { |
| 167 |
return 0; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return SSL3_MASTER_SECRET_SIZE; |
| 172 |
} |
| 173 |
|
| 174 |
BSSL_NAMESPACE_END |
| 175 |
|
| 176 |
using namespace bssl; |
| 177 |
|
| 178 |
size_t SSL_get_key_block_len(const SSL *ssl) { |
| 179 |
// See |SSL_generate_key_block|. |
| 180 |
if (SSL_in_init(ssl) || ssl_protocol_version(ssl) > TLS1_2_VERSION) { |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
size_t mac_secret_len, key_len, fixed_iv_len; |
| 185 |
if (!get_key_block_lengths(ssl, &mac_secret_len, &key_len, &fixed_iv_len, |
| 186 |
SSL_get_current_cipher(ssl))) { |
| 187 |
ERR_clear_error(); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
return 2 * (mac_secret_len + key_len + fixed_iv_len); |
| 192 |
} |
| 193 |
|
| 194 |
int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) { |
| 195 |
// Which cipher state to use is ambiguous during a handshake. In particular, |
| 196 |
// there are points where read and write states are from different epochs. |
| 197 |
// During a handshake, before ChangeCipherSpec, the encryption states may not |
| 198 |
// match |ssl->s3->client_random| and |ssl->s3->server_random|. |
| 199 |
if (SSL_in_init(ssl) || ssl_protocol_version(ssl) > TLS1_2_VERSION) { |
| 200 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
return generate_key_block(ssl, MakeSpan(out, out_len), SSL_get_session(ssl)); |
| 205 |
} |
| 206 |
|
| 207 |
int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len, |
| 208 |
const char *label, size_t label_len, |
| 209 |
const uint8_t *context, size_t context_len, |
| 210 |
int use_context) { |
| 211 |
// In TLS 1.3, the exporter may be used whenever the secret has been derived. |
| 212 |
if (ssl->s3->have_version && ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 213 |
if (ssl->s3->exporter_secret_len == 0) { |
| 214 |
OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
if (!use_context) { |
| 218 |
context = nullptr; |
| 219 |
context_len = 0; |
| 220 |
} |
| 221 |
return tls13_export_keying_material( |
| 222 |
ssl, MakeSpan(out, out_len), |
| 223 |
MakeConstSpan(ssl->s3->exporter_secret, ssl->s3->exporter_secret_len), |
| 224 |
MakeConstSpan(label, label_len), MakeConstSpan(context, context_len)); |
| 225 |
} |
| 226 |
|
| 227 |
// Exporters may be used in False Start, where the handshake has progressed |
| 228 |
// enough. Otherwise, they may not be used during a handshake. |
| 229 |
if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) { |
| 230 |
OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE); |
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
size_t seed_len = 2 * SSL3_RANDOM_SIZE; |
| 235 |
if (use_context) { |
| 236 |
if (context_len >= 1u << 16) { |
| 237 |
OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 238 |
return 0; |
| 239 |
} |
| 240 |
seed_len += 2 + context_len; |
| 241 |
} |
| 242 |
Array<uint8_t> seed; |
| 243 |
if (!seed.Init(seed_len)) { |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
OPENSSL_memcpy(seed.data(), ssl->s3->client_random, SSL3_RANDOM_SIZE); |
| 248 |
OPENSSL_memcpy(seed.data() + SSL3_RANDOM_SIZE, ssl->s3->server_random, |
| 249 |
SSL3_RANDOM_SIZE); |
| 250 |
if (use_context) { |
| 251 |
seed[2 * SSL3_RANDOM_SIZE] = static_cast<uint8_t>(context_len >> 8); |
| 252 |
seed[2 * SSL3_RANDOM_SIZE + 1] = static_cast<uint8_t>(context_len); |
| 253 |
OPENSSL_memcpy(seed.data() + 2 * SSL3_RANDOM_SIZE + 2, context, context_len); |
| 254 |
} |
| 255 |
|
| 256 |
const SSL_SESSION *session = SSL_get_session(ssl); |
| 257 |
const EVP_MD *digest = ssl_session_get_digest(session); |
| 258 |
return tls1_prf(digest, MakeSpan(out, out_len), |
| 259 |
MakeConstSpan(session->secret, session->secret_length), |
| 260 |
MakeConstSpan(label, label_len), seed, {}); |
| 261 |
} |