MCPcopy Create free account
hub / github.com/secdev/scapy / post_build

Method post_build

scapy/layers/tls/handshake.py:389–451  ·  view source on GitHub ↗
(self, p, pay)

Source from the content-addressed store, hash-verified

387 40))]
388
389 def post_build(self, p, pay):
390 if self.random_bytes is None:
391 p = p[:6] + randstring(32) + p[6 + 32:]
392 # We don't call the post_build function from class _TLSHandshake
393 # to compute the message length because we need that value now
394 # for the HMAC in binder
395 tmp_len = len(p)
396 if self.msglen is None:
397 sz = tmp_len - 4
398 p = struct.pack("!I", (orb(p[0]) << 24) | sz) + p[4:]
399 s = self.tls_session
400 if self.ext:
401 for e in self.ext:
402 if isinstance(e, TLS_Ext_PreSharedKey_CH):
403 if s.client_session_ticket:
404 # For a resumed PSK, the hash function use
405 # to compute the binder must be the same
406 # as the one used to establish the original
407 # connection. For that, we assume that
408 # the ciphersuite associate with the ticket
409 # is given as argument to tlsSession
410 # (see layers/tls/automaton_cli.py for an
411 # example)
412 res_suite = s.tls13_ticket_ciphersuite
413 cs_cls = _tls_cipher_suites_cls[res_suite]
414 hkdf = TLS13_HKDF(cs_cls.hash_alg.name.lower())
415 hash_len = hkdf.hash.digest_size
416 s.compute_tls13_early_secrets(external=False)
417 else:
418 # For out of band PSK, SHA-256 is used as default
419 # hash functions for HKDF
420 hkdf = TLS13_HKDF("sha256")
421 hash_len = hkdf.hash.digest_size
422 s.compute_tls13_early_secrets(external=True)
423
424 # RFC8446 4.2.11.2
425 # "Each entry in the binders list is computed as an HMAC
426 # over a transcript hash (see Section 4.4.1) containing a
427 # partial ClientHello up to and including the
428 # PreSharedKeyExtension.identities field."
429 # PSK Binders field is :
430 # - PSK Binders length (2 bytes)
431 # - First PSK Binder length (1 byte) +
432 # HMAC (hash_len bytes)
433 # The PSK Binder is computed in the same way as the
434 # Finished message with binder_key as BaseKey
435
436 handshake_context = b""
437 if s.tls13_retry:
438 for m in s.handshake_messages:
439 handshake_context += m
440 handshake_context += p[:-hash_len - 3]
441
442 binder_key = s.tls13_derived_secrets["binder_key"]
443 psk_binder = hkdf.compute_verify_data(binder_key,
444 handshake_context)
445
446 # Here, we replaced the last 32 bytes of the packet by the

Callers

nothing calls this directly

Calls 5

compute_verify_dataMethod · 0.95
randstringFunction · 0.90
orbFunction · 0.90
TLS13_HKDFClass · 0.90

Tested by

no test coverage detected