Search in sources :

Example 1 with MessageAbortException

use of org.eclipse.milo.opcua.stack.core.channel.MessageAbortException in project milo by eclipse.

the class UascServerAsymmetricHandler method onOpenSecureChannel.

private void onOpenSecureChannel(ChannelHandlerContext ctx, ByteBuf buffer) throws UaException {
    // Skip messageType
    buffer.skipBytes(3);
    char chunkType = (char) buffer.readByte();
    if (chunkType == 'A') {
        chunkBuffers.forEach(ByteBuf::release);
        chunkBuffers.clear();
        headerRef.set(null);
    } else {
        // Skip messageSize
        buffer.skipBytes(4);
        final long secureChannelId = buffer.readUnsignedIntLE();
        final AsymmetricSecurityHeader header = AsymmetricSecurityHeader.decode(buffer, stackServer.getConfig().getEncodingLimits());
        if (!headerRef.compareAndSet(null, header)) {
            if (!header.equals(headerRef.get())) {
                throw new UaException(StatusCodes.Bad_SecurityChecksFailed, "subsequent AsymmetricSecurityHeader did not match");
            }
        }
        if (secureChannelId != 0) {
            if (secureChannel == null) {
                throw new UaException(StatusCodes.Bad_TcpSecureChannelUnknown, "unknown secure channel id: " + secureChannelId);
            }
            if (secureChannelId != secureChannel.getChannelId()) {
                throw new UaException(StatusCodes.Bad_TcpSecureChannelUnknown, "unknown secure channel id: " + secureChannelId);
            }
        }
        if (secureChannel == null) {
            secureChannel = new ServerSecureChannel();
            secureChannel.setChannelId(stackServer.getNextChannelId());
            String securityPolicyUri = header.getSecurityPolicyUri();
            SecurityPolicy securityPolicy = SecurityPolicy.fromUri(securityPolicyUri);
            secureChannel.setSecurityPolicy(securityPolicy);
            if (securityPolicy != SecurityPolicy.None) {
                secureChannel.setRemoteCertificate(header.getSenderCertificate().bytesOrEmpty());
                CertificateValidator certificateValidator = stackServer.getConfig().getCertificateValidator();
                certificateValidator.validateCertificateChain(secureChannel.getRemoteCertificateChain());
                CertificateManager certificateManager = stackServer.getConfig().getCertificateManager();
                Optional<X509Certificate[]> localCertificateChain = certificateManager.getCertificateChain(header.getReceiverThumbprint());
                Optional<KeyPair> keyPair = certificateManager.getKeyPair(header.getReceiverThumbprint());
                if (localCertificateChain.isPresent() && keyPair.isPresent()) {
                    X509Certificate[] chain = localCertificateChain.get();
                    secureChannel.setLocalCertificate(chain[0]);
                    secureChannel.setLocalCertificateChain(chain);
                    secureChannel.setKeyPair(keyPair.get());
                } else {
                    throw new UaException(StatusCodes.Bad_SecurityChecksFailed, "no certificate for provided thumbprint");
                }
            }
        }
        int chunkSize = buffer.readerIndex(0).readableBytes();
        if (chunkSize > maxChunkSize) {
            throw new UaException(StatusCodes.Bad_TcpMessageTooLarge, String.format("max chunk size exceeded (%s)", maxChunkSize));
        }
        chunkBuffers.add(buffer.retain());
        if (maxChunkCount > 0 && chunkBuffers.size() > maxChunkCount) {
            throw new UaException(StatusCodes.Bad_TcpMessageTooLarge, String.format("max chunk count exceeded (%s)", maxChunkCount));
        }
        if (chunkType == 'F') {
            final List<ByteBuf> buffersToDecode = chunkBuffers;
            chunkBuffers = new ArrayList<>();
            headerRef.set(null);
            serializationQueue.decode((binaryDecoder, chunkDecoder) -> {
                ByteBuf message;
                long requestId;
                try {
                    ChunkDecoder.DecodedMessage decodedMessage = chunkDecoder.decodeAsymmetric(secureChannel, buffersToDecode);
                    message = decodedMessage.getMessage();
                    requestId = decodedMessage.getRequestId();
                } catch (MessageAbortException e) {
                    logger.warn("Received message abort chunk; error={}, reason={}", e.getStatusCode(), e.getMessage());
                    return;
                } catch (MessageDecodeException e) {
                    logger.error("Error decoding asymmetric message", e);
                    ctx.close();
                    return;
                }
                try {
                    OpenSecureChannelRequest request = (OpenSecureChannelRequest) binaryDecoder.setBuffer(message).readMessage(null);
                    logger.debug("Received OpenSecureChannelRequest ({}, id={}).", request.getRequestType(), secureChannelId);
                    sendOpenSecureChannelResponse(ctx, requestId, request);
                } catch (Throwable t) {
                    logger.error("Error decoding OpenSecureChannelRequest", t);
                    ctx.close();
                } finally {
                    message.release();
                    buffersToDecode.clear();
                }
            });
        }
    }
}
Also used : ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) KeyPair(java.security.KeyPair) UaException(org.eclipse.milo.opcua.stack.core.UaException) CertificateValidator(org.eclipse.milo.opcua.stack.core.security.CertificateValidator) CertificateManager(org.eclipse.milo.opcua.stack.core.security.CertificateManager) MessageAbortException(org.eclipse.milo.opcua.stack.core.channel.MessageAbortException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) X509Certificate(java.security.cert.X509Certificate) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) OpenSecureChannelRequest(org.eclipse.milo.opcua.stack.core.types.structured.OpenSecureChannelRequest) ChunkDecoder(org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) MessageDecodeException(org.eclipse.milo.opcua.stack.core.channel.MessageDecodeException) AsymmetricSecurityHeader(org.eclipse.milo.opcua.stack.core.channel.headers.AsymmetricSecurityHeader)

Example 2 with MessageAbortException

use of org.eclipse.milo.opcua.stack.core.channel.MessageAbortException in project milo by eclipse.

the class UascServerSymmetricHandler method onSecureMessage.

private void onSecureMessage(ChannelHandlerContext ctx, ByteBuf buffer) throws UaException {
    // Skip messageType
    buffer.skipBytes(3);
    char chunkType = (char) buffer.readByte();
    if (chunkType == 'A') {
        chunkBuffers.forEach(ByteBuf::release);
        chunkBuffers.clear();
    } else {
        // Skip messageSize
        buffer.skipBytes(4);
        long secureChannelId = buffer.readUnsignedIntLE();
        if (secureChannelId != secureChannel.getChannelId()) {
            throw new UaException(StatusCodes.Bad_SecureChannelIdInvalid, "invalid secure channel id: " + secureChannelId);
        }
        int chunkSize = buffer.readerIndex(0).readableBytes();
        if (chunkSize > maxChunkSize) {
            throw new UaException(StatusCodes.Bad_TcpMessageTooLarge, String.format("max chunk size exceeded (%s)", maxChunkSize));
        }
        chunkBuffers.add(buffer.retain());
        if (maxChunkCount > 0 && chunkBuffers.size() > maxChunkCount) {
            throw new UaException(StatusCodes.Bad_TcpMessageTooLarge, String.format("max chunk count exceeded (%s)", maxChunkCount));
        }
        if (chunkType == 'F') {
            final List<ByteBuf> buffersToDecode = chunkBuffers;
            chunkBuffers = new ArrayList<>();
            serializationQueue.decode((binaryDecoder, chunkDecoder) -> {
                ByteBuf message;
                long requestId;
                try {
                    ChunkDecoder.DecodedMessage decodedMessage = chunkDecoder.decodeSymmetric(secureChannel, buffersToDecode);
                    message = decodedMessage.getMessage();
                    requestId = decodedMessage.getRequestId();
                } catch (MessageAbortException e) {
                    logger.warn("Received message abort chunk; error={}, reason={}", e.getStatusCode(), e.getMessage());
                    return;
                } catch (MessageDecodeException e) {
                    logger.error("Error decoding symmetric message", e);
                    ctx.close();
                    return;
                }
                try {
                    UaRequestMessage request = (UaRequestMessage) binaryDecoder.setBuffer(message).readMessage(null);
                    String endpointUrl = ctx.channel().attr(UascServerHelloHandler.ENDPOINT_URL_KEY).get();
                    EndpointDescription endpoint = ctx.channel().attr(UascServerAsymmetricHandler.ENDPOINT_KEY).get();
                    String path = EndpointUtil.getPath(endpointUrl);
                    InetSocketAddress remoteSocketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
                    ServiceRequest serviceRequest = new ServiceRequest(stackServer, request, endpoint, secureChannel.getChannelId(), remoteSocketAddress.getAddress(), secureChannel.getRemoteCertificateBytes());
                    serviceRequest.getFuture().whenComplete((response, fault) -> {
                        if (response != null) {
                            sendServiceResponse(ctx, requestId, request, response);
                        } else {
                            UInteger requestHandle = request.getRequestHeader().getRequestHandle();
                            sendServiceFault(ctx, requestId, requestHandle, fault);
                        }
                    });
                    stackServer.onServiceRequest(path, serviceRequest);
                } catch (UaSerializationException e) {
                    logger.error("Error decoding UaRequestMessage", e);
                    sendServiceFault(ctx, requestId, uint(0), e);
                } catch (Throwable t) {
                    logger.error("Unexpected error servicing UaRequestMessage", t);
                    long statusCode = UaException.extractStatusCode(t).map(StatusCode::getValue).orElse(StatusCodes.Bad_UnexpectedError);
                    sendServiceFault(ctx, requestId, uint(0), new UaException(statusCode, t));
                } finally {
                    message.release();
                    buffersToDecode.clear();
                }
            });
        }
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) UaRequestMessage(org.eclipse.milo.opcua.stack.core.serialization.UaRequestMessage) UaException(org.eclipse.milo.opcua.stack.core.UaException) InetSocketAddress(java.net.InetSocketAddress) MessageAbortException(org.eclipse.milo.opcua.stack.core.channel.MessageAbortException) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) ServiceRequest(org.eclipse.milo.opcua.stack.server.services.ServiceRequest) ChunkDecoder(org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) MessageDecodeException(org.eclipse.milo.opcua.stack.core.channel.MessageDecodeException)

Example 3 with MessageAbortException

use of org.eclipse.milo.opcua.stack.core.channel.MessageAbortException in project milo by eclipse.

the class UascClientMessageHandler method onOpenSecureChannel.

private void onOpenSecureChannel(ChannelHandlerContext ctx, ByteBuf buffer) throws UaException {
    if (secureChannelTimeout != null) {
        if (secureChannelTimeout.cancel()) {
            logger.debug("OpenSecureChannel timeout canceled");
            secureChannelTimeout = null;
        } else {
            logger.warn("timed out waiting for secure channel");
            handshakeFuture.completeExceptionally(new UaException(StatusCodes.Bad_Timeout, "timed out waiting for secure channel"));
            ctx.close();
            return;
        }
    }
    // skip messageType, chunkType, messageSize, secureChannelId
    buffer.skipBytes(3 + 1 + 4 + 4);
    AsymmetricSecurityHeader securityHeader = AsymmetricSecurityHeader.decode(buffer, config.getEncodingLimits());
    if (headerRef.compareAndSet(null, securityHeader)) {
        // first time we've received the header; validate and verify the server certificate
        CertificateValidator certificateValidator = config.getCertificateValidator();
        SecurityPolicy securityPolicy = SecurityPolicy.fromUri(securityHeader.getSecurityPolicyUri());
        if (securityPolicy != SecurityPolicy.None) {
            ByteString serverCertificateBytes = securityHeader.getSenderCertificate();
            List<X509Certificate> serverCertificateChain = CertificateUtil.decodeCertificates(serverCertificateBytes.bytesOrEmpty());
            certificateValidator.validateCertificateChain(serverCertificateChain);
        }
    } else {
        if (!securityHeader.equals(headerRef.get())) {
            throw new UaException(StatusCodes.Bad_SecurityChecksFailed, "subsequent AsymmetricSecurityHeader did not match");
        }
    }
    if (accumulateChunk(buffer)) {
        final List<ByteBuf> buffersToDecode = chunkBuffers;
        chunkBuffers = new ArrayList<>(maxChunkCount);
        serializationQueue.decode((binaryDecoder, chunkDecoder) -> {
            ByteBuf message;
            try {
                ChunkDecoder.DecodedMessage decodedMessage = chunkDecoder.decodeAsymmetric(secureChannel, buffersToDecode);
                message = decodedMessage.getMessage();
            } catch (MessageAbortException e) {
                logger.warn("Received message abort chunk; error={}, reason={}", e.getStatusCode(), e.getMessage());
                return;
            } catch (MessageDecodeException e) {
                logger.error("Error decoding asymmetric message", e);
                handshakeFuture.completeExceptionally(e);
                ctx.close();
                return;
            }
            try {
                UaResponseMessage response = (UaResponseMessage) binaryDecoder.setBuffer(message).readMessage(null);
                StatusCode serviceResult = response.getResponseHeader().getServiceResult();
                if (serviceResult.isGood()) {
                    OpenSecureChannelResponse oscr = (OpenSecureChannelResponse) response;
                    secureChannel.setChannelId(oscr.getSecurityToken().getChannelId().longValue());
                    logger.debug("Received OpenSecureChannelResponse.");
                    NonceUtil.validateNonce(oscr.getServerNonce(), secureChannel.getSecurityPolicy());
                    installSecurityToken(ctx, oscr);
                    handshakeFuture.complete(secureChannel);
                } else {
                    ServiceFault serviceFault = (response instanceof ServiceFault) ? (ServiceFault) response : new ServiceFault(response.getResponseHeader());
                    handshakeFuture.completeExceptionally(new UaServiceFaultException(serviceFault));
                    ctx.close();
                }
            } catch (Throwable t) {
                logger.error("Error decoding OpenSecureChannelResponse", t);
                handshakeFuture.completeExceptionally(t);
                ctx.close();
            } finally {
                message.release();
            }
        });
    }
}
Also used : UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) OpenSecureChannelResponse(org.eclipse.milo.opcua.stack.core.types.structured.OpenSecureChannelResponse) UaException(org.eclipse.milo.opcua.stack.core.UaException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) CertificateValidator(org.eclipse.milo.opcua.stack.core.security.CertificateValidator) MessageAbortException(org.eclipse.milo.opcua.stack.core.channel.MessageAbortException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) X509Certificate(java.security.cert.X509Certificate) UaServiceFaultException(org.eclipse.milo.opcua.stack.core.UaServiceFaultException) ChunkDecoder(org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder) ServiceFault(org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) MessageDecodeException(org.eclipse.milo.opcua.stack.core.channel.MessageDecodeException) AsymmetricSecurityHeader(org.eclipse.milo.opcua.stack.core.channel.headers.AsymmetricSecurityHeader)

Example 4 with MessageAbortException

use of org.eclipse.milo.opcua.stack.core.channel.MessageAbortException in project milo by eclipse.

the class UascClientMessageHandler method onSecureMessage.

private void onSecureMessage(ChannelHandlerContext ctx, ByteBuf buffer) throws UaException {
    // skip messageType, chunkType, messageSize
    buffer.skipBytes(3 + 1 + 4);
    long secureChannelId = buffer.readUnsignedIntLE();
    if (secureChannelId != secureChannel.getChannelId()) {
        throw new UaException(StatusCodes.Bad_SecureChannelIdInvalid, "invalid secure channel id: " + secureChannelId);
    }
    if (accumulateChunk(buffer)) {
        final List<ByteBuf> buffersToDecode = chunkBuffers;
        chunkBuffers = new ArrayList<>(maxChunkCount);
        serializationQueue.decode((binaryDecoder, chunkDecoder) -> {
            ByteBuf message;
            long requestId;
            try {
                ChunkDecoder.DecodedMessage decodedMessage = chunkDecoder.decodeSymmetric(secureChannel, buffersToDecode);
                message = decodedMessage.getMessage();
                requestId = decodedMessage.getRequestId();
            } catch (MessageAbortException e) {
                logger.warn("Received message abort chunk; error={}, reason={}", e.getStatusCode(), e.getMessage());
                UaTransportRequest request = pending.remove(e.getRequestId());
                if (request != null) {
                    request.getFuture().completeExceptionally(e);
                } else {
                    logger.warn("No pending request for requestId={}", e.getRequestId());
                }
                return;
            } catch (MessageDecodeException e) {
                logger.error("Error decoding symmetric message", e);
                ctx.close();
                return;
            }
            UaTransportRequest request = pending.remove(requestId);
            try {
                UaResponseMessage response = (UaResponseMessage) binaryDecoder.setBuffer(message).readMessage(null);
                if (request != null) {
                    request.getFuture().complete(response);
                } else {
                    logger.warn("No pending request with requestId={} for {}", requestId, response.getClass().getSimpleName());
                }
            } catch (Throwable t) {
                logger.error("Error decoding UaResponseMessage", t);
                if (request != null) {
                    request.getFuture().completeExceptionally(t);
                }
            } finally {
                message.release();
            }
        });
    }
}
Also used : UaTransportRequest(org.eclipse.milo.opcua.stack.client.transport.UaTransportRequest) ChunkDecoder(org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder) UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) UaException(org.eclipse.milo.opcua.stack.core.UaException) MessageAbortException(org.eclipse.milo.opcua.stack.core.channel.MessageAbortException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) MessageDecodeException(org.eclipse.milo.opcua.stack.core.channel.MessageDecodeException)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)4 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)4 UaException (org.eclipse.milo.opcua.stack.core.UaException)4 ChunkDecoder (org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder)4 MessageAbortException (org.eclipse.milo.opcua.stack.core.channel.MessageAbortException)4 MessageDecodeException (org.eclipse.milo.opcua.stack.core.channel.MessageDecodeException)4 X509Certificate (java.security.cert.X509Certificate)2 AsymmetricSecurityHeader (org.eclipse.milo.opcua.stack.core.channel.headers.AsymmetricSecurityHeader)2 CertificateValidator (org.eclipse.milo.opcua.stack.core.security.CertificateValidator)2 SecurityPolicy (org.eclipse.milo.opcua.stack.core.security.SecurityPolicy)2 UaResponseMessage (org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage)2 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)2 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)2 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)2 InetSocketAddress (java.net.InetSocketAddress)1 KeyPair (java.security.KeyPair)1 UaTransportRequest (org.eclipse.milo.opcua.stack.client.transport.UaTransportRequest)1 UaSerializationException (org.eclipse.milo.opcua.stack.core.UaSerializationException)1 UaServiceFaultException (org.eclipse.milo.opcua.stack.core.UaServiceFaultException)1 ServerSecureChannel (org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel)1