Search in sources :

Example 6 with DbCryptCallback

use of org.firebirdsql.gds.ng.dbcrypt.DbCryptCallback 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());
            if (protocol.supportsWireCompression() && attachProperties.isWireCompression()) {
                xdrOut.writeInt(protocol.getMaximumType() | pflag_compress);
            } else {
                // Maximum type
                xdrOut.writeInt(protocol.getMaximumType());
            }
            // Preference weight
            xdrOut.writeInt(protocol.getWeight());
        }
        xdrOut.flush();
        FbWireOperations cryptKeyCallbackWireOperations = null;
        DbCryptCallback dbCryptCallback = null;
        int operation = readNextOperation();
        while (operation == op_crypt_key_callback) {
            if (cryptKeyCallbackWireOperations == null) {
                cryptKeyCallbackWireOperations = getCryptKeyCallbackWireOperations();
            }
            if (dbCryptCallback == null) {
                dbCryptCallback = createDbCryptCallback();
            }
            cryptKeyCallbackWireOperations.handleCryptKeyCallback(dbCryptCallback);
            operation = readNextOperation();
        }
        if (operation == op_accept || operation == op_cond_accept || operation == op_accept_data) {
            FbWireAttachment.AcceptPacket acceptPacket = new FbWireAttachment.AcceptPacket();
            acceptPacket.operation = operation;
            // p_acpt_version - Protocol version
            protocolVersion = xdrIn.readInt();
            // p_acpt_architecture - Architecture for protocol
            protocolArchitecture = xdrIn.readInt();
            // p_acpt_type - Minimum type
            int acceptType = xdrIn.readInt();
            protocolMinimumType = acceptType & ptype_MASK;
            final boolean compress = (acceptType & pflag_compress) != 0;
            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);
            }
            if (compress) {
                xdrOut.enableCompression();
                xdrIn.enableDecompression();
            }
            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) DbCryptCallback(org.firebirdsql.gds.ng.dbcrypt.DbCryptCallback)

Example 7 with DbCryptCallback

use of org.firebirdsql.gds.ng.dbcrypt.DbCryptCallback in project jaybird by FirebirdSQL.

the class StaticValueDbCryptCallbackTest method returnsReplyWithFixedResponseValue_null.

@Test
public void returnsReplyWithFixedResponseValue_null() {
    DbCryptCallback callback = new StaticValueDbCryptCallback(null);
    DbCryptData dbCryptData = callback.handleCallback(DbCryptData.EMPTY_DATA);
    assertNull("pluginData", dbCryptData.getPluginData());
    assertEquals("replySize", 0, dbCryptData.getReplySize());
}
Also used : DbCryptData(org.firebirdsql.gds.ng.dbcrypt.DbCryptData) DbCryptCallback(org.firebirdsql.gds.ng.dbcrypt.DbCryptCallback) Test(org.junit.Test)

Example 8 with DbCryptCallback

use of org.firebirdsql.gds.ng.dbcrypt.DbCryptCallback in project jaybird by FirebirdSQL.

the class StaticValueDbCryptCallbackSpiTest method fixedResponseWithBase64urlConfig.

@Test
public void fixedResponseWithBase64urlConfig() {
    final DbCryptCallback dbCryptCallback = new StaticValueDbCryptCallbackSpi().createDbCryptCallback("base64url:PDw_Pz8-Pg==");
    DbCryptData dbCryptData = dbCryptCallback.handleCallback(DbCryptData.EMPTY_DATA);
    assertArrayEquals("<<???>>".getBytes(StandardCharsets.US_ASCII), dbCryptData.getPluginData());
}
Also used : DbCryptData(org.firebirdsql.gds.ng.dbcrypt.DbCryptData) DbCryptCallback(org.firebirdsql.gds.ng.dbcrypt.DbCryptCallback) Test(org.junit.Test)

Example 9 with DbCryptCallback

use of org.firebirdsql.gds.ng.dbcrypt.DbCryptCallback in project jaybird by FirebirdSQL.

the class StaticValueDbCryptCallbackSpiTest method fixedResponseWithNullConfig.

@Test
public void fixedResponseWithNullConfig() {
    final DbCryptCallback dbCryptCallback = new StaticValueDbCryptCallbackSpi().createDbCryptCallback(null);
    DbCryptData dbCryptData = dbCryptCallback.handleCallback(DbCryptData.EMPTY_DATA);
    assertNull(dbCryptData.getPluginData());
}
Also used : DbCryptData(org.firebirdsql.gds.ng.dbcrypt.DbCryptData) DbCryptCallback(org.firebirdsql.gds.ng.dbcrypt.DbCryptCallback) Test(org.junit.Test)

Aggregations

DbCryptCallback (org.firebirdsql.gds.ng.dbcrypt.DbCryptCallback)9 DbCryptData (org.firebirdsql.gds.ng.dbcrypt.DbCryptData)6 Test (org.junit.Test)6 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 XdrInputStream (org.firebirdsql.gds.impl.wire.XdrInputStream)1 XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)1 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)1