• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <algorithm>
21 #include <utility>
22 
23 #include <openssl/aead.h>
24 #include <openssl/bytestring.h>
25 #include <openssl/digest.h>
26 #include <openssl/hkdf.h>
27 #include <openssl/hmac.h>
28 #include <openssl/mem.h>
29 
30 #include "../crypto/internal.h"
31 #include "internal.h"
32 
33 
34 BSSL_NAMESPACE_BEGIN
35 
init_key_schedule(SSL_HANDSHAKE * hs,uint16_t version,const SSL_CIPHER * cipher)36 static bool init_key_schedule(SSL_HANDSHAKE *hs, uint16_t version,
37                               const SSL_CIPHER *cipher) {
38   if (!hs->transcript.InitHash(version, cipher)) {
39     return false;
40   }
41 
42   // Initialize the secret to the zero key.
43   hs->ResizeSecrets(hs->transcript.DigestLen());
44   OPENSSL_memset(hs->secret().data(), 0, hs->secret().size());
45 
46   return true;
47 }
48 
hkdf_extract_to_secret(SSL_HANDSHAKE * hs,Span<const uint8_t> in)49 static bool hkdf_extract_to_secret(SSL_HANDSHAKE *hs, Span<const uint8_t> in) {
50   size_t len;
51   if (!HKDF_extract(hs->secret().data(), &len, hs->transcript.Digest(),
52                     in.data(), in.size(), hs->secret().data(),
53                     hs->secret().size())) {
54     return false;
55   }
56   assert(len == hs->secret().size());
57   return true;
58 }
59 
tls13_init_key_schedule(SSL_HANDSHAKE * hs,Span<const uint8_t> psk)60 bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> psk) {
61   if (!init_key_schedule(hs, ssl_protocol_version(hs->ssl), hs->new_cipher)) {
62     return false;
63   }
64 
65   hs->transcript.FreeBuffer();
66   return hkdf_extract_to_secret(hs, psk);
67 }
68 
tls13_init_early_key_schedule(SSL_HANDSHAKE * hs,Span<const uint8_t> psk)69 bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> psk) {
70   SSL *const ssl = hs->ssl;
71   return init_key_schedule(hs, ssl_session_protocol_version(ssl->session.get()),
72                            ssl->session->cipher) &&
73          hkdf_extract_to_secret(hs, psk);
74 }
75 
label_to_span(const char * label)76 static Span<const char> label_to_span(const char *label) {
77   return MakeConstSpan(label, strlen(label));
78 }
79 
hkdf_expand_label(Span<uint8_t> out,const EVP_MD * digest,Span<const uint8_t> secret,Span<const char> label,Span<const uint8_t> hash)80 static bool hkdf_expand_label(Span<uint8_t> out, const EVP_MD *digest,
81                               Span<const uint8_t> secret,
82                               Span<const char> label,
83                               Span<const uint8_t> hash) {
84   Span<const char> protocol_label = label_to_span("tls13 ");
85   ScopedCBB cbb;
86   CBB child;
87   Array<uint8_t> hkdf_label;
88   if (!CBB_init(cbb.get(), 2 + 1 + protocol_label.size() + label.size() + 1 +
89                                hash.size()) ||
90       !CBB_add_u16(cbb.get(), out.size()) ||
91       !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
92       !CBB_add_bytes(&child,
93                      reinterpret_cast<const uint8_t *>(protocol_label.data()),
94                      protocol_label.size()) ||
95       !CBB_add_bytes(&child, reinterpret_cast<const uint8_t *>(label.data()),
96                      label.size()) ||
97       !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
98       !CBB_add_bytes(&child, hash.data(), hash.size()) ||
99       !CBBFinishArray(cbb.get(), &hkdf_label)) {
100     return false;
101   }
102 
103   return HKDF_expand(out.data(), out.size(), digest, secret.data(),
104                      secret.size(), hkdf_label.data(), hkdf_label.size());
105 }
106 
107 static const char kTLS13LabelDerived[] = "derived";
108 
tls13_advance_key_schedule(SSL_HANDSHAKE * hs,Span<const uint8_t> in)109 bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> in) {
110   uint8_t derive_context[EVP_MAX_MD_SIZE];
111   unsigned derive_context_len;
112   return EVP_Digest(nullptr, 0, derive_context, &derive_context_len,
113                     hs->transcript.Digest(), nullptr) &&
114          hkdf_expand_label(hs->secret(), hs->transcript.Digest(), hs->secret(),
115                            label_to_span(kTLS13LabelDerived),
116                            MakeConstSpan(derive_context, derive_context_len)) &&
117          hkdf_extract_to_secret(hs, in);
118 }
119 
120 // derive_secret derives a secret of length |out.size()| and writes the result
121 // in |out| with the given label, the current base secret, and the most
122 // recently-saved handshake context. It returns true on success and false on
123 // error.
derive_secret(SSL_HANDSHAKE * hs,Span<uint8_t> out,Span<const char> label)124 static bool derive_secret(SSL_HANDSHAKE *hs, Span<uint8_t> out,
125                           Span<const char> label) {
126   uint8_t context_hash[EVP_MAX_MD_SIZE];
127   size_t context_hash_len;
128   if (!hs->transcript.GetHash(context_hash, &context_hash_len)) {
129     return false;
130   }
131 
132   return hkdf_expand_label(out, hs->transcript.Digest(), hs->secret(), label,
133                            MakeConstSpan(context_hash, context_hash_len));
134 }
135 
tls13_set_traffic_key(SSL * ssl,enum ssl_encryption_level_t level,enum evp_aead_direction_t direction,Span<const uint8_t> traffic_secret)136 bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level,
137                            enum evp_aead_direction_t direction,
138                            Span<const uint8_t> traffic_secret) {
139   const SSL_SESSION *session = SSL_get_session(ssl);
140   uint16_t version = ssl_session_protocol_version(session);
141 
142   UniquePtr<SSLAEADContext> traffic_aead;
143   if (ssl->quic_method == nullptr) {
144     // Look up cipher suite properties.
145     const EVP_AEAD *aead;
146     size_t discard;
147     if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, session->cipher,
148                                  version, SSL_is_dtls(ssl))) {
149       return false;
150     }
151 
152     const EVP_MD *digest = ssl_session_get_digest(session);
153 
154     // Derive the key.
155     size_t key_len = EVP_AEAD_key_length(aead);
156     uint8_t key_buf[EVP_AEAD_MAX_KEY_LENGTH];
157     auto key = MakeSpan(key_buf, key_len);
158     if (!hkdf_expand_label(key, digest, traffic_secret, label_to_span("key"),
159                            {})) {
160       return false;
161     }
162 
163     // Derive the IV.
164     size_t iv_len = EVP_AEAD_nonce_length(aead);
165     uint8_t iv_buf[EVP_AEAD_MAX_NONCE_LENGTH];
166     auto iv = MakeSpan(iv_buf, iv_len);
167     if (!hkdf_expand_label(iv, digest, traffic_secret, label_to_span("iv"),
168                            {})) {
169       return false;
170     }
171 
172 
173     traffic_aead = SSLAEADContext::Create(direction, session->ssl_version,
174                                           SSL_is_dtls(ssl), session->cipher,
175                                           key, Span<const uint8_t>(), iv);
176   } else {
177     // Install a placeholder SSLAEADContext so that SSL accessors work. The
178     // encryption itself will be handled by the SSL_QUIC_METHOD.
179     traffic_aead =
180         SSLAEADContext::CreatePlaceholderForQUIC(version, session->cipher);
181     // QUIC never installs early data keys at the TLS layer.
182     assert(level != ssl_encryption_early_data);
183   }
184 
185   if (!traffic_aead) {
186     return false;
187   }
188 
189   if (direction == evp_aead_open) {
190     if (!ssl->method->set_read_state(ssl, std::move(traffic_aead))) {
191       return false;
192     }
193   } else {
194     if (!ssl->method->set_write_state(ssl, std::move(traffic_aead))) {
195       return false;
196     }
197   }
198 
199   // Save the traffic secret.
200   if (traffic_secret.size() >
201           OPENSSL_ARRAY_SIZE(ssl->s3->read_traffic_secret) ||
202       traffic_secret.size() >
203           OPENSSL_ARRAY_SIZE(ssl->s3->write_traffic_secret)) {
204     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
205     return false;
206   }
207   if (direction == evp_aead_open) {
208     OPENSSL_memmove(ssl->s3->read_traffic_secret, traffic_secret.data(),
209                     traffic_secret.size());
210     ssl->s3->read_traffic_secret_len = traffic_secret.size();
211     ssl->s3->read_level = level;
212   } else {
213     OPENSSL_memmove(ssl->s3->write_traffic_secret, traffic_secret.data(),
214                     traffic_secret.size());
215     ssl->s3->write_traffic_secret_len = traffic_secret.size();
216     ssl->s3->write_level = level;
217   }
218 
219   return true;
220 }
221 
222 
223 static const char kTLS13LabelExporter[] = "exp master";
224 
225 static const char kTLS13LabelClientEarlyTraffic[] = "c e traffic";
226 static const char kTLS13LabelClientHandshakeTraffic[] = "c hs traffic";
227 static const char kTLS13LabelServerHandshakeTraffic[] = "s hs traffic";
228 static const char kTLS13LabelClientApplicationTraffic[] = "c ap traffic";
229 static const char kTLS13LabelServerApplicationTraffic[] = "s ap traffic";
230 
tls13_derive_early_secret(SSL_HANDSHAKE * hs)231 bool tls13_derive_early_secret(SSL_HANDSHAKE *hs) {
232   SSL *const ssl = hs->ssl;
233   if (!derive_secret(hs, hs->early_traffic_secret(),
234                      label_to_span(kTLS13LabelClientEarlyTraffic)) ||
235       !ssl_log_secret(ssl, "CLIENT_EARLY_TRAFFIC_SECRET",
236                       hs->early_traffic_secret())) {
237     return false;
238   }
239   return true;
240 }
241 
tls13_set_early_secret_for_quic(SSL_HANDSHAKE * hs)242 bool tls13_set_early_secret_for_quic(SSL_HANDSHAKE *hs) {
243   SSL *const ssl = hs->ssl;
244   if (ssl->quic_method == nullptr) {
245     return true;
246   }
247   if (ssl->server) {
248     if (!ssl->quic_method->set_encryption_secrets(
249             ssl, ssl_encryption_early_data, hs->early_traffic_secret().data(),
250             /*write_secret=*/nullptr, hs->early_traffic_secret().size())) {
251       OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR);
252       return false;
253     }
254   } else {
255     if (!ssl->quic_method->set_encryption_secrets(
256             ssl, ssl_encryption_early_data, /*read_secret=*/nullptr,
257             hs->early_traffic_secret().data(),
258             hs->early_traffic_secret().size())) {
259       OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR);
260       return false;
261     }
262   }
263   return true;
264 }
265 
set_quic_secrets(SSL_HANDSHAKE * hs,ssl_encryption_level_t level,Span<const uint8_t> client_write_secret,Span<const uint8_t> server_write_secret)266 static bool set_quic_secrets(SSL_HANDSHAKE *hs, ssl_encryption_level_t level,
267                              Span<const uint8_t> client_write_secret,
268                              Span<const uint8_t> server_write_secret) {
269   SSL *const ssl = hs->ssl;
270   assert(client_write_secret.size() == server_write_secret.size());
271   if (ssl->quic_method == nullptr) {
272     return true;
273   }
274   if (!ssl->server) {
275     std::swap(client_write_secret, server_write_secret);
276   }
277   return ssl->quic_method->set_encryption_secrets(
278       ssl, level,
279       /*read_secret=*/client_write_secret.data(),
280       /*write_secret=*/server_write_secret.data(), client_write_secret.size());
281 }
282 
tls13_derive_handshake_secrets(SSL_HANDSHAKE * hs)283 bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs) {
284   SSL *const ssl = hs->ssl;
285   if (!derive_secret(hs, hs->client_handshake_secret(),
286                      label_to_span(kTLS13LabelClientHandshakeTraffic)) ||
287       !ssl_log_secret(ssl, "CLIENT_HANDSHAKE_TRAFFIC_SECRET",
288                       hs->client_handshake_secret()) ||
289       !derive_secret(hs, hs->server_handshake_secret(),
290                      label_to_span(kTLS13LabelServerHandshakeTraffic)) ||
291       !ssl_log_secret(ssl, "SERVER_HANDSHAKE_TRAFFIC_SECRET",
292                       hs->server_handshake_secret()) ||
293       !set_quic_secrets(hs, ssl_encryption_handshake,
294                         hs->client_handshake_secret(),
295                         hs->server_handshake_secret())) {
296     return false;
297   }
298 
299   return true;
300 }
301 
tls13_derive_application_secrets(SSL_HANDSHAKE * hs)302 bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs) {
303   SSL *const ssl = hs->ssl;
304   ssl->s3->exporter_secret_len = hs->transcript.DigestLen();
305   if (!derive_secret(hs, hs->client_traffic_secret_0(),
306                      label_to_span(kTLS13LabelClientApplicationTraffic)) ||
307       !ssl_log_secret(ssl, "CLIENT_TRAFFIC_SECRET_0",
308                       hs->client_traffic_secret_0()) ||
309       !derive_secret(hs, hs->server_traffic_secret_0(),
310                      label_to_span(kTLS13LabelServerApplicationTraffic)) ||
311       !ssl_log_secret(ssl, "SERVER_TRAFFIC_SECRET_0",
312                       hs->server_traffic_secret_0()) ||
313       !derive_secret(
314           hs, MakeSpan(ssl->s3->exporter_secret, ssl->s3->exporter_secret_len),
315           label_to_span(kTLS13LabelExporter)) ||
316       !ssl_log_secret(ssl, "EXPORTER_SECRET",
317                       MakeConstSpan(ssl->s3->exporter_secret,
318                                     ssl->s3->exporter_secret_len)) ||
319       !set_quic_secrets(hs, ssl_encryption_application,
320                         hs->client_traffic_secret_0(),
321                         hs->server_traffic_secret_0())) {
322     return false;
323   }
324 
325   return true;
326 }
327 
328 static const char kTLS13LabelApplicationTraffic[] = "traffic upd";
329 
tls13_rotate_traffic_key(SSL * ssl,enum evp_aead_direction_t direction)330 bool tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction) {
331   Span<uint8_t> secret;
332   if (direction == evp_aead_open) {
333     secret = MakeSpan(ssl->s3->read_traffic_secret,
334                       ssl->s3->read_traffic_secret_len);
335   } else {
336     secret = MakeSpan(ssl->s3->write_traffic_secret,
337                       ssl->s3->write_traffic_secret_len);
338   }
339 
340   const EVP_MD *digest = ssl_session_get_digest(SSL_get_session(ssl));
341   return hkdf_expand_label(secret, digest, secret,
342                            label_to_span(kTLS13LabelApplicationTraffic), {}) &&
343          tls13_set_traffic_key(ssl, ssl_encryption_application, direction,
344                                secret);
345 }
346 
347 static const char kTLS13LabelResumption[] = "res master";
348 
tls13_derive_resumption_secret(SSL_HANDSHAKE * hs)349 bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs) {
350   if (hs->transcript.DigestLen() > SSL_MAX_MASTER_KEY_LENGTH) {
351     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
352     return false;
353   }
354   hs->new_session->master_key_length = hs->transcript.DigestLen();
355   return derive_secret(
356       hs,
357       MakeSpan(hs->new_session->master_key, hs->new_session->master_key_length),
358       label_to_span(kTLS13LabelResumption));
359 }
360 
361 static const char kTLS13LabelFinished[] = "finished";
362 
363 // tls13_verify_data sets |out| to be the HMAC of |context| using a derived
364 // Finished key for both Finished messages and the PSK binder. |out| must have
365 // space available for |EVP_MAX_MD_SIZE| bytes.
tls13_verify_data(uint8_t * out,size_t * out_len,const EVP_MD * digest,uint16_t version,Span<const uint8_t> secret,Span<const uint8_t> context)366 static bool tls13_verify_data(uint8_t *out, size_t *out_len,
367                               const EVP_MD *digest, uint16_t version,
368                               Span<const uint8_t> secret,
369                               Span<const uint8_t> context) {
370   uint8_t key_buf[EVP_MAX_MD_SIZE];
371   auto key = MakeSpan(key_buf, EVP_MD_size(digest));
372   unsigned len;
373   if (!hkdf_expand_label(key, digest, secret,
374                          label_to_span(kTLS13LabelFinished), {}) ||
375       HMAC(digest, key.data(), key.size(), context.data(), context.size(), out,
376            &len) == nullptr) {
377     return false;
378   }
379   *out_len = len;
380   return true;
381 }
382 
tls13_finished_mac(SSL_HANDSHAKE * hs,uint8_t * out,size_t * out_len,bool is_server)383 bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len,
384                         bool is_server) {
385   Span<const uint8_t> traffic_secret =
386       is_server ? hs->server_handshake_secret() : hs->client_handshake_secret();
387 
388   uint8_t context_hash[EVP_MAX_MD_SIZE];
389   size_t context_hash_len;
390   if (!hs->transcript.GetHash(context_hash, &context_hash_len) ||
391       !tls13_verify_data(out, out_len, hs->transcript.Digest(),
392                          hs->ssl->version, traffic_secret,
393                          MakeConstSpan(context_hash, context_hash_len))) {
394     return 0;
395   }
396   return 1;
397 }
398 
399 static const char kTLS13LabelResumptionPSK[] = "resumption";
400 
tls13_derive_session_psk(SSL_SESSION * session,Span<const uint8_t> nonce)401 bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce) {
402   const EVP_MD *digest = ssl_session_get_digest(session);
403   // The session initially stores the resumption_master_secret, which we
404   // override with the PSK.
405   auto session_key = MakeSpan(session->master_key, session->master_key_length);
406   return hkdf_expand_label(session_key, digest, session_key,
407                            label_to_span(kTLS13LabelResumptionPSK), nonce);
408 }
409 
410 static const char kTLS13LabelExportKeying[] = "exporter";
411 
tls13_export_keying_material(SSL * ssl,Span<uint8_t> out,Span<const uint8_t> secret,Span<const char> label,Span<const uint8_t> context)412 bool tls13_export_keying_material(SSL *ssl, Span<uint8_t> out,
413                                   Span<const uint8_t> secret,
414                                   Span<const char> label,
415                                   Span<const uint8_t> context) {
416   if (secret.empty()) {
417     assert(0);
418     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
419     return false;
420   }
421 
422   const EVP_MD *digest = ssl_session_get_digest(SSL_get_session(ssl));
423 
424   uint8_t hash_buf[EVP_MAX_MD_SIZE];
425   uint8_t export_context_buf[EVP_MAX_MD_SIZE];
426   unsigned hash_len;
427   unsigned export_context_len;
428   if (!EVP_Digest(context.data(), context.size(), hash_buf, &hash_len, digest,
429                   nullptr) ||
430       !EVP_Digest(nullptr, 0, export_context_buf, &export_context_len, digest,
431                   nullptr)) {
432     return false;
433   }
434 
435   auto hash = MakeConstSpan(hash_buf, hash_len);
436   auto export_context = MakeConstSpan(export_context_buf, export_context_len);
437   uint8_t derived_secret_buf[EVP_MAX_MD_SIZE];
438   auto derived_secret = MakeSpan(derived_secret_buf, EVP_MD_size(digest));
439   return hkdf_expand_label(derived_secret, digest, secret, label,
440                            export_context) &&
441          hkdf_expand_label(out, digest, derived_secret,
442                            label_to_span(kTLS13LabelExportKeying), hash);
443 }
444 
445 static const char kTLS13LabelPSKBinder[] = "res binder";
446 
tls13_psk_binder(uint8_t * out,size_t * out_len,uint16_t version,const EVP_MD * digest,Span<const uint8_t> psk,Span<const uint8_t> context)447 static bool tls13_psk_binder(uint8_t *out, size_t *out_len, uint16_t version,
448                              const EVP_MD *digest, Span<const uint8_t> psk,
449                              Span<const uint8_t> context) {
450   uint8_t binder_context[EVP_MAX_MD_SIZE];
451   unsigned binder_context_len;
452   if (!EVP_Digest(NULL, 0, binder_context, &binder_context_len, digest, NULL)) {
453     return false;
454   }
455 
456   uint8_t early_secret[EVP_MAX_MD_SIZE] = {0};
457   size_t early_secret_len;
458   if (!HKDF_extract(early_secret, &early_secret_len, digest, psk.data(),
459                     psk.size(), NULL, 0)) {
460     return false;
461   }
462 
463   uint8_t binder_key_buf[EVP_MAX_MD_SIZE] = {0};
464   auto binder_key = MakeSpan(binder_key_buf, EVP_MD_size(digest));
465   if (!hkdf_expand_label(binder_key, digest,
466                          MakeConstSpan(early_secret, early_secret_len),
467                          label_to_span(kTLS13LabelPSKBinder),
468                          MakeConstSpan(binder_context, binder_context_len)) ||
469       !tls13_verify_data(out, out_len, digest, version, binder_key, context)) {
470     return false;
471   }
472 
473   assert(*out_len == EVP_MD_size(digest));
474   return true;
475 }
476 
hash_transcript_and_truncated_client_hello(SSL_HANDSHAKE * hs,uint8_t * out,size_t * out_len,const EVP_MD * digest,Span<const uint8_t> client_hello,size_t binders_len)477 static bool hash_transcript_and_truncated_client_hello(
478     SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, const EVP_MD *digest,
479     Span<const uint8_t> client_hello, size_t binders_len) {
480   // Truncate the ClientHello.
481   if (binders_len + 2 < binders_len || client_hello.size() < binders_len + 2) {
482     return false;
483   }
484   client_hello = client_hello.subspan(0, client_hello.size() - binders_len - 2);
485 
486   ScopedEVP_MD_CTX ctx;
487   unsigned len;
488   if (!hs->transcript.CopyToHashContext(ctx.get(), digest) ||
489       !EVP_DigestUpdate(ctx.get(), client_hello.data(), client_hello.size()) ||
490       !EVP_DigestFinal_ex(ctx.get(), out, &len)) {
491     return false;
492   }
493 
494   *out_len = len;
495   return true;
496 }
497 
tls13_write_psk_binder(SSL_HANDSHAKE * hs,Span<uint8_t> msg)498 bool tls13_write_psk_binder(SSL_HANDSHAKE *hs, Span<uint8_t> msg) {
499   SSL *const ssl = hs->ssl;
500   const EVP_MD *digest = ssl_session_get_digest(ssl->session.get());
501   size_t hash_len = EVP_MD_size(digest);
502 
503   ScopedEVP_MD_CTX ctx;
504   uint8_t context[EVP_MAX_MD_SIZE];
505   size_t context_len;
506   uint8_t verify_data[EVP_MAX_MD_SIZE];
507   size_t verify_data_len;
508   if (!hash_transcript_and_truncated_client_hello(
509           hs, context, &context_len, digest, msg,
510           1 /* length prefix */ + hash_len) ||
511       !tls13_psk_binder(verify_data, &verify_data_len,
512                         ssl->session->ssl_version, digest,
513                         MakeConstSpan(ssl->session->master_key,
514                                       ssl->session->master_key_length),
515                         MakeConstSpan(context, context_len)) ||
516       verify_data_len != hash_len) {
517     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
518     return false;
519   }
520 
521   OPENSSL_memcpy(msg.data() + msg.size() - verify_data_len, verify_data,
522                  verify_data_len);
523   return true;
524 }
525 
tls13_verify_psk_binder(SSL_HANDSHAKE * hs,SSL_SESSION * session,const SSLMessage & msg,CBS * binders)526 bool tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
527                              const SSLMessage &msg, CBS *binders) {
528   uint8_t context[EVP_MAX_MD_SIZE];
529   size_t context_len;
530   uint8_t verify_data[EVP_MAX_MD_SIZE];
531   size_t verify_data_len;
532   CBS binder;
533   if (!hash_transcript_and_truncated_client_hello(hs, context, &context_len,
534                                                   hs->transcript.Digest(),
535                                                   msg.raw, CBS_len(binders)) ||
536       !tls13_psk_binder(
537           verify_data, &verify_data_len, hs->ssl->version,
538           hs->transcript.Digest(),
539           MakeConstSpan(session->master_key, session->master_key_length),
540           MakeConstSpan(context, context_len)) ||
541       // We only consider the first PSK, so compare against the first binder.
542       !CBS_get_u8_length_prefixed(binders, &binder)) {
543     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
544     return false;
545   }
546 
547   bool binder_ok =
548       CBS_len(&binder) == verify_data_len &&
549       CRYPTO_memcmp(CBS_data(&binder), verify_data, verify_data_len) == 0;
550 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
551   binder_ok = true;
552 #endif
553   if (!binder_ok) {
554     OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
555     return false;
556   }
557 
558   return true;
559 }
560 
561 BSSL_NAMESPACE_END
562