Search in sources :

Example 1 with ClientSecureChannel

use of org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel in project milo by eclipse.

the class ChunkSerializationTest method testSymmetric4096.

@Test
public void testSymmetric4096() throws Exception {
    ChannelParameters parameters = defaultParameters;
    ChunkEncoder encoder = new ChunkEncoder(parameters);
    ChunkDecoder decoder = new ChunkDecoder(parameters, EncodingLimits.DEFAULT);
    SecureChannel[] channels = generateChannels4096();
    ClientSecureChannel clientChannel = (ClientSecureChannel) channels[0];
    ServerSecureChannel serverChannel = (ServerSecureChannel) channels[1];
    clientChannel.attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).setIfAbsent(new LongSequence(1L, UInteger.MAX_VALUE));
    LongSequence requestId = clientChannel.attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).get();
    for (int messageSize = 0; messageSize < 1024; messageSize++) {
        byte[] messageBytes = new byte[messageSize];
        for (int i = 0; i < messageBytes.length; i++) {
            messageBytes[i] = (byte) i;
        }
        ByteBuf messageBuffer = BufferUtil.pooledBuffer().writeBytes(messageBytes);
        List<ByteBuf> chunkBuffers = new ArrayList<>();
        try {
            ChunkEncoder.EncodedMessage message = encoder.encodeSymmetric(clientChannel, requestId.getAndIncrement(), messageBuffer, MessageType.OpenSecureChannel);
            chunkBuffers.addAll(message.getMessageChunks());
        } catch (MessageEncodeException e) {
            fail("encoding error", e);
        }
        try {
            ChunkDecoder.DecodedMessage decodedMessage = decoder.decodeSymmetric(serverChannel, chunkBuffers);
            ByteBuf message = decodedMessage.getMessage();
            messageBuffer.readerIndex(0);
            assertEquals(message, messageBuffer);
            ReferenceCountUtil.release(message);
            ReferenceCountUtil.release(messageBuffer);
        } catch (Throwable t) {
            fail("decoding error", t);
        }
    }
}
Also used : ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) ChannelParameters(org.eclipse.milo.opcua.stack.core.channel.ChannelParameters) ChunkEncoder(org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ArrayList(java.util.ArrayList) ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) SecureChannel(org.eclipse.milo.opcua.stack.core.channel.SecureChannel) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ByteBuf(io.netty.buffer.ByteBuf) ChunkDecoder(org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder) LongSequence(org.eclipse.milo.opcua.stack.core.util.LongSequence) MessageEncodeException(org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException) Test(org.testng.annotations.Test)

Example 2 with ClientSecureChannel

use of org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel in project milo by eclipse.

the class ChunkSerializationTest method testAsymmetric4096.

@Test
public void testAsymmetric4096() throws Exception {
    ChannelParameters parameters = defaultParameters;
    ChunkEncoder encoder = new ChunkEncoder(parameters);
    ChunkDecoder decoder = new ChunkDecoder(parameters, EncodingLimits.DEFAULT);
    SecureChannel[] channels = generateChannels4096();
    ClientSecureChannel clientChannel = (ClientSecureChannel) channels[0];
    ServerSecureChannel serverChannel = (ServerSecureChannel) channels[1];
    clientChannel.attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).setIfAbsent(new LongSequence(1L, UInteger.MAX_VALUE));
    LongSequence requestId = clientChannel.attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).get();
    for (int messageSize = 0; messageSize < 512; messageSize++) {
        byte[] messageBytes = new byte[messageSize];
        for (int i = 0; i < messageBytes.length; i++) {
            messageBytes[i] = (byte) i;
        }
        ByteBuf messageBuffer = BufferUtil.pooledBuffer().writeBytes(messageBytes);
        List<ByteBuf> chunkBuffers = new ArrayList<>();
        try {
            ChunkEncoder.EncodedMessage message = encoder.encodeAsymmetric(clientChannel, requestId.getAndIncrement(), messageBuffer, MessageType.OpenSecureChannel);
            chunkBuffers.addAll(message.getMessageChunks());
        } catch (MessageEncodeException e) {
            fail("encoding error", e);
        }
        try {
            ChunkDecoder.DecodedMessage decodedMessage = decoder.decodeAsymmetric(serverChannel, chunkBuffers);
            ByteBuf message = decodedMessage.getMessage();
            messageBuffer.readerIndex(0);
            assertEquals(message, messageBuffer);
            ReferenceCountUtil.release(message);
            ReferenceCountUtil.release(messageBuffer);
        } catch (Throwable t) {
            fail("decoding error", t);
        }
    }
}
Also used : ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) ChannelParameters(org.eclipse.milo.opcua.stack.core.channel.ChannelParameters) ChunkEncoder(org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ArrayList(java.util.ArrayList) ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) SecureChannel(org.eclipse.milo.opcua.stack.core.channel.SecureChannel) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ByteBuf(io.netty.buffer.ByteBuf) ChunkDecoder(org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder) LongSequence(org.eclipse.milo.opcua.stack.core.util.LongSequence) MessageEncodeException(org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException) Test(org.testng.annotations.Test)

Example 3 with ClientSecureChannel

use of org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel in project milo by eclipse.

the class ChunkSerializationTest method testSymmetricMessage.

@Test(dataProvider = "getSymmetricSecurityParameters")
public void testSymmetricMessage(SecurityPolicy securityPolicy, MessageSecurityMode messageSecurity) throws Exception {
    logger.info("Symmetric chunk serialization, " + "securityPolicy={}, messageSecurityMode={}", securityPolicy, messageSecurity);
    ChannelParameters[] channelParameters = { smallParameters, defaultParameters, unlimitedChunkCountParameters, unlimitedMessageSizeParameters };
    for (ChannelParameters parameters : channelParameters) {
        int[] messageSizes = new int[] { 128, parameters.getRemoteMaxMessageSize() };
        for (int messageSize : messageSizes) {
            ChunkEncoder encoder = new ChunkEncoder(parameters);
            ChunkDecoder decoder = new ChunkDecoder(parameters, EncodingLimits.DEFAULT);
            SecureChannel[] channels = generateChannels(securityPolicy, messageSecurity);
            ClientSecureChannel clientChannel = (ClientSecureChannel) channels[0];
            ServerSecureChannel serverChannel = (ServerSecureChannel) channels[1];
            clientChannel.attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).setIfAbsent(new LongSequence(1L, UInteger.MAX_VALUE));
            LongSequence requestId = clientChannel.attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).get();
            byte[] messageBytes = new byte[messageSize];
            for (int i = 0; i < messageBytes.length; i++) {
                messageBytes[i] = (byte) i;
            }
            ByteBuf messageBuffer = BufferUtil.pooledBuffer().writeBytes(messageBytes);
            List<ByteBuf> chunkBuffers = new ArrayList<>();
            try {
                ChunkEncoder.EncodedMessage message = encoder.encodeSymmetric(clientChannel, requestId.getAndIncrement(), messageBuffer, MessageType.SecureMessage);
                chunkBuffers.addAll(message.getMessageChunks());
            } catch (MessageEncodeException e) {
                fail("encoding error", e);
            }
            try {
                ChunkDecoder.DecodedMessage decodedMessage = decoder.decodeSymmetric(serverChannel, chunkBuffers);
                ByteBuf message = decodedMessage.getMessage();
                messageBuffer.readerIndex(0);
                assertEquals(message, messageBuffer);
                ReferenceCountUtil.release(messageBuffer);
                ReferenceCountUtil.release(message);
            } catch (Throwable t) {
                fail("decoding error", t);
            }
        }
    }
}
Also used : ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) ChannelParameters(org.eclipse.milo.opcua.stack.core.channel.ChannelParameters) ChunkEncoder(org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ArrayList(java.util.ArrayList) ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) SecureChannel(org.eclipse.milo.opcua.stack.core.channel.SecureChannel) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ByteBuf(io.netty.buffer.ByteBuf) ChunkDecoder(org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder) LongSequence(org.eclipse.milo.opcua.stack.core.util.LongSequence) MessageEncodeException(org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException) Test(org.testng.annotations.Test)

Example 4 with ClientSecureChannel

use of org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel in project milo by eclipse.

the class SecureChannelFixture method generateChannels.

protected SecureChannel[] generateChannels(SecurityPolicy securityPolicy, MessageSecurityMode messageSecurity) throws Exception {
    super.setUp();
    ByteString clientNonce = generateNonce(securityPolicy);
    ByteString serverNonce = generateNonce(securityPolicy);
    ClientSecureChannel clientChannel = new ClientSecureChannel(securityPolicy == SecurityPolicy.None ? null : clientKeyPair, securityPolicy == SecurityPolicy.None ? null : clientCertificate, securityPolicy == SecurityPolicy.None ? null : newArrayList(clientCertificate), securityPolicy == SecurityPolicy.None ? null : serverCertificate, securityPolicy == SecurityPolicy.None ? null : newArrayList(serverCertificate), securityPolicy, messageSecurity);
    clientChannel.setLocalNonce(clientNonce);
    clientChannel.setRemoteNonce(serverNonce);
    ServerSecureChannel serverChannel = new ServerSecureChannel();
    serverChannel.setSecurityPolicy(securityPolicy);
    serverChannel.setMessageSecurityMode(messageSecurity);
    serverChannel.setLocalNonce(serverNonce);
    serverChannel.setRemoteNonce(clientNonce);
    if (securityPolicy != SecurityPolicy.None) {
        serverChannel.setKeyPair(serverKeyPair);
        serverChannel.setLocalCertificate(serverCertificate);
        serverChannel.setLocalCertificateChain(new X509Certificate[] { serverCertificate });
        serverChannel.setRemoteCertificate(clientCertificateBytes);
    }
    // Configure the ChannelSecurityToken for clientChannel
    ChannelSecurityToken clientToken = new ChannelSecurityToken(uint(0), uint(1), DateTime.now(), uint(60000));
    if (messageSecurity == MessageSecurityMode.None) {
        clientChannel.setChannelSecurity(new ChannelSecurity(null, clientToken));
    } else {
        ChannelSecurity.SecurityKeys clientSecrets = ChannelSecurity.generateKeyPair(clientChannel, clientChannel.getLocalNonce(), clientChannel.getRemoteNonce());
        clientChannel.setChannelSecurity(new ChannelSecurity(clientSecrets, clientToken));
    }
    // Configure the ChannelSecurityToken for serverChannel
    ChannelSecurityToken serverToken = new ChannelSecurityToken(uint(0), uint(1), DateTime.now(), uint(60000));
    if (messageSecurity == MessageSecurityMode.None) {
        serverChannel.setChannelSecurity(new ChannelSecurity(null, serverToken));
    } else {
        ChannelSecurity.SecurityKeys serverSecrets = ChannelSecurity.generateKeyPair(serverChannel, serverChannel.getRemoteNonce(), serverChannel.getLocalNonce());
        serverChannel.setChannelSecurity(new ChannelSecurity(serverSecrets, serverToken));
    }
    return new SecureChannel[] { clientChannel, serverChannel };
}
Also used : ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ChannelSecurity(org.eclipse.milo.opcua.stack.core.channel.ChannelSecurity) ChannelSecurityToken(org.eclipse.milo.opcua.stack.core.types.structured.ChannelSecurityToken) ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) SecureChannel(org.eclipse.milo.opcua.stack.core.channel.SecureChannel) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel)

Example 5 with ClientSecureChannel

use of org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel in project milo by eclipse.

the class SecureChannelFixture method generateChannels4096.

protected SecureChannel[] generateChannels4096() throws Exception {
    super.setUp();
    SecurityPolicy securityPolicy = SecurityPolicy.Basic256Sha256;
    MessageSecurityMode messageSecurity = MessageSecurityMode.SignAndEncrypt;
    ByteString clientNonce = generateNonce(securityPolicy);
    ByteString serverNonce = generateNonce(securityPolicy);
    ClientSecureChannel clientChannel = new ClientSecureChannel(clientKeyPair4096, clientCertificate4096, newArrayList(clientCertificate4096), serverCertificate4096, newArrayList(serverCertificate4096), securityPolicy, messageSecurity);
    clientChannel.setLocalNonce(clientNonce);
    clientChannel.setRemoteNonce(serverNonce);
    ServerSecureChannel serverChannel = new ServerSecureChannel();
    serverChannel.setSecurityPolicy(securityPolicy);
    serverChannel.setMessageSecurityMode(messageSecurity);
    serverChannel.setLocalNonce(serverNonce);
    serverChannel.setRemoteNonce(clientNonce);
    serverChannel.setKeyPair(serverKeyPair4096);
    serverChannel.setLocalCertificate(serverCertificate4096);
    serverChannel.setLocalCertificateChain(new X509Certificate[] { serverCertificate4096 });
    serverChannel.setRemoteCertificate(clientCertificateBytes4096);
    // Configure the ChannelSecurityToken for clientChannel
    ChannelSecurityToken clientToken = new ChannelSecurityToken(uint(0), uint(1), DateTime.now(), uint(60000));
    ChannelSecurity.SecurityKeys clientSecrets = ChannelSecurity.generateKeyPair(clientChannel, clientChannel.getLocalNonce(), clientChannel.getRemoteNonce());
    clientChannel.setChannelSecurity(new ChannelSecurity(clientSecrets, clientToken));
    // Configure the ChannelSecurityToken for serverChannel
    ChannelSecurityToken serverToken = new ChannelSecurityToken(uint(0), uint(1), DateTime.now(), uint(60000));
    ChannelSecurity.SecurityKeys serverSecrets = ChannelSecurity.generateKeyPair(serverChannel, serverChannel.getRemoteNonce(), serverChannel.getLocalNonce());
    serverChannel.setChannelSecurity(new ChannelSecurity(serverSecrets, serverToken));
    return new SecureChannel[] { clientChannel, serverChannel };
}
Also used : ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) MessageSecurityMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ChannelSecurity(org.eclipse.milo.opcua.stack.core.channel.ChannelSecurity) ChannelSecurityToken(org.eclipse.milo.opcua.stack.core.types.structured.ChannelSecurityToken) ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) SecureChannel(org.eclipse.milo.opcua.stack.core.channel.SecureChannel) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel)

Aggregations

ClientSecureChannel (org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel)6 SecureChannel (org.eclipse.milo.opcua.stack.core.channel.SecureChannel)6 ServerSecureChannel (org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel)6 ByteBuf (io.netty.buffer.ByteBuf)4 ArrayList (java.util.ArrayList)4 ChannelParameters (org.eclipse.milo.opcua.stack.core.channel.ChannelParameters)4 ChunkDecoder (org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder)4 ChunkEncoder (org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder)4 MessageEncodeException (org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException)4 LongSequence (org.eclipse.milo.opcua.stack.core.util.LongSequence)4 Test (org.testng.annotations.Test)4 ChannelSecurity (org.eclipse.milo.opcua.stack.core.channel.ChannelSecurity)2 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)2 ChannelSecurityToken (org.eclipse.milo.opcua.stack.core.types.structured.ChannelSecurityToken)2 SecurityPolicy (org.eclipse.milo.opcua.stack.core.security.SecurityPolicy)1 MessageSecurityMode (org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode)1