Beispiel #1
0
def pad_and_encrypt(message,
                    connection,
                    cert,
                    padding=None,
                    ciphertext_manipulator=None):
    """ Make a message sendable by applying padding and encryption

    :param message: message object to send
    :param connection: connection to the OPC UA server
    :param cert: server certificate to use for manipulations
    :param padding: padding function
    :param ciphertext_manipulator:  manipulation function for the ciphertext
    :return: binary request that can be send to the server
    """
    token_id = connection.channel.SecurityToken.TokenId
    chunks = MessageChunk.message_to_chunks(
        connection.security_policy,
        message,
        connection._max_chunk_size,
        message_type=ua.MessageType.SecureOpen,
        channel_id=connection.channel.SecurityToken.ChannelId,
        request_id=1,
        token_id=token_id)
    chunk = chunks[0]

    chunk.SequenceHeader.SequenceNumber = 1
    return to_binary(chunk,
                     cert,
                     padding=padding,
                     ciphertext_manipulator=ciphertext_manipulator)
Beispiel #2
0
    def test_message_chunk(self):
        pol = ua.SecurityPolicy()
        chunks = MessageChunk.message_to_chunks(pol, b'123', 65536)
        self.assertEqual(len(chunks), 1)
        seq = 0
        for chunk in chunks:
            seq += 1
            chunk.SequenceHeader.SequenceNumber = seq
        chunk2 = MessageChunk.from_binary(pol, ua.utils.Buffer(chunks[0].to_binary()))
        self.assertEqual(chunks[0].to_binary(), chunk2.to_binary())

        # for policy None, MessageChunk overhead is 12+4+8 = 24 bytes
        # Let's pack 11 bytes into 28-byte chunks. The message must be split as 4+4+3
        chunks = MessageChunk.message_to_chunks(pol, b'12345678901', 28)
        self.assertEqual(len(chunks), 3)
        self.assertEqual(chunks[0].Body, b'1234')
        self.assertEqual(chunks[1].Body, b'5678')
        self.assertEqual(chunks[2].Body, b'901')
        for chunk in chunks:
            seq += 1
            chunk.SequenceHeader.SequenceNumber = seq
            self.assertTrue(len(chunk.to_binary()) <= 28)
Beispiel #3
0
    def test_message_chunk(self):
        pol = ua.SecurityPolicy()
        chunks = MessageChunk.message_to_chunks(pol, b'123', 65536)
        self.assertEqual(len(chunks), 1)
        seq = 0
        for chunk in chunks:
            seq += 1
            chunk.SequenceHeader.SequenceNumber = seq
        chunk2 = MessageChunk.from_binary(pol, ua.utils.Buffer(chunks[0].to_binary()))
        self.assertEqual(chunks[0].to_binary(), chunk2.to_binary())

        # for policy None, MessageChunk overhead is 12+4+8 = 24 bytes
        # Let's pack 11 bytes into 28-byte chunks. The message must be split as 4+4+3
        chunks = MessageChunk.message_to_chunks(pol, b'12345678901', 28)
        self.assertEqual(len(chunks), 3)
        self.assertEqual(chunks[0].Body, b'1234')
        self.assertEqual(chunks[1].Body, b'5678')
        self.assertEqual(chunks[2].Body, b'901')
        for chunk in chunks:
            seq += 1
            chunk.SequenceHeader.SequenceNumber = seq
            self.assertTrue(len(chunk.to_binary()) <= 28)