Search in sources :

Example 1 with XdrInputStream

use of org.firebirdsql.gds.impl.wire.XdrInputStream in project jaybird by FirebirdSQL.

the class V13WireOperations method enableEncryption.

protected void enableEncryption(EncryptionInitInfo encryptionInitInfo) throws SQLException, IOException {
    final XdrInputStream xdrIn = getXdrIn();
    final XdrOutputStream xdrOut = getXdrOut();
    final Encoding encoding = getEncoding();
    final EncryptionIdentifier encryptionIdentifier = encryptionInitInfo.getEncryptionIdentifier();
    xdrOut.writeInt(op_crypt);
    xdrOut.writeString(encryptionIdentifier.getPluginName(), encoding);
    xdrOut.writeString(encryptionIdentifier.getType(), encoding);
    xdrOut.flush();
    xdrIn.setCipher(encryptionInitInfo.getDecryptionCipher());
    xdrOut.setCipher(encryptionInitInfo.getEncryptionCipher());
    readOperationResponse(readNextOperation(), null);
}
Also used : XdrInputStream(org.firebirdsql.gds.impl.wire.XdrInputStream) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) Encoding(org.firebirdsql.encodings.Encoding) EncryptionIdentifier(org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier)

Example 2 with XdrInputStream

use of org.firebirdsql.gds.impl.wire.XdrInputStream in project jaybird by FirebirdSQL.

the class WireConnection method identify.

/**
 * Performs the connection identification phase of the Wire protocol and
 * returns the FbWireDatabase implementation for the agreed protocol.
 *
 * @return FbWireDatabase
 * @throws SQLTimeoutException
 * @throws SQLException
 */
@Override
public final C identify() throws SQLException {
    try {
        xdrIn = new XdrInputStream(socket.getInputStream());
        xdrOut = new XdrOutputStream(socket.getOutputStream());
        xdrOut.writeInt(op_connect);
        xdrOut.writeInt(op_attach);
        xdrOut.writeInt(CONNECT_VERSION3);
        xdrOut.writeInt(arch_generic);
        xdrOut.writeString(getAttachObjectName(), getEncoding());
        // Count of protocols understood
        xdrOut.writeInt(protocols.getProtocolCount());
        xdrOut.writeBuffer(createUserIdentificationBlock());
        for (ProtocolDescriptor protocol : protocols) {
            // Protocol version
            xdrOut.writeInt(protocol.getVersion());
            // Architecture of client
            xdrOut.writeInt(protocol.getArchitecture());
            // Minimum type
            xdrOut.writeInt(protocol.getMinimumType());
            // Maximum type
            xdrOut.writeInt(protocol.getMaximumType());
            // Preference weight
            xdrOut.writeInt(protocol.getWeight());
        }
        xdrOut.flush();
        final int operation = readNextOperation();
        if (operation == op_accept || operation == op_cond_accept || operation == op_accept_data) {
            FbWireAttachment.AcceptPacket acceptPacket = new FbWireAttachment.AcceptPacket();
            acceptPacket.operation = operation;
            // Protocol version
            protocolVersion = xdrIn.readInt();
            // Architecture for protocol
            protocolArchitecture = xdrIn.readInt();
            // Minimum type
            protocolMinimumType = xdrIn.readInt();
            if (protocolVersion < 0) {
                protocolVersion = (protocolVersion & FB_PROTOCOL_MASK) | FB_PROTOCOL_FLAG;
            }
            if (operation == op_cond_accept || operation == op_accept_data) {
                byte[] data = acceptPacket.p_acpt_data = xdrIn.readBuffer();
                acceptPacket.p_acpt_plugin = xdrIn.readString(getEncoding());
                final int isAuthenticated = xdrIn.readInt();
                byte[] serverKeys = acceptPacket.p_acpt_keys = xdrIn.readBuffer();
                clientAuthBlock.setServerData(data);
                clientAuthBlock.setAuthComplete(isAuthenticated == 1);
                addServerKeys(serverKeys);
                clientAuthBlock.resetClient(serverKeys);
                clientAuthBlock.switchPlugin(acceptPacket.p_acpt_plugin);
            } else {
                clientAuthBlock.resetClient(null);
            }
            ProtocolDescriptor descriptor = protocols.getProtocolDescriptor(protocolVersion);
            if (descriptor == null) {
                throw new SQLException(String.format("Unsupported or unexpected protocol version %d connecting to database %s. Supported version(s): %s", protocolVersion, getServerName(), protocols.getProtocolVersions()));
            }
            C connectionHandle = createConnectionHandle(descriptor);
            if (operation == op_cond_accept) {
                connectionHandle.authReceiveResponse(acceptPacket);
            }
            return connectionHandle;
        } else {
            try {
                if (operation == op_response) {
                    // Handle exception from response
                    AbstractWireOperations wireOperations = getDefaultWireOperations();
                    wireOperations.processResponse(wireOperations.processOperation(operation));
                }
            } finally {
                try {
                    close();
                } catch (Exception ex) {
                    log.debug("Ignoring exception on disconnect in connect phase of protocol", ex);
                }
            }
            throw new FbExceptionBuilder().exception(ISCConstants.isc_connect_reject).toFlatSQLException();
        }
    } catch (SocketTimeoutException ste) {
        throw new FbExceptionBuilder().timeoutException(ISCConstants.isc_network_error).messageParameter(getServerName()).cause(ste).toSQLException();
    } catch (IOException ioex) {
        throw new FbExceptionBuilder().exception(ISCConstants.isc_network_error).messageParameter(getServerName()).cause(ioex).toSQLException();
    }
}
Also used : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException) SQLTimeoutException(java.sql.SQLTimeoutException) SQLException(java.sql.SQLException) IOException(java.io.IOException) XdrInputStream(org.firebirdsql.gds.impl.wire.XdrInputStream)

Example 3 with XdrInputStream

use of org.firebirdsql.gds.impl.wire.XdrInputStream in project jaybird by FirebirdSQL.

the class V13Statement method readSqlData.

/**
 * Reads a single row from the database.
 *
 * @return Row as a list of {@link FieldValue} instances
 * @throws SQLException
 * @throws IOException
 */
protected RowValue readSqlData() throws SQLException, IOException {
    final RowDescriptor rowDescriptor = getFieldDescriptor();
    final RowValue rowValue = rowDescriptor.createDefaultFieldValues();
    final BlrCalculator blrCalculator = getDatabase().getBlrCalculator();
    final XdrInputStream xdrIn = getXdrIn();
    final int nullBitsLen = (rowDescriptor.getCount() + 7) / 8;
    final byte[] nullBitsBytes = xdrIn.readRawBuffer(nullBitsLen);
    xdrIn.skipPadding(nullBitsLen);
    final BitSet nullBits = BitSet.valueOf(nullBitsBytes);
    for (int idx = 0; idx < rowDescriptor.getCount(); idx++) {
        final FieldDescriptor fieldDescriptor = rowDescriptor.getFieldDescriptor(idx);
        final FieldValue fieldValue = rowValue.getFieldValue(idx);
        if (nullBits.get(idx)) {
            fieldValue.setFieldData(null);
            continue;
        }
        final int len = blrCalculator.calculateIoLength(fieldDescriptor);
        final byte[] buffer = readColumnData(xdrIn, len);
        fieldValue.setFieldData(buffer);
    }
    return rowValue;
}
Also used : XdrInputStream(org.firebirdsql.gds.impl.wire.XdrInputStream) BitSet(java.util.BitSet)

Example 4 with XdrInputStream

use of org.firebirdsql.gds.impl.wire.XdrInputStream in project jaybird by FirebirdSQL.

the class V13WireOperations method authReceiveResponse.

@Override
public void authReceiveResponse(FbWireAttachment.AcceptPacket acceptPacket, FbWireOperations.ProcessAttachCallback processAttachCallback) throws SQLException, IOException {
    assert acceptPacket == null || acceptPacket.operation == op_cond_accept : "Unexpected operation in AcceptPacket";
    final XdrInputStream xdrIn = getXdrIn();
    final XdrOutputStream xdrOut = getXdrOut();
    final ClientAuthBlock clientAuthBlock = getClientAuthBlock();
    final Encoding encoding = getEncoding();
    while (true) {
        String pluginName;
        byte[] data;
        if (acceptPacket != null) {
            data = acceptPacket.p_acpt_data;
            pluginName = acceptPacket.p_acpt_plugin;
            addServerKeys(acceptPacket.p_acpt_keys);
            log.debug(String.format("authReceiveResponse: cond_accept data=%d pluginName=%d '%s'", data.length, pluginName != null ? pluginName.length() : null, pluginName));
            // TODO handle compression
            acceptPacket = null;
        } else {
            int operation = readNextOperation();
            switch(operation) {
                case op_trusted_auth:
                    // p_trau_data
                    xdrIn.readBuffer();
                    throw new FbExceptionBuilder().nonTransientConnectionException(JaybirdErrorCodes.jb_receiveTrustedAuth_NotSupported).toFlatSQLException();
                case op_cont_auth:
                    // p_data
                    data = xdrIn.readBuffer();
                    // p_name
                    pluginName = xdrIn.readString(encoding);
                    // p_list (ignore?)
                    xdrIn.readBuffer();
                    // p_keys
                    addServerKeys(xdrIn.readBuffer());
                    log.debug(String.format("authReceiveResponse: cont_auth data=%d pluginName=%d '%s'", data.length, pluginName.length(), pluginName));
                    break;
                case op_cond_accept:
                    // Note this is the equivalent of handling the acceptPacket != null above
                    // p_acpt_version
                    xdrIn.readInt();
                    // p_acpt_architecture
                    xdrIn.readInt();
                    // p_acpt_type
                    xdrIn.readInt();
                    // p_acpt_data
                    data = xdrIn.readBuffer();
                    // p_acpt_plugin
                    pluginName = xdrIn.readString(encoding);
                    // p_acpt_authenticated
                    xdrIn.readInt();
                    // p_acpt_keys
                    addServerKeys(xdrIn.readBuffer());
                    log.debug(String.format("authReceiveResponse: cond_accept data=%d pluginName=%d '%s'", data.length, pluginName.length(), pluginName));
                    // TODO handle compression
                    break;
                case op_response:
                    GenericResponse response = (GenericResponse) readOperationResponse(operation, null);
                    boolean wasAuthComplete = clientAuthBlock.isAuthComplete();
                    clientAuthBlock.setAuthComplete(true);
                    processAttachCallback.processAttachResponse(response);
                    addServerKeys(response.getData());
                    WireCrypt wireCrypt = getAttachProperties().getWireCrypt();
                    if (!wasAuthComplete && wireCrypt != WireCrypt.DISABLED) {
                        tryKnownServerKeys();
                    }
                    return;
                default:
                    throw new SQLException(String.format("Unsupported operation code: %d", operation));
            }
        }
        if (pluginName != null && pluginName.length() > 0 && Objects.equals(pluginName, clientAuthBlock.getCurrentPluginName())) {
            pluginName = null;
        }
        if (pluginName != null && pluginName.length() > 0) {
            if (!clientAuthBlock.switchPlugin(pluginName)) {
                break;
            }
        }
        if (!clientAuthBlock.hasPlugin()) {
            break;
        }
        clientAuthBlock.setServerData(data);
        log.debug(String.format("receiveResponse: authenticate(%s)", clientAuthBlock.getCurrentPluginName()));
        clientAuthBlock.authenticate();
        xdrOut.writeInt(op_cont_auth);
        // TODO Move to ClientAuthBlock?
        // p_data
        xdrOut.writeBuffer(clientAuthBlock.getClientData());
        // p_name
        xdrOut.writeString(clientAuthBlock.getCurrentPluginName(), encoding);
        if (clientAuthBlock.isFirstTime()) {
            // p_list
            xdrOut.writeString(clientAuthBlock.getPluginNames(), encoding);
            clientAuthBlock.setFirstTime(false);
        } else {
            // p_list
            xdrOut.writeBuffer(null);
        }
        // p_keys
        xdrOut.writeBuffer(null);
        xdrOut.flush();
    }
    // If we have exited from the cycle, this mean auth failed
    throw new FbExceptionBuilder().exception(ISCConstants.isc_login).toFlatSQLException();
}
Also used : XdrInputStream(org.firebirdsql.gds.impl.wire.XdrInputStream) GenericResponse(org.firebirdsql.gds.ng.wire.GenericResponse) SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) Encoding(org.firebirdsql.encodings.Encoding) WireCrypt(org.firebirdsql.gds.ng.WireCrypt) ClientAuthBlock(org.firebirdsql.gds.ng.wire.auth.ClientAuthBlock)

Example 5 with XdrInputStream

use of org.firebirdsql.gds.impl.wire.XdrInputStream in project jaybird by FirebirdSQL.

the class V10Statement method readSqlData.

/**
 * Reads a single row from the database.
 *
 * @return Row as a list of {@link FieldValue} instances
 * @throws SQLException
 * @throws IOException
 */
protected RowValue readSqlData() throws SQLException, IOException {
    final RowDescriptor rowDescriptor = getFieldDescriptor();
    final RowValue rowValue = rowDescriptor.createDefaultFieldValues();
    final BlrCalculator blrCalculator = getDatabase().getBlrCalculator();
    final XdrInputStream xdrIn = getXdrIn();
    for (int idx = 0; idx < rowDescriptor.getCount(); idx++) {
        final FieldDescriptor fieldDescriptor = rowDescriptor.getFieldDescriptor(idx);
        final FieldValue fieldValue = rowValue.getFieldValue(idx);
        final int len = blrCalculator.calculateIoLength(fieldDescriptor);
        byte[] buffer = readColumnData(xdrIn, len);
        if (xdrIn.readInt() == NULL_INDICATOR_NULL)
            buffer = null;
        fieldValue.setFieldData(buffer);
    }
    return rowValue;
}
Also used : XdrInputStream(org.firebirdsql.gds.impl.wire.XdrInputStream)

Aggregations

XdrInputStream (org.firebirdsql.gds.impl.wire.XdrInputStream)6 XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)3 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Encoding (org.firebirdsql.encodings.Encoding)2 SQLTimeoutException (java.sql.SQLTimeoutException)1 BitSet (java.util.BitSet)1 WireCrypt (org.firebirdsql.gds.ng.WireCrypt)1 GenericResponse (org.firebirdsql.gds.ng.wire.GenericResponse)1 ClientAuthBlock (org.firebirdsql.gds.ng.wire.auth.ClientAuthBlock)1 EncryptionIdentifier (org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier)1