Example #1
0
    def __recv(self, bytes_buffer):
        # print("_recv", bytes_buffer.peek())
        # raise Exception("Need more data!!!")

        wrapper = Wrapper.deserialize(bytes_buffer)
        while wrapper.record_header.size > len(wrapper.payload):
            # print("Wrapper read", wrapper.record_header.size - len(wrapper.payload))
            wrapper.payload += self.socket.recv(
                wrapper.record_header.size - len(wrapper.payload)
            )

        recdata = wrapper.record_header.serialize()
        authtag = wrapper.auth_tag

        ciphertext = wrapper.encrypted_data

        decryptor = AES.new(
            self.application_keys.server_key,
            AES.MODE_GCM,
            xor_iv(self.application_keys.server_iv, self.application_recv_counter),
        )
        decryptor.update(recdata)

        plaintext = decryptor.decrypt(bytes(ciphertext))

        decryptor.verify(authtag)
        self.application_recv_counter += 1

        return plaintext
Example #2
0
        def parse_wrapper(bytes_buffer):
            wrapper = Wrapper.deserialize(bytes_buffer)
            while wrapper.record_header.size > len(wrapper.payload):
                wrapper.payload += self.socket.recv(
                    wrapper.record_header.size - len(wrapper.payload)
                )

            recdata = wrapper.record_header.serialize()
            authtag = wrapper.auth_tag

            ciphertext = wrapper.encrypted_data

            decryptor = AES.new(
                self.handshake_keys.server_key,
                AES.MODE_GCM,
                xor_iv(self.handshake_keys.server_iv, self.handshake_recv_counter),
            )
            decryptor.update(recdata)

            plaintext = decryptor.decrypt(bytes(ciphertext))
            self.handshake_recv_counter += 1

            decryptor.verify(authtag)
            return plaintext[:-1]