1 /* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <openssl/ssl.h>
16
17 #include <assert.h>
18 #include <string.h>
19
20 #include <utility>
21
22 #include <openssl/aead.h>
23 #include <openssl/bytestring.h>
24 #include <openssl/digest.h>
25 #include <openssl/hkdf.h>
26 #include <openssl/hmac.h>
27 #include <openssl/mem.h>
28
29 #include "../crypto/internal.h"
30 #include "internal.h"
31
32
33 namespace bssl {
34
init_key_schedule(SSL_HANDSHAKE * hs,uint16_t version,const SSL_CIPHER * cipher)35 static int init_key_schedule(SSL_HANDSHAKE *hs, uint16_t version,
36 const SSL_CIPHER *cipher) {
37 if (!hs->transcript.InitHash(version, cipher)) {
38 return 0;
39 }
40
41 hs->hash_len = hs->transcript.DigestLen();
42
43 // Initialize the secret to the zero key.
44 OPENSSL_memset(hs->secret, 0, hs->hash_len);
45
46 return 1;
47 }
48
tls13_init_key_schedule(SSL_HANDSHAKE * hs,const uint8_t * psk,size_t psk_len)49 int tls13_init_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
50 size_t psk_len) {
51 if (!init_key_schedule(hs, ssl_protocol_version(hs->ssl), hs->new_cipher)) {
52 return 0;
53 }
54
55 hs->transcript.FreeBuffer();
56 return HKDF_extract(hs->secret, &hs->hash_len, hs->transcript.Digest(), psk,
57 psk_len, hs->secret, hs->hash_len);
58 }
59
tls13_init_early_key_schedule(SSL_HANDSHAKE * hs,const uint8_t * psk,size_t psk_len)60 int tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
61 size_t psk_len) {
62 SSL *const ssl = hs->ssl;
63 return init_key_schedule(hs, ssl_session_protocol_version(ssl->session),
64 ssl->session->cipher) &&
65 HKDF_extract(hs->secret, &hs->hash_len, hs->transcript.Digest(), psk,
66 psk_len, hs->secret, hs->hash_len);
67 }
68
hkdf_expand_label(uint8_t * out,const EVP_MD * digest,const uint8_t * secret,size_t secret_len,const char * label,size_t label_len,const uint8_t * hash,size_t hash_len,size_t len)69 static int hkdf_expand_label(uint8_t *out, const EVP_MD *digest,
70 const uint8_t *secret, size_t secret_len,
71 const char *label, size_t label_len,
72 const uint8_t *hash, size_t hash_len, size_t len) {
73 static const char kTLS13LabelVersion[] = "tls13 ";
74
75 ScopedCBB cbb;
76 CBB child;
77 uint8_t *hkdf_label;
78 size_t hkdf_label_len;
79 if (!CBB_init(cbb.get(), 2 + 1 + strlen(kTLS13LabelVersion) + label_len + 1 +
80 hash_len) ||
81 !CBB_add_u16(cbb.get(), len) ||
82 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
83 !CBB_add_bytes(&child, (const uint8_t *)kTLS13LabelVersion,
84 strlen(kTLS13LabelVersion)) ||
85 !CBB_add_bytes(&child, (const uint8_t *)label, label_len) ||
86 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
87 !CBB_add_bytes(&child, hash, hash_len) ||
88 !CBB_finish(cbb.get(), &hkdf_label, &hkdf_label_len)) {
89 return 0;
90 }
91
92 int ret = HKDF_expand(out, len, digest, secret, secret_len, hkdf_label,
93 hkdf_label_len);
94 OPENSSL_free(hkdf_label);
95 return ret;
96 }
97
98 static const char kTLS13LabelDerived[] = "derived";
99
tls13_advance_key_schedule(SSL_HANDSHAKE * hs,const uint8_t * in,size_t len)100 int tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in,
101 size_t len) {
102 uint8_t derive_context[EVP_MAX_MD_SIZE];
103 unsigned derive_context_len;
104 if (!EVP_Digest(nullptr, 0, derive_context, &derive_context_len,
105 hs->transcript.Digest(), nullptr)) {
106 return 0;
107 }
108
109 if (!hkdf_expand_label(hs->secret, hs->transcript.Digest(), hs->secret,
110 hs->hash_len, kTLS13LabelDerived,
111 strlen(kTLS13LabelDerived), derive_context,
112 derive_context_len, hs->hash_len)) {
113 return 0;
114 }
115
116 return HKDF_extract(hs->secret, &hs->hash_len, hs->transcript.Digest(), in,
117 len, hs->secret, hs->hash_len);
118 }
119
120 // derive_secret derives a secret of length |len| and writes the result in |out|
121 // with the given label and the current base secret and most recently-saved
122 // handshake context. It returns one on success and zero on error.
derive_secret(SSL_HANDSHAKE * hs,uint8_t * out,size_t len,const char * label,size_t label_len)123 static int derive_secret(SSL_HANDSHAKE *hs, uint8_t *out, size_t len,
124 const char *label, size_t label_len) {
125 uint8_t context_hash[EVP_MAX_MD_SIZE];
126 size_t context_hash_len;
127 if (!hs->transcript.GetHash(context_hash, &context_hash_len)) {
128 return 0;
129 }
130
131 return hkdf_expand_label(out, hs->transcript.Digest(), hs->secret,
132 hs->hash_len, label, label_len, context_hash,
133 context_hash_len, len);
134 }
135
tls13_set_traffic_key(SSL * ssl,enum evp_aead_direction_t direction,const uint8_t * traffic_secret,size_t traffic_secret_len)136 int tls13_set_traffic_key(SSL *ssl, enum evp_aead_direction_t direction,
137 const uint8_t *traffic_secret,
138 size_t traffic_secret_len) {
139 const SSL_SESSION *session = SSL_get_session(ssl);
140 uint16_t version = ssl_session_protocol_version(session);
141
142 if (traffic_secret_len > 0xff) {
143 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
144 return 0;
145 }
146
147 // Look up cipher suite properties.
148 const EVP_AEAD *aead;
149 size_t discard;
150 if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, session->cipher,
151 version, SSL_is_dtls(ssl))) {
152 return 0;
153 }
154
155 const EVP_MD *digest = ssl_session_get_digest(session);
156
157 // Derive the key.
158 size_t key_len = EVP_AEAD_key_length(aead);
159 uint8_t key[EVP_AEAD_MAX_KEY_LENGTH];
160 if (!hkdf_expand_label(key, digest, traffic_secret, traffic_secret_len, "key",
161 3, NULL, 0, key_len)) {
162 return 0;
163 }
164
165 // Derive the IV.
166 size_t iv_len = EVP_AEAD_nonce_length(aead);
167 uint8_t iv[EVP_AEAD_MAX_NONCE_LENGTH];
168 if (!hkdf_expand_label(iv, digest, traffic_secret, traffic_secret_len, "iv",
169 2, NULL, 0, iv_len)) {
170 return 0;
171 }
172
173 UniquePtr<SSLAEADContext> traffic_aead =
174 SSLAEADContext::Create(direction, session->ssl_version, SSL_is_dtls(ssl),
175 session->cipher, MakeConstSpan(key, key_len),
176 Span<const uint8_t>(), MakeConstSpan(iv, iv_len));
177 if (!traffic_aead) {
178 return 0;
179 }
180
181 if (direction == evp_aead_open) {
182 if (!ssl->method->set_read_state(ssl, std::move(traffic_aead))) {
183 return 0;
184 }
185 } else {
186 if (!ssl->method->set_write_state(ssl, std::move(traffic_aead))) {
187 return 0;
188 }
189 }
190
191 // Save the traffic secret.
192 if (direction == evp_aead_open) {
193 OPENSSL_memmove(ssl->s3->read_traffic_secret, traffic_secret,
194 traffic_secret_len);
195 ssl->s3->read_traffic_secret_len = traffic_secret_len;
196 } else {
197 OPENSSL_memmove(ssl->s3->write_traffic_secret, traffic_secret,
198 traffic_secret_len);
199 ssl->s3->write_traffic_secret_len = traffic_secret_len;
200 }
201
202 return 1;
203 }
204
205
206 static const char kTLS13LabelExporter[] = "exp master";
207 static const char kTLS13LabelEarlyExporter[] = "e exp master";
208
209 static const char kTLS13LabelClientEarlyTraffic[] = "c e traffic";
210 static const char kTLS13LabelClientHandshakeTraffic[] = "c hs traffic";
211 static const char kTLS13LabelServerHandshakeTraffic[] = "s hs traffic";
212 static const char kTLS13LabelClientApplicationTraffic[] = "c ap traffic";
213 static const char kTLS13LabelServerApplicationTraffic[] = "s ap traffic";
214
tls13_derive_early_secrets(SSL_HANDSHAKE * hs)215 int tls13_derive_early_secrets(SSL_HANDSHAKE *hs) {
216 SSL *const ssl = hs->ssl;
217 if (!derive_secret(hs, hs->early_traffic_secret, hs->hash_len,
218 kTLS13LabelClientEarlyTraffic,
219 strlen(kTLS13LabelClientEarlyTraffic)) ||
220 !ssl_log_secret(ssl, "CLIENT_EARLY_TRAFFIC_SECRET",
221 hs->early_traffic_secret, hs->hash_len) ||
222 !derive_secret(hs, ssl->s3->early_exporter_secret, hs->hash_len,
223 kTLS13LabelEarlyExporter,
224 strlen(kTLS13LabelEarlyExporter))) {
225 return 0;
226 }
227 ssl->s3->early_exporter_secret_len = hs->hash_len;
228 return 1;
229 }
230
tls13_derive_handshake_secrets(SSL_HANDSHAKE * hs)231 int tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs) {
232 SSL *const ssl = hs->ssl;
233 return derive_secret(hs, hs->client_handshake_secret, hs->hash_len,
234 kTLS13LabelClientHandshakeTraffic,
235 strlen(kTLS13LabelClientHandshakeTraffic)) &&
236 ssl_log_secret(ssl, "CLIENT_HANDSHAKE_TRAFFIC_SECRET",
237 hs->client_handshake_secret, hs->hash_len) &&
238 derive_secret(hs, hs->server_handshake_secret, hs->hash_len,
239 kTLS13LabelServerHandshakeTraffic,
240 strlen(kTLS13LabelServerHandshakeTraffic)) &&
241 ssl_log_secret(ssl, "SERVER_HANDSHAKE_TRAFFIC_SECRET",
242 hs->server_handshake_secret, hs->hash_len);
243 }
244
tls13_derive_application_secrets(SSL_HANDSHAKE * hs)245 int tls13_derive_application_secrets(SSL_HANDSHAKE *hs) {
246 SSL *const ssl = hs->ssl;
247 ssl->s3->exporter_secret_len = hs->hash_len;
248 return derive_secret(hs, hs->client_traffic_secret_0, hs->hash_len,
249 kTLS13LabelClientApplicationTraffic,
250 strlen(kTLS13LabelClientApplicationTraffic)) &&
251 ssl_log_secret(ssl, "CLIENT_TRAFFIC_SECRET_0",
252 hs->client_traffic_secret_0, hs->hash_len) &&
253 derive_secret(hs, hs->server_traffic_secret_0, hs->hash_len,
254 kTLS13LabelServerApplicationTraffic,
255 strlen(kTLS13LabelServerApplicationTraffic)) &&
256 ssl_log_secret(ssl, "SERVER_TRAFFIC_SECRET_0",
257 hs->server_traffic_secret_0, hs->hash_len) &&
258 derive_secret(hs, ssl->s3->exporter_secret, hs->hash_len,
259 kTLS13LabelExporter, strlen(kTLS13LabelExporter)) &&
260 ssl_log_secret(ssl, "EXPORTER_SECRET", ssl->s3->exporter_secret,
261 hs->hash_len);
262 }
263
264 static const char kTLS13LabelApplicationTraffic[] = "traffic upd";
265
tls13_rotate_traffic_key(SSL * ssl,enum evp_aead_direction_t direction)266 int tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction) {
267 uint8_t *secret;
268 size_t secret_len;
269 if (direction == evp_aead_open) {
270 secret = ssl->s3->read_traffic_secret;
271 secret_len = ssl->s3->read_traffic_secret_len;
272 } else {
273 secret = ssl->s3->write_traffic_secret;
274 secret_len = ssl->s3->write_traffic_secret_len;
275 }
276
277 const EVP_MD *digest = ssl_session_get_digest(SSL_get_session(ssl));
278 if (!hkdf_expand_label(
279 secret, digest, secret, secret_len, kTLS13LabelApplicationTraffic,
280 strlen(kTLS13LabelApplicationTraffic), NULL, 0, secret_len)) {
281 return 0;
282 }
283
284 return tls13_set_traffic_key(ssl, direction, secret, secret_len);
285 }
286
287 static const char kTLS13LabelResumption[] = "res master";
288
tls13_derive_resumption_secret(SSL_HANDSHAKE * hs)289 int tls13_derive_resumption_secret(SSL_HANDSHAKE *hs) {
290 if (hs->hash_len > SSL_MAX_MASTER_KEY_LENGTH) {
291 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
292 return 0;
293 }
294 hs->new_session->master_key_length = hs->hash_len;
295 return derive_secret(hs, hs->new_session->master_key,
296 hs->new_session->master_key_length,
297 kTLS13LabelResumption, strlen(kTLS13LabelResumption));
298 }
299
300 static const char kTLS13LabelFinished[] = "finished";
301
302 // tls13_verify_data sets |out| to be the HMAC of |context| using a derived
303 // Finished key for both Finished messages and the PSK binder.
tls13_verify_data(const EVP_MD * digest,uint16_t version,uint8_t * out,size_t * out_len,const uint8_t * secret,size_t hash_len,uint8_t * context,size_t context_len)304 static int tls13_verify_data(const EVP_MD *digest, uint16_t version,
305 uint8_t *out, size_t *out_len,
306 const uint8_t *secret, size_t hash_len,
307 uint8_t *context, size_t context_len) {
308 uint8_t key[EVP_MAX_MD_SIZE];
309 unsigned len;
310 if (!hkdf_expand_label(key, digest, secret, hash_len, kTLS13LabelFinished,
311 strlen(kTLS13LabelFinished), NULL, 0, hash_len) ||
312 HMAC(digest, key, hash_len, context, context_len, out, &len) == NULL) {
313 return 0;
314 }
315 *out_len = len;
316 return 1;
317 }
318
tls13_finished_mac(SSL_HANDSHAKE * hs,uint8_t * out,size_t * out_len,int is_server)319 int tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len,
320 int is_server) {
321 const uint8_t *traffic_secret;
322 if (is_server) {
323 traffic_secret = hs->server_handshake_secret;
324 } else {
325 traffic_secret = hs->client_handshake_secret;
326 }
327
328 uint8_t context_hash[EVP_MAX_MD_SIZE];
329 size_t context_hash_len;
330 if (!hs->transcript.GetHash(context_hash, &context_hash_len) ||
331 !tls13_verify_data(hs->transcript.Digest(), hs->ssl->version, out,
332 out_len, traffic_secret, hs->hash_len, context_hash,
333 context_hash_len)) {
334 return 0;
335 }
336 return 1;
337 }
338
339 static const char kTLS13LabelResumptionPSK[] = "resumption";
340
tls13_derive_session_psk(SSL_SESSION * session,Span<const uint8_t> nonce)341 bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce) {
342 const EVP_MD *digest = ssl_session_get_digest(session);
343 return hkdf_expand_label(session->master_key, digest, session->master_key,
344 session->master_key_length, kTLS13LabelResumptionPSK,
345 strlen(kTLS13LabelResumptionPSK), nonce.data(),
346 nonce.size(), session->master_key_length);
347 }
348
349 static const char kTLS13LabelExportKeying[] = "exporter";
350
tls13_export_keying_material(SSL * ssl,Span<uint8_t> out,Span<const uint8_t> secret,Span<const char> label,Span<const uint8_t> context)351 int tls13_export_keying_material(SSL *ssl, Span<uint8_t> out,
352 Span<const uint8_t> secret,
353 Span<const char> label,
354 Span<const uint8_t> context) {
355 if (secret.empty()) {
356 assert(0);
357 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
358 return 0;
359 }
360
361 const EVP_MD *digest = ssl_session_get_digest(SSL_get_session(ssl));
362
363 uint8_t hash[EVP_MAX_MD_SIZE];
364 uint8_t export_context[EVP_MAX_MD_SIZE];
365 uint8_t derived_secret[EVP_MAX_MD_SIZE];
366 unsigned hash_len;
367 unsigned export_context_len;
368 unsigned derived_secret_len = EVP_MD_size(digest);
369 return EVP_Digest(context.data(), context.size(), hash, &hash_len, digest,
370 nullptr) &&
371 EVP_Digest(nullptr, 0, export_context, &export_context_len, digest,
372 nullptr) &&
373 hkdf_expand_label(derived_secret, digest, secret.data(), secret.size(),
374 label.data(), label.size(), export_context,
375 export_context_len, derived_secret_len) &&
376 hkdf_expand_label(out.data(), digest, derived_secret,
377 derived_secret_len, kTLS13LabelExportKeying,
378 strlen(kTLS13LabelExportKeying), hash, hash_len,
379 out.size());
380 }
381
382 static const char kTLS13LabelPSKBinder[] = "res binder";
383
tls13_psk_binder(uint8_t * out,uint16_t version,const EVP_MD * digest,uint8_t * psk,size_t psk_len,uint8_t * context,size_t context_len,size_t hash_len)384 static int tls13_psk_binder(uint8_t *out, uint16_t version,
385 const EVP_MD *digest, uint8_t *psk, size_t psk_len,
386 uint8_t *context, size_t context_len,
387 size_t hash_len) {
388 uint8_t binder_context[EVP_MAX_MD_SIZE];
389 unsigned binder_context_len;
390 if (!EVP_Digest(NULL, 0, binder_context, &binder_context_len, digest, NULL)) {
391 return 0;
392 }
393
394 uint8_t early_secret[EVP_MAX_MD_SIZE] = {0};
395 size_t early_secret_len;
396 if (!HKDF_extract(early_secret, &early_secret_len, digest, psk, hash_len,
397 NULL, 0)) {
398 return 0;
399 }
400
401 uint8_t binder_key[EVP_MAX_MD_SIZE] = {0};
402 size_t len;
403 if (!hkdf_expand_label(binder_key, digest, early_secret, hash_len,
404 kTLS13LabelPSKBinder, strlen(kTLS13LabelPSKBinder),
405 binder_context, binder_context_len, hash_len) ||
406 !tls13_verify_data(digest, version, out, &len, binder_key, hash_len,
407 context, context_len)) {
408 return 0;
409 }
410
411 return 1;
412 }
413
tls13_write_psk_binder(SSL_HANDSHAKE * hs,uint8_t * msg,size_t len)414 int tls13_write_psk_binder(SSL_HANDSHAKE *hs, uint8_t *msg, size_t len) {
415 SSL *const ssl = hs->ssl;
416 const EVP_MD *digest = ssl_session_get_digest(ssl->session);
417 size_t hash_len = EVP_MD_size(digest);
418
419 if (len < hash_len + 3) {
420 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
421 return 0;
422 }
423
424 ScopedEVP_MD_CTX ctx;
425 uint8_t context[EVP_MAX_MD_SIZE];
426 unsigned context_len;
427
428 if (!EVP_DigestInit_ex(ctx.get(), digest, NULL) ||
429 !EVP_DigestUpdate(ctx.get(), hs->transcript.buffer().data(),
430 hs->transcript.buffer().size()) ||
431 !EVP_DigestUpdate(ctx.get(), msg, len - hash_len - 3) ||
432 !EVP_DigestFinal_ex(ctx.get(), context, &context_len)) {
433 return 0;
434 }
435
436 uint8_t verify_data[EVP_MAX_MD_SIZE] = {0};
437 if (!tls13_psk_binder(verify_data, ssl->session->ssl_version, digest,
438 ssl->session->master_key,
439 ssl->session->master_key_length, context, context_len,
440 hash_len)) {
441 return 0;
442 }
443
444 OPENSSL_memcpy(msg + len - hash_len, verify_data, hash_len);
445 return 1;
446 }
447
tls13_verify_psk_binder(SSL_HANDSHAKE * hs,SSL_SESSION * session,const SSLMessage & msg,CBS * binders)448 int tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
449 const SSLMessage &msg, CBS *binders) {
450 size_t hash_len = hs->transcript.DigestLen();
451
452 // The message must be large enough to exclude the binders.
453 if (CBS_len(&msg.raw) < CBS_len(binders) + 2) {
454 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
455 return 0;
456 }
457
458 // Hash a ClientHello prefix up to the binders. This includes the header. For
459 // now, this assumes we only ever verify PSK binders on initial
460 // ClientHellos.
461 uint8_t context[EVP_MAX_MD_SIZE];
462 unsigned context_len;
463 if (!EVP_Digest(CBS_data(&msg.raw), CBS_len(&msg.raw) - CBS_len(binders) - 2,
464 context, &context_len, hs->transcript.Digest(), NULL)) {
465 return 0;
466 }
467
468 uint8_t verify_data[EVP_MAX_MD_SIZE] = {0};
469 CBS binder;
470 if (!tls13_psk_binder(verify_data, hs->ssl->version, hs->transcript.Digest(),
471 session->master_key, session->master_key_length,
472 context, context_len, hash_len) ||
473 // We only consider the first PSK, so compare against the first binder.
474 !CBS_get_u8_length_prefixed(binders, &binder)) {
475 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
476 return 0;
477 }
478
479 int binder_ok =
480 CBS_len(&binder) == hash_len &&
481 CRYPTO_memcmp(CBS_data(&binder), verify_data, hash_len) == 0;
482 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
483 binder_ok = 1;
484 #endif
485 if (!binder_ok) {
486 OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
487 return 0;
488 }
489
490 return 1;
491 }
492
493 } // namespace bssl
494