Beispiel #1
0
    def unpack(self, data: bytes) -> bytes:
        ticket_lifetime_int = PackableInt(4, 0)
        data = ticket_lifetime_int.unpack(data)
        self.ticket_lifetime = ticket_lifetime_int.value

        ticket_age_add_int = PackableInt(4, 0)
        data = ticket_age_add_int.unpack(data)
        self.ticket_age_add = ticket_age_add_int.value

        ticket_nonce_vec = Vector(1)
        data = ticket_nonce_vec.unpack(data)
        self.ticket_nonce = ticket_nonce_vec.data

        ticket_vec = Vector(2)
        data = ticket_vec.unpack(data)
        self.ticket = ticket_vec.data

        ext_vec = Vector(2)
        data = ext_vec.unpack(data)
        ext_data: bytes = ext_vec.data

        extensions = []
        while len(ext_data) > 0:
            ext = Extension()
            ext_data = ext.unpack(ext_data)
            extension = Extension.construct(ext.ext_type)
            if extension:
                ext.ext: Vector
                extension.unpack(ext.ext.data)
                ext.ext = extension
            extensions.append(ext)
        self.extensions = tuple(extensions)
        return data
Beispiel #2
0
class HkdfLabel(Protocol):
    def __init__(self, length: int, label: bytes, context: bytes):
        self.length = PackableInt(2, length)
        self.label = Vector(1, b"tls13 " + label)
        self.context = Vector(1, context)

    def pack(self) -> bytes:
        return self.length.pack() + self.label.pack() + self.context.pack()

    def unpack(self, data: bytes) -> bytes:
        pass
Beispiel #3
0
 def pack(self) -> bytes:
     data = BytesIO()
     data.write(ProtocolVersion(self.legacy_record_version).pack())
     data.write(self.random)
     data.write(Vector(1, self.legacy_session_id).pack())
     cipher_suites_data = b"".join(
         (cipher_suite.pack() for cipher_suite in self.cipher_suites))
     data.write(Vector(2, cipher_suites_data).pack())
     legacy_compression_methods_data = b"".join(
         (legacy_compression_method.pack()
          for legacy_compression_method in self.legacy_compression_methods))
     data.write(Vector(1, legacy_compression_methods_data).pack())
     extensions_data = b"".join(
         (extension.pack() for extension in self.extensions))
     data.write(Vector(2, extensions_data).pack())
     return data.getvalue()
Beispiel #4
0
 def unpack(self, data: bytes) -> bytes:
     extension_vec = Vector(2)
     data = extension_vec.unpack(data)
     ext_data: bytes = extension_vec.data
     extensions = []
     while len(ext_data) > 0:
         ext = Extension()
         ext_data = ext.unpack(ext_data)
         extension = Extension.construct(ext.ext_type)
         if extension:
             ext.ext: Vector
             extension.unpack(ext.ext.data)
             ext.ext = extension
         extensions.append(ext)
     extensions = []
     self.extensions = tuple(extensions)
     return data
Beispiel #5
0
    def unpack(self, data: bytes) -> bytes:
        legacy_version_int = ProtocolVersion(0)
        data = legacy_version_int.unpack(data)
        self.legacy_version = legacy_version_int.value

        assert len(data) >= 32, f"no enough bytes to unpack random"
        self.random = data[:32]
        data = data[32:]

        legacy_session_id_echo_vec = Vector(1)
        data = legacy_session_id_echo_vec.unpack(data)
        self.legacy_session_id_echo = legacy_session_id_echo_vec.data

        self.cipher_suite, data = CipherSuite.unpack(data)
        self.legacy_compression_method, data = CompressionMethod.unpack(data)
        extension_vec = Vector(2)
        data = extension_vec.unpack(data)
        ext_data: bytes = extension_vec.data
        extensions = []
        while len(ext_data) > 0:
            ext = Extension()
            ext_data = ext.unpack(ext_data)
            extension = Extension.construct(ext.ext_type)
            if extension:
                ext.ext: Vector
                extension.unpack(ext.ext.data)
                ext.ext = extension
            extensions.append(ext)
        self.extensions = tuple(extensions)
        return data
Beispiel #6
0
 def pack(self) -> bytes:
     data = memoryview(self.data)
     fragments = []
     while True:
         if len(data) > 16384:
             fragments.append(data[:16384])
             data = data[16384:]
         else:
             fragments.append(data)
             break
     return b"".join(
         (self.content_type.pack() + self.legacy_record_version.pack() +
          Vector(2, fragment).pack() for fragment in fragments))
Beispiel #7
0
 def unpack(self, data: bytes) -> bytes:
     cert_req_ctx_vec = Vector(1)
     data = cert_req_ctx_vec.unpack(data)
     self.cert_req_ctx = cert_req_ctx_vec.data
     cert_entries_vec = Vector(3)
     data = cert_entries_vec.unpack(data)
     cert_entries_data: bytes = cert_entries_vec.data
     cert_entries = []
     while len(cert_entries_data) > 0:
         cert_entry = CertificateEntry()
         cert_entries_data = cert_entry.unpack(cert_entries_data)
         cert_entries.append(cert_entry)
     self.cert_entries = tuple(cert_entries)
     return data
Beispiel #8
0
 def unpack(self, data: bytes) -> bytes:
     cert_req_ctx_vvc = Vector(1)
     data = cert_req_ctx_vvc.unpack(data)
     self.certificate_request_context = cert_req_ctx_vvc.data
     extension_vec = Vector(2)
     data = extension_vec.unpack(data)
     ext_data: bytes = extension_vec.data
     extensions = []
     while len(ext_data) > 0:
         ext = Extension()
         ext_data = ext.unpack(ext_data)
         extensions.append(ext)
     self.extensions = tuple(extensions)
     return data
Beispiel #9
0
 def unpack(self, data: bytes) -> bytes:
     cert_data_vec = Vector(3)
     data = cert_data_vec.unpack(data)
     self.cert_data = cert_data_vec.data
     ext_data_vec = Vector(2)
     data = ext_data_vec.unpack(data)
     ext_data: bytes = ext_data_vec.data
     extensions = []
     while len(ext_data) > 0:
         ext = Extension()
         ext_data = ext.unpack(ext_data)
         extensions.append(ext)
     self.extensions = tuple(extensions)
     return data
Beispiel #10
0
 def pack(self) -> bytes:
     return Vector(3, self.cert_data).pack() + Vector(
         2, self.extensions).pack()
Beispiel #11
0
 def pack(self) -> bytes:
     extensions_data: bytes = b"".join(extension.pack()
                                       for extension in self.extensions)
     return Vector(1, self.certificate_request_context).pack() + Vector(
         2, extensions_data).pack()
Beispiel #12
0
 def pack(self) -> bytes:
     return self.handshake_type.pack() + Vector(3,
                                                self.handshake_data).pack()
Beispiel #13
0
 def pack(self) -> bytes:
     return Vector(1, self.binder).pack()
Beispiel #14
0
 def pack(self) -> bytes:
     return self.algorithm.pack() + Vector(2, self.signature).pack()
Beispiel #15
0
 def pack(self) -> bytes:
     return self.name_type.pack() + Vector(2, self.name).pack()
Beispiel #16
0
 def pack(self) -> bytes:
     groups_data = b"".join((group.pack() for group in self.groups))
     return Vector(2, groups_data).pack()
Beispiel #17
0
 def pack(self) -> bytes:
     sig_schemes_data = b"".join((sig_scheme.pack() for sig_scheme in self.sig_schemes))
     return Vector(2, sig_schemes_data).pack()
Beispiel #18
0
 def pack(self) -> bytes:
     return self.ext_type.pack() + Vector(2, self.ext.pack()).pack()
Beispiel #19
0
 def __init__(self, protocol: Protocol = None):
     if protocol is None:
         protocol = Vector(2)
     self.ext = protocol
     self.ext_type: ExtensionType = protocol.type
Beispiel #20
0
 def pack(self) -> bytes:
     return Vector(2, self.server_name_list).pack()
Beispiel #21
0
 def pack(self) -> bytes:
     return Vector(1, self.cert_req_ctx).pack() + Vector(
         3, self.cert_entries).pack()
Beispiel #22
0
 def pack(self) -> bytes:
     return PackableInt(4, self.ticket_lifetime).pack() + PackableInt(4, self.ticket_age_add).pack() \
            + Vector(1, self.ticket_nonce).pack() + Vector(2, self.ticket).pack() + Vector(2, self.extensions).pack()
Beispiel #23
0
 def __init__(self, length: int, label: bytes, context: bytes):
     self.length = PackableInt(2, length)
     self.label = Vector(1, b"tls13 " + label)
     self.context = Vector(1, context)
Beispiel #24
0
 def pack(self) -> bytes:
     identities_data = b"".join((identity.pack() for identity in self.identities))
     binders_data = b"".join((binder.pack() for binder in self.binders))
     return Vector(2, identities_data).pack() + Vector(2, binders_data).pack()
Beispiel #25
0
 def unpack(self, data: bytes) -> bytes:
     self.algorithm, data = SignatureScheme.unpack(data)
     signature_vec = Vector(2)
     data = signature_vec.unpack(data)
     self.signature = signature_vec.data
     return data
Beispiel #26
0
 def unpack(self, data: bytes) -> bytes:
     self.key_exchange.group, data = NamedGroup.unpack(data)
     key_exchange_vec = Vector(2)
     data = key_exchange_vec.unpack(data)
     self.key_exchange.unpack(key_exchange_vec.data)
     return data
Beispiel #27
0
 def pack(self) -> bytes:
     key_shares_data = b"".join((key_share.pack() for key_share in self.key_shares))
     return Vector(2, key_shares_data).pack()
Beispiel #28
0
 def pack(self) -> bytes:
     extensions_data: bytes = b"".join(extension.pack()
                                       for extension in self.extensions)
     return Vector(2, extensions_data).pack()
Beispiel #29
0
 def pack(self) -> bytes:
     return Vector(1, self.psk_key_exchange_modes).pack()
Beispiel #30
0
 def pack(self) -> bytes:
     return Vector(2, self.identity).pack() + PackableInt(4, self.obfuscated_ticket_age).pack()