Search in sources :

Example 46 with QpidByteBuffer

use of org.apache.qpid.server.bytebuffer.QpidByteBuffer in project qpid-broker-j by apache.

the class NonBlockingConnection method setTransportEncryption.

public void setTransportEncryption(TransportEncryption transportEncryption) {
    NonBlockingConnectionDelegate oldDelegate = _delegate;
    switch(transportEncryption) {
        case TLS:
            _onTransportEncryptionAction.run();
            _delegate = new NonBlockingConnectionTLSDelegate(this, _port);
            break;
        case NONE:
            _delegate = new NonBlockingConnectionPlainDelegate(this, _port);
            break;
        default:
            throw new IllegalArgumentException("unknown TransportEncryption " + transportEncryption);
    }
    if (oldDelegate != null) {
        try (QpidByteBuffer src = oldDelegate.getNetInputBuffer().duplicate()) {
            src.flip();
            _delegate.getNetInputBuffer().put(src);
        }
        oldDelegate.shutdownInput();
        oldDelegate.shutdownOutput();
    }
    LOGGER.debug("Identified transport encryption as " + transportEncryption);
}
Also used : QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 47 with QpidByteBuffer

use of org.apache.qpid.server.bytebuffer.QpidByteBuffer in project qpid-broker-j by apache.

the class NonBlockingConnectionPlainDelegate method restoreApplicationBufferForWrite.

protected void restoreApplicationBufferForWrite() {
    try (QpidByteBuffer oldNetInputBuffer = _netInputBuffer) {
        int unprocessedDataLength = _netInputBuffer.remaining();
        _netInputBuffer.limit(_netInputBuffer.capacity());
        _netInputBuffer = oldNetInputBuffer.slice();
        _netInputBuffer.limit(unprocessedDataLength);
    }
    if (_netInputBuffer.limit() != _netInputBuffer.capacity()) {
        _netInputBuffer.position(_netInputBuffer.limit());
        _netInputBuffer.limit(_netInputBuffer.capacity());
    } else {
        try (QpidByteBuffer currentBuffer = _netInputBuffer) {
            int newBufSize;
            if (currentBuffer.capacity() < _networkBufferSize) {
                newBufSize = _networkBufferSize;
            } else {
                newBufSize = currentBuffer.capacity() + _networkBufferSize;
                _parent.reportUnexpectedByteBufferSizeUsage();
            }
            _netInputBuffer = QpidByteBuffer.allocateDirect(newBufSize);
            _netInputBuffer.put(currentBuffer);
        }
    }
}
Also used : QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 48 with QpidByteBuffer

use of org.apache.qpid.server.bytebuffer.QpidByteBuffer in project qpid-broker-j by apache.

the class NonBlockingConnectionTLSDelegate method processData.

@Override
public boolean processData() throws IOException {
    if (!_hostChecked) {
        try (QpidByteBuffer buffer = _netInputBuffer.duplicate()) {
            buffer.flip();
            if (SSLUtil.isSufficientToDetermineClientSNIHost(buffer)) {
                String hostName = SSLUtil.getServerNameFromTLSClientHello(buffer);
                if (hostName != null) {
                    _parent.setSelectedHost(hostName);
                    SSLParameters sslParameters = _sslEngine.getSSLParameters();
                    sslParameters.setServerNames(Collections.singletonList(new SNIHostName(hostName)));
                    _sslEngine.setSSLParameters(sslParameters);
                }
                _hostChecked = true;
            } else {
                return false;
            }
        }
    }
    _netInputBuffer.flip();
    boolean readData = false;
    boolean tasksRun;
    int oldNetBufferPos;
    do {
        int oldAppBufPos = _applicationBuffer.position();
        oldNetBufferPos = _netInputBuffer.position();
        _status = QpidByteBuffer.decryptSSL(_sslEngine, _netInputBuffer, _applicationBuffer);
        if (_status.getStatus() == SSLEngineResult.Status.CLOSED) {
            int remaining = _netInputBuffer.remaining();
            _netInputBuffer.position(_netInputBuffer.limit());
            // We'd usually expect no more bytes to be sent following a close_notify
            LOGGER.debug("SSLEngine closed, discarded {} byte(s)", remaining);
        }
        tasksRun = runSSLEngineTasks(_status);
        _applicationBuffer.flip();
        if (_applicationBuffer.position() > oldAppBufPos) {
            readData = true;
        }
        _parent.processAmqpData(_applicationBuffer);
        restoreApplicationBufferForWrite();
    } while ((_netInputBuffer.hasRemaining() && (_netInputBuffer.position() > oldNetBufferPos)) || tasksRun);
    if (_netInputBuffer.hasRemaining()) {
        _netInputBuffer.compact();
    } else {
        _netInputBuffer.clear();
    }
    return readData;
}
Also used : SSLParameters(javax.net.ssl.SSLParameters) SNIHostName(javax.net.ssl.SNIHostName) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 49 with QpidByteBuffer

use of org.apache.qpid.server.bytebuffer.QpidByteBuffer in project qpid-broker-j by apache.

the class NonBlockingConnectionTLSDelegate method doWrite.

@Override
public WriteResult doWrite(Collection<QpidByteBuffer> buffers) throws IOException {
    final int bufCount = buffers.size();
    int totalConsumed = wrapBufferArray(buffers);
    boolean bufsSent = true;
    final Iterator<QpidByteBuffer> itr = buffers.iterator();
    int bufIndex = 0;
    while (itr.hasNext() && bufsSent && bufIndex++ < bufCount) {
        QpidByteBuffer buf = itr.next();
        bufsSent = !buf.hasRemaining();
    }
    if (!_encryptedOutput.isEmpty()) {
        _parent.writeToTransport(_encryptedOutput);
        ListIterator<QpidByteBuffer> iter = _encryptedOutput.listIterator();
        while (iter.hasNext()) {
            QpidByteBuffer buf = iter.next();
            if (!buf.hasRemaining()) {
                buf.dispose();
                iter.remove();
            } else {
                break;
            }
        }
    }
    return new WriteResult(bufsSent && _encryptedOutput.isEmpty(), totalConsumed);
}
Also used : QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 50 with QpidByteBuffer

use of org.apache.qpid.server.bytebuffer.QpidByteBuffer in project qpid-broker-j by apache.

the class NonBlockingConnectionTLSDelegate method wrapBufferArray.

private int wrapBufferArray(Collection<QpidByteBuffer> buffers) throws SSLException {
    int totalConsumed = 0;
    boolean encrypted;
    do {
        if (_sslEngine.getHandshakeStatus() != SSLEngineResult.HandshakeStatus.NEED_UNWRAP) {
            if (_netOutputBuffer.remaining() < _sslEngine.getSession().getPacketBufferSize()) {
                if (_netOutputBuffer.position() != 0) {
                    _netOutputBuffer.flip();
                    _encryptedOutput.add(_netOutputBuffer);
                } else {
                    _netOutputBuffer.dispose();
                }
                _netOutputBuffer = QpidByteBuffer.allocateDirect(_networkBufferSize);
            }
            _status = QpidByteBuffer.encryptSSL(_sslEngine, buffers, _netOutputBuffer);
            encrypted = _status.bytesProduced() > 0;
            totalConsumed += _status.bytesConsumed();
            runSSLEngineTasks(_status);
            if (encrypted && _netOutputBuffer.remaining() < _sslEngine.getSession().getPacketBufferSize()) {
                _netOutputBuffer.flip();
                _encryptedOutput.add(_netOutputBuffer);
                _netOutputBuffer = QpidByteBuffer.allocateDirect(_networkBufferSize);
            }
        } else {
            encrypted = false;
        }
    } while (encrypted && _sslEngine.getHandshakeStatus() != SSLEngineResult.HandshakeStatus.NEED_UNWRAP);
    if (_netOutputBuffer.position() != 0) {
        final QpidByteBuffer outputBuffer = _netOutputBuffer;
        _netOutputBuffer = _netOutputBuffer.slice();
        outputBuffer.flip();
        _encryptedOutput.add(outputBuffer);
    }
    return totalConsumed;
}
Also used : QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Aggregations

QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)185 NamedAddressSpace (org.apache.qpid.server.model.NamedAddressSpace)61 Message_1_0 (org.apache.qpid.server.protocol.v1_0.Message_1_0)61 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)42 Data (org.apache.qpid.server.protocol.v1_0.type.messaging.Data)30 MessageTransferMessage (org.apache.qpid.server.protocol.v0_10.MessageTransferMessage)29 AMQMessage (org.apache.qpid.server.protocol.v0_8.AMQMessage)29 ApplicationProperties (org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties)29 Properties (org.apache.qpid.server.protocol.v1_0.type.messaging.Properties)28 ArrayList (java.util.ArrayList)22 AmqpValue (org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValue)20 Test (org.junit.Test)13 MapToJmsMapMessage (org.apache.qpid.server.typedmessage.mimecontentconverter.MapToJmsMapMessage)12 EncodingRetainingSection (org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection)10 UnsignedInteger (org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger)9 Transfer (org.apache.qpid.server.protocol.v1_0.type.transport.Transfer)9 SpecificationTest (org.apache.qpid.tests.protocol.SpecificationTest)9 ByteBuffer (java.nio.ByteBuffer)8 LinkedHashMap (java.util.LinkedHashMap)8 JmsMapMessageToMap (org.apache.qpid.server.typedmessage.mimecontentconverter.JmsMapMessageToMap)8