Search in sources :

Example 1 with NullCryptor

use of org.teiid.core.crypto.NullCryptor in project teiid by teiid.

the class TestCommSockets method testConnectWithoutClientEncryption.

@Test
public void testConnectWithoutClientEncryption() throws Exception {
    SSLConfiguration config = new SSLConfiguration();
    config.setMode(SSLConfiguration.DISABLED);
    SocketServerConnection conn = helpEstablishConnection(false, config, new Properties());
    assertTrue(conn.selectServerInstance(false).getCryptor() instanceof NullCryptor);
    conn.close();
}
Also used : Properties(java.util.Properties) SocketServerConnection(org.teiid.net.socket.SocketServerConnection) NullCryptor(org.teiid.core.crypto.NullCryptor) Test(org.junit.Test)

Example 2 with NullCryptor

use of org.teiid.core.crypto.NullCryptor in project teiid by teiid.

the class TestSocketServerConnection method createConnection.

private SocketServerConnection createConnection(final Throwable t, final HostInfo hostInfo, Properties p) throws CommunicationException, ConnectionException {
    ServerDiscovery discovery = new UrlServerDiscovery(new TeiidURL(hostInfo.getHostName(), hostInfo.getPortNumber(), false));
    SocketServerInstanceFactory instanceFactory = new SocketServerInstanceFactory() {

        FakeILogon logon = new FakeILogon(t);

        @Override
        public SocketServerInstance getServerInstance(HostInfo info) throws CommunicationException, IOException {
            SocketServerInstance instance = Mockito.mock(SocketServerInstance.class);
            Mockito.stub(instance.getCryptor()).toReturn(new NullCryptor());
            Mockito.stub(instance.getHostInfo()).toReturn(hostInfo);
            Mockito.stub(instance.getService(ILogon.class)).toReturn(logon);
            Mockito.stub(instance.getServerVersion()).toReturn("07.03");
            if (t != null) {
                try {
                    Mockito.doAnswer(new Answer<Void>() {

                        @Override
                        public Void answer(InvocationOnMock invocation) throws Throwable {
                            if (logon.t == null) {
                                return null;
                            }
                            throw logon.t;
                        }
                    }).when(instance).send((Message) Mockito.anyObject(), (ResultsReceiver<Object>) Mockito.anyObject(), (Serializable) Mockito.anyObject());
                } catch (Exception e) {
                }
            }
            Mockito.stub(instance.isOpen()).toReturn(true);
            return instance;
        }

        @Override
        public void connected(SocketServerInstance instance, SessionToken session) {
        }

        @Override
        public void disconnected(SocketServerInstance instance, SessionToken session) {
        }
    };
    SocketServerConnection connection = new SocketServerConnection(instanceFactory, false, discovery, p);
    return connection;
}
Also used : SessionToken(org.teiid.client.security.SessionToken) TeiidURL(org.teiid.net.TeiidURL) NullCryptor(org.teiid.core.crypto.NullCryptor) InvalidSessionException(org.teiid.client.security.InvalidSessionException) TeiidComponentException(org.teiid.core.TeiidComponentException) CommunicationException(org.teiid.net.CommunicationException) IOException(java.io.IOException) LogonException(org.teiid.client.security.LogonException) ConnectionException(org.teiid.net.ConnectionException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HostInfo(org.teiid.net.HostInfo)

Example 3 with NullCryptor

use of org.teiid.core.crypto.NullCryptor in project teiid by teiid.

the class SocketServerInstanceImpl method doHandshake.

private void doHandshake() throws IOException, CommunicationException {
    Handshake handshake = null;
    boolean sentInit = false;
    long handShakeRetries = 1;
    if (this.soTimeout > 0) {
        handShakeRetries = Math.max(1, synchTimeout / this.soTimeout);
    }
    for (int i = 0; i < handShakeRetries; i++) {
        try {
            Object obj = this.socketChannel.read();
            if (!(obj instanceof Handshake)) {
                throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20009, null, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20009));
            }
            handshake = (Handshake) obj;
            break;
        } catch (ClassNotFoundException e1) {
            throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20010, e1, e1.getMessage());
        } catch (SocketTimeoutException e) {
            if (!sentInit && !this.info.isSsl()) {
                // write a dummy initialization value - if the server is actually ssl, this can cause the server side handshake to fail, otherwise it's ignored
                // TODO: could always do this initialization in the non-ssl case and not wait for a timeout
                this.socketChannel.write(null);
                sentInit = true;
            }
            if (i == handShakeRetries - 1) {
                throw e;
            }
        } catch (IOException e) {
            if (sentInit && !this.info.isSsl()) {
                throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20032, e, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20032));
            }
            throw e;
        }
    }
    try {
        /*if (!getVersionInfo().equals(handshake.getVersion())) {
                 throw new CommunicationException(JDBCPlugin.Event.TEIID20011, NetPlugin.Util.getString(JDBCPlugin.Event.TEIID20011, getVersionInfo(), handshake.getVersion()));
            }*/
        serverVersion = handshake.getVersion();
        handshake.setVersion();
        byte[] serverPublicKey = handshake.getPublicKey();
        byte[] serverPublicKeyLarge = handshake.getPublicKeyLarge();
        if (serverPublicKey != null) {
            DhKeyGenerator keyGen = new DhKeyGenerator();
            boolean large = false;
            if (serverPublicKeyLarge != null) {
                try {
                    byte[] publicKey = keyGen.createPublicKey(true);
                    handshake.setPublicKey(null);
                    handshake.setPublicKeyLarge(publicKey);
                    serverPublicKey = serverPublicKeyLarge;
                    large = true;
                } catch (CryptoException e) {
                // not supported on this platform
                }
            }
            if (!large) {
                byte[] publicKey = keyGen.createPublicKey(false);
                handshake.setPublicKey(publicKey);
                handshake.setPublicKeyLarge(null);
            }
            boolean useCbc = handshake.isCbc();
            // $NON-NLS-1$
            this.cryptor = keyGen.getSymmetricCryptor(serverPublicKey, "08.03".compareTo(serverVersion) > 0, this.getClass().getClassLoader(), large, useCbc);
        } else {
            this.cryptor = new NullCryptor();
        }
        this.socketChannel.write(handshake);
    } catch (CryptoException e) {
        throw new CommunicationException(JDBCPlugin.Event.TEIID20012, e, e.getMessage());
    }
}
Also used : CommunicationException(org.teiid.net.CommunicationException) IOException(java.io.IOException) NullCryptor(org.teiid.core.crypto.NullCryptor) SocketTimeoutException(java.net.SocketTimeoutException) DhKeyGenerator(org.teiid.core.crypto.DhKeyGenerator) CryptoException(org.teiid.core.crypto.CryptoException)

Example 4 with NullCryptor

use of org.teiid.core.crypto.NullCryptor in project teiid by teiid.

the class SocketClientInstance method receivedHahdshake.

private void receivedHahdshake(Handshake handshake) throws CommunicationException {
    String clientVersion = handshake.getVersion();
    this.workContext.setClientVersion(Version.getVersion(clientVersion));
    if (usingEncryption) {
        byte[] returnedPublicKey = handshake.getPublicKey();
        byte[] returnedPublicKeyLarge = handshake.getPublicKeyLarge();
        boolean large = false;
        // ensure the key information
        if (returnedPublicKey == null) {
            if (returnedPublicKeyLarge == null) {
                throw new CommunicationException(RuntimePlugin.Event.TEIID40052, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40052));
            }
            large = true;
            returnedPublicKey = returnedPublicKeyLarge;
        }
        if (LogManager.isMessageToBeRecorded(LogConstants.CTX_TRANSPORT, MessageLevel.DETAIL)) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            LogManager.logDetail(LogConstants.CTX_TRANSPORT, large ? "2048" : "1024", "key exchange being used.");
        }
        boolean useCbc = handshake.isCbc();
        try {
            // $NON-NLS-1$
            this.cryptor = keyGen.getSymmetricCryptor(returnedPublicKey, "08.03".compareTo(clientVersion) > 0, SocketClientInstance.class.getClassLoader(), large, useCbc);
        } catch (CryptoException e) {
            throw new CommunicationException(RuntimePlugin.Event.TEIID40053, e);
        }
        this.keyGen = null;
    } else {
        this.cryptor = new NullCryptor();
    }
}
Also used : CommunicationException(org.teiid.net.CommunicationException) CryptoException(org.teiid.core.crypto.CryptoException) NullCryptor(org.teiid.core.crypto.NullCryptor)

Aggregations

NullCryptor (org.teiid.core.crypto.NullCryptor)4 CommunicationException (org.teiid.net.CommunicationException)3 IOException (java.io.IOException)2 CryptoException (org.teiid.core.crypto.CryptoException)2 SocketTimeoutException (java.net.SocketTimeoutException)1 Properties (java.util.Properties)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 InvalidSessionException (org.teiid.client.security.InvalidSessionException)1 LogonException (org.teiid.client.security.LogonException)1 SessionToken (org.teiid.client.security.SessionToken)1 TeiidComponentException (org.teiid.core.TeiidComponentException)1 DhKeyGenerator (org.teiid.core.crypto.DhKeyGenerator)1 ConnectionException (org.teiid.net.ConnectionException)1 HostInfo (org.teiid.net.HostInfo)1 TeiidURL (org.teiid.net.TeiidURL)1 SocketServerConnection (org.teiid.net.socket.SocketServerConnection)1