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